Skip to content

Commit 5cbb51a

Browse files
feature(dcd): Add check for max number of endpoints
1 parent ecc8c70 commit 5cbb51a

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

src/device/dcd.h

+3
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ TU_ATTR_WEAK bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t l
190190
// Configure and enable an ISO endpoint according to descriptor
191191
TU_ATTR_WEAK bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
192192

193+
// Get max number of endpoints
194+
void dcd_get_edpt_max_count(uint8_t rhport, uint8_t *in_edpt_max_count, uint8_t *out_edpt_max_count);
195+
193196
//--------------------------------------------------------------------+
194197
// Event API (implemented by stack)
195198
//--------------------------------------------------------------------+

src/device/usbd.c

+25
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ tu_static osal_queue_t _usbd_q;
297297
static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request);
298298
static bool process_set_config(uint8_t rhport, uint8_t cfg_num);
299299
static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request);
300+
static bool usbd_edpt_check_max_count(uint8_t rhport);
300301

301302
// from usbd_control.c
302303
void usbd_control_reset(void);
@@ -396,6 +397,9 @@ bool tud_init (uint8_t rhport)
396397
// skip if already initialized
397398
if ( tud_inited() ) return true;
398399

400+
// check if required endpoint count is correct
401+
TU_ASSERT(usbd_edpt_check_max_count(rhport));
402+
399403
TU_LOG(USBD_DBG, "USBD init on controller %u\r\n", rhport);
400404
TU_LOG_INT(USBD_DBG, sizeof(usbd_device_t));
401405
TU_LOG_INT(USBD_DBG, sizeof(tu_fifo_t));
@@ -1420,4 +1424,25 @@ bool usbd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * desc_ep
14201424
return dcd_edpt_iso_activate(rhport, desc_ep);
14211425
}
14221426

1427+
static bool usbd_edpt_check_max_count(uint8_t rhport)
1428+
{
1429+
uint8_t edpt_target_in_max = 0, edpt_target_out_max = 0;
1430+
dcd_get_edpt_max_count(rhport, &edpt_target_in_max, &edpt_target_out_max);
1431+
const uint8_t edpt_total_required_count = CFG_TUD_IN_EP_COUNT + CFG_TUD_OUT_EP_COUNT;
1432+
const uint8_t edpt_target_max = edpt_target_out_max;
1433+
1434+
if (edpt_total_required_count > edpt_target_max) {
1435+
TU_LOG(USBD_DBG, "Number of required endpoints: %d is more than max number of endpoints on target: %d\r\n", edpt_total_required_count, edpt_target_max);
1436+
return false;
1437+
}
1438+
1439+
// ESP32Sx has 6 endpoints, from which only 5 can be confiugred as IN
1440+
// check if the device has no more than 5 IN endpoints
1441+
if (CFG_TUD_IN_EP_COUNT > edpt_target_in_max) {
1442+
TU_LOG(USBD_DBG, "Number of required IN endpoints: %d is more than max number of IN endpoints on target: %d\r\n", CFG_TUD_IN_EP_COUNT, edpt_target_in_max);
1443+
return false;
1444+
}
1445+
1446+
return true;
1447+
}
14231448
#endif

src/device/usbd.h

+30
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,36 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb
858858
/* Endpoint Out */\
859859
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
860860

861+
//--------------------------------------------------------------------+
862+
// Calculate used endpoint count
863+
//--------------------------------------------------------------------+
864+
865+
// Calculate used IN endpoints
866+
#define CFG_TUD_IN_EP_COUNT 2 * CFG_TUD_CDC + \
867+
CFG_TUD_MSC + \
868+
CFG_TUD_HID + \
869+
CFG_TUD_MIDI + \
870+
CFG_TUD_CUSTOM_CLASS + \
871+
2 * CFG_TUD_ECM_RNDIS + \
872+
2 * CFG_TUD_NCM + \
873+
2 * CFG_TUD_DFU + \
874+
2 * CFG_TUD_DFU_RUNTIME + \
875+
2 * CFG_TUD_BTH + \
876+
CFG_TUD_BTH_ISO_ALT_COUNT
877+
878+
// Calculate used OUT endpoints
879+
#define CFG_TUD_OUT_EP_COUNT CFG_TUD_CDC + \
880+
CFG_TUD_MSC + \
881+
CFG_TUD_HID + \
882+
CFG_TUD_MIDI + \
883+
CFG_TUD_CUSTOM_CLASS + \
884+
CFG_TUD_ECM_RNDIS + \
885+
CFG_TUD_NCM + \
886+
CFG_TUD_DFU + \
887+
CFG_TUD_DFU_RUNTIME + \
888+
CFG_TUD_BTH + \
889+
CFG_TUD_BTH_ISO_ALT_COUNT
890+
861891
#ifdef __cplusplus
862892
}
863893
#endif

src/portable/synopsys/dwc2/dcd_dwc2.c

+13
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,19 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr)
923923
}
924924
}
925925

926+
void dcd_get_edpt_max_count(uint8_t rhport, uint8_t *in_edpt_max_count, uint8_t *out_edpt_max_count)
927+
{
928+
uint8_t const max_ep_count = _dwc2_controller[rhport].ep_count;
929+
930+
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
931+
*in_edpt_max_count = max_ep_count - 1; // esp32Sx has only 5 IN endpoints
932+
*out_edpt_max_count = max_ep_count;
933+
#else
934+
*in_edpt_max_count = max_ep_count;
935+
*out_edpt_max_count = max_ep_count;
936+
#endif
937+
}
938+
926939
/*------------------------------------------------------------------*/
927940

928941
// Read a single data packet from receive FIFO

src/portable/synopsys/dwc2/dwc2_esp32.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
#endif
5656

5757
#if DWC2_FS_PERIPH_BASE
58-
#define DWC2_FS_EP_MAX 6 // USB_OUT_EP_NUM. TODO ESP32Sx only has 5 tx fifo (5 endpoint IN)
58+
#define DWC2_FS_EP_MAX 6 // USB_OUT_EP_NUM
5959
#define DWC2_FS_EP_FIFO_SIZE 1024
6060
#endif
6161

0 commit comments

Comments
 (0)