linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johan Hovold <johan@kernel.org>
To: Michael Hanselmann <public@hansmi.ch>
Cc: linux-usb@vger.kernel.org, Johan Hovold <johan@kernel.org>,
	Michael Dreher <michael@5dot1.de>,
	Jonathan Olds <jontio@i4free.co.nz>
Subject: Re: [PATCH v2 2/6] USB: serial: ch341: Add basis for quirk detection
Date: Thu, 14 May 2020 16:09:26 +0200	[thread overview]
Message-ID: <20200514140926.GD25962@localhost> (raw)
In-Reply-To: <1e1ae0da6082bb528a44ef323d4e1d3733d38858.1585697281.git.public@hansmi.ch>

On Tue, Mar 31, 2020 at 11:37:18PM +0000, Michael Hanselmann wrote:
> A subset of CH341 devices does not support all features, namely the
> prescaler is limited to a reduced precision and there is no support for
> sending a RS232 break condition. This patch adds a detection function
> which will be extended to return quirk flags as they're implemented.
> 
> The author's affected device has an imprint of "340" on the
> turquoise-colored plug, but not all such devices appear to be affected.
> 
> Signed-off-by: Michael Hanselmann <public@hansmi.ch>

I've applied this one now after making some minor changes.

> ---
>  drivers/usb/serial/ch341.c | 49 ++++++++++++++++++++++++++++++++++++++
>  1 file changed, 49 insertions(+)
> 
> diff --git a/drivers/usb/serial/ch341.c b/drivers/usb/serial/ch341.c
> index 85e7125d0194..9c839f67c3d4 100644
> --- a/drivers/usb/serial/ch341.c
> +++ b/drivers/usb/serial/ch341.c
> @@ -87,6 +87,7 @@ struct ch341_private {
>  	u8 mcr;
>  	u8 msr;
>  	u8 lcr;
> +	u8 quirks;

I used unsigned long to avoid any false sharing with the locked fields
even if that's likely not an issue here.

>  };
>  
>  static void ch341_set_termios(struct tty_struct *tty,
> @@ -330,6 +331,45 @@ out:	kfree(buffer);
>  	return r;
>  }
>  
> +static int ch341_detect_quirks(struct usb_device *dev)
> +{
> +	const unsigned int size = 2;
> +	char *buffer;
> +	int r;
> +
> +	buffer = kmalloc(size, GFP_KERNEL);
> +	if (!buffer)
> +		return -ENOMEM;
> +
> +	/*
> +	 * A subset of CH34x devices does not support all features. The
> +	 * prescaler is limited and there is no support for sending a RS232
> +	 * break condition. A read failure when trying to set up the latter is
> +	 * used to detect these devices.
> +	 */
> +	r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), CH341_REQ_READ_REG,
> +			    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
> +			    CH341_REG_BREAK, 0, buffer, size, DEFAULT_TIMEOUT);
> +	if (r == -EPIPE) {
> +		dev_dbg(&dev->dev, "%s - reading break condition register"
> +			" failed (%d)\n", __func__, r);

Log message strings should not be broken (even if that violates the 80
column rule) so that they are easier to grep for.

No need need to include the errno, here and reworded the message.

I also used passed in the port structure and used the port->dev for
logging (I know, the driver isn't consistent here).

> +		r = 0;
> +		goto out;
> +	}
> +
> +	if (r < 0) {

We generally need to check for short transfers as well when reading.

> +		dev_err(&dev->dev, "%s - USB control read error (%d)\n",
> +			__func__, r);
> +		goto out;
> +	}
> +
> +	r = 0;
> +
> +out:
> +	kfree(buffer);
> +	return r;
> +}
> +
>  static int ch341_port_probe(struct usb_serial_port *port)
>  {
>  	struct ch341_private *priv;
> @@ -351,6 +391,15 @@ static int ch341_port_probe(struct usb_serial_port *port)
>  	if (r < 0)
>  		goto error;
>  
> +	r = ch341_detect_quirks(port->serial->dev);
> +	if (r < 0)
> +		goto error;
> +	if (r != 0) {
> +		dev_dbg(&port->serial->dev->dev,
> +			"%s - enabling quirks flags %08x\n", __func__, r);
> +		priv->quirks |= r;

And finally I moved this bit into the helper itself.

> +	}
> +
>  	usb_set_serial_port_data(port, priv);
>  	return 0;

Johan

  reply	other threads:[~2020-05-14 14:09 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-31 23:37 [PATCH v2 0/6] USB: serial: ch341: Add support for limited chips (was: Add support for HL340 devices) Michael Hanselmann
2020-03-31 23:37 ` [PATCH v2 1/6] USB: serial: ch341: Reduce special cases in clock calculation Michael Hanselmann
2020-03-31 23:37 ` [PATCH v2 2/6] USB: serial: ch341: Add basis for quirk detection Michael Hanselmann
2020-05-14 14:09   ` Johan Hovold [this message]
2020-03-31 23:37 ` [PATCH v2 3/6] USB: serial: ch341: Limit prescaler on quirky chips Michael Hanselmann
2020-05-14 14:17   ` Johan Hovold
2020-05-27 13:16     ` Johan Hovold
2020-05-27 15:41       ` Michael Hanselmann
2020-05-29  7:15         ` Johan Hovold
2020-03-31 23:37 ` [PATCH v2 4/6] USB: serial: ch341: Name prescaler, divisor registers Michael Hanselmann
2020-05-14 14:24   ` Johan Hovold
2020-05-27 20:59     ` Michael Hanselmann
2020-06-29  9:51       ` Johan Hovold
2020-03-31 23:37 ` [PATCH v2 5/6] USB: serial: ch341: Compute minimum baud rate Michael Hanselmann
2020-05-27 22:19   ` Michael Hanselmann
2020-06-30  9:57     ` Johan Hovold
2020-03-31 23:37 ` [PATCH v2 6/6] USB: serial: ch341: Simulate break condition if not supported Michael Hanselmann
2020-05-14 14:47   ` Johan Hovold
2020-05-27 22:21     ` Michael Hanselmann
2020-06-30 11:39       ` Johan Hovold
2020-07-04 18:25         ` Michael Hanselmann
2020-07-06  9:31           ` Johan Hovold
2020-05-14 14:02 ` [PATCH v2 0/6] USB: serial: ch341: Add support for limited chips (was: Add support for HL340 devices) Johan Hovold

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200514140926.GD25962@localhost \
    --to=johan@kernel.org \
    --cc=jontio@i4free.co.nz \
    --cc=linux-usb@vger.kernel.org \
    --cc=michael@5dot1.de \
    --cc=public@hansmi.ch \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).