Skip to content

Correct endianness handling for big-endian hosts in SETUP packets#3595

Closed
dangfan wants to merge 1 commit into
hathach:masterfrom
canokeys:big-endian-for-setup-packets
Closed

Correct endianness handling for big-endian hosts in SETUP packets#3595
dangfan wants to merge 1 commit into
hathach:masterfrom
canokeys:big-endian-for-setup-packets

Conversation

@dangfan
Copy link
Copy Markdown
Contributor

@dangfan dangfan commented Apr 12, 2026

USB wire format is little-endian, but TinyUSB assumed host byte order == little-endian
in two places:

  1. dcd_event_setup_received() (dcd.h): after memcpy-ing the raw SETUP bytes into
    tusb_control_request_t, wValue/wIndex/wLength were left in wire byte order.
    Fix: apply tu_le16toh() on each field. On LE hosts this is a no-op.

  2. Bit-field structs (tusb_types.h): GCC on big-endian ARM allocates bit fields
    from the MSB downward, opposite to the USB spec's LSB-first numbering.
    Fix: reverse the field declaration order under TU_BIG_ENDIAN for
    tusb_desc_endpoint_t.bmAttributes and tusb_control_request_t.bmRequestType_bit.

Tested on HED CIU98320B (big-endian ARM, full-speed device) with HID keyboard demo:
full enumeration and keystroke transmission verified.

…P packets

USB wire format is little-endian, but TinyUSB assumed host byte order ==
little-endian in two places:

1. dcd_event_setup_received() (dcd.h): after memcpy-ing the raw SETUP bytes
   into tusb_control_request_t, wValue/wIndex/wLength were left in wire byte
   order.  Fix: apply tu_le16toh() on each field.  On LE hosts this is a no-op.

2. Bit-field structs (tusb_types.h): GCC on big-endian ARM allocates bit fields
   from the MSB downward, opposite to the USB spec's LSB-first numbering.
   Fix: reverse the field declaration order under TU_BIG_ENDIAN for
   tusb_desc_endpoint_t.bmAttributes and tusb_control_request_t.bmRequestType_bit.

Tested on CIU98320B (big-endian ARM, full-speed device) with HID keyboard demo:
full enumeration and keystroke transmission verified.
@dangfan dangfan closed this Apr 12, 2026
@dangfan dangfan deleted the big-endian-for-setup-packets branch April 13, 2026 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant