Date: Tue, 8 Sep 2020 22:19:07 +0000 (UTC) From: John Baldwin <jhb@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r365476 - in stable: 11/sys/dev/uart 12/sys/dev/uart Message-ID: <202009082219.088MJ7C6021933@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: jhb Date: Tue Sep 8 22:19:06 2020 New Revision: 365476 URL: https://svnweb.freebsd.org/changeset/base/365476 Log: MFC 359899: 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. Modified: stable/11/sys/dev/uart/uart_dev_ns8250.c Directory Properties: stable/11/ (props changed) Changes in other areas also in this revision: Modified: stable/12/sys/dev/uart/uart_dev_ns8250.c Directory Properties: stable/12/ (props changed) Modified: stable/11/sys/dev/uart/uart_dev_ns8250.c ============================================================================== --- stable/11/sys/dev/uart/uart_dev_ns8250.c Tue Sep 8 21:39:34 2020 (r365475) +++ stable/11/sys/dev/uart/uart_dev_ns8250.c Tue Sep 8 22:19:06 2020 (r365476) @@ -136,7 +136,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)
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202009082219.088MJ7C6021933>