Skip to content

Commit ef89321

Browse files
jfischer-nofabiobaltieri
authored andcommitted
usb: device_next: allow fifo_fill and poll_out be used simultaneously
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]>
1 parent 6cf2775 commit ef89321

File tree

1 file changed

+6
-33
lines changed

1 file changed

+6
-33
lines changed

subsys/usb/device_next/class/usbd_cdc_acm.c

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ UDC_BUF_POOL_DEFINE(cdc_acm_ep_pool,
4747
#define CDC_ACM_IRQ_RX_ENABLED 2
4848
#define CDC_ACM_IRQ_TX_ENABLED 3
4949
#define CDC_ACM_RX_FIFO_BUSY 4
50-
#define CDC_ACM_LOCK 5
5150

5251
static struct k_work_q cdc_acm_work_q;
5352
static K_KERNEL_STACK_DEFINE(cdc_acm_stack,
@@ -540,15 +539,10 @@ static void cdc_acm_tx_fifo_handler(struct k_work *work)
540539
return;
541540
}
542541

543-
if (atomic_test_and_set_bit(&data->state, CDC_ACM_LOCK)) {
544-
cdc_acm_work_submit(&data->tx_fifo_work);
545-
return;
546-
}
547-
548542
buf = cdc_acm_buf_alloc(cdc_acm_get_bulk_in(c_data));
549543
if (buf == NULL) {
550544
cdc_acm_work_submit(&data->tx_fifo_work);
551-
goto tx_fifo_handler_exit;
545+
return;
552546
}
553547

554548
len = ring_buf_get(data->tx_fifo.rb, buf->data, buf->size);
@@ -559,9 +553,6 @@ static void cdc_acm_tx_fifo_handler(struct k_work *work)
559553
LOG_ERR("Failed to enqueue");
560554
net_buf_unref(buf);
561555
}
562-
563-
tx_fifo_handler_exit:
564-
atomic_clear_bit(&data->state, CDC_ACM_LOCK);
565556
}
566557

567558
/*
@@ -808,12 +799,6 @@ static void cdc_acm_irq_cb_handler(struct k_work *work)
808799
return;
809800
}
810801

811-
if (atomic_test_and_set_bit(&data->state, CDC_ACM_LOCK)) {
812-
LOG_ERR("Polling is in progress");
813-
cdc_acm_work_submit(&data->irq_cb_work);
814-
return;
815-
}
816-
817802
data->tx_fifo.altered = false;
818803
data->rx_fifo.altered = false;
819804
data->rx_fifo.irq = false;
@@ -845,8 +830,6 @@ static void cdc_acm_irq_cb_handler(struct k_work *work)
845830
LOG_DBG("tx irq pending, submit irq_cb_work");
846831
cdc_acm_work_submit(&data->irq_cb_work);
847832
}
848-
849-
atomic_clear_bit(&data->state, CDC_ACM_LOCK);
850833
}
851834

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

868-
if (atomic_test_and_set_bit(&data->state, CDC_ACM_LOCK)) {
869-
LOG_ERR("IRQ callback is used");
870-
return -1;
871-
}
872-
873851
if (ring_buf_is_empty(data->rx_fifo.rb)) {
874-
goto poll_in_exit;
852+
return ret;
875853
}
876854

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

883-
poll_in_exit:
884-
atomic_clear_bit(&data->state, CDC_ACM_LOCK);
885-
886861
return ret;
887862
}
888863

889864
static void cdc_acm_poll_out(const struct device *dev, const unsigned char c)
890865
{
891866
struct cdc_acm_uart_data *const data = dev->data;
867+
unsigned int lock;
892868
uint32_t wrote;
893869

894-
if (atomic_test_and_set_bit(&data->state, CDC_ACM_LOCK)) {
895-
LOG_ERR("IRQ callback is used");
896-
return;
897-
}
898-
899870
while (true) {
871+
lock = irq_lock();
900872
wrote = ring_buf_put(data->tx_fifo.rb, &c, 1);
873+
irq_unlock(lock);
874+
901875
if (wrote == 1) {
902876
break;
903877
}
@@ -910,7 +884,6 @@ static void cdc_acm_poll_out(const struct device *dev, const unsigned char c)
910884
k_msleep(1);
911885
}
912886

913-
atomic_clear_bit(&data->state, CDC_ACM_LOCK);
914887
cdc_acm_work_submit(&data->tx_fifo_work);
915888
}
916889

0 commit comments

Comments
 (0)