[Linux-kernel-mentees] [PATCH 03/15] usb: serial: ch314: use usb_control_msg_recv() and usb_control_msg_send()

Johan Hovold johan at kernel.org
Fri Dec 4 09:24:25 UTC 2020


On Wed, Nov 04, 2020 at 12:16:51PM +0530, Himadri Pandya wrote:
> The new usb_control_msg_recv() and usb_control_msg_send() nicely wraps
> usb_control_msg() with proper error check. Hence use the wrappers
> instead of calling usb_control_msg() directly.
> 
> Signed-off-by: Himadri Pandya <himadrispandya at gmail.com>
> ---
>  drivers/usb/serial/ch341.c | 45 ++++++++++++--------------------------
>  1 file changed, 14 insertions(+), 31 deletions(-)
> 
> diff --git a/drivers/usb/serial/ch341.c b/drivers/usb/serial/ch341.c
> index a2e2f56c88cd..58c5d3ce4f75 100644
> --- a/drivers/usb/serial/ch341.c
> +++ b/drivers/usb/serial/ch341.c
> @@ -111,10 +111,10 @@ static int ch341_control_out(struct usb_device *dev, u8 request,
>  	dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x)\n", __func__,
>  		request, value, index);
>  
> -	r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
> -			    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
> -			    value, index, NULL, 0, DEFAULT_TIMEOUT);
> -	if (r < 0)
> +	r = usb_control_msg_send(dev, 0, request, USB_TYPE_VENDOR |
> +				 USB_RECIP_DEVICE | USB_DIR_OUT, value, index,
> +				 NULL, 0, DEFAULT_TIMEOUT, GFP_KERNEL);
> +	if (r)
>  		dev_err(&dev->dev, "failed to send control message: %d\n", r);
>  
>  	return r;
> @@ -129,23 +129,14 @@ static int ch341_control_in(struct usb_device *dev,
>  	dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x,%u)\n", __func__,
>  		request, value, index, bufsize);
>  
> -	r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
> -			    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
> -			    value, index, buf, bufsize, DEFAULT_TIMEOUT);
> -	if (r < (int)bufsize) {
> -		if (r >= 0) {
> -			dev_err(&dev->dev,
> -				"short control message received (%d < %u)\n",
> -				r, bufsize);
> -			r = -EIO;
> -		}
> -
> +	r = usb_control_msg_recv(dev, 0, request, USB_TYPE_VENDOR |
> +				 USB_RECIP_DEVICE | USB_DIR_IN, value, index,
> +				 buf, bufsize, DEFAULT_TIMEOUT, GFP_KERNEL);
> +	if (r)
>  		dev_err(&dev->dev, "failed to receive control message: %d\n",
>  			r);
> -		return r;
> -	}
>  
> -	return 0;
> +	return r;
>  }

The callers of these functions are allocating temporary transfers buffer
for each request so this is a case where the new helpers may be of worth
if you fix up the callers as well (otherwise, you're adding redundant
memdup + memcpy for each call here).

>  
>  #define CH341_CLKRATE		48000000
> @@ -343,22 +334,18 @@ static int ch341_detect_quirks(struct usb_serial_port *port)
>  	struct usb_device *udev = port->serial->dev;
>  	const unsigned int size = 2;
>  	unsigned long quirks = 0;
> -	char *buffer;
> +	u8 buffer[2];
>  	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(udev, usb_rcvctrlpipe(udev, 0), CH341_REQ_READ_REG,
> -			    USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
> -			    CH341_REG_BREAK, 0, buffer, size, DEFAULT_TIMEOUT);
> +	r = usb_control_msg_recv(udev, 0, CH341_REQ_READ_REG, USB_TYPE_VENDOR |
> +				 USB_RECIP_DEVICE | USB_DIR_IN, CH341_REG_BREAK,
> +				 0, &buffer, size, DEFAULT_TIMEOUT, GFP_KERNEL);
>  	if (r == -EPIPE) {
>  		dev_info(&port->dev, "break control not supported, using simulated break\n");
>  		quirks = CH341_QUIRK_LIMITED_PRESCALER | CH341_QUIRK_SIMULATE_BREAK;
> @@ -366,17 +353,13 @@ static int ch341_detect_quirks(struct usb_serial_port *port)
>  		goto out;
>  	}
>  
> -	if (r != size) {
> -		if (r >= 0)
> -			r = -EIO;
> +	if (r) {
>  		dev_err(&port->dev, "failed to read break control: %d\n", r);
>  		goto out;
>  	}
>  
>  	r = 0;
>  out:
> -	kfree(buffer);
> -
>  	if (quirks) {
>  		dev_dbg(&port->dev, "enabling quirk flags: 0x%02lx\n", quirks);
>  		priv->quirks |= quirks;

This is a good example of when to use the helpers. But it seems you
should remove the "r = 0" and "out" label as well, right?

Johan


More information about the Linux-kernel-mentees mailing list