[PATCH v2] tty: serial: 8250: Fix port count mismatch with the device

Crescent Hsieh posted 1 patch 1 year, 6 months ago
drivers/tty/serial/8250/8250_pci.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
[PATCH v2] tty: serial: 8250: Fix port count mismatch with the device
Posted by Crescent Hsieh 1 year, 6 months ago
Normally, the number of ports is indicated by the third digit of the
device ID on Moxa PCI serial boards. For example, `0x1121` indicates a
device with 2 ports.

However, `CP116E_A_A` and `CP116E_A_B` are exceptions; they have 8
ports, but the third digit of the device ID is `6`.

This patch introduces a function to retrieve the number of ports on Moxa
PCI serial boards, addressing the issue described above.

Fixes: 37058fd5d239 ("tty: serial: 8250: Add support for MOXA Mini PCIe boards")
Signed-off-by: Crescent Hsieh <crescentcy.hsieh@moxa.com>

---
Changes from v1 to v2:
- Add the "Fixes" tag correctly

v1: https://lore.kernel.org/all/20240614090322.2303-1-crescentcy.hsieh@moxa.com/

---
 drivers/tty/serial/8250/8250_pci.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c
index 40af74b55933..e1d7aa2fa347 100644
--- a/drivers/tty/serial/8250/8250_pci.c
+++ b/drivers/tty/serial/8250/8250_pci.c
@@ -1985,6 +1985,17 @@ enum {
 	MOXA_SUPP_RS485 = BIT(2),
 };
 
+static unsigned short moxa_get_nports(unsigned short device)
+{
+	switch (device) {
+	case PCI_DEVICE_ID_MOXA_CP116E_A_A:
+	case PCI_DEVICE_ID_MOXA_CP116E_A_B:
+		return 8;
+	}
+
+	return FIELD_GET(0x00F0, device);
+}
+
 static bool pci_moxa_is_mini_pcie(unsigned short device)
 {
 	if (device == PCI_DEVICE_ID_MOXA_CP102N	||
@@ -2038,7 +2049,7 @@ 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, i;
+	unsigned int i, num_ports = moxa_get_nports(device);
 	u8 val, init_mode = MOXA_RS232;
 
 	if (!(pci_moxa_supported_rs(dev) & MOXA_SUPP_RS232)) {
-- 
2.34.1
Re: [PATCH v2] tty: serial: 8250: Fix port count mismatch with the device
Posted by Andy Shevchenko 1 year, 6 months ago
On Mon, Jun 17, 2024 at 8:31 AM Crescent Hsieh
<crescentcy.hsieh@moxa.com> wrote:
>
> Normally, the number of ports is indicated by the third digit of the
> device ID on Moxa PCI serial boards. For example, `0x1121` indicates a
> device with 2 ports.
>
> However, `CP116E_A_A` and `CP116E_A_B` are exceptions; they have 8
> ports, but the third digit of the device ID is `6`.
>
> This patch introduces a function to retrieve the number of ports on Moxa
> PCI serial boards, addressing the issue described above.

Now it makes sense to me.
Reviewed-by: Andy Shevchenko <andy@kernel.org>

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v2] tty: serial: 8250: Fix port count mismatch with the device
Posted by Crescent CY Hsieh 1 year, 6 months ago
On Mon, Jun 17, 2024 at 12:54:34PM +0200, Andy Shevchenko wrote:
> On Mon, Jun 17, 2024 at 8:31 AM Crescent Hsieh
> <crescentcy.hsieh@moxa.com> wrote:
> >
> > Normally, the number of ports is indicated by the third digit of the
> > device ID on Moxa PCI serial boards. For example, `0x1121` indicates a
> > device with 2 ports.
> >
> > However, `CP116E_A_A` and `CP116E_A_B` are exceptions; they have 8
> > ports, but the third digit of the device ID is `6`.
> >
> > This patch introduces a function to retrieve the number of ports on Moxa
> > PCI serial boards, addressing the issue described above.
> 
> Now it makes sense to me.
> Reviewed-by: Andy Shevchenko <andy@kernel.org>

Thanks for your review, suggestions and explanation.

---
Sincerely,
Crescent Hsieh