From 87caef43891f8ce9d6987c92da1c7ba9e15f8474 Mon Sep 17 00:00:00 2001 From: Johann Fischer Date: Fri, 21 Mar 2025 10:52:20 +0100 Subject: [PATCH] usb: device_next: keep loopback function transfers going Do not report canceled transfers as an error and continue the transfers regardless of the previous transfer status. Signed-off-by: Johann Fischer --- subsys/usb/device_next/class/loopback.c | 43 +++++++++++-------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/subsys/usb/device_next/class/loopback.c b/subsys/usb/device_next/class/loopback.c index 47f23d6e735..0e5ba96f459 100644 --- a/subsys/usb/device_next/class/loopback.c +++ b/subsys/usb/device_next/class/loopback.c @@ -157,49 +157,42 @@ static int lb_request_handler(struct usbd_class_data *const c_data, { struct udc_buf_info *bi = (struct udc_buf_info *)net_buf_user_data(buf); struct lb_data *data = usbd_class_get_private(c_data); - - LOG_DBG("Transfer finished %s -> ep 0x%02x, len %u, err %d", - c_data->name, bi->ep, buf->len, err); + const size_t len = buf->len; + const uint8_t ep = bi->ep; + int ret = 0; if (bi->ep == lb_get_bulk_out(c_data)) { atomic_clear_bit(&data->state, LB_FUNCTION_OUT_ENGAGED); + if (err == 0) { + memcpy(lb_buf, buf->data, MIN(sizeof(lb_buf), buf->len)); + } } if (bi->ep == lb_get_bulk_in(c_data)) { atomic_clear_bit(&data->state, LB_FUNCTION_IN_ENGAGED); } - if (err) { - if (err == -ECONNABORTED) { - LOG_INF("request ep 0x%02x, len %u cancelled", - bi->ep, buf->len); - } else { - LOG_ERR("request ep 0x%02x, len %u failed", - bi->ep, buf->len); - } - - net_buf_unref(buf); - - return err; + net_buf_unref(buf); + if (err == -ECONNABORTED) { + LOG_INF("Transfer ep 0x%02x, len %u cancelled", ep, len); + } else if (err != 0) { + LOG_ERR("Transfer ep 0x%02x, len %u failed", ep, len); + ret = err; + } else { + LOG_DBG("Transfer ep 0x%02x, len %u finished", ep, len); } - if (bi->ep == lb_get_bulk_out(c_data)) { - memcpy(lb_buf, buf->data, MIN(sizeof(lb_buf), buf->len)); - net_buf_unref(buf); - if (!atomic_test_bit(&data->state, LB_FUNCTION_BULK_MANUAL)) { + if (!atomic_test_bit(&data->state, LB_FUNCTION_BULK_MANUAL)) { + if (ep == lb_get_bulk_out(c_data)) { lb_submit_bulk_out(c_data); } - } - if (bi->ep == lb_get_bulk_in(c_data)) { - bi->ep = lb_get_bulk_out(c_data); - net_buf_unref(buf); - if (!atomic_test_bit(&data->state, LB_FUNCTION_BULK_MANUAL)) { + if (ep == lb_get_bulk_in(c_data)) { lb_submit_bulk_in(c_data); } } - return 0; + return ret; } static void lb_update(struct usbd_class_data *c_data,