aboutsummaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorJohn Baldwin <jhb@FreeBSD.org>2020-04-13 20:43:57 +0000
committerJohn Baldwin <jhb@FreeBSD.org>2020-04-13 20:43:57 +0000
commitb47c1eda0cacfacb85c3bed6d0c60bf241d04cf8 (patch)
tree76e151ac8e737566e408c7fa5e5f00f6a5f447ea /sys
parent3fcdcab087b4490276f3a917f99a0c9a9ff2ef7e (diff)
downloadsrc-b47c1eda0cacfacb85c3bed6d0c60bf241d04cf8.tar.gz
src-b47c1eda0cacfacb85c3bed6d0c60bf241d04cf8.zip
Correct baud rate error calculation.
Shifting right by 1 is not the same as dividing by 2 for signed values. In particular, dividing a signed value by 2 gives the integer ceiling of the (e.g. -5 / 2 == -2) whereas shifting right by 1 always gives the floor (-5 >> 1 == -3). An embedded board with a 25 Mhz base clock results in an error of -30.5% when used with a baud rate of 115200. Using division, this truncates to -30% and is permitted. Using the shift, this fails and is rejected causing TIOCSETA requests to fail with EINVAL and breaking getty(8). Using division gives the same error range for both over and under baud rates and also makes the code match the behavior documented in the existing comment about supporting boards with 25 Mhz clocks. Reported by: imp MFC after: 2 weeks Sponsored by: DARPA Differential Revision: https://reviews.freebsd.org/D24367
Notes
Notes: svn path=/head/; revision=359899
Diffstat (limited to 'sys')
-rw-r--r--sys/dev/uart/uart_dev_ns8250.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/sys/dev/uart/uart_dev_ns8250.c b/sys/dev/uart/uart_dev_ns8250.c
index 4460a2d029b3..e02473aa8c36 100644
--- a/sys/dev/uart/uart_dev_ns8250.c
+++ b/sys/dev/uart/uart_dev_ns8250.c
@@ -139,7 +139,7 @@ ns8250_divisor(int rclk, int baudrate)
actual_baud = rclk / (divisor << 4);
/* 10 times error in percent: */
- error = ((actual_baud - baudrate) * 2000 / baudrate + 1) >> 1;
+ error = ((actual_baud - baudrate) * 2000 / baudrate + 1) / 2;
/* enforce maximum error tolerance: */
if (error < -UART_DEV_TOLERANCE_PCT || error > UART_DEV_TOLERANCE_PCT)