linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [4/5] USB: serial: cp210x: generalise CP2102N line-speed handling
@ 2018-07-18 12:37 Johan Hovold
  0 siblings, 0 replies; 5+ messages in thread
From: Johan Hovold @ 2018-07-18 12:37 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Johan Hovold, Greg Kroah-Hartman, Karoly Pados, USB

On Wed, Jul 18, 2018 at 03:34:34PM +0300, Andy Shevchenko wrote:
> On Wed, Jul 18, 2018 at 3:25 PM, Johan Hovold <johan@kernel.org> wrote:
> > The CP2102N equations for determining the actual baud rate can be used
> > also for other device types, so let's factor it out.
> >
> > Note that this removes the now unused cp210x_is_cp2102n() helper.
> 
> 
> > +static speed_t cp210x_get_actual_rate(struct usb_serial *serial, speed_t baud)
> > +{
> > +       struct cp210x_serial_private *priv = usb_get_serial_data(serial);
> > +       unsigned int prescale = 1;
> > +       unsigned int div;
> > +
> > +       baud = clamp(baud, 300u, priv->max_speed);
> > +
> > +       if (baud <= 365)
> > +               prescale = 4;
> > +
> > +       div = DIV_ROUND_CLOSEST(48000000, 2 * prescale * baud);
> > +       baud = 48000000 / (2 * prescale * div);
> > +
> > +       return baud;
> > +}
> 
> > -       if (cp210x_is_cp2102n(serial)) {
> > -               int clk_div;
> > -               int prescaler;
> > -
> > -               baud = clamp(baud, 300u, priv->max_speed);
> > -               prescaler = (baud <= 365) ? 4 : 1;
> > -               clk_div = DIV_ROUND_CLOSEST(48000000, 2 * prescaler * baud);
> > -               baud = 48000000 / (2 * prescaler * clk_div);
> > -       }
> 
> Looks like ping-pong type of changes.
> I think the factoring of this particular piece of code can be done in
> patch 3 in somewhat similar way.

Indeed, and it was done like this on purpose this time to save time (and
I did not want to rewrite Karoly's patch beyond recognition).

Johan
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 5+ messages in thread
* [4/5] USB: serial: cp210x: generalise CP2102N line-speed handling
@ 2018-07-18 13:26 Greg Kroah-Hartman
  0 siblings, 0 replies; 5+ messages in thread
From: Greg Kroah-Hartman @ 2018-07-18 13:26 UTC (permalink / raw)
  To: Johan Hovold; +Cc: Karoly Pados, linux-usb

On Wed, Jul 18, 2018 at 02:25:00PM +0200, Johan Hovold wrote:
> The CP2102N equations for determining the actual baud rate can be used
> also for other device types, so let's factor it out.
> 
> Note that this removes the now unused cp210x_is_cp2102n() helper.
> 
> Signed-off-by: Johan Hovold <johan@kernel.org>
> ---
>  drivers/usb/serial/cp210x.c | 50 ++++++++++++++++++++-----------------
>  1 file changed, 27 insertions(+), 23 deletions(-)

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 5+ messages in thread
* [4/5] USB: serial: cp210x: generalise CP2102N line-speed handling
@ 2018-07-18 12:40 Andy Shevchenko
  0 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2018-07-18 12:40 UTC (permalink / raw)
  To: Johan Hovold; +Cc: Greg Kroah-Hartman, Karoly Pados, USB

On Wed, Jul 18, 2018 at 3:37 PM, Johan Hovold <johan@kernel.org> wrote:
> On Wed, Jul 18, 2018 at 03:34:34PM +0300, Andy Shevchenko wrote:
>> On Wed, Jul 18, 2018 at 3:25 PM, Johan Hovold <johan@kernel.org> wrote:

>> Looks like ping-pong type of changes.
>> I think the factoring of this particular piece of code can be done in
>> patch 3 in somewhat similar way.
>
> Indeed, and it was done like this on purpose this time to save time (and
> I did not want to rewrite Karoly's patch beyond recognition).

Ah, it explains.

^ permalink raw reply	[flat|nested] 5+ messages in thread
* [4/5] USB: serial: cp210x: generalise CP2102N line-speed handling
@ 2018-07-18 12:34 Andy Shevchenko
  0 siblings, 0 replies; 5+ messages in thread
From: Andy Shevchenko @ 2018-07-18 12:34 UTC (permalink / raw)
  To: Johan Hovold; +Cc: Greg Kroah-Hartman, Karoly Pados, USB

On Wed, Jul 18, 2018 at 3:25 PM, Johan Hovold <johan@kernel.org> wrote:
> The CP2102N equations for determining the actual baud rate can be used
> also for other device types, so let's factor it out.
>
> Note that this removes the now unused cp210x_is_cp2102n() helper.


> +static speed_t cp210x_get_actual_rate(struct usb_serial *serial, speed_t baud)
> +{
> +       struct cp210x_serial_private *priv = usb_get_serial_data(serial);
> +       unsigned int prescale = 1;
> +       unsigned int div;
> +
> +       baud = clamp(baud, 300u, priv->max_speed);
> +
> +       if (baud <= 365)
> +               prescale = 4;
> +
> +       div = DIV_ROUND_CLOSEST(48000000, 2 * prescale * baud);
> +       baud = 48000000 / (2 * prescale * div);
> +
> +       return baud;
> +}

> -       if (cp210x_is_cp2102n(serial)) {
> -               int clk_div;
> -               int prescaler;
> -
> -               baud = clamp(baud, 300u, priv->max_speed);
> -               prescaler = (baud <= 365) ? 4 : 1;
> -               clk_div = DIV_ROUND_CLOSEST(48000000, 2 * prescaler * baud);
> -               baud = 48000000 / (2 * prescaler * clk_div);
> -       }

Looks like ping-pong type of changes.
I think the factoring of this particular piece of code can be done in
patch 3 in somewhat similar way.

^ permalink raw reply	[flat|nested] 5+ messages in thread
* [4/5] USB: serial: cp210x: generalise CP2102N line-speed handling
@ 2018-07-18 12:25 Johan Hovold
  0 siblings, 0 replies; 5+ messages in thread
From: Johan Hovold @ 2018-07-18 12:25 UTC (permalink / raw)
  To: Johan Hovold; +Cc: Greg Kroah-Hartman, Karoly Pados, linux-usb

The CP2102N equations for determining the actual baud rate can be used
also for other device types, so let's factor it out.

Note that this removes the now unused cp210x_is_cp2102n() helper.

Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/usb/serial/cp210x.c | 50 ++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 23 deletions(-)

diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index 3778685c7b99..957406aac9bd 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -230,6 +230,7 @@ struct cp210x_serial_private {
 #endif
 	u8			partnum;
 	speed_t			max_speed;
+	bool			use_actual_rate;
 };
 
 struct cp210x_port_private {
@@ -457,15 +458,6 @@ struct cp210x_gpio_write {
 	u8	state;
 } __packed;
 
-static bool cp210x_is_cp2102n(struct usb_serial *serial)
-{
-	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
-
-	return	(priv->partnum == CP210X_PARTNUM_CP2102N_QFN28) ||
-		(priv->partnum == CP210X_PARTNUM_CP2102N_QFN24) ||
-		(priv->partnum == CP210X_PARTNUM_CP2102N_QFN20);
-}
-
 /*
  * Helper to get interface number when we only have struct usb_serial.
  */
@@ -1036,6 +1028,23 @@ static speed_t cp210x_get_an205_rate(speed_t baud)
 	return cp210x_an205_table1[i].rate;
 }
 
+static speed_t cp210x_get_actual_rate(struct usb_serial *serial, speed_t baud)
+{
+	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
+	unsigned int prescale = 1;
+	unsigned int div;
+
+	baud = clamp(baud, 300u, priv->max_speed);
+
+	if (baud <= 365)
+		prescale = 4;
+
+	div = DIV_ROUND_CLOSEST(48000000, 2 * prescale * baud);
+	baud = 48000000 / (2 * prescale * div);
+
+	return baud;
+}
+
 /*
  * CP2101 supports the following baud rates:
  *
@@ -1072,25 +1081,17 @@ static void cp210x_change_speed(struct tty_struct *tty,
 	baud = tty->termios.c_ospeed;
 
 	/*
-	 * This maps the requested rate to the actual rate on cp2102n, a valid
-	 * rate on cp2102 or cp2103, or to an arbitrary rate in
-	 * [1M, max_speed].
+	 * This maps the requested rate to the actual rate, a valid rate on
+	 * cp2102 or cp2103, or to an arbitrary rate in [1M, max_speed].
 	 *
 	 * NOTE: B0 is not implemented.
 	 */
-	if (cp210x_is_cp2102n(serial)) {
-		int clk_div;
-		int prescaler;
-
-		baud = clamp(baud, 300u, priv->max_speed);
-		prescaler = (baud <= 365) ? 4 : 1;
-		clk_div = DIV_ROUND_CLOSEST(48000000, 2 * prescaler * baud);
-		baud = 48000000 / (2 * prescaler * clk_div);
-	} else if (baud < 1000000) {
+	if (priv->use_actual_rate)
+		baud = cp210x_get_actual_rate(serial, baud);
+	else if (baud < 1000000)
 		baud = cp210x_get_an205_rate(baud);
-	} else if (baud > priv->max_speed) {
+	else if (baud > priv->max_speed)
 		baud = priv->max_speed;
-	}
 
 	dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
 	if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
@@ -1524,6 +1525,7 @@ static int cp210x_port_remove(struct usb_serial_port *port)
 static void cp210x_init_max_speed(struct usb_serial *serial)
 {
 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
+	bool use_actual_rate = false;
 	speed_t max;
 
 	switch (priv->partnum) {
@@ -1547,6 +1549,7 @@ static void cp210x_init_max_speed(struct usb_serial *serial)
 	case CP210X_PARTNUM_CP2102N_QFN28:
 	case CP210X_PARTNUM_CP2102N_QFN24:
 	case CP210X_PARTNUM_CP2102N_QFN20:
+		use_actual_rate = true;
 		max = 3000000;
 		break;
 	default:
@@ -1555,6 +1558,7 @@ static void cp210x_init_max_speed(struct usb_serial *serial)
 	}
 
 	priv->max_speed = max;
+	priv->use_actual_rate = use_actual_rate;
 }
 
 static int cp210x_attach(struct usb_serial *serial)

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-07-18 13:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-07-18 12:37 [4/5] USB: serial: cp210x: generalise CP2102N line-speed handling Johan Hovold
  -- strict thread matches above, loose matches on Subject: below --
2018-07-18 13:26 Greg Kroah-Hartman
2018-07-18 12:40 Andy Shevchenko
2018-07-18 12:34 Andy Shevchenko
2018-07-18 12:25 Johan Hovold

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).