Skip to content

Commit 7ea8992

Browse files
MarkWangChinesenashif
authored andcommitted
drivers: usb: uhc: mcux: Fix nocache buffer allocation for IN/OUT transfer
Use net_buf_tail() instead of __buf for proper buffer positioning. Add null check and error handling for nocache buffer allocation. Only copy data for OUT transfers during buffer setup. Signed-off-by: Mark Wang <[email protected]>
1 parent d5a5095 commit 7ea8992

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

drivers/usb/uhc/uhc_mcux_ehci.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static void uhc_mcux_transfer_callback(void *param, usb_host_transfer_t *transfe
171171
if ((xfer->buf != NULL) && (transfer->transferBuffer != NULL) &&
172172
USB_EP_DIR_IS_IN(xfer->ep) && (transfer->transferSofar > 0)) {
173173
#if defined(CONFIG_NOCACHE_MEMORY)
174-
memcpy(xfer->buf->__buf, transfer->transferBuffer, transfer->transferSofar);
174+
memcpy(net_buf_tail(xfer->buf), transfer->transferBuffer, transfer->transferSofar);
175175
#endif
176176
net_buf_add(xfer->buf, transfer->transferSofar);
177177
#if defined(CONFIG_NOCACHE_MEMORY)
@@ -199,8 +199,20 @@ static usb_host_transfer_t *uhc_mcux_hal_init_transfer(const struct device *dev,
199199
#if defined(CONFIG_NOCACHE_MEMORY)
200200
mcux_xfer->setupPacket = uhc_mcux_nocache_alloc(8u);
201201
memcpy(mcux_xfer->setupPacket, xfer->setup_pkt, 8u);
202-
if (xfer->buf != NULL) {
203-
mcux_xfer->transferBuffer = uhc_mcux_nocache_alloc(mcux_xfer->transferLength);
202+
203+
if (mcux_xfer->transferBuffer != NULL && mcux_xfer->transferLength != 0) {
204+
uint8_t *nocache_buf = uhc_mcux_nocache_alloc(mcux_xfer->transferLength);
205+
206+
if (nocache_buf == NULL) {
207+
k_mem_slab_free(&mcux_uhc_transfer_pool, mcux_xfer);
208+
return NULL;
209+
}
210+
211+
if (USB_EP_DIR_IS_OUT(xfer->ep)) {
212+
memcpy(nocache_buf, mcux_xfer->transferBuffer, mcux_xfer->transferLength);
213+
}
214+
215+
mcux_xfer->transferBuffer = nocache_buf;
204216
}
205217
#endif
206218

0 commit comments

Comments
 (0)