Skip to content

Commit 388360c

Browse files
tore-espressifDazza0
authored andcommitted
fix(usb/host): Correctly parse MPS fields in HighSpeed EP descriptors
Bits [11,12] in HighSpeed periodic endpoints specify the number of additional transaction opportunities per microframe
1 parent 7cfe227 commit 388360c

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

components/usb/hcd_dwc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,9 +1536,9 @@ static bool pipe_alloc_hcd_support_verification(usb_dwc_hal_context_t *hal, cons
15361536
}
15371537
}
15381538

1539-
if (ep_desc->wMaxPacketSize > limit) {
1539+
if (USB_EP_DESC_GET_MPS(ep_desc) > limit) {
15401540
ESP_LOGE(HCD_DWC_TAG, "EP MPS (%d) exceeds supported limit (%d)",
1541-
ep_desc->wMaxPacketSize,
1541+
USB_EP_DESC_GET_MPS(ep_desc),
15421542
limit);
15431543
return false;
15441544
}
@@ -1571,7 +1571,7 @@ static void pipe_set_ep_char(const hcd_pipe_config_t *pipe_config, usb_transfer_
15711571
ep_char->mps = (pipe_config->dev_speed == USB_SPEED_LOW) ? CTRL_EP_MAX_MPS_LS : CTRL_EP_MAX_MPS_HSFS;
15721572
} else {
15731573
ep_char->bEndpointAddress = pipe_config->ep_desc->bEndpointAddress;
1574-
ep_char->mps = pipe_config->ep_desc->wMaxPacketSize;
1574+
ep_char->mps = USB_EP_DESC_GET_MPS(pipe_config->ep_desc);
15751575
}
15761576
ep_char->dev_addr = pipe_config->dev_addr;
15771577
ep_char->ls_via_fs_hub = (port_speed == USB_SPEED_FULL && pipe_config->dev_speed == USB_SPEED_LOW);

components/usb/include/usb/usb_types_ch9.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -435,6 +435,12 @@ ESP_STATIC_ASSERT(sizeof(usb_ep_desc_t) == USB_EP_DESC_SIZE, "Size of usb_ep_des
435435
#define USB_B_ENDPOINT_ADDRESS_EP_NUM_MASK 0x0f
436436
#define USB_B_ENDPOINT_ADDRESS_EP_DIR_MASK 0x80
437437

438+
/**
439+
* @brief Bit masks belonging to the wMaxPacketSize field of endpoint descriptor
440+
*/
441+
#define USB_W_MAX_PACKET_SIZE_MPS_MASK 0x07ff
442+
#define USB_W_MAX_PACKET_SIZE_MULT_MASK 0x1800
443+
438444
/**
439445
* @brief Bit masks belonging to the bmAttributes field of an endpoint descriptor
440446
*/
@@ -459,7 +465,8 @@ ESP_STATIC_ASSERT(sizeof(usb_ep_desc_t) == USB_EP_DESC_SIZE, "Size of usb_ep_des
459465
#define USB_EP_DESC_GET_XFERTYPE(desc_ptr) ((usb_transfer_type_t) ((desc_ptr)->bmAttributes & USB_BM_ATTRIBUTES_XFERTYPE_MASK))
460466
#define USB_EP_DESC_GET_EP_NUM(desc_ptr) ((desc_ptr)->bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_NUM_MASK)
461467
#define USB_EP_DESC_GET_EP_DIR(desc_ptr) (((desc_ptr)->bEndpointAddress & USB_B_ENDPOINT_ADDRESS_EP_DIR_MASK) ? 1 : 0)
462-
#define USB_EP_DESC_GET_MPS(desc_ptr) ((desc_ptr)->wMaxPacketSize & 0x7FF)
468+
#define USB_EP_DESC_GET_MPS(desc_ptr) ((desc_ptr)->wMaxPacketSize & USB_W_MAX_PACKET_SIZE_MPS_MASK)
469+
#define USB_EP_DESC_GET_MULT(desc_ptr) (((desc_ptr)->wMaxPacketSize & USB_W_MAX_PACKET_SIZE_MULT_MASK) >> 11)
463470

464471
// ------------------ String Descriptor --------------------
465472

components/usb/test_apps/hcd/main/test_hcd_bulk.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ TEST_CASE("Test HCD bulk pipe URBs", "[bulk][full_speed]")
6868
//Create URBs for CBW, Data, and CSW transport. IN Buffer sizes are rounded up to nearest MPS
6969
urb_t *urb_cbw = test_hcd_alloc_urb(0, sizeof(mock_msc_bulk_cbw_t));
7070
urb_t *urb_data = test_hcd_alloc_urb(0, TEST_NUM_SECTORS_PER_XFER * MOCK_MSC_SCSI_SECTOR_SIZE);
71-
urb_t *urb_csw = test_hcd_alloc_urb(0, sizeof(mock_msc_bulk_csw_t) + (mock_msc_scsi_bulk_in_ep_desc.wMaxPacketSize - (sizeof(mock_msc_bulk_csw_t) % mock_msc_scsi_bulk_in_ep_desc.wMaxPacketSize)));
71+
const uint16_t mps = USB_EP_DESC_GET_MPS(&mock_msc_scsi_bulk_in_ep_desc) ;
72+
urb_t *urb_csw = test_hcd_alloc_urb(0, sizeof(mock_msc_bulk_csw_t) + (mps - (sizeof(mock_msc_bulk_csw_t) % mps)));
7273
urb_cbw->transfer.num_bytes = sizeof(mock_msc_bulk_cbw_t);
7374
urb_data->transfer.num_bytes = TEST_NUM_SECTORS_PER_XFER * MOCK_MSC_SCSI_SECTOR_SIZE;
74-
urb_csw->transfer.num_bytes = sizeof(mock_msc_bulk_csw_t) + (mock_msc_scsi_bulk_in_ep_desc.wMaxPacketSize - (sizeof(mock_msc_bulk_csw_t) % mock_msc_scsi_bulk_in_ep_desc.wMaxPacketSize));
75+
urb_csw->transfer.num_bytes = sizeof(mock_msc_bulk_csw_t) + (mps - (sizeof(mock_msc_bulk_csw_t) % mps));
7576

7677
for (int block_num = 0; block_num < TEST_NUM_SECTORS_TOTAL; block_num += TEST_NUM_SECTORS_PER_XFER) {
7778
//Initialize CBW URB, then send it on the BULK OUT pipe

components/usb/usb_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static void print_ep_desc(const usb_ep_desc_t *ep_desc)
198198
USB_EP_DESC_GET_EP_NUM(ep_desc),
199199
USB_EP_DESC_GET_EP_DIR(ep_desc) ? "IN" : "OUT");
200200
printf("\t\tbmAttributes 0x%x\t%s\n", ep_desc->bmAttributes, ep_type_str);
201-
printf("\t\twMaxPacketSize %d\n", ep_desc->wMaxPacketSize);
201+
printf("\t\twMaxPacketSize %d\n", USB_EP_DESC_GET_MPS(ep_desc));
202202
printf("\t\tbInterval %d\n", ep_desc->bInterval);
203203
}
204204

0 commit comments

Comments
 (0)