root/drivers/char/tty_io.c

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

DEFINITIONS

This source file includes following definitions.
  1. tty_register_ldisc
  2. put_tty_queue
  3. get_tty_queue
  4. tty_read_raw_data
  5. tty_write_flush
  6. tty_read_flush
  7. hung_up_tty_read
  8. hung_up_tty_write
  9. hung_up_tty_select
  10. hung_up_tty_ioctl
  11. tty_lseek
  12. do_tty_hangup
  13. tty_hangup
  14. tty_vhangup
  15. tty_hung_up_p
  16. disassociate_ctty
  17. vt_waitactive
  18. complete_change_console
  19. change_console
  20. wait_for_keypress
  21. copy_to_cooked
  22. is_ignored
  23. wait_for_canon_input
  24. read_chan
  25. __wait_for_canon_input
  26. available_canon_input
  27. write_chan
  28. tty_read
  29. tty_write
  30. init_dev
  31. release_dev
  32. tty_open
  33. tty_release
  34. tty_select
  35. do_SAK
  36. tty_write_data
  37. tty_bh_routine
  38. initialize_tty_struct
  39. initialize_termios
  40. tty_init

   1 /*
   2  *  linux/kernel/tty_io.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
   9  * or rs-channels. It also implements echoing, cooked mode etc.
  10  *
  11  * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
  12  *
  13  * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
  14  * tty_struct and tty_queue structures.  Previously there was a array
  15  * of 256 tty_struct's which was statically allocated, and the
  16  * tty_queue structures were allocated at boot time.  Both are now
  17  * dynamically allocated only when the tty is open.
  18  *
  19  * Also restructured routines so that there is more of a separation
  20  * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
  21  * the low-level tty routines (serial.c, pty.c, console.c).  This
  22  * makes for cleaner and more compact code.  -TYT, 9/17/92 
  23  *
  24  * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
  25  * which can be dynamically activated and de-activated by the line
  26  * discipline handling modules (like SLIP).
  27  *
  28  * NOTE: pay no attention to the line discpline code (yet); its
  29  * interface is still subject to change in this version...
  30  * -- TYT, 1/31/92
  31  *
  32  * Added functionality to the OPOST tty handling.  No delays, but all
  33  * other bits should be there.
  34  *      -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
  35  */
  36 
  37 #include <linux/types.h>
  38 #include <linux/major.h>
  39 #include <linux/errno.h>
  40 #include <linux/signal.h>
  41 #include <linux/fcntl.h>
  42 #include <linux/sched.h>
  43 #include <linux/tty.h>
  44 #include <linux/timer.h>
  45 #include <linux/ctype.h>
  46 #include <linux/kd.h>
  47 #include <linux/mm.h>
  48 #include <linux/string.h>
  49 #include <linux/keyboard.h>
  50 #include <linux/malloc.h>
  51 
  52 #include <asm/segment.h>
  53 #include <asm/system.h>
  54 #include <asm/bitops.h>
  55 
  56 #include "vt_kern.h"
  57 
  58 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
  59 
  60 #define MAX_TTYS 256
  61 
  62 struct tty_struct *tty_table[MAX_TTYS];
  63 struct termios *tty_termios[MAX_TTYS];  /* We need to keep the termios state */
  64                                         /* around, even when a tty is closed */
  65 struct termios *termios_locked[MAX_TTYS]; /* Bitfield of locked termios flags*/
  66 struct tty_ldisc ldiscs[NR_LDISCS];     /* line disc dispatch table     */
  67 int tty_check_write[MAX_TTYS/32];       /* bitfield for the bh handler */
  68 
  69 /*
  70  * fg_console is the current virtual console,
  71  * redirect is the pseudo-tty that console output
  72  * is redirected to if asked by TIOCCONS.
  73  */
  74 int fg_console = 0;
  75 struct tty_struct * redirect = NULL;
  76 struct wait_queue * keypress_wait = NULL;
  77 
  78 static void initialize_tty_struct(int line, struct tty_struct *tty);
  79 static void initialize_termios(int line, struct termios *tp);
  80 
  81 static int tty_read(struct inode *, struct file *, char *, int);
  82 static int tty_write(struct inode *, struct file *, char *, int);
  83 static int tty_select(struct inode *, struct file *, int, select_table *);
  84 static int tty_open(struct inode *, struct file *);
  85 static void tty_release(struct inode *, struct file *);
  86 
  87 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
     /* [previous][next][first][last][top][bottom][index][help] */
  88 {
  89         if (disc < N_TTY || disc >= NR_LDISCS)
  90                 return -EINVAL;
  91         
  92         if (new_ldisc) {
  93                 ldiscs[disc] = *new_ldisc;
  94                 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
  95         } else
  96                 memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
  97         
  98         return 0;
  99 }
 100 
 101 void put_tty_queue(char c, struct tty_queue * queue)
     /* [previous][next][first][last][top][bottom][index][help] */
 102 {
 103         int head;
 104         unsigned long flags;
 105 
 106         save_flags(flags);
 107         cli();
 108         head = (queue->head + 1) & (TTY_BUF_SIZE-1);
 109         if (head != queue->tail) {
 110                 queue->buf[queue->head] = c;
 111                 queue->head = head;
 112         }
 113         restore_flags(flags);
 114 }
 115 
 116 int get_tty_queue(struct tty_queue * queue)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118         int result = -1;
 119         unsigned long flags;
 120 
 121         save_flags(flags);
 122         cli();
 123         if (queue->tail != queue->head) {
 124                 result = 0xff & queue->buf[queue->tail];
 125                 queue->tail = (queue->tail + 1) & (TTY_BUF_SIZE-1);
 126         }
 127         restore_flags(flags);
 128         return result;
 129 }
 130 
 131 /*
 132  * This routine copies out a maximum of buflen characters from the
 133  * read_q; it is a convenience for line disciplines so they can grab a
 134  * large block of data without calling get_tty_char directly.  It
 135  * returns the number of characters actually read. Return terminates
 136  * if an error character is read from the queue and the return value
 137  * is negated.
 138  */
 139 int tty_read_raw_data(struct tty_struct *tty, unsigned char *bufp, int buflen)
     /* [previous][next][first][last][top][bottom][index][help] */
 140 {
 141         int     result = 0;
 142         unsigned char   *p = bufp;
 143         unsigned long flags;
 144         int head, tail;
 145         int ok = 1;
 146 
 147         save_flags(flags);
 148         cli();
 149         tail = tty->read_q.tail;
 150         head = tty->read_q.head;
 151         while ((result < buflen) && (tail!=head) && ok) {
 152                 ok = !clear_bit (tail, &tty->readq_flags);
 153                 *p++ =  tty->read_q.buf[tail++];
 154                 tail &= TTY_BUF_SIZE-1;
 155                 result++;
 156         }
 157         tty->read_q.tail = tail;
 158         restore_flags(flags);
 159         return (ok) ? result : -result;
 160 }
 161 
 162 
 163 void tty_write_flush(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 164 {
 165         if (!tty->write || EMPTY(&tty->write_q))
 166                 return;
 167         if (set_bit(TTY_WRITE_BUSY,&tty->flags))
 168                 return;
 169         tty->write(tty);
 170         if (!clear_bit(TTY_WRITE_BUSY,&tty->flags))
 171                 printk("tty_write_flush: bit already cleared\n");
 172 }
 173 
 174 void tty_read_flush(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 175 {
 176         if (!tty || EMPTY(&tty->read_q))
 177                 return;
 178         if (set_bit(TTY_READ_BUSY, &tty->flags))
 179                 return;
 180         ldiscs[tty->disc].handler(tty);
 181         if (!clear_bit(TTY_READ_BUSY, &tty->flags))
 182                 printk("tty_read_flush: bit already cleared\n");
 183 }
 184 
 185 static int hung_up_tty_read(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 186 {
 187         return 0;
 188 }
 189 
 190 static int hung_up_tty_write(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 191 {
 192         return -EIO;
 193 }
 194 
 195 static int hung_up_tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 196 {
 197         return 1;
 198 }
 199 
 200 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 201                              unsigned int cmd, unsigned long arg)
 202 {
 203         return -EIO;
 204 }
 205 
 206 static int tty_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
     /* [previous][next][first][last][top][bottom][index][help] */
 207 {
 208         return -ESPIPE;
 209 }
 210 
 211 static struct file_operations tty_fops = {
 212         tty_lseek,
 213         tty_read,
 214         tty_write,
 215         NULL,           /* tty_readdir */
 216         tty_select,
 217         tty_ioctl,
 218         NULL,           /* tty_mmap */
 219         tty_open,
 220         tty_release
 221 };
 222 
 223 static struct file_operations hung_up_tty_fops = {
 224         tty_lseek,
 225         hung_up_tty_read,
 226         hung_up_tty_write,
 227         NULL,           /* hung_up_tty_readdir */
 228         hung_up_tty_select,
 229         hung_up_tty_ioctl,
 230         NULL,           /* hung_up_tty_mmap */
 231         NULL,           /* hung_up_tty_open */
 232         tty_release     /* hung_up_tty_release */
 233 };
 234 
 235 void do_tty_hangup(struct tty_struct * tty, struct file_operations *fops)
     /* [previous][next][first][last][top][bottom][index][help] */
 236 {
 237         int i;
 238         struct file * filp;
 239         struct task_struct *p;
 240         int dev;
 241 
 242         if (!tty)
 243                 return;
 244         dev = MKDEV(TTY_MAJOR,tty->line);
 245         for (filp = first_file, i=0; i<nr_files; i++, filp = filp->f_next) {
 246                 if (!filp->f_count)
 247                         continue;
 248                 if (filp->f_rdev != dev)
 249                         continue;
 250                 if (filp->f_inode && filp->f_inode->i_rdev == CONSOLE_DEV)
 251                         continue;
 252                 if (filp->f_op != &tty_fops)
 253                         continue;
 254                 filp->f_op = fops;
 255         }
 256         flush_input(tty);
 257         flush_output(tty);
 258         wake_up_interruptible(&tty->secondary.proc_list);
 259         if (tty->session > 0)
 260                 kill_sl(tty->session,SIGHUP,1);
 261         tty->session = 0;
 262         tty->pgrp = -1;
 263         for_each_task(p) {
 264                 if (p->tty == tty->line)
 265                         p->tty = -1;
 266         }
 267         if (tty->hangup)
 268                 (tty->hangup)(tty);
 269 }
 270 
 271 void tty_hangup(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 272 {
 273 #ifdef TTY_DEBUG_HANGUP
 274         printk("tty%d hangup...\n", tty->line);
 275 #endif
 276         do_tty_hangup(tty, &hung_up_tty_fops);
 277 }
 278 
 279 void tty_vhangup(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 280 {
 281 #ifdef TTY_DEBUG_HANGUP
 282         printk("tty%d vhangup...\n", tty->line);
 283 #endif
 284         do_tty_hangup(tty, &hung_up_tty_fops);
 285 }
 286 
 287 int tty_hung_up_p(struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
 288 {
 289         return (filp->f_op == &hung_up_tty_fops);
 290 }
 291 
 292 /*
 293  * This function is typically called only by the session leader, when
 294  * it wants to dissassociate itself from its controlling tty.
 295  *
 296  * It performs the following functions:
 297  *      (1)  Sends a SIGHUP to the foreground process group
 298  *      (2)  Clears the tty from being controlling the session
 299  *      (3)  Clears the controlling tty for all processes in the
 300  *              session group.
 301  */
 302 void disassociate_ctty(int priv)
     /* [previous][next][first][last][top][bottom][index][help] */
 303 {
 304         struct tty_struct *tty;
 305         struct task_struct *p;
 306 
 307         if (current->tty >= 0) {
 308                 tty = tty_table[current->tty];
 309                 if (tty) {
 310                         if (tty->pgrp > 0)
 311                                 kill_pg(tty->pgrp, SIGHUP, priv);
 312                         tty->session = 0;
 313                         tty->pgrp = -1;
 314                 } else
 315                         printk("disassociate_ctty: ctty is NULL?!?");
 316         }
 317 
 318         for_each_task(p)
 319                 if (p->session == current->session)
 320                         p->tty = -1;
 321 }
 322 
 323 /*
 324  * Sometimes we want to wait until a particular VT has been activated. We
 325  * do it in a very simple manner. Everybody waits on a single queue and
 326  * get woken up at once. Those that are satisfied go on with their business,
 327  * while those not ready go back to sleep. Seems overkill to add a wait
 328  * to each vt just for this - usually this does nothing!
 329  */
 330 static struct wait_queue *vt_activate_queue = NULL;
 331 
 332 /*
 333  * Sleeps until a vt is activated, or the task is interrupted. Returns
 334  * 0 if activation, -1 if interrupted.
 335  */
 336 int vt_waitactive(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 337 {
 338         interruptible_sleep_on(&vt_activate_queue);
 339         return (current->signal & ~current->blocked) ? -1 : 0;
 340 }
 341 
 342 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
 343 
 344 extern int kill_proc(int pid, int sig, int priv);
 345 
 346 /*
 347  * Performs the back end of a vt switch
 348  */
 349 void complete_change_console(unsigned int new_console)
     /* [previous][next][first][last][top][bottom][index][help] */
 350 {
 351         unsigned char old_vc_mode;
 352 
 353         if (new_console == fg_console || new_console >= NR_CONSOLES)
 354                 return;
 355 
 356         /*
 357          * If we're switching, we could be going from KD_GRAPHICS to
 358          * KD_TEXT mode or vice versa, which means we need to blank or
 359          * unblank the screen later.
 360          */
 361         old_vc_mode = vt_cons[fg_console].vc_mode;
 362         update_screen(new_console);
 363 
 364         /*
 365          * If this new console is under process control, send it a signal
 366          * telling it that it has acquired. Also check if it has died and
 367          * clean up (similar to logic employed in change_console())
 368          */
 369         if (vt_cons[new_console].vt_mode.mode == VT_PROCESS)
 370         {
 371                 /*
 372                  * Send the signal as privileged - kill_proc() will
 373                  * tell us if the process has gone or something else
 374                  * is awry
 375                  */
 376                 if (kill_proc(vt_cons[new_console].vt_pid,
 377                               vt_cons[new_console].vt_mode.acqsig,
 378                               1) != 0)
 379                 {
 380                 /*
 381                  * The controlling process has died, so we revert back to
 382                  * normal operation. In this case, we'll also change back
 383                  * to KD_TEXT mode. I'm not sure if this is strictly correct
 384                  * but it saves the agony when the X server dies and the screen
 385                  * remains blanked due to KD_GRAPHICS! It would be nice to do
 386                  * this outside of VT_PROCESS but there is no single process
 387                  * to account for and tracking tty count may be undesirable.
 388                  */
 389                         vt_cons[new_console].vc_mode = KD_TEXT;
 390                         clr_vc_kbd_flag(kbd_table + new_console, VC_RAW);
 391                         vt_cons[new_console].vt_mode.mode = VT_AUTO;
 392                         vt_cons[new_console].vt_mode.waitv = 0;
 393                         vt_cons[new_console].vt_mode.relsig = 0;
 394                         vt_cons[new_console].vt_mode.acqsig = 0;
 395                         vt_cons[new_console].vt_mode.frsig = 0;
 396                         vt_cons[new_console].vt_pid = -1;
 397                         vt_cons[new_console].vt_newvt = -1;
 398                 }
 399         }
 400 
 401         /*
 402          * We do this here because the controlling process above may have
 403          * gone, and so there is now a new vc_mode
 404          */
 405         if (old_vc_mode != vt_cons[new_console].vc_mode)
 406         {
 407                 if (vt_cons[new_console].vc_mode == KD_TEXT)
 408                         unblank_screen();
 409                 else {
 410                         timer_active &= ~(1<<BLANK_TIMER);
 411                         blank_screen();
 412                 }
 413         }
 414 
 415         /*
 416          * Wake anyone waiting for their VT to activate
 417          */
 418         vt_wake_waitactive();
 419         return;
 420 }
 421 
 422 /*
 423  * Performs the front-end of a vt switch
 424  */
 425 void change_console(unsigned int new_console)
     /* [previous][next][first][last][top][bottom][index][help] */
 426 {
 427         if (new_console == fg_console || new_console >= NR_CONSOLES)
 428                 return;
 429 
 430         /*
 431          * If this vt is in process mode, then we need to handshake with
 432          * that process before switching. Essentially, we store where that
 433          * vt wants to switch to and wait for it to tell us when it's done
 434          * (via VT_RELDISP ioctl).
 435          *
 436          * We also check to see if the controlling process still exists.
 437          * If it doesn't, we reset this vt to auto mode and continue.
 438          * This is a cheap way to track process control. The worst thing
 439          * that can happen is: we send a signal to a process, it dies, and
 440          * the switch gets "lost" waiting for a response; hopefully, the
 441          * user will try again, we'll detect the process is gone (unless
 442          * the user waits just the right amount of time :-) and revert the
 443          * vt to auto control.
 444          */
 445         if (vt_cons[fg_console].vt_mode.mode == VT_PROCESS)
 446         {
 447                 /*
 448                  * Send the signal as privileged - kill_proc() will
 449                  * tell us if the process has gone or something else
 450                  * is awry
 451                  */
 452                 if (kill_proc(vt_cons[fg_console].vt_pid,
 453                               vt_cons[fg_console].vt_mode.relsig,
 454                               1) == 0)
 455                 {
 456                         /*
 457                          * It worked. Mark the vt to switch to and
 458                          * return. The process needs to send us a
 459                          * VT_RELDISP ioctl to complete the switch.
 460                          */
 461                         vt_cons[fg_console].vt_newvt = new_console;
 462                         return;
 463                 }
 464 
 465                 /*
 466                  * The controlling process has died, so we revert back to
 467                  * normal operation. In this case, we'll also change back
 468                  * to KD_TEXT mode. I'm not sure if this is strictly correct
 469                  * but it saves the agony when the X server dies and the screen
 470                  * remains blanked due to KD_GRAPHICS! It would be nice to do
 471                  * this outside of VT_PROCESS but there is no single process
 472                  * to account for and tracking tty count may be undesirable.
 473                  */
 474                 vt_cons[fg_console].vc_mode = KD_TEXT;
 475                 clr_vc_kbd_flag(kbd_table + fg_console, VC_RAW);
 476                 vt_cons[fg_console].vt_mode.mode = VT_AUTO;
 477                 vt_cons[fg_console].vt_mode.waitv = 0;
 478                 vt_cons[fg_console].vt_mode.relsig = 0;
 479                 vt_cons[fg_console].vt_mode.acqsig = 0;
 480                 vt_cons[fg_console].vt_mode.frsig = 0;
 481                 vt_cons[fg_console].vt_pid = -1;
 482                 vt_cons[fg_console].vt_newvt = -1;
 483                 /*
 484                  * Fall through to normal (VT_AUTO) handling of the switch...
 485                  */
 486         }
 487 
 488         /*
 489          * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
 490          */
 491         if (vt_cons[fg_console].vc_mode == KD_GRAPHICS)
 492                 return;
 493 
 494         complete_change_console(new_console);
 495 }
 496 
 497 void wait_for_keypress(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 498 {
 499         sleep_on(&keypress_wait);
 500 }
 501 
 502 void copy_to_cooked(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 503 {
 504         int c, special_flag;
 505         unsigned long flags;
 506 
 507         if (!tty) {
 508                 printk("copy_to_cooked: called with NULL tty\n");
 509                 return;
 510         }
 511         if (!tty->write) {
 512                 printk("copy_to_cooked: tty %d has null write routine\n",
 513                        tty->line);
 514         }
 515         while (1) {
 516                 /*
 517                  * Check to see how much room we have left in the
 518                  * secondary queue.  Send a throttle command or abort
 519                  * if necessary.
 520                  */
 521                 c = LEFT(&tty->secondary);
 522                 if (tty->throttle && (c < SQ_THRESHOLD_LW)
 523                     && !set_bit(TTY_SQ_THROTTLED, &tty->flags))
 524                         tty->throttle(tty, TTY_THROTTLE_SQ_FULL);
 525                 if (c == 0)
 526                         break;
 527                 save_flags(flags); cli();
 528                 if (tty->read_q.tail != tty->read_q.head) {
 529                         c = 0xff & tty->read_q.buf[tty->read_q.tail];
 530                         special_flag = clear_bit(tty->read_q.tail,
 531                                                   &tty->readq_flags);
 532                         tty->read_q.tail = (tty->read_q.tail + 1) &
 533                                 (TTY_BUF_SIZE-1);
 534                         restore_flags(flags);
 535                 } else {
 536                         restore_flags(flags);
 537                         break;
 538                 }
 539                 if (special_flag) {
 540                         tty->char_error = c & 7;
 541                         continue;
 542                 }
 543                 if (tty->char_error) {
 544                         if (tty->char_error == TTY_BREAK) {
 545                                 tty->char_error = 0;
 546                                 if (I_IGNBRK(tty))
 547                                         continue;
 548                                 if (I_PARMRK(tty)) {
 549                                         put_tty_queue('\377', &tty->secondary);
 550                                         put_tty_queue('\0', &tty->secondary);
 551                                 }
 552                                 put_tty_queue('\0', &tty->secondary);
 553                                 continue;
 554                         }
 555                         if (tty->char_error == TTY_OVERRUN) {
 556                                 tty->char_error = 0;
 557                                 printk("tty%d: input overrun\n", tty->line);
 558                                 continue;
 559                         }
 560                         /* Must be a parity or frame error */
 561                         tty->char_error = 0;
 562                         if (I_IGNPAR(tty)) {
 563                                 continue;
 564                         }
 565                         if (I_PARMRK(tty)) {
 566                                 put_tty_queue('\377', &tty->secondary);
 567                                 put_tty_queue('\0', &tty->secondary);
 568                                 put_tty_queue(c, &tty->secondary);
 569                         } else
 570                                 put_tty_queue('\0', &tty->secondary);
 571                         continue;
 572                 }
 573                 if (I_STRP(tty))
 574                         c &= 0x7f;
 575                 else if (I_PARMRK(tty) && (c == '\377'))
 576                         put_tty_queue('\377', &tty->secondary);
 577                 if (c==13) {
 578                         if (I_CRNL(tty))
 579                                 c=10;
 580                         else if (I_NOCR(tty))
 581                                 continue;
 582                 } else if (c==10 && I_NLCR(tty))
 583                         c=13;
 584                 if (I_UCLC(tty))
 585                         c=tolower(c);
 586                 if (c == __DISABLED_CHAR)
 587                         tty->lnext = 1;
 588                 if (L_CANON(tty) && !tty->lnext) {
 589                         if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || c == WERASE_CHAR(tty)) {
 590                                 int seen_alnums =
 591                                   (c == WERASE_CHAR(tty)) ? 0 : -1;
 592                                 int cc;
 593 
 594                                 /* deal with killing in the input line */
 595                                 while(!(EMPTY(&tty->secondary) ||
 596                                         (cc=LAST(&tty->secondary))==10 ||
 597                                         ((EOF_CHAR(tty) != __DISABLED_CHAR) &&
 598                                          (cc==EOF_CHAR(tty))))) {
 599                                         /* if killing just a word, kill all
 600                                            non-alnum chars, then all alnum
 601                                            chars.  */
 602                                         if (seen_alnums >= 0) {
 603                                                 if (isalnum(cc))
 604                                                         seen_alnums++;
 605                                                 else if (seen_alnums)
 606                                                         break;
 607                                         }
 608                                         if (L_ECHO(tty)) {
 609                                                 int ct = 1;
 610                                                 if (cc < 32)
 611                                                   ct = (L_ECHOCTL(tty) ? 2 : 0);
 612                                                 while(ct--) {
 613                                                         put_tty_queue('\b', &tty->write_q);
 614                                                         put_tty_queue(' ', &tty->write_q);
 615                                                         put_tty_queue('\b',&tty->write_q);
 616                                                 }
 617                                         }
 618                                         DEC(tty->secondary.head);
 619                                         if(c == ERASE_CHAR(tty))
 620                                                 break;
 621                                 }
 622                                 continue;
 623                         }
 624                         if (c == LNEXT_CHAR(tty)) {
 625                                 tty->lnext = 1;
 626                                 if (L_ECHO(tty)) {
 627                                         put_tty_queue('^',&tty->write_q);
 628                                         put_tty_queue('\b',&tty->write_q);
 629                                 }
 630                                 continue;
 631                         }
 632                 }
 633                 if (I_IXON(tty) && !tty->lnext) {
 634                         if (c == STOP_CHAR(tty)) {
 635                                 tty->ctrl_status &= ~(TIOCPKT_START);
 636                                 tty->ctrl_status |= TIOCPKT_STOP;
 637                                 if (tty->link)
 638                                         wake_up_interruptible(&tty->link->except_q);
 639                                 tty->stopped=1;
 640                                 if (tty->stop)
 641                                         (tty->stop)(tty);
 642                                 if (IS_A_CONSOLE(tty->line)) {
 643                                         set_vc_kbd_flag(kbd_table + fg_console, VC_SCROLLOCK);
 644                                         set_leds();
 645                                 }
 646                                 continue;
 647                         }
 648                         if (((I_IXANY(tty)) && tty->stopped) ||
 649                             (c == START_CHAR(tty))) {
 650                                 tty->ctrl_status &= ~(TIOCPKT_STOP);
 651                                 tty->ctrl_status |= TIOCPKT_START;
 652                                 tty->stopped=0;
 653                                 if (tty->link)
 654                                         wake_up_interruptible(&tty->link->except_q);
 655                                 if (tty->start)
 656                                         (tty->start)(tty);
 657                                 if (IS_A_CONSOLE(tty->line)) {
 658                                         clr_vc_kbd_flag(kbd_table + fg_console, VC_SCROLLOCK);
 659                                         set_leds();
 660                                 }
 661                                 continue;
 662                         }
 663                 }
 664                 if (L_ISIG(tty) && !tty->lnext) {
 665                         if (c == INTR_CHAR(tty)) {
 666                                 kill_pg(tty->pgrp, SIGINT, 1);
 667                                 if (! _L_FLAG(tty, NOFLSH)) {
 668                                   flush_input(tty);
 669                                   flush_output(tty);
 670                                 }
 671                                 continue;
 672                         }
 673                         if (c == QUIT_CHAR(tty)) {
 674                                 kill_pg(tty->pgrp, SIGQUIT, 1);
 675                                 if (! _L_FLAG(tty, NOFLSH)) {
 676                                   flush_input(tty);
 677                                   flush_output(tty);
 678                                 }
 679                                 continue;
 680                         }
 681                         if (c == SUSPEND_CHAR(tty)) {
 682                                 if (!is_orphaned_pgrp(tty->pgrp)) {
 683                                         kill_pg(tty->pgrp, SIGTSTP, 1);
 684                                         if (! _L_FLAG(tty, NOFLSH)) {
 685                                           flush_input(tty);
 686                                           flush_output(tty);
 687                                         }
 688                                 }
 689                                 continue;
 690                         }
 691                 }
 692                 if (c==10 || (EOF_CHAR(tty) != __DISABLED_CHAR &&
 693                     c==EOF_CHAR(tty)))
 694                         tty->secondary.data++;
 695                 if ((c==10) && (L_ECHO(tty) || (L_CANON(tty) && L_ECHONL(tty)))) {
 696                         put_tty_queue('\n',&tty->write_q);
 697                         put_tty_queue('\r',&tty->write_q);
 698                 } else if (L_ECHO(tty)) {
 699                         if (c<32 && L_ECHOCTL(tty)) {
 700                                 put_tty_queue('^',&tty->write_q);
 701                                 put_tty_queue(c+'A'-1, &tty->write_q);
 702                                 if (EOF_CHAR(tty) != __DISABLED_CHAR &&
 703                                     c==EOF_CHAR(tty) && !tty->lnext) {
 704                                         put_tty_queue('\b',&tty->write_q);
 705                                         put_tty_queue('\b',&tty->write_q);
 706                                 }
 707                         } else
 708                                 put_tty_queue(c, &tty->write_q);
 709                 }
 710                 tty->lnext = 0;
 711                 put_tty_queue(c, &tty->secondary);
 712         }
 713         TTY_WRITE_FLUSH(tty);
 714         if (!EMPTY(&tty->secondary))
 715                 wake_up_interruptible(&tty->secondary.proc_list);
 716         if (tty->write_q.proc_list && LEFT(&tty->write_q) > TTY_BUF_SIZE/2)
 717                 wake_up_interruptible(&tty->write_q.proc_list);
 718         if (tty->throttle && (LEFT(&tty->read_q) >= RQ_THRESHOLD_HW)
 719             && clear_bit(TTY_RQ_THROTTLED, &tty->flags))
 720                 tty->throttle(tty, TTY_THROTTLE_RQ_AVAIL);
 721         if (tty->throttle && (LEFT(&tty->secondary) >= SQ_THRESHOLD_HW)
 722             && clear_bit(TTY_SQ_THROTTLED, &tty->flags))
 723                 tty->throttle(tty, TTY_THROTTLE_SQ_AVAIL);
 724 }
 725 
 726 int is_ignored(int sig)
     /* [previous][next][first][last][top][bottom][index][help] */
 727 {
 728         return ((current->blocked & (1<<(sig-1))) ||
 729                 (current->sigaction[sig-1].sa_handler == SIG_IGN));
 730 }
 731 
 732 static int available_canon_input(struct tty_struct *);
 733 static void __wait_for_canon_input(struct file * file, struct tty_struct *);
 734 
 735 static void wait_for_canon_input(struct file * file, struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 736 {
 737         if (!available_canon_input(tty)) {
 738                 if (current->signal & ~current->blocked)
 739                         return;
 740                 __wait_for_canon_input(file, tty);
 741         }
 742 }
 743 
 744 static int read_chan(struct tty_struct * tty, struct file * file, char * buf, int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 745 {
 746         struct wait_queue wait = { current, NULL };
 747         int c;
 748         char * b=buf;
 749         int minimum,time;
 750 
 751         if (L_CANON(tty))
 752                 minimum = time = current->timeout = 0;
 753         else {
 754                 time = 10L*tty->termios->c_cc[VTIME];
 755                 minimum = tty->termios->c_cc[VMIN];
 756                 if (minimum)
 757                         current->timeout = 0xffffffff;
 758                 else {
 759                         if (time)
 760                                 current->timeout = time + jiffies;
 761                         else
 762                                 current->timeout = 0;
 763                         time = 0;
 764                         minimum = 1;
 765                 }
 766         }
 767         if (file->f_flags & O_NONBLOCK) {
 768                 time = current->timeout = 0;
 769                 if (L_CANON(tty) && !available_canon_input(tty))
 770                                 return -EAGAIN;
 771         } else if (L_CANON(tty)) {
 772                 wait_for_canon_input(file, tty);
 773                 if (current->signal & ~current->blocked)
 774                         return -ERESTARTSYS;
 775         }
 776         if (minimum>nr)
 777                 minimum = nr;
 778 
 779         /* deal with packet mode:  First test for status change */
 780         if (tty->packet && tty->link && tty->link->ctrl_status) {
 781                 put_fs_byte (tty->link->ctrl_status, b);
 782                 tty->link->ctrl_status = 0;
 783                 return 1;
 784         }
 785           
 786         /* now bump the buffer up one. */
 787         if (tty->packet) {
 788                 put_fs_byte (0,b++);
 789                 nr--;
 790                 /* this really shouldn't happen, but we need to 
 791                 put it here. */
 792                 if (nr == 0)
 793                         return 1;
 794         }
 795         add_wait_queue(&tty->secondary.proc_list, &wait);
 796         while (nr>0) {
 797                 if (tty_hung_up_p(file)) {
 798                         file->f_flags &= ~O_NONBLOCK;
 799                         break;  /* force read() to return 0 */
 800                 }
 801                 TTY_READ_FLUSH(tty);
 802                 if (tty->link)
 803                         TTY_WRITE_FLUSH(tty->link);
 804                 while (nr > 0 && ((c = get_tty_queue(&tty->secondary)) >= 0)) {
 805                         if ((EOF_CHAR(tty) != __DISABLED_CHAR &&
 806                              c==EOF_CHAR(tty)) || c==10)
 807                                 tty->secondary.data--;
 808                         if ((EOF_CHAR(tty) != __DISABLED_CHAR &&
 809                              c==EOF_CHAR(tty)) && L_CANON(tty))
 810                                 break;
 811                         put_fs_byte(c,b++);
 812                         nr--;
 813                         if (time)
 814                                 current->timeout = time+jiffies;
 815                         if (c==10 && L_CANON(tty))
 816                                 break;
 817                 };
 818                 wake_up_interruptible(&tty->read_q.proc_list);
 819                 /*
 820                  * If there is enough space in the secondary queue
 821                  * now, let the low-level driver know.
 822                  */
 823                 if (tty->throttle && (LEFT(&tty->secondary) >= SQ_THRESHOLD_HW)
 824                     && clear_bit(TTY_SQ_THROTTLED, &tty->flags))
 825                         tty->throttle(tty, TTY_THROTTLE_SQ_AVAIL);
 826                 if (tty->link) {
 827                         if (IS_A_PTY_MASTER(tty->line)) {
 828                                 if ((tty->flags & (1 << TTY_SLAVE_OPENED))
 829                                     && tty->link->count <= 1) {
 830                                         file->f_flags &= ~O_NONBLOCK;
 831                                         break;
 832                                 }
 833                         } else if (!tty->link->count) {
 834                                 file->f_flags &= ~O_NONBLOCK;
 835                                 break;
 836                         }
 837                 }
 838                 if (b-buf >= minimum || !current->timeout)
 839                         break;
 840                 if (current->signal & ~current->blocked) 
 841                         break;
 842                 TTY_READ_FLUSH(tty);
 843                 if (tty->link)
 844                         TTY_WRITE_FLUSH(tty->link);
 845                 if (!EMPTY(&tty->secondary))
 846                         continue;
 847                 current->state = TASK_INTERRUPTIBLE;
 848                 if (EMPTY(&tty->secondary))
 849                         schedule();
 850                 current->state = TASK_RUNNING;
 851         }
 852         remove_wait_queue(&tty->secondary.proc_list, &wait);
 853         TTY_READ_FLUSH(tty);
 854         if (tty->link && tty->link->write)
 855                 TTY_WRITE_FLUSH(tty->link);
 856         current->timeout = 0;
 857 
 858         /* packet mode sticks in an extra 0.  If that's all we've got,
 859            we should count it a zero bytes. */
 860         if (tty->packet) {
 861                 if ((b-buf) > 1)
 862                         return b-buf;
 863         } else {
 864                 if (b-buf)
 865                         return b-buf;
 866         }
 867 
 868         if (current->signal & ~current->blocked)
 869                 return -ERESTARTSYS;
 870         if (file->f_flags & O_NONBLOCK)
 871                 return -EAGAIN;
 872         if (IS_A_PTY_MASTER(tty->line))
 873                 return -EIO;
 874         return 0;
 875 }
 876 
 877 static void __wait_for_canon_input(struct file * file, struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 878 {
 879         struct wait_queue wait = { current, NULL };
 880 
 881         add_wait_queue(&tty->secondary.proc_list, &wait);
 882         while (1) {
 883                 current->state = TASK_INTERRUPTIBLE;
 884                 if (available_canon_input(tty))
 885                         break;
 886                 if (current->signal & ~current->blocked)
 887                         break;
 888                 if (tty_hung_up_p(file))
 889                         break;
 890                 schedule();
 891         }
 892         current->state = TASK_RUNNING;
 893         remove_wait_queue(&tty->secondary.proc_list, &wait);
 894 }
 895 
 896 static int available_canon_input(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 897 {
 898         TTY_READ_FLUSH(tty);
 899         if (tty->link)
 900                 if (tty->link->count)
 901                         TTY_WRITE_FLUSH(tty->link);
 902                 else
 903                         return 1;
 904         if (FULL(&tty->read_q))
 905                 return 1;
 906         if (tty->secondary.data)
 907                 return 1;
 908         return 0;
 909 }
 910 
 911 static int write_chan(struct tty_struct * tty, struct file * file, char * buf, int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 912 {
 913         struct wait_queue wait = { current, NULL };
 914         char c, *b=buf;
 915 
 916         if (nr < 0)
 917                 return -EINVAL;
 918         if (!nr)
 919                 return 0;
 920         add_wait_queue(&tty->write_q.proc_list, &wait);
 921         while (nr>0) {
 922                 if (current->signal & ~current->blocked)
 923                         break;
 924                 if (tty_hung_up_p(file))
 925                         break;
 926                 if (tty->link && !tty->link->count) {
 927                         send_sig(SIGPIPE,current,0);
 928                         break;
 929                 }
 930                 current->state = TASK_INTERRUPTIBLE;
 931                 if (FULL(&tty->write_q)) {
 932                         TTY_WRITE_FLUSH(tty);
 933                         if (FULL(&tty->write_q))
 934                                 schedule();
 935                         current->state = TASK_RUNNING;
 936                         continue;
 937                 }
 938                 current->state = TASK_RUNNING;
 939                 while (nr>0 && !FULL(&tty->write_q)) {
 940                         c=get_fs_byte(b);
 941                         if (O_POST(tty)) {
 942                                 switch (c) {
 943                                         case '\n':
 944                                                 if (O_NLRET(tty)) {
 945                                                         tty->column = 0;
 946                                                 }
 947                                                 if (O_NLCR(tty)) {
 948                                                         if (!set_bit(TTY_CR_PENDING,&tty->flags)) {
 949                                                                 c = '\r';
 950                                                                 tty->column = 0;
 951                                                                 b--; nr++;
 952                                                         } else {
 953                                                                 clear_bit(TTY_CR_PENDING,&tty->flags);
 954                                                         }
 955                                                 }
 956                                                 break;
 957                                         case '\r':
 958                                                 if (O_NOCR(tty) && tty->column == 0) {
 959                                                         b++; nr--;
 960                                                         continue;
 961                                                 }
 962                                                 if (O_CRNL(tty)) {
 963                                                         c = '\n';
 964                                                         if (O_NLRET(tty))
 965                                                                 tty->column = 0;
 966                                                         break;
 967                                                 }
 968                                                 tty->column = 0;
 969                                                 break;
 970                                         case '\t':
 971                                                 if (O_TABDLY(tty) == XTABS) {
 972                                                         c = ' ';
 973                                                         tty->column++;
 974                                                         if (tty->column % 8 != 0) {
 975                                                                 b--; nr++;
 976                                                         }
 977                                                 }
 978                                                 break;
 979                                         case '\b':
 980                                                 tty->column--;
 981                                                 break;
 982                                         default:
 983                                                 if (O_LCUC(tty))
 984                                                         c = toupper(c);
 985                                                 tty->column++;
 986                                                 break;
 987                                 }
 988                         }
 989                         b++; nr--;
 990                         put_tty_queue(c,&tty->write_q);
 991                 }
 992                 if (need_resched)
 993                         schedule();
 994         }
 995         remove_wait_queue(&tty->write_q.proc_list, &wait);
 996         TTY_WRITE_FLUSH(tty);
 997         if (b-buf)
 998                 return b-buf;
 999         if (tty->link && !tty->link->count)
1000                 return -EPIPE;
1001         if (current->signal & ~current->blocked)
1002                 return -ERESTARTSYS;
1003         return 0;
1004 }
1005 
1006 static int tty_read(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1007 {
1008         int i, dev;
1009         struct tty_struct * tty;
1010 
1011         dev = file->f_rdev;
1012         if (MAJOR(dev) != TTY_MAJOR) {
1013                 printk("tty_read: bad pseudo-major nr #%d\n", MAJOR(dev));
1014                 return -EINVAL;
1015         }
1016         dev = MINOR(dev);
1017         tty = TTY_TABLE(dev);
1018         if (!tty || (tty->flags & (1 << TTY_IO_ERROR)))
1019                 return -EIO;
1020         if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
1021             (tty->pgrp > 0) &&
1022             (current->tty == dev) &&
1023             (tty->pgrp != current->pgrp))
1024                 if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
1025                         return -EIO;
1026                 else {
1027                         (void) kill_pg(current->pgrp, SIGTTIN, 1);
1028                         return -ERESTARTSYS;
1029                 }
1030         if (ldiscs[tty->disc].read)
1031                 i = (ldiscs[tty->disc].read)(tty,file,buf,count);
1032         else
1033                 i = -EIO;
1034         if (i > 0)
1035                 inode->i_atime = CURRENT_TIME;
1036         return i;
1037 }
1038 
1039 static int tty_write(struct inode * inode, struct file * file, char * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1040 {
1041         int dev, i, is_console;
1042         struct tty_struct * tty;
1043 
1044         dev = file->f_rdev;
1045         is_console = (inode->i_rdev == CONSOLE_DEV);
1046         if (MAJOR(dev) != TTY_MAJOR) {
1047                 printk("tty_write: pseudo-major != TTY_MAJOR\n");
1048                 return -EINVAL;
1049         }
1050         dev = MINOR(dev);
1051         if (is_console && redirect)
1052                 tty = redirect;
1053         else
1054                 tty = TTY_TABLE(dev);
1055         if (!tty || !tty->write || (tty->flags & (1 << TTY_IO_ERROR)))
1056                 return -EIO;
1057         if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
1058             (current->tty == dev) && (tty->pgrp != current->pgrp)) {
1059                 if (is_orphaned_pgrp(current->pgrp))
1060                         return -EIO;
1061                 if (!is_ignored(SIGTTOU)) {
1062                         (void) kill_pg(current->pgrp, SIGTTOU, 1);
1063                         return -ERESTARTSYS;
1064                 }
1065         }
1066         if (ldiscs[tty->disc].write)
1067                 i = (ldiscs[tty->disc].write)(tty,file,buf,count);
1068         else
1069                 i = -EIO;
1070         if (i > 0)
1071                 inode->i_mtime = CURRENT_TIME;
1072         return i;
1073 }
1074 
1075 /*
1076  * This is so ripe with races that you should *really* not touch this
1077  * unless you know exactly what you are doing. All the changes have to be
1078  * made atomically, or there may be incorrect pointers all over the place.
1079  */
1080 static int init_dev(int dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1081 {
1082         struct tty_struct *tty, *o_tty;
1083         struct termios *tp, *o_tp, *ltp, *o_ltp;
1084         int retval;
1085         int o_dev;
1086 
1087         o_dev = PTY_OTHER(dev);
1088         tty = o_tty = NULL;
1089         tp = o_tp = NULL;
1090         ltp = o_ltp = NULL;
1091 repeat:
1092         retval = -EAGAIN;
1093         if (IS_A_PTY_MASTER(dev) && tty_table[dev] && tty_table[dev]->count)
1094                 goto end_init;
1095         retval = -ENOMEM;
1096         if (!tty_table[dev] && !tty) {
1097                 if (!(tty = (struct tty_struct*) get_free_page(GFP_KERNEL)))
1098                         goto end_init;
1099                 initialize_tty_struct(dev, tty);
1100                 goto repeat;
1101         }
1102         if (!tty_termios[dev] && !tp) {
1103                 tp = (struct termios *) kmalloc(sizeof(struct termios),
1104                                                 GFP_KERNEL);
1105                 if (!tp)
1106                         goto end_init;
1107                 initialize_termios(dev, tp);
1108                 goto repeat;
1109         }
1110         if (!termios_locked[dev] && !ltp) {
1111                 ltp = (struct termios *) kmalloc(sizeof(struct termios),
1112                                                  GFP_KERNEL);
1113                 if (!ltp)
1114                         goto end_init;
1115                 memset(ltp, 0, sizeof(struct termios));
1116                 goto repeat;
1117         }
1118         if (IS_A_PTY(dev)) {
1119                 if (!tty_table[o_dev] && !o_tty) {
1120                         o_tty = (struct tty_struct *)
1121                                 get_free_page(GFP_KERNEL);
1122                         if (!o_tty)
1123                                 goto end_init;
1124                         initialize_tty_struct(o_dev, o_tty);
1125                         goto repeat;
1126                 }
1127                 if (!tty_termios[o_dev] && !o_tp) {
1128                         o_tp = (struct termios *)
1129                                 kmalloc(sizeof(struct termios), GFP_KERNEL);
1130                         if (!o_tp)
1131                                 goto end_init;
1132                         initialize_termios(o_dev, o_tp);
1133                         goto repeat;
1134                 }
1135                 if (!termios_locked[o_dev] && !o_ltp) {
1136                         o_ltp = (struct termios *)
1137                                 kmalloc(sizeof(struct termios), GFP_KERNEL);
1138                         if (!o_ltp)
1139                                 goto end_init;
1140                         memset(o_ltp, 0, sizeof(struct termios));
1141                         goto repeat;
1142                 }
1143                 
1144         }
1145         /* Now we have allocated all the structures: update all the pointers.. */
1146         if (!tty_termios[dev]) {
1147                 tty_termios[dev] = tp;
1148                 tp = NULL;
1149         }
1150         if (!tty_table[dev]) {
1151                 tty->termios = tty_termios[dev];
1152                 tty_table[dev] = tty;
1153                 tty = NULL;
1154         }
1155         if (!termios_locked[dev]) {
1156                 termios_locked[dev] = ltp;
1157                 ltp = NULL;
1158         }
1159         if (IS_A_PTY(dev)) {
1160                 if (!tty_termios[o_dev]) {
1161                         tty_termios[o_dev] = o_tp;
1162                         o_tp = NULL;
1163                 }
1164                 if (!termios_locked[o_dev]) {
1165                         termios_locked[o_dev] = o_ltp;
1166                         o_ltp = NULL;
1167                 }
1168                 if (!tty_table[o_dev]) {
1169                         o_tty->termios = tty_termios[o_dev];
1170                         tty_table[o_dev] = o_tty;
1171                         o_tty = NULL;
1172                 }
1173                 tty_table[dev]->link = tty_table[o_dev];
1174                 tty_table[o_dev]->link = tty_table[dev];
1175         }
1176         tty_table[dev]->count++;
1177         if (IS_A_PTY_MASTER(dev))
1178                 tty_table[o_dev]->count++;
1179         retval = 0;
1180 end_init:
1181         if (tty)
1182                 free_page((unsigned long) tty);
1183         if (o_tty)
1184                 free_page((unsigned long) o_tty);
1185         if (tp)
1186                 kfree_s(tp, sizeof(struct termios));
1187         if (o_tp)
1188                 kfree_s(o_tp, sizeof(struct termios));
1189         if (ltp)
1190                 kfree_s(ltp, sizeof(struct termios));
1191         if (o_ltp)
1192                 kfree_s(o_ltp, sizeof(struct termios));
1193         return retval;
1194 }
1195 
1196 /*
1197  * Even releasing the tty structures is a tricky business.. We have
1198  * to be very careful that the structures are all released at the
1199  * same time, as interrupts might otherwise get the wrong pointers.
1200  */
1201 static void release_dev(int dev, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1202 {
1203         struct tty_struct *tty, *o_tty;
1204         struct termios *tp, *o_tp;
1205         struct task_struct **p;
1206 
1207         tty = tty_table[dev];
1208         tp = tty_termios[dev];
1209         o_tty = NULL;
1210         o_tp = NULL;
1211         if (!tty) {
1212                 printk("release_dev: tty_table[%d] was NULL\n", dev);
1213                 return;
1214         }
1215         if (!tp) {
1216                 printk("release_dev: tty_termios[%d] was NULL\n", dev);
1217                 return;
1218         }
1219 #ifdef TTY_DEBUG_HANGUP
1220         printk("release_dev of tty%d (tty count=%d)...", dev, tty->count);
1221 #endif
1222         if (IS_A_PTY(dev)) {
1223                 o_tty = tty_table[PTY_OTHER(dev)];
1224                 o_tp = tty_termios[PTY_OTHER(dev)];
1225                 if (!o_tty) {
1226                         printk("release_dev: pty pair(%d) was NULL\n", dev);
1227                         return;
1228                 }
1229                 if (!o_tp) {
1230                         printk("release_dev: pty pair(%d) termios was NULL\n", dev);
1231                         return;
1232                 }
1233                 if (tty->link != o_tty || o_tty->link != tty) {
1234                         printk("release_dev: bad pty pointers\n");
1235                         return;
1236                 }
1237         }
1238         tty->write_data_cnt = 0; /* Clear out pending trash */
1239         if (tty->close)
1240                 tty->close(tty, filp);
1241         if (IS_A_PTY_MASTER(dev)) {
1242                 if (--tty->link->count < 0) {
1243                         printk("release_dev: bad tty slave count (dev = %d): %d\n",
1244                                dev, tty->count);
1245                         tty->link->count = 0;
1246                 }
1247         }
1248         if (--tty->count < 0) {
1249                 printk("release_dev: bad tty_table[%d]->count: %d\n",
1250                        dev, tty->count);
1251                 tty->count = 0;
1252         }
1253         if (tty->count)
1254                 return;
1255         
1256 #ifdef TTY_DEBUG_HANGUP
1257         printk("freeing tty structure...");
1258 #endif
1259 
1260         /*
1261          * Make sure there aren't any processes that still think this
1262          * tty is their controlling tty.
1263          */
1264         for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1265                 if ((*p) && (*p)->tty == tty->line)
1266                 (*p)->tty = -1;
1267         }
1268 
1269         /*
1270          * Shutdown the current line discipline, and reset it to
1271          * N_TTY.
1272          */
1273         if (ldiscs[tty->disc].close != NULL)
1274                 ldiscs[tty->disc].close(tty);
1275         tty->disc = N_TTY;
1276         tty->termios->c_line = N_TTY;
1277         
1278         if (o_tty) {
1279                 if (o_tty->count)
1280                         return;
1281                 else {
1282                         tty_table[PTY_OTHER(dev)] = NULL;
1283                         tty_termios[PTY_OTHER(dev)] = NULL;
1284                 }
1285         }
1286         tty_table[dev] = NULL;
1287         if (IS_A_PTY(dev)) {
1288                 tty_termios[dev] = NULL;
1289                 kfree_s(tp, sizeof(struct termios));
1290         }
1291         if (tty == redirect || o_tty == redirect)
1292                 redirect = NULL;
1293         free_page((unsigned long) tty);
1294         if (o_tty)
1295                 free_page((unsigned long) o_tty);
1296         if (o_tp)
1297                 kfree_s(o_tp, sizeof(struct termios));
1298 }
1299 
1300 /*
1301  * tty_open and tty_release keep up the tty count that contains the
1302  * number of opens done on a tty. We cannot use the inode-count, as
1303  * different inodes might point to the same tty.
1304  *
1305  * Open-counting is needed for pty masters, as well as for keeping
1306  * track of serial lines: DTR is dropped when the last close happens.
1307  * (This is not done solely through tty->count, now.  - Ted 1/27/92)
1308  *
1309  * The termios state of a pty is reset on first open so that
1310  * settings don't persist across reuse.
1311  */
1312 static int tty_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1313 {
1314         struct tty_struct *tty;
1315         int major, minor;
1316         int noctty, retval;
1317 
1318 retry_open:
1319         minor = MINOR(inode->i_rdev);
1320         major = MAJOR(inode->i_rdev);
1321         noctty = filp->f_flags & O_NOCTTY;
1322         if (major == TTYAUX_MAJOR) {
1323                 if (!minor) {
1324                         major = TTY_MAJOR;
1325                         minor = current->tty;
1326                 }
1327                 /* noctty = 1; */
1328         } else if (major == TTY_MAJOR) {
1329                 if (!minor) {
1330                         minor = fg_console + 1;
1331                         noctty = 1;
1332                 }
1333         } else {
1334                 printk("Bad major #%d in tty_open\n", MAJOR(inode->i_rdev));
1335                 return -ENODEV;
1336         }
1337         if (minor <= 0)
1338                 return -ENXIO;
1339         if (IS_A_PTY_MASTER(minor))
1340                 noctty = 1;
1341         filp->f_rdev = (major << 8) | minor;
1342         retval = init_dev(minor);
1343         if (retval)
1344                 return retval;
1345         tty = tty_table[minor];
1346 #ifdef TTY_DEBUG_HANGUP
1347         printk("opening tty%d...", tty->line);
1348 #endif
1349         if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1350                 return -EBUSY;
1351 
1352         /* clean up the packet stuff. */
1353         /*
1354          *  Why is this not done in init_dev?  Right here, if another 
1355          * process opens up a tty in packet mode, all the packet 
1356          * variables get cleared.  Come to think of it, is anything 
1357          * using the packet mode at all???  - Ted, 1/27/93
1358          *
1359          * Not to worry, a pty master can only be opened once.
1360          * And rlogind and telnetd both use packet mode.  -- jrs
1361          */
1362         tty->ctrl_status = 0;
1363         tty->packet = 0;
1364 
1365         if (tty->open) {
1366                 retval = tty->open(tty, filp);
1367         } else {
1368                 retval = -ENODEV;
1369         }
1370         if (retval) {
1371 #ifdef TTY_DEBUG_HANGUP
1372                 printk("error %d in opening tty%d...", retval, tty->line);
1373 #endif
1374 
1375                 release_dev(minor, filp);
1376                 if (retval != -ERESTARTSYS)
1377                         return retval;
1378                 if (current->signal & ~current->blocked)
1379                         return retval;
1380                 schedule();
1381                 goto retry_open;
1382         }
1383         if (!noctty &&
1384             current->leader &&
1385             current->tty<0 &&
1386             tty->session==0) {
1387                 current->tty = minor;
1388                 tty->session = current->session;
1389                 tty->pgrp = current->pgrp;
1390         }
1391         filp->f_rdev = MKDEV(TTY_MAJOR,minor); /* Set it to something normal */
1392         return 0;
1393 }
1394 
1395 /*
1396  * Note that releasing a pty master also releases the child, so
1397  * we have to make the redirection checks after that and on both
1398  * sides of a pty.
1399  */
1400 static void tty_release(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1401 {
1402         int dev;
1403 
1404         dev = filp->f_rdev;
1405         if (MAJOR(dev) != TTY_MAJOR) {
1406                 printk("tty_release: tty pseudo-major != TTY_MAJOR\n");
1407                 return;
1408         }
1409         dev = MINOR(filp->f_rdev);
1410         if (!dev) {
1411                 printk("tty_release: bad f_rdev\n");
1412                 return;
1413         }
1414         release_dev(dev, filp);
1415 }
1416 
1417 static int tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
     /* [previous][next][first][last][top][bottom][index][help] */
1418 {
1419         int dev;
1420         struct tty_struct * tty;
1421 
1422         dev = filp->f_rdev;
1423         if (MAJOR(dev) != TTY_MAJOR) {
1424                 printk("tty_select: tty pseudo-major != TTY_MAJOR\n");
1425                 return 0;
1426         }
1427         dev = MINOR(filp->f_rdev);
1428         tty = TTY_TABLE(dev);
1429         if (!tty) {
1430                 printk("tty_select: tty struct for dev %d was NULL\n", dev);
1431                 return 0;
1432         }
1433         switch (sel_type) {
1434                 case SEL_IN:
1435                         if (L_CANON(tty)) {
1436                                 if (available_canon_input(tty))
1437                                         return 1;
1438                         } else if (!EMPTY(&tty->secondary))
1439                                 return 1;
1440                         if (tty->link) {
1441                                 if (IS_A_PTY_MASTER(tty->line)) {
1442                                         if ((tty->flags & (1 << TTY_SLAVE_OPENED))
1443                                             && tty->link->count <= 1)
1444                                                 return 1;
1445                                 } else {
1446                                         if (!tty->link->count)
1447                                                 return 1;
1448                                 }
1449                         }
1450 
1451                         /* see if the status byte can be read. */
1452                         if (tty->packet && tty->link && tty->link->ctrl_status)
1453                                 return 1;
1454 
1455                         select_wait(&tty->secondary.proc_list, wait);
1456                         return 0;
1457                 case SEL_OUT:
1458                         if (!FULL(&tty->write_q))
1459                                 return 1;
1460                         select_wait(&tty->write_q.proc_list, wait);
1461                         return 0;
1462                 case SEL_EX:
1463                         if (tty->link) {
1464                                 if (IS_A_PTY_MASTER(tty->line)) {
1465                                         if ((tty->flags & (1 << TTY_SLAVE_OPENED))
1466                                             && tty->link->count <= 1)
1467                                                 return 1;
1468                                         if (tty->packet
1469                                             && tty->link->ctrl_status)
1470                                                 return 1;
1471                                 } else {
1472                                         if (!tty->link->count)
1473                                                 return 1;
1474                                 }
1475                         }
1476                         select_wait(&tty->except_q, wait);
1477                         return 0;
1478         }
1479         return 0;
1480 }
1481 
1482 /*
1483  * This implements the "Secure Attention Key" ---  the idea is to
1484  * prevent trojan horses by killing all processes associated with this
1485  * tty when the user hits the "Secure Attention Key".  Required for
1486  * super-paranoid applications --- see the Orange Book for more details.
1487  * 
1488  * This code could be nicer; ideally it should send a HUP, wait a few
1489  * seconds, then send a INT, and then a KILL signal.  But you then
1490  * have to coordinate with the init process, since all processes associated
1491  * with the current tty must be dead before the new getty is allowed
1492  * to spawn.
1493  */
1494 void do_SAK( struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1495 {
1496 #ifdef TTY_SOFT_SAK
1497         tty_hangup(tty);
1498 #else
1499         struct task_struct **p;
1500         int line = tty->line;
1501         int session = tty->session;
1502         int             i;
1503         struct file     *filp;
1504         
1505         flush_input(tty);
1506         flush_output(tty);
1507         for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1508                 if (!(*p))
1509                         continue;
1510                 if (((*p)->tty == line) ||
1511                     ((session > 0) && ((*p)->session == session)))
1512                         send_sig(SIGKILL, *p, 1);
1513                 else {
1514                         for (i=0; i < NR_OPEN; i++) {
1515                                 filp = (*p)->filp[i];
1516                                 if (filp && (filp->f_op == &tty_fops) &&
1517                                     (MINOR(filp->f_rdev) == line)) {
1518                                         send_sig(SIGKILL, *p, 1);
1519                                         break;
1520                                 }
1521                         }
1522                 }
1523         }
1524 #endif
1525 }
1526 
1527 /*
1528  * This routine allows a kernel routine to send a large chunk of data
1529  * to a particular tty; if all of the data can be queued up for ouput
1530  * immediately, tty_write_data() will return 0.  If, however, not all
1531  * of the data can be immediately queued for delivery, the number of
1532  * bytes left to be queued up will be returned, and the rest of the
1533  * data will be queued up when there is room.  The callback function
1534  * will be called (with the argument callarg) when the last of the
1535  * data is finally in the queue.
1536  *
1537  * Note that the callback routine will _not_ be called if all of the
1538  * data could be queued immediately.  This is to avoid a problem with
1539  * the kernel stack getting too deep, which might happen if the
1540  * callback routine calls tty_write_data with itself as an argument.
1541  */
1542 int tty_write_data(struct tty_struct *tty, char *bufp, int buflen,
     /* [previous][next][first][last][top][bottom][index][help] */
1543                     void (*callback)(void * data), void * callarg)
1544 {
1545         int head, tail, count;
1546         unsigned long flags;
1547         char *p;
1548 
1549 #define VLEFT ((tail-head-1)&(TTY_BUF_SIZE-1))
1550 
1551         save_flags(flags);
1552         cli();
1553         if (tty->write_data_cnt) {
1554                 restore_flags(flags);
1555                 return -EBUSY;
1556         }
1557 
1558         head = tty->write_q.head;
1559         tail = tty->write_q.tail;
1560         count = buflen;
1561         p = bufp;
1562 
1563         while (count && VLEFT > 0) {
1564                 tty->write_q.buf[head++] = *p++;
1565                 head &= TTY_BUF_SIZE-1;
1566                 count--;
1567         }
1568         tty->write_q.head = head;
1569         if (count) {
1570                 tty->write_data_cnt = count;
1571                 tty->write_data_ptr = (unsigned char *) p;
1572                 tty->write_data_callback = callback;
1573                 tty->write_data_arg = callarg;
1574         }
1575         restore_flags(flags);
1576         tty->write(tty);
1577         return count;
1578 }
1579 
1580 /*
1581  * This routine routine is called after an interrupt has drained a
1582  * tty's write queue, so that there is more space for data waiting to
1583  * be sent in tty->write_data_ptr.
1584  *
1585  * tty_check_write[8] is a bitstring which indicates which ttys
1586  * needs to be processed.
1587  */
1588 void tty_bh_routine(void * unused)
     /* [previous][next][first][last][top][bottom][index][help] */
1589 {
1590         int     i, j, line, mask;
1591         int     head, tail, count;
1592         unsigned char * p;
1593         struct tty_struct * tty;
1594 
1595         for (i = 0, line = 0; i < MAX_TTYS / 32; i++) {
1596                 if (!tty_check_write[i]) {
1597                         line += 32;
1598                         continue;
1599                 }
1600                 for (j=0, mask=0; j < 32; j++, line++, mask <<= 1) {
1601                         if (clear_bit(j, &tty_check_write[i])) {
1602                                 tty = tty_table[line];
1603                                 if (!tty || !tty->write_data_cnt)
1604                                         continue;
1605                                 cli();
1606                                 head = tty->write_q.head;
1607                                 tail = tty->write_q.tail;
1608                                 count = tty->write_data_cnt;
1609                                 p = tty->write_data_ptr;
1610 
1611                                 while (count && VLEFT > 0) {
1612                                         tty->write_q.buf[head++] = *p++;
1613                                         head &= TTY_BUF_SIZE-1;
1614                                         count--;
1615                                 }
1616                                 tty->write_q.head = head;
1617                                 tty->write_data_ptr = p;
1618                                 tty->write_data_cnt = count;
1619                                 sti();
1620                                 if (!count)
1621                                         (tty->write_data_callback)
1622                                                 (tty->write_data_arg);
1623                         }
1624                 }
1625         }
1626         
1627 }
1628 
1629 /*
1630  * This subroutine initializes a tty structure.  We have to set up
1631  * things correctly for each different type of tty.
1632  */
1633 static void initialize_tty_struct(int line, struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1634 {
1635         memset(tty, 0, sizeof(struct tty_struct));
1636         tty->line = line;
1637         tty->disc = N_TTY;
1638         tty->pgrp = -1;
1639         tty->winsize.ws_row = 0;
1640         tty->winsize.ws_col = 0;
1641         if (IS_A_CONSOLE(line)) {
1642                 tty->open = con_open;
1643                 tty->winsize.ws_row = video_num_lines;
1644                 tty->winsize.ws_col = video_num_columns;
1645         } else if IS_A_SERIAL(line) {
1646                 tty->open = rs_open;
1647         } else if IS_A_PTY(line) {
1648                 tty->open = pty_open;
1649         }
1650         tty->except_q = NULL;
1651 }
1652 
1653 static void initialize_termios(int line, struct termios * tp)
     /* [previous][next][first][last][top][bottom][index][help] */
1654 {
1655         memset(tp, 0, sizeof(struct termios));
1656         memcpy(tp->c_cc, INIT_C_CC, NCCS);
1657         if (IS_A_CONSOLE(line)) {
1658                 tp->c_iflag = ICRNL | IXON;
1659                 tp->c_oflag = OPOST | ONLCR;
1660                 tp->c_cflag = B38400 | CS8 | CREAD;
1661                 tp->c_lflag = ISIG | ICANON | ECHO |
1662                         ECHOCTL | ECHOKE;
1663         } else if (IS_A_SERIAL(line)) {
1664                 tp->c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1665                 tp->c_oflag = OPOST | ONLCR | XTABS;
1666         } else if (IS_A_PTY_MASTER(line)) {
1667                 tp->c_cflag = B9600 | CS8 | CREAD;
1668         } else if (IS_A_PTY_SLAVE(line)) {
1669                 tp->c_iflag = ICRNL | IXON;
1670                 tp->c_oflag = OPOST | ONLCR;
1671                 tp->c_cflag = B38400 | CS8 | CREAD;
1672                 tp->c_lflag = ISIG | ICANON | ECHO |
1673                         ECHOCTL | ECHOKE;
1674         }
1675 }
1676 
1677 static struct tty_ldisc tty_ldisc_N_TTY = {
1678         0,                      /* flags */
1679         NULL,                   /* open */
1680         NULL,                   /* close */
1681         read_chan,              /* read */
1682         write_chan,             /* write */
1683         NULL,                   /* ioctl */
1684         copy_to_cooked          /* handler */
1685 };
1686 
1687         
1688 long tty_init(long kmem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1689 {
1690         int i;
1691 
1692         if (sizeof(struct tty_struct) > PAGE_SIZE)
1693                 panic("size of tty structure > PAGE_SIZE!");
1694         if (register_chrdev(TTY_MAJOR,"tty",&tty_fops))
1695                 panic("unable to get major %d for tty device", TTY_MAJOR);
1696         if (register_chrdev(TTYAUX_MAJOR,"tty",&tty_fops))
1697                 panic("unable to get major %d for tty device", TTYAUX_MAJOR);
1698         for (i=0 ; i< MAX_TTYS ; i++) {
1699                 tty_table[i] =  0;
1700                 tty_termios[i] = 0;
1701         }
1702         memset(tty_check_write, 0, sizeof(tty_check_write));
1703         bh_base[TTY_BH].routine = tty_bh_routine;
1704 
1705         /* Setup the default TTY line discipline. */
1706         memset(ldiscs, 0, sizeof(ldiscs));
1707         (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
1708 
1709         kmem_start = kbd_init(kmem_start);
1710         kmem_start = con_init(kmem_start);
1711         kmem_start = rs_init(kmem_start);
1712         return kmem_start;
1713 }

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