Skip to content

Commit 4c7fd70

Browse files
authored
Merge pull request #3597 from canokeys/big-endian-for-setup-packets
fix #3596: big-endian host support for SETUP packet handling
2 parents 1e64433 + 5939831 commit 4c7fd70

9 files changed

Lines changed: 46 additions & 15 deletions

File tree

examples/device/audio_test_multi_rate/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ static bool audio20_get_req_entity(uint8_t rhport, tusb_control_request_t const
532532
bool tud_audio_set_itf_cb(uint8_t rhport, tusb_control_request_t const *p_request) {
533533
(void) rhport;
534534
//uint8_t const itf = tu_u16_low(tu_le16toh(p_request->wIndex));
535-
uint8_t const alt = tu_u16_low(tu_le16toh(p_request->wValue));
535+
uint8_t const alt = tu_u16_low(p_request->wValue);
536536

537537
// Clear buffer when streaming format is changed
538538
if (alt != 0) {

examples/device/cdc_uac2/src/uac2_app.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ bool tud_audio_set_itf_close_ep_cb(uint8_t rhport, tusb_control_request_t const
263263
{
264264
(void)rhport;
265265

266-
uint8_t const itf = tu_u16_low(tu_le16toh(p_request->wIndex));
267-
uint8_t const alt = tu_u16_low(tu_le16toh(p_request->wValue));
266+
uint8_t const itf = tu_u16_low(p_request->wIndex);
267+
uint8_t const alt = tu_u16_low(p_request->wValue);
268268

269269
if (ITF_NUM_AUDIO_STREAMING_SPK == itf && alt == 0) {
270270
// Audio streaming stop
@@ -277,8 +277,8 @@ bool tud_audio_set_itf_close_ep_cb(uint8_t rhport, tusb_control_request_t const
277277
bool tud_audio_set_itf_cb(uint8_t rhport, tusb_control_request_t const * p_request)
278278
{
279279
(void)rhport;
280-
uint8_t const itf = tu_u16_low(tu_le16toh(p_request->wIndex));
281-
uint8_t const alt = tu_u16_low(tu_le16toh(p_request->wValue));
280+
uint8_t const itf = tu_u16_low(p_request->wIndex);
281+
uint8_t const alt = tu_u16_low(p_request->wValue);
282282

283283
TU_LOG2("Set interface %d alt %d\r\n", itf, alt);
284284
if (ITF_NUM_AUDIO_STREAMING_SPK == itf && alt != 0) {

examples/device/uac2_headset/src/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,8 @@ bool tud_audio_set_req_entity_cb(uint8_t rhport, tusb_control_request_t const *p
522522
bool tud_audio_set_itf_close_ep_cb(uint8_t rhport, tusb_control_request_t const *p_request) {
523523
(void) rhport;
524524

525-
uint8_t const itf = tu_u16_low(tu_le16toh(p_request->wIndex));
526-
uint8_t const alt = tu_u16_low(tu_le16toh(p_request->wValue));
525+
uint8_t const itf = tu_u16_low(p_request->wIndex);
526+
uint8_t const alt = tu_u16_low(p_request->wValue);
527527

528528
if (ITF_NUM_AUDIO_STREAMING_SPK == itf && alt == 0) {
529529
blink_interval_ms = BLINK_MOUNTED;
@@ -534,8 +534,8 @@ bool tud_audio_set_itf_close_ep_cb(uint8_t rhport, tusb_control_request_t const
534534

535535
bool tud_audio_set_itf_cb(uint8_t rhport, tusb_control_request_t const *p_request) {
536536
(void) rhport;
537-
uint8_t const itf = tu_u16_low(tu_le16toh(p_request->wIndex));
538-
uint8_t const alt = tu_u16_low(tu_le16toh(p_request->wValue));
537+
uint8_t const itf = tu_u16_low(p_request->wIndex);
538+
uint8_t const alt = tu_u16_low(p_request->wValue);
539539

540540
TU_LOG2("Set interface %d alt %d\r\n", itf, alt);
541541
if (ITF_NUM_AUDIO_STREAMING_SPK == itf && alt != 0) {

examples/device/uac2_speaker_fb/src/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ static bool audio20_set_req_entity(tusb_control_request_t const *p_request, uint
457457

458458
bool tud_audio_set_itf_cb(uint8_t rhport, tusb_control_request_t const *p_request) {
459459
(void) rhport;
460-
uint8_t const itf = tu_u16_low(tu_le16toh(p_request->wIndex));
461-
uint8_t const alt = tu_u16_low(tu_le16toh(p_request->wValue));
460+
uint8_t const itf = tu_u16_low(p_request->wIndex);
461+
uint8_t const alt = tu_u16_low(p_request->wValue);
462462

463463
TU_LOG2("Set interface %d alt %d\r\n", itf, alt);
464464
if (ITF_NUM_AUDIO_STREAMING == itf && alt != 0)
@@ -531,8 +531,8 @@ bool tud_audio_get_req_entity_cb(uint8_t rhport, tusb_control_request_t const *p
531531
bool tud_audio_set_itf_close_ep_cb(uint8_t rhport, tusb_control_request_t const *p_request) {
532532
(void) rhport;
533533

534-
uint8_t const itf = tu_u16_low(tu_le16toh(p_request->wIndex));
535-
uint8_t const alt = tu_u16_low(tu_le16toh(p_request->wValue));
534+
uint8_t const itf = tu_u16_low(p_request->wIndex);
535+
uint8_t const alt = tu_u16_low(p_request->wValue);
536536

537537
if (ITF_NUM_AUDIO_STREAMING == itf && alt == 0) {
538538
blink_interval_ms = BLINK_MOUNTED;

src/class/mtp/mtp_device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ bool mtpd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
321321
.session_id = p_mtp->session_id,
322322
.request = request,
323323
.buf = p_mtp->control_buf,
324-
.bufsize = tu_le16toh(request->wLength),
324+
.bufsize = request->wLength,
325325
};
326326

327327
switch (request->bRequest) {

src/common/tusb_compiler.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
#define TU_LITTLE_ENDIAN (0x12u)
6767
#define TU_BIG_ENDIAN (0x21u)
6868

69+
#define TU_BITFIELD_LE (0x34u)
70+
#define TU_BITFIELD_BE (0x43u)
71+
6972
/*------------------------------------------------------------------*/
7073
/* Count number of arguments of __VA_ARGS__
7174
* - reference www.stackoverflow.com/questions/2124339/c-preprocessor-va-args-number-of-arguments
@@ -167,8 +170,10 @@
167170
// For TI ARM compiler, __BYTE_ORDER__ is not defined for MSP430 but still LE
168171
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || defined(__MSP430__)
169172
#define TU_BYTE_ORDER TU_LITTLE_ENDIAN
173+
#define TU_BITFIELD_ORDER TU_BITFIELD_LE
170174
#else
171175
#define TU_BYTE_ORDER TU_BIG_ENDIAN
176+
#define TU_BITFIELD_ORDER TU_BITFIELD_BE
172177
#endif
173178

174179
// Unfortunately XC16 doesn't provide builtins for 32bit endian conversion
@@ -212,8 +217,10 @@
212217
// Endian conversion use well-known host to network (big endian) naming
213218
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
214219
#define TU_BYTE_ORDER TU_LITTLE_ENDIAN
220+
#define TU_BITFIELD_ORDER TU_BITFIELD_LE
215221
#else
216222
#define TU_BYTE_ORDER TU_BIG_ENDIAN
223+
#define TU_BITFIELD_ORDER TU_BITFIELD_BE
217224
#endif
218225

219226
#define TU_BSWAP16(u16) (__iar_builtin_REV16(u16))
@@ -239,8 +246,10 @@
239246
// Endian conversion use well-known host to network (big endian) naming
240247
#if defined(__LIT)
241248
#define TU_BYTE_ORDER TU_LITTLE_ENDIAN
249+
#define TU_BITFIELD_ORDER TU_BITFIELD_LE
242250
#else
243251
#define TU_BYTE_ORDER TU_BIG_ENDIAN
252+
#define TU_BITFIELD_ORDER TU_BITFIELD_BE
244253
#endif
245254

246255
#define TU_BSWAP16(u16) ((unsigned short)_builtin_revw((unsigned long)u16))

src/common/tusb_types.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,19 @@ typedef struct TU_ATTR_PACKED {
409409
uint8_t bEndpointAddress ; // The address of the endpoint
410410

411411
struct TU_ATTR_PACKED {
412+
#if (TU_BITFIELD_ORDER == TU_BITFIELD_LE)
412413
uint8_t xfer : 2; // Control, ISO, Bulk, Interrupt
413414
uint8_t sync : 2; // None, Asynchronous, Adaptive, Synchronous
414415
uint8_t usage : 2; // Data, Feedback, Implicit feedback
415416
uint8_t : 2;
417+
#elif (TU_BITFIELD_ORDER == TU_BITFIELD_BE)
418+
uint8_t : 2;
419+
uint8_t usage : 2;
420+
uint8_t sync : 2;
421+
uint8_t xfer : 2;
422+
#else
423+
#error "Please define TU_BITFIELD_ORDER as TU_BITFIELD_LE or TU_BITFIELD_BE"
424+
#endif
416425
} bmAttributes;
417426

418427
uint16_t wMaxPacketSize ; // Bit 10..0 : max packet size, bit 12..11 additional transaction per highspeed micro-frame
@@ -522,9 +531,17 @@ typedef struct TU_ATTR_PACKED {
522531
typedef struct TU_ATTR_PACKED {
523532
union {
524533
struct TU_ATTR_PACKED {
534+
#if (TU_BITFIELD_ORDER == TU_BITFIELD_LE)
525535
uint8_t recipient : 5; ///< Recipient type tusb_request_recipient_t.
526536
uint8_t type : 2; ///< Request type tusb_request_type_t.
527537
uint8_t direction : 1; ///< Direction type. tusb_dir_t
538+
#elif (TU_BITFIELD_ORDER == TU_BITFIELD_BE)
539+
uint8_t direction : 1; ///< Direction type. tusb_dir_t
540+
uint8_t type : 2; ///< Request type tusb_request_type_t.
541+
uint8_t recipient : 5; ///< Recipient type tusb_request_recipient_t.
542+
#else
543+
#error "Please define TU_BITFIELD_ORDER as TU_BITFIELD_LE or TU_BITFIELD_BE"
544+
#endif
528545
} bmRequestType_bit;
529546

530547
uint8_t bmRequestType;

src/device/dcd.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ TU_ATTR_ALWAYS_INLINE static inline void dcd_event_setup_received(uint8_t rhport
219219
event.rhport = rhport;
220220
event.event_id = DCD_EVENT_SETUP_RECEIVED;
221221
(void) memcpy(&event.setup_received, setup, sizeof(tusb_control_request_t));
222+
// USB wire format is little-endian. Convert multi-byte fields to host byte order
223+
// so the stack always sees correct values regardless of CPU endianness.
224+
event.setup_received.wValue = tu_le16toh(event.setup_received.wValue);
225+
event.setup_received.wIndex = tu_le16toh(event.setup_received.wIndex);
226+
event.setup_received.wLength = tu_le16toh(event.setup_received.wLength);
222227
dcd_event_handler(&event, in_isr);
223228
}
224229

src/device/usbd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
12121212
TU_LOG_USBD(" String[%u]\r\n", desc_index);
12131213

12141214
// String Descriptor always uses the desc set from user
1215-
uint8_t const* desc_str = (uint8_t const*) tud_descriptor_string_cb(desc_index, tu_le16toh(p_request->wIndex));
1215+
uint8_t const* desc_str = (uint8_t const*) tud_descriptor_string_cb(desc_index, p_request->wIndex);
12161216
TU_VERIFY(desc_str);
12171217

12181218
// first byte of descriptor is its size

0 commit comments

Comments
 (0)