USB: serial: CH341: add hardware flow control RTS/CTS
Subsystems (2)
PATCH RESEND 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 a CH340E in combination with a CP2102.
Fixes part of the following bug report:
--- drivers/usb/serial/ch341.c | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)
drivers/usb/serial/ch341.c +41 -0
@@ -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 0x000 +#define CH341_FLOW_CTL_RTSCTS 0x101 +#define CH341_QUIRK_LIMITED_PRESCALER BIT(0)#define CH341_QUIRK_SIMULATE_BREAK BIT(1)
@@ -478,6 +482,41 @@ 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; + +if (old_termios && +C_CRTSCTS (tty) == (old_termios->c_cflag &CRTSCTS )) +return ; + +if (C_CRTSCTS (tty)) { + r =ch341_control_out (port->serial->dev,CH341_REQ_WRITE_REG , +CH341_REG_FLOW_CTL | + ((u16 )CH341_REG_FLOW_CTL <<8 ), +CH341_FLOW_CTL_RTSCTS ); +if (r <0 ) { +dev_err (&port->dev, +"%s - failed to enable flow control: %d\n" , + __func__, r); + tty->termios.c_cflag &= ~CRTSCTS ; + } + }else { + r =ch341_control_out (port->serial->dev,CH341_REQ_WRITE_REG , +CH341_REG_FLOW_CTL | + ((u16 )CH341_REG_FLOW_CTL <<8 ), +CH341_FLOW_CTL_NONE ); +if (r <0 ) { +dev_err (&port->dev, +"%s - failed to disable flow control: %d\n" , + __func__, r); + tty->termios.c_cflag |=CRTSCTS ; + } + } +} +/* Old_termios contains the original termios settings and * tty->termios contains the new setting to be used. */
@@ -546,6 +585,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.0
Johan Hovold
maintainerQuoted 8 lines · Lode Willems
This adds support for enabling and disabling RTS/CTS hardware flow control.
Tested using a CH340E in combination with a CP2102.Fixes part of the following bug report:
Signed-off-by Lode Willems <[email protected]>
Thanks for the patch and sorry about the late feedback on this one. I wanted to give it a spin with the devices I have here (ch340g and ch341a).
Appears to the modem control lines are not wired up on the ch341a evaluation board I have, but the device accepts the request and stops transmitting with hardware flow control enabled.
With ch340g in loopback, I also see it refuse to transmit unless cts is asserted. I was not able to get the device to deassert rts when attempting to fill its receive buffers, however. Perhaps the hardware does not support this, but is this something you tried?
Quoted 17 lines
--- drivers/usb/serial/ch341.c | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)drivers/usb/serial/ch341.c +1
-0@@ -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
drivers/usb/serial/ch341.c line 82
Quoted 5 lines
drivers/usb/serial/ch341.c +2
-0@@ -77,6 +78,9 @@#define CH341_LCR_CS6 0x01#define CH341_LCR_CS5 0x00 +#define CH341_FLOW_CTL_NONE 0x000 +#define CH341_FLOW_CTL_RTSCTS 0x101
The registers are eight bits wide, but the driver writes two at a time. So this should just be 0x00 and 0x01.
Quoted 7 lines
drivers/usb/serial/ch341.c +1
-0+#define CH341_QUIRK_LIMITED_PRESCALER BIT(0)#define CH341_QUIRK_SIMULATE_BREAK BIT(1)@@ -478,6 +482,41 @@ static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)return r; }
drivers/usb/serial/ch341.c static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port) · line 493
Quoted 10 lines
drivers/usb/serial/ch341.c +9
-0+static void ch341_set_flow_control (struct tty_struct *tty, +struct usb_serial_port *port, +const struct ktermios *old_termios) +{ +int r; + +if (old_termios && +C_CRTSCTS (tty) == (old_termios->c_cflag &CRTSCTS )) +return ;
Just drop this and set the requested setting unconditionally.
Quoted 14 lines
drivers/usb/serial/ch341.c +14
-0+ +if (C_CRTSCTS (tty)) { + r =ch341_control_out (port->serial->dev,CH341_REQ_WRITE_REG , +CH341_REG_FLOW_CTL | + ((u16 )CH341_REG_FLOW_CTL <<8 ), +CH341_FLOW_CTL_RTSCTS ); +if (r <0 ) { +dev_err (&port->dev, +"%s - failed to enable flow control: %d\n" , + __func__, r); + tty->termios.c_cflag &= ~CRTSCTS ; + } + }else { + r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
drivers/usb/serial/ch341.c static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port) · line 517
Quoted 10 lines
drivers/usb/serial/ch341.c +10
-0+CH341_REG_FLOW_CTL | + ((u16 )CH341_REG_FLOW_CTL <<8 ), + CH341_FLOW_CTL_NONE); +if (r <0 ) { +dev_err (&port->dev, +"%s - failed to disable flow control: %d\n" , + __func__, r); + tty->termios.c_cflag |=CRTSCTS ; + } + }
Please rewrite this so that you prepare the value and index parameters based on the termios settings and then do one control request. If it fails you can restore the old setting (if old_termios is non-NULL).
And please drop the redundant __func__ from the error message (even if the driver still uses it in some other functions still).
drivers/usb/serial/ch341.c static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port) · line 519
Quoted 2 lines
drivers/usb/serial/ch341.c +2
-0+} +
Johan
Quoted 5 lines · Johan Hovold
Quoted 4 lines · Lode Willems
This adds support for enabling and disabling RTS/CTS hardware flow control.
Tested using a CH340E in combination with a CP2102.
Quoted 8 lines
Quoted 4 lines
Fixes part of the following bug report:
Signed-off-by Lode Willems <[email protected]>Thanks for the patch and sorry about the late feedback on this one. I wanted to give it a spin with the devices I have here (ch340g and ch341a).
Thanks for the review. Sorry in advance if this e-mail is formatted incorrectly, this is my first time replaying to a review.
Quoted 5 lines
Appears to the modem control lines are not wired up on the ch341a evaluation board I have, but the device accepts the request and stops transmitting with hardware flow control enabled.
Since creating this patch I've acquired a ch341a breakout board and can confirm that it transmits with hardware flow control enabled and CTS asserted.
Quoted 5 lines
With ch340g in loopback, I also see it refuse to transmit unless cts is asserted. I was not able to get the device to deassert rts when attempting to fill its receive buffers, however. Perhaps the hardware does not support this, but is this something you tried?
I didn't try this before. Just trying a couple of things now I also couldn't seem to make it deassert RTS, but this may be a failure of how I'm testing this.
Quoted 20 lines
Quoted 20 lines
--- drivers/usb/serial/ch341.c | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)drivers/usb/serial/ch341.c +1
-0@@ -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
Quoted 5 lines
drivers/usb/serial/ch341.c line 82
Quoted 2 lines
drivers/usb/serial/ch341.c +2
-0+#define CH341_FLOW_CTL_NONE 0x000 +#define CH341_FLOW_CTL_RTSCTS 0x101The registers are eight bits wide, but the driver writes two at a time. So this should just be
0x00and0x01.
Ok. I'm guessing I don't have to send the same request twice and can just leave the top eight bits of the value and index empty? It seems to work in the quick testing I've done.
Quoted 9 lines
Quoted 9 lines
drivers/usb/serial/ch341.c +1
-0+#define CH341_QUIRK_LIMITED_PRESCALER BIT(0)#define CH341_QUIRK_SIMULATE_BREAK BIT(1)@@ -478,6 +482,41 @@ static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)return r; }
Quoted 11 lines
drivers/usb/serial/ch341.c static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port) · line 493
Quoted 9 lines
drivers/usb/serial/ch341.c +9
-0+static void ch341_set_flow_control (struct tty_struct *tty, +struct usb_serial_port *port, +const struct ktermios *old_termios) +{ +int r; + +if (old_termios && +C_CRTSCTS (tty) == (old_termios->c_cflag &CRTSCTS )) +return ;Just drop this and set the requested setting unconditionally.
Ok
Quoted 21 lines
Quoted 21 lines
drivers/usb/serial/ch341.c +20
-0+ +if (C_CRTSCTS (tty)) { + r =ch341_control_out (port->serial->dev,CH341_REQ_WRITE_REG , +CH341_REG_FLOW_CTL | + ((u16 )CH341_REG_FLOW_CTL <<8 ), +CH341_FLOW_CTL_RTSCTS ); +if (r <0 ) { +dev_err (&port->dev, +"%s - failed to enable flow control: %d\n" , + __func__, r); + tty->termios.c_cflag &= ~CRTSCTS ; + } + }else { + r =ch341_control_out (port->serial->dev,CH341_REQ_WRITE_REG , +CH341_REG_FLOW_CTL | + ((u16 )CH341_REG_FLOW_CTL <<8 ), +CH341_FLOW_CTL_NONE ); +if (r <0 ) { + dev_err(&port->dev, + "%s - failed to disable flow control: %d\n",
Quoted 8 lines
drivers/usb/serial/ch341.c static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port) · line 517
Quoted 4 lines
drivers/usb/serial/ch341.c +4
-0+ __func__, r); + tty->termios.c_cflag |=CRTSCTS ; + } + }Please rewrite this so that you prepare the value and index parameters based on the termios settings and then do one control request. If it fails you can restore the old setting (if
old_termiosis non-NULL).
Ok, I haven't seen any other driver restore the old setting on request failure, now I'm questioning if it's actually necessary?
If it is, I'll change it to the following:
tty->termios.c_cflag = (tty->termios.c_cflag & !CRTSCTS) | (old_termios->c_cflag & CRTSCTS);
Quoted 3 lines
And please drop the redundant
__func__from the error message (even if the driver still uses it in some other functions still).
Ok. Looking at the code again, the error already gets logged in ch341_control_out on failure. Maybe this log line shouldn't be added?
Quoted 5 lines
drivers/usb/serial/ch341.c static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port) · line 519
Quoted 3 lines
drivers/usb/serial/ch341.c +2
-0+} +Johan
Lode
Johan Hovold
maintainerQuoted 9 lines · Lode Willems
Quoted 8 lines · Johan Hovold
Quoted 7 lines · Lode Willems
This adds support for enabling and disabling RTS/CTS hardware flow control.
Tested using a CH340E in combination with a CP2102.Fixes part of the following bug report:
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Signed-off-by Lode Willems <[email protected]>Thanks for the patch and sorry about the late feedback on this one. I wanted to give it a spin with the devices I have here (ch340g and ch341a).
Thanks for the review. Sorry in advance if this e-mail is formatted incorrectly, this is my first time replaying to a review.
Looks all good.
Quoted 7 lines
Quoted 3 lines
Appears to the modem control lines are not wired up on the ch341a evaluation board I have, but the device accepts the request and stops transmitting with hardware flow control enabled.
Since creating this patch I've acquired a ch341a breakout board and can confirm that it transmits with hardware flow control enabled and CTS asserted.
Thanks for checking.
Quoted 8 lines
Quoted 4 lines
With ch340g in loopback, I also see it refuse to transmit unless cts is asserted. I was not able to get the device to deassert rts when attempting to fill its receive buffers, however. Perhaps the hardware does not support this, but is this something you tried?
I didn't try this before. Just trying a couple of things now I also couldn't seem to make it deassert RTS, but this may be a failure of how I'm testing this.
Or it's a limitation of the hardware (e.g. it really only support auto-cts). I patched the driver to not submit any read urbs, which should eventually fill up the receive buffers.
Quoted 9 lines
Quoted 5 lines
drivers/usb/serial/ch341.c line 82
Quoted 2 lines
drivers/usb/serial/ch341.c +2
-0+#define CH341_FLOW_CTL_NONE 0x000 +#define CH341_FLOW_CTL_RTSCTS 0x101The registers are eight bits wide, but the driver writes two at a time. So this should just be
0x00and0x01.Ok. I'm guessing I don't have to send the same request twice and can just leave the top eight bits of the value and index empty? It seems to work in the quick testing I've done.
Possibly, but writing the same register twice is what the vendor driver is doing here (e.g. otherwise you may actually write to offset 0 as well).
Quoted 44 lines
Quoted 11 lines
drivers/usb/serial/ch341.c static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port) · line 493
Quoted 9 lines
drivers/usb/serial/ch341.c +9
-0+static void ch341_set_flow_control (struct tty_struct *tty, +struct usb_serial_port *port, +const struct ktermios *old_termios) +{ +int r; + +if (old_termios && +C_CRTSCTS (tty) == (old_termios->c_cflag &CRTSCTS )) +return ;Just drop this and set the requested setting unconditionally.
Ok
Quoted 21 lines
Quoted 21 lines
drivers/usb/serial/ch341.c +20
-0+ +if (C_CRTSCTS (tty)) { + r =ch341_control_out (port->serial->dev,CH341_REQ_WRITE_REG , +CH341_REG_FLOW_CTL | + ((u16 )CH341_REG_FLOW_CTL <<8 ), +CH341_FLOW_CTL_RTSCTS ); +if (r <0 ) { +dev_err (&port->dev, +"%s - failed to enable flow control: %d\n" , + __func__, r); + tty->termios.c_cflag &= ~CRTSCTS ; + } + }else { + r =ch341_control_out (port->serial->dev,CH341_REQ_WRITE_REG , +CH341_REG_FLOW_CTL | + ((u16 )CH341_REG_FLOW_CTL <<8 ), +CH341_FLOW_CTL_NONE ); +if (r <0 ) { + dev_err(&port->dev, + "%s - failed to disable flow control: %d\n",Quoted 8 lines
drivers/usb/serial/ch341.c static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port) · line 517
Quoted 4 lines
drivers/usb/serial/ch341.c +4
-0+ __func__, r); + tty->termios.c_cflag |=CRTSCTS ; + } + }Please rewrite this so that you prepare the value and index parameters based on the termios settings and then do one control request. If it fails you can restore the old setting (if
old_termiosis non-NULL).
Quoted 2 lines
Ok, I haven't seen any other driver restore the old setting on request failure, now I'm questioning if it's actually necessary?
Not all drivers do, but it is the right thing to do as that's the only way we have to report failure to change a setting.
Quoted 2 lines
If it is, I'll change it to the following:
tty->termios.c_cflag = (tty->termios.c_cflag & !CRTSCTS)
You meant bitwise negation here (~) as you used above.
Quoted 1 line
| (old_termios->c_cflag & CRTSCTS);
But remember to only do this if old_termios is non-NULL.
Might be cleaner as two separate statements (&= and |=).
Quoted 5 lines
Quoted 2 lines
And please drop the redundant
__func__from the error message (even if the driver still uses it in some other functions still).Ok. Looking at the code again, the error already gets logged in
ch341_control_outon failure. Maybe this log line shouldn't be added?
Yeah, I guess that's more in line with what the driver is currently doing. We could always amend those printk to include index and value to make it easier to figure out which call is failing if this becomes an issue.
Johan