Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[safe/jmp/linux-2.6] / drivers / char / n_tty.c
index 90c3969..efbfe96 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * n_tty.c --- implements the N_TTY line discipline.
- * 
+ *
  * This code used to be in tty_io.c, but things are getting hairy
  * enough that it made sense to split things off.  (The N_TTY
  * processing has changed so much that it's hardly recognizable,
@@ -8,25 +8,25 @@
  *
  * Note that the open routine for N_TTY is guaranteed never to return
  * an error.  This is because Linux will fall back to setting a line
- * to N_TTY if it can not switch to any other line discipline.  
+ * to N_TTY if it can not switch to any other line discipline.
  *
  * Written by Theodore Ts'o, Copyright 1994.
- * 
+ *
  * This file also contains code originally written by Linus Torvalds,
  * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
- * 
+ *
  * This file may be redistributed under the terms of the GNU General Public
  * License.
  *
  * Reduced memory usage for older ARM systems  - Russell King.
  *
- * 2000/01/20   Fixed SMP locking on put_tty_queue using bits of 
+ * 2000/01/20   Fixed SMP locking on put_tty_queue using bits of
  *             the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
  *             who actually finally proved there really was a race.
  *
  * 2002/03/18   Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
  *             waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
- *             Also fixed a bug in BLOCKING mode where write_chan returns
+ *             Also fixed a bug in BLOCKING mode where n_tty_write returns
  *             EAGAIN
  */
 
@@ -99,6 +99,7 @@ static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
 
 static void n_tty_set_room(struct tty_struct *tty)
 {
+       /* tty->read_cnt is not read locked ? */
        int     left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
 
        /*
@@ -121,6 +122,16 @@ static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
        }
 }
 
+/**
+ *     put_tty_queue           -       add character to tty
+ *     @c: character
+ *     @tty: tty device
+ *
+ *     Add a character to the tty read_buf queue. This is done under the
+ *     read_lock to serialize character addition and also to protect us
+ *     against parallel reads or flushes
+ */
+
 static void put_tty_queue(unsigned char c, struct tty_struct *tty)
 {
        unsigned long flags;
@@ -137,29 +148,26 @@ static void put_tty_queue(unsigned char c, struct tty_struct *tty)
  *     check_unthrottle        -       allow new receive data
  *     @tty; tty device
  *
- *     Check whether to call the driver.unthrottle function.
- *     We test the TTY_THROTTLED bit first so that it always
- *     indicates the current state. The decision about whether
- *     it is worth allowing more input has been taken by the caller.
+ *     Check whether to call the driver unthrottle functions
+ *
  *     Can sleep, may be called under the atomic_read_lock mutex but
  *     this is not guaranteed.
  */
-static void check_unthrottle(struct tty_struct * tty)
+static void check_unthrottle(struct tty_struct *tty)
 {
-       if (tty->count &&
-           test_and_clear_bit(TTY_THROTTLED, &tty->flags) && 
-           tty->driver->unthrottle)
-               tty->driver->unthrottle(tty);
+       if (tty->count)
+               tty_unthrottle(tty);
 }
 
 /**
  *     reset_buffer_flags      -       reset buffer state
  *     @tty: terminal to reset
  *
- *     Reset the read buffer counters, clear the flags, 
+ *     Reset the read buffer counters, clear the flags,
  *     and make sure the driver is unthrottled. Called
  *     from n_tty_open() and n_tty_flush_buffer().
+ *
+ *     Locking: tty_read_lock for read fields.
  */
 static void reset_buffer_flags(struct tty_struct *tty)
 {
@@ -183,22 +191,24 @@ static void reset_buffer_flags(struct tty_struct *tty)
  *     at hangup) or when the N_TTY line discipline internally has to
  *     clean the pending queue (for example some signals).
  *
- *     FIXME: tty->ctrl_status is not spinlocked and relies on
- *     lock_kernel() still.
+ *     Locking: ctrl_lock, read_lock.
  */
-static void n_tty_flush_buffer(struct tty_struct * tty)
+
+static void n_tty_flush_buffer(struct tty_struct *tty)
 {
+       unsigned long flags;
        /* clear everything and unthrottle the driver */
        reset_buffer_flags(tty);
-       
+
        if (!tty->link)
                return;
 
+       spin_lock_irqsave(&tty->ctrl_lock, flags);
        if (tty->link->packet) {
                tty->ctrl_status |= TIOCPKT_FLUSHREAD;
                wake_up_interruptible(&tty->link->read_wait);
        }
+       spin_unlock_irqrestore(&tty->ctrl_lock, flags);
 }
 
 /**
@@ -206,9 +216,11 @@ static void n_tty_flush_buffer(struct tty_struct * tty)
  *     @tty: tty device
  *
  *     Report the number of characters buffered to be delivered to user
- *     at this instant in time. 
+ *     at this instant in time.
+ *
+ *     Locking: read_lock
  */
+
 static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
 {
        unsigned long flags;
@@ -234,7 +246,7 @@ static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
  *     character. We use this to correctly compute the on screen size
  *     of the character when printing
  */
+
 static inline int is_utf8_continuation(unsigned char c)
 {
        return (c & 0xc0) == 0x80;
@@ -247,7 +259,7 @@ static inline int is_utf8_continuation(unsigned char c)
  *     Returns true if the utf8 character 'c' is a multibyte continuation
  *     character and the terminal is in unicode mode.
  */
+
 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
 {
        return I_IUTF8(tty) && is_utf8_continuation(c);
@@ -264,33 +276,38 @@ static inline int is_continuation(unsigned char c, struct tty_struct *tty)
  *     relevant in the world today. If you ever need them, add them here.
  *
  *     Called from both the receive and transmit sides and can be called
- *     re-entrantly. Relies on lock_kernel() still.
+ *     re-entrantly. Relies on lock_kernel() for tty->column state.
  */
+
 static int opost(unsigned char c, struct tty_struct *tty)
 {
        int     space, spaces;
 
-       space = tty->driver->write_room(tty);
+       space = tty_write_room(tty);
        if (!space)
                return -1;
 
+       lock_kernel();
        if (O_OPOST(tty)) {
                switch (c) {
                case '\n':
                        if (O_ONLRET(tty))
                                tty->column = 0;
                        if (O_ONLCR(tty)) {
-                               if (space < 2)
+                               if (space < 2) {
+                                       unlock_kernel();
                                        return -1;
-                               tty->driver->put_char(tty, '\r');
+                               }
+                               tty_put_char(tty, '\r');
                                tty->column = 0;
                        }
                        tty->canon_column = tty->column;
                        break;
                case '\r':
-                       if (O_ONOCR(tty) && tty->column == 0)
+                       if (O_ONOCR(tty) && tty->column == 0) {
+                               unlock_kernel();
                                return 0;
+                       }
                        if (O_OCRNL(tty)) {
                                c = '\n';
                                if (O_ONLRET(tty))
@@ -302,10 +319,13 @@ static int opost(unsigned char c, struct tty_struct *tty)
                case '\t':
                        spaces = 8 - (tty->column & 7);
                        if (O_TABDLY(tty) == XTABS) {
-                               if (space < spaces)
+                               if (space < spaces) {
+                                       unlock_kernel();
                                        return -1;
+                               }
                                tty->column += spaces;
-                               tty->driver->write(tty, "        ", spaces);
+                               tty->ops->write(tty, "        ", spaces);
+                               unlock_kernel();
                                return 0;
                        }
                        tty->column += spaces;
@@ -322,7 +342,8 @@ static int opost(unsigned char c, struct tty_struct *tty)
                        break;
                }
        }
-       tty->driver->put_char(tty, c);
+       tty_put_char(tty, c);
+       unlock_kernel();
        return 0;
 }
 
@@ -337,22 +358,24 @@ static int opost(unsigned char c, struct tty_struct *tty)
  *     the simple cases normally found and helps to generate blocks of
  *     symbols for the console driver and thus improve performance.
  *
- *     Called from write_chan under the tty layer write lock.
+ *     Called from n_tty_write under the tty layer write lock. Relies
+ *     on lock_kernel for the tty->column state.
  */
-static ssize_t opost_block(struct tty_struct * tty,
-                      const unsigned char * buf, unsigned int nr)
+
+static ssize_t opost_block(struct tty_struct *tty,
+                      const unsigned char *buf, unsigned int nr)
 {
        int     space;
        int     i;
        const unsigned char *cp;
 
-       space = tty->driver->write_room(tty);
+       space = tty_write_room(tty);
        if (!space)
                return 0;
        if (nr > space)
                nr = space;
 
+       lock_kernel();
        for (i = 0, cp = buf; i < nr; i++, cp++) {
                switch (*cp) {
                case '\n':
@@ -384,49 +407,45 @@ static ssize_t opost_block(struct tty_struct * tty,
                }
        }
 break_out:
-       if (tty->driver->flush_chars)
-               tty->driver->flush_chars(tty);
-       i = tty->driver->write(tty, buf, i);    
+       if (tty->ops->flush_chars)
+               tty->ops->flush_chars(tty);
+       i = tty->ops->write(tty, buf, i);
+       unlock_kernel();
        return i;
 }
 
 
 /**
- *     put_char        -       write character to driver
- *     @c: character (or part of unicode symbol)
- *     @tty: terminal device
- *
- *     Queue a byte to the driver layer for output
- */
-static inline void put_char(unsigned char c, struct tty_struct *tty)
-{
-       tty->driver->put_char(tty, c);
-}
-
-/**
  *     echo_char       -       echo characters
  *     @c: unicode byte to echo
  *     @tty: terminal device
  *
- *     Echo user input back onto the screen. This must be called only when 
+ *     Echo user input back onto the screen. This must be called only when
  *     L_ECHO(tty) is true. Called from the driver receive_buf path.
+ *
+ *     Relies on BKL for tty column locking
  */
 
 static void echo_char(unsigned char c, struct tty_struct *tty)
 {
        if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
-               put_char('^', tty);
-               put_char(c ^ 0100, tty);
+               tty_put_char(tty, '^');
+               tty_put_char(tty, c ^ 0100);
                tty->column += 2;
        } else
                opost(c, tty);
 }
 
+/**
+ *     finsh_erasing           -       complete erase
+ *     @tty: tty doing the erase
+ *
+ *     Relies on BKL for tty column locking
+ */
 static inline void finish_erasing(struct tty_struct *tty)
 {
        if (tty->erasing) {
-               put_char('/', tty);
+               tty_put_char(tty, '/');
                tty->column++;
                tty->erasing = 0;
        }
@@ -440,14 +459,17 @@ static inline void finish_erasing(struct tty_struct *tty)
  *     Perform erase and necessary output when an erase character is
  *     present in the stream from the driver layer. Handles the complexities
  *     of UTF-8 multibyte symbols.
+ *
+ *     Locking: read_lock for tty buffers, BKL for column/erasing state
  */
+
 static void eraser(unsigned char c, struct tty_struct *tty)
 {
        enum { ERASE, WERASE, KILL } kill_type;
        int head, seen_alnums, cnt;
        unsigned long flags;
 
+       /* FIXME: locking needed ? */
        if (tty->read_head == tty->canon_head) {
                /* opost('\a', tty); */         /* what do you think? */
                return;
@@ -482,6 +504,7 @@ static void eraser(unsigned char c, struct tty_struct *tty)
        }
 
        seen_alnums = 0;
+       /* FIXME: Locking ?? */
        while (tty->read_head != tty->canon_head) {
                head = tty->read_head;
 
@@ -510,7 +533,7 @@ static void eraser(unsigned char c, struct tty_struct *tty)
                if (L_ECHO(tty)) {
                        if (L_ECHOPRT(tty)) {
                                if (!tty->erasing) {
-                                       put_char('\\', tty);
+                                       tty_put_char(tty, '\\');
                                        tty->column++;
                                        tty->erasing = 1;
                                }
@@ -518,7 +541,7 @@ static void eraser(unsigned char c, struct tty_struct *tty)
                                echo_char(c, tty);
                                while (--cnt > 0) {
                                        head = (head+1) & (N_TTY_BUF_SIZE-1);
-                                       put_char(tty->read_buf[head], tty);
+                                       tty_put_char(tty, tty->read_buf[head]);
                                }
                        } else if (kill_type == ERASE && !L_ECHOE(tty)) {
                                echo_char(ERASE_CHAR(tty), tty);
@@ -541,27 +564,27 @@ static void eraser(unsigned char c, struct tty_struct *tty)
 
                                /* should never happen */
                                if (tty->column > 0x80000000)
-                                       tty->column = 0; 
+                                       tty->column = 0;
 
                                /* Now backup to that column. */
                                while (tty->column > col) {
                                        /* Can't use opost here. */
-                                       put_char('\b', tty);
+                                       tty_put_char(tty, '\b');
                                        if (tty->column > 0)
                                                tty->column--;
                                }
                        } else {
                                if (iscntrl(c) && L_ECHOCTL(tty)) {
-                                       put_char('\b', tty);
-                                       put_char(' ', tty);
-                                       put_char('\b', tty);
+                                       tty_put_char(tty, '\b');
+                                       tty_put_char(tty, ' ');
+                                       tty_put_char(tty, '\b');
                                        if (tty->column > 0)
                                                tty->column--;
                                }
                                if (!iscntrl(c) || L_ECHOCTL(tty)) {
-                                       put_char('\b', tty);
-                                       put_char(' ', tty);
-                                       put_char('\b', tty);
+                                       tty_put_char(tty, '\b');
+                                       tty_put_char(tty, ' ');
+                                       tty_put_char(tty, '\b');
                                        if (tty->column > 0)
                                                tty->column--;
                                }
@@ -584,16 +607,17 @@ static void eraser(unsigned char c, struct tty_struct *tty)
  *     may caus terminal flushing to take place according to the termios
  *     settings and character used. Called from the driver receive_buf
  *     path so serialized.
+ *
+ *     Locking: ctrl_lock, read_lock (both via flush buffer)
  */
+
 static inline void isig(int sig, struct tty_struct *tty, int flush)
 {
        if (tty->pgrp)
                kill_pgrp(tty->pgrp, sig, 1);
        if (flush || !L_NOFLSH(tty)) {
                n_tty_flush_buffer(tty);
-               if (tty->driver->flush_buffer)
-                       tty->driver->flush_buffer(tty);
+               tty_driver_flush_buffer(tty);
        }
 }
 
@@ -606,7 +630,7 @@ static inline void isig(int sig, struct tty_struct *tty, int flush)
  *
  *     Called from the receive_buf path so single threaded.
  */
+
 static inline void n_tty_receive_break(struct tty_struct *tty)
 {
        if (I_IGNBRK(tty))
@@ -635,7 +659,7 @@ static inline void n_tty_receive_break(struct tty_struct *tty)
  *     need locking as num_overrun and overrun_time are function
  *     private.
  */
+
 static inline void n_tty_receive_overrun(struct tty_struct *tty)
 {
        char buf[64];
@@ -662,9 +686,8 @@ static inline void n_tty_receive_overrun(struct tty_struct *tty)
 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
                                              unsigned char c)
 {
-       if (I_IGNPAR(tty)) {
+       if (I_IGNPAR(tty))
                return;
-       }
        if (I_PARMRK(tty)) {
                put_tty_queue('\377', tty);
                put_tty_queue('\0', tty);
@@ -682,7 +705,7 @@ static inline void n_tty_receive_parity_error(struct tty_struct *tty,
  *     @c: character
  *
  *     Process an individual character of input received from the driver.
- *     This is serialized with respect to itself by the rules for the 
+ *     This is serialized with respect to itself by the rules for the
  *     driver above.
  */
 
@@ -694,7 +717,7 @@ static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
                put_tty_queue(c, tty);
                return;
        }
-       
+
        if (I_ISTRIP(tty))
                c &= 0x7f;
        if (I_IUCLC(tty) && L_IEXTEN(tty))
@@ -702,7 +725,7 @@ static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
 
        if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
            ((I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty)) ||
-            c == INTR_CHAR(tty) || c == QUIT_CHAR(tty)))
+            c == INTR_CHAR(tty) || c == QUIT_CHAR(tty) || c == SUSP_CHAR(tty)))
                start_tty(tty);
 
        if (tty->closing) {
@@ -726,7 +749,7 @@ static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
                tty->lnext = 0;
                if (L_ECHO(tty)) {
                        if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
-                               put_char('\a', tty); /* beep if no space */
+                               tty_put_char(tty, '\a'); /* beep if no space */
                                return;
                        }
                        /* Record the column of first canon char. */
@@ -739,14 +762,7 @@ static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
                put_tty_queue(c, tty);
                return;
        }
-               
-       if (c == '\r') {
-               if (I_IGNCR(tty))
-                       return;
-               if (I_ICRNL(tty))
-                       c = '\n';
-       } else if (c == '\n' && I_INLCR(tty))
-               c = '\r';
+
        if (I_IXON(tty)) {
                if (c == START_CHAR(tty)) {
                        start_tty(tty);
@@ -757,6 +773,7 @@ static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
                        return;
                }
        }
+
        if (L_ISIG(tty)) {
                int signal;
                signal = SIGINT;
@@ -776,8 +793,7 @@ send_signal:
                         */
                        if (!L_NOFLSH(tty)) {
                                n_tty_flush_buffer(tty);
-                               if (tty->driver->flush_buffer)
-                                       tty->driver->flush_buffer(tty);
+                               tty_driver_flush_buffer(tty);
                        }
                        if (L_ECHO(tty))
                                echo_char(c, tty);
@@ -786,6 +802,15 @@ send_signal:
                        return;
                }
        }
+
+       if (c == '\r') {
+               if (I_IGNCR(tty))
+                       return;
+               if (I_ICRNL(tty))
+                       c = '\n';
+       } else if (c == '\n' && I_INLCR(tty))
+               c = '\r';
+
        if (tty->icanon) {
                if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
                    (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
@@ -797,8 +822,8 @@ send_signal:
                        if (L_ECHO(tty)) {
                                finish_erasing(tty);
                                if (L_ECHOCTL(tty)) {
-                                       put_char('^', tty);
-                                       put_char('\b', tty);
+                                       tty_put_char(tty, '^');
+                                       tty_put_char(tty, '\b');
                                }
                        }
                        return;
@@ -819,14 +844,14 @@ send_signal:
                if (c == '\n') {
                        if (L_ECHO(tty) || L_ECHONL(tty)) {
                                if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
-                                       put_char('\a', tty);
+                                       tty_put_char(tty, '\a');
                                opost('\n', tty);
                        }
                        goto handle_newline;
                }
                if (c == EOF_CHAR(tty)) {
-                       if (tty->canon_head != tty->read_head)
-                               set_bit(TTY_PUSH, &tty->flags);
+                       if (tty->canon_head != tty->read_head)
+                               set_bit(TTY_PUSH, &tty->flags);
                        c = __DISABLED_CHAR;
                        goto handle_newline;
                }
@@ -837,7 +862,7 @@ send_signal:
                         */
                        if (L_ECHO(tty)) {
                                if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
-                                       put_char('\a', tty);
+                                       tty_put_char(tty, '\a');
                                /* Record the column of first canon char. */
                                if (tty->canon_head == tty->read_head)
                                        tty->canon_column = tty->column;
@@ -850,7 +875,7 @@ send_signal:
                        if (I_PARMRK(tty) && c == (unsigned char) '\377')
                                put_tty_queue(c, tty);
 
-               handle_newline:
+handle_newline:
                        spin_lock_irqsave(&tty->read_lock, flags);
                        set_bit(tty->read_head, tty->read_flags);
                        put_tty_queue_nolock(c, tty);
@@ -863,11 +888,11 @@ send_signal:
                        return;
                }
        }
-       
+
        finish_erasing(tty);
        if (L_ECHO(tty)) {
                if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
-                       put_char('\a', tty); /* beep if no space */
+                       tty_put_char(tty, '\a'); /* beep if no space */
                        return;
                }
                if (c == '\n')
@@ -884,7 +909,7 @@ send_signal:
                put_tty_queue(c, tty);
 
        put_tty_queue(c, tty);
-}      
+}
 
 
 /**
@@ -898,12 +923,10 @@ send_signal:
 
 static void n_tty_write_wakeup(struct tty_struct *tty)
 {
-       if (tty->fasync)
-       {
-               set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
+       if (tty->fasync) {
+               set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
                kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
        }
-       return;
 }
 
 /**
@@ -918,7 +941,7 @@ static void n_tty_write_wakeup(struct tty_struct *tty)
  *     not from interrupt context. The driver is responsible for making
  *     calls one at a time and in order (or using flush_to_ldisc)
  */
+
 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
                              char *fp, int count)
 {
@@ -950,7 +973,7 @@ static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
                tty->read_cnt += i;
                spin_unlock_irqrestore(&tty->read_lock, cpuflags);
        } else {
-               for (i=count, p = cp, f = fp; i; i--, p++) {
+               for (i = count, p = cp, f = fp; i; i--, p++) {
                        if (f)
                                flags = *f++;
                        switch (flags) {
@@ -968,13 +991,13 @@ static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
                                n_tty_receive_overrun(tty);
                                break;
                        default:
-                               printk("%s: unknown flag %d\n",
+                               printk(KERN_ERR "%s: unknown flag %d\n",
                                       tty_name(tty, buf), flags);
                                break;
                        }
                }
-               if (tty->driver->flush_chars)
-                       tty->driver->flush_chars(tty);
+               if (tty->ops->flush_chars)
+                       tty->ops->flush_chars(tty);
        }
 
        n_tty_set_room(tty);
@@ -990,18 +1013,14 @@ static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
         * mode.  We don't want to throttle the driver if we're in
         * canonical mode and don't have a newline yet!
         */
-       if (tty->receive_room < TTY_THRESHOLD_THROTTLE) {
-               /* check TTY_THROTTLED first so it indicates our state */
-               if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
-                   tty->driver->throttle)
-                       tty->driver->throttle(tty);
-       }
+       if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
+               tty_throttle(tty);
 }
 
 int is_ignored(int sig)
 {
        return (sigismember(&current->blocked, sig) ||
-               current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
+               current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
 }
 
 /**
@@ -1011,16 +1030,30 @@ int is_ignored(int sig)
  *
  *     Called by the tty layer when the user changes termios flags so
  *     that the line discipline can plan ahead. This function cannot sleep
- *     and is protected from re-entry by the tty layer. The user is 
+ *     and is protected from re-entry by the tty layer. The user is
  *     guaranteed that this function will not be re-entered or in progress
  *     when the ldisc is closed.
+ *
+ *     Locking: Caller holds tty->termios_mutex
  */
-static void n_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
+
+static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
 {
-       if (!tty)
-               return;
-       
+       int canon_change = 1;
+       BUG_ON(!tty);
+
+       if (old)
+               canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON;
+       if (canon_change) {
+               memset(&tty->read_flags, 0, sizeof tty->read_flags);
+               tty->canon_head = tty->read_tail;
+               tty->canon_data = 0;
+               tty->erasing = 0;
+       }
+
+       if (canon_change && !L_ICANON(tty) && tty->read_cnt)
+               wake_up_interruptible(&tty->read_wait);
+
        tty->icanon = (L_ICANON(tty) != 0);
        if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
                tty->raw = 1;
@@ -1079,18 +1112,21 @@ static void n_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
                        tty->real_raw = 0;
        }
        n_tty_set_room(tty);
+       /* The termios change make the tty ready for I/O */
+       wake_up_interruptible(&tty->write_wait);
+       wake_up_interruptible(&tty->read_wait);
 }
 
 /**
  *     n_tty_close             -       close the ldisc for this tty
  *     @tty: device
  *
- *     Called from the terminal layer when this line discipline is 
- *     being shut down, either because of a close or becsuse of a 
+ *     Called from the terminal layer when this line discipline is
+ *     being shut down, either because of a close or becsuse of a
  *     discipline change. The function will not be called while other
  *     ldisc methods are in progress.
  */
+
 static void n_tty_close(struct tty_struct *tty)
 {
        n_tty_flush_buffer(tty);
@@ -1104,7 +1140,7 @@ static void n_tty_close(struct tty_struct *tty)
  *     n_tty_open              -       open an ldisc
  *     @tty: terminal to open
  *
- *     Called when this line discipline is being attached to the 
+ *     Called when this line discipline is being attached to the
  *     terminal device. Can sleep. Called serialized so that no
  *     other events will occur in parallel. No further open will occur
  *     until a close.
@@ -1147,7 +1183,7 @@ static inline int input_available_p(struct tty_struct *tty, int amt)
  *     @b: user data
  *     @nr: size of data
  *
- *     Helper function to speed up read_chan.  It is only called when
+ *     Helper function to speed up n_tty_read.  It is only called when
  *     ICANON is off; it copies characters straight from the tty queue to
  *     user space directly.  It can be profitably called twice; once to
  *     drain the space from the tail pointer to the (physical) end of the
@@ -1157,7 +1193,7 @@ static inline int input_available_p(struct tty_struct *tty, int amt)
  *     Called under the tty->atomic_read_lock sem
  *
  */
+
 static int copy_from_read_buf(struct tty_struct *tty,
                                      unsigned char __user **b,
                                      size_t *nr)
@@ -1186,7 +1222,8 @@ static int copy_from_read_buf(struct tty_struct *tty,
        return retval;
 }
 
-extern ssize_t redirected_tty_write(struct file *,const char *,size_t,loff_t *);
+extern ssize_t redirected_tty_write(struct file *, const char __user *,
+                                                       size_t, loff_t *);
 
 /**
  *     job_control             -       check job control
@@ -1194,10 +1231,15 @@ extern ssize_t redirected_tty_write(struct file *,const char *,size_t,loff_t *);
  *     @file: file handle
  *
  *     Perform job control management checks on this file/tty descriptor
- *     and if appropriate send any needed signals and return a negative 
+ *     and if appropriate send any needed signals and return a negative
  *     error code if action should be taken.
+ *
+ *     FIXME:
+ *     Locking: None - redirected write test is safe, testing
+ *     current->signal should possibly lock current->sighand
+ *     pgrp locking ?
  */
+
 static int job_control(struct tty_struct *tty, struct file *file)
 {
        /* Job control check -- must be done at start and after
@@ -1208,7 +1250,7 @@ static int job_control(struct tty_struct *tty, struct file *file)
        if (file->f_op->write != redirected_tty_write &&
            current->signal->tty == tty) {
                if (!tty->pgrp)
-                       printk("read_chan: no tty->pgrp!\n");
+                       printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
                else if (task_pgrp(current) != tty->pgrp) {
                        if (is_ignored(SIGTTIN) ||
                            is_current_pgrp_orphaned())
@@ -1220,10 +1262,10 @@ static int job_control(struct tty_struct *tty, struct file *file)
        }
        return 0;
 }
+
 
 /**
- *     read_chan               -       read function for tty
+ *     n_tty_read              -       read function for tty
  *     @tty: tty device
  *     @file: file object
  *     @buf: userspace buffer pointer
@@ -1236,8 +1278,8 @@ static int job_control(struct tty_struct *tty, struct file *file)
  *
  *     This code must be sure never to sleep through a hangup.
  */
-static ssize_t read_chan(struct tty_struct *tty, struct file *file,
+
+static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
                         unsigned char __user *buf, size_t nr)
 {
        unsigned char __user *b = buf;
@@ -1248,18 +1290,16 @@ static ssize_t read_chan(struct tty_struct *tty, struct file *file,
        ssize_t size;
        long timeout;
        unsigned long flags;
+       int packet;
 
 do_it_again:
 
-       if (!tty->read_buf) {
-               printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
-               return -EIO;
-       }
+       BUG_ON(!tty->read_buf);
 
        c = job_control(tty, file);
-       if(c < 0)
+       if (c < 0)
                return c;
-       
+
        minimum = time = 0;
        timeout = MAX_SCHEDULE_TIMEOUT;
        if (!tty->icanon) {
@@ -1287,21 +1327,23 @@ do_it_again:
        if (file->f_flags & O_NONBLOCK) {
                if (!mutex_trylock(&tty->atomic_read_lock))
                        return -EAGAIN;
-       }
-       else {
+       } else {
                if (mutex_lock_interruptible(&tty->atomic_read_lock))
                        return -ERESTARTSYS;
        }
+       packet = tty->packet;
 
        add_wait_queue(&tty->read_wait, &wait);
        while (nr) {
                /* First test for status change. */
-               if (tty->packet && tty->link->ctrl_status) {
+               if (packet && tty->link->ctrl_status) {
                        unsigned char cs;
                        if (b != buf)
                                break;
+                       spin_lock_irqsave(&tty->link->ctrl_lock, flags);
                        cs = tty->link->ctrl_status;
                        tty->link->ctrl_status = 0;
+                       spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
                        if (tty_put_user(tty, cs, b++)) {
                                retval = -EFAULT;
                                b--;
@@ -1314,11 +1356,11 @@ do_it_again:
                   so that any interrupt will set the state back to
                   TASK_RUNNING. */
                set_current_state(TASK_INTERRUPTIBLE);
-               
+
                if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
                    ((minimum - (b - buf)) >= 1))
                        tty->minimum_to_wake = (minimum - (b - buf));
-               
+
                if (!input_available_p(tty, 0)) {
                        if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
                                retval = -EIO;
@@ -1336,6 +1378,7 @@ do_it_again:
                                retval = -ERESTARTSYS;
                                break;
                        }
+                       /* FIXME: does n_tty_set_room need locking ? */
                        n_tty_set_room(tty);
                        timeout = schedule_timeout(timeout);
                        continue;
@@ -1343,7 +1386,7 @@ do_it_again:
                __set_current_state(TASK_RUNNING);
 
                /* Deal with packet mode. */
-               if (tty->packet && b == buf) {
+               if (packet && b == buf) {
                        if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
                                retval = -EFAULT;
                                b--;
@@ -1355,7 +1398,7 @@ do_it_again:
                if (tty->icanon) {
                        /* N.B. avoid overrun if nr == 0 */
                        while (nr && tty->read_cnt) {
-                               int eol;
+                               int eol;
 
                                eol = test_and_clear_bit(tty->read_tail,
                                                tty->read_flags);
@@ -1391,6 +1434,8 @@ do_it_again:
                                break;
                } else {
                        int uncopied;
+                       /* The copy function takes the read lock and handles
+                          locking internally for this case */
                        uncopied = copy_from_read_buf(tty, &b, &nr);
                        uncopied += copy_from_read_buf(tty, &b, &nr);
                        if (uncopied) {
@@ -1427,17 +1472,16 @@ do_it_again:
        if (size) {
                retval = size;
                if (nr)
-                       clear_bit(TTY_PUSH, &tty->flags);
+                       clear_bit(TTY_PUSH, &tty->flags);
        } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
                 goto do_it_again;
 
        n_tty_set_room(tty);
-
        return retval;
 }
 
 /**
- *     write_chan              -       write function for tty
+ *     n_tty_write             -       write function for tty
  *     @tty: tty device
  *     @file: file object
  *     @buf: userspace buffer pointer
@@ -1450,9 +1494,9 @@ do_it_again:
  *
  *     This code must be sure never to sleep through a hangup.
  */
-static ssize_t write_chan(struct tty_struct * tty, struct file * file,
-                         const unsigned char * buf, size_t nr)
+
+static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
+                         const unsigned char *buf, size_t nr)
 {
        const unsigned char *b = buf;
        DECLARE_WAITQUEUE(wait, current);
@@ -1495,11 +1539,11 @@ static ssize_t write_chan(struct tty_struct * tty, struct file * file,
                                        break;
                                b++; nr--;
                        }
-                       if (tty->driver->flush_chars)
-                               tty->driver->flush_chars(tty);
+                       if (tty->ops->flush_chars)
+                               tty->ops->flush_chars(tty);
                } else {
                        while (nr > 0) {
-                               c = tty->driver->write(tty, b, nr);
+                               c = tty->ops->write(tty, b, nr);
                                if (c < 0) {
                                        retval = c;
                                        goto break_out;
@@ -1525,7 +1569,7 @@ break_out:
 }
 
 /**
- *     normal_poll             -       poll method for N_TTY
+ *     n_tty_poll              -       poll method for N_TTY
  *     @tty: terminal device
  *     @file: file accessing it
  *     @wait: poll table
@@ -1536,14 +1580,10 @@ break_out:
  *
  *     This code must be sure never to sleep through a hangup.
  *     Called without the kernel lock held - fine
- *
- *     FIXME: if someone changes the VMIN or discipline settings for the
- *     terminal while another process is in poll() the poll does not
- *     recompute the new limits. Possibly set_termios should issue
- *     a read wakeup to fix this bug.
  */
-static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
+
+static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
+                                                       poll_table *wait)
 {
        unsigned int mask = 0;
 
@@ -1563,25 +1603,63 @@ static unsigned int normal_poll(struct tty_struct * tty, struct file * file, pol
                else
                        tty->minimum_to_wake = 1;
        }
-       if (!tty_is_writelocked(tty) &&
-                       tty->driver->chars_in_buffer(tty) < WAKEUP_CHARS &&
-                       tty->driver->write_room(tty) > 0)
+       if (tty->ops->write && !tty_is_writelocked(tty) &&
+                       tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
+                       tty_write_room(tty) > 0)
                mask |= POLLOUT | POLLWRNORM;
        return mask;
 }
 
-struct tty_ldisc tty_ldisc_N_TTY = {
+static unsigned long inq_canon(struct tty_struct *tty)
+{
+       int nr, head, tail;
+
+       if (!tty->canon_data)
+               return 0;
+       head = tty->canon_head;
+       tail = tty->read_tail;
+       nr = (head - tail) & (N_TTY_BUF_SIZE-1);
+       /* Skip EOF-chars.. */
+       while (head != tail) {
+               if (test_bit(tail, tty->read_flags) &&
+                   tty->read_buf[tail] == __DISABLED_CHAR)
+                       nr--;
+               tail = (tail+1) & (N_TTY_BUF_SIZE-1);
+       }
+       return nr;
+}
+
+static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
+                      unsigned int cmd, unsigned long arg)
+{
+       int retval;
+
+       switch (cmd) {
+       case TIOCOUTQ:
+               return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
+       case TIOCINQ:
+               /* FIXME: Locking */
+               retval = tty->read_cnt;
+               if (L_ICANON(tty))
+                       retval = inq_canon(tty);
+               return put_user(retval, (unsigned int __user *) arg);
+       default:
+               return n_tty_ioctl_helper(tty, file, cmd, arg);
+       }
+}
+
+struct tty_ldisc_ops tty_ldisc_N_TTY = {
        .magic           = TTY_LDISC_MAGIC,
        .name            = "n_tty",
        .open            = n_tty_open,
        .close           = n_tty_close,
        .flush_buffer    = n_tty_flush_buffer,
        .chars_in_buffer = n_tty_chars_in_buffer,
-       .read            = read_chan,
-       .write           = write_chan,
+       .read            = n_tty_read,
+       .write           = n_tty_write,
        .ioctl           = n_tty_ioctl,
        .set_termios     = n_tty_set_termios,
-       .poll            = normal_poll,
+       .poll            = n_tty_poll,
        .receive_buf     = n_tty_receive_buf,
        .write_wakeup    = n_tty_write_wakeup
 };