serial: core: fix potential NULL pointer dereference
[cascardo/linux.git] / drivers / tty / serial / serial_core.c
index 9fc1533..6e4f636 100644 (file)
@@ -235,18 +235,9 @@ static int uart_startup(struct tty_struct *tty, struct uart_state *state,
        if (tty_port_initialized(port))
                return 0;
 
-       /*
-        * Set the TTY IO error marker - we will only clear this
-        * once we have successfully opened the port.
-        */
-       set_bit(TTY_IO_ERROR, &tty->flags);
-
        retval = uart_port_startup(tty, state, init_hw);
-       if (!retval) {
-               tty_port_set_initialized(port, 1);
-               clear_bit(TTY_IO_ERROR, &tty->flags);
-       } else if (retval > 0)
-               retval = 0;
+       if (retval)
+               set_bit(TTY_IO_ERROR, &tty->flags);
 
        return retval;
 }
@@ -972,8 +963,11 @@ static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
                        }
                        uart_change_speed(tty, state, NULL);
                }
-       } else
+       } else {
                retval = uart_startup(tty, state, 1);
+               if (retval > 0)
+                       retval = 0;
+       }
  exit:
        return retval;
 }
@@ -1139,6 +1133,8 @@ static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
                uport->ops->config_port(uport, flags);
 
                ret = uart_startup(tty, state, 1);
+               if (ret > 0)
+                       ret = 0;
        }
 out:
        mutex_unlock(&port->mutex);
@@ -1465,7 +1461,6 @@ static void uart_close(struct tty_struct *tty, struct file *filp)
 {
        struct uart_state *state = tty->driver_data;
        struct tty_port *port;
-       struct uart_port *uport;
 
        if (!state) {
                struct uart_driver *drv = tty->driver->driver_state;
@@ -1481,56 +1476,36 @@ static void uart_close(struct tty_struct *tty, struct file *filp)
        port = &state->port;
        pr_debug("uart_close(%d) called\n", tty->index);
 
-       if (tty_port_close_start(port, tty, filp) == 0)
-               return;
+       tty_port_close(tty->port, tty, filp);
+}
 
-       mutex_lock(&port->mutex);
-       uport = uart_port_check(state);
+static void uart_tty_port_shutdown(struct tty_port *port)
+{
+       struct uart_state *state = container_of(port, struct uart_state, port);
+       struct uart_port *uport = uart_port_check(state);
 
        /*
         * At this point, we stop accepting input.  To do this, we
         * disable the receive line status interrupts.
         */
-       if (tty_port_initialized(port) &&
-           !WARN(!uport, "detached port still initialized!\n")) {
-               spin_lock_irq(&uport->lock);
-               uport->ops->stop_rx(uport);
-               spin_unlock_irq(&uport->lock);
-               /*
-                * Before we drop DTR, make sure the UART transmitter
-                * has completely drained; this is especially
-                * important if there is a transmit FIFO!
-                */
-               uart_wait_until_sent(tty, uport->timeout);
-       }
-
-       uart_shutdown(tty, state);
-       tty_port_tty_set(port, NULL);
+       if (WARN(!uport, "detached port still initialized!\n"))
+               return;
 
-       spin_lock_irq(&port->lock);
+       spin_lock_irq(&uport->lock);
+       uport->ops->stop_rx(uport);
+       spin_unlock_irq(&uport->lock);
 
-       if (port->blocked_open) {
-               spin_unlock_irq(&port->lock);
-               if (port->close_delay)
-                       msleep_interruptible(jiffies_to_msecs(port->close_delay));
-               spin_lock_irq(&port->lock);
-       } else if (uport && !uart_console(uport)) {
-               spin_unlock_irq(&port->lock);
-               uart_change_pm(state, UART_PM_STATE_OFF);
-               spin_lock_irq(&port->lock);
-       }
-       spin_unlock_irq(&port->lock);
-       tty_port_set_active(port, 0);
+       uart_port_shutdown(port);
 
        /*
-        * Wake up anyone trying to open this port.
+        * It's possible for shutdown to be called after suspend if we get
+        * a DCD drop (hangup) at just the right time.  Clear suspended bit so
+        * we don't try to resume a port that has been shutdown.
         */
-       wake_up_interruptible(&port->open_wait);
+       tty_port_set_suspended(port, 0);
 
-       mutex_unlock(&port->mutex);
+       uart_change_pm(state, UART_PM_STATE_OFF);
 
-       tty_ldisc_flush(tty);
-       tty->closing = 0;
 }
 
 static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
@@ -1711,52 +1686,31 @@ static int uart_open(struct tty_struct *tty, struct file *filp)
        struct uart_driver *drv = tty->driver->driver_state;
        int retval, line = tty->index;
        struct uart_state *state = drv->state + line;
-       struct tty_port *port = &state->port;
-       struct uart_port *uport;
 
-       pr_debug("uart_open(%d) called\n", line);
+       tty->driver_data = state;
 
-       spin_lock_irq(&port->lock);
-       ++port->count;
-       spin_unlock_irq(&port->lock);
+       retval = tty_port_open(&state->port, tty, filp);
+       if (retval > 0)
+               retval = 0;
 
-       /*
-        * We take the semaphore here to guarantee that we won't be re-entered
-        * while allocating the state structure, or while we request any IRQs
-        * that the driver may need.  This also has the nice side-effect that
-        * it delays the action of uart_hangup, so we can guarantee that
-        * state->port.tty will always contain something reasonable.
-        */
-       if (mutex_lock_interruptible(&port->mutex)) {
-               retval = -ERESTARTSYS;
-               goto end;
-       }
+       return retval;
+}
+
+static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
+{
+       struct uart_state *state = container_of(port, struct uart_state, port);
+       struct uart_port *uport;
 
        uport = uart_port_check(state);
-       if (!uport || uport->flags & UPF_DEAD) {
-               retval = -ENXIO;
-               goto err_unlock;
-       }
+       if (!uport || uport->flags & UPF_DEAD)
+               return -ENXIO;
 
-       tty->driver_data = state;
-       uport->state = state;
        port->low_latency = (uport->flags & UPF_LOW_LATENCY) ? 1 : 0;
-       tty_port_tty_set(port, tty);
 
        /*
         * Start up the serial port.
         */
-       retval = uart_startup(tty, state, 0);
-
-       /*
-        * If we succeeded, wait until the port is ready.
-        */
-err_unlock:
-       mutex_unlock(&port->mutex);
-       if (retval == 0)
-               retval = tty_port_block_til_ready(port, tty, filp);
-end:
-       return retval;
+       return uart_startup(tty, state, 0);
 }
 
 static const char *uart_type(struct uart_port *port)
@@ -1940,7 +1894,7 @@ uart_get_console(struct uart_port *ports, int nr, struct console *co)
  *
  *     Returns 0 on success or -EINVAL on failure
  */
-int uart_parse_earlycon(char *p, unsigned char *iotype, unsigned long *addr,
+int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
                        char **options)
 {
        if (strncmp(p, "mmio,", 5) == 0) {
@@ -1968,7 +1922,11 @@ int uart_parse_earlycon(char *p, unsigned char *iotype, unsigned long *addr,
                return -EINVAL;
        }
 
-       *addr = simple_strtoul(p, NULL, 0);
+       /*
+        * Before you replace it with kstrtoull(), think about options separator
+        * (',') it will not tolerate
+        */
+       *addr = simple_strtoull(p, NULL, 0);
        p = strchr(p, ',');
        if (p)
                p++;
@@ -2470,6 +2428,8 @@ static const struct tty_operations uart_ops = {
 static const struct tty_port_operations uart_port_ops = {
        .carrier_raised = uart_carrier_raised,
        .dtr_rts        = uart_dtr_rts,
+       .activate       = uart_port_activate,
+       .shutdown       = uart_tty_port_shutdown,
 };
 
 /**
@@ -2786,6 +2746,8 @@ int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
        uport->cons = drv->cons;
        uport->minor = drv->tty_driver->minor_start + uport->line;
 
+       port->console = uart_console(uport);
+
        /*
         * If this port is a console, then the spinlock is already
         * initialised.