All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michael G. Katzmann" <michaelk@IEEE.org>
To: linux-serial@vger.kernel.org
Subject: Re: non-standard baud rates with Prolific 2303 USB-serial
Date: Sun, 21 Feb 2021 21:15:06 -0500	[thread overview]
Message-ID: <dc3458f1-830b-284b-3464-20124dc3900a@IEEE.org> (raw)
In-Reply-To: <3aee5708-7961-f464-8c5f-6685d96920d6@IEEE.org>


So on more investigation and many hours of trial and error I have determined the correct formua for the non-standard rates with this Prolific 2303.
There are four bytes used to send the baud rate. When not using the 'standard' rates, the Linus driver sets byte 3 to 0x80 and bytes 1 and 0 hold the divisor of the clock (12MHz * 32).
This divisor is encoded in a special way. The 'exponent' (log 4) and the mantissa. This is calculated by dividing the divisor by 4 until it is less than 512 (the number of times you can
do this is the exponent). e then goes in bits 1,2 and 3 of byte 1 with the MSBit of the mantissa in bit 0 of byte 1. Byte 0 holds the lower 8 bits of the mantissa.

This does not work on the device I have USB PID/VID/REV is 0673 / 2303/3.00. Instead...the calculation uses 1024 instead of 512 in the method above and the exponent is in byte 1 bits 7,6&5 (the top 2 bits of the mantissa are in bits 0 and 1).
I tested this on all baud rates that stty will allow that are not 'standard' and it works. (50, 110 .... 3000000).

The changed routine is below. I presume the original driver worked so perhaps this device is a new variant with the same USB VID/PID.

Michael

static speed_t pl2303_encode_baud_rate_divisor(unsigned char buf[4],
                                speed_t baud)
{
    unsigned int baseline, mantissa, exponent;
    /*
     * Apparently the formula is:
     *   baudrate = 12M * 32 / (mantissa * 4^exponent)
     * where
     *   mantissa = buf[8:0]
     *   exponent = buf[11:9]
         *
         * Michael Katzmann: At least versions of the chip VID 0x067b PID 0x2303 bcdDevice 3.00
         *                   uses mantissa = buf[9:0]
     *                        exponent = buf[15:13]
     */
    baseline = 12000000 * 32;
    mantissa = baseline / baud;
    if (mantissa == 0)
        mantissa = 1;    /* Avoid dividing by zero if baud > 32*12M. */
    exponent = 0;
#undef ORIGINAL
#ifdef ORIGINAL
    while (mantissa >= 512) {
        if (exponent < 7) {
            mantissa >>= 2;    /* divide by 4 */
            exponent++;
        } else {
            /* Exponent is maxed. Trim mantissa and leave. */
            mantissa = 511;
            break;
        }
    }
    buf[3] = 0x80;
    buf[2] = 0;
    buf[1] = exponent << 1 | mantissa >> 8;
    buf[0] = mantissa & 0xff;
#else
    while (mantissa >= 1024) {
        if (exponent < 7) {
            mantissa >>= 2;    /* divide by 4 */
            exponent++;
        } else {
            // This is an logical modification of the original code
                        // but I do not know if this an actual limitation
            /* Exponent is maxed. Trim mantissa and leave. */
            mantissa = 1023 ;
            break;
        }
    }
    buf[3] = 0x80;
    buf[2] = 0;
    buf[1] = exponent << 5 | (mantissa >> 8);
    buf[0] = mantissa & 0xff;
#endif
    /* Calculate and return the exact baud rate. */
    baud = (baseline / mantissa) >> (exponent << 1);

    return baud;
}


On 2/21/21 10:37 AM, Michael G. Katzmann wrote:
> The Linux driver does not seem to produce sensible baud rated for other than the 'supported' rates.
>
> stty to 110 bd results in ~95,000 bd (that's not a typo but 95 thousand). Other rates like 200 also produce odd speeds although not in a logical manner.
>
> The data sheets don't describe the formula used for deriving the four bytes used to set the speed.
>
> I tried adding 110 to the supported rates but this did not produce the correct baud rate so I presume the Windows driver is using a formula different than the one in the Linux driver for 'non standard' baud rates.
>

  reply	other threads:[~2021-02-22  2:15 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-21 15:37 non-standard baud rates with Prolific 2303 USB-serial Michael G. Katzmann
2021-02-22  2:15 ` Michael G. Katzmann [this message]
2021-02-22  8:52   ` 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
2021-03-15 10:07                                                 ` Charles Yeh
2021-03-15 10:24                                                   ` Johan Hovold
2021-02-22  3:57 ` Grant Edwards

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=dc3458f1-830b-284b-3464-20124dc3900a@IEEE.org \
    --to=michaelk@ieee.org \
    --cc=linux-serial@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.