[PATCH v3 6/6] tty: serial: 8250: Add support for MOXA PCIe boards to switch interface between RS422/RS485

Crescent CY Hsieh posted 6 patches 2 years, 2 months ago
[PATCH v3 6/6] tty: serial: 8250: Add support for MOXA PCIe boards to switch interface between RS422/RS485
Posted by Crescent CY Hsieh 2 years, 2 months ago
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 uses "SER_RS485_TERMINATE_BUS" to represent RS422 as a
workaround solution:

- RS232                   = (no flags are set)
- RS422                   = SER_RS485_ENABLED | SER_RS485_TERMINATE_BUS
- RS485_2W (half-duplex)  = SER_RS485_ENABLED
- RS485_4W (full-duplex)  = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX

Signed-off-by: Crescent CY Hsieh <crescentcy.hsieh@moxa.com>
---
 drivers/tty/serial/8250/8250_pci.c | 58 ++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
index 29a28e72b..098ac466b 100644
--- a/drivers/tty/serial/8250/8250_pci.c
+++ b/drivers/tty/serial/8250/8250_pci.c
@@ -1974,6 +1974,10 @@ pci_sunix_setup(struct serial_private *priv,
 #define MOXA_RS485_2W	0x0F
 #define MOXA_UIR_OFFSET	0x04
 
+static const struct serial_rs485 pci_moxa_rs485_supported = {
+	.flags = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX | SER_RS485_TERMINATE_BUS,
+};
+
 static bool pci_moxa_is_mini_pcie(unsigned short device)
 {
 	if (device == PCI_DEVICE_ID_MOXA_CP102N	||
@@ -2024,6 +2028,46 @@ 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", but there is currently no dedicated flag for switching
+ * to RS422. As a workaround, we utilize the "SER_RS485_TERMINATE_BUS" flag to
+ * represent RS422.
+ *
+ *	RS232			= (no flags are set)
+ *	RS422			= SER_RS485_ENABLED | SER_RS485_TERMINATE_BUS
+ *	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);
+	unsigned short device = dev->device;
+	u8 mode = MOXA_RS232;
+
+	if (rs485->flags & SER_RS485_ENABLED) {
+		if (rs485->flags & SER_RS485_TERMINATE_BUS) {
+			mode = MOXA_RS422;
+		} else {
+			if (rs485->flags & SER_RS485_RX_DURING_TX)
+				mode = MOXA_RS485_4W;
+			else
+				mode = MOXA_RS485_2W;
+		}
+	} else {
+		/*
+		 * RS232 is not supported for MOXA PCIe boards with device IDs
+		 * matching the pattern 0x*3**.
+		 */
+		if (pci_moxa_match_second_digit(device, 0x0300))
+			return -EOPNOTSUPP;
+	}
+
+	return pci_moxa_set_interface(dev, port->port_id, mode);
+}
+
 static int pci_moxa_init(struct pci_dev *dev)
 {
 	unsigned short device = dev->device;
@@ -2063,9 +2107,23 @@ pci_moxa_setup(struct serial_private *priv,
 		const struct pciserial_board *board,
 		struct uart_8250_port *port, int idx)
 {
+	struct pci_dev *dev = priv->dev;
+	unsigned short device = dev->device;
 	unsigned int bar = FL_GET_BASE(board->flags);
 	int offset;
 
+	/*
+	 * For the device IDs of MOXA PCIe boards match the pattern 0x*1** and 0x*3**,
+	 * these boards support switching interface between RS422/RS485 using TIOCSRS485.
+	 */
+	if (pci_moxa_match_second_digit(device, 0x0100) ||
+	    pci_moxa_match_second_digit(device, 0x0300)) {
+		port->port.rs485_config = pci_moxa_rs485_config;
+		port->port.rs485_supported = pci_moxa_rs485_supported;
+
+		if (pci_moxa_match_second_digit(device, 0x0300))
+			port->port.rs485.flags |= SER_RS485_ENABLED | SER_RS485_TERMINATE_BUS;
+	}
 	if (board->num_ports == 4 && idx == 3)
 		offset = 7 * board->uart_offset;
 	else
-- 
2.34.1
Re: [PATCH v3 6/6] tty: serial: 8250: Add support for MOXA PCIe boards to switch interface between RS422/RS485
Posted by Ilpo Järvinen 2 years, 1 month ago
On Wed, 18 Oct 2023, 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 uses "SER_RS485_TERMINATE_BUS" to represent RS422 as a
> workaround solution:
> 
> - RS232                   = (no flags are set)
> - RS422                   = SER_RS485_ENABLED | SER_RS485_TERMINATE_BUS
> - RS485_2W (half-duplex)  = SER_RS485_ENABLED
> - RS485_4W (full-duplex)  = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX
> 
> Signed-off-by: Crescent CY Hsieh <crescentcy.hsieh@moxa.com>
> ---
>  drivers/tty/serial/8250/8250_pci.c | 58 ++++++++++++++++++++++++++++++
>  1 file changed, 58 insertions(+)
> 
> diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
> index 29a28e72b..098ac466b 100644
> --- a/drivers/tty/serial/8250/8250_pci.c
> +++ b/drivers/tty/serial/8250/8250_pci.c
> @@ -1974,6 +1974,10 @@ pci_sunix_setup(struct serial_private *priv,
>  #define MOXA_RS485_2W	0x0F
>  #define MOXA_UIR_OFFSET	0x04
>  
> +static const struct serial_rs485 pci_moxa_rs485_supported = {
> +	.flags = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX | SER_RS485_TERMINATE_BUS,

As recently discussed on linux-serial list, one of the RTS flags should be
enabled (the one the HW uses even if it's not configurable).

> +};
> +
>  static bool pci_moxa_is_mini_pcie(unsigned short device)
>  {
>  	if (device == PCI_DEVICE_ID_MOXA_CP102N	||
> @@ -2024,6 +2028,46 @@ 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", but there is currently no dedicated flag for switching
> + * to RS422. As a workaround, we utilize the "SER_RS485_TERMINATE_BUS" flag to
> + * represent RS422.
> + *
> + *	RS232			= (no flags are set)
> + *	RS422			= SER_RS485_ENABLED | SER_RS485_TERMINATE_BUS
> + *	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);
> +	unsigned short device = dev->device;
> +	u8 mode = MOXA_RS232;
> +
> +	if (rs485->flags & SER_RS485_ENABLED) {
> +		if (rs485->flags & SER_RS485_TERMINATE_BUS) {
> +			mode = MOXA_RS422;
> +		} else {
> +			if (rs485->flags & SER_RS485_RX_DURING_TX)
> +				mode = MOXA_RS485_4W;
> +			else
> +				mode = MOXA_RS485_2W;
> +		}
> +	} else {
> +		/*
> +		 * RS232 is not supported for MOXA PCIe boards with device IDs
> +		 * matching the pattern 0x*3**.
> +		 */
> +		if (pci_moxa_match_second_digit(device, 0x0300))
> +			return -EOPNOTSUPP;

This is not the correct error code I think. Maybe return -ENXIO instead.

> +	}
> +
> +	return pci_moxa_set_interface(dev, port->port_id, mode);
> +}
> +
>  static int pci_moxa_init(struct pci_dev *dev)
>  {
>  	unsigned short device = dev->device;
> @@ -2063,9 +2107,23 @@ pci_moxa_setup(struct serial_private *priv,
>  		const struct pciserial_board *board,
>  		struct uart_8250_port *port, int idx)
>  {
> +	struct pci_dev *dev = priv->dev;
> +	unsigned short device = dev->device;
>  	unsigned int bar = FL_GET_BASE(board->flags);
>  	int offset;
>  
> +	/*
> +	 * For the device IDs of MOXA PCIe boards match the pattern 0x*1** and 0x*3**,
> +	 * these boards support switching interface between RS422/RS485 using TIOCSRS485.
> +	 */
> +	if (pci_moxa_match_second_digit(device, 0x0100) ||
> +	    pci_moxa_match_second_digit(device, 0x0300)) {

As mentioned in the other patch, all these literals must be named.

> +		port->port.rs485_config = pci_moxa_rs485_config;
> +		port->port.rs485_supported = pci_moxa_rs485_supported;
> +
> +		if (pci_moxa_match_second_digit(device, 0x0300))
> +			port->port.rs485.flags |= SER_RS485_ENABLED | SER_RS485_TERMINATE_BUS;
> +	}
>  	if (board->num_ports == 4 && idx == 3)
>  		offset = 7 * board->uart_offset;
>  	else
> 

-- 
 i.
Re: [PATCH v3 6/6] tty: serial: 8250: Add support for MOXA PCIe boards to switch interface between RS422/RS485
Posted by Crescent CY Hsieh 2 years, 1 month ago
On Mon, Oct 23, 2023 at 12:25:18PM +0300, Ilpo Järvinen wrote:
> On Wed, 18 Oct 2023, Crescent CY Hsieh wrote:
> > +		/*
> > +		 * RS232 is not supported for MOXA PCIe boards with device IDs
> > +		 * matching the pattern 0x*3**.
> > +		 */
> > +		if (pci_moxa_match_second_digit(device, 0x0300))
> > +			return -EOPNOTSUPP;
> 
> This is not the correct error code I think. Maybe return -ENXIO instead.

I think EOPNOTSUPP or ENOTSUPP would be more reasonable, they directly
indicates "operation is not supported".

However, EOPNOTSUPP is used for network-related and ENOTSUPP is used for
NFSv3 protocol, even though they are already been used throughout the
kernel.

Maybe add a new one stand for serial, or clean them up into a general
one, or use EOPNOTSUPP and ENOTSUPP just for now?

---
Sincerely,
Crescent CY Hsieh
Re: [PATCH v3 6/6] tty: serial: 8250: Add support for MOXA PCIe boards to switch interface between RS422/RS485
Posted by Greg Kroah-Hartman 2 years, 1 month ago
On Tue, Oct 24, 2023 at 03:48:07PM +0800, Crescent CY Hsieh wrote:
> On Mon, Oct 23, 2023 at 12:25:18PM +0300, Ilpo Järvinen wrote:
> > On Wed, 18 Oct 2023, Crescent CY Hsieh wrote:
> > > +		/*
> > > +		 * RS232 is not supported for MOXA PCIe boards with device IDs
> > > +		 * matching the pattern 0x*3**.
> > > +		 */
> > > +		if (pci_moxa_match_second_digit(device, 0x0300))
> > > +			return -EOPNOTSUPP;
> > 
> > This is not the correct error code I think. Maybe return -ENXIO instead.
> 
> I think EOPNOTSUPP or ENOTSUPP would be more reasonable, they directly
> indicates "operation is not supported".
> 
> However, EOPNOTSUPP is used for network-related and ENOTSUPP is used for
> NFSv3 protocol, even though they are already been used throughout the
> kernel.
> 
> Maybe add a new one stand for serial, or clean them up into a general
> one, or use EOPNOTSUPP and ENOTSUPP just for now?

-ENODEV should be the proper one here.

thanks,

greg k-h
Re: [PATCH v3 6/6] tty: serial: 8250: Add support for MOXA PCIe boards to switch interface between RS422/RS485
Posted by Jiri Slaby 2 years, 2 months ago
On 18. 10. 23, 11:17, 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 uses "SER_RS485_TERMINATE_BUS" to represent RS422 as a
> workaround solution:
> 
> - RS232                   = (no flags are set)
> - RS422                   = SER_RS485_ENABLED | SER_RS485_TERMINATE_BUS
> - RS485_2W (half-duplex)  = SER_RS485_ENABLED
> - RS485_4W (full-duplex)  = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX
> 
> Signed-off-by: Crescent CY Hsieh <crescentcy.hsieh@moxa.com>
> ---
>   drivers/tty/serial/8250/8250_pci.c | 58 ++++++++++++++++++++++++++++++
>   1 file changed, 58 insertions(+)
> 
> diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
> index 29a28e72b..098ac466b 100644
> --- a/drivers/tty/serial/8250/8250_pci.c
> +++ b/drivers/tty/serial/8250/8250_pci.c
> @@ -1974,6 +1974,10 @@ pci_sunix_setup(struct serial_private *priv,
>   #define MOXA_RS485_2W	0x0F
>   #define MOXA_UIR_OFFSET	0x04
>   
> +static const struct serial_rs485 pci_moxa_rs485_supported = {
> +	.flags = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX | SER_RS485_TERMINATE_BUS,
> +};
> +
>   static bool pci_moxa_is_mini_pcie(unsigned short device)
>   {
>   	if (device == PCI_DEVICE_ID_MOXA_CP102N	||
> @@ -2024,6 +2028,46 @@ 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", but there is currently no dedicated flag for switching
> + * to RS422. As a workaround, we utilize the "SER_RS485_TERMINATE_BUS" flag to
> + * represent RS422.
> + *
> + *	RS232			= (no flags are set)
> + *	RS422			= SER_RS485_ENABLED | SER_RS485_TERMINATE_BUS
> + *	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);
> +	unsigned short device = dev->device;
> +	u8 mode = MOXA_RS232;
> +
> +	if (rs485->flags & SER_RS485_ENABLED) {
> +		if (rs485->flags & SER_RS485_TERMINATE_BUS) {
> +			mode = MOXA_RS422;
> +		} else {
> +			if (rs485->flags & SER_RS485_RX_DURING_TX)
> +				mode = MOXA_RS485_4W;
> +			else
> +				mode = MOXA_RS485_2W;
> +		}
> +	} else {
> +		/*
> +		 * RS232 is not supported for MOXA PCIe boards with device IDs
> +		 * matching the pattern 0x*3**.
> +		 */
> +		if (pci_moxa_match_second_digit(device, 0x0300))

The same here:
if (!(pci_moxa_supported_rs(dev) & MOXA_SUPP_RS232))

> +			return -EOPNOTSUPP;
> +	}
> +
> +	return pci_moxa_set_interface(dev, port->port_id, mode);
> +}
> +
>   static int pci_moxa_init(struct pci_dev *dev)
>   {
>   	unsigned short device = dev->device;
> @@ -2063,9 +2107,23 @@ pci_moxa_setup(struct serial_private *priv,
>   		const struct pciserial_board *board,
>   		struct uart_8250_port *port, int idx)
>   {
> +	struct pci_dev *dev = priv->dev;
> +	unsigned short device = dev->device;
>   	unsigned int bar = FL_GET_BASE(board->flags);
>   	int offset;
>   
> +	/*
> +	 * For the device IDs of MOXA PCIe boards match the pattern 0x*1** and 0x*3**,
> +	 * these boards support switching interface between RS422/RS485 using TIOCSRS485.
> +	 */
> +	if (pci_moxa_match_second_digit(device, 0x0100) ||
> +	    pci_moxa_match_second_digit(device, 0x0300)) {

if (pci_moxa_supported_rs(dev) & (MOXA_SUPP_RS422 | MOXA_SUPP_RS485) ==
     MOXA_SUPP_RS422 | MOXA_SUPP_RS485)

or maybe even simple:

if (pci_moxa_supported_rs(dev) & MOXA_SUPP_RS485)
?

> +		port->port.rs485_config = pci_moxa_rs485_config;
> +		port->port.rs485_supported = pci_moxa_rs485_supported;
> +
> +		if (pci_moxa_match_second_digit(device, 0x0300))

if (!(pci_moxa_supported_rs(dev) & MOXA_SUPP_RS232))

> +			port->port.rs485.flags |= SER_RS485_ENABLED | SER_RS485_TERMINATE_BUS;
> +	}
>   	if (board->num_ports == 4 && idx == 3)
>   		offset = 7 * board->uart_offset;
>   	else

-- 
js
suse labs
Re: [PATCH v3 6/6] tty: serial: 8250: Add support for MOXA PCIe boards to switch interface between RS422/RS485
Posted by Jiri Slaby 2 years, 2 months ago
On 18. 10. 23, 11:17, 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 uses "SER_RS485_TERMINATE_BUS" to represent RS422 as a
> workaround solution:
> 
> - RS232                   = (no flags are set)
> - RS422                   = SER_RS485_ENABLED | SER_RS485_TERMINATE_BUS
> - RS485_2W (half-duplex)  = SER_RS485_ENABLED
> - RS485_4W (full-duplex)  = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX
> 
> Signed-off-by: Crescent CY Hsieh <crescentcy.hsieh@moxa.com>
> ---
>   drivers/tty/serial/8250/8250_pci.c | 58 ++++++++++++++++++++++++++++++
>   1 file changed, 58 insertions(+)
> 
> diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
> index 29a28e72b..098ac466b 100644
> --- a/drivers/tty/serial/8250/8250_pci.c
> +++ b/drivers/tty/serial/8250/8250_pci.c
> @@ -1974,6 +1974,10 @@ pci_sunix_setup(struct serial_private *priv,
>   #define MOXA_RS485_2W	0x0F
>   #define MOXA_UIR_OFFSET	0x04
>   
> +static const struct serial_rs485 pci_moxa_rs485_supported = {
> +	.flags = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX | SER_RS485_TERMINATE_BUS,
> +};
> +
>   static bool pci_moxa_is_mini_pcie(unsigned short device)
>   {
>   	if (device == PCI_DEVICE_ID_MOXA_CP102N	||
> @@ -2024,6 +2028,46 @@ 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", but there is currently no dedicated flag for switching
> + * to RS422. As a workaround, we utilize the "SER_RS485_TERMINATE_BUS" flag to
> + * represent RS422.
> + *
> + *	RS232			= (no flags are set)
> + *	RS422			= SER_RS485_ENABLED | SER_RS485_TERMINATE_BUS

Oh, I noticed only now. Can we implement this properly? I mean by 
defining e.g. SER_RS422_ENABLED? And add checks to 
uart_check_rs485_flags() that only one of 485/422 is set and whatever 
else makes sense.

> + *	RS485_2W (half-duplex)	= SER_RS485_ENABLED
> + *	RS485_4W (full-duplex)	= SER_RS485_ENABLED | SER_RS485_RX_DURING_TX
> + */

thanks,
-- 
js
suse labs