mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-04 06:10:03 +08:00
898 lines
29 KiB
Diff
898 lines
29 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Pooya Parsa <pooya@pi0.io>
|
|
Date: Sat, 28 Nov 2026 23:53:12 +0000
|
|
Subject: [PATCH] wasm: kwserial, serial ports of the JavaScript host
|
|
|
|
Gives the guest the host's serial ports (in a browser, ports picked
|
|
through the Web Serial API) as /dev/ttyACM0 to ttyACM3, on serial_core.
|
|
Nothing is mapped: every port operation is a host import, and the host
|
|
copies the bytes during the calls.
|
|
|
|
Eight imports, none of which suspends: wasm_driver_serial_init() (the
|
|
number of ports, 1..4; <= 0: no device, and then nothing is registered
|
|
or allocated), _open(port) (the first open; -ENODEV when the host has
|
|
no port in that slot), _close(port), _config(port, baud, cfg) (data
|
|
bits, two stop bits, odd or even parity, CRTSCTS), _write(port, buf,
|
|
len) and _read(port, buf, max), which return how many bytes the host
|
|
took or gave, _set_signals(port, signals) (DTR, RTS, BREAK) and
|
|
_status(port): the input signals, whether a port is attached and
|
|
whether the host's transmit queue is empty, and events that the read
|
|
clears (data received, room to transmit, an input signal changed, a
|
|
hangup, and break, framing, parity and overrun errors). The host raises
|
|
WASM_IRQ_SERIAL (12), one bit for all ports, when it sets an event.
|
|
|
|
The handler reports the receive errors before the data of the same
|
|
interrupt (the host cannot place them in the byte stream; with INPCK a
|
|
NUL takes the bad character's place), drains the received bytes into
|
|
the tty, reports DCD and CTS changes through serial_core and wakes
|
|
TIOCMIWAIT for all four inputs, and sends more output. A short write
|
|
waits for the room event; tx_empty asks the host, so tcdrain waits
|
|
until the host's queue is empty. With CRTSCTS the host does the flow
|
|
control (AUTORTS and AUTOCTS), and on throttle the driver stops reading
|
|
from the host, which keeps the bytes, in every mode (UPSTAT_SYNC_FIFO).
|
|
A hangup event hangs the tty up; so does a restored snapshot, from the
|
|
park restore notifier, for every port that was open, since the new
|
|
host has none. serial_core lets CAP_SYS_ADMIN open a port whose startup
|
|
failed, for setserial; the driver's tty open fails it for root too.
|
|
|
|
On SMP the host's imports answer on CPU 0 alone, and its interrupt
|
|
arrives there. startup, shutdown, set_termios, break_ctl and tx_empty
|
|
run through kw_host_call(); start_tx, set_mctrl and unthrottle on
|
|
another CPU raise WASM_IRQ_SERIAL on CPU 0, whose handler sends the
|
|
output signals and the waiting bytes.
|
|
---
|
|
drivers/tty/serial/Kconfig | 11 +
|
|
drivers/tty/serial/Makefile | 1 +
|
|
drivers/tty/serial/kwserial.c | 814 ++++++++++++++++++++++++++++++++++
|
|
3 files changed, 826 insertions(+)
|
|
create mode 100644 drivers/tty/serial/kwserial.c
|
|
|
|
diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
|
|
--- a/drivers/tty/serial/Kconfig
|
|
+++ b/drivers/tty/serial/Kconfig
|
|
@@ -1592,6 +1592,17 @@ config SERIAL_NUVOTON_MA35D1_CONSOLE
|
|
but you can alter that using a kernel command line option such as
|
|
"console=ttyNVTx".
|
|
|
|
+config SERIAL_KWSERIAL
|
|
+ bool "unkernel host serial ports"
|
|
+ depends on WASM
|
|
+ select SERIAL_CORE
|
|
+ help
|
|
+ Serial ports of the JavaScript host of a unkernel instance (in a
|
|
+ browser, ports picked through the Web Serial API), as /dev/ttyACM0
|
|
+ to ttyACM3. Every port operation is a host import and the host
|
|
+ raises one interrupt for all ports. Without host ports nothing is
|
|
+ registered.
|
|
+
|
|
endmenu
|
|
|
|
config SERIAL_MCTRL_GPIO
|
|
diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile
|
|
--- a/drivers/tty/serial/Makefile
|
|
+++ b/drivers/tty/serial/Makefile
|
|
@@ -98,3 +98,4 @@ obj-$(CONFIG_SERIAL_MCTRL_GPIO) += serial_mctrl_gpio.o
|
|
|
|
obj-$(CONFIG_KGDB_SERIAL_CONSOLE) += kgdboc.o
|
|
obj-$(CONFIG_SERIAL_NUVOTON_MA35D1) += ma35d1_serial.o
|
|
+obj-$(CONFIG_SERIAL_KWSERIAL) += kwserial.o
|
|
diff --git a/drivers/tty/serial/kwserial.c b/drivers/tty/serial/kwserial.c
|
|
new file mode 100644
|
|
--- /dev/null
|
|
+++ b/drivers/tty/serial/kwserial.c
|
|
@@ -0,0 +1,814 @@
|
|
+// SPDX-License-Identifier: GPL-2.0-only
|
|
+/*
|
|
+ * kwserial: serial ports of the JavaScript host (unkernel).
|
|
+ *
|
|
+ * Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
+ *
|
|
+ * The host offers up to four serial ports (in a browser, ports picked
|
|
+ * through the Web Serial API; js/core/serial.ts in unkernel), which become
|
|
+ * /dev/ttyACM0.. on serial_core. Every port operation is a host import: open
|
|
+ * and close, the line settings, the output signals (DTR, RTS, BREAK), the
|
|
+ * bytes in each direction and a status word. Nothing is mapped and there
|
|
+ * is no FIFO to program; the host copies bytes during the calls.
|
|
+ *
|
|
+ * The status word has level bits (the input signals CTS, DSR, DCD and RI,
|
|
+ * whether a physical port is attached, whether the host's transmit queue is
|
|
+ * empty) and event bits, which the read clears: received data pending, room
|
|
+ * to transmit again, an input signal changed, the port hung up (unplugged,
|
|
+ * or the host could not open it), and the receive errors (break, framing,
|
|
+ * parity, overrun). The host raises WASM_IRQ_SERIAL, one bit for all ports,
|
|
+ * whenever it sets an event bit; the handler reads each open port's status,
|
|
+ * reports the errors (they are not positioned in the byte stream, so they
|
|
+ * come before the data of the same interrupt), drains the received bytes
|
|
+ * into the tty, reports the input signal changes (DCD and CTS through
|
|
+ * serial_core, all four to TIOCMIWAIT) and sends more output.
|
|
+ *
|
|
+ * Output: start_tx hands the transmit FIFO's bytes to the host, which takes
|
|
+ * what it has room for; when it takes less than offered it later sets the
|
|
+ * room event, and the rest follows from the interrupt. tx_empty asks the
|
|
+ * host whether its queue has drained (tcdrain polls it). With CRTSCTS the
|
|
+ * host does the RTS/CTS flow control itself (serial_core's AUTORTS and
|
|
+ * AUTOCTS), as a USB serial adapter does. When the line discipline
|
|
+ * throttles, the driver stops reading from the host, which keeps the bytes,
|
|
+ * and reads again on unthrottle; nothing received is dropped here.
|
|
+ *
|
|
+ * A hangup event hangs the tty up, so readers get EOF and writers EIO; the
|
|
+ * port opens again when the host has a port in that slot. A restored
|
|
+ * snapshot runs on a new host, which has no open port: every port that was
|
|
+ * open is hung up from the park restore notifier.
|
|
+ *
|
|
+ * Present only when the host offers ports: wasm_driver_serial_init() <= 0
|
|
+ * means none, and then nothing is registered or allocated. No import here
|
|
+ * suspends, and each is called with the port's lock held and interrupts
|
|
+ * off, so the host sees one port's calls in order.
|
|
+ *
|
|
+ * SMP: the host's imports answer on CPU 0 alone, and its interrupt arrives
|
|
+ * there. The process-context operations (startup, shutdown, set_termios,
|
|
+ * break_ctl, tx_empty) run through kw_host_call(). start_tx, set_mctrl and
|
|
+ * unthrottle, which may come in atomic context on another CPU, leave their
|
|
+ * work to CPU 0: they raise WASM_IRQ_SERIAL there, and its handler sends
|
|
+ * the output signals and the bytes waiting in the FIFO.
|
|
+ */
|
|
+
|
|
+#include <linux/init.h>
|
|
+#include <linux/interrupt.h>
|
|
+#include <linux/kw_park.h>
|
|
+#include <linux/notifier.h>
|
|
+#include <linux/platform_device.h>
|
|
+#include <linux/serial.h>
|
|
+#include <linux/serial_core.h>
|
|
+#include <linux/slab.h>
|
|
+#include <linux/tty.h>
|
|
+#include <linux/tty_flip.h>
|
|
+#include <linux/workqueue.h>
|
|
+#include <asm/irq.h>
|
|
+#include <asm/wasm.h>
|
|
+
|
|
+#define KWSERIAL_MAX_PORTS 4
|
|
+#define KWSERIAL_BAUD_MIN 50
|
|
+#define KWSERIAL_BAUD_MAX 4000000
|
|
+/*
|
|
+ * What the host may hold beyond the wire, for serial_core's estimate of
|
|
+ * how long a drain takes (tcdrain without CRTSCTS gives up after twice
|
|
+ * that).
|
|
+ */
|
|
+#define KWSERIAL_FIFO_SIZE 4096
|
|
+/* Bytes taken from the host per read, and reads per interrupt. */
|
|
+#define KWSERIAL_RX_CHUNK 512
|
|
+#define KWSERIAL_RX_BUDGET 128
|
|
+
|
|
+/* wasm_driver_serial_status(): level bits, not cleared by the read. */
|
|
+#define KWS_CTS BIT(0)
|
|
+#define KWS_DSR BIT(1)
|
|
+#define KWS_DCD BIT(2)
|
|
+#define KWS_RI BIT(3)
|
|
+#define KWS_ATTACHED BIT(4) /* a physical port is attached */
|
|
+#define KWS_TX_EMPTY BIT(5) /* the host's transmit queue is empty */
|
|
+#define KWS_INPUTS (KWS_CTS | KWS_DSR | KWS_DCD | KWS_RI)
|
|
+#define KWS_LEVELS (KWS_INPUTS | KWS_ATTACHED | KWS_TX_EMPTY)
|
|
+/* Event bits, cleared by the read. */
|
|
+#define KWS_EV_RX BIT(8) /* received data pending */
|
|
+#define KWS_EV_TX BIT(9) /* room to transmit again */
|
|
+#define KWS_EV_SIGNALS BIT(11) /* an input signal level changed */
|
|
+#define KWS_EV_HANGUP BIT(12) /* unplugged, or the host's open failed */
|
|
+#define KWS_EV_BREAK BIT(16)
|
|
+#define KWS_EV_FRAME BIT(17)
|
|
+#define KWS_EV_PARITY BIT(18)
|
|
+#define KWS_EV_OVERRUN BIT(19)
|
|
+#define KWS_EV_ERRORS (KWS_EV_BREAK | KWS_EV_FRAME | KWS_EV_PARITY | KWS_EV_OVERRUN)
|
|
+/* In ignore_status_mask only: CREAD is off, received bytes are dropped. */
|
|
+#define KWS_IGNORE_RX KWS_EV_RX
|
|
+
|
|
+/* wasm_driver_serial_config() @cfg. */
|
|
+#define KWS_CFG_BITS_MASK 0xf /* data bits, 5..8 */
|
|
+#define KWS_CFG_STOP2 BIT(4)
|
|
+#define KWS_CFG_PARITY_SHIFT 5 /* 0 none, 1 odd, 2 even */
|
|
+#define KWS_CFG_PARITY_ODD 1
|
|
+#define KWS_CFG_PARITY_EVEN 2
|
|
+#define KWS_CFG_CRTSCTS BIT(7)
|
|
+
|
|
+/* wasm_driver_serial_set_signals() @signals. */
|
|
+#define KWS_SIG_DTR BIT(0)
|
|
+#define KWS_SIG_RTS BIT(1)
|
|
+#define KWS_SIG_BREAK BIT(2)
|
|
+
|
|
+/* Host imports, module "unkernel" (js/core/serial.ts). None of them suspends. */
|
|
+/* The number of ports the host offers (1..4); <= 0: no device. */
|
|
+int wasm_driver_serial_init(void)
|
|
+ __unkernel_import(wasm_driver_serial_init);
|
|
+/*
|
|
+ * The first open of @port: 0, or -ENODEV when the host has no physical
|
|
+ * port in that slot now. The host opens the physical port asynchronously;
|
|
+ * a later failure is a hangup event.
|
|
+ */
|
|
+int wasm_driver_serial_open(u32 port)
|
|
+ __unkernel_import(wasm_driver_serial_open);
|
|
+/* The last close: the host drops what is unread and unsent. */
|
|
+void wasm_driver_serial_close(u32 port)
|
|
+ __unkernel_import(wasm_driver_serial_close);
|
|
+/* Line settings: @baud, and @cfg's data bits, stop bits, parity and CRTSCTS. */
|
|
+void wasm_driver_serial_config(u32 port, u32 baud, u32 cfg)
|
|
+ __unkernel_import(wasm_driver_serial_config);
|
|
+/*
|
|
+ * Offer @len bytes at @buf; returns how many the host copied (0..@len).
|
|
+ * After a short count the host sets KWS_EV_TX once it has room again.
|
|
+ */
|
|
+int wasm_driver_serial_write(u32 port, const void *buf, u32 len)
|
|
+ __unkernel_import(wasm_driver_serial_write);
|
|
+/* Copy up to @max received bytes to @buf; returns how many, 0 if none. */
|
|
+int wasm_driver_serial_read(u32 port, void *buf, u32 max)
|
|
+ __unkernel_import(wasm_driver_serial_read);
|
|
+/* The output signals (KWS_SIG_*), the whole state every time. */
|
|
+void wasm_driver_serial_set_signals(u32 port, u32 signals)
|
|
+ __unkernel_import(wasm_driver_serial_set_signals);
|
|
+/* The status word (KWS_*); the read clears the event bits. */
|
|
+u32 wasm_driver_serial_status(u32 port)
|
|
+ __unkernel_import(wasm_driver_serial_status);
|
|
+
|
|
+struct kwserial_port {
|
|
+ struct uart_port port;
|
|
+ /* Under port.lock: */
|
|
+ u32 levels; /* KWS_LEVELS of the last status read */
|
|
+ unsigned int mctrl; /* TIOCM_DTR and TIOCM_RTS as last set */
|
|
+ bool open; /* between startup and shutdown */
|
|
+ bool hung; /* hung up: nothing more until shutdown */
|
|
+ bool brk; /* BREAK asserted */
|
|
+ bool tx_full; /* the host took less than offered: wait for KWS_EV_TX */
|
|
+ bool rx_pending; /* KWS_EV_RX seen, not yet read to the end */
|
|
+ bool rx_stopped; /* stop_rx: the port is on its way down */
|
|
+ bool throttled; /* the line discipline is full */
|
|
+ bool signals_dirty; /* set_mctrl on another CPU, for CPU 0 to send */
|
|
+ int open_err; /* the host's answer to the last open, if it failed */
|
|
+ u8 rxbuf[KWSERIAL_RX_CHUNK];
|
|
+};
|
|
+
|
|
+static struct kwserial_port *kwserial_ports;
|
|
+static unsigned int kwserial_nr;
|
|
+static struct platform_device *kwserial_pdev;
|
|
+/* Reads again after a full tty buffer or a used-up read budget, on CPU 0. */
|
|
+static struct delayed_work kwserial_retry;
|
|
+
|
|
+#ifdef CONFIG_SMP
|
|
+#define KWSERIAL_CPU 0
|
|
+#else
|
|
+#define KWSERIAL_CPU WORK_CPU_UNBOUND
|
|
+#endif
|
|
+
|
|
+/* serial_core's tty operations, with kwserial_tty_open() for open. */
|
|
+static struct tty_operations kwserial_tty_ops;
|
|
+static int (*kwserial_uart_open)(struct tty_struct *tty, struct file *filp);
|
|
+
|
|
+static struct uart_driver kwserial_driver = {
|
|
+ .owner = THIS_MODULE,
|
|
+ .driver_name = "kwserial",
|
|
+ .dev_name = "ttyACM",
|
|
+};
|
|
+
|
|
+#define to_kwserial(p) container_of(p, struct kwserial_port, port)
|
|
+
|
|
+/* May this context call the host's imports itself? */
|
|
+static bool kwserial_on_host_cpu(void)
|
|
+{
|
|
+ return !IS_ENABLED(CONFIG_SMP) || raw_smp_processor_id() == 0;
|
|
+}
|
|
+
|
|
+/* Leave the work to WASM_IRQ_SERIAL's handler on CPU 0 (another CPU, SMP). */
|
|
+static void kwserial_kick(void)
|
|
+{
|
|
+#ifdef CONFIG_SMP
|
|
+ kw_irq_raise(0, BIT(WASM_IRQ_SERIAL));
|
|
+#endif
|
|
+}
|
|
+
|
|
+/* The callers below hold port.lock, on the host's CPU. */
|
|
+
|
|
+static void kwserial_send_signals(struct kwserial_port *sp)
|
|
+{
|
|
+ unsigned int mctrl = sp->mctrl;
|
|
+ u32 sig = 0;
|
|
+
|
|
+ sp->signals_dirty = false;
|
|
+ if (!sp->open || sp->hung)
|
|
+ return;
|
|
+ if (mctrl & TIOCM_DTR)
|
|
+ sig |= KWS_SIG_DTR;
|
|
+ if (mctrl & TIOCM_RTS)
|
|
+ sig |= KWS_SIG_RTS;
|
|
+ if (sp->brk)
|
|
+ sig |= KWS_SIG_BREAK;
|
|
+ wasm_driver_serial_set_signals(sp->port.line, sig);
|
|
+}
|
|
+
|
|
+static void kwserial_tx(struct kwserial_port *sp)
|
|
+{
|
|
+ struct uart_port *port = &sp->port;
|
|
+ struct tty_port *tport = &port->state->port;
|
|
+ bool sent = false;
|
|
+ unsigned int len;
|
|
+ u8 *tail;
|
|
+ int n;
|
|
+
|
|
+ if (!sp->open || sp->hung || sp->tx_full)
|
|
+ return;
|
|
+ if (port->x_char) {
|
|
+ if (wasm_driver_serial_write(port->line, &port->x_char, 1) != 1) {
|
|
+ sp->tx_full = true;
|
|
+ return;
|
|
+ }
|
|
+ port->icount.tx++;
|
|
+ port->x_char = 0;
|
|
+ }
|
|
+ while (!uart_tx_stopped(port)) {
|
|
+ len = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail, UART_XMIT_SIZE);
|
|
+ if (!len)
|
|
+ break;
|
|
+ n = wasm_driver_serial_write(port->line, tail, len);
|
|
+ if (n > 0) {
|
|
+ uart_xmit_advance(port, min_t(unsigned int, n, len));
|
|
+ sent = true;
|
|
+ }
|
|
+ if (n < (int)len) {
|
|
+ sp->tx_full = true;
|
|
+ break;
|
|
+ }
|
|
+ }
|
|
+ if (sent && kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
|
|
+ uart_write_wakeup(port);
|
|
+}
|
|
+
|
|
+/* The receive errors of one status read, before its data; true if any went to the tty. */
|
|
+static bool kwserial_rx_errors(struct kwserial_port *sp, u32 st)
|
|
+{
|
|
+ struct uart_port *port = &sp->port;
|
|
+ struct tty_port *tport = &port->state->port;
|
|
+ u32 report = port->read_status_mask & ~port->ignore_status_mask;
|
|
+ bool queued = false;
|
|
+
|
|
+ /* A break reads as a NUL unless it is flagged (BRKINT, PARMRK) or ignored. */
|
|
+ if (st & KWS_EV_BREAK) {
|
|
+ port->icount.brk++;
|
|
+ if (!uart_handle_break(port) && !(port->ignore_status_mask & KWS_EV_BREAK))
|
|
+ queued |= tty_insert_flip_char(tport, 0, report & KWS_EV_BREAK ?
|
|
+ TTY_BREAK : TTY_NORMAL);
|
|
+ }
|
|
+ /*
|
|
+ * The host does not hand over the bad character; with INPCK a NUL takes
|
|
+ * its place, flagged as the line discipline expects (PARMRK).
|
|
+ */
|
|
+ if (st & KWS_EV_PARITY) {
|
|
+ port->icount.parity++;
|
|
+ if (report & KWS_EV_PARITY)
|
|
+ queued |= tty_insert_flip_char(tport, 0, TTY_PARITY);
|
|
+ }
|
|
+ if (st & KWS_EV_FRAME) {
|
|
+ port->icount.frame++;
|
|
+ if (report & KWS_EV_FRAME)
|
|
+ queued |= tty_insert_flip_char(tport, 0, TTY_FRAME);
|
|
+ }
|
|
+ if (st & KWS_EV_OVERRUN) {
|
|
+ port->icount.overrun++;
|
|
+ if (report & KWS_EV_OVERRUN)
|
|
+ queued |= tty_insert_flip_char(tport, 0, TTY_OVERRUN);
|
|
+ }
|
|
+ return queued;
|
|
+}
|
|
+
|
|
+/* Take the received bytes from the host; true if any went to the tty. */
|
|
+static bool kwserial_rx(struct kwserial_port *sp)
|
|
+{
|
|
+ struct uart_port *port = &sp->port;
|
|
+ struct tty_port *tport = &port->state->port;
|
|
+ bool drop = port->ignore_status_mask & KWS_IGNORE_RX;
|
|
+ bool queued = false;
|
|
+ unsigned int room, copied;
|
|
+ int i, n;
|
|
+
|
|
+ for (i = 0; i < KWSERIAL_RX_BUDGET; i++) {
|
|
+ room = KWSERIAL_RX_CHUNK;
|
|
+ if (!drop) {
|
|
+ room = min(room, tty_buffer_space_avail(tport));
|
|
+ if (!room) {
|
|
+ /* The host keeps the bytes; try again a little later. */
|
|
+ mod_delayed_work_on(KWSERIAL_CPU, system_percpu_wq,
|
|
+ &kwserial_retry, msecs_to_jiffies(10));
|
|
+ return queued;
|
|
+ }
|
|
+ }
|
|
+ n = wasm_driver_serial_read(port->line, sp->rxbuf, room);
|
|
+ if (n <= 0) {
|
|
+ sp->rx_pending = false;
|
|
+ return queued;
|
|
+ }
|
|
+ n = min_t(unsigned int, n, room);
|
|
+ port->icount.rx += n;
|
|
+ if (drop)
|
|
+ continue;
|
|
+ copied = tty_insert_flip_string(tport, sp->rxbuf, n);
|
|
+ if (copied < n)
|
|
+ port->icount.buf_overrun += n - copied;
|
|
+ queued = true;
|
|
+ }
|
|
+ /* More may be waiting: let others run first. */
|
|
+ mod_delayed_work_on(KWSERIAL_CPU, system_percpu_wq, &kwserial_retry, 0);
|
|
+ return queued;
|
|
+}
|
|
+
|
|
+/* Read the status and act on it: signals, hangup, errors, data in and out. */
|
|
+static void kwserial_service(struct kwserial_port *sp, bool first)
|
|
+{
|
|
+ struct uart_port *port = &sp->port;
|
|
+ struct tty_port *tport = &port->state->port;
|
|
+ bool queued = false;
|
|
+ u32 st, changed;
|
|
+
|
|
+ if (!sp->open)
|
|
+ return;
|
|
+ if (sp->signals_dirty)
|
|
+ kwserial_send_signals(sp);
|
|
+ st = wasm_driver_serial_status(port->line);
|
|
+ changed = first ? 0 : (st ^ sp->levels) & KWS_INPUTS;
|
|
+ sp->levels = st & KWS_LEVELS;
|
|
+ if (sp->hung)
|
|
+ return;
|
|
+ if (st & KWS_EV_HANGUP) {
|
|
+ sp->hung = true;
|
|
+ if (tport->tty)
|
|
+ tty_hangup(tport->tty);
|
|
+ return;
|
|
+ }
|
|
+ if (changed) {
|
|
+ /* Every edge counts for TIOCMIWAIT, RI's too. */
|
|
+ if (changed & KWS_RI)
|
|
+ port->icount.rng++;
|
|
+ if (changed & KWS_DSR)
|
|
+ port->icount.dsr++;
|
|
+ if (changed & KWS_DCD)
|
|
+ uart_handle_dcd_change(port, st & KWS_DCD);
|
|
+ if (changed & KWS_CTS)
|
|
+ uart_handle_cts_change(port, st & KWS_CTS);
|
|
+ wake_up_interruptible(&tport->delta_msr_wait);
|
|
+ }
|
|
+ if (st & KWS_EV_ERRORS)
|
|
+ queued |= kwserial_rx_errors(sp, st);
|
|
+ if (st & KWS_EV_RX)
|
|
+ sp->rx_pending = true;
|
|
+ if (sp->rx_pending && !sp->throttled && !sp->rx_stopped)
|
|
+ queued |= kwserial_rx(sp);
|
|
+ if (st & KWS_EV_TX)
|
|
+ sp->tx_full = false;
|
|
+ kwserial_tx(sp);
|
|
+ if (queued)
|
|
+ tty_flip_buffer_push(tport);
|
|
+}
|
|
+
|
|
+static void kwserial_service_all(void)
|
|
+{
|
|
+ unsigned long flags;
|
|
+ unsigned int i;
|
|
+
|
|
+ for (i = 0; i < kwserial_nr; i++) {
|
|
+ struct kwserial_port *sp = &kwserial_ports[i];
|
|
+
|
|
+ uart_port_lock_irqsave(&sp->port, &flags);
|
|
+ kwserial_service(sp, false);
|
|
+ uart_port_unlock_irqrestore(&sp->port, flags);
|
|
+ }
|
|
+}
|
|
+
|
|
+static irqreturn_t kwserial_irq(int irq, void *dev_id)
|
|
+{
|
|
+ kwserial_service_all();
|
|
+ return IRQ_HANDLED;
|
|
+}
|
|
+
|
|
+static void kwserial_retry_work(struct work_struct *work)
|
|
+{
|
|
+ kwserial_service_all();
|
|
+}
|
|
+
|
|
+/*
|
|
+ * The process-context operations go through one dispatcher, so that on SMP
|
|
+ * they can run on CPU 0 as a whole (kw_host_call()).
|
|
+ */
|
|
+enum kwserial_op {
|
|
+ KWS_OP_STARTUP,
|
|
+ KWS_OP_SHUTDOWN,
|
|
+ KWS_OP_CONFIG,
|
|
+ KWS_OP_BREAK,
|
|
+ KWS_OP_SERVICE,
|
|
+};
|
|
+
|
|
+struct kwserial_call {
|
|
+ struct kwserial_port *sp;
|
|
+ enum kwserial_op op;
|
|
+ u32 baud, cfg;
|
|
+ bool brk;
|
|
+};
|
|
+
|
|
+static long kwserial_host(void *arg)
|
|
+{
|
|
+ struct kwserial_call *c = arg;
|
|
+ struct kwserial_port *sp = c->sp;
|
|
+ struct uart_port *port = &sp->port;
|
|
+ unsigned long flags;
|
|
+ long ret = 0;
|
|
+
|
|
+ uart_port_lock_irqsave(port, &flags);
|
|
+ switch (c->op) {
|
|
+ case KWS_OP_STARTUP:
|
|
+ ret = wasm_driver_serial_open(port->line);
|
|
+ sp->open_err = ret;
|
|
+ if (ret)
|
|
+ break;
|
|
+ sp->open = true;
|
|
+ sp->hung = false;
|
|
+ sp->brk = false;
|
|
+ sp->tx_full = false;
|
|
+ sp->rx_pending = false;
|
|
+ sp->rx_stopped = false;
|
|
+ sp->throttled = false;
|
|
+ sp->signals_dirty = false;
|
|
+ kwserial_service(sp, true);
|
|
+ break;
|
|
+ case KWS_OP_SHUTDOWN:
|
|
+ sp->open = false;
|
|
+ wasm_driver_serial_close(port->line);
|
|
+ break;
|
|
+ case KWS_OP_CONFIG:
|
|
+ if (sp->open)
|
|
+ wasm_driver_serial_config(port->line, c->baud, c->cfg);
|
|
+ break;
|
|
+ case KWS_OP_BREAK:
|
|
+ sp->brk = c->brk;
|
|
+ kwserial_send_signals(sp);
|
|
+ break;
|
|
+ case KWS_OP_SERVICE:
|
|
+ kwserial_service(sp, false);
|
|
+ break;
|
|
+ }
|
|
+ uart_port_unlock_irqrestore(port, flags);
|
|
+ return ret;
|
|
+}
|
|
+
|
|
+static long kwserial_call(struct kwserial_call *c)
|
|
+{
|
|
+#ifdef CONFIG_SMP
|
|
+ return kw_host_call(kwserial_host, c);
|
|
+#else
|
|
+ return kwserial_host(c);
|
|
+#endif
|
|
+}
|
|
+
|
|
+/* uart_ops */
|
|
+
|
|
+static unsigned int kwserial_tx_empty(struct uart_port *port)
|
|
+{
|
|
+ struct kwserial_port *sp = to_kwserial(port);
|
|
+ struct kwserial_call c = { .sp = sp, .op = KWS_OP_SERVICE };
|
|
+ unsigned int empty;
|
|
+ unsigned long flags;
|
|
+
|
|
+ kwserial_call(&c);
|
|
+ uart_port_lock_irqsave(port, &flags);
|
|
+ empty = !sp->open || sp->hung ||
|
|
+ (!port->x_char && kfifo_is_empty(&port->state->port.xmit_fifo) &&
|
|
+ (sp->levels & KWS_TX_EMPTY));
|
|
+ uart_port_unlock_irqrestore(port, flags);
|
|
+ return empty ? TIOCSER_TEMT : 0;
|
|
+}
|
|
+
|
|
+/* Under port.lock, maybe in atomic context on any CPU. */
|
|
+static void kwserial_set_mctrl(struct uart_port *port, unsigned int mctrl)
|
|
+{
|
|
+ struct kwserial_port *sp = to_kwserial(port);
|
|
+
|
|
+ sp->mctrl = mctrl;
|
|
+ if (!sp->open)
|
|
+ return;
|
|
+ if (kwserial_on_host_cpu()) {
|
|
+ kwserial_send_signals(sp);
|
|
+ } else {
|
|
+ sp->signals_dirty = true;
|
|
+ kwserial_kick();
|
|
+ }
|
|
+}
|
|
+
|
|
+/* Under port.lock: the input signals as of the last status read. */
|
|
+static unsigned int kwserial_get_mctrl(struct uart_port *port)
|
|
+{
|
|
+ u32 levels = to_kwserial(port)->levels;
|
|
+ unsigned int mctrl = 0;
|
|
+
|
|
+ if (levels & KWS_CTS)
|
|
+ mctrl |= TIOCM_CTS;
|
|
+ if (levels & KWS_DSR)
|
|
+ mctrl |= TIOCM_DSR;
|
|
+ if (levels & KWS_DCD)
|
|
+ mctrl |= TIOCM_CAR;
|
|
+ if (levels & KWS_RI)
|
|
+ mctrl |= TIOCM_RNG;
|
|
+ return mctrl;
|
|
+}
|
|
+
|
|
+/* Under port.lock: kwserial_tx() checks uart_tx_stopped() itself. */
|
|
+static void kwserial_stop_tx(struct uart_port *port)
|
|
+{
|
|
+}
|
|
+
|
|
+/* Under port.lock, maybe in atomic context on any CPU. */
|
|
+static void kwserial_start_tx(struct uart_port *port)
|
|
+{
|
|
+ if (kwserial_on_host_cpu())
|
|
+ kwserial_tx(to_kwserial(port));
|
|
+ else
|
|
+ kwserial_kick();
|
|
+}
|
|
+
|
|
+static void kwserial_throttle(struct uart_port *port)
|
|
+{
|
|
+ unsigned long flags;
|
|
+
|
|
+ uart_port_lock_irqsave(port, &flags);
|
|
+ to_kwserial(port)->throttled = true;
|
|
+ uart_port_unlock_irqrestore(port, flags);
|
|
+}
|
|
+
|
|
+static void kwserial_unthrottle(struct uart_port *port)
|
|
+{
|
|
+ struct kwserial_port *sp = to_kwserial(port);
|
|
+ unsigned long flags;
|
|
+
|
|
+ uart_port_lock_irqsave(port, &flags);
|
|
+ sp->throttled = false;
|
|
+ if (kwserial_on_host_cpu())
|
|
+ kwserial_service(sp, false);
|
|
+ else
|
|
+ kwserial_kick();
|
|
+ uart_port_unlock_irqrestore(port, flags);
|
|
+}
|
|
+
|
|
+/* Under port.lock. */
|
|
+static void kwserial_stop_rx(struct uart_port *port)
|
|
+{
|
|
+ to_kwserial(port)->rx_stopped = true;
|
|
+}
|
|
+
|
|
+static void kwserial_break_ctl(struct uart_port *port, int ctl)
|
|
+{
|
|
+ struct kwserial_call c = { .sp = to_kwserial(port), .op = KWS_OP_BREAK, .brk = ctl != 0 };
|
|
+
|
|
+ kwserial_call(&c);
|
|
+}
|
|
+
|
|
+static int kwserial_startup(struct uart_port *port)
|
|
+{
|
|
+ struct kwserial_call c = { .sp = to_kwserial(port), .op = KWS_OP_STARTUP };
|
|
+
|
|
+ return kwserial_call(&c);
|
|
+}
|
|
+
|
|
+static void kwserial_shutdown(struct uart_port *port)
|
|
+{
|
|
+ struct kwserial_call c = { .sp = to_kwserial(port), .op = KWS_OP_SHUTDOWN };
|
|
+
|
|
+ kwserial_call(&c);
|
|
+}
|
|
+
|
|
+static void kwserial_set_termios(struct uart_port *port, struct ktermios *termios,
|
|
+ const struct ktermios *old)
|
|
+{
|
|
+ struct kwserial_call c = { .sp = to_kwserial(port), .op = KWS_OP_CONFIG };
|
|
+ tcflag_t cflag, iflag;
|
|
+ unsigned int baud;
|
|
+ unsigned long flags;
|
|
+
|
|
+ /* No mark or space parity (Web Serial has none). */
|
|
+ termios->c_cflag &= ~CMSPAR;
|
|
+ cflag = termios->c_cflag;
|
|
+ iflag = termios->c_iflag;
|
|
+ baud = uart_get_baud_rate(port, termios, old, KWSERIAL_BAUD_MIN, KWSERIAL_BAUD_MAX);
|
|
+
|
|
+ c.baud = baud;
|
|
+ c.cfg = tty_get_char_size(cflag) & KWS_CFG_BITS_MASK;
|
|
+ if (cflag & CSTOPB)
|
|
+ c.cfg |= KWS_CFG_STOP2;
|
|
+ if (cflag & PARENB)
|
|
+ c.cfg |= (cflag & PARODD ? KWS_CFG_PARITY_ODD : KWS_CFG_PARITY_EVEN) <<
|
|
+ KWS_CFG_PARITY_SHIFT;
|
|
+ if (cflag & CRTSCTS)
|
|
+ c.cfg |= KWS_CFG_CRTSCTS;
|
|
+
|
|
+ uart_port_lock_irqsave(port, &flags);
|
|
+ uart_update_timeout(port, cflag, baud);
|
|
+ /* Which receive events reach the tty, as 8250 does it. */
|
|
+ port->read_status_mask = KWS_EV_OVERRUN;
|
|
+ if (iflag & INPCK)
|
|
+ port->read_status_mask |= KWS_EV_FRAME | KWS_EV_PARITY;
|
|
+ if (iflag & (IGNBRK | BRKINT | PARMRK))
|
|
+ port->read_status_mask |= KWS_EV_BREAK;
|
|
+ port->ignore_status_mask = 0;
|
|
+ if (iflag & IGNPAR)
|
|
+ port->ignore_status_mask |= KWS_EV_FRAME | KWS_EV_PARITY;
|
|
+ if (iflag & IGNBRK) {
|
|
+ port->ignore_status_mask |= KWS_EV_BREAK;
|
|
+ if (iflag & IGNPAR)
|
|
+ port->ignore_status_mask |= KWS_EV_OVERRUN;
|
|
+ }
|
|
+ if (!(cflag & CREAD))
|
|
+ port->ignore_status_mask |= KWS_IGNORE_RX;
|
|
+ /* The host does RTS/CTS itself, so serial_core leaves both alone. */
|
|
+ if (cflag & CRTSCTS)
|
|
+ port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
|
|
+ else
|
|
+ port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
|
|
+ uart_port_unlock_irqrestore(port, flags);
|
|
+
|
|
+ if (tty_termios_baud_rate(termios))
|
|
+ tty_termios_encode_baud_rate(termios, baud, baud);
|
|
+ kwserial_call(&c);
|
|
+}
|
|
+
|
|
+static const char *kwserial_type(struct uart_port *port)
|
|
+{
|
|
+ return port->type == PORT_GENERIC ? "kwserial" : NULL;
|
|
+}
|
|
+
|
|
+static void kwserial_release_port(struct uart_port *port)
|
|
+{
|
|
+}
|
|
+
|
|
+static int kwserial_request_port(struct uart_port *port)
|
|
+{
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static void kwserial_config_port(struct uart_port *port, int flags)
|
|
+{
|
|
+ if (flags & UART_CONFIG_TYPE)
|
|
+ port->type = PORT_GENERIC;
|
|
+}
|
|
+
|
|
+static int kwserial_verify_port(struct uart_port *port, struct serial_struct *ser)
|
|
+{
|
|
+ if (ser->type != PORT_UNKNOWN && ser->type != PORT_GENERIC)
|
|
+ return -EINVAL;
|
|
+ if (ser->irq != port->irq)
|
|
+ return -EINVAL;
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static const struct uart_ops kwserial_ops = {
|
|
+ .tx_empty = kwserial_tx_empty,
|
|
+ .set_mctrl = kwserial_set_mctrl,
|
|
+ .get_mctrl = kwserial_get_mctrl,
|
|
+ .stop_tx = kwserial_stop_tx,
|
|
+ .start_tx = kwserial_start_tx,
|
|
+ .throttle = kwserial_throttle,
|
|
+ .unthrottle = kwserial_unthrottle,
|
|
+ .stop_rx = kwserial_stop_rx,
|
|
+ .break_ctl = kwserial_break_ctl,
|
|
+ .startup = kwserial_startup,
|
|
+ .shutdown = kwserial_shutdown,
|
|
+ .set_termios = kwserial_set_termios,
|
|
+ .type = kwserial_type,
|
|
+ .release_port = kwserial_release_port,
|
|
+ .request_port = kwserial_request_port,
|
|
+ .config_port = kwserial_config_port,
|
|
+ .verify_port = kwserial_verify_port,
|
|
+};
|
|
+
|
|
+/*
|
|
+ * A snapshot was restored: the new host has none of the ports open. Hang
|
|
+ * up every port that was, so its users see EOF and EIO and open it again.
|
|
+ * Runs while every other task is parked; tty_hangup() only queues work.
|
|
+ */
|
|
+static int kwserial_restored(struct notifier_block *nb, unsigned long action, void *data)
|
|
+{
|
|
+ unsigned long flags;
|
|
+ unsigned int i;
|
|
+
|
|
+ for (i = 0; i < kwserial_nr; i++) {
|
|
+ struct kwserial_port *sp = &kwserial_ports[i];
|
|
+ struct tty_port *tport = &sp->port.state->port;
|
|
+
|
|
+ uart_port_lock_irqsave(&sp->port, &flags);
|
|
+ if (sp->open && !sp->hung) {
|
|
+ sp->hung = true;
|
|
+ if (tport->tty)
|
|
+ tty_hangup(tport->tty);
|
|
+ }
|
|
+ uart_port_unlock_irqrestore(&sp->port, flags);
|
|
+ }
|
|
+ return NOTIFY_OK;
|
|
+}
|
|
+
|
|
+static struct notifier_block kwserial_restore_nb = {
|
|
+ .notifier_call = kwserial_restored,
|
|
+};
|
|
+
|
|
+/*
|
|
+ * serial_core lets CAP_SYS_ADMIN open a port whose startup failed, with
|
|
+ * every read and write failing (EIO), so that setserial can still
|
|
+ * reconfigure it. There is nothing to reconfigure here: the open fails for
|
|
+ * root too, with the host's error (ENODEV: no port in the slot).
|
|
+ */
|
|
+static int kwserial_tty_open(struct tty_struct *tty, struct file *filp)
|
|
+{
|
|
+ struct kwserial_port *sp = &kwserial_ports[tty->index];
|
|
+ int ret = kwserial_uart_open(tty, filp);
|
|
+
|
|
+ if (!ret && test_bit(TTY_IO_ERROR, &tty->flags) && READ_ONCE(sp->open_err))
|
|
+ ret = READ_ONCE(sp->open_err);
|
|
+ return ret;
|
|
+}
|
|
+
|
|
+static int __init kwserial_init(void)
|
|
+{
|
|
+ unsigned int i;
|
|
+ int n, err;
|
|
+
|
|
+ n = wasm_driver_serial_init();
|
|
+ if (n <= 0)
|
|
+ return 0;
|
|
+ n = min(n, KWSERIAL_MAX_PORTS);
|
|
+
|
|
+ kwserial_ports = kzalloc_objs(*kwserial_ports, n);
|
|
+ if (!kwserial_ports)
|
|
+ return -ENOMEM;
|
|
+ kwserial_pdev = platform_device_register_simple("kwserial", -1, NULL, 0);
|
|
+ if (IS_ERR(kwserial_pdev)) {
|
|
+ err = PTR_ERR(kwserial_pdev);
|
|
+ goto err_free;
|
|
+ }
|
|
+ kwserial_driver.nr = n;
|
|
+ err = uart_register_driver(&kwserial_driver);
|
|
+ if (err)
|
|
+ goto err_pdev;
|
|
+ kwserial_tty_ops = *kwserial_driver.tty_driver->ops;
|
|
+ kwserial_uart_open = kwserial_tty_ops.open;
|
|
+ kwserial_tty_ops.open = kwserial_tty_open;
|
|
+ tty_set_operations(kwserial_driver.tty_driver, &kwserial_tty_ops);
|
|
+ INIT_DELAYED_WORK(&kwserial_retry, kwserial_retry_work);
|
|
+ err = request_irq(WASM_IRQ_SERIAL, kwserial_irq, 0, "kwserial", &kwserial_driver);
|
|
+ if (err)
|
|
+ goto err_driver;
|
|
+
|
|
+ for (i = 0; i < n; i++) {
|
|
+ struct uart_port *port = &kwserial_ports[i].port;
|
|
+
|
|
+ port->dev = &kwserial_pdev->dev;
|
|
+ port->line = i;
|
|
+ port->type = PORT_GENERIC;
|
|
+ port->iotype = UPIO_MEM;
|
|
+ port->irq = WASM_IRQ_SERIAL;
|
|
+ port->uartclk = KWSERIAL_BAUD_MAX * 16;
|
|
+ port->fifosize = KWSERIAL_FIFO_SIZE;
|
|
+ port->flags = UPF_FIXED_TYPE;
|
|
+ /* Throttling reaches the driver in every mode: stop reading the host. */
|
|
+ port->status = UPSTAT_SYNC_FIFO;
|
|
+ port->ops = &kwserial_ops;
|
|
+ err = uart_add_one_port(&kwserial_driver, port);
|
|
+ if (err) {
|
|
+ pr_warn("kwserial: ttyACM%u not added (%d)\n", i, err);
|
|
+ break;
|
|
+ }
|
|
+ kwserial_nr = i + 1;
|
|
+ }
|
|
+ if (!kwserial_nr)
|
|
+ goto err_irq;
|
|
+ kw_park_register_restore_notifier(&kwserial_restore_nb);
|
|
+ pr_info("kwserial: %u host serial port(s)\n", kwserial_nr);
|
|
+ return 0;
|
|
+
|
|
+err_irq:
|
|
+ free_irq(WASM_IRQ_SERIAL, &kwserial_driver);
|
|
+err_driver:
|
|
+ uart_unregister_driver(&kwserial_driver);
|
|
+err_pdev:
|
|
+ platform_device_unregister(kwserial_pdev);
|
|
+err_free:
|
|
+ kfree(kwserial_ports);
|
|
+ kwserial_ports = NULL;
|
|
+ return err;
|
|
+}
|
|
+device_initcall(kwserial_init);
|