From patchwork Wed Oct 18 17:48:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 736150 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 18FEDCDB484 for ; Wed, 18 Oct 2023 17:49:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230426AbjJRRtp (ORCPT ); Wed, 18 Oct 2023 13:49:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230132AbjJRRtn (ORCPT ); Wed, 18 Oct 2023 13:49:43 -0400 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2047.outbound.protection.outlook.com [40.107.22.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CF6A112; Wed, 18 Oct 2023 10:49:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZJMFDHnUNSKyDjDufaULtC/PvwznpBvduHHHhBlZqGKkMRPIT3YpJY+hp+vEOnxMKkO8YUJdIUhLDtPAD6sESs7TkenWZmeYahhqNkpMvtMKrvJQqYqffSmyASxVwGtxCQQH5doYP6VBx4KwZpJ2Lo/jrDG0qGm7FQdNdBz+y3DdzrbHPWmRRfr2aZKt/kge9sNh/emzDEX8CgkWTglMHVGRHTZ9QAm38fwqQ2ZyxXdDe9ENJpy5mDQqFqjl32qA10k/IY9XtHlOC5Qhn8o05BR0kClYzU70MqgHUD5uP2GFCrN96pkm/ox/cYrM3Cn7/fxMdnw9XNY9r7lqONwk7w== 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=kig7DXAIM3jbV8RnY2R78WbZepJvDlAZxse7gk0wTaQ=; b=PTECnKq3pP1/XA5RRiKHUSFx2lMLOkm5T7kYxEXFmPuG3IUOzxurRWLQXMVP1a65WhbUX+gg9VtANJXSTMoNMbIhm62BACUEXpq34YDKw9iWGKc0GoKv4yHq5Kb/p0dBeX8pgC7YnO6DNl5KEev028J3AjD0LdsyvpIdzB6DWr18r5yLTQbYHcdwzFtLhBySQ+7TjemyGlp7fUo5InBPgAcND+4FGFrHzbIFOn1EjR/Rkskkx/fOaxvytErOL4PWWEhK/16oS+b7HqOHLoo+9O73CpcpaFkzOGyqSrQkAN/KwMKA5ZpFWxY7QpuWRXhbjMhl90svB48yfoK3rAJSTQ== 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=kig7DXAIM3jbV8RnY2R78WbZepJvDlAZxse7gk0wTaQ=; b=GUD+rpoE3gNaAKX/aB0nTbYTdQ+IG/cdHb6jNJTtAMZs82d1KVxX+/nn7kwBx2wi9UeoqkWib1T1ODAdeq2aUzaGKyMpla05Ps/xA3Z0F85zX61/tuWe4VcbLHEwuO4dgq2wBwrh8nru2vRCSfciuWyjDl5+Ar61+04Q1+aPgaI= 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 AS8P193MB1478.EURP193.PROD.OUTLOOK.COM (2603:10a6:20b:350::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6907.23; Wed, 18 Oct 2023 17:49:37 +0000 Received: from VI1P193MB0413.EURP193.PROD.OUTLOOK.COM ([fe80::550d:2425:c0ed:3e59]) by VI1P193MB0413.EURP193.PROD.OUTLOOK.COM ([fe80::550d:2425:c0ed:3e59%3]) with mapi id 15.20.6886.034; Wed, 18 Oct 2023 17:49:37 +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: [PATCH v4 1/7] serial: Do not hold the port lock when setting rx-during-tx GPIO Date: Wed, 18 Oct 2023 19:48:34 +0200 Message-Id: <20231018174840.28977-2-l.sanfilippo@kunbus.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231018174840.28977-1-l.sanfilippo@kunbus.com> References: <20231018174840.28977-1-l.sanfilippo@kunbus.com> X-ClientProxiedBy: FR0P281CA0130.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:97::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_|AS8P193MB1478:EE_ X-MS-Office365-Filtering-Correlation-Id: df67c3bb-bcd5-4e5d-4540-08dbd0029899 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zVuJrcLoriTOHM1JYrJky0Z5WHGN5rPnXGVQACkTIrIYvRUecpBsHpY2CI3ai5qLSEbDzIj8QWbOLxjnXg2xaCfqBGgsovT3OzhQohraoRFzuOpDg5+VANWny39G0ytw/1nQOaLY7bVPt5+H9Xhj4oQQb+CMbImsW3zsJb6v2b7V/3hB8HrAsc5GNbjQSg62kL6OeOJS8MOilXEtCkS0uEWMZ+IklIpr38HEpS3a18FuxF7s1nOCM2xUYIqGNsnZe/9JewHtZUIKqsgcd1F23ySj0O0Z+sIe0mo4cwdwzkiASBzJbCPRvS/Uk+FQX4b9h7rHimpWQCV2OVsR3xNQez31au9wXi81FdF9pYiosTFpNkMErJFHw1YpZ4ZPLeynNhE/iOZ3KjThyBeeEr3pqd87aSfXcv62GHK3EDGNvKK47D4/IgHmvEGElZa5h1qddUeYS2zqIhG/TgZU9I3YT6oXJ9kpX3V5UGMBNIlm1QWb38rnAXcxmHjd/IJWFs74pwFH8z1K6TzqneoT0+QCzLIpgrmHpZWahqYrqBlHFSQ= 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)(396003)(39840400004)(376002)(346002)(366004)(136003)(230922051799003)(186009)(1800799009)(64100799003)(451199024)(36756003)(66946007)(66556008)(66476007)(86362001)(38100700002)(1076003)(2616005)(83380400001)(6666004)(6512007)(52116002)(6506007)(7416002)(6486002)(316002)(478600001)(5660300002)(4326008)(8676002)(41300700001)(2906002)(8936002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: vBvekx8VX1rcwvvmhxBGQrT4TM8YbhB0Cm6Ka9lfPE5p6ojGn+ZPnO25BldsuKwQwquwLQ728F+L+znhwniiwSG8339uUh3fK5+/cWcAE2SiXvwSAKPG5+7IdB9DBWSveVmrCbAxzTjxMKWk/G1/xUgojTmn3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qj8aZzC3SBrEJ9RYaHA/bbVkQJNQhzv+Gf+HP/FjBj9EDD9wDG/1tizgqqYFaYd5voviV0bxeVPmpiBu1nSRLXX9ekOhj8SIJKyDe5Q8o/hy6pPSC8yedrEF+3fhMjXXs2txMn4+X5oruCvDJtyieq3ZBikrMW0T7iIZP2zgDMFdHtCEddG/bsY/NekDeDjwfA4gGyMdQYXqKwyhZbJTzqjMIUOOfDH1m/5tYHP+ix9XGDSpxYszVNx/z6LDLT6fhCYIiA57Dy95B+Rcb6Yvw8jN77m+GueRQEsqALCUHPpwNF487o6cCIbn42ZIhn4o3dJhKG5KPkQFKT/8JV9bt3lwBX185OfSmnbQ7eeLRbahffzdvgfP/0gGYnj0OsPUbS8KNSArxqJ1tDaAmcKDGXlL3uISQoCHG67vLOd X-OriginatorOrg: kunbus.com X-MS-Exchange-CrossTenant-Network-Message-Id: df67c3bb-bcd5-4e5d-4540-08dbd0029899 X-MS-Exchange-CrossTenant-AuthSource: VI1P193MB0413.EURP193.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Oct 2023 17:49:37.3227 (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: dwcD3vU1XL3VB/zRaSK5EE8tX9LeKWKLBmOzFAzMO1IYEcbM3poO9fpJvz5eVVMD0lT173xsiN1B6A53TG/pSw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8P193MB1478 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 13cb78340709..edb2ec6a5567 100644 --- a/drivers/tty/serial/imx.c +++ b/drivers/tty/serial/imx.c @@ -1947,10 +1947,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 d5ba6e90bd95..35e014f83465 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -1391,14 +1391,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) @@ -1411,7 +1418,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); spin_lock_irqsave(&port->lock, flags); ret = port->rs485_config(port, NULL, rs485); @@ -1455,7 +1462,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); spin_lock_irqsave(&port->lock, 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 5e9cf0c48813..8eb13bf055f2 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) {