Skip to content

Commit bd15f65

Browse files
authored
Merge pull request #2710 from hathach/dwc2-test-mode-followup
2 parents b97520e + 4ce1cce commit bd15f65

File tree

6 files changed

+32
-71
lines changed

6 files changed

+32
-71
lines changed

src/common/tusb_mcu.h

-3
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@
195195
#elif TU_CHECK_MCU(OPT_MCU_STM32F4)
196196
#define TUP_USBIP_DWC2
197197
#define TUP_USBIP_DWC2_STM32
198-
#define TUP_USBIP_DWC2_TEST_MODE
199198

200199
// For most mcu, FS has 4, HS has 6. TODO 446/469/479 HS has 9
201200
#define TUP_DCD_ENDPOINT_MAX 6
@@ -210,7 +209,6 @@
210209
// MCU with on-chip HS Phy
211210
#if defined(STM32F723xx) || defined(STM32F730xx) || defined(STM32F733xx)
212211
#define TUP_RHPORT_HIGHSPEED 1 // Port0: FS, Port1: HS
213-
#define TUP_USBIP_DWC2_TEST_MODE
214212
#endif
215213

216214
#elif TU_CHECK_MCU(OPT_MCU_STM32H7)
@@ -285,7 +283,6 @@
285283
defined(STM32U5F7xx) || defined(STM32U5F9xx) || defined(STM32U5G7xx) || defined(STM32U5G9xx)
286284
#define TUP_DCD_ENDPOINT_MAX 9
287285
#define TUP_RHPORT_HIGHSPEED 1
288-
#define TUP_USBIP_DWC2_TEST_MODE
289286
#else
290287
#define TUP_DCD_ENDPOINT_MAX 6
291288
#endif

src/common/tusb_types.h

+9
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ enum {
214214

215215
#define TUSB_DESC_CONFIG_POWER_MA(x) ((x)/2)
216216

217+
// USB 2.0 Spec Table 9-7: Test Mode Selectors
218+
typedef enum {
219+
TUSB_FEATURE_TEST_J = 1,
220+
TUSB_FEATURE_TEST_K,
221+
TUSB_FEATURE_TEST_SE0_NAK,
222+
TUSB_FEATURE_TEST_PACKET,
223+
TUSB_FEATURE_TEST_FORCE_ENABLE,
224+
} tusb_feature_test_mode_t;
225+
217226
//--------------------------------------------------------------------+
218227
//
219228
//--------------------------------------------------------------------+

src/device/dcd.h

+1-12
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,6 @@ typedef struct TU_ATTR_ALIGNED(4) {
8989
};
9090
} dcd_event_t;
9191

92-
typedef enum {
93-
TEST_J = 1,
94-
TEST_K,
95-
TEST_SE0_NAK,
96-
TEST_PACKET,
97-
TEST_FORCE_ENABLE,
98-
} test_mode_t;
99-
10092
//TU_VERIFY_STATIC(sizeof(dcd_event_t) <= 12, "size is not correct");
10193

10294
//--------------------------------------------------------------------+
@@ -150,11 +142,8 @@ void dcd_disconnect(uint8_t rhport);
150142
void dcd_sof_enable(uint8_t rhport, bool en);
151143

152144
#if CFG_TUD_TEST_MODE
153-
// Check if the test mode is supported, returns true is test mode selector is supported
154-
bool dcd_check_test_mode_support(test_mode_t test_selector) TU_ATTR_WEAK;
155-
156145
// Put device into a test mode (needs power cycle to quit)
157-
void dcd_enter_test_mode(uint8_t rhport, test_mode_t test_selector) TU_ATTR_WEAK;
146+
void dcd_enter_test_mode(uint8_t rhport, tusb_feature_test_mode_t test_selector);
158147
#endif
159148
//--------------------------------------------------------------------+
160149
// Endpoint API

src/device/usbd.c

+17-39
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,17 @@ TU_ATTR_ALWAYS_INLINE static inline bool queue_event(dcd_event_t const * event,
321321
static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request);
322322
static bool process_set_config(uint8_t rhport, uint8_t cfg_num);
323323
static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request);
324+
324325
#if CFG_TUD_TEST_MODE
325-
static bool process_test_mode_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
326+
static bool process_test_mode_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) {
327+
TU_VERIFY(CONTROL_STAGE_ACK == stage);
328+
uint8_t const selector = tu_u16_high(request->wIndex);
329+
TU_LOG_USBD(" Enter Test Mode (test selector index: %d)\r\n", selector);
330+
dcd_enter_test_mode(rhport, (tusb_feature_test_mode_t) selector);
331+
return true;
332+
}
326333
#endif
334+
327335
// from usbd_control.c
328336
void usbd_control_reset(void);
329337
void usbd_control_set_request(tusb_control_request_t const *request);
@@ -695,7 +703,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
695703
}
696704

697705
if ( TUSB_REQ_TYPE_STANDARD != p_request->bmRequestType_bit.type ) {
698-
// Non standard request is not supported
706+
// Non-standard request is not supported
699707
TU_BREAKPOINT();
700708
return false;
701709
}
@@ -759,43 +767,27 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
759767
break;
760768

761769
case TUSB_REQ_SET_FEATURE:
762-
// Handle the feature selector
763-
switch(p_request->wValue)
764-
{
765-
// Support for remote wakeup
770+
switch(p_request->wValue) {
766771
case TUSB_REQ_FEATURE_REMOTE_WAKEUP:
767772
TU_LOG_USBD(" Enable Remote Wakeup\r\n");
768-
769773
// Host may enable remote wake up before suspending especially HID device
770774
_usbd_dev.remote_wakeup_en = true;
771775
tud_control_status(rhport, p_request);
772776
break;
773777

774-
#if CFG_TUD_TEST_MODE
775-
// Support for TEST_MODE
778+
#if CFG_TUD_TEST_MODE
776779
case TUSB_REQ_FEATURE_TEST_MODE: {
777780
// Only handle the test mode if supported and valid
778-
TU_VERIFY(dcd_enter_test_mode && dcd_check_test_mode_support && 0 == tu_u16_low(p_request->wIndex));
779-
780-
uint8_t selector = tu_u16_high(p_request->wIndex);
781-
782-
// Stall request if the selected test mode isn't supported
783-
if (!dcd_check_test_mode_support((test_mode_t)selector))
784-
{
785-
TU_LOG_USBD(" Unsupported Test Mode (test selector index: %d)\r\n", selector);
781+
TU_VERIFY(0 == tu_u16_low(p_request->wIndex));
786782

787-
return false;
788-
}
789-
790-
// Acknowledge request
791-
tud_control_status(rhport, p_request);
792-
793-
TU_LOG_USBD(" Enter Test Mode (test selector index: %d)\r\n", selector);
783+
uint8_t const selector = tu_u16_high(p_request->wIndex);
784+
TU_VERIFY(TUSB_FEATURE_TEST_J <= selector && selector <= TUSB_FEATURE_TEST_FORCE_ENABLE);
794785

795786
usbd_control_set_complete_callback(process_test_mode_cb);
787+
tud_control_status(rhport, p_request);
796788
break;
797789
}
798-
#endif /* CFG_TUD_TEST_MODE */
790+
#endif /* CFG_TUD_TEST_MODE */
799791

800792
// Stall unsupported feature selector
801793
default: return false;
@@ -1127,20 +1119,6 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
11271119
}
11281120
}
11291121

1130-
#if CFG_TUD_TEST_MODE
1131-
static bool process_test_mode_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
1132-
{
1133-
// At this point it should already be ensured that dcd_enter_test_mode() is defined
1134-
1135-
// Only enter the test mode after the request for it has completed
1136-
TU_VERIFY(CONTROL_STAGE_ACK == stage);
1137-
1138-
dcd_enter_test_mode(rhport, (test_mode_t)tu_u16_high(request->wIndex));
1139-
1140-
return true;
1141-
}
1142-
#endif /* CFG_TUD_TEST_MODE */
1143-
11441122
//--------------------------------------------------------------------+
11451123
// DCD Event Handler
11461124
//--------------------------------------------------------------------+

src/portable/synopsys/dwc2/dcd_dwc2.c

+4-16
Original file line numberDiff line numberDiff line change
@@ -1195,25 +1195,13 @@ void dcd_int_handler(uint8_t rhport) {
11951195
// }
11961196
}
11971197

1198-
#if defined(TUP_USBIP_DWC2_TEST_MODE) && CFG_TUD_TEST_MODE
1199-
1200-
bool dcd_check_test_mode_support(test_mode_t test_selector) {
1201-
// Check if test mode selector is unsupported
1202-
if (TEST_FORCE_ENABLE < test_selector || TEST_J > test_selector) {
1203-
return false;
1204-
}
1205-
1206-
return true;
1207-
}
1208-
1209-
void dcd_enter_test_mode(uint8_t rhport, test_mode_t test_selector) {
1210-
// Get port address...
1198+
#if CFG_TUD_TEST_MODE
1199+
void dcd_enter_test_mode(uint8_t rhport, tusb_feature_test_mode_t test_selector) {
12111200
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
12121201

12131202
// Enable the test mode
1214-
dwc2->dctl = (dwc2->dctl & ~DCTL_TCTL_Msk) | (test_selector << DCTL_TCTL_Pos);
1203+
dwc2->dctl = (dwc2->dctl & ~DCTL_TCTL_Msk) | (((uint8_t) test_selector) << DCTL_TCTL_Pos);
12151204
}
1216-
1217-
#endif /* TUP_USBIP_DWC2_TEST_MODE && CFG_TUD_TEST_MODE */
1205+
#endif
12181206

12191207
#endif

src/tusb_option.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@
381381
#error "CFG_TUD_ENDPPOINT_MAX must be less than or equal to TUP_DCD_ENDPOINT_MAX"
382382
#endif
383383

384-
// USB 2.0 compliance test mode support
384+
// USB 2.0 7.1.20: compliance test mode support
385385
#ifndef CFG_TUD_TEST_MODE
386386
#define CFG_TUD_TEST_MODE 0
387387
#endif

0 commit comments

Comments
 (0)