Skip to contentSkip to conversation

USB: serial: CH341: add hardware flow control RTS/CTS

PATCH In review Lode Willems 2 participants 2 messages Dec 18, 2024 – Jan 8, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 USB: serial: CH341: add hardware flow control RTS/CTS

This adds support for enabling and disabling
RTS/CTS hardware flow control.
Tested using CH341A and CH340E.

Fixes part of the following bug report:

---

Changes in v2:
- Change the CH341_FLOW_CTL_* values to be 8 bits wide
- Set flow control setting unconditionally
- Prepare parameters and do one control request
- Drop the error message

 drivers/usb/serial/ch341.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/usb/serial/ch341.c +28 -0
index d10e4c4848a0..c255a8437e4a 100644
@@ -63,6 +63,7 @@
 #define CH341_REG_DIVISOR      0x13
 #define CH341_REG_LCR          0x18
 #define CH341_REG_LCR2         0x25
+#define CH341_REG_FLOW_CTL     0x27
 
 #define CH341_NBREAK_BITS      0x01
 
@@ -77,6 +78,9 @@
 #define CH341_LCR_CS6          0x01
 #define CH341_LCR_CS5          0x00
 
+#define CH341_FLOW_CTL_NONE    0x00
+#define CH341_FLOW_CTL_RTSCTS  0x01
+
 #define CH341_QUIRK_LIMITED_PRESCALER	BIT(0)
 #define CH341_QUIRK_SIMULATE_BREAK	BIT(1)
 
@@ -478,6 +482,28 @@ static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
 	return r;
 }
 
+static void ch341_set_flow_control(struct tty_struct *tty,
+				   struct usb_serial_port *port,
+				   const struct ktermios *old_termios)
+{
+	int r;
+	u16 flow_ctl;
+
+	if (C_CRTSCTS(tty))
+		flow_ctl = ((u16)CH341_FLOW_CTL_RTSCTS << 8) | CH341_FLOW_CTL_RTSCTS;
+	else
+		flow_ctl = ((u16)CH341_FLOW_CTL_NONE << 8) | CH341_FLOW_CTL_NONE;
+
+	r = ch341_control_out(port->serial->dev,
+			      CH341_REQ_WRITE_REG,
+			      ((u16)CH341_REG_FLOW_CTL << 8) | CH341_REG_FLOW_CTL,
+			      flow_ctl);
+	if (r < 0 && old_termios) {
+		tty->termios.c_cflag &= ~CRTSCTS;
+		tty->termios.c_cflag |= (old_termios->c_cflag & CRTSCTS);
+	}
+}
+
 /* Old_termios contains the original termios settings and
  * tty->termios contains the new setting to be used.
  */
@@ -546,6 +572,8 @@ static void ch341_set_termios(struct tty_struct *tty,
 	spin_unlock_irqrestore(&priv->lock, flags);
 
 	ch341_set_handshake(port->serial->dev, priv->mcr);
+
+	ch341_set_flow_control(tty, port, old_termios);
 }
 
 /*
Signature · 1 line
2.47.1
maintainer
#1 lore raw
Quoted 7 lines · Lode Willems

This adds support for enabling and disabling
RTS/CTS hardware flow control.
Tested using CH341A and CH340E.

Fixes part of the following bug report:

Quoted 8 lines

---

Changes in v2:
- Change the CH341_FLOW_CTL_* values to be 8 bits wide
- Set flow control setting unconditionally
- Prepare parameters and do one control request
- Drop the error message

Thanks for the update.

Quoted 11 lines
drivers/usb/serial/ch341.c +9 -0
+#define CH341_REG_FLOW_CTL     0x27
 
+#define CH341_FLOW_CTL_NONE    0x00
+#define CH341_FLOW_CTL_RTSCTS  0x01
 
+static void ch341_set_flow_control(struct tty_struct *tty,
+				   struct usb_serial_port *port,
+				   const struct ktermios *old_termios)
+{
+	int r;
+	u16 flow_ctl;

drivers/usb/serial/ch341.c static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port) · line 500

Quoted 10 lines
drivers/usb/serial/ch341.c +10 -0
+
+	if (C_CRTSCTS(tty))
+		flow_ctl = ((u16)CH341_FLOW_CTL_RTSCTS << 8) | CH341_FLOW_CTL_RTSCTS;
+	else
+		flow_ctl = ((u16)CH341_FLOW_CTL_NONE << 8) | CH341_FLOW_CTL_NONE;
+
+	r = ch341_control_out(port->serial->dev,
+			      CH341_REQ_WRITE_REG,
+			      ((u16)CH341_REG_FLOW_CTL << 8) | CH341_REG_FLOW_CTL,
+			      flow_ctl);

I dropped the unnecessary casts above (and will follow-up with a patch removing one such cast from the existing code). I also think preparing the index argument once in the call (i.e. (flow_ctl << 8) | flow_ctl)) makes the code a bit more readable.

End result is here:

	https://git.kernel.org/pub/scm/linux/kernel/git/johan/usb-serial.git/commit/?h=usb-next&id=35478bc369a67b703a079ee123c6e58290114aae	

Johan