Skip to content

Commit

Permalink
usbd: device_next: cdc: wait IN EP ready before enqueue data to EP
Browse files Browse the repository at this point in the history
Under the current structure of usbd_cdc_acm, there is a possibility that
TX data is enqueued to UDC driver before the previous transaction is
completed, hence causing the dropped data. This commit adds a mechanism
for usbd_cdc_acm driver to wait for IN endpoint to complete existing
transaction before allows the subsequent transaction to be enqueued.

Signed-off-by: Chew Zeh Yang <[email protected]>
  • Loading branch information
zeonchew committed Sep 27, 2024
1 parent 7e892f8 commit 1fc6702
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions subsys/usb/device_next/class/usbd_cdc_acm.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ UDC_BUF_POOL_DEFINE(cdc_acm_ep_pool,
#define CDC_ACM_IRQ_TX_ENABLED 3
#define CDC_ACM_RX_FIFO_BUSY 4
#define CDC_ACM_LOCK 5
#define CDC_ACM_TX_EP_BUSY 6

Check notice on line 52 in subsys/usb/device_next/class/usbd_cdc_acm.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

subsys/usb/device_next/class/usbd_cdc_acm.c:52 -#define CDC_ACM_TX_EP_BUSY 6 +#define CDC_ACM_TX_EP_BUSY 6
static struct k_work_q cdc_acm_work_q;
static K_KERNEL_STACK_DEFINE(cdc_acm_stack,
Expand Down Expand Up @@ -216,6 +217,9 @@ static int usbd_cdc_acm_request(struct usbd_class_data *const c_data,
if (bi->ep == cdc_acm_get_bulk_out(c_data)) {
atomic_clear_bit(&data->state, CDC_ACM_RX_FIFO_BUSY);
}
else if (bi->ep == cdc_acm_get_bulk_in(c_data)) {

Check failure on line 220 in subsys/usb/device_next/class/usbd_cdc_acm.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

ELSE_AFTER_BRACE

subsys/usb/device_next/class/usbd_cdc_acm.c:220 else should follow close brace '}'
atomic_clear_bit(&data->state, CDC_ACM_TX_EP_BUSY);

Check notice on line 221 in subsys/usb/device_next/class/usbd_cdc_acm.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

subsys/usb/device_next/class/usbd_cdc_acm.c:221 - } - else if (bi->ep == cdc_acm_get_bulk_in(c_data)) { + } else if (bi->ep == cdc_acm_get_bulk_in(c_data)) {
}

goto ep_request_error;
}
Expand All @@ -236,6 +240,7 @@ static int usbd_cdc_acm_request(struct usbd_class_data *const c_data,

if (bi->ep == cdc_acm_get_bulk_in(c_data)) {
/* TX transfer completion */
atomic_clear_bit(&data->state, CDC_ACM_TX_EP_BUSY);
if (data->cb) {
cdc_acm_work_submit(&data->irq_cb_work);
}
Expand Down Expand Up @@ -540,6 +545,11 @@ static void cdc_acm_tx_fifo_handler(struct k_work *work)
return;
}

if (atomic_test_and_set_bit(&data->state, CDC_ACM_TX_EP_BUSY)) {
cdc_acm_work_submit(&data->tx_fifo_work);
goto tx_fifo_handler_exit;
}

buf = cdc_acm_buf_alloc(cdc_acm_get_bulk_in(c_data));
if (buf == NULL) {
cdc_acm_work_submit(&data->tx_fifo_work);
Expand Down

0 comments on commit 1fc6702

Please sign in to comment.