Skip to content

Commit 54fb44b

Browse files
drivers/usbdev: add UVC gadget class driver
Add USB Video Class 1.1 gadget driver supporting Bulk transport with uncompressed YUY2 video streaming. Resolution and frame interval are negotiated dynamically via PROBE/COMMIT control. - uvc.h: protocol constants, streaming control struct, public API - uvc.c: class driver with PROBE/COMMIT, bulk EP, /dev/uvc0 chardev - Kconfig/Make.defs: USBUVC config and build rules - boardctl.c: BOARDIOC_USBDEV_UVC standalone init path Hardened against host disconnect: - Removed nxmutex_lock from USB interrupt context paths - Added 30s semaphore timeout in uvc_write with EP_CANCEL fallback - Drain stale wrsem counts in VS_COMMIT before new stream - Guard uvc_streaming_stop() against double EP_CANCEL race - Handle EP_SUBMIT returning -ESHUTDOWN gracefully Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
1 parent 2c64005 commit 54fb44b

7 files changed

Lines changed: 1990 additions & 0 deletions

File tree

boards/boardctl.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
# include <nuttx/usb/cdcacm.h>
5757
# include <nuttx/usb/pl2303.h>
5858
# include <nuttx/usb/usbmsc.h>
59+
# include <nuttx/usb/uvc.h>
5960
# include <nuttx/usb/composite.h>
6061
#endif
6162

@@ -206,6 +207,44 @@ static inline int
206207
break;
207208
#endif
208209

210+
#if defined(CONFIG_USBUVC) && !defined(CONFIG_USBUVC_COMPOSITE)
211+
case BOARDIOC_USBDEV_UVC: /* UVC class */
212+
switch (ctrl->action)
213+
{
214+
case BOARDIOC_USBDEV_INITIALIZE: /* Initialize UVC device */
215+
break;
216+
217+
case BOARDIOC_USBDEV_CONNECT: /* Connect the UVC device */
218+
{
219+
FAR const struct uvc_params_s *params;
220+
221+
DEBUGASSERT(ctrl->handle != NULL);
222+
223+
/* Application passes video params via *handle */
224+
225+
params = (FAR const struct uvc_params_s *)*ctrl->handle;
226+
*ctrl->handle = usbdev_uvc_initialize(params);
227+
if (*ctrl->handle == NULL)
228+
{
229+
ret = -EIO;
230+
}
231+
}
232+
break;
233+
234+
case BOARDIOC_USBDEV_DISCONNECT: /* Disconnect the UVC device */
235+
{
236+
DEBUGASSERT(ctrl->handle != NULL && *ctrl->handle != NULL);
237+
usbdev_uvc_uninitialize(*ctrl->handle);
238+
}
239+
break;
240+
241+
default:
242+
ret = -EINVAL;
243+
break;
244+
}
245+
break;
246+
#endif
247+
209248
#ifdef CONFIG_USBDEV_COMPOSITE
210249
case BOARDIOC_USBDEV_COMPOSITE: /* Composite device */
211250
switch (ctrl->action)

drivers/usbdev/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ if(CONFIG_USBDEV)
6161
list(APPEND SRCS mtp.c)
6262
endif()
6363

64+
if(CONFIG_USBUVC)
65+
list(APPEND SRCS uvc.c)
66+
endif()
67+
6468
if(CONFIG_NET_CDCECM)
6569
list(APPEND SRCS cdcecm.c)
6670
endif()

drivers/usbdev/Kconfig

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,47 @@ config USBADB_INTERFACESTR
948948

949949
endif # USBADB
950950

951+
menuconfig USBUVC
952+
bool "USB Video Class (UVC) gadget support"
953+
default n
954+
depends on USBDEV
955+
---help---
956+
Enables USB Video Class (UVC) gadget device support.
957+
The device appears as a USB webcam to the host.
958+
959+
if USBUVC
960+
961+
config USBUVC_COMPOSITE
962+
bool "UVC composite support"
963+
default n
964+
depends on USBDEV_COMPOSITE
965+
---help---
966+
Configure the UVC driver as part of a composite USB device.
967+
When enabled, the UVC gadget can be combined with other USB
968+
class drivers (e.g., CDC/ACM) in a single composite device.
969+
970+
if !USBUVC_COMPOSITE
971+
972+
config USBUVC_EPBULKIN
973+
int "Bulk IN endpoint number"
974+
default 1
975+
976+
config USBUVC_EP0MAXPACKET
977+
int "Max packet size for EP0"
978+
default 64
979+
980+
endif # !USBUVC_COMPOSITE
981+
982+
config USBUVC_EPBULKIN_FSSIZE
983+
int "Bulk IN full-speed max packet size"
984+
default 64
985+
986+
config USBUVC_NWRREQS
987+
int "Number of write requests"
988+
default 4
989+
990+
endif # USBUVC
991+
951992
menuconfig USBMSC
952993
bool "USB Mass storage class device"
953994
default n

drivers/usbdev/Make.defs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ ifeq ($(CONFIG_USBADB),y)
5656
CSRCS += adb.c
5757
endif
5858

59+
ifeq ($(CONFIG_USBUVC),y)
60+
CSRCS += uvc.c
61+
endif
62+
5963
ifeq ($(CONFIG_USBMTP),y)
6064
CSRCS += mtp.c
6165
endif

0 commit comments

Comments
 (0)