From patchwork Thu Jul 24 12:02:56 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Coquelin X-Patchwork-Id: 4617241 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 582459F375 for ; Thu, 24 Jul 2014 12:06:15 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 84DB320179 for ; Thu, 24 Jul 2014 12:06:14 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7F17C2008F for ; Thu, 24 Jul 2014 12:06:13 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XAHka-0000E7-Vn; Thu, 24 Jul 2014 12:03:56 +0000 Received: from mx07-00178001.pphosted.com ([62.209.51.94]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1XAHkS-0008U5-Qk for linux-arm-kernel@lists.infradead.org; Thu, 24 Jul 2014 12:03:49 +0000 Received: from pps.filterd (m0046670.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.14.5/8.14.5) with SMTP id s6OBxfJQ015206; Thu, 24 Jul 2014 14:03:22 +0200 Received: from beta.dmz-us.st.com (beta.dmz-us.st.com [167.4.1.35]) by mx07-00178001.pphosted.com with ESMTP id 1naxk9s67t-1 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Thu, 24 Jul 2014 14:03:22 +0200 Received: from zeta.dmz-us.st.com (ns4.st.com [167.4.16.71]) by beta.dmz-us.st.com (STMicroelectronics) with ESMTP id 9378029; Thu, 24 Jul 2014 12:03:18 +0000 (GMT) Received: from mail7.sgp.st.com (unknown [164.129.223.81]) by zeta.dmz-us.st.com (STMicroelectronics) with ESMTP id 9511B65; Thu, 24 Jul 2014 12:03:16 +0000 (GMT) Received: from lmenx29l.lme.st.com ([10.201.23.80]) by mail7.sgp.st.com (MOS 4.3.3-GA) with ESMTP id BZR07648 (AUTH lme00137); Thu, 24 Jul 2014 14:03:11 +0200 From: Maxime COQUELIN To: Greg Kroah-Hartman , Jiri Slaby , Srinivas Kandagatla , Patrice Chotard , linux-arm-kernel@lists.infradead.org, linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] serial: st-asc: Fix overflow in baudrate calculation Date: Thu, 24 Jul 2014 14:02:56 +0200 Message-Id: <1406203376-18203-3-git-send-email-maxime.coquelin@st.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1406203376-18203-1-git-send-email-maxime.coquelin@st.com> References: <1406203376-18203-1-git-send-email-maxime.coquelin@st.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.12.52, 1.0.14, 0.0.0000 definitions=2014-07-24_03:2014-07-24, 2014-07-24, 1970-01-01 signatures=0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140724_050349_184903_5D287276 X-CRM114-Status: GOOD ( 10.37 ) X-Spam-Score: -0.7 (/) Cc: kernel@stlinux.com, maxime.coquelin@st.com X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP In the current calculation, if the required baud rate is above 262143, we get an overflow. This patch uses a 64bits variable to do the maths. Also, we remove the '+1' to avoid a divide by zero if the input clock rate is something unexpected. Indeed, if the input clock rate is zero, it is preferable to be notified, since the UART won't work anyway. Signed-off-by: Maxime Coquelin --- drivers/tty/serial/st-asc.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/tty/serial/st-asc.c b/drivers/tty/serial/st-asc.c index 2369bc0..7ac9902 100644 --- a/drivers/tty/serial/st-asc.c +++ b/drivers/tty/serial/st-asc.c @@ -533,12 +533,12 @@ static void asc_set_termios(struct uart_port *port, struct ktermios *termios, * ASCBaudRate = ------------------------ * inputclock * - * However to keep the maths inside 32bits we divide top and - * bottom by 64. The +1 is to avoid a divide by zero if the - * input clock rate is something unexpected. + * To keep maths inside 64bits, we divide inputclock by 16. */ - u32 counter = (baud * 16384) / ((port->uartclk / 64) + 1); - asc_out(port, ASC_BAUDRATE, counter); + u64 dividend = (u64)baud * (1 << 16); + + do_div(dividend, port->uartclk / 16); + asc_out(port, ASC_BAUDRATE, dividend); ctrl_val |= ASC_CTL_BAUDMODE; }