Skip to content

Commit

Permalink
usb: device_next: allow fifo_fill and poll_out be used simultaneously
Browse files Browse the repository at this point in the history
As it is still accepted practice, allow fifo_fill and poll_out to be
used simultaneously. The lock around fifo_fill was already in place.

Signed-off-by: Johann Fischer <[email protected]>
  • Loading branch information
jfischer-no authored and fabiobaltieri committed Oct 2, 2024
1 parent 6cf2775 commit ef89321
Showing 1 changed file with 6 additions and 33 deletions.
39 changes: 6 additions & 33 deletions subsys/usb/device_next/class/usbd_cdc_acm.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ UDC_BUF_POOL_DEFINE(cdc_acm_ep_pool,
#define CDC_ACM_IRQ_RX_ENABLED 2
#define CDC_ACM_IRQ_TX_ENABLED 3
#define CDC_ACM_RX_FIFO_BUSY 4
#define CDC_ACM_LOCK 5

static struct k_work_q cdc_acm_work_q;
static K_KERNEL_STACK_DEFINE(cdc_acm_stack,
Expand Down Expand Up @@ -540,15 +539,10 @@ static void cdc_acm_tx_fifo_handler(struct k_work *work)
return;
}

if (atomic_test_and_set_bit(&data->state, CDC_ACM_LOCK)) {
cdc_acm_work_submit(&data->tx_fifo_work);
return;
}

buf = cdc_acm_buf_alloc(cdc_acm_get_bulk_in(c_data));
if (buf == NULL) {
cdc_acm_work_submit(&data->tx_fifo_work);
goto tx_fifo_handler_exit;
return;
}

len = ring_buf_get(data->tx_fifo.rb, buf->data, buf->size);
Expand All @@ -559,9 +553,6 @@ static void cdc_acm_tx_fifo_handler(struct k_work *work)
LOG_ERR("Failed to enqueue");
net_buf_unref(buf);
}

tx_fifo_handler_exit:
atomic_clear_bit(&data->state, CDC_ACM_LOCK);
}

/*
Expand Down Expand Up @@ -808,12 +799,6 @@ static void cdc_acm_irq_cb_handler(struct k_work *work)
return;
}

if (atomic_test_and_set_bit(&data->state, CDC_ACM_LOCK)) {
LOG_ERR("Polling is in progress");
cdc_acm_work_submit(&data->irq_cb_work);
return;
}

data->tx_fifo.altered = false;
data->rx_fifo.altered = false;
data->rx_fifo.irq = false;
Expand Down Expand Up @@ -845,8 +830,6 @@ static void cdc_acm_irq_cb_handler(struct k_work *work)
LOG_DBG("tx irq pending, submit irq_cb_work");
cdc_acm_work_submit(&data->irq_cb_work);
}

atomic_clear_bit(&data->state, CDC_ACM_LOCK);
}

static void cdc_acm_irq_callback_set(const struct device *dev,
Expand All @@ -865,13 +848,8 @@ static int cdc_acm_poll_in(const struct device *dev, unsigned char *const c)
uint32_t len;
int ret = -1;

if (atomic_test_and_set_bit(&data->state, CDC_ACM_LOCK)) {
LOG_ERR("IRQ callback is used");
return -1;
}

if (ring_buf_is_empty(data->rx_fifo.rb)) {
goto poll_in_exit;
return ret;
}

len = ring_buf_get(data->rx_fifo.rb, c, 1);
Expand All @@ -880,24 +858,20 @@ static int cdc_acm_poll_in(const struct device *dev, unsigned char *const c)
ret = 0;
}

poll_in_exit:
atomic_clear_bit(&data->state, CDC_ACM_LOCK);

return ret;
}

static void cdc_acm_poll_out(const struct device *dev, const unsigned char c)
{
struct cdc_acm_uart_data *const data = dev->data;
unsigned int lock;
uint32_t wrote;

if (atomic_test_and_set_bit(&data->state, CDC_ACM_LOCK)) {
LOG_ERR("IRQ callback is used");
return;
}

while (true) {
lock = irq_lock();
wrote = ring_buf_put(data->tx_fifo.rb, &c, 1);
irq_unlock(lock);

if (wrote == 1) {
break;
}
Expand All @@ -910,7 +884,6 @@ static void cdc_acm_poll_out(const struct device *dev, const unsigned char c)
k_msleep(1);
}

atomic_clear_bit(&data->state, CDC_ACM_LOCK);
cdc_acm_work_submit(&data->tx_fifo_work);
}

Expand Down

0 comments on commit ef89321

Please sign in to comment.