From patchwork Thu Jul 5 14:49:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johan Hovold X-Patchwork-Id: 10509553 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 10E95603D7 for ; Thu, 5 Jul 2018 14:51:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F333F29180 for ; Thu, 5 Jul 2018 14:51:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E6B7D29189; Thu, 5 Jul 2018 14:51:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 78BEE29180 for ; Thu, 5 Jul 2018 14:51:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753677AbeGEOv1 (ORCPT ); Thu, 5 Jul 2018 10:51:27 -0400 Received: from mail-lj1-f194.google.com ([209.85.208.194]:37420 "EHLO mail-lj1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753547AbeGEOtl (ORCPT ); Thu, 5 Jul 2018 10:49:41 -0400 Received: by mail-lj1-f194.google.com with SMTP id c11-v6so6878568lja.4 for ; Thu, 05 Jul 2018 07:49:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=DuWNuadmmIjksT8q8pgIO5K1KkSaE76dyLHyPE8t0Fg=; b=TucQfcZCTj31JcaQh5d+NwhoXKMhdyhBEZh7pWuG/umNI460gEDJVpmdQT/GJIdHKl /dZJtivVarncoQcB1dfHX7vFUXetg2jDr1HDPw1bAsGd4qCgcvyANkmmMRHigDS2OCV2 oPDK2OVNCrLiNqzi/2P2jWyJDjKQ0IE4WA+7RgudiPYn7Qh3RS9bttUrxbDIhE+bf+wM kocUzCa5Q7ggyyUAdJJCy+mq2SFNebvS93BxC8GI/qR00R54Ce1XllvxkY4AsmhtiiLN TvIlsw5qmMr6O/FRLxUOnRPzkWgl4a17A78oLpEqIsSXzn1sOul2VtCe5lCafnK2yVDC sbGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=DuWNuadmmIjksT8q8pgIO5K1KkSaE76dyLHyPE8t0Fg=; b=SFZClAfwCFyCkYaNZAqdXfor20odGOQCz1G82xWFxXwZignIUx7UyjOmYrlgzoXhHs N50YheZGujtlAzJN0VPvxKk1OPgpnQdhm0S4+ATMN4tTWQlrYAjNRSAIjoUoUqLIDrU8 Usw5Ecx11OubH32wUbJhqDhmTc1MFy7koFPScogF4tnVAJLBMr4ry2EjSnyCpHV2y6Li R+kuxS/ivAA7bfm5EoUQ1yvnA1nwcHVVLo0n1zOgQNjyIHBOMSrn7i5Rmedwv4DUmqW9 NObHpf+7YHjWhm1mcBosXN380Da+eSL8nyt5/SsapYdIdiDR8FfmwYPuSfHnMYeyczId tmGg== X-Gm-Message-State: APt69E0lPzotQhaPV/UF7J21mUfZx5DtY5ZCMBOxM3dKLM+sBoJKV9NS QfCjlVKb1iV5p1vn6dT6IMM= X-Google-Smtp-Source: AAOMgpf59/bEuoXsK2UFgEamFrnFDCCbM/uVsLh6L2Qg5BSnb9+z8VROJtBDeVZ7LUQTDBPAkkNVxA== X-Received: by 2002:a2e:4401:: with SMTP id r1-v6mr4434354lja.21.1530802180135; Thu, 05 Jul 2018 07:49:40 -0700 (PDT) Received: from xi.terra (c-8bb2e655.07-184-6d6c6d4.bbcust.telenor.se. [85.230.178.139]) by smtp.gmail.com with ESMTPSA id m29-v6sm1419146lfj.45.2018.07.05.07.49.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 05 Jul 2018 07:49:38 -0700 (PDT) Received: from johan by xi.terra with local (Exim 4.91) (envelope-from ) id 1fb5ZZ-000300-FS; Thu, 05 Jul 2018 16:49:29 +0200 From: Johan Hovold To: Johan Hovold Cc: Greg Kroah-Hartman , linux-usb@vger.kernel.org, Karoly Pados Subject: [PATCH 1/2] USB: serial: cp210x: make line-speed quantisation data driven Date: Thu, 5 Jul 2018 16:49:08 +0200 Message-Id: <20180705144909.11483-2-johan@kernel.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180705144909.11483-1-johan@kernel.org> References: <20180705144909.11483-1-johan@kernel.org> Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Older cp210x devices only support a fixed set of line speeds to which a requested speed is mapped. Reimplement this mapping using a table instead of a long if-else construct. Signed-off-by: Johan Hovold --- drivers/usb/serial/cp210x.c | 99 +++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 43 deletions(-) diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c index 626a29d9aa58..e1a4d4b3d3aa 100644 --- a/drivers/usb/serial/cp210x.c +++ b/drivers/usb/serial/cp210x.c @@ -767,48 +767,6 @@ static int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl) return 0; } -/* - * cp210x_quantise_baudrate - * Quantises the baud rate as per AN205 Table 1 - */ -static unsigned int cp210x_quantise_baudrate(unsigned int baud) -{ - if (baud <= 300) - baud = 300; - else if (baud <= 600) baud = 600; - else if (baud <= 1200) baud = 1200; - else if (baud <= 1800) baud = 1800; - else if (baud <= 2400) baud = 2400; - else if (baud <= 4000) baud = 4000; - else if (baud <= 4803) baud = 4800; - else if (baud <= 7207) baud = 7200; - else if (baud <= 9612) baud = 9600; - else if (baud <= 14428) baud = 14400; - else if (baud <= 16062) baud = 16000; - else if (baud <= 19250) baud = 19200; - else if (baud <= 28912) baud = 28800; - else if (baud <= 38601) baud = 38400; - else if (baud <= 51558) baud = 51200; - else if (baud <= 56280) baud = 56000; - else if (baud <= 58053) baud = 57600; - else if (baud <= 64111) baud = 64000; - else if (baud <= 77608) baud = 76800; - else if (baud <= 117028) baud = 115200; - else if (baud <= 129347) baud = 128000; - else if (baud <= 156868) baud = 153600; - else if (baud <= 237832) baud = 230400; - else if (baud <= 254234) baud = 250000; - else if (baud <= 273066) baud = 256000; - else if (baud <= 491520) baud = 460800; - else if (baud <= 567138) baud = 500000; - else if (baud <= 670254) baud = 576000; - else if (baud < 1000000) - baud = 921600; - else if (baud > 2000000) - baud = 2000000; - return baud; -} - static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port) { int result; @@ -1028,6 +986,58 @@ static void cp210x_get_termios_port(struct usb_serial_port *port, *cflagp = cflag; } +struct cp210x_rate { + speed_t rate; + speed_t high; +}; + +static const struct cp210x_rate cp210x_an205_table1[] = { + { 300, 300 }, + { 600, 600 }, + { 1200, 1200 }, + { 1800, 1800 }, + { 2400, 2400 }, + { 4000, 4000 }, + { 4800, 4803 }, + { 7200, 7207 }, + { 9600, 9612 }, + { 14400, 14428 }, + { 16000, 16062 }, + { 19200, 19250 }, + { 28800, 28912 }, + { 38400, 38601 }, + { 51200, 51558 }, + { 56000, 56280 }, + { 57600, 58053 }, + { 64000, 64111 }, + { 76800, 77608 }, + { 115200, 117028 }, + { 128000, 129347 }, + { 153600, 156868 }, + { 230400, 237832 }, + { 250000, 254234 }, + { 256000, 273066 }, + { 460800, 491520 }, + { 500000, 567138 }, + { 576000, 670254 }, + { 921600, UINT_MAX } +}; + +/* + * Quantises the baud rate as per AN205 Table 1 + */ +static speed_t cp210x_get_an205_rate(speed_t baud) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(cp210x_an205_table1); ++i) { + if (baud <= cp210x_an205_table1[i].high) + break; + } + + return cp210x_an205_table1[i].rate; +} + /* * CP2101 supports the following baud rates: * @@ -1066,7 +1076,10 @@ static void cp210x_change_speed(struct tty_struct *tty, * * NOTE: B0 is not implemented. */ - baud = cp210x_quantise_baudrate(baud); + if (baud < 1000000) + baud = cp210x_get_an205_rate(baud); + else if (baud > 2000000) + baud = 2000000; dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud); if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {