Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp4595539rdb; Fri, 29 Dec 2023 07:05:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IGcyOozr4eAOJ/64bzsUZGrLFQlGCn6hlpALH+7fy2DklHsPn+ZVCI8XQ4sEZsFVUMWDp03 X-Received: by 2002:a17:902:c3c1:b0:1d4:10b5:2baa with SMTP id j1-20020a170902c3c100b001d410b52baamr4988523plj.79.1703862322398; Fri, 29 Dec 2023 07:05:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703862322; cv=none; d=google.com; s=arc-20160816; b=w1SkXmbicGwSEKeiGGmysCEaMCjd2W4Fi7vg95D17K/dz0dMIcEfYXWOOyue45PI4z BM8iBBSDU6Mx32MLEFvoOz/9CDgfwzE5jGDucjBoXIR8a4rN1g2h7rImPq8KnddoFJJ0 L9u2LkzUjbeodpGKQM9bMq9lioihiTAPe6Cc9kCasSi8GEJTNqw7xbbtMIFQbfKhNbBc Lyi1RE/VnDTY691y9vvp1vrf7Qp/Rgub2YCB5qX2aaUwQ1WIOjCtVVJ3mITHp6yNrIMb H0o5I1R9bq3Teq7tUnEjH24UrZ8lWuiZAj06Xy1cALefzOWykjqnYu2VD3Yz3iNn044u j+1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=ui-outboundreport:content-transfer-encoding:content-language :in-reply-to:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:user-agent:date:message-id:references:cc:to:subject:from :dkim-signature; bh=AVsyOvF8iyVIZ+y0HTYWiJ5lv/jurTEehXeDcW566s0=; fh=Cui6N3tK8eIjJ8k9yubWrYe3fQ7WibulN2lMB9/popY=; b=0C0FqPSQpQbJ7AIF/RuYkXnZVDZYoKeNt2d05wpwP8o97prfW/PdX9jDnoLbaZ6knv 6HmbXnbSf8XEIdoMzj4dwtILa2pssfp21YKcWNdSnNp05Ow5r4aUwLlnpO1rRegSjqzo Rfd3IRzwNxbpmyXkDQqeIrs50gQ8Wyc2620rG3QngeTNct2gbQKcFUE3QsPWMmCtuZQE 44w9ZL8ACiy3f1IGvEqsZccJal1bGHRlDeMj5bc7w2PXfvXR388WpOqN+6RCTM7zjoHG Grjm32lgnIR9iUJEOaHh9oEWAjnhRByMM3B3tL6Wslsd1hmiSfMoirgECeGBQuBzNXnz EuTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmx.de header.s=s31663417 header.b=r2dZZbEa; spf=pass (google.com: domain of linux-kernel+bounces-13128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13128-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gmx.de Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d12-20020a170902728c00b001d408a4f5fdsi13597513pll.646.2023.12.29.07.05.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Dec 2023 07:05:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-13128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmx.de header.s=s31663417 header.b=r2dZZbEa; spf=pass (google.com: domain of linux-kernel+bounces-13128-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13128-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gmx.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 7892AB223DC for ; Fri, 29 Dec 2023 15:05:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B951B125A2; Fri, 29 Dec 2023 15:05:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmx.de header.i=linosanfilippo@gmx.de header.b="r2dZZbEa" X-Original-To: linux-kernel@vger.kernel.org Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) (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 B743411CAE; Fri, 29 Dec 2023 15:05:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1703862196; x=1704466996; i=linosanfilippo@gmx.de; bh=AVsyOvF8iyVIZ+y0HTYWiJ5lv/jurTEehXeDcW566s0=; h=X-UI-Sender-Class:From:Subject:To:Cc:References:Date: In-Reply-To; b=r2dZZbEaWPakyKajbHilP2dnmyJQIb86Rl008O7k9kDpUqJpmZQEZkjI/NXiK8rG MnvCowJCC5OgZxpapX/fGY8SxDumm/i7qAlZayspO5DABfCL8PdKmD4Y2YnRV09JR TmR+fpSUNJ1mwUOskIIoBrgZzrLIamEi2adOrQIK19sHEF039vFt+VjnDngfrAuV1 Es9klZjHMmjwV1MA9dqP1NpU34hNI2+tCLoNhxBp/YTY0Dwd0oA6bdrZ7nnw5Y+I/ c+/jpKt9464KyX6IwDzsyj9R/3fyRd7t4v/cdh8jOpnyGG8QyzF1ZwLOTIh6Emml9 aG+lcQpdgMCs2IVwag== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [192.168.2.37] ([84.162.15.98]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MVvPJ-1rilU12tdd-00Rsyf; Fri, 29 Dec 2023 16:03:16 +0100 From: Lino Sanfilippo Subject: Re: [PATCH v6 1/7] serial: Do not hold the port lock when setting rx-during-tx GPIO To: Maarten Brock , Lino Sanfilippo Cc: gregkh@linuxfoundation.org, jirislaby@kernel.org, ilpo.jarvinen@linux.intel.com, 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, lukas@wunner.de, p.rosenberger@kunbus.com, stable@vger.kernel.org, Hugo Villeneuve References: <20231225113524.8800-1-l.sanfilippo@kunbus.com> <20231225113524.8800-2-l.sanfilippo@kunbus.com> <5177a7aef77a6b77a6e742a2fdd52a0e@vanmierlo.com> Message-ID: <988518d5-0d4f-1362-64f9-8bfeb3e3b700@gmx.de> Date: Fri, 29 Dec 2023 16:03:13 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: <5177a7aef77a6b77a6e742a2fdd52a0e@vanmierlo.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:p+fqf9l7jTjDJGxOH1YnDruj+CgRcfHEjHCkTRBGnhLYpt29OXV R50OBYMIgvUo5aIQm5sh9SxU6sVZXMnlFA0+GDq6udaQo3wcl+rbv34yuG5VN29b2Vlffu6 zAvc8CpRqId2jLxcr6nPA9IofjPOvyAU9zamJUHNf+kGZ62cHjoS5DczHJqzwEn8+y+LMaq NrW/B/qTVorJKXXGloH4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13:31, Maarten Brock wrote: > Lino Sanfilippo wrote on 2023-12-25 12:35: >> diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/seri= al_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, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 !!(rs485->flags & SER_RS485_TERMINATE_BU= S)); >> =C2=A0} >> >> +static void uart_set_rs485_rx_during_tx(struct uart_port *port, >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 const struct serial_rs485 *r= s485) >> +{ >> +=C2=A0=C2=A0=C2=A0 if (!(rs485->flags & SER_RS485_ENABLED)) >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return; >> + > > How about checking port->rs485_rx_during_tx_gpio here against NULL inste= ad of > before every call? > gpiod_set_value_cansleep() already checks for a NULL pointer, so doing thi= s check in the caller is not needed. >> +=C2=A0=C2=A0=C2=A0 gpiod_set_value_cansleep(port->rs485_rx_during_tx_g= pio, >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 !!(rs485->flags & SER_RS485_RX_DURING_TX)); >> +} >> + >> =C2=A0static int uart_rs485_config(struct uart_port *port) >> =C2=A0{ >> =C2=A0=C2=A0=C2=A0=C2=A0 struct serial_rs485 *rs485 =3D &port->rs485; >> @@ -1413,12 +1423,17 @@ static int uart_rs485_config(struct uart_port *= port) >> >> =C2=A0=C2=A0=C2=A0=C2=A0 uart_sanitize_serial_rs485(port, rs485); >> =C2=A0=C2=A0=C2=A0=C2=A0 uart_set_rs485_termination(port, rs485); >> +=C2=A0=C2=A0=C2=A0 uart_set_rs485_rx_during_tx(port, rs485); >> >> =C2=A0=C2=A0=C2=A0=C2=A0 uart_port_lock_irqsave(port, &flags); >> =C2=A0=C2=A0=C2=A0=C2=A0 ret =3D port->rs485_config(port, NULL, rs485); >> =C2=A0=C2=A0=C2=A0=C2=A0 uart_port_unlock_irqrestore(port, flags); >> -=C2=A0=C2=A0=C2=A0 if (ret) >> +=C2=A0=C2=A0=C2=A0 if (ret) { >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 memset(rs485, 0, sizeo= f(*rs485)); >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* unset GPIOs */ >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 gpiod_set_value_cansleep(po= rt->rs485_term_gpio, 0); >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 gpiod_set_value_cansleep(po= rt->rs485_rx_during_tx_gpio, 0); >> +=C2=A0=C2=A0=C2=A0 } >> >> =C2=A0=C2=A0=C2=A0=C2=A0 return ret; >> =C2=A0} >> @@ -1457,6 +1472,7 @@ static int uart_set_rs485_config(struct >> tty_struct *tty, struct uart_port *port, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return ret; >> =C2=A0=C2=A0=C2=A0=C2=A0 uart_sanitize_serial_rs485(port, &rs485); >> =C2=A0=C2=A0=C2=A0=C2=A0 uart_set_rs485_termination(port, &rs485); >> +=C2=A0=C2=A0=C2=A0 uart_set_rs485_rx_during_tx(port, &rs485); >> >> =C2=A0=C2=A0=C2=A0=C2=A0 uart_port_lock_irqsave(port, &flags); >> =C2=A0=C2=A0=C2=A0=C2=A0 ret =3D 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, >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 port->ops->set_mctrl(port, port->mctrl); >> =C2=A0=C2=A0=C2=A0=C2=A0 } >> =C2=A0=C2=A0=C2=A0=C2=A0 uart_port_unlock_irqrestore(port, flags); >> -=C2=A0=C2=A0=C2=A0 if (ret) >> +=C2=A0=C2=A0=C2=A0 if (ret) { >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* restore old GPIO setting= s */ >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 gpiod_set_value_cansleep(po= rt->rs485_term_gpio, >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 !!(= port->rs485.flags & SER_RS485_TERMINATE_BUS)); >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 gpiod_set_value_cansleep(po= rt->rs485_rx_during_tx_gpio, >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 !!(= port->rs485.flags & SER_RS485_RX_DURING_TX)); > > This does not look like restoring. Hmm. The rx-during-tx and terminate-bus GPIOs may have changed before the drivers rs485_config() was called. If that function fails, the GPIOs are set back to the values they had before (i.e what is still stored in the ports serial_rs485 struct). So what is wrong with the term "restore"? > Further this looks suspiciously like duplicated code Since the added code consists of two one-liners I am not sure how to decrease code duplication in this case. We could introduce wrapper functio= ns (the only ones we have so far to set the GPIOs are uart_set_rs485_termination() and uart_set_rs485_rx_during_tx() which cannot be used here due to the initial check for SER_RS485_ENABLED). But would that really help? > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return ret; >> +=C2=A0=C2=A0=C2=A0 } >> >> =C2=A0=C2=A0=C2=A0=C2=A0 if (copy_to_user(rs485_user, &port->rs485, siz= eof(port->rs485))) >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return -EFAULT; >> diff --git a/drivers/tty/serial/stm32-usart.c b/drivers/tty/serial/stm3= 2-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 >> >> =C2=A0=C2=A0=C2=A0=C2=A0 stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->= uart_enable_bit)); >> >> -=C2=A0=C2=A0=C2=A0 if (port->rs485_rx_during_tx_gpio) >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 gpiod_set_value_cansleep(po= rt->rs485_rx_during_tx_gpio, >> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 !!(rs485conf->flags & = SER_RS485_RX_DURING_TX)); >> -=C2=A0=C2=A0=C2=A0 else >> +=C2=A0=C2=A0=C2=A0 if (!port->rs485_rx_during_tx_gpio) > > Should the ! be there? > Thats a good point, the "else" seems indeed to be wrong. It has been intro= duced with the code that added the GPIO support (c54d48543689 "serial: stm32: Ad= d support for rs485 RX_DURING_TX output GPIO") I will fix it in the next version of this patch, thanks. >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 rs485conf->flags |=3D = SER_RS485_RX_DURING_TX; >> >> =C2=A0=C2=A0=C2=A0=C2=A0 if (rs485conf->flags & SER_RS485_ENABLED) { > > Kind Regards > Maarten Brock > Thanks a lot for the review. BR, Lino