Commit graph

877 commits

Author SHA1 Message Date
Abderrahmane JARMOUNI
ea71891fa2 drivers: usb: nrf_usbd_common: fix Kconfig leak
Fix Kconfig Log options leak

Signed-off-by: Abderrahmane JARMOUNI <git@jarmouni.me>
2025-10-16 17:12:27 +03:00
Mathieu Choplain
1609f10082 drivers: usb: udc: stm32: configure OTGFS/HS RxFIFO size using Kconfig
Create a new Kconfig option allowing to tweak the RxFIFO size on OTG_FS
and OTG_HS instances, and replace the old hardcoded method with this new
mecanism.

The default value of 600 bytes yields a similar size to the the previous
hardcoded default of 160 words (= 640 bytes) when combined with the fixed
overhead computed by the driver (~56 bytes on OTG_FS with 6 endpoints).

Also fix a tiny error in a logging message (DRAM size in bytes, not bits).

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-10 12:56:58 -04:00
Mathieu Choplain
6efa7f31fc drivers: usb: udc: stm32: allow EP max packet size up to HW capabilities
The maximal packet size (for non-control endpoints) was obtained by the
driver from HAL definitions which appear to not properly reflect hardware
capabilities.

Update driver to allow endpoints with wMaxPacketSize up to the maximal
value allowed by the USB Specification depending on operation mode, since
all STM32 USB controllers always support these values. Also move the EP
max packet size field in the 'struct udc_stm32_config' to avoid implicit
padding and add a documentation comment.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-10 12:56:58 -04:00
Mathieu Choplain
bbb8aba17d drivers: usb: udc: stm32: accept non-word-aligned MaxPacketSize or FIFOs
STM32 OTG USB controllers use word-addressable RAM with a 32-bit word size
so all allocations from the USB SRAM must be 32-bit aligned. The driver did
not accept unaligned values of wMaxPacketSize, and FIFO allocation code was
implicitly expecting FIFO sizes to be aligned as well (since it allocated
"bytes / 4" without rounding up).

Update driver to accept values of wMaxPacketSize that aren't word-aligned
and to allocate properly sized FIFOs sizes when an unaligned size has been
requested.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-10 12:56:58 -04:00
Mathieu Choplain
ac998f7109 drivers: usb: udc: stm32: clean up handling of USB buffer table
The ST USB controller (compatible 'st,stm32-usb') is fitted with private
SRAM called the Private Memory Area or PMA. This is primarily used to hold
data transfered over USB, but it also contains the so-called 'buffer table'
(a.k.a. BTABLE) which holds the base address and size of buffers in the PMA
allocated to each endpoint. The BTABLE is placed by the driver at the start
of the PMA and occupies a fixed size ('USB_BTABLE_SIZE'), which was stored
in the driver configuration field 'pma_offset'. This mechanism is unused on
non-ST USB controllers from STM32 microcontrollers, but USB_BTABLE_SIZE is
still defined (to a dummy value) and stored in the 'pma_offset' field which
becomes unused.

Remove the 'USB_BTABLE_SIZE' definition and the 'pma_offset' field from the
driver configuration, and update the ST USB controller-specific verison of
'udc_stm32_mem_init' to derive the BTABLE size from the number of endpoints
that the controller has instead.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-10 12:56:58 -04:00
Mathieu Choplain
3788b2c0a6 drivers: usb: udc: stm32: turn EP0 max packet size into a constant
The EP0 max packet size was de facto a constant because its value was the
same regardless of which USB IP was in use. However, it was stored as part
of the instance configuration anyways which is wasteful and slower.

Create new "UDC_STM32_EP0_MAX_PACKET_SIZE" driver-level constant with which
all usage of the per-instance configuration field is replaced.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-10-10 12:56:58 -04:00
Fabio Baltieri
37717b229f sys: util: rename Z_MIN Z_MAX Z_CLAMP to min max and clamp
Rename these three macros to an unprefixed lower-case variant. This is
normally not done for Zephyr macros (see container_of) but in this case
it seems like a good idea to adopt the lowercase names to:

1. have the same convention as the equivalent Linux macros, helping devs
   working cross project recognizing (mis)use patterns.
2. make it somewhat intuitive that the lowercase ones are meant to be
   used in functions while the uppercase ones are to be used for static
   evaluation.

Add few c++ guards to avoid colliding with std::min and std::max.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2025-10-03 21:14:11 -04:00
Sylvio Alves
7c1193c813 udc_dwc2: fix off-by-one in TX FIFO unset check
Fix the check in dwc2_unset_dedicated_fifo() that wrongly included the
current endpoint when testing for higher FIFOs. This caused false
warnings and early returns.

Use ~BIT_MASK(ep_idx + 1) to only test FIFOs above the current EP.

Signed-off-by: Sylvio Alves <sylvio.alves@espressif.com>
2025-10-02 21:59:14 +02:00
Alain Volmat
c3995b65ba drivers: usb: udc: stm32: avoid unnecessary clock_control_subsys_t cast
Avoid incorrect (clock_control_subsys_t *) incorrect cast and
remove the cast of pclken in calls to the clock_control_
framework.

Signed-off-by: Alain Volmat <alain.volmat@foss.st.com>
2025-10-02 16:47:53 +02:00
Tomasz Moń
c180420477 drivers: udc_dwc2: Fix memory leak on subsequent bus resets
Do not queue new buffer after bus reset if there is one already queued.
This fixes memory leak on each bus reset if there are no SETUP transfers
received between resets.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2025-10-01 17:21:18 +03:00
Declan Snyder
570b445a61 drivers: Convert to use SPI macro without delay parameters
Convert all drivers and other consumers to use SPI macros without the
delay parameters.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
2025-10-01 14:39:36 +03:00
Andrzej Głąbek
6360c7f67c drivers: usb: udc: Fix VBUS ready timeout dependency in Kconfig.dwc2
This is a follow up to commit 4fe2c5bd5f.

The UDC_DWC2_USBHS_VBUS_READY_TIMEOUT Kconfig option should be available
also for nRF92 Series SoCs (as it was previously when it depended on
NRFS_HAS_VBUS_DETECTOR_SERVICE), otherwise some builds will fail for
such targets.

Signed-off-by: Andrzej Głąbek <andrzej.glabek@nordicsemi.no>
2025-09-30 19:37:31 +02:00
Mathieu Choplain
092a0820c4 drivers: usb: udc: stm32: enhance PHY type & operating speed determination
Rework how the STM32 UDC driver determines the PHY used by a given instance
and the speed at which the instance is able to and allowed to operate.

The PHY determination now uses the 'phys' property instead of looking at
whether nodes with a specific compatible were enabled. Similarly, the speed
determination takes into account the 'maximum-speed' property, along with
the selected PHY's and the instance's capabilities.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-09-30 19:36:55 +02:00
Mathieu Choplain
e31ddec781 drivers: usb: udc: stm32: rework PWR and PHY initialization
Rework how the driver performs PWR configuration and initializes HS PHYs.

Originally, the initialization logic was split in half without any real
logic. There were also attempts to share some logic but this resulted in
code that was difficult to understand.

Initialize PWR and PHY in well separated steps, and use explicit series
and PHY type checks in each part to ensure the logic is easy to understand.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-09-30 19:36:55 +02:00
Mathieu Choplain
7ec83e5c95 drivers: usb: udc: stm32: use HAL PCD macros for speed
Rework the STM32 USB UDC driver to use HAL PCD macros for speed selection,
as should have always been done since that is the library used by the shim
driver. Also dummy definitions to ensure cross-series compatibility
without many #if blocks in the driver.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2025-09-30 19:36:55 +02:00
Martin Gysel
a7df9e9860 drivers: udc_stm32: select Kconfig option UDC_DRIVER_HAS_HIGH_SPEED_SUPPORT
The driver supports high-speed capable controllers. Select
the appropriate Kconfig options to ensure that the stack does
not optimize away high-speed support.

Signed-off-by: Martin Gysel <me@bearsh.org>
2025-09-30 19:36:55 +02:00
Henrik Brix Andersen
24f473fef8 drivers: usb: udc: sam0: do not select CONFIG_SYS_MEM_BLOCKS
Do not select CONFIG_SYS_MEM_BLOCKS when enabling the Atmel SAM0 USB device
controller driver as the implementation does not use the system memory
blocks allocator.

Signed-off-by: Henrik Brix Andersen <henrik@brixandersen.dk>
2025-09-30 15:28:16 +03:00
Johann Fischer
4fe2c5bd5f drivers: udc: allow to use timeout Kconfig option on nRF54LM20A SoC
Kconfig option UDC_DWC2_USBHS_VBUS_READY_TIMEOUT depends on services
exclusively available for nRF54H20, but the option can also be used for
nRF54LM20A, where there are no service dependencies, and VREG can be
accessed by the driver directly. Let depend the option on the SOC
series, as the controller can be used by the different CPUs on the SOC.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2025-09-26 20:48:32 -04:00
Johann Fischer
0018e8d600 drivers: udc: cleanup depends in Kconfig.dwc2
Factor out UDC_DWC2 dependency.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2025-09-26 20:48:32 -04:00
Mathieu CHOPLAIN
3ae98de48e drivers: usb: udc: stm32: handle endpoint halt properly
Handle endpoints in halted state properly by marking endpoints as halted
when appropriate, and inhibiting transfers involving halted endpoints.

Co-authored-by: IBEN EL HADJ MESSAOUD Marwa <marwa.ibenelhadjmessaoud-ext@st.com>
Signed-off-by: Mathieu CHOPLAIN <mathieu.choplain-ext@st.com>
2025-09-18 09:47:36 +01:00
Raffael Rostagno
9dcc2d8086 drivers: usb: dwc2: Limit RX FIFO size allocation
Limit RX FIFO size allocation to a percentage of the total
SPRAM available, to cap allocation according to memory size.

Signed-off-by: Raffael Rostagno <raffael.rostagno@espressif.com>
2025-09-16 16:04:03 +02:00
Raffael Rostagno
41cac25632 drivers: usb: dwc2: esp32: Add support
Add USB device driver support for ESP32 devices with
USB-OTG peripheral.

Signed-off-by: Raffael Rostagno <raffael.rostagno@espressif.com>
2025-09-16 16:04:03 +02:00
Tomasz Moń
0748b4e47d drivers: udc_dwc2: Control D+ pull-up on nRF54H20
Disable D+ pull-up on core disable to make sure that device does not
indicate its presence to host before the stack is ready (software
initializes controller and clears SftDiscon bit).

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2025-09-16 14:05:18 +02:00
Mathieu CHOPLAIN
5bef4a9d62 drivers: usb: udc: skeleton: handle special case of set_halt() for EP0
The UDC stack assumes that 'halt' on control endpoints is automatically
cleared by device drivers and never calls udc_ep_clear_halt() for such
endpoints. In practice, most controllers do perform this automatic clear
in hardware, but this requires special handling in udc_ep_set_halt() for
control endpoints (EP0), as setting the 'halted' bit on such endpoints will
lead to the software getting out-of-sync with the hardware.

Update the UDC skeleton driver to handle this edge case and add a comment
documenting the stack's behavior, along with the hardware behavior expected
by the code added to the skeleton driver for it to be functional.

Co-authored-by: Johann Fischer <johann.fischer@nordicsemi.no>
Signed-off-by: Mathieu CHOPLAIN <mathieu.choplain-ext@st.com>
2025-09-15 12:09:32 +02:00
Richard Yim
de1673a80b drivers: uhc: uhc_mcux_common: add direction handling for non-setup packets
added logic to determine the direction of USB transfers for non-setup
packets using the endpoint direction. This ensures proper handling of
USB_IN and USB_OUT directions.

Signed-off-by: Richard Yim <richardyim@ami.com>
2025-09-15 10:28:43 +02:00
Chun-Chieh Li
13315d1ea0 drivers: usb: udc: numaker: refactor large isr function
For clear, this splits large isr function into smaller ones which are
dedicated for handling plug/unplug, wakeup, reset/suspend/resume, SOF,
Setup, and endpoint events.

Signed-off-by: Chun-Chieh Li <ccli8@nuvoton.com>
2025-09-08 11:13:23 -04:00
Chun-Chieh Li
866080d9c5 drivers: usb: udc: numaker: remove unneeded volatile
This removes unneeded 'volatile' qualifier for normal memory variables.

Signed-off-by: Chun-Chieh Li <ccli8@nuvoton.com>
2025-09-08 11:13:23 -04:00
Chun-Chieh Li
e6bc45504a drivers: usb: udc: numaker: support VBUS detect
This supports VBUS detect (caps.can_detect_vbus). This requires enables
VBUS detect interrupt early.

Signed-off-by: Chun-Chieh Li <ccli8@nuvoton.com>
2025-09-08 11:13:23 -04:00
Chun-Chieh Li
cf787d4e80 drivers: usb: udc: numaker: refine timing to clear interrupt flags
This refines interrupt handling sequence: clear interrupt flag first,
then process it. This can avoid one race condition when interrupt flag
is cleared but not processed for interrupts of the same type coming
very closely.

Signed-off-by: Chun-Chieh Li <ccli8@nuvoton.com>
2025-09-08 11:13:23 -04:00
Chun-Chieh Li
4516310f4b drivers: usb: udc: numaker: fix timing to enable D+ pull-up
This fixes timing to enable D+ pull-up so that device won't be recognized
by host until user application invokes udc_enable().

Signed-off-by: Chun-Chieh Li <ccli8@nuvoton.com>
2025-09-08 11:13:23 -04:00
Chun-Chieh Li
3bd8b61ff9 drivers: usb: udc: numaker: fix typo
This fixes typo in isr function name.

Signed-off-by: Chun-Chieh Li <ccli8@nuvoton.com>
2025-09-08 11:13:23 -04:00
Chun-Chieh Li
e2ff312b28 drivers: usb: udc: numaker: recover unwanted assignment break
This recovers unwanted assignment break caused by clang-format.

Signed-off-by: Chun-Chieh Li <ccli8@nuvoton.com>
2025-09-08 11:13:23 -04:00
Johann Fischer
70a68dfe3c drivers: udc_kinetis: allocate control OUT length as multiple of MPS0
Controllers that use buffers directly must always allocate the length of
the control OUT buffer as a multiple of the control endpoint MPS.
Kinetis UDC driver explicitly checks the remaining buffer size and
finishes the transfer earlier.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2025-09-03 14:55:50 +01:00
Johann Fischer
d493523b76 drivers: udc_kinetis: fix typo and remove unused variable
Fix typo und remove unused variable, add a check for buffer allocation.
Do not disable controller on udc_disable.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2025-09-03 14:55:50 +01:00
Johann Fischer
e61cd224dd drivers: udc: do not reset odd buffer flag on endpoint enable
Some controllers, such as those used in Kinetis devices, do not provide
a way to reset the buffer descriptor flags for each endpoint. Do not
reset the odd flag on endpoint enable, as this will cause it to become
out of sync with the hardware.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2025-09-03 14:55:50 +01:00
Chun-Chieh Li
cfa837c6dd drivers: usb: udc: refine log message in udc_common.c
This refines log message in udc_common.c, including:
1. Add address of allocated net_buf info into udc_ep_buf_alloc log so
   that net_buf create/destroy can be easily monitored with calls to
   udc_ep_buf_alloc and udc_buf_destroy.
2. Add ep info into udc_ep_buf_alloc log on failure.

Signed-off-by: Chun-Chieh Li <ccli8@nuvoton.com>
2025-09-01 23:27:19 +02:00
Dmitrii Sharshakov
166e1a3b81 drivers: udc_rpi_pico: use reset API
No need to toggle reset line manually, use the standard reset API.

This improves code searchability and aids debugging.

Requires f2f496df84 to make sure reset
deassertion is waited for.

Signed-off-by: Dmitrii Sharshakov <d3dx12.xx@gmail.com>
2025-08-29 08:59:59 +02:00
Jamie McCrae
db8f991c77 kconfig: Use $(...) instead of ${...} for getting variables
Updates this to comply with the Zephyr Kconfig recommendations

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
2025-08-20 12:05:41 +02:00
BUDKE Gerson Fernando
77070941fa dts: phy: Add clock-reference prop in stm32u5-otghs-phy
The OTG_HS PHY from stm32u5a5xx device require the correct reference
clock frequency selction in SYSCFG_OTGHSPHYCR. The current default is
hard coded to 16Mhz (which matches the development board crystal).
However, a custom board my require a different crystal and then the
USB will not work. This add a required field in the
st,stm32u5-otghs-phy binding to force user to select the correct
clock reference. The current nucleo_u5a5zj_q baord was updated to
reflect the mandatory field.

Signed-off-by: BUDKE Gerson Fernando <gerson.budke@leica-geosystems.com>
2025-08-20 12:05:24 +02:00
Johann Fischer
58b37007d2 drivers: udc_stm32: fix remote wakeup handling
Clear the suspended status and submit the resume event once the remote
wakeup signaling is finished. Clear the suspended status on bus reset as
well.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2025-08-18 11:56:57 +02:00
Johann Fischer
bd5dcc603f drivers: udc: make suspend/resume logging message more precise
Make suspend/resume logging message more precise.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2025-08-18 11:56:57 +02:00
Michał Stasiak
4abc1dd67c modules: hal_nordic: remove deprecated error code
NRFX_ERROR_ALREADY_INITIALIZED has beed deprecated and
repleaced by NRFX_ERROR_ALREADY.

Signed-off-by: Michał Stasiak <michal.stasiak@nordicsemi.no>
2025-08-13 11:09:47 +01:00
Khoa Nguyen
3aa72e4e9e drivers: Update p_context for all Renesas drivers
The p_context no longer has the const type, so all
Renesas-supported drivers need to be updated accordingly.

Signed-off-by: Khoa Nguyen <khoa.nguyen.xh@renesas.com>
2025-08-08 11:52:13 +03:00
Tomasz Moń
66b938f93a drivers: udc_dwc2: Add nRF54LM20A vendor quirks
Initial implementation of nRF54L quirks necessary for nRF54LM20A.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2025-08-06 12:01:34 -04:00
Johann Fischer
efaaad0a09 usb: mark new stack as unstable and set it as the default
Document that the legacy stack is now deprecated.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2025-08-06 11:57:39 -04:00
Johann Fischer
4af7549d72 usb: deprecate legacy USB device support
Deprecate legacy USB device support.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
2025-08-06 11:57:39 -04:00
Tomasz Moń
382c0dea69 drivers: udc_dwc2: Periodic Transfer Interrupt support
Add Kconfig option to enable Periodic Transfer Interrupt mode for
isochronous endpoints. The tradeoff is reduced CPU usage in exchange for
not timing out isochronous transfers.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2025-08-06 17:10:40 +03:00
Maureen Helm
a4c757adc2 drivers: udc: Fix max32 driver high speed support
High speed support has been broken for the max32 driver since commit
faeabc63c9.

Signed-off-by: Maureen Helm <maureen.helm@analog.com>
2025-07-30 17:21:09 -04:00
Tomasz Moń
8d1f7b3bef drivers: udc_dwc2: Fix incomplete iso handling race
Incomplete iso IN/OUT is just informative and its occurrence does not
prevent the endpoint from actually transmitting/receiving data. Such
"late" isochronous transfers, which are perfectly fine according to USB
specification, were observed on Windows host with nRF54H20 running
explicit feedback sample operating at High-Speed.

The incorrect handling manifested itself with "ISO RX buffer too small"
error message. The faulty scenario was:
  * incompISOIN handler does not find any matching endpoint
  * incompISOOUT handler disables endpoint, discards buffer and sets
    rearm flag
  * next DWC2 interrupt handler iteration after reading GINTSTS
  * XferCompl interrupt on iso IN endpoint
  * XferCompl interrupt on iso OUT endpoint
      - transfer was actually happening to the buffer discarded in
	incompISOOUT handler
      - XferCompl handler modified the next buffer
  * GOUTNakEff interrupt, iso OUT endpoint EPDIS bit is set
  * EPDisbld interrupt, rearm flag set
      - the buffer modified by XferCompl is used and fails because it is
	not large enough

Modify the sequence so it accounts for host actions and the above faulty
scenario no longer causes any problems.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2025-07-29 08:52:20 -04:00
Tomasz Moń
6eb2fa8edd drivers: udc_dwc2: Rearm isochronous OUT endpoints during incompisoout
Rearm isochronous endpoints when handling incomplete iso out interrupt
to make it possible to rearm the endpoint in time (before SOF),
especially when operating at High-Speed.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
2025-07-29 08:52:20 -04:00