Skip to content

Commit

Permalink
feature(dcd): Add check for max number of endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-marcisovsky committed Feb 13, 2024
1 parent ecc8c70 commit 5cbb51a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/device/dcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ TU_ATTR_WEAK bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t l
// Configure and enable an ISO endpoint according to descriptor
TU_ATTR_WEAK bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);

// Get max number of endpoints
void dcd_get_edpt_max_count(uint8_t rhport, uint8_t *in_edpt_max_count, uint8_t *out_edpt_max_count);

//--------------------------------------------------------------------+
// Event API (implemented by stack)
//--------------------------------------------------------------------+
Expand Down
25 changes: 25 additions & 0 deletions src/device/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ tu_static osal_queue_t _usbd_q;
static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request);
static bool process_set_config(uint8_t rhport, uint8_t cfg_num);
static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request);
static bool usbd_edpt_check_max_count(uint8_t rhport);

// from usbd_control.c
void usbd_control_reset(void);
Expand Down Expand Up @@ -396,6 +397,9 @@ bool tud_init (uint8_t rhport)
// skip if already initialized
if ( tud_inited() ) return true;

// check if required endpoint count is correct
TU_ASSERT(usbd_edpt_check_max_count(rhport));

TU_LOG(USBD_DBG, "USBD init on controller %u\r\n", rhport);
TU_LOG_INT(USBD_DBG, sizeof(usbd_device_t));
TU_LOG_INT(USBD_DBG, sizeof(tu_fifo_t));
Expand Down Expand Up @@ -1420,4 +1424,25 @@ bool usbd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * desc_ep
return dcd_edpt_iso_activate(rhport, desc_ep);
}

static bool usbd_edpt_check_max_count(uint8_t rhport)
{
uint8_t edpt_target_in_max = 0, edpt_target_out_max = 0;
dcd_get_edpt_max_count(rhport, &edpt_target_in_max, &edpt_target_out_max);
const uint8_t edpt_total_required_count = CFG_TUD_IN_EP_COUNT + CFG_TUD_OUT_EP_COUNT;
const uint8_t edpt_target_max = edpt_target_out_max;

if (edpt_total_required_count > edpt_target_max) {
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);
return false;
}

// ESP32Sx has 6 endpoints, from which only 5 can be confiugred as IN
// check if the device has no more than 5 IN endpoints
if (CFG_TUD_IN_EP_COUNT > edpt_target_in_max) {
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);
return false;
}

return true;
}
#endif
30 changes: 30 additions & 0 deletions src/device/usbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,36 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb
/* Endpoint Out */\
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0

//--------------------------------------------------------------------+
// Calculate used endpoint count
//--------------------------------------------------------------------+

// Calculate used IN endpoints
#define CFG_TUD_IN_EP_COUNT 2 * CFG_TUD_CDC + \
CFG_TUD_MSC + \
CFG_TUD_HID + \
CFG_TUD_MIDI + \
CFG_TUD_CUSTOM_CLASS + \
2 * CFG_TUD_ECM_RNDIS + \
2 * CFG_TUD_NCM + \
2 * CFG_TUD_DFU + \
2 * CFG_TUD_DFU_RUNTIME + \
2 * CFG_TUD_BTH + \
CFG_TUD_BTH_ISO_ALT_COUNT

// Calculate used OUT endpoints
#define CFG_TUD_OUT_EP_COUNT CFG_TUD_CDC + \
CFG_TUD_MSC + \
CFG_TUD_HID + \
CFG_TUD_MIDI + \
CFG_TUD_CUSTOM_CLASS + \
CFG_TUD_ECM_RNDIS + \
CFG_TUD_NCM + \
CFG_TUD_DFU + \
CFG_TUD_DFU_RUNTIME + \
CFG_TUD_BTH + \
CFG_TUD_BTH_ISO_ALT_COUNT

#ifdef __cplusplus
}
#endif
Expand Down
13 changes: 13 additions & 0 deletions src/portable/synopsys/dwc2/dcd_dwc2.c
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,19 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr)
}
}

void dcd_get_edpt_max_count(uint8_t rhport, uint8_t *in_edpt_max_count, uint8_t *out_edpt_max_count)
{
uint8_t const max_ep_count = _dwc2_controller[rhport].ep_count;

#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
*in_edpt_max_count = max_ep_count - 1; // esp32Sx has only 5 IN endpoints
*out_edpt_max_count = max_ep_count;
#else
*in_edpt_max_count = max_ep_count;
*out_edpt_max_count = max_ep_count;
#endif
}

/*------------------------------------------------------------------*/

// Read a single data packet from receive FIFO
Expand Down
2 changes: 1 addition & 1 deletion src/portable/synopsys/dwc2/dwc2_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
#endif

#if DWC2_FS_PERIPH_BASE
#define DWC2_FS_EP_MAX 6 // USB_OUT_EP_NUM. TODO ESP32Sx only has 5 tx fifo (5 endpoint IN)
#define DWC2_FS_EP_MAX 6 // USB_OUT_EP_NUM
#define DWC2_FS_EP_FIFO_SIZE 1024
#endif

Expand Down

0 comments on commit 5cbb51a

Please sign in to comment.