From patchwork Sun Nov 19 11:28:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 745601 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4B3A6C5AD4C for ; Sun, 19 Nov 2023 11:32:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232005AbjKSLcX (ORCPT ); Sun, 19 Nov 2023 06:32:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231455AbjKSLcG (ORCPT ); Sun, 19 Nov 2023 06:32:06 -0500 Received: from EUR03-DBA-obe.outbound.protection.outlook.com (mail-dbaeur03on2064.outbound.protection.outlook.com [40.107.104.64]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4784A3AA2; Sun, 19 Nov 2023 03:30:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gqMnpe1A7TTuPXB8XlXjjpGFNvwYV2UDVDr54x5Nau8izBIuXhzwelALKBkyJVnx8teBM+OEIU5OIMrcVNml8jweKTmaPN6egSFHSBe6eksIQ/RFkiUd3FskI0JVzqcBWEdk+yIjl7b3AgXnSH2Dts+8w6V/vvbt8yup0fKeucr5SkKVWqEHE2WCy3eVc+2zyxXxxIqOg4Wl+eBxoS6X7uyhz4AeTzZ/KWmK8V1YqIQGKe6UlacjrzLOQ3nWwK8B5vhFUPu3CcRBjA6xYuoFCUWhnxbgvxZsiH0d0e08/tdsmovKKwgbYcBLBMuqEqcDqJVMNem09ES8VtBD5PXASw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Y2pTezH0DgO8+z7w1y5ywOUCZlRR/vO2d+VhJfncoRA=; b=Qb0QQbA2gQU9MoUOZxSeqFTsWKJXbVP/AxPACyDocAiYIye8QktYLOL8XJ6Q/oh2HhACq2Xrlv9u6y/h1VBjSA1icN7FGvRseDVz+lQuEhKaEBEeNbZ3YGFncUzBOnSF0stjlHhwKnA9UYuhiWwU2eVF5W0N3P7dKCt3cYvggMapQD22sMAP2MjelSYB5WgS3BwsDdTxzphr+El3MMZnAGV2Yu4t//d3/+KekEb1oaK7OWI7W/WUrnv8LXci7f5XqO8Q3N468AAILAwVLWw5k9abbYu+JLuAnTMd82PC5smaHgLENqCMa7lTJxsvuZUAtt3qtdMocUBwHTgaSQxM+A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=kunbus.com; dmarc=pass action=none header.from=kunbus.com; dkim=pass header.d=kunbus.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kunbus.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Y2pTezH0DgO8+z7w1y5ywOUCZlRR/vO2d+VhJfncoRA=; b=kVyC4JL3qta0Rq0u5WhXjLzyr3rywyZY73jY73LdvUJcXE8ivyDERZ0+OFAlTrQ4bpsPdCeODtcnLSbQOAiyi32kQWio3i7U0ege/iVDKvERXutNc57ppVRilbmufmDJz/u0APaK/+PjG52AWdmx9FWyYOtpJfTounVEx6Woelo= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=kunbus.com; Received: from VI1P193MB0413.EURP193.PROD.OUTLOOK.COM (2603:10a6:803:4e::14) by AM8P193MB1060.EURP193.PROD.OUTLOOK.COM (2603:10a6:20b:1e5::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7002.26; Sun, 19 Nov 2023 11:29:59 +0000 Received: from VI1P193MB0413.EURP193.PROD.OUTLOOK.COM ([fe80::653f:d0f3:e7f6:8c06]) by VI1P193MB0413.EURP193.PROD.OUTLOOK.COM ([fe80::653f:d0f3:e7f6:8c06%5]) with mapi id 15.20.7002.025; Sun, 19 Nov 2023 11:29:59 +0000 From: Lino Sanfilippo To: gregkh@linuxfoundation.org, jirislaby@kernel.org, ilpo.jarvinen@linux.intel.com Cc: u.kleine-koenig@pengutronix.de, shawnguo@kernel.org, s.hauer@pengutronix.de, mcoquelin.stm32@gmail.com, alexandre.torgue@foss.st.com, cniedermaier@dh-electronics.com, linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, LinoSanfilippo@gmx.de, lukas@wunner.de, p.rosenberger@kunbus.com, Lino Sanfilippo , stable@vger.kernel.org Subject: [RESEND PATCH v4 1/7] serial: Do not hold the port lock when setting rx-during-tx GPIO Date: Sun, 19 Nov 2023 12:28:49 +0100 Message-ID: <20231119112856.11587-2-l.sanfilippo@kunbus.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231119112856.11587-1-l.sanfilippo@kunbus.com> References: <20231119112856.11587-1-l.sanfilippo@kunbus.com> X-ClientProxiedBy: FR5P281CA0007.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:f2::17) To VI1P193MB0413.EURP193.PROD.OUTLOOK.COM (2603:10a6:803:4e::14) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VI1P193MB0413:EE_|AM8P193MB1060:EE_ X-MS-Office365-Filtering-Correlation-Id: ef6725b6-4b36-4df9-26f8-08dbe8f2dd1b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GUTUgW8ALe0G2xq5rLSflLGhGbHz3foUod+q/Bp98SkOWkZwLAbsskDqkpHr9rDpdSFv4LJaPkAmb5cRFluE5IL1YdFZf3HqZtXHNb3PjxmV1imKZY69wCrnncJBFNlhqSMGlwDut2EAscpTzlUV5xjH6yhixcMMP3sKv6/+T413/BM3PuPohcifnEdd8zCimLDr072z31F2/3fZ2ao5a8ovIi4pCyof3SkPjcxuOj0r7wc/vMvuj88MvaPXk9H6aqMDPmYdauclQ9/DRjrrNJs67FabQyJTP0/YduobSwEUAg+shzoJU27Txxq6I7Gog4KRrZTPNCsuKVJHhUWp+cU6kT1KRlzT7cluw9bVmakw9TtRpXLgIO6OCIMCFPidoVRUotFJCM2coNB2/ErELsbk7zXr5dHE/8YT/033tvL9KduE/1fLtneMHcIRHFMIjI4CBxCPGvW8ULVCIkht7mLI2yLCnuB/L2G0sYNdBaWAt4P3w4N0dk+tw2bem37L6jf8p0D8hoKOVZ1W/Fcn1ipxvgo1EWj/6K7sokpc5nQ= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:VI1P193MB0413.EURP193.PROD.OUTLOOK.COM; PTR:; CAT:NONE; SFS:(13230031)(346002)(136003)(396003)(366004)(39830400003)(376002)(230922051799003)(1800799012)(64100799003)(186009)(451199024)(6506007)(6512007)(52116002)(66946007)(66556008)(316002)(4326008)(8676002)(8936002)(66476007)(6486002)(478600001)(83380400001)(1076003)(2616005)(38100700002)(6666004)(5660300002)(7416002)(2906002)(86362001)(41300700001)(36756003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ycBRkyfAQ76L/h7O0AVb8etclca5oTgbxv1WjzBeBCu3iMySu9gu93whUXT/DdV1OzEC2aF7fACCAsXhILk5v6qPb+NgtAh2mykA1P3arnV3wZGSTw1at0TWd30b0vmtsTiuYB95FKk7yEE6CGG87lKs+OTCds2LPBPj/77Yw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vysZhiQ9ZOk5bCEj3lpBmDqeuqHdiFK0vziBMzN5MuUNLYgkRkehiGHHoZzUQe2ZgInXCfyiB5XyDaEE1iYgFZpNJhRsBewCYIwX6baqTJykK93BiBxszgdMSc8SDfr3EsqI/Wgaw1L6h3oHmwgWFNep9PwXlM4tJsfua5KUPaBqOdtjILZOutQM7l2hJWjLQH0UOplzfkk9wHUjGZLaQpo4lNY4FsfsBdz8DWOsgadgDN17LxoWO6EnO4qDhvIj5tmkoNcKcWuShfHpPnaEeJkk0HkdjYzeHakS3JzoyT/SBzikxnN38y7xEqZjmRuJbdZPmESxViImKrJYcLwwONcoh8v7EWr+gZemf0Ut5jdLlH+fOZwag5aL6aTPiBO6GOWkR+Gy4StS0/t9y6IuO8IMgmVBe80EGeRb/ X-OriginatorOrg: kunbus.com X-MS-Exchange-CrossTenant-Network-Message-Id: ef6725b6-4b36-4df9-26f8-08dbe8f2dd1b X-MS-Exchange-CrossTenant-AuthSource: VI1P193MB0413.EURP193.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Nov 2023 11:29:59.4907 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: aaa4d814-e659-4b0a-9698-1c671f11520b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: IM981oZrSd5OGu9aotJLllfE8gp1pXovJCBdVLsFPmQfz/yw71jDsJtx6/lujfBQ4im35LMfVGIN2F4W1CTtpQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8P193MB1060 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org Both the imx and stm32 driver set the rx-during-tx GPIO in rs485_config(). Since this function is called with the port lock held, this can be an problem in case that setting the GPIO line can sleep (e.g. if a GPIO expander is used which is connected via SPI or I2C). Avoid this issue by moving the GPIO setting outside of the port lock into the serial core and thus making it a generic feature. Since both setting the term and the rx-during-tx GPIO is done at the same code place, move it into a common function. Fixes: c54d48543689 ("serial: stm32: Add support for rs485 RX_DURING_TX output GPIO") Fixes: ca530cfa968c ("serial: imx: Add support for RS485 RX_DURING_TX output GPIO") Cc: Shawn Guo Cc: Sascha Hauer Cc: stable@vger.kernel.org Signed-off-by: Lino Sanfilippo --- drivers/tty/serial/imx.c | 4 ---- drivers/tty/serial/serial_core.c | 15 +++++++++++---- drivers/tty/serial/stm32-usart.c | 5 +---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c index 708b9852a575..9cffeb23112b 100644 --- a/drivers/tty/serial/imx.c +++ b/drivers/tty/serial/imx.c @@ -1943,10 +1943,6 @@ static int imx_uart_rs485_config(struct uart_port *port, struct ktermios *termio rs485conf->flags & SER_RS485_RX_DURING_TX) imx_uart_start_rx(port); - if (port->rs485_rx_during_tx_gpio) - gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, - !!(rs485conf->flags & SER_RS485_RX_DURING_TX)); - return 0; } diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c index f1348a509552..ab766f48c172 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -1392,14 +1392,21 @@ static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs4 memset(rs485->padding1, 0, sizeof(rs485->padding1)); } -static void uart_set_rs485_termination(struct uart_port *port, - const struct serial_rs485 *rs485) +/* + * Set optional RS485 GPIOs for bus termination and data reception during + * transmission. These GPIOs are controlled by the serial core independently + * from the UART driver. + */ +static void uart_set_rs485_gpios(struct uart_port *port, + const struct serial_rs485 *rs485) { if (!(rs485->flags & SER_RS485_ENABLED)) return; gpiod_set_value_cansleep(port->rs485_term_gpio, !!(rs485->flags & SER_RS485_TERMINATE_BUS)); + gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, + !!(rs485->flags & SER_RS485_RX_DURING_TX)); } static int uart_rs485_config(struct uart_port *port) @@ -1412,7 +1419,7 @@ static int uart_rs485_config(struct uart_port *port) return 0; uart_sanitize_serial_rs485(port, rs485); - uart_set_rs485_termination(port, rs485); + uart_set_rs485_gpios(port, rs485); uart_port_lock_irqsave(port, &flags); ret = port->rs485_config(port, NULL, rs485); @@ -1456,7 +1463,7 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port, if (ret) return ret; uart_sanitize_serial_rs485(port, &rs485); - uart_set_rs485_termination(port, &rs485); + uart_set_rs485_gpios(port, &rs485); uart_port_lock_irqsave(port, &flags); ret = port->rs485_config(port, &tty->termios, &rs485); diff --git a/drivers/tty/serial/stm32-usart.c b/drivers/tty/serial/stm32-usart.c index 3048620315d6..ec9a72a5bea9 100644 --- a/drivers/tty/serial/stm32-usart.c +++ b/drivers/tty/serial/stm32-usart.c @@ -226,10 +226,7 @@ static int stm32_usart_config_rs485(struct uart_port *port, struct ktermios *ter stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); - if (port->rs485_rx_during_tx_gpio) - gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, - !!(rs485conf->flags & SER_RS485_RX_DURING_TX)); - else + if (!port->rs485_rx_during_tx_gpio) rs485conf->flags |= SER_RS485_RX_DURING_TX; if (rs485conf->flags & SER_RS485_ENABLED) {