home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / drivers / char / tty_io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-08  |  46.1 KB  |  1,855 lines

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