Skip to content

Commit 4e6f80d

Browse files
mmahadevan108fabiobaltieri
authored andcommitted
drivers: usb: mcux: Fix the write operation
1. Do not copy over the data to the local buffer, it can be directly sent to the controller. The cache is flushed before calling the HAL send function. Also do not allocate a buffer from the heap pool for the write operation. 2. Remove a length check as this is handled by the HAL driver. Signed-off-by: Mahesh Mahadevan <[email protected]>
1 parent 9bef7d0 commit 4e6f80d

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

drivers/usb/device/usb_dc_mcux.c

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg)
241241
{
242242
uint8_t ep_abs_idx = EP_ABS_IDX(cfg->ep_addr);
243243
usb_device_endpoint_init_struct_t ep_init;
244-
struct k_mem_block *block;
245244
struct usb_ep_ctrl_data *eps = &dev_state.eps[ep_abs_idx];
246245
usb_status_t status;
247246
uint8_t ep;
@@ -269,19 +268,25 @@ int usb_dc_ep_configure(const struct usb_dc_ep_cfg_data *const cfg)
269268
LOG_WRN("Failed to un-initialize endpoint (status=%d)", (int)status);
270269
}
271270

272-
block = &(eps->block);
273-
if (block->data) {
274-
k_heap_free(&ep_buf_pool, block->data);
275-
block->data = NULL;
276-
}
271+
/* Allocate buffers used during read operation */
272+
if (USB_EP_DIR_IS_OUT(cfg->ep_addr)) {
273+
struct k_mem_block *block;
274+
275+
block = &(eps->block);
276+
if (block->data) {
277+
k_heap_free(&ep_buf_pool, block->data);
278+
block->data = NULL;
279+
}
277280

278-
block->data = k_heap_alloc(&ep_buf_pool, cfg->ep_mps, K_NO_WAIT);
279-
if (block->data == NULL) {
280-
LOG_ERR("Failed to allocate memory");
281-
return -ENOMEM;
281+
block->data = k_heap_alloc(&ep_buf_pool, cfg->ep_mps, K_NO_WAIT);
282+
if (block->data == NULL) {
283+
LOG_ERR("Failed to allocate memory");
284+
return -ENOMEM;
285+
}
286+
287+
memset(block->data, 0, cfg->ep_mps);
282288
}
283289

284-
memset(block->data, 0, cfg->ep_mps);
285290
dev_state.eps[ep_abs_idx].ep_mps = cfg->ep_mps;
286291
status = dev_state.dev_struct.controllerInterface->deviceControl(
287292
dev_state.dev_struct.controllerHandle,
@@ -486,8 +491,6 @@ int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
486491
const uint32_t data_len, uint32_t *const ret_bytes)
487492
{
488493
uint8_t ep_abs_idx = EP_ABS_IDX(ep);
489-
uint8_t *buffer = (uint8_t *)dev_state.eps[ep_abs_idx].block.data;
490-
uint32_t len_to_send;
491494
usb_status_t status;
492495

493496
if (ep_abs_idx >= NUM_OF_EP_MAX) {
@@ -500,29 +503,19 @@ int usb_dc_ep_write(const uint8_t ep, const uint8_t *const data,
500503
return -EINVAL;
501504
}
502505

503-
if (data_len > dev_state.eps[ep_abs_idx].ep_mps) {
504-
len_to_send = dev_state.eps[ep_abs_idx].ep_mps;
505-
} else {
506-
len_to_send = data_len;
507-
}
508-
509-
for (uint32_t n = 0; n < len_to_send; n++) {
510-
buffer[n] = data[n];
511-
}
512-
513-
#if defined(CONFIG_HAS_MCUX_CACHE) && !defined(EP_BUF_NONCACHED)
514-
DCACHE_CleanByRange((uint32_t)buffer, len_to_send);
506+
#if defined(CONFIG_HAS_MCUX_CACHE)
507+
DCACHE_CleanByRange((uint32_t)data, data_len);
515508
#endif
516509
status = dev_state.dev_struct.controllerInterface->deviceSend(
517510
dev_state.dev_struct.controllerHandle,
518-
ep, buffer, len_to_send);
511+
ep, (uint8_t *)data, data_len);
519512
if (kStatus_USB_Success != status) {
520513
LOG_ERR("Failed to fill ep 0x%02x buffer", ep);
521514
return -EIO;
522515
}
523516

524517
if (ret_bytes) {
525-
*ret_bytes = len_to_send;
518+
*ret_bytes = data_len;
526519
}
527520

528521
return 0;

0 commit comments

Comments
 (0)