Commit graph zephyr/subsys/usb
Author SHA1 Message Date
Aiden Hu
d820b0bd6a usb: host: uvc: implement USB host UVC class with video API
Add a USB Video Class (UVC) host class implementation that enables
Zephyr-based systems to act as USB hosts for standard UVC cameras.
This allows applications to capture video streams from USB cameras
using the standard Zephyr video API.

The implementation follows the UVC specification for device enumeration,
format negotiation, and stream control. It handles the complexity of UVC
descriptor parsing, bandwidth management, and isochronous transfer
scheduling, presenting a clean video device interface to applications.

Support for common camera controls such as brightness, contrast,
exposure, focus, and zoom allows applications to adjust camera settings
at runtime. Dynamic format and frame rate changes are also supported,
making it suitable for various use cases from simple video capture to
more advanced computer vision applications.

Signed-off-by: Aiden Hu <weiwei.hu@nxp.com>
2026-03-21 00:46:16 +00:00
Aiden Hu
ee9c2221ee usb: uvc: add UVC_MAX_CTRL_SIZE macro
This macro is used for the data size of uvc control.

Signed-off-by: Aiden Hu <weiwei.hu@nxp.com>
2026-03-21 00:46:16 +00:00
Tomasz Moń
188118f562 usb: device_next: Preallocate SETUP buffer
Preallocate and keep reusing the same buffer for SETUP data. This
ensures that USB stack will always be able to handle SETUP stage
regardless of UDC memory pool usage.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2026-03-19 16:58:56 +01:00
Tomasz Moń
244b015e99 usb: device_next: Allocate control buffers in stack
Allocating buffer in response to control transfer is part of processing
and therefore should not be done in UDC driver but rather in the stack.

Simplify UDC driver design by moving all control transfer buffer
allocations and processing to USB stack.

New control transfer handling flow is as follows:
  1. USB stack allocates and queues buffer for Setup stage. This informs
     UDC driver that USB stack is ready to process new transfer.
  2. UDC driver completes enqueued Setup buffer. SETUP data may have
     been received by device before Setup buffer was enqueued.
     If multiple SETUP data was received, UDC driver must complete
     request using last received SETUP data.
  3. USB stack processes request. USB stack is responsible for:
       * Stalling control endpoint when request cannot be processed
       * Allocating and enqueueing Data stage buffer if necessary
           - Depending on enqueued endpoint (IN/OUT), UDC driver is
	     expected to complete the transfer. If host sends new SETUP
             data for any reason (e.g. timeout), then Data stage buffer
             must be completed with -ECONNRESET code.
           - UDC driver may postpone processing Data IN until USB stack
	     enqueues new Setup stage buffer.
       * Handling status stage if applicable
           - If Data IN was enqueued, stack immediately allocates and
	     enqueues Status OUT stage buffer.
           - If Data OUT was enqueued, stack waits for Data OUT buffer
	     completion before doing any further processing.
           - If control transfer handling fails, stack is expected to
             STALL control endpoint.
       * Allocating and enqueuing Setup buffer.
           - This informs UDC driver that stack has finished processing
	     control transfer. UDC driver may choose to start processing
             Data IN and/or Status OUT only after new Setup buffer is
             enqueued.
  4. UDC driver must fail (complete with -ECONNRESET) any enqueued and
     not completed Data and Status buffers if host sends new SETUP data.
     UDC driver must be able to buffer last received SETUP data until
     USB stack is ready (enqueues Setup buffer).
  5. UDC driver must complete all Data and Status buffers before it
     completes Setup buffer.
  6. UDC driver may keep ownership of Setup, Data and/or Status buffers
     across USB bus resets. USB stack does not attempt to dequeue any
     control transfer buffer it enqueued.

This approach implicitly synchronizes UDC driver against USB stack,
which ensures that only one set of Setup/Data/Status buffers is
allocated at a time.

Another advantage of the rework is drawing a clear line on buffer
ownership. The buffer responsibilities are as follows:
  * USB stack is the only entity that both allocates and frees the
    buffers. Only USB stack is allowed to set "setup", "data" and
    "status" fields in struct udc_buf_info.
  * UDC takes ownership of buffers handed to it in udc_ep_enqueue().
  * UDC releases buffer ownership by calling udc_submit_ep_event().

Because there is just a single place where the buffer ownership changes,
and all buffers must go USB stack (alloc) -> UDC (perform requests on
the bus) -> USB stack (free) route it would be possible in the future to
implement a "tap" similar to Linux usbmon that would ease debugging.

This commit significantly changes how USB stack communicates with UDC
drivers. It was decided that supporting both the old and new model
simultaneously would require way too much effort. Therefore all UDC
drivers were reworked. Following people worked on driver rework:
  * Tomasz Moń - ambiq, dwc2, kinetis, mcux ip3511, nrf, numaker,
                 renesas ra, rpi pico, smartbond, virtual
  * Mathieu Choplain - stm32
  * Mark Wang - mcux ehci
  * Johann Fischer - sam0, stm32
  * Ren Chen - it82xx2
  * Brandon Hurst - max32
  * Gerson Fernando Budke - sam udp, sam usbc, sam usbhs

Co-authored-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
Co-authored-by: Johann Fischer <johann.fischer@nordicsemi.no>
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
Co-authored-by: Mark Wang <yichang.wang@nxp.com>
Signed-off-by: Mark Wang <yichang.wang@nxp.com>
Co-authored-by: Ren Chen <Ren.Chen@ite.com.tw>
Signed-off-by: Ren Chen <Ren.Chen@ite.com.tw>
Co-authored-by: Brandon Hurst <brandon.hurst@analog.com>
Signed-off-by: Brandon Hurst <brandon.hurst@analog.com>
Co-authored-by: Gerson Fernando Budke <nandojve@gmail.com>
Signed-off-by: Gerson Fernando Budke <nandojve@gmail.com>
Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2026-03-19 16:58:56 +01:00
Jamie McCrae
f22592cbc5 tree: Replace FIXED_PARTITION_* macro usage with PARTITION_*
Replaces usage of these deprecated macros with ones that support
fixed and mapped partition compatibles. Also includes an update to
hal_espressif which also (rightly or wrongly) has zephyr specific
code in it

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
2026-03-17 18:24:52 -04:00
Roman Leonov
638731f438 drivers: usb: host: Fixed small typo in description
Fixed small typo in doxygen description interace -> interface.

Signed-off-by: Roman Leonov <jam_roma@yahoo.com>
2026-03-17 18:18:49 -04:00
Aiden Hu
fb3ae541cf usb: host: refine connect/disconnect handling
The attach and detach flow was previously scattered across bus event
handlers and class code, causing duplicated enumeration and class
probe/remove logic. Root-device state was also manipulated directly in
the host context, which made lifecycle handling implicit and limited
the ability to support devices behind a hub.

This update introduces common helpers for device attach and detach and
replaces direct root access with simple accessor functions. By unifying
enumeration and teardown into a single path, the control flow becomes
clearer and more consistent, and the host stack is no longer tied to a
single-root assumption. These changes form the basis for supporting
multiple devices attached through a hub in future updates.

Signed-off-by: Aiden Hu <weiwei.hu@nxp.com>
2026-03-17 10:25:34 +00:00
Josuah Demangeon
8d669364e6 usb: device_next: uvc: fix ordering of frame interval
Fix the qsort comparison function that was hitting an integer underflow.
Some video sources such as video-sw-generator have UINT32_MAX for maximum
frame interval (arbitrarily slow), which made the comparison underflow,
which in turn lead to frame interval being listed in the wrong order,
breaking Windows support.

Fixes #101259.

Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-03-11 23:18:21 -04:00
Henrik Brix Andersen
0f8e72a7c2 usb: device_next: reduce log level to debug for internal status logs
Reduce the log level to debug for a number of internal status logs within
the USB device_next stack.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2026-03-11 20:55:57 -04:00
James Bennion-Pedley
d0057bd2cc usb: uac2: kconfig override for feedback endpoint size
Adds compile-time option for overriding UAC2 Feedback endpoint to allow
usage with non-class-compliant Windows UAC2 driver.

Signed-off-by: James Bennion-Pedley <James.Bennion-Pedley@thinksmartbox.com>
2026-03-06 15:12:15 +01:00
Tomasz Moń
cbe1958809 usb: device_next: dfu: actually fail requests
Control read handlers handle_get_status(), handle_get_state() and
handle_upload() return value is used to set errno. Setting errno inside
the functions and then returning 0 therefore does not fail the request
as intended.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2026-02-20 14:58:46 +01:00
Josuah Demangeon
6c1d24cde1 usb: device_next: uvc: fix frame/formats descriptors
The standard format "frame-based payload" was abused as a way to get
generic descriptors that match all other format and frame types.
This is not accurate as some fields do not match, causing misalignment
of some struct fields in some case.

Correct the misuse by introducing new valid structs and refactoring the
descriptor parsing to use them instead.

Signed-off-by: Josuah Demangeon <me@josuah.net>
2026-02-18 10:30:44 +00:00
Josuah Demangeon
4360894965 usb: host: class: support multiple controllers
Only bind to a controller after probing. Wait that a class is matching and
assigned to a device to assign a host controller to a class. This allows
a class to be used by any of the controller in place.
Remove the `c_data->uhs_ctx` field, not used anywhere, in favor of using
`c_data->udev->ctx`.

Signed-off-by: Josuah Demangeon <me@josuah.net>
2026-02-18 10:29:27 +00:00
Muhammad Waleed Badar
4490c05eef usb: device_next: shell: adopt SHELL_HELP macro
Convert USB device shell command help strings to use the
SHELL_HELP() macro instead of inline help text.

This aligns the USB device shell with the updated shell help
formatting used across the tree and ensures consistent
presentation of command descriptions and argument usage.

Signed-off-by: Muhammad Waleed Badar <walid.badar@gmail.com>
2026-02-13 13:56:04 +01:00
Johann Fischer
eee1fa2d2c usb: usbip: use recently introduce zsock_send_all()
zsock_send() does not necessarily have to send the entire data. Use the
recently introduced function zsock_send_all().

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2026-02-13 10:05:52 +01:00
Johann Fischer
6f602ded27 usb: usbip: retry if the UHC transfer allocation fails
Depending on the server, functions, and client application, we may
get out of transfers very quickly. To throttle here may allow
submitted transfers to be finished.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2026-02-13 10:05:52 +01:00
Johann Fischer
665ab07831 usb: usbip: use variable buffer pool for URB data
UHC transfer handling and assignment of completed transfers to URB
sequences relies on the buf->size. For that, we can just use variable
net_buf pools instead of fixed. Perhaps it gives us also better
utilization of the buffer.

Add Kconfig optons to configure pool size and number of buffers
and use large default pool size for native_sim platform.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2026-02-13 10:05:52 +01:00
Johann Fischer
c9e105873a usb: usbip: improve completion handling when client disconnects
Make request completion handling a bit more robust when, for whatever
reason, the connection has been closed. Do not implicitly clear the USB
device pointer on client disconnect, to allow the completion callback to
clean up. Before handling USBIP_RET_SUBMIT, check whether the device is
still exported, and if not, just drop completed transfer.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2026-02-13 10:05:52 +01:00
Johann Fischer
0c31b37525 usb: move instances Kconfig template to common directory
Move the Kconfig template to configure the number of instances in, for
example, functions or class drivers, to a common directory.
This makes it less confusing when used in host support.

Template was not used in CDC ECM function and can be removed.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2026-02-12 14:28:18 +00:00
Aiden Hu
0b6930e66d usb: host: fix usbh_bus_thread name setting
Use correct thread data pointer when setting bus thread name.

Signed-off-by: Aiden Hu <weiwei.hu@nxp.com>
2026-02-11 07:59:17 +01:00
Josuah Demangeon
5c88168758 usb: host: shell: call usbh_device_set_address()
Instead of calling the low-level function from usbh_ch9.c, call the
higher level function from usbh_device.c to update the address,
making sure that the udev->address and udev->state are updated
appropriately.

Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-10 13:02:10 +01:00
Josuah Demangeon
abdc1ff11d usb: host: add a usbh_device_set_address() function
To complement usbh_req_set_address(), add a matching function
usbh_device_set_address() that also takes care of updating the data
structures.

Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-10 13:02:10 +01:00
Jay Beavers
03a86f89a1 usb: device_next: fix bInterfaceSubClass for CDC ECM data interface
According to the USB CDC 1.2 specification, section 4.6, the value of
bInterfaceSubClass for the data interface should be 0.

Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
2026-02-10 08:23:40 +01:00
Fabio Baltieri
89e778d5b2 usb: device_next: usbd_cdc_{ecm,ncm}: support promisc mode
Flag the ECM and NCM interfaces as supporting promisc mode. There's no
filtering really so enabling promisc is a noop but pretending to have
support is needed to allow these to be added to a bridge.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2026-02-10 08:22:46 +01:00
Aiden Hu
376d2c3139 usb: uvc: update header for UVC 1.5 compliance
Add input terminal descriptor and CS descriptor header and
extend frame descriptor fields (buffer size, default interval)

Signed-off-by: Aiden Hu <weiwei.hu@nxp.com>
Co-authored-by: Aiden Hu <weiwei.hu@nxp.com>
Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-09 16:12:00 -06:00
Josuah Demangeon
9fd1afc6f9 usb: uvc: use max host/device for MAX_FRMIVAL
Compute the maximum of the CONFIG_USBD_VIDEO_MAX_FRMIVAL and
CONFIG_USBH_VIDEO_MAX_FRMIVAL for struct definition, so that
the same struct can be relevant for both host and device
implementation.

Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
Signed-off-by: Aiden Hu <weiwei.hu@nxp.com>
Co-authored-by: Aiden Hu <weiwei.hu@nxp.com>
2026-02-09 16:12:00 -06:00
Josuah Demangeon
69d9f133c4 usb: host: uvc: placeholder implementation
Loop through each of the VideoStreaming and VideoControl descriptor
to parse them. This is meant as a stub for the purpose of testing the
class API.

Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-09 16:12:00 -06:00
Josuah Demangeon
5412bf3265 usb: uvc: move helpers functinos to common/
Move UVC helper functions to a file shared between UVC host and device.
The arrays are not visible anymore from either USB host or device, but
instead accessed through a front-end funciton.

Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-06 11:18:45 +01:00
Johann Fischer
5163ea64fe usb: device_next: hid: do not use false and true in COND_CODE_1
With the C23 standard, false and true are keywords and constants of type
bool with a value of 0 and 1. Though there does not seem to be issues
with GCC version 14, there is a note in the specification that the
spelling of false and true inside expressions that are subject to the #
and ## preprocessing operators is unspecified.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2026-02-05 16:54:58 +01:00
Alberto Escolar Piedras
45fae3de24 usb: host: Correct function prototype
Correct function prototype which causes a compiler warning
and builds to fail due to it.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2026-02-05 16:51:26 +01:00
Josuah Demangeon
a8007b2209 usb: host: add debug logs for the configuration descriptor
The USB host will have to select the appropriate size for the configurtion
descriptor, and the defaults will not be enough for some classes that
encode a lot of information in the configuration descriptor.
Add logging to debug this.

Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-05 13:12:58 +00:00
Josuah Demangeon
cdd8bdf239 tests: usb: host: test API using initialization code from USB samples
Add tests making sure the USB Host class APIs introduced build
and run as expected. Use USB device samples common code for
initialization.

Co-authored-by: Johann Fischer <johann.fischer@nordicsemi.no>
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-05 13:12:58 +00:00
Josuah Demangeon
32c25aa978 usb: host: add a nil entry at the end of configuration descriptor
Allocate a larger size to keep room for a nil descriptor at the end of
the configuration descriptor. This is used to walk through the list of
descriptors until this empty descriptor happens.

Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-05 13:12:58 +00:00
Josuah Demangeon
7e455ae0fe usb: host: improve safety of configuration descriptor parsing
Make sure that there is at least enough room for the header before
dereferencing the fields such as bLength.

Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-05 13:12:58 +00:00
Josuah Demangeon
29504e2d86 usb: uvc: move the header definition to include/
Move the UVC header with all the definitions from the UVC standard to
share it between USB host and device class implementation.

Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-05 13:12:58 +00:00
Josuah Demangeon
92c4d2744e usb: host: introduce usbh_class with init/remove functions
Add functions to probe/remove all classes as part of a new usbh_class.c
and a matching usbh_class.h. These functions are called from the function
usbh_init_device_intl() in usbh_core.c to initialize every class upon
connection of a device. Every class driver provide filters to match the
interfaces of the device.

Co-authored-by: Aiden Hu <weiwei.hu@nxp.com>
Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-05 13:12:58 +00:00
Johann Fischer
b71d3223a8 usb: host: add functions to initialize and probe class drivers
Add basic functions to initialize and probe class drivers.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-05 13:12:58 +00:00
Johann Fischer
293b5d0271 usb: host: add helpers to parse USB descriptors
Add helpers to parse and validate USB descriptors.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-05 13:12:58 +00:00
Josuah Demangeon
7e38871a66 usb: host: store the pointers to the interface association
In addition to storing the interface pointers, also store the pointers
for the interface association. Also add comments to the other fields
of the structs to adapt to a new CI check.

Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-05 13:12:58 +00:00
Josuah Demangeon
e103442d25 usb: host: introduce wrappers to access the class function pointers
Add API wrappers around the function pointers in struct usbh_class_api,
while also documenting the USB host class internal API.

Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-05 13:12:58 +00:00
Josuah Demangeon
1df78741cc usb: host: add copyright notice to linker script
Add missing copyright notice for the linker script to help with
check_compliance.py.

Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
2026-02-05 13:12:58 +00:00
Johann Fischer
506a59396f usb: device_next: fix compilation if the user disables HWINFO
Make USBD_HWINFO_DEVID_LENGTH prompt optional so that the code compiles
even if HWINFO is disabled.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2026-01-23 13:48:16 +01:00
Victor Brzeski
6b31178272 uac2: disable active terminals when class is disabled
When the USB Bus is reset, the USBD stack will disable and re-enable
all Classes. If this occurs while streaming audio with UAC2, the
terminals remain active when re-enabled, yet the endpoints have
yet to be enabled (resulting in -ENODEV logspam). This change
disables all active terminals when the class is disabled.

Signed-off-by: Victor Brzeski <vbrzeski@gmail.com>
2026-01-22 13:01:30 -05:00
Alberto Escolar Piedras
18bf241ec4 usb: host: usbip: Fix net API use
In c52c206e26 &
32059d7414 the USB code was changed to
use the Zephyr native net_ prefixed API, but some were forgotten
(nto[hn][ls]()). Let's change it now.

Without this fix/change the code still builds as we are by now setting
CONFIG_NET_NAMESPACE_COMPAT_MODE. But when this is not set, things
fail to build.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2025-12-18 14:51:52 +00:00
Alberto Escolar Piedras
b5709a3cf4 usb: next: Fix net API use
In 32059d7414 this code was changed to
use the Zephyr native net_ prefixed API, but some were forgotten
(ntohs()). Let's change it now.

Without this fix/change the code still builds as we are by now setting
CONFIG_NET_NAMESPACE_COMPAT_MODE. But when this is not set, things
fail to build.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2025-12-18 14:51:52 +00:00
Alberto Escolar Piedras
ba84bfea3c usb: legacy: Fix net API use
In c52c206e26 this code was changed to use
the Zephyr native net_ prefixed API, but some were forgotten
(ntohs()). Let's change it now.

Without this fix/change the code still builds as we are by now setting
CONFIG_NET_NAMESPACE_COMPAT_MODE. But when this is not set, things
fail to build.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2025-12-18 14:51:52 +00:00
Sylvio Alves
f8d2e00a0e includes: remove duplicated entries in zephyr-tree
Remove duplicated #include directives within the same
preprocessor scope across the Zephyr tree.

Duplicates inside different #ifdef branches are preserved
as they may be intentional.

Signed-off-by: Sylvio Alves <sylvio.alves@espressif.com>
2025-12-17 13:57:38 -05:00
Josuah Demangeon
367c859ee9 usb: device_next: uvc: fix frame interval sorting
The qsort() function takes a callback argument that is having the
same semantics as strcmp(). Fix uvc_compare_frmival_desc() sorting
to make qsort() list frame intervals in increasing values.

Fix a bug where Windows would not enumerate devices when the video device
have multiple frame interval supported.

Signed-off-by: Josuah Demangeon <me@josuah.net>
2025-12-10 15:46:09 +00:00
Jukka Rissanen
32059d7414 usb: next: Change to use network namespaced APIs
Do not use POSIX symbols directly but use network namespaced
APIs instead.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-12-08 06:15:04 -05:00
Jukka Rissanen
c52c206e26 usb: legacy: Change to use network namespaced APIs
Do not use POSIX symbols directly but use network namespaced
APIs instead.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2025-12-08 06:15:04 -05:00