From bf76b2acc3f4b956c2a37c96402c430718f6946a Mon Sep 17 00:00:00 2001 From: Andrei Emeltchenko Date: Thu, 16 May 2019 10:57:03 +0300 Subject: [PATCH] usb: device: Add transfer cancel helper Add usb_cancel_transfers() helper to cancel all ongoing transfers. Signed-off-by: Andrei Emeltchenko --- include/usb/usb_device.h | 5 +++++ subsys/usb/usb_device.c | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/usb/usb_device.h b/include/usb/usb_device.h index fbe7fa72c9c148..f862e19a855b4f 100644 --- a/include/usb/usb_device.h +++ b/include/usb/usb_device.h @@ -418,6 +418,11 @@ int usb_transfer_sync(u8_t ep, u8_t *data, size_t dlen, unsigned int flags); */ void usb_cancel_transfer(u8_t ep); +/** + * @brief Cancel all ongoing transfers + */ +void usb_cancel_transfers(void); + /** * @brief Check that transfer is ongoing for the endpoint * diff --git a/subsys/usb/usb_device.c b/subsys/usb/usb_device.c index c589ececabbe54..f5eb5515e2bb3a 100644 --- a/subsys/usb/usb_device.c +++ b/subsys/usb/usb_device.c @@ -1419,6 +1419,24 @@ void usb_cancel_transfer(u8_t ep) irq_unlock(key); } +void usb_cancel_transfers(void) +{ + for (int i = 0; i < ARRAY_SIZE(usb_dev.transfer); i++) { + struct usb_transfer_data *trans = &usb_dev.transfer[i]; + unsigned int key; + + key = irq_lock(); + + if (trans->status == -EBUSY) { + trans->status = -ECANCELED; + k_work_submit(&trans->work); + LOG_DBG("Cancel transfer"); + } + + irq_unlock(key); + } +} + struct usb_transfer_sync_priv { int tsize; struct k_sem sem;