Message ID | 20220712115306.26471-5-kabel@kernel.org |
---|---|
State | New |
Headers | show |
Series | ftdi_sio driver improvements | expand |
On Tue, Jul 12, 2022 at 01:53:03PM +0200, Marek Behún wrote: > From: Pali Rohár <pali@kernel.org> > > If setting new baud rate fails, all other drivers leave the device at > previous baud rate, only ftdi_sio resets to 9600 Baud. > Change this behavior to that of other drivers so that /dev/ttyUSB* > devices behave in the same way. These statements are not true. Several USB serial driver set 9600 baud on errors for historical reasons. Yet others clamp. It's inconsistent at best. > Signed-off-by: Pali Rohár <pali@kernel.org> > Tested-by: Marek Behún <kabel@kernel.org> > Signed-off-by: Marek Behún <kabel@kernel.org> > --- > Greg pointed out that this make break some people's workflow, that they > may depend on this behavior. > > This is of course possible, although IMO improbable: it would be weird > to depend on the fall back to 9600 Baud on error, instead of expecting > that the baud rate didn't change at all (like in other /dev/ttyUSB* > drivers). > > Nonetheless if someone complains that they workflow got broken, we will > need to revert this. > static u32 get_ftdi_divisor(struct tty_struct *tty, > - struct usb_serial_port *port) > + struct usb_serial_port *port, speed_t old_baud) > { > struct ftdi_private *priv = usb_get_serial_port_data(port); > struct device *dev = &port->dev; > @@ -1322,6 +1322,8 @@ static u32 get_ftdi_divisor(struct tty_struct *tty, > __func__, priv->custom_divisor, baud); > } > > + if (!baud) > + baud = old_baud; > if (!baud) > baud = 9600; This looks like it breaks B0 handling. Either way, an unrelated change. > switch (priv->chip_type) { > @@ -1330,8 +1332,12 @@ static u32 get_ftdi_divisor(struct tty_struct *tty, > if (div_value == -1) { > dev_dbg(dev, "%s - Baudrate (%d) requested is not supported\n", > __func__, baud); > - baud = 9600; > + baud = old_baud ? old_baud : 9600; > div_value = ftdi_sio_baud_to_divisor(baud); > + if (div_value == -1) { > + baud = 9600; > + div_value = ftdi_sio_baud_to_divisor(baud); > + } > div_okay = 0; > } > break; > @@ -1340,8 +1346,8 @@ static u32 get_ftdi_divisor(struct tty_struct *tty, > div_value = ftdi_232am_baud_to_divisor(baud); > } else { > dev_dbg(dev, "%s - Baud rate too high!\n", __func__); > - baud = 9600; > - div_value = ftdi_232am_baud_to_divisor(9600); > + baud = (old_baud >= 183 && old_baud <= 3000000) ? old_baud : 9600; And please avoid using the ternary operator which tend to just hurt readability. Looks like this needs to be refactored in some way. > @@ -1525,7 +1536,7 @@ static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss) > if (priv->flags & ASYNC_SPD_MASK) > dev_warn_ratelimited(&port->dev, "use of SPD flags is deprecated\n"); > > - change_speed(tty, port); > + change_speed(tty, port, 0); Zero has a special meaning (B0). > } > mutex_unlock(&priv->cfg_lock); > return 0; > @@ -2774,9 +2785,12 @@ static void ftdi_set_termios(struct tty_struct *tty, > /* Drop RTS and DTR */ > clear_mctrl(port, TIOCM_DTR | TIOCM_RTS); > } else { > + speed_t old_baud = > + old_termios ? tty_termios_baud_rate(old_termios) : 0; Just use 9600 if you don't have an old termios. The termios rate should be valid in that case. Johan
On Sunday 24 July 2022 14:20:58 Johan Hovold wrote: > On Tue, Jul 12, 2022 at 01:53:03PM +0200, Marek Behún wrote: > > From: Pali Rohár <pali@kernel.org> > > > > If setting new baud rate fails, all other drivers leave the device at > > previous baud rate, only ftdi_sio resets to 9600 Baud. > > > Change this behavior to that of other drivers so that /dev/ttyUSB* > > devices behave in the same way. > > These statements are not true. Several USB serial driver set 9600 baud > on errors for historical reasons. Yet others clamp. It's inconsistent at > best. > > > Signed-off-by: Pali Rohár <pali@kernel.org> > > Tested-by: Marek Behún <kabel@kernel.org> > > Signed-off-by: Marek Behún <kabel@kernel.org> > > --- > > Greg pointed out that this make break some people's workflow, that they > > may depend on this behavior. > > > > This is of course possible, although IMO improbable: it would be weird > > to depend on the fall back to 9600 Baud on error, instead of expecting > > that the baud rate didn't change at all (like in other /dev/ttyUSB* > > drivers). > > > > Nonetheless if someone complains that they workflow got broken, we will > > need to revert this. > > > static u32 get_ftdi_divisor(struct tty_struct *tty, > > - struct usb_serial_port *port) > > + struct usb_serial_port *port, speed_t old_baud) > > { > > struct ftdi_private *priv = usb_get_serial_port_data(port); > > struct device *dev = &port->dev; > > @@ -1322,6 +1322,8 @@ static u32 get_ftdi_divisor(struct tty_struct *tty, > > __func__, priv->custom_divisor, baud); > > } > > > > + if (!baud) > > + baud = old_baud; > > if (!baud) > > baud = 9600; > > This looks like it breaks B0 handling. Either way, an unrelated change. Unreleased to this change. > > switch (priv->chip_type) { > > @@ -1330,8 +1332,12 @@ static u32 get_ftdi_divisor(struct tty_struct *tty, > > if (div_value == -1) { > > dev_dbg(dev, "%s - Baudrate (%d) requested is not supported\n", > > __func__, baud); > > - baud = 9600; > > + baud = old_baud ? old_baud : 9600; > > div_value = ftdi_sio_baud_to_divisor(baud); > > + if (div_value == -1) { > > + baud = 9600; > > + div_value = ftdi_sio_baud_to_divisor(baud); > > + } > > div_okay = 0; > > } > > break; > > @@ -1340,8 +1346,8 @@ static u32 get_ftdi_divisor(struct tty_struct *tty, > > div_value = ftdi_232am_baud_to_divisor(baud); > > } else { > > dev_dbg(dev, "%s - Baud rate too high!\n", __func__); > > - baud = 9600; > > - div_value = ftdi_232am_baud_to_divisor(9600); > > + baud = (old_baud >= 183 && old_baud <= 3000000) ? old_baud : 9600; > > And please avoid using the ternary operator which tend to just hurt > readability. > > Looks like this needs to be refactored in some way. > > > @@ -1525,7 +1536,7 @@ static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss) > > if (priv->flags & ASYNC_SPD_MASK) > > dev_warn_ratelimited(&port->dev, "use of SPD flags is deprecated\n"); > > > > - change_speed(tty, port); > > + change_speed(tty, port, 0); > > Zero has a special meaning (B0). > > > } > > mutex_unlock(&priv->cfg_lock); > > return 0; > > @@ -2774,9 +2785,12 @@ static void ftdi_set_termios(struct tty_struct *tty, > > /* Drop RTS and DTR */ > > clear_mctrl(port, TIOCM_DTR | TIOCM_RTS); > > } else { > > + speed_t old_baud = > > + old_termios ? tty_termios_baud_rate(old_termios) : 0; > > Just use 9600 if you don't have an old termios. The termios rate should > be valid in that case. Ok
On Thu, Aug 18, 2022 at 04:12:54PM +0200, Pali Rohár wrote: > On Sunday 24 July 2022 14:20:58 Johan Hovold wrote: > > > @@ -2774,9 +2785,12 @@ static void ftdi_set_termios(struct tty_struct *tty, > > > /* Drop RTS and DTR */ > > > clear_mctrl(port, TIOCM_DTR | TIOCM_RTS); > > > } else { > > > + speed_t old_baud = > > > + old_termios ? tty_termios_baud_rate(old_termios) : 0; > > > > Just use 9600 if you don't have an old termios. The termios rate should > > be valid in that case. > > Ok Please trim your replies. Johan
diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c index 5b109714612f..cdbba1a9edd9 100644 --- a/drivers/usb/serial/ftdi_sio.c +++ b/drivers/usb/serial/ftdi_sio.c @@ -1299,7 +1299,7 @@ static int update_mctrl(struct usb_serial_port *port, unsigned int set, static u32 get_ftdi_divisor(struct tty_struct *tty, - struct usb_serial_port *port) + struct usb_serial_port *port, speed_t old_baud) { struct ftdi_private *priv = usb_get_serial_port_data(port); struct device *dev = &port->dev; @@ -1322,6 +1322,8 @@ static u32 get_ftdi_divisor(struct tty_struct *tty, __func__, priv->custom_divisor, baud); } + if (!baud) + baud = old_baud; if (!baud) baud = 9600; switch (priv->chip_type) { @@ -1330,8 +1332,12 @@ static u32 get_ftdi_divisor(struct tty_struct *tty, if (div_value == -1) { dev_dbg(dev, "%s - Baudrate (%d) requested is not supported\n", __func__, baud); - baud = 9600; + baud = old_baud ? old_baud : 9600; div_value = ftdi_sio_baud_to_divisor(baud); + if (div_value == -1) { + baud = 9600; + div_value = ftdi_sio_baud_to_divisor(baud); + } div_okay = 0; } break; @@ -1340,8 +1346,8 @@ static u32 get_ftdi_divisor(struct tty_struct *tty, div_value = ftdi_232am_baud_to_divisor(baud); } else { dev_dbg(dev, "%s - Baud rate too high!\n", __func__); - baud = 9600; - div_value = ftdi_232am_baud_to_divisor(9600); + baud = (old_baud >= 183 && old_baud <= 3000000) ? old_baud : 9600; + div_value = ftdi_232am_baud_to_divisor(baud); div_okay = 0; } break; @@ -1363,9 +1369,9 @@ static u32 get_ftdi_divisor(struct tty_struct *tty, div_value = ftdi_232bm_baud_to_divisor(baud); } else { dev_dbg(dev, "%s - Baud rate too high!\n", __func__); - div_value = ftdi_232bm_baud_to_divisor(9600); + baud = (old_baud >= 183 && old_baud <= 3000000) ? old_baud : 9600; + div_value = ftdi_232bm_baud_to_divisor(baud); div_okay = 0; - baud = 9600; } break; case FT2232H: /* FT2232H chip */ @@ -1377,9 +1383,14 @@ static u32 get_ftdi_divisor(struct tty_struct *tty, div_value = ftdi_232bm_baud_to_divisor(baud); } else { dev_dbg(dev, "%s - Baud rate too high!\n", __func__); - div_value = ftdi_232bm_baud_to_divisor(9600); + if (old_baud >= 183 && old_baud < 1200) { + baud = old_baud; + div_value = ftdi_232bm_baud_to_divisor(baud); + } else { + baud = (old_baud >= 1200 && old_baud <= 12000000) ? old_baud : 9600; + div_value = ftdi_2232h_baud_to_divisor(baud); + } div_okay = 0; - baud = 9600; } break; } /* priv->chip_type */ @@ -1394,7 +1405,7 @@ static u32 get_ftdi_divisor(struct tty_struct *tty, return div_value; } -static int change_speed(struct tty_struct *tty, struct usb_serial_port *port) +static int change_speed(struct tty_struct *tty, struct usb_serial_port *port, speed_t old_baud) { struct ftdi_private *priv = usb_get_serial_port_data(port); u16 value; @@ -1402,7 +1413,7 @@ static int change_speed(struct tty_struct *tty, struct usb_serial_port *port) u32 index_value; int rv; - index_value = get_ftdi_divisor(tty, port); + index_value = get_ftdi_divisor(tty, port, old_baud); value = (u16)index_value; index = (u16)(index_value >> 16); if (priv->chip_type == FT2232C || priv->chip_type == FT2232H || @@ -1525,7 +1536,7 @@ static int set_serial_info(struct tty_struct *tty, struct serial_struct *ss) if (priv->flags & ASYNC_SPD_MASK) dev_warn_ratelimited(&port->dev, "use of SPD flags is deprecated\n"); - change_speed(tty, port); + change_speed(tty, port, 0); } mutex_unlock(&priv->cfg_lock); return 0; @@ -2774,9 +2785,12 @@ static void ftdi_set_termios(struct tty_struct *tty, /* Drop RTS and DTR */ clear_mctrl(port, TIOCM_DTR | TIOCM_RTS); } else { + speed_t old_baud = + old_termios ? tty_termios_baud_rate(old_termios) : 0; + /* set the baudrate determined before */ mutex_lock(&priv->cfg_lock); - if (change_speed(tty, port)) + if (change_speed(tty, port, old_baud)) dev_err(ddev, "%s urb failed to set baudrate\n", __func__); mutex_unlock(&priv->cfg_lock); /* Ensure RTS and DTR are raised when baudrate changed from 0 */