Skip to content

Commit 60835aa

Browse files
tmon-nordicfabiobaltieri
authored andcommitted
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ń <[email protected]>
1 parent b7ab025 commit 60835aa

File tree

7 files changed

+35
-27
lines changed

7 files changed

+35
-27
lines changed

subsys/usb/device/usb_descriptor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ static int usb_validate_ep_cfg_data(struct usb_ep_descriptor * const ep_descr,
266266

267267
ep_cfg.ep_type = (ep_descr->bmAttributes &
268268
USB_EP_TRANSFER_TYPE_MASK);
269-
ep_cfg.ep_mps = ep_descr->wMaxPacketSize;
269+
ep_cfg.ep_mps = sys_le16_to_cpu(ep_descr->wMaxPacketSize);
270270
ep_cfg.ep_addr = ep_descr->bEndpointAddress;
271271
if (ep_cfg.ep_addr & USB_EP_DIR_IN) {
272272
if ((*requested_ep & (1U << (idx + 16U)))) {

subsys/usb/device_next/class/bt_hci.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ static struct usbd_bt_hci_desc bt_hci_desc_##n = { \
564564
.bDescriptorType = USB_DESC_ENDPOINT, \
565565
.bEndpointAddress = BT_HCI_EP_VOICE_IN, \
566566
.bmAttributes = USB_EP_TYPE_ISO, \
567-
.wMaxPacketSize = 0, \
567+
.wMaxPacketSize = sys_cpu_to_le16(0), \
568568
.bInterval = BT_HCI_EP_INTERVAL_VOICE, \
569569
}, \
570570
\
@@ -573,7 +573,7 @@ static struct usbd_bt_hci_desc bt_hci_desc_##n = { \
573573
.bDescriptorType = USB_DESC_ENDPOINT, \
574574
.bEndpointAddress = BT_HCI_EP_VOICE_OUT, \
575575
.bmAttributes = USB_EP_TYPE_ISO, \
576-
.wMaxPacketSize = 0, \
576+
.wMaxPacketSize = sys_cpu_to_le16(0), \
577577
.bInterval = BT_HCI_EP_INTERVAL_VOICE, \
578578
}, \
579579
\
@@ -594,7 +594,7 @@ static struct usbd_bt_hci_desc bt_hci_desc_##n = { \
594594
.bDescriptorType = USB_DESC_ENDPOINT, \
595595
.bEndpointAddress = BT_HCI_EP_VOICE_IN, \
596596
.bmAttributes = USB_EP_TYPE_ISO, \
597-
.wMaxPacketSize = BT_HCI_EP_MPS_VOICE, \
597+
.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_MPS_VOICE), \
598598
.bInterval = BT_HCI_EP_INTERVAL_VOICE, \
599599
}, \
600600
\
@@ -603,7 +603,7 @@ static struct usbd_bt_hci_desc bt_hci_desc_##n = { \
603603
.bDescriptorType = USB_DESC_ENDPOINT, \
604604
.bEndpointAddress = BT_HCI_EP_VOICE_OUT, \
605605
.bmAttributes = USB_EP_TYPE_ISO, \
606-
.wMaxPacketSize = BT_HCI_EP_MPS_VOICE, \
606+
.wMaxPacketSize = sys_cpu_to_le16(BT_HCI_EP_MPS_VOICE), \
607607
.bInterval = BT_HCI_EP_INTERVAL_VOICE, \
608608
}, \
609609
\

subsys/usb/device_next/class/loopback.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ static struct loopback_desc lb_desc_##x = { \
270270
.bDescriptorType = USB_DESC_ENDPOINT, \
271271
.bEndpointAddress = 0x83, \
272272
.bmAttributes = USB_EP_TYPE_ISO, \
273-
.wMaxPacketSize = 0, \
273+
.wMaxPacketSize = sys_cpu_to_le16(0), \
274274
.bInterval = LB_ISO_EP_INTERVAL, \
275275
}, \
276276
\
@@ -279,7 +279,7 @@ static struct loopback_desc lb_desc_##x = { \
279279
.bDescriptorType = USB_DESC_ENDPOINT, \
280280
.bEndpointAddress = 0x03, \
281281
.bmAttributes = USB_EP_TYPE_ISO, \
282-
.wMaxPacketSize = 0, \
282+
.wMaxPacketSize = sys_cpu_to_le16(0), \
283283
.bInterval = LB_ISO_EP_INTERVAL, \
284284
}, \
285285
\

subsys/usb/device_next/class/usbd_uac2_macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@
464464
USB_DESC_ENDPOINT, /* bDescriptorType */ \
465465
FIRST_IN_EP_ADDR, /* bEndpointAddress */ \
466466
USB_EP_TYPE_INTERRUPT, /* bmAttributes */ \
467-
0x06, /* wMaxPacketSize */ \
467+
U16_LE(0x06), /* wMaxPacketSize */ \
468468
0x01, /* bInterval */ \
469469

470470
#define AC_ENDPOINT_DESCRIPTOR_ARRAY(node) \

subsys/usb/device_next/usbd_endpoint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int usbd_ep_enable(const struct device *dev,
2424
int ret;
2525

2626
ret = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes,
27-
ed->wMaxPacketSize, ed->bInterval);
27+
sys_le16_to_cpu(ed->wMaxPacketSize), ed->bInterval);
2828
if (ret == 0) {
2929
usbd_ep_bm_set(ep_bm, ed->bEndpointAddress);
3030
}

subsys/usb/device_next/usbd_init.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static int assign_ep_addr(const struct device *dev,
2828
int ret = -ENODEV;
2929

3030
for (unsigned int idx = 1; idx < 16U; idx++) {
31-
uint16_t mps = ed->wMaxPacketSize;
31+
uint16_t mps = sys_le16_to_cpu(ed->wMaxPacketSize);
3232
uint8_t ep;
3333

3434
if (USB_EP_DIR_IS_IN(ed->bEndpointAddress)) {
@@ -50,7 +50,7 @@ static int assign_ep_addr(const struct device *dev,
5050
if (ret == 0) {
5151
LOG_DBG("ep 0x%02x -> 0x%02x", ed->bEndpointAddress, ep);
5252
ed->bEndpointAddress = ep;
53-
ed->wMaxPacketSize = mps;
53+
ed->wMaxPacketSize = sys_cpu_to_le16(mps);
5454
usbd_ep_bm_set(class_ep_bm, ed->bEndpointAddress);
5555
usbd_ep_bm_set(config_ep_bm, ed->bEndpointAddress);
5656

@@ -166,8 +166,10 @@ static int init_configuration_inst(struct usbd_context *const uds_ctx,
166166
return ret;
167167
}
168168

169-
LOG_INF("\tep 0x%02x mps %u interface ep-bm 0x%08x",
170-
ed->bEndpointAddress, ed->wMaxPacketSize, class_ep_bm);
169+
LOG_INF("\tep 0x%02x mps 0x%04x interface ep-bm 0x%08x",
170+
ed->bEndpointAddress,
171+
sys_le16_to_cpu(ed->wMaxPacketSize),
172+
class_ep_bm);
171173
}
172174

173175
dhp++;

tests/drivers/udc/src/main.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <zephyr/ztest.h>
88
#include <zephyr/drivers/usb/udc.h>
9+
#include <zephyr/sys/byteorder.h>
910
#include <zephyr/usb/usb_ch9.h>
1011

1112
#include <zephyr/logging/log.h>
@@ -95,7 +96,7 @@ static void test_udc_thread(void *p1, void *p2, void *p3)
9596
static void test_udc_ep_try_config(const struct device *dev,
9697
struct usb_ep_descriptor *ed)
9798
{
98-
uint16_t mps = ed->wMaxPacketSize;
99+
uint16_t mps = sys_le16_to_cpu(ed->wMaxPacketSize);
99100
int err;
100101

101102
err = udc_ep_try_config(dev, ed->bEndpointAddress,
@@ -129,13 +130,17 @@ static void test_udc_ep_enable(const struct device *dev,
129130
int err1, err2, err3, err4;
130131

131132
err1 = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes,
132-
ed->wMaxPacketSize, ed->bInterval);
133+
sys_le16_to_cpu(ed->wMaxPacketSize),
134+
ed->bInterval);
133135
err2 = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes,
134-
ed->wMaxPacketSize, ed->bInterval);
136+
sys_le16_to_cpu(ed->wMaxPacketSize),
137+
ed->bInterval);
135138
err3 = udc_ep_enable(dev, FALSE_EP_ADDR, ed->bmAttributes,
136-
ed->wMaxPacketSize, ed->bInterval);
139+
sys_le16_to_cpu(ed->wMaxPacketSize),
140+
ed->bInterval);
137141
err4 = udc_ep_enable(dev, ctrl_ep, ed->bmAttributes,
138-
ed->wMaxPacketSize, ed->bInterval);
142+
sys_le16_to_cpu(ed->wMaxPacketSize),
143+
ed->bInterval);
139144

140145
if (!udc_is_initialized(dev) && !udc_is_enabled(dev)) {
141146
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,
192197
struct net_buf *buf;
193198

194199
buf = udc_ep_buf_alloc(dev, ed->bEndpointAddress,
195-
ed->wMaxPacketSize);
200+
sys_le16_to_cpu(ed->wMaxPacketSize));
196201

197202
zassert_not_null(buf, "Failed to allocate request");
198203

@@ -326,13 +331,14 @@ static void test_udc_ep_api(const struct device *dev,
326331

327332
for (int i = 0; i < num_of_iterations; i++) {
328333
err = udc_ep_enable(dev, ed->bEndpointAddress, ed->bmAttributes,
329-
ed->wMaxPacketSize, ed->bInterval);
334+
sys_le16_to_cpu(ed->wMaxPacketSize),
335+
ed->bInterval);
330336
zassert_ok(err, "Failed to enable endpoint");
331337

332338
/* It needs a little reserve for memory management overhead. */
333339
for (int n = 0; n < (CONFIG_UDC_BUF_COUNT - 4); n++) {
334340
buf = udc_ep_buf_alloc(dev, ed->bEndpointAddress,
335-
ed->wMaxPacketSize);
341+
sys_le16_to_cpu(ed->wMaxPacketSize));
336342
zassert_not_null(buf,
337343
"Failed to allocate request (%d) for 0x%02x",
338344
n, ed->bEndpointAddress);
@@ -362,7 +368,7 @@ static void test_udc_ep_mps(uint8_t type)
362368
.bDescriptorType = USB_DESC_ENDPOINT,
363369
.bEndpointAddress = 0x01,
364370
.bmAttributes = type,
365-
.wMaxPacketSize = 0,
371+
.wMaxPacketSize = sys_cpu_to_le16(0),
366372
.bInterval = 0,
367373
};
368374
const struct device *dev;
@@ -399,7 +405,7 @@ static void test_udc_ep_mps(uint8_t type)
399405
continue;
400406
}
401407

402-
ed.wMaxPacketSize = mps[i];
408+
ed.wMaxPacketSize = sys_cpu_to_le16(mps[i]);
403409
test_udc_ep_api(dev, &ed);
404410

405411
ed.bEndpointAddress |= USB_EP_DIR_IN;
@@ -440,7 +446,7 @@ static struct usb_ep_descriptor ed_ctrl_out = {
440446
.bDescriptorType = USB_DESC_ENDPOINT,
441447
.bEndpointAddress = USB_CONTROL_EP_OUT,
442448
.bmAttributes = USB_EP_TYPE_CONTROL,
443-
.wMaxPacketSize = 64,
449+
.wMaxPacketSize = sys_cpu_to_le16(64),
444450
.bInterval = 0,
445451
};
446452

@@ -449,7 +455,7 @@ static struct usb_ep_descriptor ed_ctrl_in = {
449455
.bDescriptorType = USB_DESC_ENDPOINT,
450456
.bEndpointAddress = USB_CONTROL_EP_IN,
451457
.bmAttributes = USB_EP_TYPE_CONTROL,
452-
.wMaxPacketSize = 64,
458+
.wMaxPacketSize = sys_cpu_to_le16(64),
453459
.bInterval = 0,
454460
};
455461

@@ -458,7 +464,7 @@ static struct usb_ep_descriptor ed_bulk_out = {
458464
.bDescriptorType = USB_DESC_ENDPOINT,
459465
.bEndpointAddress = 0x01,
460466
.bmAttributes = USB_EP_TYPE_BULK,
461-
.wMaxPacketSize = 64,
467+
.wMaxPacketSize = sys_cpu_to_le16(64),
462468
.bInterval = 0,
463469
};
464470

@@ -467,7 +473,7 @@ static struct usb_ep_descriptor ed_bulk_in = {
467473
.bDescriptorType = USB_DESC_ENDPOINT,
468474
.bEndpointAddress = 0x81,
469475
.bmAttributes = USB_EP_TYPE_BULK,
470-
.wMaxPacketSize = 64,
476+
.wMaxPacketSize = sys_cpu_to_le16(64),
471477
.bInterval = 0,
472478
};
473479

0 commit comments

Comments
 (0)