From nobody Wed Oct 1 23:30:06 2025 Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8F8C2F7AB8 for ; Fri, 26 Sep 2025 13:11:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758892306; cv=none; b=dZuekbqhTvwC1zcmFUWwGlVxnPp1Vt6YFpQhgtayYekm/ZgfhKzG+NBvLZEfp0+PcdsUAcs169vHq60vuDTNqiMQ0sJAiR2KwqTLWONuLvASJmsoCz46f62nPJv4ssHIEaMH9xE4wTyvm8p8hO32nqi2YE5tRs0tPXQ3eA2FCQ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758892306; c=relaxed/simple; bh=0RIh4sUDiZBXgZTKOmEK1Orq2YrtKqmiRHFViDVMTyY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=C+fdjZy2Zl7Ta7vRbSgpt0fGk+MkFOEz6ilKZQ19ZtNxubcx+73Og3rwYUK1IbJ4uwJqFL17XP1v+WecIVotPJvOgAzcPHq1YZIRLWILc1lUkV625f6G/AEtx9mc7jYLdjz+0zvXAaYZPHzTkvyCGrLSTrQ23+ofCSxzEBpCZ9k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=Jp31ks4m; arc=none smtp.client-ip=209.85.167.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Jp31ks4m" Received: by mail-lf1-f44.google.com with SMTP id 2adb3069b0e04-57f1b88354eso2214477e87.1 for ; Fri, 26 Sep 2025 06:11:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1758892295; x=1759497095; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=pYyVOOG6RRM8nz0iAqyuD1/OPjUMPsAlYF5n8yOgLvk=; b=Jp31ks4mL7Ki0Npp/5ZfQsr+IueaUcOc309+knHt8jGeIvlcRAdwUedGXvAsVfDL7j LPreeYzbSYHna3R0SRcMw8ywRZGlKh/Ipie0kesevaWw9zmntZF0O0qa43fHBPsTho7m ExqS+8xGgE/63cwWiGX9yGnVN9V7cBtqTm1Ck= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758892295; x=1759497095; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pYyVOOG6RRM8nz0iAqyuD1/OPjUMPsAlYF5n8yOgLvk=; b=t4OU3DhxrpbTIzemsig9LCpwzo1nbM3+vLXp+dGhAdL+AaKBq/gVhWsiquLCjOhCcE GiRHKmKFQAPntN3y3G6IBqQmnRt7pTbKTLDNhQiaDXKxIjChZS6yL8ORkXwSCTBoYgr2 VOkbPfHfU0ZrThTGlk2aDUAe19HzO/zexPUp9B2mGpgdpyy3neWgRtmrBa9U29bOKuCR PmC5giwt4lVEnAlX+5fU77ofkuIn3oxA54tIpjRUkOIFwleED5biNx3Yhkteea19EeR1 L5OaftRMHMel/ipPXEJxZ8sbMN3WWYzSIkrHZOSAcyXnsJBMJJqhAfvHst74zyka1Bb8 J8vw== X-Forwarded-Encrypted: i=1; AJvYcCWadyb36VWwR9fjVi/jZXUlQy7uSVZiGEaQywVFyVugeZ6ehhH7LNIpkBy7UOBVwFqQGW2AYupPyBJWS/c=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6T/5AFBKfvABFoDo7I8rIFHQ5qzgu+vDKyFkXA1SGlVNe8QSo WUHpe6kgpweDHKzo531zDjnlPFCtiXKJtXszhe6OWuJpZQRNuL2SIEkuhNeMBwClqA== X-Gm-Gg: ASbGncvBhoL7madsFmpmv9RtFC0jDD0edq33wjzTwj/LVZegpPwOFUE2P2iY4z5+gRE lLNTzo2itT2TMWFbk0oIfgbFHL90URCVmcUCmK8En2J8etkrcu+jK+osuKaDgprBWs5TvA1Rc4I aF+r9iBRBCg/2CAtiIZ2HSc29MWk0GY6ZitqVgLZ3HyAMW0U1EkGJKtEvrQV8mUfogDJ7kSQs3n mjFdWxNyzFmQYw+F1kSDkJns+8RJj7roYQ+TKHi4F++8WVgotidvCwtvcyDmBkCFuPOH5WrYQvj F2Lu+dakluAs4pBJpDVtk96LPH7M9ZialYdo2ClrGiJ9V5PVfMpAunTl3iPw6w2hALeT6WJv2AQ 3mr9x45M3fC+ksjfEIQAq0j1IDLAQCBl0h5/FQX7umzfCCEPFlG4a3+tlad4ayyJPLZVzl2IB+s rxEQ== X-Google-Smtp-Source: AGHT+IGpDWvtokITIA2zTptMNOmw/ZYaGZaFcnmGwqV7Ib3ziWjeQpXNcwBL/nnOmUeOfVGug4KhQg== X-Received: by 2002:a05:6512:220d:b0:577:494e:ca63 with SMTP id 2adb3069b0e04-582d0740473mr2291049e87.12.1758892294482; Fri, 26 Sep 2025 06:11:34 -0700 (PDT) Received: from ribalda.c.googlers.com (64.153.228.35.bc.googleusercontent.com. [35.228.153.64]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-58527c6b014sm123872e87.43.2025.09.26.06.11.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Sep 2025 06:11:34 -0700 (PDT) From: Ricardo Ribalda Date: Fri, 26 Sep 2025 13:11:32 +0000 Subject: [PATCH v3 08/12] media: uvcvideo: Add support for V4L2_CID_CAMERA_ORIENTATION Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250926-uvc-orientation-v3-8-6dc2fa5b4220@chromium.org> References: <20250926-uvc-orientation-v3-0-6dc2fa5b4220@chromium.org> In-Reply-To: <20250926-uvc-orientation-v3-0-6dc2fa5b4220@chromium.org> To: Hans de Goede , Laurent Pinchart , Mauro Carvalho Chehab , Sakari Ailus , Greg Kroah-Hartman , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Linus Walleij , Bartosz Golaszewski , "Rafael J. Wysocki" , Len Brown , Robert Moore , Hans Verkuil Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, devicetree@vger.kernel.org, linux-gpio@vger.kernel.org, linux-acpi@vger.kernel.org, acpica-devel@lists.linux.dev, Ricardo Ribalda X-Mailer: b4 0.14.2 Fetch the orientation from the fwnode and map it into a control. The uvc driver does not use the media controller, so we need to create a software entity, like we previously did with the external gpio. We do not re-purpose the external gpio entity because its is planned to remove it. We need to make uvc_alloc_entity non static. Signed-off-by: Ricardo Ribalda --- drivers/media/usb/uvc/Kconfig | 1 + drivers/media/usb/uvc/Makefile | 3 +- drivers/media/usb/uvc/uvc_ctrl.c | 21 ++++++++++ drivers/media/usb/uvc/uvc_driver.c | 22 +++++++---- drivers/media/usb/uvc/uvc_entity.c | 1 + drivers/media/usb/uvc/uvc_swentity.c | 76 ++++++++++++++++++++++++++++++++= ++++ drivers/media/usb/uvc/uvcvideo.h | 17 ++++++++ include/linux/usb/uvc.h | 3 ++ 8 files changed, 135 insertions(+), 9 deletions(-) diff --git a/drivers/media/usb/uvc/Kconfig b/drivers/media/usb/uvc/Kconfig index 579532272fd6d7a8ef65c1a3a892b723f40e584e..65f9cf81c8f434c50910db0ae94= 788af182eb4c4 100644 --- a/drivers/media/usb/uvc/Kconfig +++ b/drivers/media/usb/uvc/Kconfig @@ -4,6 +4,7 @@ config USB_VIDEO_CLASS depends on VIDEO_DEV select VIDEOBUF2_VMALLOC select UVC_COMMON + select V4L2_FWNODE help Support for the USB Video Class (UVC). Currently only video input devices, such as webcams, are supported. diff --git a/drivers/media/usb/uvc/Makefile b/drivers/media/usb/uvc/Makefile index 4f9eee4f81ab6436a8b90324a688a149b2c3bcd1..b4398177c4bb0a9bd49dfd4ca7f= 2e933b4a1d7df 100644 --- a/drivers/media/usb/uvc/Makefile +++ b/drivers/media/usb/uvc/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 uvcvideo-objs :=3D uvc_driver.o uvc_queue.o uvc_v4l2.o uvc_video.o uvc_ct= rl.o \ - uvc_status.o uvc_isight.o uvc_debugfs.o uvc_metadata.o + uvc_status.o uvc_isight.o uvc_debugfs.o uvc_metadata.o \ + uvc_swentity.o ifeq ($(CONFIG_MEDIA_CONTROLLER),y) uvcvideo-objs +=3D uvc_entity.o endif diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_c= trl.c index a869257e9b7c07eaa7d725d107bd1cb57d3c7377..aa9de84de9236540c46ee78fb84= 58873d503786e 100644 --- a/drivers/media/usb/uvc/uvc_ctrl.c +++ b/drivers/media/usb/uvc/uvc_ctrl.c @@ -385,6 +385,13 @@ static const struct uvc_control_info uvc_ctrls[] =3D { | UVC_CTRL_FLAG_GET_RANGE | UVC_CTRL_FLAG_RESTORE, }, + { + .entity =3D UVC_GUID_SWENTITY, + .selector =3D 0, + .index =3D 0, + .size =3D 1, + .flags =3D UVC_CTRL_FLAG_GET_CUR, + }, }; =20 static const u32 uvc_control_classes[] =3D { @@ -1009,6 +1016,17 @@ static const struct uvc_control_mapping uvc_ctrl_map= pings[] =3D { .menu_mask =3D BIT(V4L2_COLORFX_VIVID) | BIT(V4L2_COLORFX_NONE), }, + { + .id =3D V4L2_CID_CAMERA_ORIENTATION, + .entity =3D UVC_GUID_SWENTITY, + .selector =3D 0, + .size =3D 8, + .offset =3D 0, + .v4l2_type =3D V4L2_CTRL_TYPE_MENU, + .data_type =3D UVC_CTRL_DATA_TYPE_ENUM, + .menu_mask =3D GENMASK(V4L2_CAMERA_ORIENTATION_EXTERNAL, + V4L2_CAMERA_ORIENTATION_FRONT), + }, }; =20 /* ------------------------------------------------------------------------ @@ -3281,6 +3299,9 @@ static int uvc_ctrl_init_chain(struct uvc_video_chain= *chain) } else if (UVC_ENTITY_TYPE(entity) =3D=3D UVC_EXT_GPIO_UNIT) { bmControls =3D entity->gpio.bmControls; bControlSize =3D entity->gpio.bControlSize; + } else if (UVC_ENTITY_TYPE(entity) =3D=3D UVC_SWENTITY_UNIT) { + bmControls =3D entity->swentity.bmControls; + bControlSize =3D entity->swentity.bControlSize; } =20 /* Remove bogus/blacklisted controls */ diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc= _driver.c index fa61f1d0ea2c3d52c560b2f0834e52ca75c4227e..91edc7703b1987d8dfd552999ed= bdbf1b4653f3f 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -794,9 +794,9 @@ static const u8 uvc_media_transport_input_guid[16] =3D UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT; static const u8 uvc_processing_guid[16] =3D UVC_GUID_UVC_PROCESSING; =20 -static struct uvc_entity *uvc_alloc_new_entity(struct uvc_device *dev, u16= type, - u16 id, unsigned int num_pads, - unsigned int extra_size) +struct uvc_entity *uvc_alloc_new_entity(struct uvc_device *dev, u16 type, + u16 id, unsigned int num_pads, + unsigned int extra_size) { struct uvc_entity *entity; unsigned int num_inputs; @@ -1887,12 +1887,14 @@ static int uvc_scan_device(struct uvc_device *dev) return -ENODEV; } =20 - /* Add GPIO entity to the first chain. */ - if (dev->gpio_unit) { - chain =3D list_first_entry(&dev->chains, - struct uvc_video_chain, list); + /* Add virtual entities to the first chain. */ + chain =3D list_first_entry(&dev->chains, struct uvc_video_chain, list); + + if (dev->gpio_unit) list_add_tail(&dev->gpio_unit->chain, &chain->entities); - } + + if (dev->swentity_unit) + list_add_tail(&dev->swentity_unit->chain, &chain->entities); =20 return 0; } @@ -2244,6 +2246,10 @@ static int uvc_probe(struct usb_interface *intf, if (ret < 0) goto error; =20 + ret =3D uvc_swentity_init(dev); + if (ret < 0) + goto error; + dev_info(&dev->intf->dev, "Found UVC %u.%02x device %s (%04x:%04x)\n", dev->uvc_version >> 8, dev->uvc_version & 0xff, udev->product ? udev->product : "", diff --git a/drivers/media/usb/uvc/uvc_entity.c b/drivers/media/usb/uvc/uvc= _entity.c index ee1007add243036f68b7014ca621813e461fa73d..d5125fdd829a7e11f7083762531= f811069e0e43f 100644 --- a/drivers/media/usb/uvc/uvc_entity.c +++ b/drivers/media/usb/uvc/uvc_entity.c @@ -86,6 +86,7 @@ static int uvc_mc_init_entity(struct uvc_video_chain *cha= in, case UVC_VC_PROCESSING_UNIT: case UVC_VC_EXTENSION_UNIT: case UVC_EXT_GPIO_UNIT: + case UVC_SWENTITY_UNIT: /* For lack of a better option. */ function =3D MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER; break; diff --git a/drivers/media/usb/uvc/uvc_swentity.c b/drivers/media/usb/uvc/u= vc_swentity.c new file mode 100644 index 0000000000000000000000000000000000000000..eefc5d08e370515181f74590f2f= 38189770b01b2 --- /dev/null +++ b/drivers/media/usb/uvc/uvc_swentity.c @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * uvc_swentity.c -- USB Video Class driver + * + * Copyright 2025 Google LLC + */ + +#include +#include + +#include + +#include "uvcvideo.h" + +static int uvc_swentity_get_cur(struct uvc_device *dev, struct uvc_entity = *entity, + u8 cs, void *data, u16 size) +{ + if (size < 1) + return -EINVAL; + + switch (entity->swentity.props.orientation) { + case V4L2_FWNODE_ORIENTATION_FRONT: + *(u8 *)data =3D V4L2_CAMERA_ORIENTATION_FRONT; + break; + case V4L2_FWNODE_ORIENTATION_BACK: + *(u8 *)data =3D V4L2_CAMERA_ORIENTATION_BACK; + break; + default: + *(u8 *)data =3D V4L2_CAMERA_ORIENTATION_EXTERNAL; + } + + return 0; +} + +static int uvc_swentity_get_info(struct uvc_device *dev, + struct uvc_entity *entity, u8 cs, u8 *caps) +{ + *caps =3D UVC_CONTROL_CAP_GET; + return 0; +} + +int uvc_swentity_init(struct uvc_device *dev) +{ + static const u8 uvc_swentity_guid[] =3D UVC_GUID_SWENTITY; + struct v4l2_fwnode_device_properties props; + struct uvc_entity *unit; + int ret; + + ret =3D v4l2_fwnode_device_parse(&dev->udev->dev, &props); + if (ret) + return dev_err_probe(&dev->intf->dev, ret, + "Can't parse fwnode\n"); + + if (props.orientation =3D=3D V4L2_FWNODE_PROPERTY_UNSET) + return 0; + + unit =3D uvc_alloc_new_entity(dev, UVC_SWENTITY_UNIT, + UVC_SWENTITY_UNIT_ID, 0, 1); + if (!unit) + return -ENOMEM; + + memcpy(unit->guid, uvc_swentity_guid, sizeof(unit->guid)); + unit->swentity.props =3D props; + unit->swentity.bControlSize =3D 1; + unit->swentity.bmControls =3D (u8 *)unit + sizeof(*unit); + unit->swentity.bmControls[0] =3D 1; + unit->get_cur =3D uvc_swentity_get_cur; + unit->get_info =3D uvc_swentity_get_info; + strscpy(unit->name, "SWENTITY", sizeof(unit->name)); + + list_add_tail(&unit->list, &dev->entities); + + dev->swentity_unit =3D unit; + + return 0; +} diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvi= deo.h index 24292efbe47d3cd440252afb85ecbc826e2eedc1..04ca5dcce11d902dbfdf32f2a96= 2159ba7940a39 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -18,6 +18,7 @@ #include #include #include +#include #include =20 /* -----------------------------------------------------------------------= --- @@ -38,6 +39,9 @@ (UVC_ENTITY_IS_TERM(entity) && \ ((entity)->type & 0x8000) =3D=3D UVC_TERM_OUTPUT) =20 +#define UVC_SWENTITY_UNIT 0x7ffd +#define UVC_SWENTITY_UNIT_ID 0x101 + #define UVC_EXT_GPIO_UNIT 0x7ffe #define UVC_EXT_GPIO_UNIT_ID 0x100 =20 @@ -247,6 +251,12 @@ struct uvc_entity { int irq; bool initialized; } gpio; + + struct { + u8 bControlSize; + u8 *bmControls; + struct v4l2_fwnode_device_properties props; + } swentity; }; =20 u8 bNrInPins; @@ -623,6 +633,7 @@ struct uvc_device { } async_ctrl; =20 struct uvc_entity *gpio_unit; + struct uvc_entity *swentity_unit; }; =20 struct uvc_fh { @@ -688,6 +699,9 @@ do { \ */ =20 struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id); +struct uvc_entity *uvc_alloc_new_entity(struct uvc_device *dev, u16 type, + u16 id, unsigned int num_pads, + unsigned int extra_size); =20 /* Video buffers queue management. */ int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type); @@ -813,4 +827,7 @@ void uvc_debugfs_cleanup_stream(struct uvc_streaming *s= tream); size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf, size_t size); =20 +/* swentity */ +int uvc_swentity_init(struct uvc_device *dev); + #endif diff --git a/include/linux/usb/uvc.h b/include/linux/usb/uvc.h index 22e0dab0809e296e089940620ae0e8838e109701..654182c2d22cdd9c72709247c1c= 8cabf9f5b19ee 100644 --- a/include/linux/usb/uvc.h +++ b/include/linux/usb/uvc.h @@ -29,6 +29,9 @@ #define UVC_GUID_EXT_GPIO_CONTROLLER \ {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03} +#define UVC_GUID_SWENTITY \ + {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04} #define UVC_GUID_CHROMEOS_XU \ {0x24, 0xe9, 0xd7, 0x74, 0xc9, 0x49, 0x45, 0x4a, \ 0x98, 0xa3, 0xc8, 0x07, 0x7e, 0x05, 0x1c, 0xa3} --=20 2.51.0.536.g15c5d4f767-goog