How wlhe/uvc-gadget works
1. What this repo is
- A small Linux userspace application, uvc-gadget, that turns a device into a USB Video Class (UVC) camera gadget.
- It is not a framework or service platform; the architecture is centered on one executable plus a few support docs.
2. Core executable
- uvc-gadget.c is the main implementation and likely contains:
- command-line parsing,
- USB gadget / UVC setup,
- video capture setup,
- streaming loop,
- buffer management and event handling.
- uvc.h is a copied UVC header used to build the app without depending directly on the kernel tree at build time.
3. Primary architecture boundary
- The important system boundary is between userspace and the Linux kernel:
- userspace app controls the pipeline,
- kernel provides V4L2 capture and USB gadget/UVC function support.
- The app bridges a local video source to a USB endpoint visible to a host PC.
4. Main data flow
- Video frames come from one of two sources:
- a real V4L2 capture device (-v device), or
- a synthetic / test path when -d disables real capture device use.
- Frames are then packaged and streamed to the UVC output device (-u device) over USB.
- In practice, the app acts as a relay from capture pipeline to gadget streaming endpoint.
5. Runtime configuration and modes
- Command-line options define the streaming profile:
- frame format: YUYV or MJPEG,
- resolution: 360p/VGA or 720p/WXGA,
- USB speed: Full/High/SuperSpeed,
- transport type: bulk or isochronous,
- burst/mult settings,
- buffer count and I/O method.
- These options materially change the USB/video negotiation and buffering behavior.
6. Video pipeline concerns
- The repo’s architecture is heavily shaped by V4L2 buffer handling:
- MMAP or USER_PTR I/O modes,
- multiple queued buffers,
- support for MJPEG images and live capture input.
- This suggests a tight event-driven loop where capture buffers are filled, then handed off to the UVC side.
7. USB gadget / UVC concerns
- The gadget side depends on Linux USB gadget configuration, especially UVC function setup through configfs.
- The docs (doc/configfs_start.txt, doc/gadget_configfs.txt) indicate that gadget creation is an explicit setup step outside the app itself.
- doc/gadget-testing.txt appears to document validation and test procedures for the gadget path.
8. Supporting documentation
- README / Readme.md describe usage, build flags, and command-line parameters.
- The doc/ directory provides operational knowledge for starting configfs-based gadgets and testing them.
- This repo therefore separates implementation from kernel/gadget setup instructions.
9. Build and portability
- Makefile is the only build tooling shown; it supports native build and cross-compilation via ARCH, CROSS_COMPILE, and KERNEL_DIR.
- That indicates the target environment is typically an embedded Linux system rather than a desktop app.
- The code likely depends on Linux kernel headers and V4L2/USB gadget APIs.
10. External dependencies
- The app depends on:
- a Linux kernel with UVC gadget support,
- V4L2 capture support,
- configfs-based gadget configuration,
- a connected USB host for validation.
- The README’s upstream references show this is a fork of the ideasonboard uvc-gadget project with applied enhancement and bugfix patchsets.
11. How to diagram it
- Draw the app in the middle as a userspace bridge.
- On the left, show a V4L2 camera/capture device feeding frames into the app.
- On the right, show the Linux USB gadget/UVC function exposing a virtual camera to the USB host.
- Below or alongside, show configfs/kernel setup as the control plane that enables the UVC gadget endpoint before streaming begins.
12. Repo shape summary
- This repository is intentionally compact:
- one main C program,
- one local header,
- build file,
- operational docs.
- Its architecture is therefore dominated by data movement and device control, not by internal modular decomposition.
Main entry points: uvc-gadget.c uvc-gadget · configfs_start.txt configfs setup · README Project docs · Makefile Build system
Components 17
Userspace app
Main executable and its local build-time header
- uvc-gadget · Main executable that connects a video source to a USB UVC gadget
uvc-gadget.c - CLI config · Parses format, resolution, transport, buffering, and I/O mode settings
uvc-gadget.c - Capture ingest · Opens and reads frames from a V4L2 capture device or synthetic source
uvc-gadget.c - Stream loop · Coordinates buffer queueing, events, and frame handoff between sides
uvc-gadget.c - UVC output · Packages and sends frames to the gadget streaming endpoint
uvc-gadget.c - uvc.h · Copied UVC definitions used for build-time compatibility
uvc.h
Kernel boundary
Linux capture and USB gadget interfaces
- V4L2 device · Physical or virtual camera source consumed by the app
- Synthetic source · Fallback path used when real capture is disabled
- V4L2 API · Linux video capture API used for buffer and format negotiation
- USB gadget · Linux USB gadget framework that exposes the device to a host
- UVC function · UVC gadget function configured through configfs and driven by the app
- USB host · Connected computer that sees the gadget as a camera
Setup docs
Gadget configuration and testing guidance
- configfs setup · Prepares the USB gadget and UVC function before streaming starts
doc/configfs_start.txt - gadget config · Documents configfs-based gadget composition and parameters
doc/gadget_configfs.txt - gadget testing · Documents validation and test procedures for the gadget path
doc/gadget-testing.txt
Build and usage
Makefile and repository documentation
- Build system · Native and cross-build entrypoint with kernel directory settings
Makefile - Project docs · README files describing build flags, usage, and runtime options
README
Connections 18
- CLI config → uvc-gadget: configures · Command-line options shape runtime behavior
- uvc-gadget → Capture ingest: reads frames · Main program pulls video from the selected input source
- Capture ingest → V4L2 device: captures from · Uses a V4L2 capture device when enabled
- Capture ingest → Synthetic source: falls back to · Uses a synthetic or test source when capture is disabled
- Capture ingest → V4L2 API: uses · Interacts with the Linux V4L2 API
- uvc-gadget → Stream loop: drives · Event loop coordinates buffering and transfer
- Stream loop → UVC output: hands off · Processed frames are delivered to the UVC output side
- UVC output → UVC function: streams to · Frames are sent into the UVC gadget function
- UVC function → USB gadget: exposed by · UVC function is part of the USB gadget subsystem
- USB gadget → USB host: presents as camera · The host sees the gadget as a USB camera device
- configfs setup → USB gadget: configures · Configfs setup creates the gadget shell and links the function
- configfs setup → UVC function: enables · Configfs binds the UVC function into the gadget
- gadget config → configfs setup: documents · Operational guide for gadget configuration
- gadget testing → configfs setup: validates · Testing guide covers the configured gadget path
- uvc.h → uvc-gadget: build dependency · Local UVC header supplies needed definitions
- Build system → uvc-gadget: builds · Makefile compiles the executable
- Project docs → Build system: describes · Docs explain build and runtime usage
- Project docs → uvc-gadget: explains · README covers command-line options and behavior