Move most of the shared sources from drivers/video/ into subsys/video/.
The content left in drivers/video/ is now only used by drivers, or
drivers themselves.
Signed-off-by: Josuah Demangeon <me@josuah.net>
Move all the content of <zephyr/drivers/video.h> to a separate include
file in <zephyr/video/video.h> except driver entry points, all with a
video_drivers_... prefix.
Video control IDs are now in <zephyr/video/controls.h> and included
from both <zephyr/drivers/video.h> and <zephyr/video/video.h>.
Video format FourCCs are now in <zephyr/video/formats.h> and included
from both <zephyr/drivers/video.h> and <zephyr/video/video.h>.
As a side effect, introduce a video subsystem in MAINTAINERS.yml
Signed-off-by: Josuah Demangeon <me@josuah.net>
The prl_run function unconditionally called
prl_rx_wait_for_phy_message on every cycle, including wakeups
caused by TCPC_ALERT_TRANSMIT_MSG_SUCCESS. This allowed stale
data in the TCPC RX buffer to be read and forwarded to the
Policy Engine as a real message.
Add PRL_FLAGS_RX_COMPLETE (bit 10) to prl_rx->flags. The flag is
set in alert_handler when TCPC_ALERT_MSG_STATUS is received,
which the TCPC driver fires only for real non-GoodCRC messages.
prl_run now gates prl_rx_wait_for_phy_message on this flag, so
the RX path is only entered when the TCPC has explicitly
signaled a message is available.
Signed-off-by: Nicholas Cadieux <ncadieux@qti.qualcomm.com>
The PRL flag fields are reset with plain stores
(flags = ATOMIC_INIT(0)), which race with atomic_set_bit() calls
made from alert_handler on the TCPC driver's workqueue thread. A
concurrent set can be lost, dropping a message or reset
notification. Use atomic_clear() so the reset is race-safe.
Signed-off-by: Nicholas Cadieux <ncadieux@qti.qualcomm.com>
When USB control request cannot be handled the endpoint should be
stalled regardless of the reason. Simplify control request to host
handlers by changing the return type to the allocated buffer. Returning
NULL is enough to indicate to device stack that the request should be
stalled.
Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
For control transfers with data stage from device to host, only the
handler knows how large buffer is necessary (by specification host is
allowed to request up to 65535 bytes for every control read transfer).
While generally hosts do keep wLength relatively low, there are cases
where the value is big enough that the buffer cannot be allocated with
default UDC_BUF_POOL_SIZE value. Right now the only workaround is to
increase UDC_BUF_POOL_SIZE to large enough value but this is not a
viable solution on memory constrained targets. For example USB3CV
Mass Storage tests read string descriptor 0 with wLength set to 4096,
while the Zephyr response to this request is 4 bytes (and therefore 4
byte long buffer is enough).
Move the allocation to handler to allow the code to allocate as small
buffer as possible to handle the transfer. This allows MSC sample to
pass USB3CV with default UDC_BUF_POOL_SIZE value.
Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
When a UVC device is removed, signal any waiting threads that buffers
have been aborted by raising the VIDEO_BUF_ABORTED signal. This ensures
proper cleanup and prevents threads from waiting indefinitely on buffers
that will never complete.
Signed-off-by: Mark Wang <yichang.wang@nxp.com>
Universal Serial Bus Specification Revision 2.0 does not differentiate
between "stack errors" and "protocol errors". 8.5.3.1 Reporting Status
Results lists three control transfer outcomes:
* Function completes (ZLP/ACK)
* Function has an error (STALL)
* Function is busy (NAK)
If the control transfer fails, the device should respond with a STALL
handshake. In addition to protocol errors, which are reported using
errno values, the new device stack uses control transfer handler
return values as indicators for unrecoverable errors. Handlers that
wish to send a STALL handshake must set an errno value and return 0.
The idea of using both errno and return values was to propagate
unrecoverable errors to the user application, which would then shut
down and re-enable the stack. However, this approach is not unambiguous
for users and causes non-compliant timeout behavior when a handler
returns a non-zero value.
Make the stack always behave standards-compliant by removing errno
usage and passing the result directly via the control handler return
value. For now, keep the errno detection to output a deprecation warning.
Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
The `thread` field is not initialized, if it's being animated via
k_work_queue_run instead of k_work_queue_start.
Signed-off-by: Michael Zimmermann <michael.zimmermann@sevenlab.de>
On linux the tty is opened in ECHO mode by default causing any data sent
immidiately after boot to be looped back into rx. This was mitigated in
old implementation in the commit 0127d000a2
("usb: device: cdc_acm: Use ZLP to detect initial host read").
The mitigation works by enqueing a ZLP. When the ZLP is consumed by the
host it means that it started reading. The patch then waits for some
time before sending actual payload. This gives the host application time
to switch off the ECHO mode.
This commit introduces analogous solution to device_next.
Signed-off-by: Antoni Duda <antoni.duda@nordicsemi.no>
Limit the number of bytes that will be copied to the DATA IN buffer in
the CDC NCM implementation.
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
The buf parameter in handle_download() is allowed to be NULL. Check it
explicitly before using it for the length calculation or as an argument
for the callback.
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
Add new optional callback that lets application decide whether Set
Report payload should be received or not. This allows applications to
stop Set Report control transfer handling during Data OUT stage.
Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
Universal Serial Bus Specification Revision 2.0 9.2.7 Request Error
states "It is preferred that the STALL PID be returned at the next
Data stage transaction, as this avoids unnecessary bus activity.".
USB device_next implementation always attempted to receive Data OUT
stage without asking the handler if request can be handled. The only
case where the Data OUT stage was STALLed was when there was not enough
memory to allocate Data OUT buffer.
Do not automatically enqueue buffer to receive Data OUT stage. Call the
handler first with NULL buffer to determine whether the Data OUT stage
can be received. The handler will be called again after Data OUT stage
is received (it may not be called if there is not enough memory to
allocate Data OUT buffer, or if host aborts the transfer).
Modify all in-tree control transfer handlers to perform sanity check
before Data OUT stage is received. Out-of-tree classes that handle
control transfers with Data OUT stage will have to be updated. It was
chosen to implement this as a breaking change in order to not add any
new callbacks and to encourage device implementations to STALL Data OUT
stage as preferred by the USB specification.
Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
Replace the two-line pattern:
net_buf_unref(buf);
buf = NULL;
with net_buf_drop(&buf) which performs both operations
atomically in a single step.
Assisted-by: GitHub Copilot:claude-sonnet-4.6
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
Replace the two-line pattern:
net_buf_unref(buf);
buf = NULL;
with net_buf_drop(&buf) which performs both operations
atomically in a single step.
Assisted-by: GitHub Copilot:claude-sonnet-4.6
Signed-off-by: Johan Hedberg <johan.hedberg@silabs.com>
Allow the notification callback to be set as long as USB is not enabled,
and permit users to change the callback.
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
the return value of uart_irq_update() is not relevant,
therefore change it to void.
Also as most uart drivers don't need irq_update, make
it optional to implement this function in the driver.
Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
Move ctx->root assignment and clearing from event handlers
into usbh_device_connect() and usbh_device_disconnect().
This avoid dangling pointer issue.
Signed-off-by: Aiden Hu <weiwei.hu@nxp.com>
Add Kconfig option USBD_CDC_ACM_BUF_POOL_SIZE to allow
configuring the CDC ACM buffer pool size instead of
using the fixed MPS value.
Signed-off-by: Aiden Hu <weiwei.hu@nxp.com>
The bDataInterface field is expected to contain the interface to use
for sending the data payloads, containing the BULK IN and OUT endpoints.
It is having a default value of 1 which fits USB descriptors at startup,
but once the stack renumbers the interfaces, it needs to be updated
with the numbers the host assigned.
Signed-off-by: Josuah Demangeon <me@josuah.net>
Introduces K_MEM_SLAB_DEFINE_TYPE() and K_MEM_SLAB_DEFINE_STATIC_TYPE()
helpers to allow the user to declare slabs for types without having to
manually ensure the alignment is correct.
Manual slab alignment was very error-prone and this change fixes several
instances of misalignment that would be trapped by the undefined
behavior sanitizer when running on 64-bit targets.
Signed-off-by: Egill Sigurdur <egill@egill.xyz>
Add a command to select the USB host context, similar to the select
command in device support. Set the USB host context used by the shell to
the first entry in the section by default. The USB host context defined
in the shell is not necessary anymore and is moved to the sample code.
This has a very small impact on the user. The way the host shell
commands are used has not changed.
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
Fix copy and paste mistakes in shell outputs. Remove unnecessary
shell_error() in device descriptor command.
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
Replace EALREADY return values of the functions that modify or set USB
device attributes to EBUSY. EBUSY seems to be more appropriate for the
case when a user tries to change the attributes of an operational
device.
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
The cdc_ncm_send() function ignores the return value from
usbd_ep_enqueue(). If the enqueue fails, the code proceeds to block
forever on k_sem_take() waiting for a completion callback that will
never arrive, causing a deadlock.
This was discovered by comparing the CDC-NCM implementation with
CDC-ECM, which correctly checks the return value:
ret = usbd_ep_enqueue(c_data, buf);
if (ret) {
LOG_ERR("Failed to enqueue net_buf for 0x%02x", ep);
net_buf_unref(buf);
return ret;
}
The NCM driver was missing this error handling, leading to potential
hangs if usbd_ep_enqueue() fails for any reason (e.g., endpoint not
ready, USB disconnected, buffer issues).
Fix by checking the return value and properly cleaning up (freeing
the buffer) before returning the error code to the caller.
Signed-off-by: Jay Beavers <jay@tolttechnologies.com>
Allow cdc_acm_poll_in() to receive when CDC ACM function gets enabled,
but only when the IRQ API is not already used.
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
When the SenderResponseTimer expires in PE_DRS_SEND_SWAP, PE was
unconditionally transitioning to PE_SNK_READY. This is incorrect when
the port is operating as a Source, as it should return to PE_SRC_READY.
Replace pe_set_state(dev, PE_SNK_READY) with pe_set_ready_state(dev),
which selects the correct ready state based on the current power role.
Signed-off-by: Nicholas Cadieux <ncadieux@qti.qualcomm.com>
The transition from Attached.SNK to Unattached.SNK is currently
triggered if vbus is < vSafe5V.
USB-Type C Spec R2.4 Table 4-3 defines vSinkDisconnect as
the voltage to be used for the transition from
Attached.SNK to Unattached.SNK
This commit changes the check to use the correct voltage level.
Signed-off-by: Alexander Wachter <alexander@wachter.cloud>
On PD capable SRC detach, DPM was not being notified of
POWER_CHANGE_0A0 or NOT_PD_CONNECTED. Fix by notifying
POWER_CHANGE_0A0 in tc_attached_snk_exit() and NOT_PD_CONNECTED in
pe_suspend_entry().
On PD capable SNK or SRC detach, pe_suspend_entry() did not clear
the explicit contract flag. Fix by clearing PE_FLAGS_EXPLICIT_CONTRACT
in pe_suspend_entry().
On PD capable SNK attach, DPM was not being notified of PD_CONNECTED
at the correct time. Fix by notifying PD_CONNECTED when PS_RDY is
successfully transmitted (source) or received (sink), using
atomic_test_and_set_bit() to prevent duplicate notifications during
renegotiations.
Signed-off-by: Nicholas Cadieux <ncadieux@qti.qualcomm.com>
Fixes an unused function warning when compiling with llvm, looks like
that does not fire with gcc for inline functions but it does for llvm,
tested with:
west build -p -b nrf52840dk/nrf52840 samples/subsys/usb/cdc_acm --
-DTOOLCHAIN_VARIANT_COMPILER=llvm -DCONFIG_UART_LINE_CTRL=n
Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
Fixes:
zephyrproject/zephyr/subsys/usb/device_next/class/usbd_hid.c:635:19:
warning: unused function 'hid_dev_set_out_polling' [-Wunused-function]
zephyrproject/zephyr/subsys/usb/device_next/class/usbd_hid.c:665:19:
warning: unused function 'hid_dev_set_in_polling' [-Wunused-function]
When compiling with LLVM:
west build -p -b nrf52840dk/nrf52840 samples/subsys/usb/hid-keyboard --
-DTOOLCHAIN_VARIANT_COMPILER=llvm
Functions are only used when CONFIG_USBD_HID_SET_POLLING_PERIOD is
enabled, set as __maybe_unused instaed of inline.
Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
UVC counts frame intervals as 100ns units, so to get a number in
nanosecond, the value from USB needs to be divided by either
(USEC_PER_SEC * 10) or (NSEC_PER_SEC / 100). A mix of the two
was used.
Signed-off-by: Josuah Demangeon <me@josuah.net>
When UVC is part of a multi-function device, baInterfaceNr(0) may point
to an incorrect interface number. Assign the actual streaming interface
number after the configuration descriptor is initialized.
This is follow-up on commit 8d7bb597fc
("usb: uvc: use uvc_device_ in API and add _enable()/_shutdown()").
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
Use a different API call to complete the function rather than
relying on USB .init() to terminate the configuration.
This allows to set the endpoint descriptors before usbd_init()
is called.
Signed-off-by: Josuah Demangeon <josuah.demangeon@nordicsemi.no>
The vconn_en and vconn_discharge callbacks were invoked with the TCPC
device as the first argument. Application callbacks then called
usbc_get_dpm_data(dev) with the TCPC device, causing dev->data (which
is struct tcpc_data *) to be misinterpreted as struct usbc_port_data *.
This resulted in dpm_data->vconn_pol being written to an incorrect
memory offset, silently corrupting TCPC driver state.
Fix by adding a const struct device *usbc_dev parameter to
tcpc_vconn_control_cb_t and tcpc_vconn_discharge_cb_t, and propagating
it through set_vconn_cb and set_vconn_discharge_cb in the driver API.
Signed-off-by: Nicholas Cadieux <ncadieux@qti.qualcomm.com>
Introduces a new TC_STARTUP_STATE to handle the initialization phase of
the Type-C state machine.
Previously, the state machine was initialized to TC_DISABLED_STATE during
POST_KERNEL initialization. This state inherits from
TC_CC_OPEN_SUPER_STATE, which executes tcpc_set_vconn(false). Since
VCONN callbacks are typically registered in main() (which runs after
POST_KERNEL), this resulted in an error log and the statemachine
unexpectedly transitioning to TC_ERROR_RECOVERY_STATE.
The new TC_STARTUP_STATE has no parent state and performs no hardware
operations. The state machine now transitions:
1. Boot (POST_KERNEL): Initialize to TC_STARTUP_STATE.
2. Start (MAIN): When usbc_start() runs the thread, tc_init() transitions
to TC_DISABLED_STATE (ensuring VCONN/VBUS off with valid callbacks).
3. Run: Immediately transitions to TC_ERROR_RECOVERY_STATE to begin
protocol operation.
Signed-off-by: Nicholas Cadieux <ncadieux@qti.qualcomm.com>
Introduces DRP support to the USB-C subsystem, enabling dynamic
support for both Source and Sink power roles. Unattached devices
toggle between Source (Rp) and Sink (Rd) CC advertisements based
on the configured DRP period (tDRP) and duty cycle (dcSRC.DRP).
Once an attach is detected, the device enters the appropriate
Attached.SRC or Attached.SNK usbc_tc state and starts the
corresponding usbc_pe state machine to negotiate a power contract.
Signed-off-by: Nicholas Cadieux <ncadieux@qti.qualcomm.com>
Function udc_buf_get_all() was intended to be a helper to remove all
requests from endpoint FIFO. While for just freeing all queue the
it may be argued that there may be some doubtful simplicity argument,
merging multiple submitted transfers into one is just enforcing
unnecessary complexity on class implementations.
At general level, every submitted (enqueued) request should get
corresponding completion (request callback) call. UDC drivers were
violating this sensible behavior when dequeuing (cancelling) requests by
merging all submitted requests into one.
Remove udc_buf_get_all() and replace all uses with simple loops. For
most classes (that submit just one request for an endpoint at a time)
this has no functional difference. For classes that implement double
buffering this simplifies completion handling.
Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>