Re: [PATCH v9 20/22] uvcvideo: improve error handling in uvc_query_ctrl()

From: Laurent Pinchart
Date: Thu Jun 10 2021 - 13:14:26 EST


Hi Ricardo and Hans,

Thank you for the patch.

On Fri, Mar 26, 2021 at 10:58:38AM +0100, Ricardo Ribalda wrote:
> From: Hans Verkuil <hverkuil-cisco@xxxxxxxxx>
>
> - If __uvc_query_ctrl() failed with a non-EPIPE error, then
> report that with dev_err. If an error code is obtained, then
> report that with dev_dbg.
>
> - For error 2 (Wrong state) return -EACCES instead of -EILSEQ.
> EACCES is a much more appropriate error code. EILSEQ will return
> "Invalid or incomplete multibyte or wide character." in strerror(),
> which is a *very* confusing message.

I would have split the commit in two.

> Signed-off-by: Hans Verkuil <hans.verkuil@xxxxxxxxx>
> ---
>
> I have changed a bit the patch from the original version.
>
> drivers/media/usb/uvc/uvc_video.c | 38 +++++++++++++++++--------------
> 1 file changed, 21 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c
> index b63c073ec30e..1c3a94d91724 100644
> --- a/drivers/media/usb/uvc/uvc_video.c
> +++ b/drivers/media/usb/uvc/uvc_video.c
> @@ -76,35 +76,31 @@ int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
> if (likely(ret == size))
> return 0;
>
> - dev_dbg(&dev->udev->dev,
> - "Failed to query (%s) UVC control %u on unit %u: %d (exp. %u).\n",
> - uvc_query_name(query), cs, unit, ret, size);
> -
> - if (ret != -EPIPE)
> - return ret;
> + if (ret < 0 && ret != -EPIPE)
> + goto err;
>
> + // reuse data[0] for request the error code.

C-style comments please.

s/for request/to request/

> tmp = *(u8 *)data;
> -
> ret = __uvc_query_ctrl(dev, UVC_GET_CUR, 0, intfnum,
> UVC_VC_REQUEST_ERROR_CODE_CONTROL, data, 1,
> UVC_CTRL_CONTROL_TIMEOUT);
> -

No need to drop the blank lines.

> error = *(u8 *)data;
> *(u8 *)data = tmp;
>
> - if (ret != 1)
> - return ret < 0 ? ret : -EPIPE;
> + if (ret != 1) {
> + ret = ret < 0 ? ret : -EPIPE;
> + goto err;
> + }
>
> - uvc_dbg(dev, CONTROL, "Control error %u\n", error);
> + dev_dbg(&dev->udev->dev,

Why not uvc_dbg ?

> + "Failed to query (%s) UVC control %u on unit %u: got error %u.\n",
> + uvc_query_name(query), cs, unit, error);
>
> switch (error) {
> - case 0:
> - /* Cannot happen - we received a STALL */
> - return -EPIPE;
> case 1: /* Not ready */
> return -EBUSY;
> case 2: /* Wrong state */
> - return -EILSEQ;
> + return -EACCES;
> case 3: /* Power */
> return -EREMOTE;
> case 4: /* Out of range */
> @@ -120,10 +116,18 @@ int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
> case 8: /* Invalid value within range */
> return -EINVAL;
> default: /* reserved or unknown */
> - break;
> + dev_err(&dev->udev->dev,
> + "Failed to query (%s) UVC control %u on unit %u: got error %u.\n",
> + uvc_query_name(query), cs, unit, error);

when debugging is enabled, this will print the error message a second
time, it's not very nice.

> + return -EPIPE;
> }
>
> - return -EPIPE;
> +err:
> + dev_err(&dev->udev->dev,
> + "Failed to query (%s) UVC control %u on unit %u: %d (exp. %u).\n",
> + uvc_query_name(query), cs, unit, ret, size);
> +
> + return ret;
> }
>
> static void uvc_fixup_video_ctrl(struct uvc_streaming *stream,

--
Regards,

Laurent Pinchart