Skip to content

Commit

Permalink
feature(dcd): Check avaliable EPs while opening them
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-marcisovsky committed Feb 27, 2024
1 parent 3f8200b commit 4c24ffb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 72 deletions.
3 changes: 0 additions & 3 deletions src/device/dcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,6 @@ 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: 0 additions & 25 deletions src/device/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ 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 @@ -397,9 +396,6 @@ 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 @@ -1424,25 +1420,4 @@ 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 = TU_MAX(edpt_target_out_max, edpt_target_in_max);

if (edpt_total_required_count > edpt_target_max) {
TU_LOG(1, "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(1, "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: 0 additions & 30 deletions src/device/usbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -858,36 +858,6 @@ 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
36 changes: 22 additions & 14 deletions src/portable/synopsys/dwc2/dcd_dwc2.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,27 @@ void dcd_sof_enable(uint8_t rhport, bool en)
/* DCD Endpoint port
*------------------------------------------------------------------*/

// Check is IN/OUT endpoint is avaliable before opening it
static bool dcd_edpt_check_if_avaliable(uint8_t rhport, uint8_t direction)
{
if (direction) { // IN direction
if (ep_avaliable_count[rhport].in_ep < 1) {
TU_LOG(1, "Trying to open IN endpoint, but max number of IN endpoints already opened on this target \r\n");
return false;
}
} else { // OUT direction
if (ep_avaliable_count[rhport].out_ep < 1) {
TU_LOG(1, "Trying to open OUT endpoint, but max number of OUT endpoints already opened on this target \r\n");
return false;
}
}

ep_avaliable_count[rhport].in_ep--;
ep_avaliable_count[rhport].out_ep--;

return true;
}

bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
{
(void) rhport;
Expand All @@ -640,7 +661,7 @@ bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt)
uint8_t const epnum = tu_edpt_number(desc_edpt->bEndpointAddress);
uint8_t const dir = tu_edpt_dir(desc_edpt->bEndpointAddress);

TU_ASSERT(epnum < ep_count);
TU_ASSERT(dcd_edpt_check_if_avaliable(rhport, dir));

xfer_ctl_t * xfer = XFER_CTL_BASE(epnum, dir);
xfer->max_size = tu_edpt_packet_size(desc_edpt);
Expand Down Expand Up @@ -923,19 +944,6 @@ 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
16 changes: 16 additions & 0 deletions src/portable/synopsys/dwc2/dwc2_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ static const dwc2_controller_t _dwc2_controller[] =
#endif
};

static ep_avaliable_count_t ep_avaliable_count[] =
{
#ifdef DWC2_FS_PERIPH_BASE
// ESP32Sx has 6 endpoints, from which only 5 can be confiugred as IN
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
{ .out_ep = DWC2_FS_EP_MAX, .in_ep = DWC2_FS_EP_MAX - 1},
#else
{ .out_ep = DWC2_FS_EP_MAX, .in_ep = DWC2_FS_EP_MAX},
#endif
#endif

#ifdef DWC2_HS_PERIPH_BASE
{ .out_ep = DWC2_HS_EP_MAX, .in_ep = DWC2_HS_EP_MAX},
#endif
};

static intr_handle_t usb_ih[DWC2_PERIPH_COUNT];

static void dcd_int_handler_wrap(void* arg)
Expand Down
6 changes: 6 additions & 0 deletions src/portable/synopsys/dwc2/dwc2_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ typedef struct
uint32_t ep_fifo_size;
}dwc2_controller_t;

typedef struct
{
int8_t out_ep;
int8_t in_ep;
}ep_avaliable_count_t;

/* DWC OTG HW Release versions */
#define DWC2_CORE_REV_2_71a 0x4f54271a
#define DWC2_CORE_REV_2_72a 0x4f54272a
Expand Down

0 comments on commit 4c24ffb

Please sign in to comment.