Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D079EC28EBD for ; Mon, 10 Jun 2019 02:32:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 933D22067C for ; Mon, 10 Jun 2019 02:32:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="W9Jvwg1N" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730165AbfFJCcY (ORCPT ); Sun, 9 Jun 2019 22:32:24 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:35874 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730055AbfFJCcY (ORCPT ); Sun, 9 Jun 2019 22:32:24 -0400 Received: by mail-pf1-f196.google.com with SMTP id u22so4360150pfm.3 for ; Sun, 09 Jun 2019 19:32:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:references:in-reply-to:subject:date:message-id :mime-version:content-transfer-encoding:thread-index :content-language; bh=QUweCUwjXg7O7PTP/jbrlEgxePjg74Y2vdFGqMqgLhU=; b=W9Jvwg1Nb28DCNkVy2cxkJDfmRvsEeqL3s3h7F8TbA7QGLqs/2jj2hRp6VaOmLfH/R MZfSllllewUNgG6jUu4xCjOb/f5mIxPKqYOU56rHaRRKa2aL2FQ5sTvqPsvMZFoEKzuq TZdDugYMZzGmIDF0zok25RtKAp+vV425uCZhvnwCKO63ajVCHghvaFMmqhc6Y/xinRRr vqx1+K1PjgFefKWmOJoDayS77hc/ToILYjngnKxHRgbM0r51amM5vz3P0vy3ecOXcMOj JCb89Kbo2enmirJ0lzqvS3eziBWOkaYXkc0pgjvcpTFAuzoInIHfSNLIL5Rp4R81w32E mRaQ== 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:references:in-reply-to:subject :date:message-id:mime-version:content-transfer-encoding:thread-index :content-language; bh=QUweCUwjXg7O7PTP/jbrlEgxePjg74Y2vdFGqMqgLhU=; b=OWJ3uKjiJwtRTElv3IvbZuzQ0Al8YCCbONLyhJlMs4j9eWxPit+Ok0YuIM4gXPFMJn LpkEygXLLsSGiWwOsSB3TnBaJjwyi+IkBBAtmUGqHfJKv1yYil59l6phittIyY3htj2E m1z/3TB6ifhOG7d+uehQPexzpbRsYh7zQo56JcJoGV5UefEJgMdBnBdil9rq5taUGTiP qzpj8MK+hV4qbUz0jXAmSsGGDP+3h4ueIUbIZ1bAFUgryRhLskSb4aphAivlNFZ7kgh3 mfFnuYjd6QQ1zv+ecCczkVcuj+ENyGUlyNNHOZiTh7YE+U+ATlMczWnPsYXSlW3sMBK2 1Y7Q== X-Gm-Message-State: APjAAAUsg69TAc3IbUf3nnafYCX2oVKEO+f77tPkCFG+/WKOureaST1O K8YllhVaMDAnznBPY5FXZd0= X-Google-Smtp-Source: APXvYqy1aHvT6OO2C+e58cvmb+V6MQ6EfBZATiaT8Vb6/7mjQGRP3zqLDlj0rJxtpDlb77HOufKHGQ== X-Received: by 2002:a63:af44:: with SMTP id s4mr13665272pgo.411.1560133943685; Sun, 09 Jun 2019 19:32:23 -0700 (PDT) Received: from PC201407091922 (28.215.252.27.dyn.cust.vf.net.nz. [27.252.215.28]) by smtp.gmail.com with ESMTPSA id a3sm2590802pfo.49.2019.06.09.19.32.20 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 09 Jun 2019 19:32:23 -0700 (PDT) From: "Jonathan Olds" To: "'Johan Hovold'" Cc: , , , , "'Jonti Olds'" References: <000901d50e93$7cb31470$76193d50$@co.nz> <20190603072337.GB3668@localhost> In-Reply-To: Subject: RE: linux/drivers/usb/serial/ch341.c calculates some baud rates wrong Date: Mon, 10 Jun 2019 14:32:16 +1200 Message-ID: <000001d51f34$bad6afd0$30840f70$@co.nz> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: AdUZ3UfHN2bTxA3STX64Y2wikQAqMwDqkJjwAGrGwXA= Content-Language: en-nz Sender: linux-usb-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Hi Johan, Thanks for the info. I followed https://nickdesaulniers.github.io/blog/2017/05/16/submitting-your-first-patc h-to-the-linux-kernel-and-responding-to-feedback/ and made a proposal patch ("[PATCH] USB: serial: ch341: fix wrong baud rate setting calculation" https://lore.kernel.org/linux-usb/20190608051309.4689-1-jontio@i4free.co.nz/ ). I've measured the actual baud rates for a lot of given baud rates and I think I have deduced the formulas for calculating the "a" value. "a" is a uint16 and split up in two, a LSB and a MSB. The current driver only uses LSB from the set {0,1,2,3}. There is another valid LSB of 7 that the current driver doesn't use. The formula for LSB from the set {0,1,2,3} is... Actual baud rate == 2^(3*LSB-10)*12000000/(256-MSB), if LSB is in {0,1,2,3} and 0=248 the clock divider is turned back on and LSB is effectively 3 again. So we can also use the following formula... Actual baud rate == 12000000/(256-MSB), if LSB == 7 and 0 Hi, > > Sorry for sending this email to so many but I've never contributed to > the Linux kernel before and I'm not sure how this is done or who to talk to. You can use scripts/get_maintainer.pl to figure out which maintainers and lists to CC. For USB serial that would be me and we always keep the linux-usb list on CC. It never hurts to CC the original authors as well, just like you did here. Note that you need to send mails to list as plain text, not html. > With a CH340 chip the ch341 driver is loaded. My chip id is 0x31 > according to the driver debug info. > > In "static int ch341_set_baudrate_lcr(struct usb_device *dev, struct > ch341_private *priv, u8 lcr)" > > > Currently we have... ... > a = (factor & 0xff00) | divisor; > This "a" thing is called index when used in this function... > > "ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x1312, a);" > > This "a" is wrong for some baud rates. (I've only tried 921600 so far) > > For example at 921600 baud "a" is calculated as "0xF303". This creates > a baud rate of about 857000 baud. This can be seen in the following figures... > I saw this is another driver... ... > So this person used cases for various baud rates that have incorrect > rates using the method that the current Linux kernel driver implements. > > I then use the 0xF307 that this calculates for 921600 baud and put an > if statement into the current Linux driver like so... ... > This produced the correct baud rate as can be seen in the following > figure... > I found this bug out of necessity. I made a board ( > http://jontio.zapto.org/jpic ) and programmed initially in Windows > without any baud rate issues. Then moving to Linux the board wasn't > able to connect as the baud rate where incorrect. So it is a real bug > and one that needs addressing. Fortunately the solution looks like > using the code with the cases in it. I have no idea where the > programming documentation is for this chip so this index number for > setting the baud rate is just a magic number to me at the moment. > > What is the process to get this fix incorporated into new updates to > Linux kernel? If you want I can create a list of what standard baud > rates are incorrect and by how much with an oscilloscope; I think it > will only be for the very high baud rates and may only affect 921600 and above. Such a list would be very useful to have. And I agree that if you can't infer the algorithm used from your experimental data we may need to start special casing. Please do gather the data, and send it to the list and propose a patch for fixing the broken rates. Thanks, Johan