mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiri Slaby <jirislaby@kernel.org>
To: Crescent CY Hsieh <crescentcy.hsieh@moxa.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org
Subject: Re: [PATCH 2/2] tty: serial: 8250: Add support for MOXA PCIe boards to switch interface
Date: Fri, 27 Oct 2023 10:29:38 +0200	[thread overview]
Message-ID: <37a5e055-93b5-4f05-bcca-4caa5ed6f97f@kernel.org> (raw)
In-Reply-To: <20231027062440.7749-3-crescentcy.hsieh@moxa.com>

On 27. 10. 23, 8:24, Crescent CY Hsieh wrote:
> MOXA PCIe boards have 4 serial interfaces and don't require additional
> stuff to switch between interfaces:
> 
> - RS232
> - RS422
> - RS485_2W (half-duplex)
> - RS485_4W (full-duplex)
> 
> By using ioctl command "TIOCRS485", it can switch between default
> interface and RS485 if supported.
> 
> That means, for RS422/RS485 board, it can switch between RS422 and
> RS485 by setting the flags within struct serial_rs485.
> 
> However, for the RS232/RS422/RS485 board, it can only switch between
> RS232 and RS485, there's no flag for switching interface into RS422.
> 
> This patch adds a flag call "SER_RS422_ENALBED" in serial.h and modifies

It's not "ENALBED".

Anyway, I am afraid you have to split the patch into two:
1) add the flag and core support (but wait a bit for others if they 
agree with this approach)
2) add the support for moxa.

> serial_core.c to make it possible to switch interface between RS232,
> RS422 and RS485.
...
> --- a/drivers/tty/serial/8250/8250_pci.c
> +++ b/drivers/tty/serial/8250/8250_pci.c

> @@ -2032,6 +2036,37 @@ static int pci_moxa_set_interface(const struct pci_dev *dev,
>   	return 0;
>   }
>   
> +/*
> + * MOXA PCIe boards support switching the serial interface using the ioctl
> + * command "TIOCSRS485".
> + *
> + *	RS232			= (no flags are set)
> + *	RS422			= SER_RS422_ENABLED
> + *	RS485_2W (half-duplex)	= SER_RS485_ENABLED
> + *	RS485_4W (full-duplex)	= SER_RS485_ENABLED | SER_RS485_RX_DURING_TX
> + */
> +static int pci_moxa_rs485_config(struct uart_port *port,
> +				 struct ktermios *termios,
> +				 struct serial_rs485 *rs485)
> +{
> +	struct pci_dev *dev = to_pci_dev(port->dev);
> +	u8 mode = MOXA_RS232;
> +
> +	if (rs485->flags & SER_RS485_ENABLED) {
> +		if (rs485->flags & SER_RS485_RX_DURING_TX)
> +			mode = MOXA_RS485_4W;
> +		else
> +			mode = MOXA_RS485_2W;
> +	} else if (rs485->flags & SER_RS422_ENABLED) {
> +		mode = MOXA_RS422;
> +	} else {
> +		if (!(pci_moxa_supported_rs(dev) & MOXA_SUPP_RS232))
> +			return -ENODEV;
> +	}
> +
> +	return pci_moxa_set_interface(dev, port->port_id, mode);

Looks good to me now.

> --- a/drivers/tty/serial/serial_core.c
> +++ b/drivers/tty/serial/serial_core.c
> @@ -1305,7 +1305,7 @@ static int uart_get_icount(struct tty_struct *tty,
>   
>   #define SER_RS485_LEGACY_FLAGS	(SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | \
>   				 SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX | \
> -				 SER_RS485_TERMINATE_BUS)
> +				 SER_RS485_TERMINATE_BUS | SER_RS422_ENABLED)
>   
>   static int uart_check_rs485_flags(struct uart_port *port, struct serial_rs485 *rs485)
>   {
> @@ -1371,11 +1371,26 @@ static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs4
>   {
>   	u32 supported_flags = port->rs485_supported.flags;
>   
> -	if (!(rs485->flags & SER_RS485_ENABLED)) {
> +	if (!(rs485->flags & SER_RS485_ENABLED) && !(rs485->flags & SER_RS422_ENABLED)) {

maybe easier:
   if (!(rs485->flags & (SER_RS485_ENABLED | SER_RS422_ENABLED))) {
?

>   		memset(rs485, 0, sizeof(*rs485));
>   		return;
>   	}
>   
> +	/* Pick sane setting if the user enables both interfaces */
> +	if (rs485->flags & SER_RS485_ENABLED && rs485->flags & SER_RS422_ENABLED) {
> +		dev_warn_ratelimited(port->dev,
> +			"%s (%d): Invalid serial interface setting, using RS485 instead\n",
> +			port->name, port->line);
> +		rs485->flags &= ~(SER_RS422_ENABLED);

No need for parens.

> +	}
> +
> +	/* Clear other bits and return if enable RS422 */

"if RS422 is enabled"
or
"if enabling RS422"

> +	if (rs485->flags & SER_RS422_ENABLED) {
> +		memset(rs485, 0, sizeof(*rs485));
> +		rs485->flags |= SER_RS422_ENABLED;
> +		return;
> +	}
> +
>   	/* Pick sane settings if the user hasn't */
>   	if ((supported_flags & (SER_RS485_RTS_ON_SEND|SER_RS485_RTS_AFTER_SEND)) &&
>   	    !(rs485->flags & SER_RS485_RTS_ON_SEND) ==
> @@ -1400,7 +1415,7 @@ static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs4
>   static void uart_set_rs485_termination(struct uart_port *port,
>   				       const struct serial_rs485 *rs485)
>   {
> -	if (!(rs485->flags & SER_RS485_ENABLED))
> +	if (!(rs485->flags & SER_RS485_ENABLED) && !(rs485->flags & SER_RS422_ENABLED))

if (!(rs485->flags & (SER_RS485_ENABLED | SER_RS422_ENABLED))) {

thanks,
-- 
js
suse labs


  reply	other threads:[~2023-10-27  8:29 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-27  6:24 [PATCH 0/2] tty: serial: 8250: Adjustments of MOXA PCIe boards serial interface Crescent CY Hsieh
2023-10-27  6:24 ` [PATCH 1/2] tty: serial: 8250: Fix MOXA RS422/RS485 PCIe boards not work by default Crescent CY Hsieh
2023-10-27  8:22   ` Jiri Slaby
2023-10-27  9:09   ` kernel test robot
2023-10-28 10:10   ` kernel test robot
2023-10-27  6:24 ` [PATCH 2/2] tty: serial: 8250: Add support for MOXA PCIe boards to switch interface Crescent CY Hsieh
2023-10-27  8:29   ` Jiri Slaby [this message]
2023-10-27 10:10     ` Crescent CY Hsieh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=37a5e055-93b5-4f05-bcca-4caa5ed6f97f@kernel.org \
    --to=jirislaby@kernel.org \
    --cc=crescentcy.hsieh@moxa.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®