Merge tag 'for-linus-20141215' of git://git.infradead.org/linux-mtd
[cascardo/linux.git] / drivers / tty / n_tty.c
index 2e900a9..d2b4967 100644 (file)
@@ -321,7 +321,8 @@ static void n_tty_check_unthrottle(struct tty_struct *tty)
 
 static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
 {
-       *read_buf_addr(ldata, ldata->read_head++) = c;
+       *read_buf_addr(ldata, ldata->read_head) = c;
+       ldata->read_head++;
 }
 
 /**
@@ -351,13 +352,13 @@ static void n_tty_packet_mode_flush(struct tty_struct *tty)
 {
        unsigned long flags;
 
-       spin_lock_irqsave(&tty->ctrl_lock, flags);
        if (tty->link->packet) {
+               spin_lock_irqsave(&tty->ctrl_lock, flags);
                tty->ctrl_status |= TIOCPKT_FLUSHREAD;
+               spin_unlock_irqrestore(&tty->ctrl_lock, flags);
                if (waitqueue_active(&tty->link->read_wait))
                        wake_up_interruptible(&tty->link->read_wait);
        }
-       spin_unlock_irqrestore(&tty->ctrl_lock, flags);
 }
 
 /**
@@ -2123,12 +2124,11 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
 {
        struct n_tty_data *ldata = tty->disc_data;
        unsigned char __user *b = buf;
-       DECLARE_WAITQUEUE(wait, current);
+       DEFINE_WAIT_FUNC(wait, woken_wake_function);
        int c;
        int minimum, time;
        ssize_t retval = 0;
        long timeout;
-       unsigned long flags;
        int packet;
 
        c = job_control(tty, file);
@@ -2174,10 +2174,10 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
                        unsigned char cs;
                        if (b != buf)
                                break;
-                       spin_lock_irqsave(&tty->link->ctrl_lock, flags);
+                       spin_lock_irq(&tty->link->ctrl_lock);
                        cs = tty->link->ctrl_status;
                        tty->link->ctrl_status = 0;
-                       spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
+                       spin_unlock_irq(&tty->link->ctrl_lock);
                        if (tty_put_user(tty, cs, b++)) {
                                retval = -EFAULT;
                                b--;
@@ -2186,10 +2186,6 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
                        nr--;
                        break;
                }
-               /* This statement must be first before checking for input
-                  so that any interrupt will set the state back to
-                  TASK_RUNNING. */
-               set_current_state(TASK_INTERRUPTIBLE);
 
                if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
                    ((minimum - (b - buf)) >= 1))
@@ -2197,45 +2193,29 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
 
                if (!input_available_p(tty, 0)) {
                        if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
-                               up_read(&tty->termios_rwsem);
-                               tty_flush_to_ldisc(tty);
-                               down_read(&tty->termios_rwsem);
-                               if (!input_available_p(tty, 0)) {
-                                       retval = -EIO;
-                                       break;
-                               }
-                       } else {
-                               if (tty_hung_up_p(file))
-                                       break;
-                               if (!timeout)
-                                       break;
-                               if (file->f_flags & O_NONBLOCK) {
-                                       retval = -EAGAIN;
-                                       break;
-                               }
-                               if (signal_pending(current)) {
-                                       retval = -ERESTARTSYS;
-                                       break;
-                               }
-                               n_tty_set_room(tty);
-                               up_read(&tty->termios_rwsem);
-
-                               timeout = schedule_timeout(timeout);
-
-                               down_read(&tty->termios_rwsem);
-                               continue;
+                               retval = -EIO;
+                               break;
                        }
-               }
-               __set_current_state(TASK_RUNNING);
-
-               /* Deal with packet mode. */
-               if (packet && b == buf) {
-                       if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
-                               retval = -EFAULT;
-                               b--;
+                       if (tty_hung_up_p(file))
+                               break;
+                       if (!timeout)
+                               break;
+                       if (file->f_flags & O_NONBLOCK) {
+                               retval = -EAGAIN;
                                break;
                        }
-                       nr--;
+                       if (signal_pending(current)) {
+                               retval = -ERESTARTSYS;
+                               break;
+                       }
+                       n_tty_set_room(tty);
+                       up_read(&tty->termios_rwsem);
+
+                       timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
+                                            timeout);
+
+                       down_read(&tty->termios_rwsem);
+                       continue;
                }
 
                if (ldata->icanon && !L_EXTPROC(tty)) {
@@ -2247,8 +2227,17 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
                                break;
                } else {
                        int uncopied;
-                       /* The copy function takes the read lock and handles
-                          locking internally for this case */
+
+                       /* Deal with packet mode. */
+                       if (packet && b == buf) {
+                               if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
+                                       retval = -EFAULT;
+                                       b--;
+                                       break;
+                               }
+                               nr--;
+                       }
+
                        uncopied = copy_from_read_buf(tty, &b, &nr);
                        uncopied += copy_from_read_buf(tty, &b, &nr);
                        if (uncopied) {
@@ -2273,7 +2262,6 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
 
        mutex_unlock(&ldata->atomic_read_lock);
 
-       __set_current_state(TASK_RUNNING);
        if (b - buf)
                retval = b - buf;
 
@@ -2306,7 +2294,7 @@ 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);
+       DEFINE_WAIT_FUNC(wait, woken_wake_function);
        int c;
        ssize_t retval = 0;
 
@@ -2324,7 +2312,6 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
 
        add_wait_queue(&tty->write_wait, &wait);
        while (1) {
-               set_current_state(TASK_INTERRUPTIBLE);
                if (signal_pending(current)) {
                        retval = -ERESTARTSYS;
                        break;
@@ -2378,12 +2365,11 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
                }
                up_read(&tty->termios_rwsem);
 
-               schedule();
+               wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
 
                down_read(&tty->termios_rwsem);
        }
 break_out:
-       __set_current_state(TASK_RUNNING);
        remove_wait_queue(&tty->write_wait, &wait);
        if (b - buf != nr && tty->fasync)
                set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);