| From 4b3c6ab7d1bb6e03d0bfad9bfd9cd2358b4566e5 Mon Sep 17 00:00:00 2001 |
| From: Ricardo Ribalda <ribalda@chromium.org> |
| Date: Wed, 23 Dec 2020 14:35:28 +0100 |
| Subject: [PATCH] FROMLIST: media: uvcvideo: Implement |
| UVC_QUIRK_PRIVACY_DURING_STREAM |
| |
| 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 <ribalda@chromium.org> |
| (am from https://lore.kernel.org/patchwork/patch/1356318/) |
| (also found at https://lore.kernel.org/r/20201223133528.55014-13-ribalda@chromium.org) |
| |
| BUG=b:167994459, b:173778998 |
| TEST=v4l2-ctl -C privacy |
| |
| Change-Id: Icaaed64f39a02893a58918aea1ea10b2689b7577 |
| Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/kernel/+/2804463 |
| Reviewed-by: Sean Paul <seanpaul@chromium.org> |
| Reviewed-by: Tomasz Figa <tfiga@chromium.org> |
| Tested-by: Ricardo Ribalda <ribalda@chromium.org> |
| Commit-Queue: Ricardo Ribalda <ribalda@chromium.org> |
| --- |
| drivers/media/usb/uvc/uvc_driver.c | 55 +++++++++++++++++++++++++++--- |
| drivers/media/usb/uvc/uvc_video.c | 27 +++++++++++++++ |
| drivers/media/usb/uvc/uvcvideo.h | 7 ++++ |
| 3 files changed, 84 insertions(+), 5 deletions(-) |
| |
| diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c |
| index 23d839f74ca5a62e0c0ca65e26ecafbef43cb4c2..f6c4b5bedb4f4b352276c55310832169943191e7 100644 |
| --- a/drivers/media/usb/uvc/uvc_driver.c |
| +++ b/drivers/media/usb/uvc/uvc_driver.c |
| @@ -8,6 +8,7 @@ |
| |
| #include <linux/atomic.h> |
| #include <linux/bits.h> |
| +#include <linux/dmi.h> |
| #include <linux/gpio/consumer.h> |
| #include <linux/kernel.h> |
| #include <linux/list.h> |
| @@ -1193,7 +1194,7 @@ static int uvc_parse_control(struct uvc_device *dev) |
| * Privacy GPIO |
| */ |
| |
| -static void uvc_gpio_event(struct uvc_device *dev) |
| +void uvc_gpio_event(struct uvc_device *dev) |
| { |
| struct uvc_entity *unit = dev->gpio_unit; |
| struct uvc_video_chain *chain; |
| @@ -1202,19 +1203,29 @@ static void uvc_gpio_event(struct uvc_device *dev) |
| if (!unit) |
| return; |
| |
| + mutex_lock(&unit->gpio.event_mutex); |
| + |
| new_val = gpiod_get_value_cansleep(unit->gpio.gpio_privacy); |
| + if (unit->gpio.last_event_val != new_val) { |
| + /* GPIO entities are always on the first chain. */ |
| + chain = list_first_entry(&dev->chains, |
| + struct uvc_video_chain, list); |
| + uvc_ctrl_status_event(chain, unit->controls, &new_val); |
| + } |
| + unit->gpio.last_event_val = new_val; |
| |
| - /* GPIO entities are always on the first chain. */ |
| - chain = list_first_entry(&dev->chains, struct uvc_video_chain, list); |
| - uvc_ctrl_status_event(chain, unit->controls, &new_val); |
| + mutex_unlock(&unit->gpio.event_mutex); |
| } |
| |
| 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) |
| + if (cs != UVC_CT_PRIVACY_CONTROL || size < 1 || !dev->gpio_unit) |
| return -EINVAL; |
| |
| + if (!dev->gpio_unit->gpio.is_gpio_ready) |
| + return -EBUSY; |
| + |
| *(u8 *)data = gpiod_get_value_cansleep(entity->gpio.gpio_privacy); |
| |
| return 0; |
| @@ -1234,10 +1245,31 @@ static irqreturn_t uvc_gpio_irq(int irq, void *data) |
| { |
| struct uvc_device *dev = data; |
| |
| + if (!dev->gpio_unit->gpio.is_gpio_ready) |
| + return IRQ_HANDLED; |
| + |
| uvc_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_gpio_parse(struct uvc_device *dev) |
| { |
| struct uvc_entity *unit; |
| @@ -1259,6 +1291,7 @@ static int uvc_gpio_parse(struct uvc_device *dev) |
| return -ENOMEM; |
| |
| unit->gpio.gpio_privacy = gpio_privacy; |
| + unit->gpio.last_event_val = -1; |
| unit->gpio.irq = irq; |
| unit->gpio.bControlSize = 1; |
| unit->gpio.bmControls = (u8 *)unit + sizeof(*unit); |
| @@ -1266,6 +1299,18 @@ static int uvc_gpio_parse(struct uvc_device *dev) |
| unit->get_cur = uvc_gpio_get_cur; |
| unit->get_info = uvc_gpio_get_info; |
| strscpy(unit->name, "GPIO", sizeof(unit->name)); |
| + mutex_init(&unit->gpio.event_mutex); |
| + |
| + /* |
| + * Note: This quirk will not match external UVC cameras, |
| + * as they will not have the corresponding ACPI GPIO entity. |
| + */ |
| + if (dmi_check_system(privacy_valid_during_streamon)) { |
| + dev->quirks |= UVC_QUIRK_PRIVACY_DURING_STREAM; |
| + unit->gpio.is_gpio_ready = false; |
| + } else { |
| + unit->gpio.is_gpio_ready = true; |
| + } |
| |
| list_add_tail(&unit->list, &dev->entities); |
| |
| diff --git a/drivers/media/usb/uvc/uvc_video.c b/drivers/media/usb/uvc/uvc_video.c |
| index d4b023d4de7c3b37bc0287dce16301e49f4e5bf4..3349cebdc223ec1c8b6c75e7b2560ff6ef11dd04 100644 |
| --- a/drivers/media/usb/uvc/uvc_video.c |
| +++ b/drivers/media/usb/uvc/uvc_video.c |
| @@ -2216,6 +2216,29 @@ int uvc_video_init(struct uvc_streaming *stream) |
| return 0; |
| } |
| |
| +static void uvc_gpio_privacy_quirks(struct uvc_streaming *stream, bool enable) |
| +{ |
| + struct uvc_device *dev = stream->dev; |
| + struct uvc_video_chain *first_chain; |
| + |
| + if (!(dev->quirks & UVC_QUIRK_PRIVACY_DURING_STREAM)) |
| + return; |
| + |
| + if (!dev->gpio_unit) |
| + return; |
| + |
| + first_chain = list_first_entry(&dev->chains, |
| + struct uvc_video_chain, list); |
| + /* GPIO entities are always on the first chain. */ |
| + if (stream->chain != first_chain) |
| + return; |
| + |
| + dev->gpio_unit->gpio.is_gpio_ready = enable; |
| + |
| + if (enable) |
| + uvc_gpio_event(stream->dev); |
| +} |
| + |
| int uvc_video_start_streaming(struct uvc_streaming *stream) |
| { |
| int ret; |
| @@ -2233,6 +2256,8 @@ int uvc_video_start_streaming(struct uvc_streaming *stream) |
| if (ret < 0) |
| goto error_video; |
| |
| + uvc_gpio_privacy_quirks(stream, true); |
| + |
| return 0; |
| |
| error_video: |
| @@ -2245,6 +2270,8 @@ int uvc_video_start_streaming(struct uvc_streaming *stream) |
| |
| void uvc_video_stop_streaming(struct uvc_streaming *stream) |
| { |
| + uvc_gpio_privacy_quirks(stream, false); |
| + |
| uvc_video_stop_transfer(stream, 1); |
| |
| if (stream->intf->num_altsetting > 1) { |
| diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h |
| index 50f171e7381bb5557d074dfb3cd95eaf5504c3b7..586b76c4e66e7145333a35aa34a9442505c3d6f3 100644 |
| --- a/drivers/media/usb/uvc/uvcvideo.h |
| +++ b/drivers/media/usb/uvc/uvcvideo.h |
| @@ -73,6 +73,7 @@ |
| #define UVC_QUIRK_FORCE_Y8 0x00000800 |
| #define UVC_QUIRK_FORCE_BPP 0x00001000 |
| #define UVC_QUIRK_WAKE_AUTOSUSPEND 0x00002000 |
| +#define UVC_QUIRK_PRIVACY_DURING_STREAM 0x00004000 |
| |
| /* Format flags */ |
| #define UVC_FMT_FLAG_COMPRESSED 0x00000001 |
| @@ -226,6 +227,9 @@ struct uvc_entity { |
| u8 *bmControls; |
| struct gpio_desc *gpio_privacy; |
| int irq; |
| + struct mutex event_mutex; |
| + int last_event_val; |
| + bool is_gpio_ready; |
| } gpio; |
| }; |
| |
| @@ -717,6 +721,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_gpio_event(struct uvc_device *dev); |
| + |
| /* Video */ |
| int uvc_video_init(struct uvc_streaming *stream); |
| int uvc_video_suspend(struct uvc_streaming *stream); |
| -- |
| 2.34.1 |
| |