diff --git a/drivers/tty/serial/apple_dockchannel_uart-test.c b/drivers/tty/serial/apple_dockchannel_uart-test.c index 0556266de9628..959732a7c576a 100644 --- a/drivers/tty/serial/apple_dockchannel_uart-test.c +++ b/drivers/tty/serial/apple_dockchannel_uart-test.c @@ -674,6 +674,254 @@ static void dcut_drain_timer_no_reader(struct kunit *test) KUNIT_EXPECT_EQ(test, ctx->rx_ncounts, 0); } +static void dcut_drain_timer_idle_backoff(struct kunit *test) +{ + struct dcut_ctx *ctx = test->priv; + struct dcuart_port *dc = &ctx->dcuart; + const unsigned int expected[] = { 20, 40, 80, SW_DRAIN_IDLE_MS, SW_DRAIN_IDLE_MS }; + unsigned int got_ms; + bool pending; + int ret; + size_t i; + + dc->sw_drain = true; + local_bh_disable(); + ret = dcuart_startup(&dc->port); + got_ms = dc->drain_ms; + pending = timer_pending(&dc->drain_timer); + timer_delete(&dc->drain_timer); + local_bh_enable(); + timer_delete_sync(&dc->drain_timer); + + KUNIT_EXPECT_EQ(test, ret, 0); + KUNIT_EXPECT_TRUE(test, pending); + KUNIT_EXPECT_EQ(test, got_ms, SW_DRAIN_MS); + + for (i = 0; i < ARRAY_SIZE(expected); i++) { + local_bh_disable(); + dcuart_drain_timer(&dc->drain_timer); + got_ms = dc->drain_ms; + pending = timer_pending(&dc->drain_timer); + timer_delete(&dc->drain_timer); + local_bh_enable(); + timer_delete_sync(&dc->drain_timer); + + KUNIT_EXPECT_EQ(test, got_ms, expected[i]); + KUNIT_EXPECT_TRUE(test, pending); + } + + /* Receiving a byte snaps polling back to SW_DRAIN_MS. */ + dcut_queue_rx(ctx, 1, 1); + local_bh_disable(); + dcuart_drain_timer(&dc->drain_timer); + got_ms = dc->drain_ms; + pending = timer_pending(&dc->drain_timer); + timer_delete(&dc->drain_timer); + local_bh_enable(); + timer_delete_sync(&dc->drain_timer); + + KUNIT_EXPECT_EQ(test, dc->port.icount.rx, 1); + KUNIT_EXPECT_EQ(test, got_ms, SW_DRAIN_MS); + KUNIT_EXPECT_TRUE(test, pending); + + dcuart_shutdown(&dc->port); + KUNIT_EXPECT_FALSE(test, timer_pending(&dc->drain_timer)); +} + +static void dcut_drain_timer_closed_console(struct kunit *test) +{ + struct dcut_ctx *ctx = test->priv; + struct dcuart_port *dc = &ctx->dcuart; + unsigned long before, after, expires; + unsigned int got_ms; + bool pending; + + dc->sw_drain = true; + ctx->state->port.console = true; + KUNIT_EXPECT_EQ(test, dcuart_startup(&dc->port), 0); + timer_delete_sync(&dc->drain_timer); + + /* No tty open: skip RX MMIO and stop the drain timer completely. */ + dcut_queue_rx(ctx, 2, 2); + dcuart_drain_timer(&dc->drain_timer); + KUNIT_EXPECT_EQ(test, ctx->rx_ncounts, 0); + KUNIT_EXPECT_EQ(test, dc->port.icount.rx, 0); + KUNIT_EXPECT_FALSE(test, timer_pending(&dc->drain_timer)); + + /* Reopen restarts polling immediately at SW_DRAIN_MS via start_rx(). */ + ctx->state->port.count = 1; + local_bh_disable(); + before = jiffies; + dcuart_start_rx(&dc->port); + after = jiffies; + pending = timer_pending(&dc->drain_timer); + got_ms = dc->drain_ms; + expires = dc->drain_timer.expires; + timer_delete(&dc->drain_timer); + local_bh_enable(); + timer_delete_sync(&dc->drain_timer); + + KUNIT_EXPECT_TRUE(test, pending); + KUNIT_EXPECT_EQ(test, got_ms, SW_DRAIN_MS); + KUNIT_EXPECT_TRUE(test, time_before_eq(expires, + after + msecs_to_jiffies(SW_DRAIN_MS))); + KUNIT_EXPECT_TRUE(test, time_after_eq(expires, before)); + + /* Next callback drains the buffered bytes and stays at SW_DRAIN_MS. */ + local_bh_disable(); + dcuart_drain_timer(&dc->drain_timer); + got_ms = dc->drain_ms; + pending = timer_pending(&dc->drain_timer); + timer_delete(&dc->drain_timer); + local_bh_enable(); + timer_delete_sync(&dc->drain_timer); + + KUNIT_EXPECT_EQ(test, dc->port.icount.rx, 2); + KUNIT_EXPECT_EQ(test, got_ms, SW_DRAIN_MS); + KUNIT_EXPECT_TRUE(test, pending); + + /* stop_rx() pauses polling and start_rx() resumes it. */ + dcuart_stop_rx(&dc->port); + KUNIT_EXPECT_FALSE(test, timer_pending(&dc->drain_timer)); + dcuart_drain_timer(&dc->drain_timer); + KUNIT_EXPECT_FALSE(test, timer_pending(&dc->drain_timer)); + local_bh_disable(); + dcuart_start_rx(&dc->port); + pending = timer_pending(&dc->drain_timer); + timer_delete(&dc->drain_timer); + local_bh_enable(); + timer_delete_sync(&dc->drain_timer); + KUNIT_EXPECT_TRUE(test, pending); + + dcuart_shutdown(&dc->port); + KUNIT_EXPECT_FALSE(test, timer_pending(&dc->drain_timer)); +} + +static void dcut_shutdown_blocks_rearm(struct kunit *test) +{ + struct dcut_ctx *ctx = test->priv; + struct dcuart_port *dc = &ctx->dcuart; + + dc->sw_drain = true; + dc->never_wait = true; + KUNIT_EXPECT_EQ(test, dcuart_startup(&dc->port), 0); + dcuart_shutdown(&dc->port); + KUNIT_EXPECT_FALSE(test, timer_pending(&dc->drain_timer)); + + /* A racing timer callback, TX kick, or start_rx must not re-arm. */ + dcut_queue_tx(ctx, "abc"); + ctx->tx_room = 0; + dcuart_drain_timer(&dc->drain_timer); + KUNIT_EXPECT_FALSE(test, timer_pending(&dc->drain_timer)); + dcuart_start_tx(&dc->port); + KUNIT_EXPECT_FALSE(test, timer_pending(&dc->drain_timer)); + dcuart_start_rx(&dc->port); + KUNIT_EXPECT_FALSE(test, timer_pending(&dc->drain_timer)); + + /* Re-starting the port clears shutting_down and resumes polling. */ + KUNIT_EXPECT_EQ(test, dcuart_startup(&dc->port), 0); + KUNIT_EXPECT_TRUE(test, timer_pending(&dc->drain_timer)); + dcuart_shutdown(&dc->port); +} + +static void dcut_start_tx_shortens_backoff(struct kunit *test) +{ + struct dcut_ctx *ctx = test->priv; + struct dcuart_port *dc = &ctx->dcuart; + unsigned long backed_off_expires, tx_expires; + unsigned int backed_off_ms, tx_ms, gen_before, gen_after; + bool backed_off_pending, tx_pending; + int i, ret; + + dc->sw_drain = true; + dc->never_wait = true; + local_bh_disable(); + ret = dcuart_startup(&dc->port); + timer_delete(&dc->drain_timer); + local_bh_enable(); + timer_delete_sync(&dc->drain_timer); + KUNIT_EXPECT_EQ(test, ret, 0); + + /* Drive real backoff up to SW_DRAIN_IDLE_MS so expires reflects 100 ms. */ + local_bh_disable(); + for (i = 0; i < 4; i++) { + dcuart_drain_timer(&dc->drain_timer); + if (i < 3) + timer_delete(&dc->drain_timer); + } + backed_off_ms = dc->drain_ms; + backed_off_pending = timer_pending(&dc->drain_timer); + backed_off_expires = dc->drain_timer.expires; + gen_before = dc->drain_kick_gen; + + dcut_queue_tx(ctx, "hello"); + ctx->tx_room = 2; + dcuart_start_tx(&dc->port); + tx_ms = dc->drain_ms; + gen_after = dc->drain_kick_gen; + tx_pending = timer_pending(&dc->drain_timer); + tx_expires = dc->drain_timer.expires; + timer_delete(&dc->drain_timer); + local_bh_enable(); + timer_delete_sync(&dc->drain_timer); + + KUNIT_EXPECT_EQ(test, backed_off_ms, SW_DRAIN_IDLE_MS); + KUNIT_EXPECT_TRUE(test, backed_off_pending); + KUNIT_EXPECT_EQ(test, tx_ms, SW_DRAIN_MS); + KUNIT_EXPECT_NE(test, gen_after, gen_before); + KUNIT_EXPECT_TRUE(test, tx_pending); + KUNIT_EXPECT_TRUE(test, time_before(tx_expires, backed_off_expires)); + + dcuart_shutdown(&dc->port); +} + +static void dcut_preserves_earlier_deadline(struct kunit *test) +{ + struct dcut_ctx *ctx = test->priv; + struct dcuart_port *dc = &ctx->dcuart; + unsigned long early_expires, rx_expires, cb_expires; + unsigned int rx_ms, cb_ms; + bool rx_pending, cb_pending; + + dc->sw_drain = true; + ctx->state->port.console = true; + ctx->state->port.count = 1; + KUNIT_EXPECT_EQ(test, dcuart_startup(&dc->port), 0); + timer_delete_sync(&dc->drain_timer); + + /* + * Repeated start_rx() on an already-open console must not push an + * earlier pending deadline forward, and an empty RX poll must preserve + * a kick armed before the callback snapshots drain_kick_gen. + */ + local_bh_disable(); + early_expires = jiffies + 1; + dc->drain_ms = SW_DRAIN_MS; + mod_timer(&dc->drain_timer, early_expires); + + dcuart_start_rx(&dc->port); + rx_pending = timer_pending(&dc->drain_timer); + rx_expires = dc->drain_timer.expires; + rx_ms = dc->drain_ms; + + dcuart_drain_timer(&dc->drain_timer); + cb_pending = timer_pending(&dc->drain_timer); + cb_expires = dc->drain_timer.expires; + cb_ms = dc->drain_ms; + timer_delete(&dc->drain_timer); + local_bh_enable(); + timer_delete_sync(&dc->drain_timer); + + KUNIT_EXPECT_TRUE(test, rx_pending); + KUNIT_EXPECT_EQ(test, rx_expires, early_expires); + KUNIT_EXPECT_EQ(test, rx_ms, SW_DRAIN_MS); + KUNIT_EXPECT_TRUE(test, cb_pending); + KUNIT_EXPECT_EQ(test, cb_expires, early_expires); + KUNIT_EXPECT_EQ(test, cb_ms, SW_DRAIN_MS); + + dcuart_shutdown(&dc->port); +} + /* termios and port ops */ static void dcut_set_termios(struct kunit *test) @@ -837,6 +1085,11 @@ static struct kunit_case dcuart_test_cases[] = { KUNIT_CASE(dcut_start_tx_waiting), KUNIT_CASE(dcut_drain_timer_sw_drain), KUNIT_CASE(dcut_drain_timer_no_reader), + KUNIT_CASE(dcut_drain_timer_idle_backoff), + KUNIT_CASE(dcut_drain_timer_closed_console), + KUNIT_CASE(dcut_shutdown_blocks_rearm), + KUNIT_CASE(dcut_start_tx_shortens_backoff), + KUNIT_CASE(dcut_preserves_earlier_deadline), KUNIT_CASE(dcut_set_termios), KUNIT_CASE(dcut_port_ops), KUNIT_CASE(dcut_console_putc_room), diff --git a/drivers/tty/serial/apple_dockchannel_uart.c b/drivers/tty/serial/apple_dockchannel_uart.c index 1411e84708c0d..489930828156c 100644 --- a/drivers/tty/serial/apple_dockchannel_uart.c +++ b/drivers/tty/serial/apple_dockchannel_uart.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "apple_dockchannel.h" @@ -46,10 +47,16 @@ #define MAX_PORTS 8 /* - * Software-drained channels are polled at this interval. At 115200 baud - * that is about 115 bytes per poll, far below the FIFO size. + * Software-drained channels poll at SW_DRAIN_MS while active (~115 bytes at + * 115200 baud, far below the 2 KiB FIFO). When open and idle they back off + * exponentially to SW_DRAIN_IDLE_MS (~1152 bytes at 115200 baud, still under + * the FIFO). A console port stays initialized after its last close (tty_port + * skips shutdown for consoles), so when no tty holds it open and no TX is + * pending the drain timer stops completely and resumes via start_rx() on + * reopen. */ -#define SW_DRAIN_MS 10 +#define SW_DRAIN_MS 10 +#define SW_DRAIN_IDLE_MS 100 /* * TX never waits for FIFO room: what does not fit stays in the xmit buffer * and the timer sends it as room appears. After this many ticks with no room @@ -67,8 +74,13 @@ struct dcuart_port { /* The channel does not interrupt on RX; drain it from a timer. */ bool sw_drain; struct timer_list drain_timer; - /* Serializes IRQ mask updates and RX FIFO draining. */ + /* Serializes IRQ mask updates, RX FIFO draining, and drain_timer state. */ spinlock_t lock; + bool drain_active; + bool rx_stopped; + bool shutting_down; + unsigned int drain_ms; + unsigned int drain_kick_gen; /* DOCKCHANNEL_NEVER_WAIT: TX never waits for room (J775D only). */ bool never_wait; /* One console write's view of the TX FIFO (dockchannel_console_putc). */ @@ -293,31 +305,94 @@ static unsigned int dcuart_tx_chars(struct dcuart_port *dcuart, bool tick) return 0; } -static void dcuart_drain_timer(struct timer_list *t) +static bool dcuart_has_reader(struct uart_port *port) { - struct dcuart_port *dcuart = timer_container_of(dcuart, t, drain_timer); - struct uart_port *port = &dcuart->port; + struct tty_port *tport = &port->state->port; unsigned long flags; - unsigned int pending; + bool open; - if (!dcuart->never_wait) { - bool got; + if (!tport->console) + return true; - spin_lock_irqsave(&dcuart->lock, flags); - got = dcuart_drain_rx(dcuart); - spin_unlock_irqrestore(&dcuart->lock, flags); + spin_lock_irqsave(&tport->lock, flags); + open = tport->count > 0 || tport->tty; + spin_unlock_irqrestore(&tport->lock, flags); + return open; +} - if (got) - tty_flip_buffer_push(&port->state->port); - mod_timer(&dcuart->drain_timer, jiffies + msecs_to_jiffies(SW_DRAIN_MS)); +static unsigned int dcuart_next_drain_ms(struct dcuart_port *dcuart, + bool active) +{ + unsigned int cur; + + if (active) + return SW_DRAIN_MS; + + cur = dcuart->drain_ms ? dcuart->drain_ms : SW_DRAIN_MS; + return min_t(unsigned int, cur * 2, SW_DRAIN_IDLE_MS); +} + +/* Called with dcuart->lock held. Never pushes an earlier deadline forward. */ +static void dcuart_arm_drain_timer_locked(struct dcuart_port *dcuart, + unsigned int delay_ms) +{ + unsigned long expires; + + if (dcuart->shutting_down) + return; + + expires = jiffies + msecs_to_jiffies(delay_ms); + if (timer_pending(&dcuart->drain_timer) && + time_before_eq(dcuart->drain_timer.expires, expires)) { + if (!dcuart->drain_ms || delay_ms < dcuart->drain_ms) + dcuart->drain_ms = delay_ms; + return; + } + + dcuart->drain_ms = delay_ms; + mod_timer(&dcuart->drain_timer, expires); +} + +static void dcuart_kick_drain_timer(struct dcuart_port *dcuart) +{ + unsigned long flags; + + if (!dcuart->sw_drain) return; + + spin_lock_irqsave(&dcuart->lock, flags); + if (dcuart->drain_active && !dcuart->shutting_down && + !dcuart->rx_stopped) { + dcuart->drain_kick_gen++; + dcuart_arm_drain_timer_locked(dcuart, SW_DRAIN_MS); } + spin_unlock_irqrestore(&dcuart->lock, flags); +} - if (dcuart->sw_drain) { - bool got; +static void dcuart_drain_timer(struct timer_list *t) +{ + struct dcuart_port *dcuart = timer_container_of(dcuart, t, drain_timer); + struct uart_port *port = &dcuart->port; + unsigned long flags; + unsigned int pending = 0; + unsigned int start_gen; + bool reader; + bool got = false; + /* + * Snapshot drain_kick_gen before checking reader presence: any reopen + * that runs after this point advances drain_kick_gen under dcuart->lock, + * while any reopen before it is already visible to dcuart_has_reader(). + */ + spin_lock_irqsave(&dcuart->lock, flags); + start_gen = dcuart->drain_kick_gen; + spin_unlock_irqrestore(&dcuart->lock, flags); + + reader = dcuart->sw_drain && dcuart_has_reader(port); + if (reader) { spin_lock_irqsave(&dcuart->lock, flags); - got = dcuart_drain_rx(dcuart); + if (!dcuart->shutting_down && !dcuart->rx_stopped) + got = dcuart_drain_rx(dcuart); spin_unlock_irqrestore(&dcuart->lock, flags); if (got) @@ -325,11 +400,24 @@ static void dcuart_drain_timer(struct timer_list *t) } uart_port_lock_irqsave(port, &flags); - pending = dcuart_tx_chars(dcuart, true); - uart_port_unlock_irqrestore(port, flags); + if (dcuart->never_wait) + pending = dcuart_tx_chars(dcuart, true); + + spin_lock(&dcuart->lock); + if (!dcuart->shutting_down) { + bool kicked = dcuart->drain_kick_gen != start_gen; + bool need_rx = dcuart->sw_drain && !dcuart->rx_stopped && + (reader || kicked); + + if (need_rx || pending) { + unsigned int delay = dcuart_next_drain_ms(dcuart, + got || pending || kicked); - if (dcuart->sw_drain || pending) - mod_timer(&dcuart->drain_timer, jiffies + msecs_to_jiffies(SW_DRAIN_MS)); + dcuart_arm_drain_timer_locked(dcuart, delay); + } + } + spin_unlock(&dcuart->lock); + uart_port_unlock_irqrestore(port, flags); } /* @@ -348,6 +436,7 @@ static unsigned int dcuart_tx_empty(struct uart_port *port) static void dcuart_start_tx(struct uart_port *port) { struct dcuart_port *dcuart = port_to_dc(port); + unsigned long flags; if (!dcuart->never_wait) { struct tty_port *tport = &port->state->port; @@ -357,25 +446,56 @@ static void dcuart_start_tx(struct uart_port *port) dcuart_putc(port, ch); port->icount.tx++; } + dcuart_kick_drain_timer(dcuart); return; } - /* The rest goes from the timer, which the RX poll keeps running anyway. */ - if (dcuart_tx_chars(dcuart, false) && !timer_pending(&dcuart->drain_timer)) - mod_timer(&dcuart->drain_timer, jiffies + msecs_to_jiffies(SW_DRAIN_MS)); + /* The rest goes from the timer; shorten any backed-off poll interval. */ + if (dcuart_tx_chars(dcuart, false)) { + spin_lock_irqsave(&dcuart->lock, flags); + if (!dcuart->shutting_down) { + dcuart->drain_kick_gen++; + dcuart_arm_drain_timer_locked(dcuart, SW_DRAIN_MS); + } + spin_unlock_irqrestore(&dcuart->lock, flags); + } else { + dcuart_kick_drain_timer(dcuart); + } } static void dcuart_stop_tx(struct uart_port *port) { } +static void dcuart_start_rx(struct uart_port *port) +{ + struct dcuart_port *dcuart = port_to_dc(port); + unsigned long flags; + + spin_lock_irqsave(&dcuart->lock, flags); + dcuart->rx_stopped = false; + if (dcuart->drain_active && !dcuart->shutting_down) { + if (dcuart->sw_drain) { + dcuart->drain_kick_gen++; + dcuart_arm_drain_timer_locked(dcuart, SW_DRAIN_MS); + } else { + dcuart_irq_enable(dcuart, dcuart->irq_rx); + } + } + spin_unlock_irqrestore(&dcuart->lock, flags); +} + static void dcuart_stop_rx(struct uart_port *port) { struct dcuart_port *dcuart = port_to_dc(port); unsigned long flags; spin_lock_irqsave(&dcuart->lock, flags); + dcuart->rx_stopped = true; dcuart_irq_disable(dcuart, dcuart->irq_rx); + if (dcuart->sw_drain && + (!dcuart->never_wait || kfifo_is_empty(&port->state->port.xmit_fifo))) + timer_delete(&dcuart->drain_timer); spin_unlock_irqrestore(&dcuart->lock, flags); } @@ -399,16 +519,19 @@ static int dcuart_startup(struct uart_port *port) port->state->port.closing_wait = ASYNC_CLOSING_WAIT_NONE; spin_lock_irqsave(&dcuart->lock, flags); + dcuart->shutting_down = false; + dcuart->rx_stopped = false; + dcuart->drain_active = true; dcuart_irq_disable(dcuart, dcuart->irq_tx | dcuart->irq_rx); writel_relaxed(1, dcuart->config_base + CONFIG_RX_THRESH); writel_relaxed(FIFO_SIZE, dcuart->config_base + CONFIG_TX_THRESH); if (!dcuart->sw_drain) dcuart_irq_enable(dcuart, dcuart->irq_rx); + else + dcuart_arm_drain_timer_locked(dcuart, SW_DRAIN_MS); spin_unlock_irqrestore(&dcuart->lock, flags); - if (dcuart->sw_drain) - mod_timer(&dcuart->drain_timer, jiffies + msecs_to_jiffies(SW_DRAIN_MS)); - else + if (!dcuart->sw_drain) enable_irq(port->irq); return 0; } @@ -418,15 +541,17 @@ static void dcuart_shutdown(struct uart_port *port) struct dcuart_port *dcuart = port_to_dc(port); unsigned long flags; + spin_lock_irqsave(&dcuart->lock, flags); + dcuart->shutting_down = true; + dcuart->drain_active = false; + dcuart_irq_disable(dcuart, dcuart->irq_tx | dcuart->irq_rx); + spin_unlock_irqrestore(&dcuart->lock, flags); + /* With never_wait the timer also sends TX in IRQ mode. */ if (dcuart->sw_drain || dcuart->never_wait) timer_delete_sync(&dcuart->drain_timer); if (!dcuart->sw_drain) disable_irq(port->irq); - - spin_lock_irqsave(&dcuart->lock, flags); - dcuart_irq_disable(dcuart, dcuart->irq_tx | dcuart->irq_rx); - spin_unlock_irqrestore(&dcuart->lock, flags); } static void dcuart_set_mctrl(struct uart_port *port, unsigned int mctrl) @@ -445,6 +570,7 @@ static void dcuart_set_termios(struct uart_port *port, struct ktermios *new, new->c_cflag |= CS8 | CLOCAL; tty_termios_encode_baud_rate(new, 115200, 115200); uart_update_timeout(port, new->c_cflag, 115200); + dcuart_kick_drain_timer(port_to_dc(port)); } static void dcuart_release_port(struct uart_port *port) @@ -466,6 +592,7 @@ static const struct uart_ops dcuart_ops = { .tx_empty = dcuart_tx_empty, .start_tx = dcuart_start_tx, .stop_tx = dcuart_stop_tx, + .start_rx = dcuart_start_rx, .stop_rx = dcuart_stop_rx, .type = dcuart_type, .startup = dcuart_startup, @@ -566,17 +693,43 @@ static int dcuart_probe(struct platform_device *pdev) static void dcuart_remove(struct platform_device *pdev) { struct dcuart_port *dcuart = platform_get_drvdata(pdev); + unsigned long flags; uart_remove_one_port(&dcuart_drv, &dcuart->port); + + /* A console port skips shutdown on hangup; ensure the timer stops. */ + spin_lock_irqsave(&dcuart->lock, flags); + dcuart->shutting_down = true; + dcuart->drain_active = false; + spin_unlock_irqrestore(&dcuart->lock, flags); + timer_delete_sync(&dcuart->drain_timer); + dcuart_ports[dcuart->port.line] = NULL; } +static int dcuart_suspend(struct device *dev) +{ + struct dcuart_port *dcuart = dev_get_drvdata(dev); + + return uart_suspend_port(&dcuart_drv, &dcuart->port); +} + +static int dcuart_resume(struct device *dev) +{ + struct dcuart_port *dcuart = dev_get_drvdata(dev); + + return uart_resume_port(&dcuart_drv, &dcuart->port); +} + +static DEFINE_SIMPLE_DEV_PM_OPS(dcuart_pm_ops, dcuart_suspend, dcuart_resume); + static struct platform_driver dcuart_platform_drv = { .probe = dcuart_probe, .remove = dcuart_remove, .driver = { .name = "apple-dockchannel-uart", .of_match_table = dcuart_id_table, + .pm = pm_sleep_ptr(&dcuart_pm_ops), }, }; diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c index 6332ed545c899..5424f6ceeb384 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -1944,11 +1944,23 @@ static int uart_install(struct tty_driver *driver, struct tty_struct *tty) static int uart_open(struct tty_struct *tty, struct file *filp) { struct uart_state *state = tty->driver_data; + struct tty_port *port = &state->port; + bool was_initialized = tty_port_initialized(port); int retval; - retval = tty_port_open(&state->port, tty, filp); + retval = tty_port_open(port, tty, filp); if (retval > 0) retval = 0; + if (!retval && was_initialized && port->console) { + struct uart_port *uport; + unsigned long flags; + + uport = uart_port_ref_lock(state, &flags); + if (uport && uport->type != PORT_UNKNOWN && !uport->irq && + !uport->suspended && uport->ops->start_rx) + uport->ops->start_rx(uport); + uart_port_unlock_deref(uport, flags); + } return retval; } @@ -2352,7 +2364,7 @@ int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport) guard(mutex)(&port->mutex); tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port); - if (tty_dev && device_may_wakeup(tty_dev)) { + if (uport->irq && tty_dev && device_may_wakeup(tty_dev)) { enable_irq_wake(uport->irq); put_device(tty_dev); return 0; @@ -2430,8 +2442,10 @@ int uart_resume_port(struct uart_driver *drv, struct uart_port *uport) guard(mutex)(&port->mutex); tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port); - if (!uport->suspended && device_may_wakeup(tty_dev)) { - if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq)))) + if (!uport->suspended && uport->irq && tty_dev && device_may_wakeup(tty_dev)) { + struct irq_data *irqd = irq_get_irq_data(uport->irq); + + if (irqd && irqd_is_wakeup_set(irqd)) disable_irq_wake(uport->irq); put_device(tty_dev); return 0;