From 60835aa393bbe750f154eafb3459ef03e71ccff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mo=C5=84?= Date: Fri, 19 Jul 2024 08:09:22 +0200 Subject: [PATCH] usb: Add missing wMaxPacketSize endian conversions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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ń --- subsys/usb/device/usb_descriptor.c | 2 +- subsys/usb/device_next/class/bt_hci.c | 8 ++--- subsys/usb/device_next/class/loopback.c | 4 +-- .../usb/device_next/class/usbd_uac2_macros.h | 2 +- subsys/usb/device_next/usbd_endpoint.c | 2 +- subsys/usb/device_next/usbd_init.c | 10 +++--- tests/drivers/udc/src/main.c | 34 +++++++++++-------- 7 files changed, 35 insertions(+), 27 deletions(-) diff --git a/subsys/usb/device/usb_descriptor.c b/subsys/usb/device/usb_descriptor.c index 1fcbefa095a..3b6b29de4eb 100644 --- a/subsys/usb/device/usb_descriptor.c +++ b/subsys/usb/device/usb_descriptor.c @@ -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 & 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; if (ep_cfg.ep_addr & USB_EP_DIR_IN) { if ((*requested_ep & (1U << (idx + 16U)))) { diff --git a/subsys/usb/device_next/class/bt_hci.c b/subsys/usb/device_next/class/bt_hci.c index e9b8506b248..debefa1fa1d 100644 --- a/subsys/usb/device_next/class/bt_hci.c +++ b/subsys/usb/device_next/class/bt_hci.c @@ -564,7 +564,7 @@ static struct usbd_bt_hci_desc bt_hci_desc_##n = { \ .bDescriptorType = USB_DESC_ENDPOINT, \ .bEndpointAddress = BT_HCI_EP_VOICE_IN, \ .bmAttributes = USB_EP_TYPE_ISO, \ - .wMaxPacketSize = 0, \ + .wMaxPacketSize = sys_cpu_to_le16(0), \ .bInterval = BT_HCI_EP_INTERVAL_VOICE, \ }, \ \ @@ -573,7 +573,7 @@ static struct usbd_bt_hci_desc bt_hci_desc_##n = { \ .bDescriptorType = USB_DESC_ENDPOINT, \ .bEndpointAddress = BT_HCI_EP_VOICE_OUT, \ .bmAttributes = USB_EP_TYPE_ISO, \ - .wMaxPacketSize = 0, \ + .wMaxPacketSize = sys_cpu_to_le16(0), \ .bInterval = BT_HCI_EP_INTERVAL_VOICE, \ }, \ \ @@ -594,7 +594,7 @@ static struct usbd_bt_hci_desc bt_hci_desc_##n = { \ .bDescriptorType = USB_DESC_ENDPOINT, \ .bEndpointAddress = BT_HCI_EP_VOICE_IN, \ .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, \ }, \ \ @@ -603,7 +603,7 @@ static struct usbd_bt_hci_desc bt_hci_desc_##n = { \ .bDescriptorType = USB_DESC_ENDPOINT, \ .bEndpointAddress = BT_HCI_EP_VOICE_OUT, \ .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, \ }, \ \ diff --git a/subsys/usb/device_next/class/loopback.c b/subsys/usb/device_next/class/loopback.c index 8a9688dde53..2237cabcc79 100644 --- a/subsys/usb/device_next/class/loopback.c +++ b/subsys/usb/device_next/class/loopback.c @@ -270,7 +270,7 @@ static struct loopback_desc lb_desc_##x = { \ .bDescriptorType = USB_DESC_ENDPOINT, \ .bEndpointAddress = 0x83, \ .bmAttributes = USB_EP_TYPE_ISO, \ - .wMaxPacketSize = 0, \ + .wMaxPacketSize = sys_cpu_to_le16(0), \ .bInterval = LB_ISO_EP_INTERVAL, \ }, \ \ @@ -279,7 +279,7 @@ static struct loopback_desc lb_desc_##x = { \ .bDescriptorType = USB_DESC_ENDPOINT, \ .bEndpointAddress = 0x03, \ .bmAttributes = USB_EP_TYPE_ISO, \ - .wMaxPacketSize = 0, \ + .wMaxPacketSize = sys_cpu_to_le16(0), \ .bInterval = LB_ISO_EP_INTERVAL, \ }, \ \ diff --git a/subsys/usb/device_next/class/usbd_uac2_macros.h b/subsys/usb/device_next/class/usbd_uac2_macros.h index 58716214b56..7182e8e34b6 100644 --- a/subsys/usb/device_next/class/usbd_uac2_macros.h +++ b/subsys/usb/device_next/class/usbd_uac2_macros.h @@ -464,7 +464,7 @@ USB_DESC_ENDPOINT, /* bDescriptorType */ \ FIRST_IN_EP_ADDR, /* bEndpointAddress */ \ USB_EP_TYPE_INTERRUPT, /* bmAttributes */ \ - 0x06, /* wMaxPacketSize */ \ + U16_LE(0x06), /* wMaxPacketSize */ \ 0x01, /* bInterval */ \ #define AC_ENDPOINT_DESCRIPTOR_ARRAY(node) \ diff --git a/subsys/usb/device_next/usbd_endpoint.c b/subsys/usb/device_next/usbd_endpoint.c index 5d49e0df8ea..1d41f4c99a2 100644 --- a/subsys/usb/device_next/usbd_endpoint.c +++ b/subsys/usb/device_next/usbd_endpoint.c @@ -24,7 +24,7 @@ int usbd_ep_enable(const struct device *dev, int ret; ret = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes, - ed->wMaxPacketSize, ed->bInterval); + sys_le16_to_cpu(ed->wMaxPacketSize), ed->bInterval); if (ret == 0) { usbd_ep_bm_set(ep_bm, ed->bEndpointAddress); } diff --git a/subsys/usb/device_next/usbd_init.c b/subsys/usb/device_next/usbd_init.c index 69de723b221..551dc32164f 100644 --- a/subsys/usb/device_next/usbd_init.c +++ b/subsys/usb/device_next/usbd_init.c @@ -28,7 +28,7 @@ static int assign_ep_addr(const struct device *dev, int ret = -ENODEV; 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; if (USB_EP_DIR_IS_IN(ed->bEndpointAddress)) { @@ -50,7 +50,7 @@ static int assign_ep_addr(const struct device *dev, if (ret == 0) { LOG_DBG("ep 0x%02x -> 0x%02x", 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(config_ep_bm, ed->bEndpointAddress); @@ -166,8 +166,10 @@ static int init_configuration_inst(struct usbd_context *const uds_ctx, return ret; } - LOG_INF("\tep 0x%02x mps %u interface ep-bm 0x%08x", - ed->bEndpointAddress, ed->wMaxPacketSize, class_ep_bm); + LOG_INF("\tep 0x%02x mps 0x%04x interface ep-bm 0x%08x", + ed->bEndpointAddress, + sys_le16_to_cpu(ed->wMaxPacketSize), + class_ep_bm); } dhp++; diff --git a/tests/drivers/udc/src/main.c b/tests/drivers/udc/src/main.c index 64298b1508c..d2babb427ec 100644 --- a/tests/drivers/udc/src/main.c +++ b/tests/drivers/udc/src/main.c @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -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, struct usb_ep_descriptor *ed) { - uint16_t mps = ed->wMaxPacketSize; + uint16_t mps = sys_le16_to_cpu(ed->wMaxPacketSize); int err; 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; 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, - ed->wMaxPacketSize, ed->bInterval); + sys_le16_to_cpu(ed->wMaxPacketSize), + ed->bInterval); 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, - ed->wMaxPacketSize, ed->bInterval); + sys_le16_to_cpu(ed->wMaxPacketSize), + ed->bInterval); if (!udc_is_initialized(dev) && !udc_is_enabled(dev)) { 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; buf = udc_ep_buf_alloc(dev, ed->bEndpointAddress, - ed->wMaxPacketSize); + sys_le16_to_cpu(ed->wMaxPacketSize)); 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++) { 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"); /* It needs a little reserve for memory management overhead. */ for (int n = 0; n < (CONFIG_UDC_BUF_COUNT - 4); n++) { buf = udc_ep_buf_alloc(dev, ed->bEndpointAddress, - ed->wMaxPacketSize); + sys_le16_to_cpu(ed->wMaxPacketSize)); zassert_not_null(buf, "Failed to allocate request (%d) for 0x%02x", n, ed->bEndpointAddress); @@ -362,7 +368,7 @@ static void test_udc_ep_mps(uint8_t type) .bDescriptorType = USB_DESC_ENDPOINT, .bEndpointAddress = 0x01, .bmAttributes = type, - .wMaxPacketSize = 0, + .wMaxPacketSize = sys_cpu_to_le16(0), .bInterval = 0, }; const struct device *dev; @@ -399,7 +405,7 @@ static void test_udc_ep_mps(uint8_t type) continue; } - ed.wMaxPacketSize = mps[i]; + ed.wMaxPacketSize = sys_cpu_to_le16(mps[i]); test_udc_ep_api(dev, &ed); ed.bEndpointAddress |= USB_EP_DIR_IN; @@ -440,7 +446,7 @@ static struct usb_ep_descriptor ed_ctrl_out = { .bDescriptorType = USB_DESC_ENDPOINT, .bEndpointAddress = USB_CONTROL_EP_OUT, .bmAttributes = USB_EP_TYPE_CONTROL, - .wMaxPacketSize = 64, + .wMaxPacketSize = sys_cpu_to_le16(64), .bInterval = 0, }; @@ -449,7 +455,7 @@ static struct usb_ep_descriptor ed_ctrl_in = { .bDescriptorType = USB_DESC_ENDPOINT, .bEndpointAddress = USB_CONTROL_EP_IN, .bmAttributes = USB_EP_TYPE_CONTROL, - .wMaxPacketSize = 64, + .wMaxPacketSize = sys_cpu_to_le16(64), .bInterval = 0, }; @@ -458,7 +464,7 @@ static struct usb_ep_descriptor ed_bulk_out = { .bDescriptorType = USB_DESC_ENDPOINT, .bEndpointAddress = 0x01, .bmAttributes = USB_EP_TYPE_BULK, - .wMaxPacketSize = 64, + .wMaxPacketSize = sys_cpu_to_le16(64), .bInterval = 0, }; @@ -467,7 +473,7 @@ static struct usb_ep_descriptor ed_bulk_in = { .bDescriptorType = USB_DESC_ENDPOINT, .bEndpointAddress = 0x81, .bmAttributes = USB_EP_TYPE_BULK, - .wMaxPacketSize = 64, + .wMaxPacketSize = sys_cpu_to_le16(64), .bInterval = 0, };