# wlhe/uvc-gadget architecture

> Architecture diagram of the GitHub repository wlhe/uvc-gadget, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wlhe/uvc-gadget
- Repository: https://github.com/wlhe/uvc-gadget

## Overview

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`](https://github.com/wlhe/uvc-gadget/blob/master/uvc-gadget.c) (uvc-gadget), [`doc/configfs_start.txt`](https://github.com/wlhe/uvc-gadget/blob/master/doc/configfs_start.txt) (configfs setup), [`README`](https://github.com/wlhe/uvc-gadget/tree/master/README) (Project docs), [`Makefile`](https://github.com/wlhe/uvc-gadget/tree/master/Makefile) (Build system)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g1["Userspace app"]
  node_n1["uvc-gadget<br/>userspace bridge<br/>[uvc-gadget.c]"]
  node_n2["CLI config<br/>runtime options<br/>[uvc-gadget.c]"]
  node_n3["Capture ingest<br/>v4l2 input<br/>[uvc-gadget.c]"]
  node_n4["Stream loop<br/>pipeline core<br/>[uvc-gadget.c]"]
  node_n5["UVC output<br/>usb gadget io<br/>[uvc-gadget.c]"]
  node_n6["uvc.h<br/>local header<br/>[uvc.h]"]
end

subgraph group_g2["Kernel boundary"]
  node_n7[("V4L2 device<br/>capture device")]
  node_n8(("Synthetic source<br/>test input"))
  node_n9{{"V4L2 API<br/>kernel interface"}}
  node_n10{{"USB gadget<br/>kernel subsystem"}}
  node_n11{{"UVC function<br/>kernel function"}}
  node_n12[("USB host<br/>external host")]
end

subgraph group_g3["Setup docs"]
  node_n13["configfs setup<br/>gadget setup<br/>[configfs_start.txt]"]
  node_n14["gadget config<br/>setup guide"]
  node_n15["gadget testing<br/>validation guide<br/>[gadget-testing.txt]"]
end

subgraph group_g4["Build and usage"]
  node_n16["Build system<br/>make build"]
  node_n17["Project docs<br/>usage docs"]
end

node_n2 -->|"configures"| node_n1
node_n1 -->|"reads frames"| node_n3
node_n3 -->|"captures from"| node_n7
node_n3 -->|"falls back to"| node_n8
node_n3 -->|"uses"| node_n9
node_n1 -->|"drives"| node_n4
node_n4 -->|"hands off"| node_n5
node_n5 -->|"streams to"| node_n11
node_n11 -->|"exposed by"| node_n10
node_n10 -->|"presents as camera"| node_n12
node_n13 -->|"configures"| node_n10
node_n13 -->|"enables"| node_n11
node_n14 -.->|"documents"| node_n13
node_n15 -.->|"validates"| node_n13
node_n6 -->|"build dependency"| node_n1
node_n16 -->|"builds"| node_n1
node_n17 -.->|"describes"| node_n16
node_n17 -.->|"explains"| node_n1

click node_n1 "https://github.com/wlhe/uvc-gadget/blob/master/uvc-gadget.c"
click node_n2 "https://github.com/wlhe/uvc-gadget/blob/master/uvc-gadget.c"
click node_n3 "https://github.com/wlhe/uvc-gadget/blob/master/uvc-gadget.c"
click node_n4 "https://github.com/wlhe/uvc-gadget/blob/master/uvc-gadget.c"
click node_n5 "https://github.com/wlhe/uvc-gadget/blob/master/uvc-gadget.c"
click node_n6 "https://github.com/wlhe/uvc-gadget/blob/master/uvc.h"
click node_n13 "https://github.com/wlhe/uvc-gadget/blob/master/doc/configfs_start.txt"
click node_n14 "https://github.com/wlhe/uvc-gadget/blob/master/doc/gadget_configfs.txt"
click node_n15 "https://github.com/wlhe/uvc-gadget/blob/master/doc/gadget-testing.txt"
click node_n16 "https://github.com/wlhe/uvc-gadget/tree/master/Makefile"
click node_n17 "https://github.com/wlhe/uvc-gadget/tree/master/README"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n1,node_n2,node_n3,node_n4,node_n5,node_n6 toneBlue
class node_n7,node_n8,node_n9,node_n10,node_n11,node_n12 toneAmber
class node_n13,node_n14,node_n15 toneMint
class node_n16,node_n17 toneRose
```

## Components

### Userspace app

Main executable and its local build-time header

- [uvc-gadget](https://github.com/wlhe/uvc-gadget/blob/master/uvc-gadget.c): userspace bridge · Main executable that connects a video source to a USB UVC gadget · `uvc-gadget.c`
- [CLI config](https://github.com/wlhe/uvc-gadget/blob/master/uvc-gadget.c): runtime options · Parses format, resolution, transport, buffering, and I/O mode settings · `uvc-gadget.c`
- [Capture ingest](https://github.com/wlhe/uvc-gadget/blob/master/uvc-gadget.c): v4l2 input · Opens and reads frames from a V4L2 capture device or synthetic source · `uvc-gadget.c`
- [Stream loop](https://github.com/wlhe/uvc-gadget/blob/master/uvc-gadget.c): pipeline core · Coordinates buffer queueing, events, and frame handoff between sides · `uvc-gadget.c`
- [UVC output](https://github.com/wlhe/uvc-gadget/blob/master/uvc-gadget.c): usb gadget io · Packages and sends frames to the gadget streaming endpoint · `uvc-gadget.c`
- [uvc.h](https://github.com/wlhe/uvc-gadget/blob/master/uvc.h): local header · Copied UVC definitions used for build-time compatibility · `uvc.h`

### Kernel boundary

Linux capture and USB gadget interfaces

- **V4L2 device**: capture device · Physical or virtual camera source consumed by the app
- **Synthetic source**: test input · Fallback path used when real capture is disabled
- **V4L2 API**: kernel interface · Linux video capture API used for buffer and format negotiation
- **USB gadget**: kernel subsystem · Linux USB gadget framework that exposes the device to a host
- **UVC function**: kernel function · UVC gadget function configured through configfs and driven by the app
- **USB host**: external host · Connected computer that sees the gadget as a camera

### Setup docs

Gadget configuration and testing guidance

- [configfs setup](https://github.com/wlhe/uvc-gadget/blob/master/doc/configfs_start.txt): gadget setup · Prepares the USB gadget and UVC function before streaming starts · `doc/configfs_start.txt`
- [gadget config](https://github.com/wlhe/uvc-gadget/blob/master/doc/gadget_configfs.txt): setup guide · Documents configfs-based gadget composition and parameters · `doc/gadget_configfs.txt`
- [gadget testing](https://github.com/wlhe/uvc-gadget/blob/master/doc/gadget-testing.txt): validation guide · Documents validation and test procedures for the gadget path · `doc/gadget-testing.txt`

### Build and usage

Makefile and repository documentation

- [Build system](https://github.com/wlhe/uvc-gadget/tree/master/Makefile): make build · Native and cross-build entrypoint with kernel directory settings · `Makefile`
- [Project docs](https://github.com/wlhe/uvc-gadget/tree/master/README): usage docs · README files describing build flags, usage, and runtime options · `README`

## Connections

- 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
