Commit Graph

853 Commits

Author SHA1 Message Date
91fbbd192c usbd: clear endpoint busy/claimed when a completion event is dropped
An XFER_COMPLETE dropped by a full event queue leaves its endpoint's
BUSY|CLAIMED state set forever - the consumer that normally clears it
never sees the event, so usbd_edpt_claim()/usbd_edpt_xfer() fail from
then on and the class never re-arms the endpoint. Clear both flags when
the enqueue fails: the completion is lost either way, but the endpoint
stays usable.

Unit test: arm a bulk endpoint, drop its completion against a full
queue, verify the endpoint can be claimed and re-armed.
2026-08-13 11:45:36 +07:00
a52562b2be usbd: clear the queued-setup counter on bus reset
A SETUP counted before a bus reset must not be carried across it: the
consumer would either skip a post-reset SETUP (count drained by the
stale entry) or, if the count leaked high for any other reason, skip
them all. usbd_reset() now zeroes the counter; the consumer already
guards on zero, and any pre-reset SETUP still in the queue is stale by
definition and correctly discarded.
2026-08-12 22:41:17 +07:00
a0249ada90 usbd: don't leak the queued-setup counter when the event queue is full
A SETUP arriving while the event queue is full is silently dropped by
queue_event(), but _usbd_queued_setup has already been incremented. The
leaked count makes the event handler skip every subsequent SETUP
("Skipped since there is other SETUP in queue") forever: EP0 stays deaf
until tud_init() while the device otherwise looks alive - enumerated,
endpoints armed. Undo the increment when the enqueue fails.

Unit test: fill the queue so a SETUP is dropped, then verify the next
SETUP still completes a GET_DESCRIPTOR control transfer.
2026-08-12 22:41:17 +07:00
05f19ab438 TU_MIN suggestion 2026-07-26 12:16:23 -07:00
7e59f1bf8a fix narrowing, add cast 2026-07-23 15:26:18 -07:00
fd63ad6c2b usbd: forward vendor EP0 requests; clear ep state on iso activate
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HeF2gZ1M7GWkz6Av4BpKPg
2026-07-09 23:38:11 +07:00
19274b95a6 Fix usbd empty builtin driver warning v2
Signed-off-by: HiFiPhile <admin@hifiphile.com>
2026-07-05 15:25:34 +02:00
6912f23618 Fix usbd empty builtin driver warning
Generated-by: OpenAI Codex
Signed-off-by: aineoae86-sys <ai.neo.ae86@gmail.com>
2026-07-05 15:38:17 +08:00
7a4111b96e license: use SPDX identifiers for src/ headers (#3749)
* license: use SPDX identifiers for src/ headers

Replace the full ~20-line MIT license boilerplate on every src/ file with a
two-line SPDX tag (SPDX-FileCopyrightText + SPDX-License-Identifier), following
the REUSE convention used by CircuitPython and the Linux kernel. Removes ~3500
lines of duplicated boilerplate.
2026-07-02 21:55:45 +07:00
693cdce08e Merge remote-tracking branch 'tinyusb/master' into pr-osal-spin-deinit
Signed-off-by: HiFiPhile <admin@hifiphile.com>
2026-06-22 21:30:58 +02:00
41e9eaa65a usbd: add osal_spin_deinit() to tud_deinit()
Signed-off-by: HiFiPhile <admin@hifiphile.com>
2026-06-22 21:27:05 +02:00
7b791916a7 device: clamp EP0 OUT data copy to the control transfer buffer (#3705)
* device: clamp EP0 OUT data copy to the control transfer buffer

usbd_control_xfer_cb() copied xferred_bytes from the EP0 bounce buffer
into the requester's buffer with no bound. A non-compliant host that
sends an OUT data packet larger than the control transfer's data_len
(= min(len, wLength), the buffer capacity) would overflow that buffer
and over-count total_xferred. Clamp xferred_bytes to the remaining
buffer space before the memcpy and accounting.
2026-06-16 17:36:17 +07:00
97852816e8 midi2: align descriptors with USB-MIDI 2.0 spec
Brings the MIDI 2.0 device driver into full conformance with USB
Device Class Definition for MIDI Devices v2.0 (USB-IF, May 2020).

- Alt 1 MS Interface Header wTotalLength now reports 0x0007 per
  Table 5-2 ("set to match bLength"), replacing the prior 0x0011
  carried over from USB-MIDI 1.0 conventions.
- GET_DESCRIPTOR class request now validates bmRequestType direction,
  type and recipient plus wIndex and wValue high byte per Section 6.
- iBlockItem in the default Group Terminal Block is driven by
  CFG_TUD_MIDI2_BLOCK_STRIDX so applications can attach a UI string
  descriptor to the block per Table 5-6.
- UMP word byte order assumption (little-endian host per Section
  3.2.2) is documented inline so future big-endian ports know where
  to wrap access with tu_htole32 / tu_le32toh.

Validated on RP2040 and ESP32-P4 under Linux kernel 6.17: lsusb -v
reports wTotalLength = 0x0007 on Alt 1 MS Header (raw bytes
07 24 01 00 02 07 00). amidi -l enumerates Group Terminals exposed
via the class-specific GET_DESCRIPTOR response.
2026-05-12 11:07:45 -03:00
fa9edeff9c fix: address PR review feedback for MIDI 2.0 drivers
Host driver (midi2_host.c):
- midih2_open() now returns actual parsed length instead of max_len,
  preventing composite device interface conflicts
- Parsers (alt0/alt1) refactored to return const uint8_t* end pointer
  following midi_host.c switch/case pattern
- Alt 1 CS Endpoint now parses MIDI 2.0 layout (bNumGrpTrmBlk at
  offset 3 with MIDI_CS_ENDPOINT_GENERAL_2_0 subtype check) instead
  of reusing MIDI 1.0 struct (bNumEmbMIDIJack)
- midih2_set_config() now issues SET_INTERFACE control request via
  tuh_interface_set() before completing configuration. Falls back to
  alt 0 if SET_INTERFACE fails
- Extracted midih2_set_config_complete() and midih2_set_interface_cb()
  for async SET_INTERFACE handling

Device driver (midi2_device.c):
- midi2d_open() skip loop now checks bInterfaceNumber, stopping at
  interfaces that belong to other functions in composite devices
- SET_INTERFACE handler now rejects alt > 1 (returns false/stall)
- Named constants for GTB descriptor types and MIDI protocol values

Descriptor macros (usbd.h):
- TUD_MIDI2_DESC_ALT1_HEAD: iInterface set to 0 (consistent with
  Alt 0), wTotalLength now uses TUD_MIDI2_DESC_ALT1_CS_LEN to cover
  all Alt 1 class-specific descriptors
- TUD_MIDI2_DESC_ALT1_EP: now accepts GTB ID list via variadic args,
  emitting complete CS endpoint descriptor

Host example:
- CMakeLists.txt restricted to rp2040 family (display.c requires
  Pico SDK headers)
- display.c: null terminator after strncpy in log scroll

Documentation:
- class_drivers.rst updated to reflect SET_INTERFACE behavior and
  auto-select with fallback

Addresses: Codex P1 (#1, #2, #3), Copilot (#4-#9)
2026-05-12 11:07:44 -03:00
bc8ce76ae8 feat: add MIDI 2.0 Device class driver (USB-MIDI 2.0)
Add native USB-MIDI 2.0 Device class driver to TinyUSB. Implements the
USB-MIDI 2.0 specification with both Alt Setting 0 (MIDI 1.0 fallback)
and Alt Setting 1 (UMP native) descriptor support.

Driver features:
- UMP (Universal MIDI Packet) read/write with atomic message framing
- Protocol negotiation: Endpoint Discovery, Config Request/Notify,
  Function Block Discovery (embedded in driver)
- Group Terminal Block descriptor via GET_DESCRIPTOR
- Alt Setting switch handler with endpoint re-arm
- Static allocation, no dynamic memory, ISR-safe

Build system:
- Register midi2d_* in usbd.c driver table
- Add TUD_MIDI2_DESCRIPTOR macros to usbd.h
- Add config defaults (CFG_TUD_MIDI2_*) to tusb_option.h
- Add midi2_ump_word_count() to midi.h (shared by Device and Host)
- Add midi2_device.c/h to family.cmake and CMakeLists.txt
- Add midi2_device.h include to tusb.h

All changes guarded by #if CFG_TUD_MIDI2 (default 0). Zero impact on
existing drivers and examples.

Tested: Raspberry Pi Pico (RP2040), Linux ALSA, Windows MIDI Services
2026-05-12 11:07:44 -03:00
21fbee1e63 Merge remote-tracking branch 'tinyusb/master' into ncm_packet_filter
Signed-off-by: HiFiPhile <admin@hifiphile.com>
2026-05-08 13:13:08 +02:00
9b1b781c3e Merge pull request #3627 from hathach/merge-usbd-control
Refactor USB control transfer handling into `usbd.c`
2026-05-05 15:08:41 +07:00
e954302c10 fix usbd control to support wLength hack 2026-05-04 13:39:39 +07:00
9d68ed65f7 refactor: replace tu_edpt_state_t struct with uint8_t and update all endpoint state handling methods and accesses 2026-05-04 12:11:03 +07:00
a5e9ce5fbb refactor usbd.c: extract process_std_device_request for clarity 2026-05-04 10:29:29 +07:00
eb66712196 refactor usbd.c: centralize control transfer state management into _usbd_dev structure and remove usbd_control_reset 2026-05-04 08:55:08 +07:00
ea2de8be7e Merge branch 'master' into copilot/upgrade-net-lwip-webserver-descriptors 2026-05-03 14:36:25 +02:00
5951d07ad9 Merge remote-tracking branch 'origin/master' into ncm_packet_filter 2026-05-01 13:58:17 +02:00
8db6084aca ncm: implement GetNtbInputSize request
Signed-off-by: HiFiPHile <admin@hifiphile.com>
2026-05-01 13:54:22 +02:00
2931121b07 refactor capability
Signed-off-by: Zixun LI <admin@hifiphile.com>
2026-04-30 15:03:09 +02:00
9d3ad336bf deprecated usbd_control.c and merge its functionality into usbd.c 2026-04-29 17:27:59 +07:00
d529c5321f clean up 2026-04-29 14:56:47 +07:00
7d556a3ae1 Merge branch 'master' into fork/kira-live/master
# Conflicts:
#	src/device/usbd_control.c
2026-04-28 22:08:28 +07:00
405dadecc7 Merge branch 'master' into ncm_packet_filter 2026-04-27 15:16:16 +03:00
2cdd98104e NCM caps enum 2026-04-27 14:40:18 +03:00
d3107be360 usbd_control: consolidate status stage ep selection
Extract the "which endpoint is the Status stage on" rule into a single
TU_ATTR_ALWAYS_INLINE helper, and use it from both status_stage_xact()
and the completion callback. Replaces the two-operand wLength/direction
check with a direct endpoint-match comparison, matching the first
operand's pattern.

Per USB 2.0 §9.3.1, when wLength == 0 the bmRequestType Direction bit
is ignored and the Status stage is always IN; otherwise the Status
stage is opposite to the Data stage direction.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-26 11:51:49 +07:00
2dc90e364a Merge branch 'master' into musb-followup-3594
# Conflicts:
#	src/common/tusb_types.h
#	src/portable/mentor/musb/dcd_musb.c
2026-04-25 23:00:48 +07:00
0e4869a729 clean up 2026-04-25 14:41:08 +07:00
0649b18b2e NCM packet filter support 2026-04-24 22:39:02 +03:00
8a63f9c57e fix zero wLength request in control request 2026-04-24 09:43:13 +08:00
5c0c166246 Merge upstream master into net descriptor-based ep_size branch
- Resolve .gitignore conflict: incorporate upstream's .worktrees entry and
  expand dependency path patterns to cover all tools/get_deps.py fetched dirs
  (lib/, tools/linkermap, tools/uf2, hw/mcu/*) instead of listing only a few

- Auto-merged upstream changes: build system cleanups, BSP updates,
  portability fixes, new boards (nrf54lm20dk, stm32h743_weact),
  fatfs relocation, and many other upstream improvements

- Net driver changes (ecm_rndis_device.c, ncm_device.c, net_device.h,
  usbd.h, usb_descriptors.c) retain our PR's descriptor-based ep_size
  approach as our branch takes precedence

Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
2026-04-23 09:49:02 +00:00
5939831f17 remove duplicated tu_le16toh since we have converted the endian when setup. 2026-04-17 18:39:56 +08:00
34aded88ed fix(device): big-endian host support for SETUP packet handling
1. Add TU_LITTLE_ENDIAN_BITFIELD / TU_BIG_ENDIAN_BITFIELD macros in
tusb_compiler.h (GCC and IAR), following Linux kernel style.

2. Update bmAttributes (tusb_desc_endpoint_t) and bmRequestType_bit
(tusb_control_request_t) in tusb_types.h to use these macros with explicit
#error fallback if undefined.

3. Add tu_le16toh() conversion in dcd_event_setup_received() for
wValue/wIndex/wLength.

Tested on CIU98320B (big-endian ARM Cortex-M, full-speed HID keyboard).
2026-04-13 08:56:36 +08:00
3e5b79282a net: upgrade net_lwip_webserver to separate FS/HS descriptors and add bInterval to TUD_CDC_NCM_DESCRIPTOR
Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
Agent-Logs-Url: https://github.com/hathach/tinyusb/sessions/e212b526-e279-4a83-88bf-a742df293165
2026-03-22 19:43:46 +00:00
db7722dee8 Simplify tud_task() like in tuh_task() 2026-03-22 14:12:50 +01:00
2e29388051 refactor(usbd): replace _usbd_ctrl_epbuf with usbd_get_ctrl_buf() for cleaner abstraction and consistency across modules 2026-03-11 15:14:09 +07:00
336e89792f Merge branch 'master' into ep0_direct
# Conflicts:
#	hw/bsp/same7x/boards/same70_qmtech/board.cmake
#	hw/bsp/same7x/boards/same70_xplained/board.cmake
#	hw/bsp/same7x/family.cmake
2026-03-11 13:34:59 +07:00
73cd531295 replace printer_to_hid example with printer_to_cdc example
fix printer GET_DEVICE_ID request weird wIndex (interface high, alt low)
2026-03-06 17:22:38 +07:00
5838c7f09d update printer class: enhance descriptors, buffer sizes, and callbacks 2026-03-06 15:11:59 +07:00
558abb93af Merge branch 'refs/heads/master' into fork/remiberthoz/device-class-printer 2026-03-06 12:05:54 +07:00
ecc0e12dd5 Merge remote-tracking branch 'tinyusb/master' into dwc2_deinit
Signed-off-by: HiFiPhile <admin@hifiphile.com>
2026-02-27 11:47:58 +01:00
f0b44ec615 use CFG_TUD_CONFIGURE_DWC2_DEFAULT to make it easier to add more value 2026-02-13 22:29:14 +07:00
8f14cf4bfa add CFG_TUD_VBUS_SENSE, that could allow to skip tud_configure() for fixed vbus sensing
simplify dwc2_stm32_gccfg_cfg() using guid value
2026-02-13 19:45:09 +07:00
35a90f213d Merge branch 'refs/heads/master' into dwc2_vbus 2026-02-13 01:10:55 +07:00
fa2e076d72 Fix DFU descriptor version from 1.0.1 (0x0101) to 1.1.0 (0x0110)
Co-authored-by: hathach <249515+hathach@users.noreply.github.com>
2026-02-03 08:20:18 +00:00