fb:support 180 degree rotate
[firefly-linux-kernel-4.4.55.git] / drivers / char / tty_ldisc.c
index aafdbaebc16a8697df6f16efbaadbce255e05d86..cf0bfc6b1dbd64753372c3c59bf6b81a4a3a0512 100644 (file)
@@ -45,6 +45,7 @@
 
 static DEFINE_SPINLOCK(tty_ldisc_lock);
 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
+static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_idle);
 /* Line disc dispatch table */
 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
 
@@ -81,6 +82,7 @@ static void put_ldisc(struct tty_ldisc *ld)
                return;
        }
        local_irq_restore(flags);
+       wake_up(&tty_ldisc_idle);
 }
 
 /**
@@ -442,9 +444,15 @@ static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
 
 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
 {
+       int ret;
+
        WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
-       if (ld->ops->open)
-               return ld->ops->open(tty);
+       if (ld->ops->open) {
+               ret = ld->ops->open(tty);
+               if (ret)
+                       clear_bit(TTY_LDISC_OPEN, &tty->flags);
+               return ret;
+       }
        return 0;
 }
 
@@ -518,7 +526,24 @@ static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
 static int tty_ldisc_halt(struct tty_struct *tty)
 {
        clear_bit(TTY_LDISC, &tty->flags);
-       return cancel_delayed_work(&tty->buf.work);
+       return cancel_delayed_work_sync(&tty->buf.work);
+}
+
+/**
+ *     tty_ldisc_wait_idle     -       wait for the ldisc to become idle
+ *     @tty: tty to wait for
+ *
+ *     Wait for the line discipline to become idle. The discipline must
+ *     have been halted for this to guarantee it remains idle.
+ */
+static int tty_ldisc_wait_idle(struct tty_struct *tty)
+{
+       int ret;
+       ret = wait_event_interruptible_timeout(tty_ldisc_idle,
+                       atomic_read(&tty->ldisc->users) == 1, 5 * HZ);
+       if (ret < 0)
+               return ret;
+       return ret > 0 ? 0 : -EBUSY;
 }
 
 /**
@@ -616,7 +641,16 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 
        flush_scheduled_work();
 
+       retval = tty_ldisc_wait_idle(tty);
+
        mutex_lock(&tty->ldisc_mutex);
+
+       /* handle wait idle failure locked */
+       if (retval) {
+               tty_ldisc_put(new_ldisc);
+               goto enable;
+       }
+
        if (test_bit(TTY_HUPPED, &tty->flags)) {
                /* We were raced by the hangup method. It will have stomped
                   the ldisc data and closed the ldisc down */
@@ -649,6 +683,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 
        tty_ldisc_put(o_ldisc);
 
+enable:
        /*
         *      Allow ldisc referencing to occur again
         */
@@ -687,14 +722,18 @@ static void tty_reset_termios(struct tty_struct *tty)
 /**
  *     tty_ldisc_reinit        -       reinitialise the tty ldisc
  *     @tty: tty to reinit
+ *     @ldisc: line discipline to reinitialize
  *
- *     Switch the tty back to N_TTY line discipline and leave the
- *     ldisc state closed
+ *     Switch the tty to a line discipline and leave the ldisc
+ *     state closed
  */
 
-static void tty_ldisc_reinit(struct tty_struct *tty)
+static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
 {
-       struct tty_ldisc *ld;
+       struct tty_ldisc *ld = tty_ldisc_get(ldisc);
+
+       if (IS_ERR(ld))
+               return -1;
 
        tty_ldisc_close(tty, tty->ldisc);
        tty_ldisc_put(tty->ldisc);
@@ -702,10 +741,10 @@ static void tty_ldisc_reinit(struct tty_struct *tty)
        /*
         *      Switch the line discipline back
         */
-       ld = tty_ldisc_get(N_TTY);
-       BUG_ON(IS_ERR(ld));
        tty_ldisc_assign(tty, ld);
-       tty_set_termios_ldisc(tty, N_TTY);
+       tty_set_termios_ldisc(tty, ldisc);
+
+       return 0;
 }
 
 /**
@@ -726,6 +765,8 @@ static void tty_ldisc_reinit(struct tty_struct *tty)
 void tty_ldisc_hangup(struct tty_struct *tty)
 {
        struct tty_ldisc *ld;
+       int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
+       int err = 0;
 
        /*
         * FIXME! What are the locking issues here? This may me overdoing
@@ -753,28 +794,35 @@ void tty_ldisc_hangup(struct tty_struct *tty)
        wake_up_interruptible_poll(&tty->read_wait, POLLIN);
        /*
         * Shutdown the current line discipline, and reset it to
-        * N_TTY.
+        * N_TTY if need be.
+        *
+        * Avoid racing set_ldisc or tty_ldisc_release
         */
-       if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
-               /* Make sure the old ldisc is quiescent */
-               tty_ldisc_halt(tty);
-               flush_scheduled_work();
-
-               /* Avoid racing set_ldisc or tty_ldisc_release */
-               mutex_lock(&tty->ldisc_mutex);
-               if (tty->ldisc) {       /* Not yet closed */
-                       /* Switch back to N_TTY */
-                       tty_ldisc_reinit(tty);
-                       /* At this point we have a closed ldisc and we want to
-                          reopen it. We could defer this to the next open but
-                          it means auditing a lot of other paths so this is
-                          a FIXME */
+       mutex_lock(&tty->ldisc_mutex);
+       tty_ldisc_halt(tty);
+       /* At this point we have a closed ldisc and we want to
+          reopen it. We could defer this to the next open but
+          it means auditing a lot of other paths so this is
+          a FIXME */
+       if (tty->ldisc) {       /* Not yet closed */
+               if (reset == 0) {
+
+                       if (!tty_ldisc_reinit(tty, tty->termios->c_line))
+                               err = tty_ldisc_open(tty, tty->ldisc);
+                       else
+                               err = 1;
+               }
+               /* If the re-open fails or we reset then go to N_TTY. The
+                  N_TTY open cannot fail */
+               if (reset || err) {
+                       BUG_ON(tty_ldisc_reinit(tty, N_TTY));
                        WARN_ON(tty_ldisc_open(tty, tty->ldisc));
-                       tty_ldisc_enable(tty);
                }
-               mutex_unlock(&tty->ldisc_mutex);
-               tty_reset_termios(tty);
+               tty_ldisc_enable(tty);
        }
+       mutex_unlock(&tty->ldisc_mutex);
+       if (reset)
+               tty_reset_termios(tty);
 }
 
 /**