Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: uvcvideo: implement UVC v1.5 ROI

Implement support for ROI as described in UVC 1.5:
4.2.2.1.20 Digital Region of Interest (ROI) Control

ROI control is implemented using V4L2 control API as
two UVC-specific controls:
V4L2_CID_UVC_REGION_OF_INTEREST_RECT and
V4L2_CID_UVC_REGION_OF_INTEREST_AUTO.

Reviewed-by: Ricardo Ribalda <ribalda@chromium.org>
Signed-off-by: Yunke Cao <yunkec@google.com>
Reviewed-by: Yunke Cao <yunkec@google.com>
Tested-by: Yunke Cao <yunkec@google.com>
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
Link: https://lore.kernel.org/r/20250203-uvc-roi-v17-16-5900a9fed613@chromium.org
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Hans Verkuil <hverkuil@xs4all.nl>
[hverkuil: fix control names: "Of" -> "of", "Controls" -> "Ctrls"]

authored by

Yunke Cao and committed by
Hans Verkuil
2dc768d7 990262fd

+109
+81
drivers/media/usb/uvc/uvc_ctrl.c
··· 358 358 .flags = UVC_CTRL_FLAG_GET_CUR 359 359 | UVC_CTRL_FLAG_AUTO_UPDATE, 360 360 }, 361 + /* 362 + * UVC_CTRL_FLAG_AUTO_UPDATE is needed because the RoI may get updated 363 + * by sensors. 364 + * "This RoI should be the same as specified in most recent SET_CUR 365 + * except in the case where the ‘Auto Detect and Track’ and/or 366 + * ‘Image Stabilization’ bit have been set." 367 + * 4.2.2.1.20 Digital Region of Interest (ROI) Control 368 + */ 369 + { 370 + .entity = UVC_GUID_UVC_CAMERA, 371 + .selector = UVC_CT_REGION_OF_INTEREST_CONTROL, 372 + .index = 21, 373 + .size = 10, 374 + .flags = UVC_CTRL_FLAG_SET_CUR | UVC_CTRL_FLAG_GET_CUR 375 + | UVC_CTRL_FLAG_GET_MIN | UVC_CTRL_FLAG_GET_MAX 376 + | UVC_CTRL_FLAG_GET_DEF 377 + | UVC_CTRL_FLAG_AUTO_UPDATE, 378 + }, 361 379 }; 362 380 363 381 static const u32 uvc_control_classes[] = { ··· 619 601 buf, sizeof(*buf)); 620 602 621 603 return out_mapping; 604 + } 605 + 606 + static int uvc_get_rect(struct uvc_control_mapping *mapping, u8 query, 607 + const void *uvc_in, size_t v4l2_size, void *v4l2_out) 608 + { 609 + const struct uvc_rect *uvc_rect = uvc_in; 610 + struct v4l2_rect *v4l2_rect = v4l2_out; 611 + 612 + if (WARN_ON(v4l2_size != sizeof(struct v4l2_rect))) 613 + return -EINVAL; 614 + 615 + if (uvc_rect->left > uvc_rect->right || 616 + uvc_rect->top > uvc_rect->bottom) 617 + return -EIO; 618 + 619 + v4l2_rect->top = uvc_rect->top; 620 + v4l2_rect->left = uvc_rect->left; 621 + v4l2_rect->height = uvc_rect->bottom - uvc_rect->top + 1; 622 + v4l2_rect->width = uvc_rect->right - uvc_rect->left + 1; 623 + 624 + return 0; 625 + } 626 + 627 + static int uvc_set_rect(struct uvc_control_mapping *mapping, size_t v4l2_size, 628 + const void *v4l2_in, void *uvc_out) 629 + { 630 + struct uvc_rect *uvc_rect = uvc_out; 631 + const struct v4l2_rect *v4l2_rect = v4l2_in; 632 + 633 + if (WARN_ON(v4l2_size != sizeof(struct v4l2_rect))) 634 + return -EINVAL; 635 + 636 + uvc_rect->top = min(0xffff, v4l2_rect->top); 637 + uvc_rect->left = min(0xffff, v4l2_rect->left); 638 + uvc_rect->bottom = min(0xffff, v4l2_rect->top + v4l2_rect->height - 1); 639 + uvc_rect->right = min(0xffff, v4l2_rect->left + v4l2_rect->width - 1); 640 + 641 + return 0; 622 642 } 623 643 624 644 static const struct uvc_control_mapping uvc_ctrl_mappings[] = { ··· 952 896 .entity = UVC_GUID_UVC_PROCESSING, 953 897 .selector = UVC_PU_POWER_LINE_FREQUENCY_CONTROL, 954 898 .filter_mapping = uvc_ctrl_filter_plf_mapping, 899 + }, 900 + { 901 + .id = V4L2_CID_UVC_REGION_OF_INTEREST_RECT, 902 + .entity = UVC_GUID_UVC_CAMERA, 903 + .selector = UVC_CT_REGION_OF_INTEREST_CONTROL, 904 + .size = sizeof(struct uvc_rect) * 8, 905 + .offset = 0, 906 + .v4l2_type = V4L2_CTRL_TYPE_RECT, 907 + .data_type = UVC_CTRL_DATA_TYPE_RECT, 908 + .get = uvc_get_rect, 909 + .set = uvc_set_rect, 910 + .name = "Region of Interest Rectangle", 911 + }, 912 + { 913 + .id = V4L2_CID_UVC_REGION_OF_INTEREST_AUTO, 914 + .entity = UVC_GUID_UVC_CAMERA, 915 + .selector = UVC_CT_REGION_OF_INTEREST_CONTROL, 916 + .size = 16, 917 + .offset = 64, 918 + .v4l2_type = V4L2_CTRL_TYPE_BITMASK, 919 + .data_type = UVC_CTRL_DATA_TYPE_BITMASK, 920 + .name = "Region of Interest Auto Ctrls", 955 921 }, 956 922 }; 957 923 ··· 1551 1473 1552 1474 static size_t uvc_mapping_v4l2_size(struct uvc_control_mapping *mapping) 1553 1475 { 1476 + if (mapping->v4l2_type == V4L2_CTRL_TYPE_RECT) 1477 + return sizeof(struct v4l2_rect); 1478 + 1554 1479 if (uvc_ctrl_mapping_is_compound(mapping)) 1555 1480 return DIV_ROUND_UP(mapping->size, 8); 1556 1481
+7
drivers/media/usb/uvc/uvcvideo.h
··· 543 543 u16 uvc_version; 544 544 }; 545 545 546 + struct uvc_rect { 547 + u16 top; 548 + u16 left; 549 + u16 bottom; 550 + u16 right; 551 + } __packed; 552 + 546 553 struct uvc_status_streaming { 547 554 u8 button; 548 555 } __packed;
+1
include/uapi/linux/usb/video.h
··· 104 104 #define UVC_CT_ROLL_ABSOLUTE_CONTROL 0x0f 105 105 #define UVC_CT_ROLL_RELATIVE_CONTROL 0x10 106 106 #define UVC_CT_PRIVACY_CONTROL 0x11 107 + #define UVC_CT_REGION_OF_INTEREST_CONTROL 0x14 107 108 108 109 /* A.9.5. Processing Unit Control Selectors */ 109 110 #define UVC_PU_CONTROL_UNDEFINED 0x00
+13
include/uapi/linux/uvcvideo.h
··· 16 16 #define UVC_CTRL_DATA_TYPE_BOOLEAN 3 17 17 #define UVC_CTRL_DATA_TYPE_ENUM 4 18 18 #define UVC_CTRL_DATA_TYPE_BITMASK 5 19 + #define UVC_CTRL_DATA_TYPE_RECT 6 19 20 20 21 /* Control flags */ 21 22 #define UVC_CTRL_FLAG_SET_CUR (1 << 0) ··· 38 37 UVC_CTRL_FLAG_GET_DEF) 39 38 40 39 #define UVC_MENU_NAME_LEN 32 40 + 41 + /* V4L2 driver-specific controls */ 42 + #define V4L2_CID_UVC_REGION_OF_INTEREST_RECT (V4L2_CID_USER_UVC_BASE + 1) 43 + #define V4L2_CID_UVC_REGION_OF_INTEREST_AUTO (V4L2_CID_USER_UVC_BASE + 2) 44 + #define V4L2_UVC_REGION_OF_INTEREST_AUTO_EXPOSURE (1 << 0) 45 + #define V4L2_UVC_REGION_OF_INTEREST_AUTO_IRIS (1 << 1) 46 + #define V4L2_UVC_REGION_OF_INTEREST_AUTO_WHITE_BALANCE (1 << 2) 47 + #define V4L2_UVC_REGION_OF_INTEREST_AUTO_FOCUS (1 << 3) 48 + #define V4L2_UVC_REGION_OF_INTEREST_AUTO_FACE_DETECT (1 << 4) 49 + #define V4L2_UVC_REGION_OF_INTEREST_AUTO_DETECT_AND_TRACK (1 << 5) 50 + #define V4L2_UVC_REGION_OF_INTEREST_AUTO_IMAGE_STABILIZATION (1 << 6) 51 + #define V4L2_UVC_REGION_OF_INTEREST_AUTO_HIGHER_QUALITY (1 << 7) 41 52 42 53 struct uvc_menu_info { 43 54 __u32 value;
+7
include/uapi/linux/v4l2-controls.h
··· 215 215 */ 216 216 #define V4L2_CID_USER_THP7312_BASE (V4L2_CID_USER_BASE + 0x11c0) 217 217 218 + /* 219 + * The base for the uvc driver controls. 220 + * See linux/uvcvideo.h for the list of controls. 221 + * We reserve 64 controls for this driver. 222 + */ 223 + #define V4L2_CID_USER_UVC_BASE (V4L2_CID_USER_BASE + 0x11e0) 224 + 218 225 /* MPEG-class control IDs */ 219 226 /* The MPEG controls are applicable to all codec controls 220 227 * and the 'MPEG' part of the define is historical */