2023-10-18 09:22:12

by Crescent CY Hsieh

[permalink] [raw]
Subject: [PATCH v3 0/6] tty: serial: 8250: Changes of MOXA PCIe boards in 8250_pci.c

These patch series do some changes to MOXA PCIe boards in 8250_pci.c,
including:

- Modify MOXA enum name within 8250_pci.c
- Cleanup MOXA configurations within 8250_pci.c
- Relocate macros within 8250_pci.c
- Add support for MOXA Mini PCIe boards
- Fix MOXA RS422/RS485 boards not function by default
- Add support for MOXA PCIe boards to switch between different serial
interfaces

Each patch depends on previous one.

---
Changes in v3:
- Add reviewer tag in first 4 patches.
- [PATCH v3 2/6]:
- Make commit message clearer.
- [PATCH v3 5/6]:
- Delete pci_moxa_match_x3xx() function.
- Introduce pci_moxa_match_second_digit() function.
- [PATCH v3 6/6]:
- Delete pci_moxa_match_x1xx() function.
- Replace pci_moxa_match_x1xx() and pci_moxa_match_x3xx() with
pci_moxa_match_second_digit().
- Add comment at pci_moxa_rs485_config().
- Revise comment and return value where device not support RS232
interface.

Changes in v2:
- Split cleanup patch into first 2 patches of this patch series.
- Reposition macro list to the top of the code.
- Use PCI_VDEVICE() instead of PCI_DEVICE() in serial_pci_tbl[].
- Replace some C99 types with kernel types.
- Introduce functions for checking device ID pattern.
- Replace magic constant with BIT().
- Sort macros into pre-existing macro list.
- Rewrite set_interface condition architecture.
- Utilize unused flag to represent RS422 interface instead of adding a
new one.

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

Crescent CY Hsieh (6):
tty: serial: 8250: Modify MOXA enum name within 8250_pci.c
tty: serial: 8250: Cleanup MOXA configurations within 8250_pci.c
tty: serial: 8250: Relocate macros within 8250_pci.c
tty: serial: 8250: Add support for MOXA Mini PCIe boards
tty: serial: 8250: Fix MOXA RS422/RS485 PCIe boards not work by
default
tty: serial: 8250: Add support for MOXA PCIe boards to switch
interface between RS422/RS485

drivers/tty/serial/8250/8250_pci.c | 370 ++++++++++++++++++++---------
1 file changed, 256 insertions(+), 114 deletions(-)

--
2.34.1


2023-10-18 09:22:14

by Crescent CY Hsieh

[permalink] [raw]
Subject: [PATCH v3 4/6] tty: serial: 8250: Add support for MOXA Mini PCIe boards

Add support for MOXA Mini PCIe serial boards:

- CP102N: 2 ports | RS232
- CP104N: 4 ports | RS232
- CP112N: 2 ports | RS232/RS422/RS485
- CP114N: 4 ports | RS232/RS422/RS485
- CP132N: 2 ports | RS422/RS485
- CP134N: 4 ports | RS422/RS485

Signed-off-by: Crescent CY Hsieh <[email protected]>
Reviewed-by: Jiri Slaby <[email protected]>
---
drivers/tty/serial/8250/8250_pci.c | 56 ++++++++++++++++++++++++++++++
1 file changed, 56 insertions(+)

diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
index ef23f1c6d..b2be3783f 100644
--- a/drivers/tty/serial/8250/8250_pci.c
+++ b/drivers/tty/serial/8250/8250_pci.c
@@ -83,14 +83,20 @@

#define PCI_DEVICE_ID_MOXA_CP102E 0x1024
#define PCI_DEVICE_ID_MOXA_CP102EL 0x1025
+#define PCI_DEVICE_ID_MOXA_CP102N 0x1027
#define PCI_DEVICE_ID_MOXA_CP104EL_A 0x1045
+#define PCI_DEVICE_ID_MOXA_CP104N 0x1046
+#define PCI_DEVICE_ID_MOXA_CP112N 0x1121
#define PCI_DEVICE_ID_MOXA_CP114EL 0x1144
+#define PCI_DEVICE_ID_MOXA_CP114N 0x1145
#define PCI_DEVICE_ID_MOXA_CP116E_A_A 0x1160
#define PCI_DEVICE_ID_MOXA_CP116E_A_B 0x1161
#define PCI_DEVICE_ID_MOXA_CP118EL_A 0x1182
#define PCI_DEVICE_ID_MOXA_CP118E_A_I 0x1183
#define PCI_DEVICE_ID_MOXA_CP132EL 0x1322
+#define PCI_DEVICE_ID_MOXA_CP132N 0x1323
#define PCI_DEVICE_ID_MOXA_CP134EL_A 0x1342
+#define PCI_DEVICE_ID_MOXA_CP134N 0x1343
#define PCI_DEVICE_ID_MOXA_CP138E_A 0x1381
#define PCI_DEVICE_ID_MOXA_CP168EL_A 0x1683

@@ -1957,6 +1963,49 @@ pci_sunix_setup(struct serial_private *priv,
return setup_port(priv, port, bar, offset, 0);
}

+#define MOXA_PUART_GPIO_EN 0x09
+#define MOXA_PUART_GPIO_OUT 0x0A
+
+#define MOXA_GPIO_PIN2 BIT(2)
+
+static bool pci_moxa_is_mini_pcie(unsigned short device)
+{
+ if (device == PCI_DEVICE_ID_MOXA_CP102N ||
+ device == PCI_DEVICE_ID_MOXA_CP104N ||
+ device == PCI_DEVICE_ID_MOXA_CP112N ||
+ device == PCI_DEVICE_ID_MOXA_CP114N ||
+ device == PCI_DEVICE_ID_MOXA_CP132N ||
+ device == PCI_DEVICE_ID_MOXA_CP134N)
+ return true;
+
+ return false;
+}
+
+static int pci_moxa_init(struct pci_dev *dev)
+{
+ unsigned short device = dev->device;
+ resource_size_t iobar_addr = pci_resource_start(dev, 2);
+ unsigned int num_ports = (device & 0x00F0) >> 4;
+ u8 val;
+
+ /*
+ * Enable hardware buffer to prevent break signal output when system boots up.
+ * This hardware buffer is only supported on Mini PCIe series.
+ */
+ if (pci_moxa_is_mini_pcie(device)) {
+ /* Set GPIO direction */
+ val = inb(iobar_addr + MOXA_PUART_GPIO_EN);
+ val |= MOXA_GPIO_PIN2;
+ outb(val, iobar_addr + MOXA_PUART_GPIO_EN);
+ /* Enable low GPIO */
+ val = inb(iobar_addr + MOXA_PUART_GPIO_OUT);
+ val &= ~MOXA_GPIO_PIN2;
+ outb(val, iobar_addr + MOXA_PUART_GPIO_OUT);
+ }
+
+ return num_ports;
+}
+
static int
pci_moxa_setup(struct serial_private *priv,
const struct pciserial_board *board,
@@ -2633,6 +2682,7 @@ static struct pci_serial_quirk pci_serial_quirks[] = {
.device = PCI_ANY_ID,
.subvendor = PCI_ANY_ID,
.subdevice = PCI_ANY_ID,
+ .init = pci_moxa_init,
.setup = pci_moxa_setup,
},
{
@@ -5349,14 +5399,20 @@ static const struct pci_device_id serial_pci_tbl[] = {
*/
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102E), pbn_moxa_2 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102EL), pbn_moxa_2 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102N), pbn_moxa_2 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL_A), pbn_moxa_4 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104N), pbn_moxa_4 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP112N), pbn_moxa_2 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114EL), pbn_moxa_4 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114N), pbn_moxa_4 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP116E_A_A), pbn_moxa_8 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP116E_A_B), pbn_moxa_8 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL_A), pbn_moxa_8 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118E_A_I), pbn_moxa_8 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132EL), pbn_moxa_2 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132N), pbn_moxa_2 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134EL_A), pbn_moxa_4 },
+ { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134N), pbn_moxa_4 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP138E_A), pbn_moxa_8 },
{ PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL_A), pbn_moxa_8 },

--
2.34.1

2023-10-18 09:22:17

by Crescent CY Hsieh

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

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 <[email protected]>
---
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

2023-10-19 06:06:56

by Jiri Slaby

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

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 <[email protected]>
> ---
> 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

2023-10-19 06:25:11

by Jiri Slaby

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

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 <[email protected]>
> ---
> 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

2023-10-21 16:32:08

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH v3 0/6] tty: serial: 8250: Changes of MOXA PCIe boards in 8250_pci.c

On Wed, Oct 18, 2023 at 05:17:33PM +0800, Crescent CY Hsieh wrote:
> These patch series do some changes to MOXA PCIe boards in 8250_pci.c,
> including:
>
> - Modify MOXA enum name within 8250_pci.c
> - Cleanup MOXA configurations within 8250_pci.c
> - Relocate macros within 8250_pci.c
> - Add support for MOXA Mini PCIe boards
> - Fix MOXA RS422/RS485 boards not function by default
> - Add support for MOXA PCIe boards to switch between different serial
> interfaces
>
> Each patch depends on previous one.

I've applied the first 4 to my tree now, plaese fix up the last two and
send them as a new patch series for review.

thanks,

greg k-h

2023-10-23 09:26:41

by Ilpo Järvinen

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

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 <[email protected]>
> ---
> 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.

2023-10-24 07:48:37

by Crescent CY Hsieh

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

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

2023-10-24 08:11:01

by Greg Kroah-Hartman

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

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