From mboxrd@z Thu Jan 1 00:00:00 1970 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=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=unavailable 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 07910C433E6 for ; Mon, 21 Dec 2020 04:54:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CBFCA22CB2 for ; Mon, 21 Dec 2020 04:54:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728402AbgLUEyk (ORCPT ); Sun, 20 Dec 2020 23:54:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728378AbgLUEyj (ORCPT ); Sun, 20 Dec 2020 23:54:39 -0500 Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57821C061282 for ; Sun, 20 Dec 2020 20:53:59 -0800 (PST) Received: by mail-pg1-x536.google.com with SMTP id e2so5631681pgi.5 for ; Sun, 20 Dec 2020 20:53:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Z8nUgXXr4gmphfYkGDSdKR/yaNjlB+snIU3z29dtuO0=; b=CFTaxcaCBM9h1WplhSBTLnJNW21DX5HpaNsFW4iLnBo+LwJpwODan+3Ow4g2gks0cL tpQEsMEU9GYYHHgw61jhi3Ls4HMZDu7w9/BTK69iSYD5RifeAYwFf44Y8pV20riOjDAm SO+u3oN/EHlmu7ssIi43WNdlyH1XJzfUavBcY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Z8nUgXXr4gmphfYkGDSdKR/yaNjlB+snIU3z29dtuO0=; b=H0Xgp98c1vBbTqShcRup7Z39RAw1oYjmBY6rtlNvGaTLvCYxiJglCZdw44jWhTKf5o jqXaAtemKdVM45YCShL575XD2xkMZvtTOVdExfJfhbpe6D9pX9oi2pzPDDnIP21T6DqN KBjp0w3/bQbQJLNnn9l6zz+uyW2djCbaxJtG9m6dWcx10mVN7mTssMrwdAhRkTYHffEQ 4e+NODBxY5UI2aWPp1sUJuHZO2/jTyGJ/hy04gYgvlGQtFjxx78u8NvnRV/fnEnfFIw3 o46njTESLn2olH8AzRbicsqLPja51OvvXiSjkaqBLioLLs/9S3gwBlm3PNuS8raln6WD x2JQ== X-Gm-Message-State: AOAM532xSW2XbqMektJefgGMmi1683a6EGZDHi79NCzZqzAJGzPD26Ka 2zml2kwIeYpUDf0RWkqqjeIbFWmRi+MI2g== X-Google-Smtp-Source: ABdhPJy7UbEJP/+WpCYZ+kkMkInF1kGKkDO7nnOzUun6FtK8iyFJaQs4p+FYFf/P90Gpv1od+4sDtA== X-Received: by 2002:a5e:de08:: with SMTP id e8mr12583296iok.203.1608516760195; Sun, 20 Dec 2020 18:12:40 -0800 (PST) Received: from mail-io1-f48.google.com (mail-io1-f48.google.com. [209.85.166.48]) by smtp.gmail.com with ESMTPSA id f6sm21020761ioh.2.2020.12.20.18.12.39 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 20 Dec 2020 18:12:39 -0800 (PST) Received: by mail-io1-f48.google.com with SMTP id o6so7480856iob.10 for ; Sun, 20 Dec 2020 18:12:39 -0800 (PST) X-Received: by 2002:a5d:8d8b:: with SMTP id b11mr11861196ioj.68.1608516759003; Sun, 20 Dec 2020 18:12:39 -0800 (PST) MIME-Version: 1.0 References: <20201215154439.69062-1-ribalda@chromium.org> <20201215154439.69062-10-ribalda@chromium.org> In-Reply-To: From: Ricardo Ribalda Date: Mon, 21 Dec 2020 03:12:28 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v4 9/9] media: uvcvideo: Implement UVC_QUIRK_PRIVACY_DURING_STREAM To: Laurent Pinchart Cc: Mauro Carvalho Chehab , Linux Media Mailing List , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Laurent On Mon, Dec 21, 2020 at 3:08 AM Laurent Pinchart wrote: > > Hi Ricardo, > > On Mon, Dec 21, 2020 at 02:10:18AM +0100, Ricardo Ribalda wrote: > > On Sun, Dec 20, 2020 at 6:22 PM Laurent Pinchart wrote: > > > On Tue, Dec 15, 2020 at 04:44:39PM +0100, Ricardo Ribalda wrote: > > > > Some devices, can only read the privacy_pin if the device is > > > > streaming. > > > > > > :-( > > > > :"-( > > > > > > This patch implement a quirk for such devices, in order to avoid invalid > > > > reads and/or spurious events. > > > > > > > > Signed-off-by: Ricardo Ribalda > > > > --- > > > > drivers/media/usb/uvc/uvc_driver.c | 97 ++++++++++++++++++++++++++---- > > > > drivers/media/usb/uvc/uvc_queue.c | 3 + > > > > drivers/media/usb/uvc/uvcvideo.h | 6 ++ > > > > 3 files changed, 94 insertions(+), 12 deletions(-) > > > > > > > > diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c > > > > index e49491250e87..61313019e226 100644 > > > > --- a/drivers/media/usb/uvc/uvc_driver.c > > > > +++ b/drivers/media/usb/uvc/uvc_driver.c > > > > @@ -7,6 +7,7 @@ > > > > */ > > > > > > > > #include > > > > +#include > > > > #include > > > > #include > > > > #include > > > > @@ -1471,13 +1472,39 @@ static int uvc_parse_control(struct uvc_device *dev) > > > > return 0; > > > > } > > > > > > > > +static bool uvc_ext_gpio_is_streaming(struct uvc_device *dev) > > > > +{ > > > > + struct uvc_streaming *streaming; > > > > + > > > > + list_for_each_entry(streaming, &dev->streams, list) { > > > > + if (uvc_queue_streaming(&streaming->queue)) > > > > + return true; > > > > + } > > > > + > > > > + return false; > > > > +} > > > > + > > > > +/* Update the cached value and return true if it has changed */ > > > > +static bool uvc_gpio_update_value(struct uvc_entity *unit, u8 *new_val) > > > > +{ > > > > + *new_val = gpiod_get_value(unit->gpio.gpio_privacy); > > > > + > > > > + return atomic_xchg(&unit->gpio.gpio_privacy_value, *new_val) != > > > > + *new_val; > > > > > > That's a weird indentation. Also, as the left hand side modifies > > > *new_val, does C guarantee the order in which the two operands to != are > > > evaluated ? Could the code be written in an easier to read way ? > > > > > > > +} > > > > + > > > > static int uvc_gpio_get_cur(struct uvc_device *dev, struct uvc_entity *entity, > > > > u8 cs, void *data, u16 size) > > > > { > > > > if (cs != UVC_CT_PRIVACY_CONTROL || size < 1) > > > > return -EINVAL; > > > > > > > > - *(uint8_t *)data = gpiod_get_value(entity->gpio.gpio_privacy); > > > > + if ((dev->quirks & UVC_QUIRK_PRIVACY_DURING_STREAM) && > > > > + !uvc_ext_gpio_is_streaming(dev)) > > > > + return -EBUSY; > > > > + > > > > + uvc_gpio_update_value(entity, (uint8_t *)data); > > > > + > > > > return 0; > > > > } > > > > > > > > @@ -1491,26 +1518,69 @@ static int uvc_gpio_get_info(struct uvc_device *dev, struct uvc_entity *entity, > > > > return 0; > > > > } > > > > > > > > -static irqreturn_t uvc_privacy_gpio_irq(int irq, void *data) > > > > +static struct uvc_entity *uvc_find_ext_gpio_unit(struct uvc_device *dev) > > > > { > > > > - struct uvc_device *dev = data; > > > > - struct uvc_video_chain *chain; > > > > struct uvc_entity *unit; > > > > - u8 value; > > > > > > > > - /* GPIO entities are always on the first chain */ > > > > - chain = list_first_entry(&dev->chains, struct uvc_video_chain, list); > > > > list_for_each_entry(unit, &dev->entities, list) { > > > > - if (UVC_ENTITY_TYPE(unit) != UVC_EXT_GPIO_UNIT) > > > > - continue; > > > > - value = gpiod_get_value(unit->gpio.gpio_privacy); > > > > - uvc_ctrl_status_event(NULL, chain, unit->controls, &value); > > > > - return IRQ_HANDLED; > > > > + if (UVC_ENTITY_TYPE(unit) == UVC_EXT_GPIO_UNIT) > > > > + return unit; > > > > } > > > > > > > > + return unit; > > > > +} > > > > + > > > > +void uvc_privacy_gpio_event(struct uvc_device *dev) > > > > +{ > > > > + struct uvc_entity *unit; > > > > + struct uvc_video_chain *chain; > > > > + u8 new_value; > > > > + > > > > + unit = uvc_find_ext_gpio_unit(dev); > > > > + if (WARN_ONCE(!unit, "Unable to find entity ext_gpio_unit")) > > > > + return; > > > > + > > > > + if (!uvc_gpio_update_value(unit, &new_value)) > > > > + return; > > > > > > If VIDIOC_G_CTRL() is called before the IRQ is processed, this > > > uvc_gpio_update_value() call will return false, and no event will be > > > generated. I don't think that's right, and even should be generated > > > every time the control changes. > > > > > I was almost sure that get_cur had also the events wired.... but no. > > > > > > + > > > > + /* GPIO entities are always on the first chain */ > > > > + chain = list_first_entry(&dev->chains, struct uvc_video_chain, list); > > > > + uvc_ctrl_status_event(NULL, chain, unit->controls, &new_value); > > > > +} > > > > + > > > > +static irqreturn_t uvc_privacy_gpio_irq(int irq, void *data) > > > > +{ > > > > + struct uvc_device *dev = data; > > > > + > > > > + /* Ignore privacy events during streamoff */ > > > > + if (dev->quirks & UVC_QUIRK_PRIVACY_DURING_STREAM) > > > > + if (!uvc_ext_gpio_is_streaming(dev)) > > > > + return IRQ_HANDLED; > > > > > > if (dev->quirks & UVC_QUIRK_PRIVACY_DURING_STREAM) { > > > if (!uvc_ext_gpio_is_streaming(dev)) > > > return IRQ_HANDLED; > > > } > > > > > > There's a potential race condition with VIDIOC_STREAMON and > > > VIDIOC_STREAMOFF. Could you explain what the device does exactly when > > > not streaming ? As the GPIO isn't tied to the UVC controller, how comes > > > the streaming state influences it ? Any hope the firmware could be fixed > > > instead ? > > > > In the affected devices, the privacy_pin is an output of the camera > > module instead of an independent pin. > > So the privacy switch is an input of a camera module, which then output > its state on a GPIO instead of exposing it through the UVC privacy > control ? Amazing design ! Dont kill the messenger... > > > When the camera is not streaming, the camera does not drive the pin, > > so the system reads whatever pull-up, pull-down the pin is configured > > by default in the firmware/hardware. > > > > Unfortunately the only way to fix it would be to change the module, or > > the firmware of the module, and neigher things are not feasable :(. So > > we have to use a quirk for them. > > Future models have this fixed. > > Changing the hardware, obviously. Changing the firmware of the module... > Someone needs to be scolded along the development chain, can you take > care of that at least ? ;-) Will do my best ;) > > Thinking more about this, do you have a use case for knowing the state > of the privacy switch when not streaming ? Notify user space that the switch has toggled, to provide visual feedback of the action. > > > Regarding the race condition... The use of the atomic_t was to avoid > > that race, but what I did not realise was that by default get_cur does > > not send an event... So I am working on a new series, which also > > includes a fix for the async_control wq. > > > > Thanks for your review!!! > > > > > > + > > > > + uvc_privacy_gpio_event(dev); > > > > + > > > > return IRQ_HANDLED; > > > > } > > > > > > > > +static const struct dmi_system_id privacy_valid_during_streamon[] = { > > > > + { > > > > + .ident = "HP Elite c1030 Chromebook", > > > > + .matches = { > > > > + DMI_MATCH(DMI_SYS_VENDOR, "HP"), > > > > + DMI_MATCH(DMI_PRODUCT_NAME, "Jinlon"), > > > > + }, > > > > + }, > > > > + { > > > > + .ident = "HP Pro c640 Chromebook", > > > > + .matches = { > > > > + DMI_MATCH(DMI_SYS_VENDOR, "HP"), > > > > + DMI_MATCH(DMI_PRODUCT_NAME, "Dratini"), > > > > + }, > > > > + }, > > > > + { } /* terminate list */ > > > > +}; > > > > + > > > > + > > > > static int uvc_parse_gpio(struct uvc_device *dev) > > > > { > > > > struct uvc_entity *unit; > > > > @@ -1545,6 +1615,9 @@ static int uvc_parse_gpio(struct uvc_device *dev) > > > > if (irq == -EPROBE_DEFER) > > > > return -EPROBE_DEFER; > > > > > > > > + if (dmi_check_system(privacy_valid_during_streamon)) > > > > + dev->quirks |= UVC_QUIRK_PRIVACY_DURING_STREAM; > > > > + > > > > if (irq < 0) > > > > return 0; > > > > > > > > diff --git a/drivers/media/usb/uvc/uvc_queue.c b/drivers/media/usb/uvc/uvc_queue.c > > > > index cd60c6c1749e..e800d491303f 100644 > > > > --- a/drivers/media/usb/uvc/uvc_queue.c > > > > +++ b/drivers/media/usb/uvc/uvc_queue.c > > > > @@ -337,9 +337,12 @@ int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf, > > > > int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type) > > > > { > > > > int ret; > > > > + struct uvc_streaming *stream = uvc_queue_to_stream(queue); > > > > > > > > mutex_lock(&queue->mutex); > > > > ret = vb2_streamon(&queue->queue, type); > > > > + if (stream->dev->quirks & UVC_QUIRK_PRIVACY_DURING_STREAM) > > > > + uvc_privacy_gpio_event(stream->dev); > > > > mutex_unlock(&queue->mutex); > > > > > > > > return ret; > > > > diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h > > > > index 2b5ba4b02d3a..2a95b3ed3ea8 100644 > > > > --- a/drivers/media/usb/uvc/uvcvideo.h > > > > +++ b/drivers/media/usb/uvc/uvcvideo.h > > > > @@ -6,6 +6,7 @@ > > > > #error "The uvcvideo.h header is deprecated, use linux/uvcvideo.h instead." > > > > #endif /* __KERNEL__ */ > > > > > > > > +#include > > > > #include > > > > #include > > > > #include > > > > @@ -209,6 +210,7 @@ > > > > #define UVC_QUIRK_RESTORE_CTRLS_ON_INIT 0x00000400 > > > > #define UVC_QUIRK_FORCE_Y8 0x00000800 > > > > #define UVC_QUIRK_FORCE_BPP 0x00001000 > > > > +#define UVC_QUIRK_PRIVACY_DURING_STREAM 0x00002000 > > > > > > > > /* Format flags */ > > > > #define UVC_FMT_FLAG_COMPRESSED 0x00000001 > > > > @@ -359,6 +361,7 @@ struct uvc_entity { > > > > u8 bControlSize; > > > > u8 *bmControls; > > > > struct gpio_desc *gpio_privacy; > > > > + atomic_t gpio_privacy_value; > > > > } gpio; > > > > }; > > > > > > > > @@ -815,6 +818,9 @@ extern const struct v4l2_file_operations uvc_fops; > > > > int uvc_mc_register_entities(struct uvc_video_chain *chain); > > > > void uvc_mc_cleanup_entity(struct uvc_entity *entity); > > > > > > > > +/* Privacy gpio */ > > > > +void uvc_privacy_gpio_event(struct uvc_device *dev); > > > > + > > > > /* Video */ > > > > int uvc_video_init(struct uvc_streaming *stream); > > > > int uvc_video_suspend(struct uvc_streaming *stream); > > -- > Regards, > > Laurent Pinchart -- Ricardo Ribalda