usb: Add missing wMaxPacketSize endian conversions

wMaxPacketSize in endpoint descriptor is stored in little-endian order,
but the mps parameter passed to functions is in host order.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
This commit is contained in:
Tomasz Moń 2024-07-19 08:09:22 +02:00 committed by Fabio Baltieri
commit 60835aa393
7 changed files with 35 additions and 27 deletions

View file

@ -266,7 +266,7 @@ static int usb_validate_ep_cfg_data(struct usb_ep_descriptor * const ep_descr,
ep_cfg.ep_type = (ep_descr->bmAttributes & ep_cfg.ep_type = (ep_descr->bmAttributes &
USB_EP_TRANSFER_TYPE_MASK); USB_EP_TRANSFER_TYPE_MASK);
ep_cfg.ep_mps = ep_descr->wMaxPacketSize; ep_cfg.ep_mps = sys_le16_to_cpu(ep_descr->wMaxPacketSize);
ep_cfg.ep_addr = ep_descr->bEndpointAddress; ep_cfg.ep_addr = ep_descr->bEndpointAddress;
if (ep_cfg.ep_addr & USB_EP_DIR_IN) { if (ep_cfg.ep_addr & USB_EP_DIR_IN) {
if ((*requested_ep & (1U << (idx + 16U)))) { if ((*requested_ep & (1U << (idx + 16U)))) {

View file

@ -564,7 +564,7 @@ static struct usbd_bt_hci_desc bt_hci_desc_##n = { \
.bDescriptorType = USB_DESC_ENDPOINT, \ .bDescriptorType = USB_DESC_ENDPOINT, \
.bEndpointAddress = BT_HCI_EP_VOICE_IN, \ .bEndpointAddress = BT_HCI_EP_VOICE_IN, \
.bmAttributes = USB_EP_TYPE_ISO, \ .bmAttributes = USB_EP_TYPE_ISO, \
.wMaxPacketSize = 0, \ .wMaxPacketSize = sys_cpu_to_le16(0), \
.bInterval = BT_HCI_EP_INTERVAL_VOICE, \ .bInterval = BT_HCI_EP_INTERVAL_VOICE, \
}, \ }, \
\ \
@ -573,7 +573,7 @@ static struct usbd_bt_hci_desc bt_hci_desc_##n = { \
.bDescriptorType = USB_DESC_ENDPOINT, \ .bDescriptorType = USB_DESC_ENDPOINT, \
.bEndpointAddress = BT_HCI_EP_VOICE_OUT, \ .bEndpointAddress = BT_HCI_EP_VOICE_OUT, \
.bmAttributes = USB_EP_TYPE_ISO, \ .bmAttributes = USB_EP_TYPE_ISO, \
.wMaxPacketSize = 0, \ .wMaxPacketSize = sys_cpu_to_le16(0), \
.bInterval = BT_HCI_EP_INTERVAL_VOICE, \ .bInterval = BT_HCI_EP_INTERVAL_VOICE, \
}, \ }, \
\ \
@ -594,7 +594,7 @@ static struct usbd_bt_hci_desc bt_hci_desc_##n = { \
.bDescriptorType = USB_DESC_ENDPOINT, \ .bDescriptorType = USB_DESC_ENDPOINT, \
.bEndpointAddress = BT_HCI_EP_VOICE_IN, \ .bEndpointAddress = BT_HCI_EP_VOICE_IN, \
.bmAttributes = USB_EP_TYPE_ISO, \ .bmAttributes = USB_EP_TYPE_ISO, \
.wMaxPacketSize = BT_HCI_EP_MPS_VOICE, \ .wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_MPS_VOICE), \
.bInterval = BT_HCI_EP_INTERVAL_VOICE, \ .bInterval = BT_HCI_EP_INTERVAL_VOICE, \
}, \ }, \
\ \
@ -603,7 +603,7 @@ static struct usbd_bt_hci_desc bt_hci_desc_##n = { \
.bDescriptorType = USB_DESC_ENDPOINT, \ .bDescriptorType = USB_DESC_ENDPOINT, \
.bEndpointAddress = BT_HCI_EP_VOICE_OUT, \ .bEndpointAddress = BT_HCI_EP_VOICE_OUT, \
.bmAttributes = USB_EP_TYPE_ISO, \ .bmAttributes = USB_EP_TYPE_ISO, \
.wMaxPacketSize = BT_HCI_EP_MPS_VOICE, \ .wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_MPS_VOICE), \
.bInterval = BT_HCI_EP_INTERVAL_VOICE, \ .bInterval = BT_HCI_EP_INTERVAL_VOICE, \
}, \ }, \
\ \

View file

@ -270,7 +270,7 @@ static struct loopback_desc lb_desc_##x = { \
.bDescriptorType = USB_DESC_ENDPOINT, \ .bDescriptorType = USB_DESC_ENDPOINT, \
.bEndpointAddress = 0x83, \ .bEndpointAddress = 0x83, \
.bmAttributes = USB_EP_TYPE_ISO, \ .bmAttributes = USB_EP_TYPE_ISO, \
.wMaxPacketSize = 0, \ .wMaxPacketSize = sys_cpu_to_le16(0), \
.bInterval = LB_ISO_EP_INTERVAL, \ .bInterval = LB_ISO_EP_INTERVAL, \
}, \ }, \
\ \
@ -279,7 +279,7 @@ static struct loopback_desc lb_desc_##x = { \
.bDescriptorType = USB_DESC_ENDPOINT, \ .bDescriptorType = USB_DESC_ENDPOINT, \
.bEndpointAddress = 0x03, \ .bEndpointAddress = 0x03, \
.bmAttributes = USB_EP_TYPE_ISO, \ .bmAttributes = USB_EP_TYPE_ISO, \
.wMaxPacketSize = 0, \ .wMaxPacketSize = sys_cpu_to_le16(0), \
.bInterval = LB_ISO_EP_INTERVAL, \ .bInterval = LB_ISO_EP_INTERVAL, \
}, \ }, \
\ \

View file

@ -464,7 +464,7 @@
USB_DESC_ENDPOINT, /* bDescriptorType */ \ USB_DESC_ENDPOINT, /* bDescriptorType */ \
FIRST_IN_EP_ADDR, /* bEndpointAddress */ \ FIRST_IN_EP_ADDR, /* bEndpointAddress */ \
USB_EP_TYPE_INTERRUPT, /* bmAttributes */ \ USB_EP_TYPE_INTERRUPT, /* bmAttributes */ \
0x06, /* wMaxPacketSize */ \ U16_LE(0x06), /* wMaxPacketSize */ \
0x01, /* bInterval */ \ 0x01, /* bInterval */ \
#define AC_ENDPOINT_DESCRIPTOR_ARRAY(node) \ #define AC_ENDPOINT_DESCRIPTOR_ARRAY(node) \

View file

@ -24,7 +24,7 @@ int usbd_ep_enable(const struct device *dev,
int ret; int ret;
ret = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes, ret = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes,
ed->wMaxPacketSize, ed->bInterval); sys_le16_to_cpu(ed->wMaxPacketSize), ed->bInterval);
if (ret == 0) { if (ret == 0) {
usbd_ep_bm_set(ep_bm, ed->bEndpointAddress); usbd_ep_bm_set(ep_bm, ed->bEndpointAddress);
} }

View file

@ -28,7 +28,7 @@ static int assign_ep_addr(const struct device *dev,
int ret = -ENODEV; int ret = -ENODEV;
for (unsigned int idx = 1; idx < 16U; idx++) { for (unsigned int idx = 1; idx < 16U; idx++) {
uint16_t mps = ed->wMaxPacketSize; uint16_t mps = sys_le16_to_cpu(ed->wMaxPacketSize);
uint8_t ep; uint8_t ep;
if (USB_EP_DIR_IS_IN(ed->bEndpointAddress)) { if (USB_EP_DIR_IS_IN(ed->bEndpointAddress)) {
@ -50,7 +50,7 @@ static int assign_ep_addr(const struct device *dev,
if (ret == 0) { if (ret == 0) {
LOG_DBG("ep 0x%02x -> 0x%02x", ed->bEndpointAddress, ep); LOG_DBG("ep 0x%02x -> 0x%02x", ed->bEndpointAddress, ep);
ed->bEndpointAddress = ep; ed->bEndpointAddress = ep;
ed->wMaxPacketSize = mps; ed->wMaxPacketSize = sys_cpu_to_le16(mps);
usbd_ep_bm_set(class_ep_bm, ed->bEndpointAddress); usbd_ep_bm_set(class_ep_bm, ed->bEndpointAddress);
usbd_ep_bm_set(config_ep_bm, ed->bEndpointAddress); usbd_ep_bm_set(config_ep_bm, ed->bEndpointAddress);
@ -166,8 +166,10 @@ static int init_configuration_inst(struct usbd_context *const uds_ctx,
return ret; return ret;
} }
LOG_INF("\tep 0x%02x mps %u interface ep-bm 0x%08x", LOG_INF("\tep 0x%02x mps 0x%04x interface ep-bm 0x%08x",
ed->bEndpointAddress, ed->wMaxPacketSize, class_ep_bm); ed->bEndpointAddress,
sys_le16_to_cpu(ed->wMaxPacketSize),
class_ep_bm);
} }
dhp++; dhp++;

View file

@ -6,6 +6,7 @@
#include <zephyr/ztest.h> #include <zephyr/ztest.h>
#include <zephyr/drivers/usb/udc.h> #include <zephyr/drivers/usb/udc.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/usb/usb_ch9.h> #include <zephyr/usb/usb_ch9.h>
#include <zephyr/logging/log.h> #include <zephyr/logging/log.h>
@ -95,7 +96,7 @@ static void test_udc_thread(void *p1, void *p2, void *p3)
static void test_udc_ep_try_config(const struct device *dev, static void test_udc_ep_try_config(const struct device *dev,
struct usb_ep_descriptor *ed) struct usb_ep_descriptor *ed)
{ {
uint16_t mps = ed->wMaxPacketSize; uint16_t mps = sys_le16_to_cpu(ed->wMaxPacketSize);
int err; int err;
err = udc_ep_try_config(dev, ed->bEndpointAddress, err = udc_ep_try_config(dev, ed->bEndpointAddress,
@ -129,13 +130,17 @@ static void test_udc_ep_enable(const struct device *dev,
int err1, err2, err3, err4; int err1, err2, err3, err4;
err1 = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes, err1 = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes,
ed->wMaxPacketSize, ed->bInterval); sys_le16_to_cpu(ed->wMaxPacketSize),
ed->bInterval);
err2 = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes, err2 = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes,
ed->wMaxPacketSize, ed->bInterval); sys_le16_to_cpu(ed->wMaxPacketSize),
ed->bInterval);
err3 = udc_ep_enable(dev, FALSE_EP_ADDR, ed->bmAttributes, err3 = udc_ep_enable(dev, FALSE_EP_ADDR, ed->bmAttributes,
ed->wMaxPacketSize, ed->bInterval); sys_le16_to_cpu(ed->wMaxPacketSize),
ed->bInterval);
err4 = udc_ep_enable(dev, ctrl_ep, ed->bmAttributes, err4 = udc_ep_enable(dev, ctrl_ep, ed->bmAttributes,
ed->wMaxPacketSize, ed->bInterval); sys_le16_to_cpu(ed->wMaxPacketSize),
ed->bInterval);
if (!udc_is_initialized(dev) && !udc_is_enabled(dev)) { if (!udc_is_initialized(dev) && !udc_is_enabled(dev)) {
zassert_equal(err1, -EPERM, "Not failed to enable endpoint"); zassert_equal(err1, -EPERM, "Not failed to enable endpoint");
@ -192,7 +197,7 @@ static struct net_buf *test_udc_ep_buf_alloc(const struct device *dev,
struct net_buf *buf; struct net_buf *buf;
buf = udc_ep_buf_alloc(dev, ed->bEndpointAddress, buf = udc_ep_buf_alloc(dev, ed->bEndpointAddress,
ed->wMaxPacketSize); sys_le16_to_cpu(ed->wMaxPacketSize));
zassert_not_null(buf, "Failed to allocate request"); zassert_not_null(buf, "Failed to allocate request");
@ -326,13 +331,14 @@ static void test_udc_ep_api(const struct device *dev,
for (int i = 0; i < num_of_iterations; i++) { for (int i = 0; i < num_of_iterations; i++) {
err = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes, err = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes,
ed->wMaxPacketSize, ed->bInterval); sys_le16_to_cpu(ed->wMaxPacketSize),
ed->bInterval);
zassert_ok(err, "Failed to enable endpoint"); zassert_ok(err, "Failed to enable endpoint");
/* It needs a little reserve for memory management overhead. */ /* It needs a little reserve for memory management overhead. */
for (int n = 0; n < (CONFIG_UDC_BUF_COUNT - 4); n++) { for (int n = 0; n < (CONFIG_UDC_BUF_COUNT - 4); n++) {
buf = udc_ep_buf_alloc(dev, ed->bEndpointAddress, buf = udc_ep_buf_alloc(dev, ed->bEndpointAddress,
ed->wMaxPacketSize); sys_le16_to_cpu(ed->wMaxPacketSize));
zassert_not_null(buf, zassert_not_null(buf,
"Failed to allocate request (%d) for 0x%02x", "Failed to allocate request (%d) for 0x%02x",
n, ed->bEndpointAddress); n, ed->bEndpointAddress);
@ -362,7 +368,7 @@ static void test_udc_ep_mps(uint8_t type)
.bDescriptorType = USB_DESC_ENDPOINT, .bDescriptorType = USB_DESC_ENDPOINT,
.bEndpointAddress = 0x01, .bEndpointAddress = 0x01,
.bmAttributes = type, .bmAttributes = type,
.wMaxPacketSize = 0, .wMaxPacketSize = sys_cpu_to_le16(0),
.bInterval = 0, .bInterval = 0,
}; };
const struct device *dev; const struct device *dev;
@ -399,7 +405,7 @@ static void test_udc_ep_mps(uint8_t type)
continue; continue;
} }
ed.wMaxPacketSize = mps[i]; ed.wMaxPacketSize = sys_cpu_to_le16(mps[i]);
test_udc_ep_api(dev, &ed); test_udc_ep_api(dev, &ed);
ed.bEndpointAddress |= USB_EP_DIR_IN; ed.bEndpointAddress |= USB_EP_DIR_IN;
@ -440,7 +446,7 @@ static struct usb_ep_descriptor ed_ctrl_out = {
.bDescriptorType = USB_DESC_ENDPOINT, .bDescriptorType = USB_DESC_ENDPOINT,
.bEndpointAddress = USB_CONTROL_EP_OUT, .bEndpointAddress = USB_CONTROL_EP_OUT,
.bmAttributes = USB_EP_TYPE_CONTROL, .bmAttributes = USB_EP_TYPE_CONTROL,
.wMaxPacketSize = 64, .wMaxPacketSize = sys_cpu_to_le16(64),
.bInterval = 0, .bInterval = 0,
}; };
@ -449,7 +455,7 @@ static struct usb_ep_descriptor ed_ctrl_in = {
.bDescriptorType = USB_DESC_ENDPOINT, .bDescriptorType = USB_DESC_ENDPOINT,
.bEndpointAddress = USB_CONTROL_EP_IN, .bEndpointAddress = USB_CONTROL_EP_IN,
.bmAttributes = USB_EP_TYPE_CONTROL, .bmAttributes = USB_EP_TYPE_CONTROL,
.wMaxPacketSize = 64, .wMaxPacketSize = sys_cpu_to_le16(64),
.bInterval = 0, .bInterval = 0,
}; };
@ -458,7 +464,7 @@ static struct usb_ep_descriptor ed_bulk_out = {
.bDescriptorType = USB_DESC_ENDPOINT, .bDescriptorType = USB_DESC_ENDPOINT,
.bEndpointAddress = 0x01, .bEndpointAddress = 0x01,
.bmAttributes = USB_EP_TYPE_BULK, .bmAttributes = USB_EP_TYPE_BULK,
.wMaxPacketSize = 64, .wMaxPacketSize = sys_cpu_to_le16(64),
.bInterval = 0, .bInterval = 0,
}; };
@ -467,7 +473,7 @@ static struct usb_ep_descriptor ed_bulk_in = {
.bDescriptorType = USB_DESC_ENDPOINT, .bDescriptorType = USB_DESC_ENDPOINT,
.bEndpointAddress = 0x81, .bEndpointAddress = 0x81,
.bmAttributes = USB_EP_TYPE_BULK, .bmAttributes = USB_EP_TYPE_BULK,
.wMaxPacketSize = 64, .wMaxPacketSize = sys_cpu_to_le16(64),
.bInterval = 0, .bInterval = 0,
}; };