Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795061551; b=1b7J6/8PJdUjfvA2vFKzfczqZDirp+pXz6FOmgWigdFdbWOe65eUN1G3tSqhfcv/PunT zhzs/iBZb1jpdV9/XYAF9rs9S8V12nkGFJcrMgxQo1eL/QOGW+DttR1FhMxZiESO1NjLo hUu8IlsviRBm0HD7JstwzasGUiy5wf55ArRrMk+j1hBnBICQJi9IGhQdmAKpjOVqnipyN hKPmOUhnOC0A4Gf1ep9F8KutI5uuW2UWnDLJWeaK/jxhwk7qFxxOKAUVnPVZVp/2KGbQQ TjlellwAY+FSRyHgtc+4FRxA5TOzLTTA/Bnx1Qa40s+/8VkNRp1LGLIongTz741CZKQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795061551; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=jpQZqqa0/3hQVPSCQHPPTB6zRD192wMqvsZKFdh77ys=; b=vfU3Ebc1rPEYb7PobrAoGNh3OrDfXUzWjWEoNPfq6guk9IyzFCEbUHNsj2sHD6xWqYYb vwE8NntcGpNKqLI/UICVTWzFkblGXw7GPhB6f/o07LbDQI9JamaH3XlJvdHY+kG1BC2vJ 3RQR05Gp6+F3fPS8+qjKZUiatrGsdokkOEDfEWEw/D9ZNdWReAKk/j0LKn++GS8CFLpZO DCxsS+DvOinfEoLXW/NRr7sSzr6E6B9FL/bkO9AnlMpg2AaNdx5ouxJCzXQl6fJzU5OZ3 qGa6ahF2hr93cxxGoAAf0WtYcejh6qT7iFGyADli8KD0P3WOOeRZcKkk3XlYVSDjZmg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b="yEk+e/VI"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B142606A332; Thu, 19 Nov 2026 04:12:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=yEk+e/VI Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9636E1F000C4; Thu, 19 Nov 2026 04:12:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795061551; bh=jpQZqqa0/3hQVPSCQHPPTB6zRD192wMqvsZKFdh77ys=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=yEk+e/VIXcxFPHA69qDlleIx/QH1SSjlJGru8CRrT7mb1AX/vzZv58g1QL2YQqjVW ujGTib0NS7Iqe/FErHTAK5jgOszOmX5DeAFsxD7zE+nLHHT+m6ylbLlr42JTSw+n3l MuM4KXHdV3KGW4PEt5ByE0pxIjrgEIDmElmHTde8= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Tapio Reijonen , Sasha Levin Subject: [PATCH 5.10 1591/1848] serial: max310x: fix RS485 delays being programmed as bit-times Date: Wed, 18 Nov 2026 16:00:29 +0100 Message-ID: <20261118150026.118766211@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118145911.150052362@linuxfoundation.org> References: <20261118145911.150052362@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.10-stable review patch. If anyone has any objections, please let me know. ------------------ From: Tapio Reijonen [ Upstream commit 7784d6aee59a142505072b3fbea97932b2fd1fa0 ] The HDPIXDELAY register counts the RTS setup and hold delays in bit-times, four bits per direction, but the driver has been writing the struct serial_rs485 delay_rts_before_send/delay_rts_after_send values into it unconverted - and the uapi expresses those in milliseconds. A requested 9 ms setup delay is programmed as 9 bit-times, which at 9600 baud is 0.94 ms, roughly a tenth of what userspace asked for; the error grows with the baud rate. Cache the baud rate in set_termios() and convert the delays to bit-times at the current rate, rounding up so the delay on the wire is never shorter than requested, and capping at the 15 bit-times the 4-bit field can hold. The conversion lives in max310x_set_rts_ctl_params(); set_termios() now calls it as well, since the conversion depends on the baud rate. The delays a 4-bit bit-time field can represent still fall well short of the milliseconds the uapi can express; requests beyond 15 bit-times are capped, and the -ERANGE rejection of values above 15 ms remains in place for now. Fixes: 55367c620aed ("serial: max310x: Add support for RS-485 mode") Assisted-by: Claude:claude-fable-5 Signed-off-by: Tapio Reijonen Link: https://patch.msgid.link/20261008-max310x-rs485-sw-delay-v8-4-6ba881f81e1f@vaisala.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/tty/serial/max310x.c | 40 ++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c index 184de3da6846e..f908d544e3c62 100644 --- a/drivers/tty/serial/max310x.c +++ b/drivers/tty/serial/max310x.c @@ -162,6 +162,10 @@ #define MAX310X_IRDA_IRDAEN_BIT (1 << 0) /* IRDA mode enable */ #define MAX310X_IRDA_SIR_BIT (1 << 1) /* SIR mode enable */ +/* HDPIXDELAY accessor macros */ +#define MAX310X_HDPIXDELAY_SETUP(val) (((val) & 0x0f) << 4) +#define MAX310X_HDPIXDELAY_HOLD(val) ((val) & 0x0f) + /* Flow control trigger level register masks */ #define MAX310X_FLOWLVL_HALT_MASK (0x000f) /* Flow control halt level */ #define MAX310X_FLOWLVL_RES_MASK (0x00f0) /* Flow control resume level */ @@ -283,6 +287,7 @@ struct max310x_one { struct work_struct md_work; struct work_struct rs_work; struct regmap *regmap; + unsigned int baud; bool tx_break; /* break_ctl() owns the transceiver */ u8 rx_buf[MAX310X_FIFO_SIZE]; @@ -899,23 +904,33 @@ static void max310x_set_mctrl(struct uart_port *port, unsigned int mctrl) } /* - * Program the chip's RS485 RTS timing (HDPIXDELAY) and the auto - * transceiver control (MODE1) from the current configuration. The - * rs485-config worker, startup() and break_ctl()'s break-off restore - * each carried their own copy of this sequence. + * Program the chip's RS485 RTS timing. The HDPIXDELAY setup and hold fields + * count bit-times, four bits per direction, while the uapi expresses the + * delays in milliseconds: convert at the current baud rate, rounding up, and + * cap at the field maximum. */ static void max310x_set_rts_ctl_params(struct max310x_one *one) { + const unsigned int max_bit_dly = 15; struct uart_port *port = &one->port; - unsigned int delay; + unsigned int setup = 0, hold = 0; u8 mode1 = 0; - delay = (clamp(port->rs485.delay_rts_before_send, 0U, 15U) << 4) | - clamp(port->rs485.delay_rts_after_send, 0U, 15U); - max310x_port_write(port, MAX310X_HDPIXDELAY_REG, delay); + if (port->rs485.flags & SER_RS485_ENABLED) { + /* Convert milliseconds to bit-times, rounding up. */ + setup = DIV_ROUND_UP(one->baud * port->rs485.delay_rts_before_send, + MSEC_PER_SEC); + hold = DIV_ROUND_UP(one->baud * port->rs485.delay_rts_after_send, + MSEC_PER_SEC); + setup = min(setup, max_bit_dly); + hold = min(hold, max_bit_dly); - if (port->rs485.flags & SER_RS485_ENABLED) mode1 = MAX310X_MODE1_TRNSCVCTRL_BIT; + } + + max310x_port_write(port, MAX310X_HDPIXDELAY_REG, + MAX310X_HDPIXDELAY_SETUP(setup) | + MAX310X_HDPIXDELAY_HOLD(hold)); /* * A break owns the transceiver: break_ctl() disabled auto-RTS and @@ -1069,6 +1084,13 @@ static void max310x_set_termios(struct uart_port *port, /* Update timeout according to new baud rate */ uart_update_timeout(port, termios->c_cflag, baud); + + /* + * Cache the new baud rate and reprogram the RS485 RTS delays, whose + * millisecond-to-bit-time conversion depends on it. + */ + to_max310x_port(port)->baud = baud; + max310x_set_rts_ctl_params(to_max310x_port(port)); } static void max310x_rs_proc(struct work_struct *ws) -- 2.53.0