* [RFC PATCH] usb: gadget: uvc: allow userspace to send status packets
@ 2026-12-04 10:25 Keisuke Tsukuda
2026-12-04 10:54 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Keisuke Tsukuda @ 2026-12-04 10:25 UTC (permalink / raw)
To: nkapron@google.com, gregkh@linuxfoundation.org
Cc: linux-usb@vger.kernel.org, linux-api@vger.kernel.org,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Laurent Pinchart,
Dan Scally, Michael Grzeschik
The UVC gadget can expose a VideoControl interrupt endpoint, but its
userspace application cannot submit status packets to that endpoint. As a
result, a device-side change to a control cannot be reported to the host.
Add UVCIOC_SEND_STATUS to submit a raw UVC status packet through the
optional interrupt endpoint. A raw packet lets the application supply the
status type and payload for the controls it implements. The ioctl accepts
1 to 16 bytes, matching the endpoint's packet size. Its compat handler
also lets 32-bit tasks submit status packets.
The driver uses one request at a time and returns -EBUSY while that request
is pending. Queueing is serialized with endpoint enable and disable, and
unbind waits for a pending request to be returned before freeing it. The
endpoint API permits enable and disable from atomic context, so the
spinlock also covers those operations.
The ioctl reports queueing, not delivery to the host. If the host is not
polling the endpoint, a request can remain pending and later calls return
-EBUSY. Userspace remains responsible for answering GET_CUR with the
current value. The driver also reports an endpoint enable error instead of
continuing with a disabled endpoint.
Signed-off-by: Keisuke Tsukuda <tkdkei@outlook.jp>
---
On a Tinker Board 3N (dwc3), a 32-bit ARM process queued a Brightness
status packet with the RFC change as the only UVC module delta from
the running kernel. A 64-bit gadget application answered control
requests. A Linux 6.8 host received the control event and read
GET_CUR=128 over a 5 Gb/s USB link. The host's event watcher held the
video node open, keeping uvcvideo's status URB active. The host also
queried Brightness repeatedly with VIDIOC_G_CTRL. No video STREAMON
was used. The running kernel already contained a separate frame
interval fix. The test module contained neither the separate 32-bit
SEND_RESPONSE fix nor the posted isochronous payload-size fix. The
original module was reloaded after the test. The standalone diff also
built an arm64 usb_f_uvc.o with CONFIG_COMPAT=y and W=1 against the
stated base.
Earlier trials on a Raspberry Pi Compute Module 4 (CM4, dwc2) exercised
USB disconnect, reconnect, and unbind with a request pending. The
module also carried the separate 32-bit SEND_RESPONSE fix and the posted
isochronous payload-size fix; the relevant status and unbind code matches
this RFC apart from comments.
All host trials used Linux. Windows and macOS hosts were not tested.
Since Linux 6.16, opening a uvcvideo node alone no longer starts its
status URB. Streaming, some control ioctls, and pending asynchronous
controls do. In a separate CM4 trial with a Linux 7.0.0-27-generic
host idle, userspace queued a packet for Brightness 16, changed the
current value to 128, and got EBUSY when sending 128. When streaming
began, the host event carried 16 while GET_CUR returned 128.
A completion event would let userspace retry with its latest value. I
propose a new V4L2 event on the gadget device containing the USB
completion status. Is that the right interface for a follow-up version?
This patch reuses struct uvc_request_data for the ioctl. Would a
separate structure with reserved fields be preferable for this UAPI?
This RFC leaves the existing 32-bit SEND_RESPONSE path unchanged. A
single 32-bit gadget application still needs a separate compat fix to
answer control requests; that independent fix is planned as a normal
patch.
base-commit: 9357829ac604c28510b71b154cb6630af81226ff
Documentation/ABI/testing/uvc-gadget-status | 30 +++++++
Documentation/usb/gadget_uvc.rst | 24 ++++-
Documentation/userspace-api/ioctl/ioctl-number.rst | 1
MAINTAINERS | 1
drivers/usb/gadget/function/f_uvc.c | 89 +++++++++++++++++++-
drivers/usb/gadget/function/uvc.h | 8 ++
drivers/usb/gadget/function/uvc_v4l2.c | 19 ++++
include/uapi/linux/usb/g_uvc.h | 3 +
8 files changed, 165 insertions(+), 10 deletions(-)
diff --git a/Documentation/ABI/testing/uvc-gadget-status b/Documentation/ABI/testing/uvc-gadget-status
new file mode 100644
index 0000000..51b2250
--- /dev/null
+++ b/Documentation/ABI/testing/uvc-gadget-status
@@ -0,0 +1,30 @@
+What: /dev/video[0-9]+
+Date: December 2026
+Contact: linux-usb@vger.kernel.org
+Description:
+ UVCIOC_SEND_STATUS submits a status packet on the optional
+ VideoControl interrupt endpoint of a UVC gadget. The endpoint
+ must be enabled through the control/enable_interrupt_ep configfs
+ attribute before binding the gadget.
+
+ The argument is a struct uvc_request_data as defined in
+ include/uapi/linux/usb/g_uvc.h. The length field must be 1 to 16,
+ and the first length bytes of data are sent as a raw UVC status
+ packet. The driver does not validate the packet contents.
+
+ For example, if a Processing Unit with bUnitID 2 advertises
+ Brightness Control, the seven bytes 01 02 00 02 00 80 00
+ report a value change to 128. The userspace application must
+ also return the current value in subsequent GET_CUR requests.
+
+ A successful ioctl only means that the request was queued. It
+ does not report USB transfer completion or host receipt. Only one
+ packet can be pending; if the host is not polling the endpoint,
+ that packet may remain queued and later calls return -EBUSY.
+ A queued packet may reach the host after the control value has
+ changed again; GET_CUR must still return the current value.
+
+ The ioctl returns -EINVAL for an invalid length, -EOPNOTSUPP if
+ the interrupt endpoint was not configured, and -ENOTCONN if the
+ endpoint is disabled. Other USB request queue errors may also be
+ returned.
diff --git a/Documentation/usb/gadget_uvc.rst b/Documentation/usb/gadget_uvc.rst
index bf78fba..fe4facd 100644
--- a/Documentation/usb/gadget_uvc.rst
+++ b/Documentation/usb/gadget_uvc.rst
@@ -334,17 +334,29 @@ assign those strings as descriptors:
The interrupt endpoint
~~~~~~~~~~~~~~~~~~~~~~
-The VideoControl interface has an optional interrupt endpoint which is by default
-disabled. This is intended to support delayed response control set requests for
-UVC (which should respond through the interrupt endpoint rather than tying up
-endpoint 0). At present support for sending data through this endpoint is missing
-and so it is left disabled to avoid confusion. If you wish to enable it you can
-do so through the configfs attribute:
+The VideoControl interface has an optional interrupt endpoint which is disabled
+by default. It can report status changes to the host, such as a control value
+changed by the device. Enable the endpoint before binding the gadget:
.. code-block:: bash
echo 1 > $FUNCTION/control/enable_interrupt_ep
+Userspace can send a UVC status packet through this endpoint with the
+``UVCIOC_SEND_STATUS`` ioctl on the gadget's V4L2 device. The ioctl takes a
+``struct uvc_request_data`` whose ``length`` is between 1 and 16 bytes and
+whose ``data`` contains the packet. Userspace is responsible for constructing
+a valid UVC status packet; the driver does not inspect its fields. A successful
+ioctl means the request was queued, not that the host received it. Only one
+packet can be pending, and the ioctl returns ``-EBUSY`` until it completes.
+If the host is not polling the endpoint, the packet can remain pending.
+
+When reporting a control value change, userspace must return the current value
+through control requests such as ``GET_CUR``. The driver does not manage
+asynchronous controls. If userspace advertises one through ``GET_INFO``, it
+must send the corresponding Control Change status packet when ``SET_CUR``
+processing completes.
+
Bandwidth configuration
~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst
index dbe4620..8c52a01 100644
--- a/Documentation/userspace-api/ioctl/ioctl-number.rst
+++ b/Documentation/userspace-api/ioctl/ioctl-number.rst
@@ -209,6 +209,7 @@ Code Seq# Include File Comments
'U' all sound/asound.h conflict!
'U' 00-CF linux/uinput.h conflict!
'U' 00-EF linux/usbdevice_fs.h
+'U' 01-02 linux/usb/g_uvc.h conflict!
'U' C0-CF drivers/bluetooth/hci_uart.h
'V' all linux/vt.h conflict!
'V' all linux/videodev2.h conflict!
diff --git a/MAINTAINERS b/MAINTAINERS
index 84cb98e..c8ba8de 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -29207,6 +29207,7 @@ F: include/uapi/linux/uvcvideo.h
USB WEBCAM GADGET
L: linux-usb@vger.kernel.org
S: Orphan
+F: Documentation/ABI/testing/uvc-gadget-status
F: drivers/usb/gadget/function/*uvc*
F: drivers/usb/gadget/legacy/webcam.c
F: include/uapi/linux/usb/g_uvc.h
diff --git a/drivers/usb/gadget/function/f_uvc.c b/drivers/usb/gadget/function/f_uvc.c
index c7ce2ce..6cf7613 100644
--- a/drivers/usb/gadget/function/f_uvc.c
+++ b/drivers/usb/gadget/function/f_uvc.c
@@ -58,8 +58,6 @@ static struct usb_gadget_strings *uvc_function_strings[] = {
#define UVC_INTF_VIDEO_CONTROL 0
#define UVC_INTF_VIDEO_STREAMING 1
-#define UVC_STATUS_MAX_PACKET_SIZE 16 /* 16 bytes status */
-
static struct usb_interface_assoc_descriptor uvc_iad = {
.bLength = sizeof(uvc_iad),
.bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
@@ -220,6 +218,44 @@ uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
}
}
+/* usb_ep_disable() can complete a request while status_ep_lock is held. */
+static void
+uvc_function_status_complete(struct usb_ep *ep, struct usb_request *req)
+{
+ struct uvc_device *uvc = req->context;
+
+ complete(&uvc->status_ready);
+}
+
+int uvc_function_send_status(struct uvc_device *uvc, const u8 *data,
+ unsigned int length)
+{
+ struct usb_request *req = uvc->status_req;
+ unsigned long flags;
+ int ret;
+
+ if (length < 1 || length > UVC_STATUS_MAX_PACKET_SIZE)
+ return -EINVAL;
+ if (!uvc->enable_interrupt_ep)
+ return -EOPNOTSUPP;
+
+ spin_lock_irqsave(&uvc->status_ep_lock, flags);
+ if (!uvc->interrupt_ep->enabled) {
+ ret = -ENOTCONN;
+ } else if (!try_wait_for_completion(&uvc->status_ready)) {
+ ret = -EBUSY;
+ } else {
+ memcpy(req->buf, data, length);
+ req->length = length;
+ ret = usb_ep_queue(uvc->interrupt_ep, req, GFP_ATOMIC);
+ if (ret)
+ complete(&uvc->status_ready);
+ }
+ spin_unlock_irqrestore(&uvc->status_ep_lock, flags);
+
+ return ret;
+}
+
static int
uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
{
@@ -295,6 +331,7 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
struct usb_composite_dev *cdev = f->config->cdev;
struct v4l2_event v4l2_event;
struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
+ unsigned long flags;
int ret;
uvcg_info(f, "%s(%u, %u)\n", __func__, interface, alt);
@@ -305,14 +342,20 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
if (uvc->enable_interrupt_ep) {
uvcg_info(f, "reset UVC interrupt endpoint\n");
+ spin_lock_irqsave(&uvc->status_ep_lock, flags);
usb_ep_disable(uvc->interrupt_ep);
+ spin_unlock_irqrestore(&uvc->status_ep_lock, flags);
if (!uvc->interrupt_ep->desc)
if (config_ep_by_speed(cdev->gadget, f,
uvc->interrupt_ep))
return -EINVAL;
- usb_ep_enable(uvc->interrupt_ep);
+ spin_lock_irqsave(&uvc->status_ep_lock, flags);
+ ret = usb_ep_enable(uvc->interrupt_ep);
+ spin_unlock_irqrestore(&uvc->status_ep_lock, flags);
+ if (ret)
+ return ret;
}
if (uvc->state == UVC_STATE_DISCONNECTED) {
@@ -381,6 +424,7 @@ uvc_function_disable(struct usb_function *f)
{
struct uvc_device *uvc = to_uvc(f);
struct v4l2_event v4l2_event;
+ unsigned long flags;
uvcg_info(f, "%s()\n", __func__);
@@ -391,8 +435,11 @@ uvc_function_disable(struct usb_function *f)
uvc->state = UVC_STATE_DISCONNECTED;
usb_ep_disable(uvc->video.ep);
- if (uvc->enable_interrupt_ep)
+ if (uvc->enable_interrupt_ep) {
+ spin_lock_irqsave(&uvc->status_ep_lock, flags);
usb_ep_disable(uvc->interrupt_ep);
+ spin_unlock_irqrestore(&uvc->status_ep_lock, flags);
+ }
}
/* --------------------------------------------------------------------------
@@ -865,6 +912,23 @@ uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
uvc->control_req->complete = uvc_function_ep0_complete;
uvc->control_req->context = uvc;
+ if (uvc->enable_interrupt_ep) {
+ uvc->status_req = usb_ep_alloc_request(uvc->interrupt_ep,
+ GFP_KERNEL);
+ if (!uvc->status_req) {
+ ret = -ENOMEM;
+ goto error;
+ }
+ uvc->status_req->buf =
+ kmalloc(UVC_STATUS_MAX_PACKET_SIZE, GFP_KERNEL);
+ if (!uvc->status_req->buf) {
+ ret = -ENOMEM;
+ goto error;
+ }
+ uvc->status_req->complete = uvc_function_status_complete;
+ uvc->status_req->context = uvc;
+ }
+
if (v4l2_device_register(&cdev->gadget->dev, &uvc->v4l2_dev)) {
uvcg_err(f, "failed to register V4L2 device\n");
goto error;
@@ -888,6 +952,11 @@ v4l2_error:
uvcg_video_deinit(&uvc->video);
v4l2_device_unregister(&uvc->v4l2_dev);
error:
+ if (uvc->status_req) {
+ kfree(uvc->status_req->buf);
+ usb_ep_free_request(uvc->interrupt_ep, uvc->status_req);
+ uvc->status_req = NULL;
+ }
if (uvc->control_req) {
usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
uvc->control_req = NULL;
@@ -1076,6 +1145,15 @@ static void uvc_function_unbind(struct usb_configuration *c,
uvc->control_req = NULL;
kfree(uvc->control_buf);
uvc->control_buf = NULL;
+ if (uvc->status_req) {
+ /* Endpoint disable returns pending requests before unbind. */
+ wait_for_completion(&uvc->status_ready);
+ kfree(uvc->status_req->buf);
+ usb_ep_free_request(uvc->interrupt_ep, uvc->status_req);
+ uvc->status_req = NULL;
+ /* The same function object may be bound again. */
+ complete(&uvc->status_ready);
+ }
usb_free_all_descriptors(f);
}
@@ -1093,6 +1171,9 @@ static struct usb_function *uvc_alloc(struct usb_function_instance *fi)
mutex_init(&uvc->video.mutex);
mutex_init(&uvc->lock);
+ spin_lock_init(&uvc->status_ep_lock);
+ init_completion(&uvc->status_ready);
+ complete(&uvc->status_ready);
uvc->func_unbound = true;
uvc->state = UVC_STATE_DISCONNECTED;
init_waitqueue_head(&uvc->func_connected_queue);
diff --git a/drivers/usb/gadget/function/uvc.h b/drivers/usb/gadget/function/uvc.h
index 7abfdd5..2e0bc30 100644
--- a/drivers/usb/gadget/function/uvc.h
+++ b/drivers/usb/gadget/function/uvc.h
@@ -9,6 +9,7 @@
#ifndef _UVC_GADGET_H_
#define _UVC_GADGET_H_
+#include <linux/completion.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
@@ -175,6 +176,11 @@ struct uvc_device {
unsigned int control_intf;
struct usb_ep *interrupt_ep;
+ struct usb_request *status_req;
+ /* Serialize status queueing with endpoint reset and disable. */
+ spinlock_t status_ep_lock;
+ /* One token for the preallocated request and its completion callback. */
+ struct completion status_ready;
struct usb_request *control_req;
void *control_buf;
bool enable_interrupt_ep;
@@ -212,5 +218,7 @@ static inline struct uvc_file_handle *file_to_uvc_file_handle(struct file *filp)
extern void uvc_function_setup_continue(struct uvc_device *uvc, int disable_ep);
extern void uvc_function_connect(struct uvc_device *uvc);
extern void uvc_function_disconnect(struct uvc_device *uvc);
+int uvc_function_send_status(struct uvc_device *uvc, const u8 *data,
+ unsigned int length);
#endif /* _UVC_GADGET_H_ */
diff --git a/drivers/usb/gadget/function/uvc_v4l2.c b/drivers/usb/gadget/function/uvc_v4l2.c
index e9b9288..14e6269 100644
--- a/drivers/usb/gadget/function/uvc_v4l2.c
+++ b/drivers/usb/gadget/function/uvc_v4l2.c
@@ -6,6 +6,7 @@
* Laurent Pinchart (laurent.pinchart@ideasonboard.com)
*/
+#include <linux/compat.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/kernel.h>
@@ -635,11 +636,15 @@ uvc_v4l2_ioctl_default(struct file *file, void *fh, bool valid_prio,
{
struct video_device *vdev = video_devdata(file);
struct uvc_device *uvc = video_get_drvdata(vdev);
+ struct uvc_request_data *data = arg;
switch (cmd) {
case UVCIOC_SEND_RESPONSE:
return uvc_send_response(uvc, arg);
+ case UVCIOC_SEND_STATUS:
+ return uvc_function_send_status(uvc, data->data, data->length);
+
default:
return -ENOIOCTLCMD;
}
@@ -739,11 +744,25 @@ static unsigned long uvcg_v4l2_get_unmapped_area(struct file *file,
}
#endif
+#ifdef CONFIG_COMPAT
+static long uvc_v4l2_compat_ioctl32(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ if (cmd != UVCIOC_SEND_STATUS)
+ return -ENOIOCTLCMD;
+
+ return video_ioctl2(file, cmd, (unsigned long)compat_ptr(arg));
+}
+#endif
+
const struct v4l2_file_operations uvc_v4l2_fops = {
.owner = THIS_MODULE,
.open = uvc_v4l2_open,
.release = uvc_v4l2_release,
.unlocked_ioctl = video_ioctl2,
+#ifdef CONFIG_COMPAT
+ .compat_ioctl32 = uvc_v4l2_compat_ioctl32,
+#endif
.mmap = uvc_v4l2_mmap,
.poll = uvc_v4l2_poll,
#ifndef CONFIG_MMU
diff --git a/include/uapi/linux/usb/g_uvc.h b/include/uapi/linux/usb/g_uvc.h
index 8d7824d..cfe290b 100644
--- a/include/uapi/linux/usb/g_uvc.h
+++ b/include/uapi/linux/usb/g_uvc.h
@@ -38,5 +38,8 @@ struct uvc_event {
};
#define UVCIOC_SEND_RESPONSE _IOW('U', 1, struct uvc_request_data)
+#define UVCIOC_SEND_STATUS _IOW('U', 2, struct uvc_request_data)
+
+#define UVC_STATUS_MAX_PACKET_SIZE 16
#endif /* __LINUX_USB_G_UVC_H */
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [RFC PATCH] usb: gadget: uvc: allow userspace to send status packets
2026-12-04 10:25 [RFC PATCH] usb: gadget: uvc: allow userspace to send status packets Keisuke Tsukuda
@ 2026-12-04 10:54 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 10:54 UTC (permalink / raw)
To: Keisuke Tsukuda; +Cc: linux-usb@vger.kernel.org
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] usb: gadget: uvc: stale interrupt_ep->desc on SuperSpeed
reconnect
- [Medium] usb: gadget: uvc: non-extensible UVCIOC_SEND_STATUS UAPI and
no completion
- [Medium] usb: gadget: uvc: unbind hangs on status_ready without
disabling interrupt_ep
- [Medium] usb: uvc: uvc_function_set_alt() skips config_ep_by_speed()
on stale desc
- [Medium] usb: gadget: uvc: uvc_function_unbind() waits without
disabling interrupt_ep
--
commit 740d5c5287a2ae908ef90a9a8ab7b974a03848e7
Author: Keisuke Tsukuda <tkdkei@outlook.jp>
usb: gadget: uvc: allow userspace to send status packets
This commit adds the UVCIOC_SEND_STATUS ioctl and its 32-bit compat handler
so userspace can queue raw status packets on the optional VideoControl
interrupt endpoint. It preallocates a single status request, serializes
queueing with endpoint enable and disable, propagates usb_ep_enable()
errors, and waits for any pending request during unbind.
> diff --git a/drivers/usb/gadget/function/f_uvc.c b/drivers/usb/gadget/function/f_uvc.c
> index c7ce2ce1d5db5..6cf76134853fb 100644
> --- a/drivers/usb/gadget/function/f_uvc.c
> +++ b/drivers/usb/gadget/function/f_uvc.c
[ ... ]
> @@ -305,14 +342,20 @@ uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
>
> if (uvc->enable_interrupt_ep) {
> uvcg_info(f, "reset UVC interrupt endpoint\n");
> + spin_lock_irqsave(&uvc->status_ep_lock, flags);
> usb_ep_disable(uvc->interrupt_ep);
> + spin_unlock_irqrestore(&uvc->status_ep_lock, flags);
>
> if (!uvc->interrupt_ep->desc)
> if (config_ep_by_speed(cdev->gadget, f,
> uvc->interrupt_ep))
> return -EINVAL;
>
> - usb_ep_enable(uvc->interrupt_ep);
> + spin_lock_irqsave(&uvc->status_ep_lock, flags);
> + ret = usb_ep_enable(uvc->interrupt_ep);
> + spin_unlock_irqrestore(&uvc->status_ep_lock, flags);
> + if (ret)
> + return ret;
> }
[Severity: High]
Can propagating the error from usb_ep_enable() here fail SET_CONFIGURATION
when reconnecting at SuperSpeed after a Full-Speed or High-Speed session?
On UDCs whose ep->ops->disable() does not clear usb_ep->desc (such as cdnsp
and tegra-xudc), uvc->interrupt_ep->desc remains non-NULL after disconnect,
so config_ep_by_speed() is skipped and uvc->interrupt_ep->comp_desc stays
NULL. During usb_ep_enable(), cdnsp_get_endpoint_max_burst() dereferences
NULL comp_desc->bMaxBurst, while __tegra_xudc_ep_enable() returns -EINVAL
which now aborts uvc_function_set_alt() and fails enumeration. Calling
config_ep_by_speed() unconditionally, as uvc->video.ep already does, avoids
both failures.
[ ... ]
> @@ -391,8 +435,11 @@ uvc_function_disable(struct usb_function *f)
> uvc->state = UVC_STATE_DISCONNECTED;
>
> usb_ep_disable(uvc->video.ep);
> - if (uvc->enable_interrupt_ep)
> + if (uvc->enable_interrupt_ep) {
> + spin_lock_irqsave(&uvc->status_ep_lock, flags);
> usb_ep_disable(uvc->interrupt_ep);
> + spin_unlock_irqrestore(&uvc->status_ep_lock, flags);
> + }
> }
[Severity: Medium]
Should uvc_function_disable() also clear uvc->interrupt_ep->desc (and
comp_desc) after disabling the endpoint?
Because core usb_ep_disable() and UDC drivers such as tegra-xudc and dwc2
leave usb_ep->desc populated, a multi-configuration gadget that shares the
same hardware endpoint across configurations can re-enable interrupt_ep
with the previous configuration's stale descriptor, or fail at SuperSpeed
with a stale NULL comp_desc. This matches the issue fixed for f_ecm and
f_rndis in commit e5b5da96da50 ("usb: gadget: fix wrong endpoint desc").
[ ... ]
> @@ -1076,6 +1145,15 @@ static void uvc_function_unbind(struct usb_configuration *c,
> uvc->control_req = NULL;
> kfree(uvc->control_buf);
> uvc->control_buf = NULL;
> + if (uvc->status_req) {
> + /* Endpoint disable returns pending requests before unbind. */
> + wait_for_completion(&uvc->status_ready);
> + kfree(uvc->status_req->buf);
> + usb_ep_free_request(uvc->interrupt_ep, uvc->status_req);
> + uvc->status_req = NULL;
> + /* The same function object may be bound again. */
> + complete(&uvc->status_ready);
> + }
[Severity: Medium]
Can wait_for_completion(&uvc->status_ready) hang forever here if an
in-flight setup request re-enables uvc->interrupt_ep during configfs
unbind?
Unlike legacy usb_remove_function(), configfs purge_configs_funcs() invokes
f->unbind() without calling f->disable(). On UDCs that do not implement
udc_async_callbacks or set gadget->irq (such as tegra-xudc and
renesas_usb3), an in-flight setup callback can run between
usb_gadget_disconnect_locked() and gi->unbind = 1:
CPU0 (unbind) CPU1 (EP0 setup / userspace)
gadget_unbind_driver()
usb_gadget_disconnect_locked()
uvc_function_disable()
configfs_composite_setup()
uvc_function_set_alt()
usb_ep_enable(interrupt_ep)
configfs_composite_unbind()
gi->unbind = 1
purge_configs_funcs()
uvc_function_unbind()
uvc_function_send_status()
try_wait_for_completion(&status_ready)
usb_ep_queue(interrupt_ep, status_req)
wait_for_completion(&vdev_release_done)
wait_for_completion(&status_ready) <-- hangs forever
Because usb_gadget_udc_stop_locked() cannot run until uvc_function_unbind()
returns, status_req is never completed and unbind wedges in uninterruptible
sleep while holding configfs and gadget device locks.
[Severity: Medium]
Should uvc_function_unbind() explicitly disable uvc->interrupt_ep under
uvc->status_ep_lock before waiting on uvc->status_ready?
On UDCs such as musb (where musb_gadget_pullup() defers pullup disable to
delayed work without stopping setup IRQs) and chipidea (where
isr_setup_packet_handler() drops ci->lock before driver->setup() and
ci_udc_pullup(0) does not synchronize IRQs), a concurrent setup request can
re-enable uvc->interrupt_ep after usb_gadget_disconnect_locked(), allowing
UVCIOC_SEND_STATUS to queue status_req while uvc_function_unbind() waits
for the V4L2 device to close. Calling usb_ep_disable(uvc->interrupt_ep)
under status_ep_lock after wait_for_completion(&vdev_release_done) cancels
any pending request before wait_for_completion(&uvc->status_ready).
> diff --git a/include/uapi/linux/usb/g_uvc.h b/include/uapi/linux/usb/g_uvc.h
> index 8d7824dde1b2f..cfe290b3063a6 100644
> --- a/include/uapi/linux/usb/g_uvc.h
> +++ b/include/uapi/linux/usb/g_uvc.h
> @@ -38,5 +38,8 @@ struct uvc_event {
> };
>
> #define UVCIOC_SEND_RESPONSE _IOW('U', 1, struct uvc_request_data)
> +#define UVCIOC_SEND_STATUS _IOW('U', 2, struct uvc_request_data)
> +
> +#define UVC_STATUS_MAX_PACKET_SIZE 16
>
> #endif /* __LINUX_USB_G_UVC_H */
[Severity: Medium]
Should UVCIOC_SEND_STATUS use a dedicated extensible struct instead of
reusing struct uvc_request_data?
struct uvc_request_data uses a signed __s32 length specifically for
UVCIOC_SEND_RESPONSE's EP0 stall convention and has no flags or reserved
fields for future extensions without adding another ioctl. Exporting
UVC_STATUS_MAX_PACKET_SIZE as 16 also locks the driver's current hardcoded
wMaxPacketSize into permanent UAPI, even though UVC status packets can
exceed 16 bytes if made configurable.
In addition, uvc_function_status_complete() currently discards req->status
without notifying userspace, so callers that receive -EBUSY while a host
(such as an idle Linux 6.16+ uvcvideo host) is not polling the interrupt
endpoint have no event-driven signal to resend the latest control value.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204102506.98205-1-tkdkei@outlook.jp?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 10:54 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 10:25 [RFC PATCH] usb: gadget: uvc: allow userspace to send status packets Keisuke Tsukuda
2026-12-04 10:54 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox