Skip to content

Commit 49bda21

Browse files
Michał Peciojhovold
Michał Pecio
authored andcommitted
USB: pl2303: fix baud-rate divisor calculations
This commit fixes the following issues: 1. The 9th bit of buf was believed to be the LSB of divisor's exponent, but the hardware interprets it as MSB (9th bit) of the mantissa. The exponent is actually one bit shorter and applies to base 4, not 2 as previously believed. 2. Loop iterations doubled the exponent instead of incrementing. 3. The exponent wasn't checked for overflow. 4. The function returned requested rate instead of actual rate. Due to issue #2, the old code deviated from the wrong formula described in #1 and actually yielded correct rates when divisor was lower than 4096 by using exponents of 0, 2 or 4 base-2, interpreted as 0, 1, 2 base-4 with the 9th mantissa bit clear. However, at 93.75 kbaud or less the rate turned out too slow due to #2 or too fast due to #2 and #3. I tested this patch by sending and validating 0x00,0x01,..,0xff to an FTDI dongle at 234, 987, 2401, 9601, 31415, 115199, 250k, 500k, 750k, 1M, 1.5M, 3M+1 baud. All rates passed. I also used pv to check speed at some rates unsupported by FTDI: 45 (the lowest possible), 2M, 4M, 5M and 6M-1. Looked sane. Signed-off-by: Michal Pecio <michal.pecio@gmail.com> Fixes: 399aa9a ("USB: pl2303: use divisors for unsupported baud rates") Cc: stable <stable@vger.kernel.org> # v3.18 [johan: update summary ] Signed-off-by: Johan Hovold <johan@kernel.org>
1 parent f4c126e commit 49bda21

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

drivers/usb/serial/pl2303.c

+26-9
Original file line numberDiff line numberDiff line change
@@ -362,21 +362,38 @@ static speed_t pl2303_encode_baud_rate_direct(unsigned char buf[4],
362362
static speed_t pl2303_encode_baud_rate_divisor(unsigned char buf[4],
363363
speed_t baud)
364364
{
365-
unsigned int tmp;
365+
unsigned int baseline, mantissa, exponent;
366366

367367
/*
368368
* Apparently the formula is:
369-
* baudrate = 12M * 32 / (2^buf[1]) / buf[0]
369+
* baudrate = 12M * 32 / (mantissa * 4^exponent)
370+
* where
371+
* mantissa = buf[8:0]
372+
* exponent = buf[11:9]
370373
*/
371-
tmp = 12000000 * 32 / baud;
374+
baseline = 12000000 * 32;
375+
mantissa = baseline / baud;
376+
if (mantissa == 0)
377+
mantissa = 1; /* Avoid dividing by zero if baud > 32*12M. */
378+
exponent = 0;
379+
while (mantissa >= 512) {
380+
if (exponent < 7) {
381+
mantissa >>= 2; /* divide by 4 */
382+
exponent++;
383+
} else {
384+
/* Exponent is maxed. Trim mantissa and leave. */
385+
mantissa = 511;
386+
break;
387+
}
388+
}
389+
372390
buf[3] = 0x80;
373391
buf[2] = 0;
374-
buf[1] = (tmp >= 256);
375-
while (tmp >= 256) {
376-
tmp >>= 2;
377-
buf[1] <<= 1;
378-
}
379-
buf[0] = tmp;
392+
buf[1] = exponent << 1 | mantissa >> 8;
393+
buf[0] = mantissa & 0xff;
394+
395+
/* Calculate and return the exact baud rate. */
396+
baud = (baseline / mantissa) >> (exponent << 1);
380397

381398
return baud;
382399
}

0 commit comments

Comments
 (0)