From patchwork Mon Dec 25 11:35:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lino Sanfilippo X-Patchwork-Id: 758577 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2070.outbound.protection.outlook.com [40.107.22.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9AF75102A; Mon, 25 Dec 2023 11:36:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=kunbus.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kunbus.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=kunbus.com header.i=@kunbus.com header.b="QNvQ8c4g" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jpWz0EEmYtlUsz1NWSJoBt8BOBF5lH7rrpIH/2nFCdWLPnXjeW71uBixkaDNCxwCCwaw7vlcdhGeDRIGYhaxpdLTNidYWBkjyQK6PkMMHpFoTTRiQeHH6EHVTRCZVc3tHXBOzc1KH+fMSD9W9MEodBuKERYMwFWpMYlK0G85t745HJmJwlB0+QSbnk4ohve5FLVq5kbsPNd0k6WJaCdCVZT5wh/asaKLnl9JTrnJL3/0O6NhLyOokyROyKFXt9Yr4UQzizuj8xKBMBdSTXzENDjr12r82VbubY29gK4M7/SXr8G7XTJdbwNnRvpvWRdVOjqDn9DXF6LP3MXQ0lm0Bg== 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=1TC+21YnKQMHFgAz2YduVy8ekn/I7Y9I8WDYbieztEk=; b=JItsaISwsdVODEf6M872IlrjPcyFwS0dJUvnG6GgiGJUAVfSbvfelxkFCpinBaHdRAVOYo+p6M80jpqAWbvqF+BDn7n2IsNfW8mvrf95WiZ8nkaIcmr4zpnWS60HNGYxcBhzwpdwQkU98XDBAYfAtTklmbJOw7ODia7+poy7ot1S2AclunN/0q02jQcFkoXOR+BNUHCEToNBQ3M4jdit2/YegmcXexJbrVVQab/bpQlmXfxufTIaSvgTx05jBt62meKwlepQ8oxRPuPoC+BYZCg84eds5uHJO7778PhWA20GXedBifieF5ZOtN1XW1E9oRUH2s68A1gCpbxNSV3F3w== 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=1TC+21YnKQMHFgAz2YduVy8ekn/I7Y9I8WDYbieztEk=; b=QNvQ8c4gl8aCvMgaXIkLggINom/yZ0ohE72J1J0wcuFQqjf33T3rdZy8IAILZOBIAJYJMjyblrBOobEjWHr6T1hqEhRJCU9+G/H7fHlSlDiK8sgU2eHyr2H21KjOqHoAGzQEfpl6FbwSNvqGh7oUNZTQyT6KQxqGSXIDUjjKuwk= 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 GV1P193MB2070.EURP193.PROD.OUTLOOK.COM (2603:10a6:150:21::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7113.26; Mon, 25 Dec 2023 11:35:54 +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.7113.026; Mon, 25 Dec 2023 11:35:54 +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, hugo@hugovil.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, Hugo Villeneuve Subject: [PATCH v6 1/7] serial: Do not hold the port lock when setting rx-during-tx GPIO Date: Mon, 25 Dec 2023 12:35:17 +0100 Message-ID: <20231225113524.8800-2-l.sanfilippo@kunbus.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231225113524.8800-1-l.sanfilippo@kunbus.com> References: <20231225113524.8800-1-l.sanfilippo@kunbus.com> X-ClientProxiedBy: FR5P281CA0055.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:f0::17) To VI1P193MB0413.EURP193.PROD.OUTLOOK.COM (2603:10a6:803:4e::14) Precedence: bulk X-Mailing-List: linux-serial@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VI1P193MB0413:EE_|GV1P193MB2070:EE_ X-MS-Office365-Filtering-Correlation-Id: 44124829-273c-4d47-2c69-08dc053da643 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6jJfUYkdSoDse7HWl5mRa96dswVKDFkIfRdGILJs+YD1orEe5kETVepAbbwEBgwoWZUy4pmPbBy+ijgPWAECHzWeaEmYMP0wB+CdtYoOHereDyBb0QGzV6ZgXrAshtWRKCbAXlmcvsAonKc7AREfQ9mzAYZIZXVpdTYSBQhf0p9zNn9U0kbXmUHD7qVDHe6On77OsWMKAQ4WtVv97vFKxeZJNBA3lnEN0J8MSo0cSPqaFhVfAHN6LPMmV9jtEkUa7SWzQ/MxRPgqoEAhdAcIQoSzPGc3kn2jqjw04LsGlZbR6h7oj1I730NEw1F5GLY9SWacPtPm2ZcfYPxBrN06xIGRluxrl1codMqB3voAYYnEhlGSk6h6Jc5I8ydguwtHtA9GBdLOxAAvp4TuMTSeGFRWisrK3rbGiUx0572VkSL+ZLKPmH7GuuPS4wDC+iqPDRcfq+QkPx+1gsM4VyJtNNnjdSa5mrcLeXHirbvQ7FxfcFlCEI0CyhueEwy8tSy1ncE0KZqMfwaO4SYEz1IS/uZ2JQueEArelpA49QgEJRT3JvScES0XhuuuqtSNQhHm 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)(376002)(136003)(346002)(366004)(39830400003)(396003)(230922051799003)(1800799012)(64100799003)(186009)(451199024)(86362001)(1076003)(2616005)(83380400001)(66574015)(38100700002)(41300700001)(36756003)(7416002)(5660300002)(478600001)(6486002)(2906002)(6666004)(8676002)(8936002)(66946007)(66476007)(66556008)(316002)(54906003)(4326008)(6506007)(6512007)(52116002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?SJ9zAljz42Q5pDFZKi73b74JFr7x?= =?utf-8?q?sCPhOV8t7yq2FoC9orT8BOfwFwi51JKeXNzBYACYGZIElGbE+ejopxmdNcaGut/+8?= =?utf-8?q?F4vXgakpI3Qy+h5ykG4v167IQHi7agZG4uQAaMu94mS9W76TF1/HDsxIVo6PMI37d?= =?utf-8?q?Brnu0dARU/P7QmpXKGqS/DCOuFMaXhxJ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UqCgQyQZhI1DuE/JBEFbEtNQipq?= =?utf-8?q?dlbn3s7D1y6BM61MoDuhRFINpgbSR30x6LlN8KI3iuKMLBLX7efYPA5C9Lug0NVw7?= =?utf-8?q?exhJzetUpKYyuJTH5+p0PJB0jGD/hny4U5I1iuuGjhJ0yZFe898WmIv3RL+k0zBAP?= =?utf-8?q?+3/r5rZb29RaovMIr4hR79bJ8jBav3cbTDZA9+nJf2gyTiOQ4tPD0HHOVax3BEaY9?= =?utf-8?q?mvzLmV5GjEdOUhjS0SEW1f7ZcHK/ShyBme1eCP65KB1oR3JH7YDyFtc8mDvpQQmYW?= =?utf-8?q?YkV3uvuUtQN/isoIPeFBbYVePanQnEpmyHHOdZrieIYnN3SOzJhcZGI2Atz5u60ZI?= =?utf-8?q?qjYZB4gErbXcFBhW9/CaVJcV2Dip0jY0WismHUCYmk/lex7s9fN7E+QmmeSFRBY8I?= =?utf-8?q?OqO0YkmEdOuOCIFhdjhsQXfU7EIIqmMVbHZi06ihqLm4ROTDnvBZrkCS0GMQUhEEj?= =?utf-8?q?rm3/Lqes48dKvWUjxLM/C2uFOwODv8xJUS9hXy40hmH8MDZRc3mVVnwgTAmuSp7tt?= =?utf-8?q?7KxbMvwKRaD?= X-OriginatorOrg: kunbus.com X-MS-Exchange-CrossTenant-Network-Message-Id: 44124829-273c-4d47-2c69-08dc053da643 X-MS-Exchange-CrossTenant-AuthSource: VI1P193MB0413.EURP193.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Dec 2023 11:35:52.2121 (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: Wk5hTa9PCnqEfH4FDfetHkjhmJyFeeuN39dA3C2Zz5599yjxvsALmC8bsgofMEX5qYo2wmNWjqURSiujMxEdHQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV1P193MB2070 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. Also reset old GPIO settings in case that changing the RS485 configuration failed. 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 Reviewed-by: Hugo Villeneuve Reviewed-by: Ilpo Järvinen Signed-off-by: Lino Sanfilippo --- drivers/tty/serial/imx.c | 4 ---- drivers/tty/serial/serial_core.c | 26 ++++++++++++++++++++++++-- drivers/tty/serial/stm32-usart.c | 5 +---- 3 files changed, 25 insertions(+), 10 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..d155131f221d 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -1402,6 +1402,16 @@ static void uart_set_rs485_termination(struct uart_port *port, !!(rs485->flags & SER_RS485_TERMINATE_BUS)); } +static void uart_set_rs485_rx_during_tx(struct uart_port *port, + const struct serial_rs485 *rs485) +{ + if (!(rs485->flags & SER_RS485_ENABLED)) + return; + + 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) { struct serial_rs485 *rs485 = &port->rs485; @@ -1413,12 +1423,17 @@ static int uart_rs485_config(struct uart_port *port) uart_sanitize_serial_rs485(port, rs485); uart_set_rs485_termination(port, rs485); + uart_set_rs485_rx_during_tx(port, rs485); uart_port_lock_irqsave(port, &flags); ret = port->rs485_config(port, NULL, rs485); uart_port_unlock_irqrestore(port, flags); - if (ret) + if (ret) { memset(rs485, 0, sizeof(*rs485)); + /* unset GPIOs */ + gpiod_set_value_cansleep(port->rs485_term_gpio, 0); + gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 0); + } return ret; } @@ -1457,6 +1472,7 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port, return ret; uart_sanitize_serial_rs485(port, &rs485); uart_set_rs485_termination(port, &rs485); + uart_set_rs485_rx_during_tx(port, &rs485); uart_port_lock_irqsave(port, &flags); ret = port->rs485_config(port, &tty->termios, &rs485); @@ -1468,8 +1484,14 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port, port->ops->set_mctrl(port, port->mctrl); } uart_port_unlock_irqrestore(port, flags); - if (ret) + if (ret) { + /* restore old GPIO settings */ + gpiod_set_value_cansleep(port->rs485_term_gpio, + !!(port->rs485.flags & SER_RS485_TERMINATE_BUS)); + gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, + !!(port->rs485.flags & SER_RS485_RX_DURING_TX)); return ret; + } if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485))) return -EFAULT; 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) {