Skip to content

Commit 2886477

Browse files
ribaldamchehab
authored andcommitted
media: uvcvideo: Implement UVC_EXT_GPIO_UNIT
Some devices can implement a physical switch to disable the input of the camera on demand. Think of it like an elegant privacy sticker. The system can read the status of the privacy switch via a GPIO. It is important to know the status of the switch, e.g. to notify the user when the camera will produce black frames and a videochat application is used. In some systems, the GPIO is connected to the main SoC instead of the camera controller, with the connection reported by the system firmware (ACPI or DT). In that case, the UVC device isn't aware of the GPIO. We need to implement a virtual entity to handle the GPIO fully on the driver side. For example, for ACPI-based systems, the GPIO is reported in the USB device object: Scope (\_SB.PCI0.XHCI.RHUB.HS07) { /.../ Name (_CRS, ResourceTemplate () // _CRS: Current Resource Settings { GpioIo (Exclusive, PullDefault, 0x0000, 0x0000, IoRestrictionOutputOnly, "\\_SB.PCI0.GPIO", 0x00, ResourceConsumer, , ) { // Pin list 0x0064 } }) Name (_DSD, Package (0x02) // _DSD: Device-Specific Data { ToUUID ("daffd814-6eba-4d8c-8a91-bc9bbf4aa301") /* Device Properties for _DSD */, Package (0x01) { Package (0x02) { "privacy-gpio", Package (0x04) { \_SB.PCI0.XHCI.RHUB.HS07, Zero, Zero, One } } } }) } Signed-off-by: Ricardo Ribalda <ribalda@chromium.org> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
1 parent 65900c5 commit 2886477

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

drivers/media/usb/uvc/uvc_ctrl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,9 @@ int uvc_ctrl_init_device(struct uvc_device *dev)
22912291
} else if (UVC_ENTITY_TYPE(entity) == UVC_ITT_CAMERA) {
22922292
bmControls = entity->camera.bmControls;
22932293
bControlSize = entity->camera.bControlSize;
2294+
} else if (UVC_ENTITY_TYPE(entity) == UVC_EXT_GPIO_UNIT) {
2295+
bmControls = entity->gpio.bmControls;
2296+
bControlSize = entity->gpio.bControlSize;
22942297
}
22952298

22962299
/* Remove bogus/blacklisted controls */

drivers/media/usb/uvc/uvc_driver.c

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include <linux/atomic.h>
10+
#include <linux/gpio/consumer.h>
1011
#include <linux/kernel.h>
1112
#include <linux/list.h>
1213
#include <linux/module.h>
@@ -1020,6 +1021,7 @@ static int uvc_parse_streaming(struct uvc_device *dev,
10201021
}
10211022

10221023
static const u8 uvc_camera_guid[16] = UVC_GUID_UVC_CAMERA;
1024+
static const u8 uvc_gpio_guid[16] = UVC_GUID_EXT_GPIO_CONTROLLER;
10231025
static const u8 uvc_media_transport_input_guid[16] =
10241026
UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT;
10251027
static const u8 uvc_processing_guid[16] = UVC_GUID_UVC_PROCESSING;
@@ -1051,6 +1053,9 @@ static struct uvc_entity *uvc_alloc_entity(u16 type, u16 id,
10511053
* is initialized by the caller.
10521054
*/
10531055
switch (type) {
1056+
case UVC_EXT_GPIO_UNIT:
1057+
memcpy(entity->guid, uvc_gpio_guid, 16);
1058+
break;
10541059
case UVC_ITT_CAMERA:
10551060
memcpy(entity->guid, uvc_camera_guid, 16);
10561061
break;
@@ -1464,6 +1469,108 @@ static int uvc_parse_control(struct uvc_device *dev)
14641469
return 0;
14651470
}
14661471

1472+
/* -----------------------------------------------------------------------------
1473+
* Privacy GPIO
1474+
*/
1475+
1476+
static void uvc_gpio_event(struct uvc_device *dev)
1477+
{
1478+
struct uvc_entity *unit = dev->gpio_unit;
1479+
struct uvc_video_chain *chain;
1480+
u8 new_val;
1481+
1482+
if (!unit)
1483+
return;
1484+
1485+
new_val = gpiod_get_value_cansleep(unit->gpio.gpio_privacy);
1486+
1487+
/* GPIO entities are always on the first chain. */
1488+
chain = list_first_entry(&dev->chains, struct uvc_video_chain, list);
1489+
uvc_ctrl_status_event(chain, unit->controls, &new_val);
1490+
}
1491+
1492+
static int uvc_gpio_get_cur(struct uvc_device *dev, struct uvc_entity *entity,
1493+
u8 cs, void *data, u16 size)
1494+
{
1495+
if (cs != UVC_CT_PRIVACY_CONTROL || size < 1)
1496+
return -EINVAL;
1497+
1498+
*(u8 *)data = gpiod_get_value_cansleep(entity->gpio.gpio_privacy);
1499+
1500+
return 0;
1501+
}
1502+
1503+
static int uvc_gpio_get_info(struct uvc_device *dev, struct uvc_entity *entity,
1504+
u8 cs, u8 *caps)
1505+
{
1506+
if (cs != UVC_CT_PRIVACY_CONTROL)
1507+
return -EINVAL;
1508+
1509+
*caps = UVC_CONTROL_CAP_GET | UVC_CONTROL_CAP_AUTOUPDATE;
1510+
return 0;
1511+
}
1512+
1513+
static irqreturn_t uvc_gpio_irq(int irq, void *data)
1514+
{
1515+
struct uvc_device *dev = data;
1516+
1517+
uvc_gpio_event(dev);
1518+
return IRQ_HANDLED;
1519+
}
1520+
1521+
static int uvc_gpio_parse(struct uvc_device *dev)
1522+
{
1523+
struct uvc_entity *unit;
1524+
struct gpio_desc *gpio_privacy;
1525+
int irq;
1526+
1527+
gpio_privacy = devm_gpiod_get_optional(&dev->udev->dev, "privacy",
1528+
GPIOD_IN);
1529+
if (IS_ERR_OR_NULL(gpio_privacy))
1530+
return PTR_ERR_OR_ZERO(gpio_privacy);
1531+
1532+
unit = uvc_alloc_entity(UVC_EXT_GPIO_UNIT, UVC_EXT_GPIO_UNIT_ID, 0, 1);
1533+
if (!unit)
1534+
return -ENOMEM;
1535+
1536+
irq = gpiod_to_irq(gpio_privacy);
1537+
if (irq < 0) {
1538+
if (irq != EPROBE_DEFER)
1539+
dev_err(&dev->udev->dev,
1540+
"No IRQ for privacy GPIO (%d)\n", irq);
1541+
return irq;
1542+
}
1543+
1544+
unit->gpio.gpio_privacy = gpio_privacy;
1545+
unit->gpio.irq = irq;
1546+
unit->gpio.bControlSize = 1;
1547+
unit->gpio.bmControls = (u8 *)unit + sizeof(*unit);
1548+
unit->gpio.bmControls[0] = 1;
1549+
unit->get_cur = uvc_gpio_get_cur;
1550+
unit->get_info = uvc_gpio_get_info;
1551+
strncpy(unit->name, "GPIO", sizeof(unit->name) - 1);
1552+
1553+
list_add_tail(&unit->list, &dev->entities);
1554+
1555+
dev->gpio_unit = unit;
1556+
1557+
return 0;
1558+
}
1559+
1560+
static int uvc_gpio_init_irq(struct uvc_device *dev)
1561+
{
1562+
struct uvc_entity *unit = dev->gpio_unit;
1563+
1564+
if (!unit || unit->gpio.irq < 0)
1565+
return 0;
1566+
1567+
return devm_request_threaded_irq(&dev->udev->dev, unit->gpio.irq, NULL,
1568+
uvc_gpio_irq,
1569+
IRQF_ONESHOT | IRQF_TRIGGER_FALLING |
1570+
IRQF_TRIGGER_RISING,
1571+
"uvc_privacy_gpio", dev);
1572+
}
1573+
14671574
/* ------------------------------------------------------------------------
14681575
* UVC device scan
14691576
*/
@@ -1953,6 +2060,13 @@ static int uvc_scan_device(struct uvc_device *dev)
19532060
return -1;
19542061
}
19552062

2063+
/* Add GPIO entity to the first chain. */
2064+
if (dev->gpio_unit) {
2065+
chain = list_first_entry(&dev->chains,
2066+
struct uvc_video_chain, list);
2067+
list_add_tail(&dev->gpio_unit->chain, &chain->entities);
2068+
}
2069+
19562070
return 0;
19572071
}
19582072

@@ -2285,6 +2399,12 @@ static int uvc_probe(struct usb_interface *intf,
22852399
goto error;
22862400
}
22872401

2402+
/* Parse the associated GPIOs. */
2403+
if (uvc_gpio_parse(dev) < 0) {
2404+
uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC GPIOs\n");
2405+
goto error;
2406+
}
2407+
22882408
uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
22892409
dev->uvc_version >> 8, dev->uvc_version & 0xff,
22902410
udev->product ? udev->product : "<unnamed>",
@@ -2335,6 +2455,13 @@ static int uvc_probe(struct usb_interface *intf,
23352455
"supported.\n", ret);
23362456
}
23372457

2458+
ret = uvc_gpio_init_irq(dev);
2459+
if (ret < 0) {
2460+
dev_err(&dev->udev->dev,
2461+
"Unable to request privacy GPIO IRQ (%d)\n", ret);
2462+
goto error;
2463+
}
2464+
23382465
uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
23392466
usb_enable_autosuspend(udev);
23402467
return 0;

drivers/media/usb/uvc/uvc_entity.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ static int uvc_mc_init_entity(struct uvc_video_chain *chain,
105105
case UVC_OTT_DISPLAY:
106106
case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
107107
case UVC_EXTERNAL_VENDOR_SPECIFIC:
108+
case UVC_EXT_GPIO_UNIT:
108109
default:
109110
function = MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN;
110111
break;

drivers/media/usb/uvc/uvcvideo.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#error "The uvcvideo.h header is deprecated, use linux/uvcvideo.h instead."
77
#endif /* __KERNEL__ */
88

9+
#include <linux/atomic.h>
910
#include <linux/kernel.h>
1011
#include <linux/poll.h>
1112
#include <linux/usb.h>
@@ -37,6 +38,8 @@
3738
(UVC_ENTITY_IS_TERM(entity) && \
3839
((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
3940

41+
#define UVC_EXT_GPIO_UNIT 0x7ffe
42+
#define UVC_EXT_GPIO_UNIT_ID 0x100
4043

4144
/* ------------------------------------------------------------------------
4245
* GUIDs
@@ -56,6 +59,9 @@
5659
#define UVC_GUID_UVC_SELECTOR \
5760
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
5861
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02}
62+
#define UVC_GUID_EXT_GPIO_CONTROLLER \
63+
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
64+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03}
5965

6066
#define UVC_GUID_FORMAT_MJPEG \
6167
{ 'M', 'J', 'P', 'G', 0x00, 0x00, 0x10, 0x00, \
@@ -212,6 +218,7 @@
212218
* Structures.
213219
*/
214220

221+
struct gpio_desc;
215222
struct uvc_device;
216223

217224
/* TODO: Put the most frequently accessed fields at the beginning of
@@ -353,6 +360,13 @@ struct uvc_entity {
353360
u8 *bmControls;
354361
u8 *bmControlsType;
355362
} extension;
363+
364+
struct {
365+
u8 bControlSize;
366+
u8 *bmControls;
367+
struct gpio_desc *gpio_privacy;
368+
int irq;
369+
} gpio;
356370
};
357371

358372
u8 bNrInPins;
@@ -691,6 +705,8 @@ struct uvc_device {
691705
struct uvc_control *ctrl;
692706
const void *data;
693707
} async_ctrl;
708+
709+
struct uvc_entity *gpio_unit;
694710
};
695711

696712
enum uvc_handle_state {

0 commit comments

Comments
 (0)