serial: core: Privatize modem status enable flags
authorPeter Hurley <peter@hurleysoftware.com>
Wed, 10 Sep 2014 19:06:24 +0000 (15:06 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 24 Sep 2014 04:19:35 +0000 (21:19 -0700)
The serial core uses the tty port flags, ASYNC_CTS_FLOW and
ASYNC_CD_CHECK, to track whether CTS and DCD changes should be
ignored or handled. However, the tty port flags are not safe for
atomic bit operations and no lock provides serialized updates.

Introduce the struct uart_port status field to track CTS and DCD
enable states, and serialize access with uart port lock. Substitute
uart_cts_enabled() helper for tty_port_cts_enabled().

Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/tty/serial/mxs-auart.c
drivers/tty/serial/serial_core.c
include/linux/serial_core.h

index b5c329248c810c07d2bb84beacc7b608b8248545..10c29334fe2f46b29e3a68a675cd02601d91293d 100644 (file)
@@ -408,7 +408,7 @@ static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
 
        ctrl &= ~(AUART_CTRL2_RTSEN | AUART_CTRL2_RTS);
        if (mctrl & TIOCM_RTS) {
-               if (tty_port_cts_enabled(&u->state->port))
+               if (uart_cts_enabled(u))
                        ctrl |= AUART_CTRL2_RTSEN;
                else
                        ctrl |= AUART_CTRL2_RTS;
index f764de32b658a6615131ff581d994cc0bf3d8772..dd21ed900635000d55f21c013b949c3b6e8f279f 100644 (file)
@@ -59,6 +59,11 @@ static void uart_change_pm(struct uart_state *state,
 
 static void uart_port_shutdown(struct tty_port *port);
 
+static int uart_dcd_enabled(struct uart_port *uport)
+{
+       return uport->status & UPSTAT_DCD_ENABLE;
+}
+
 /*
  * This routine is used by the interrupt handler to schedule processing in
  * the software interrupt portion of the driver.
@@ -130,7 +135,6 @@ static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
                int init_hw)
 {
        struct uart_port *uport = state->uart_port;
-       struct tty_port *port = &state->port;
        unsigned long page;
        int retval = 0;
 
@@ -176,12 +180,12 @@ static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
                                uart_set_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
                }
 
-               if (tty_port_cts_enabled(port)) {
-                       spin_lock_irq(&uport->lock);
+               spin_lock_irq(&uport->lock);
+               if (uart_cts_enabled(uport)) {
                        if (!(uport->ops->get_mctrl(uport) & TIOCM_CTS))
                                tty->hw_stopped = 1;
-                       spin_unlock_irq(&uport->lock);
                }
+               spin_unlock_irq(&uport->lock);
        }
 
        /*
@@ -435,7 +439,6 @@ EXPORT_SYMBOL(uart_get_divisor);
 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
                                        struct ktermios *old_termios)
 {
-       struct tty_port *port = &state->port;
        struct uart_port *uport = state->uart_port;
        struct ktermios *termios;
 
@@ -450,17 +453,19 @@ static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
        uport->ops->set_termios(uport, termios, old_termios);
 
        /*
-        * Set flags based on termios cflag
+        * Set modem status enables based on termios cflag
         */
+       spin_lock_irq(&uport->lock);
        if (termios->c_cflag & CRTSCTS)
-               set_bit(ASYNCB_CTS_FLOW, &port->flags);
+               uport->status |= UPSTAT_CTS_ENABLE;
        else
-               clear_bit(ASYNCB_CTS_FLOW, &port->flags);
+               uport->status &= ~UPSTAT_CTS_ENABLE;
 
        if (termios->c_cflag & CLOCAL)
-               clear_bit(ASYNCB_CHECK_CD, &port->flags);
+               uport->status &= ~UPSTAT_DCD_ENABLE;
        else
-               set_bit(ASYNCB_CHECK_CD, &port->flags);
+               uport->status |= UPSTAT_DCD_ENABLE;
+       spin_unlock_irq(&uport->lock);
 }
 
 static inline int __uart_put_char(struct uart_port *port,
@@ -2765,7 +2770,7 @@ void uart_handle_dcd_change(struct uart_port *uport, unsigned int status)
 
        uport->icount.dcd++;
 
-       if (port->flags & ASYNC_CHECK_CD) {
+       if (uart_dcd_enabled(uport)) {
                if (status)
                        wake_up_interruptible(&port->open_wait);
                else if (tty)
@@ -2790,7 +2795,7 @@ void uart_handle_cts_change(struct uart_port *uport, unsigned int status)
 
        uport->icount.cts++;
 
-       if (tty_port_cts_enabled(port)) {
+       if (uart_cts_enabled(uport)) {
                if (tty->hw_stopped) {
                        if (status) {
                                tty->hw_stopped = 0;
index 3bd7d55eebcecb581631273420c439d4884fe751..452c9cc9d717645fe7566cf219e558c458933134 100644 (file)
@@ -112,6 +112,7 @@ struct uart_icount {
 };
 
 typedef unsigned int __bitwise__ upf_t;
+typedef unsigned int __bitwise__ upstat_t;
 
 struct uart_port {
        spinlock_t              lock;                   /* port lock */
@@ -190,6 +191,12 @@ struct uart_port {
 #define UPF_CHANGE_MASK                ((__force upf_t) (0x17fff))
 #define UPF_USR_MASK           ((__force upf_t) (UPF_SPD_MASK|UPF_LOW_LATENCY))
 
+       /* status must be updated while holding port lock */
+       upstat_t                status;
+
+#define UPSTAT_CTS_ENABLE      ((__force upstat_t) (1 << 0))
+#define UPSTAT_DCD_ENABLE      ((__force upstat_t) (1 << 1))
+
        unsigned int            mctrl;                  /* current modem ctrl settings */
        unsigned int            timeout;                /* character-based timeout */
        unsigned int            type;                   /* port type */
@@ -355,6 +362,11 @@ static inline int uart_tx_stopped(struct uart_port *port)
        return 0;
 }
 
+static inline bool uart_cts_enabled(struct uart_port *uport)
+{
+       return uport->status & UPSTAT_CTS_ENABLE;
+}
+
 /*
  * The following are helper functions for the low level drivers.
  */