linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johan Hovold <johan@kernel.org>
To: "Michael G. Katzmann" <michaelk@IEEE.org>
Cc: "Charles Yeh" <charlesyeh522@gmail.com>,
	"Yeh.Charles [葉榮鑫]" <charles-yeh@prolific.com.tw>,
	linux-serial@vger.kernel.org, linux-usb@vger.kernel.org,
	"Joe Abbott" <jabbott@rollanet.org>
Subject: Re: non-standard baud rates with Prolific 2303 USB-serial
Date: Mon, 15 Mar 2021 10:07:33 +0100	[thread overview]
Message-ID: <YE8j1XdfQNDGLzZR@hovoldconsulting.com> (raw)
In-Reply-To: <a475110e-2f44-eeca-3cd2-dd946e5abfe7@IEEE.org>

On Fri, Mar 12, 2021 at 08:28:04PM -0500, Michael G. Katzmann wrote:
> On 3/12/21 8:44 AM, Johan Hovold wrote:
> 
> 
> Let me try that patch in the right format 8-)

That's better, but we also need a commit message and and Signed-off-by
line from you. Take a look at the general format of the patches I CCed
you on and there's some more information under Documentation/:

	Documentation/process/submitting-patches.rst

The easiest is probably to use git-format-patch and git-send-email to
prevent the patch from being corrupted (e.g. the tabs below have been
replaced by spaces).
 
> --- a/drivers/usb/serial/pl2303.c    2021-03-12 09:30:22.963992109 -0500
> +++ b/drivers/usb/serial/pl2303.c    2021-03-12 20:00:20.003526891 -0500
> @@ -188,6 +188,7 @@
>      unsigned long quirks;
>      unsigned int no_autoxonxoff:1;
>      unsigned int no_divisors:1;
> +    unsigned int alt_divisors:1;
>  };
>  
>  struct pl2303_serial_private {
> @@ -217,10 +218,12 @@
>      [TYPE_TA] = {
>          .name            = "TA",
>          .max_baud_rate        = 6000000,
> +        .alt_divisors        = true,
>      },
>      [TYPE_TB] = {
>          .name            = "TB",
>          .max_baud_rate        = 12000000,
> +        .alt_divisors        = true,

Are you sure that the TB uses the alternate encoding?

Charles, could you help us out here? Which other device types use the
alternate encoding (e.g. HX(A) or TB) if any?

>      },
>      [TYPE_HXD] = {
>          .name            = "HXD",
> @@ -618,6 +621,46 @@
>      return baud;
>  }
>  
> +static speed_t pl2303_encode_baud_rate_divisor_alt(unsigned char buf[4],
> +                                                                speed_t baud)
> +{
> +        unsigned int baseline, mantissa, exponent;
> +
> +        /*
> +         * Apparently, for the TA version the formula is:
> +         *   baudrate = 12M * 32 / (mantissa * 2^exponent)
> +         * where
> +         *   mantissa = buf[10:0]
> +         *   exponent = buf[15:13 16]
> +         */
> +        baseline = 12000000 * 32;
> +        mantissa = baseline / baud;
> +        if (mantissa == 0)
> +                mantissa = 1;   /* Avoid dividing by zero if baud > 32*12M. */
> +        exponent = 0;
> +        while (mantissa >= 2048) {
> +                if (exponent < 15) {
> +                        mantissa >>= 1; /* divide by 2 */
> +                        exponent++;
> +                } else {
> +                        /* Exponent is maxed. Trim mantissa and leave. */
> +                        mantissa = 2047;
> +                        break;
> +                }
> +        }
> +
> +        buf[3] = 0x80;
> +        buf[2] = exponent & 0x01; // LS bit of exponent
> +        buf[1] = (exponent & ~0x01) << 4 | mantissa >> 8; // 3 bits of the exponent and MS 3 bits of the mantissa
> +        buf[0] = mantissa & 0xff; // LS 8 bits of the mantissa

Please avoid c99-style comments, but perhaps the comment at the start of
function is sufficient here.

Looks great otherwise.

> +
> +        /* Calculate and return the exact baud rate. */
> +        baud = (baseline / mantissa) >> exponent;
> +
> +        return baud;
> +}
> +
> +
>  static void pl2303_encode_baud_rate(struct tty_struct *tty,
>                      struct usb_serial_port *port,
>                      u8 buf[4])
> @@ -645,6 +688,8 @@
>  
>      if (baud == baud_sup)
>          baud = pl2303_encode_baud_rate_direct(buf, baud);
> +    else if (spriv->type->alt_divisors)
> +                baud = pl2303_encode_baud_rate_divisor_alt(buf, baud);
>      else
>          baud = pl2303_encode_baud_rate_divisor(buf, baud);

This works too if you don't want to pass in the port to
pl2303_encode_baud_rate_divisor() and hide the device-type differences
there (e.g. to make the logic in pl2303_encode_baud_rate() easier to
follow).

Johan

  reply	other threads:[~2021-03-15  9:07 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <3aee5708-7961-f464-8c5f-6685d96920d6@IEEE.org>
     [not found] ` <dc3458f1-830b-284b-3464-20124dc3900a@IEEE.org>
2021-02-22  8:52   ` non-standard baud rates with Prolific 2303 USB-serial Johan Hovold
2021-02-22 12:39     ` Michael G. Katzmann
2021-02-22 13:18       ` Johan Hovold
     [not found]         ` <fb1489c2-b972-619b-b7ce-4ae8e1d2cc0f@IEEE.org>
2021-02-22 15:34           ` Johan Hovold
2021-02-22 15:42             ` Michael G. Katzmann
2021-02-22 15:50               ` Johan Hovold
2021-02-22 16:37                 ` Michael G. Katzmann
2021-02-22 16:48                   ` Johan Hovold
2021-02-24 23:08                     ` Joe Abbott
2021-02-25  8:44                       ` Johan Hovold
     [not found]                   ` <43da22ced8e14442bbc8babea77e4ed7@MailHC2.prolific.com.tw>
2021-02-23 10:18                     ` Johan Hovold
2021-02-23 13:25                     ` Michael G. Katzmann
2021-02-23 14:58                 ` Michael G. Katzmann
2021-02-23 15:43                   ` Johan Hovold
2021-02-23 15:57                     ` Michael G. Katzmann
2021-02-23 16:14                       ` Johan Hovold
2021-02-23 16:30                         ` Michael G. Katzmann
2021-02-23 16:52                           ` Johan Hovold
2021-02-23 19:15                             ` Michael G. Katzmann
2021-02-24 17:04                               ` Johan Hovold
2021-02-24 18:13                                 ` Michael G. Katzmann
2021-02-25  8:42                                   ` Johan Hovold
2021-04-08 15:35                                     ` Johan Hovold
2021-02-24  7:34                             ` Charles Yeh
2021-02-24 17:00                               ` Johan Hovold
2021-02-24 17:12                                 ` Michael G. Katzmann
2021-03-05  9:32                                 ` Charles Yeh
2021-03-05  9:36                                   ` Johan Hovold
2021-03-06 20:18                                     ` Michael G. Katzmann
2021-03-07  4:15                                     ` Michael G. Katzmann
2021-03-11 16:08                                       ` Johan Hovold
2021-03-12 13:17                                         ` Michael G. Katzmann
2021-03-12 13:44                                           ` Johan Hovold
2021-03-12 20:29                                             ` Michael G. Katzmann
2021-03-13  1:28                                             ` Michael G. Katzmann
2021-03-15  9:07                                               ` Johan Hovold [this message]
2021-03-15 10:07                                                 ` Charles Yeh
2021-03-15 10:24                                                   ` Johan Hovold

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YE8j1XdfQNDGLzZR@hovoldconsulting.com \
    --to=johan@kernel.org \
    --cc=charles-yeh@prolific.com.tw \
    --cc=charlesyeh522@gmail.com \
    --cc=jabbott@rollanet.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=michaelk@IEEE.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).