Skip to content

Commit 0bdf2f4

Browse files
committed
subsys/usb: update to new ring_buffer api
move to new ring_buffer api. Signed-off-by: Måns Ansgariusson <[email protected]>
1 parent b4c8870 commit 0bdf2f4

File tree

6 files changed

+49
-54
lines changed

6 files changed

+49
-54
lines changed

subsys/usb/device/class/Kconfig.cdc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ config USB_CDC_ACM
1111
depends on DT_HAS_ZEPHYR_CDC_ACM_UART_ENABLED
1212
select SERIAL_HAS_DRIVER
1313
select SERIAL_SUPPORT_INTERRUPT
14-
select RING_BUFFER
1514
select UART_INTERRUPT_DRIVEN
1615
help
1716
USB CDC ACM class support.

subsys/usb/device/class/cdc_acm.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ struct cdc_acm_dev_data_t {
112112
bool tx_irq_ena; /* Tx interrupt enable status */
113113
bool rx_irq_ena; /* Rx interrupt enable status */
114114
uint8_t rx_buf[CDC_ACM_BUFFER_SIZE]; /* Internal RX buffer */
115-
struct ring_buf *rx_ringbuf;
116-
struct ring_buf *tx_ringbuf;
115+
struct ring_buffer *rx_ringbuf;
116+
struct ring_buffer *tx_ringbuf;
117117
/* Interface data buffer */
118118
/* CDC ACM line coding properties. LE order */
119119
struct cdc_acm_line_coding line_coding;
@@ -230,7 +230,7 @@ static void cdc_acm_write_cb(uint8_t ep, int size, void *priv)
230230
* ensure that actual payload will not be sent before initialization
231231
* timeout passes.
232232
*/
233-
if (ring_buf_is_empty(dev_data->tx_ringbuf) && size) {
233+
if (ring_buffer_empty(dev_data->tx_ringbuf) && size) {
234234
LOG_DBG("tx_ringbuf is empty");
235235
return;
236236
}
@@ -269,9 +269,7 @@ static void tx_work_handler(struct k_work *work)
269269
return;
270270
}
271271

272-
len = ring_buf_get_claim(dev_data->tx_ringbuf, &data,
273-
CONFIG_USB_CDC_ACM_RINGBUF_SIZE);
274-
272+
len = ring_buffer_read_ptr(dev_data->tx_ringbuf, &data);
275273
if (!len) {
276274
LOG_DBG("Nothing to send");
277275
return;
@@ -294,7 +292,7 @@ static void tx_work_handler(struct k_work *work)
294292
usb_transfer(ep, data, len, USB_TRANS_WRITE,
295293
cdc_acm_write_cb, dev_data);
296294

297-
ring_buf_get_finish(dev_data->tx_ringbuf, len);
295+
ring_buffer_consume(dev_data->tx_ringbuf, len);
298296
}
299297

300298
static void cdc_acm_read_cb(uint8_t ep, int size, void *priv)
@@ -303,13 +301,13 @@ static void cdc_acm_read_cb(uint8_t ep, int size, void *priv)
303301
size_t wrote;
304302

305303
LOG_DBG("ep %x size %d dev_data %p rx_ringbuf space %u",
306-
ep, size, dev_data, ring_buf_space_get(dev_data->rx_ringbuf));
304+
ep, size, dev_data, ring_buffer_space(dev_data->rx_ringbuf));
307305

308306
if (size <= 0) {
309307
goto done;
310308
}
311309

312-
wrote = ring_buf_put(dev_data->rx_ringbuf, dev_data->rx_buf, size);
310+
wrote = ring_buffer_write(dev_data->rx_ringbuf, dev_data->rx_buf, size);
313311
if (wrote < size) {
314312
LOG_ERR("Ring buffer full, drop %zd bytes", size - wrote);
315313
}
@@ -321,7 +319,7 @@ static void cdc_acm_read_cb(uint8_t ep, int size, void *priv)
321319
k_work_submit_to_queue(&USB_WORK_Q, &dev_data->cb_work);
322320
}
323321

324-
if (ring_buf_space_get(dev_data->rx_ringbuf) < sizeof(dev_data->rx_buf)) {
322+
if (ring_buffer_space(dev_data->rx_ringbuf) < sizeof(dev_data->rx_buf)) {
325323
dev_data->rx_paused = true;
326324
return;
327325
}
@@ -526,10 +524,10 @@ static int cdc_acm_fifo_fill(const struct device *dev,
526524
size_t wrote;
527525

528526
LOG_DBG("dev_data %p len %d tx_ringbuf space %u",
529-
dev_data, len, ring_buf_space_get(dev_data->tx_ringbuf));
527+
dev_data, len, ring_buffer_space(dev_data->tx_ringbuf));
530528

531529
lock = irq_lock();
532-
wrote = ring_buf_put(dev_data->tx_ringbuf, tx_data, len);
530+
wrote = ring_buffer_write(dev_data->tx_ringbuf, tx_data, len);
533531
irq_unlock(lock);
534532
LOG_DBG("Wrote %zu of %d bytes to TX ringbuffer", wrote, len);
535533

@@ -557,12 +555,12 @@ static int cdc_acm_fifo_read(const struct device *dev, uint8_t *rx_data,
557555
uint32_t len;
558556

559557
LOG_DBG("dev %p size %d rx_ringbuf space %u",
560-
dev, size, ring_buf_space_get(dev_data->rx_ringbuf));
558+
dev, size, ring_buffer_space(dev_data->rx_ringbuf));
561559

562-
len = ring_buf_get(dev_data->rx_ringbuf, rx_data, size);
560+
len = ring_buffer_read(dev_data->rx_ringbuf, rx_data, size);
563561

564562
if (dev_data->rx_paused == true) {
565-
if (ring_buf_space_get(dev_data->rx_ringbuf) >= CDC_ACM_BUFFER_SIZE) {
563+
if (ring_buffer_space(dev_data->rx_ringbuf) >= CDC_ACM_BUFFER_SIZE) {
566564
struct usb_cfg_data *cfg = (void *)dev->config;
567565

568566
if (dev_data->configured) {
@@ -615,7 +613,7 @@ static int cdc_acm_irq_tx_ready(const struct device *dev)
615613
struct cdc_acm_dev_data_t * const dev_data = dev->data;
616614

617615
if (dev_data->tx_irq_ena && dev_data->tx_ready) {
618-
return ring_buf_space_get(dev_data->tx_ringbuf);
616+
return ring_buffer_space(dev_data->tx_ringbuf);
619617
}
620618

621619
return 0;
@@ -694,11 +692,11 @@ static int cdc_acm_irq_update(const struct device *dev)
694692
{
695693
struct cdc_acm_dev_data_t * const dev_data = dev->data;
696694

697-
if (!ring_buf_space_get(dev_data->tx_ringbuf)) {
695+
if (!ring_buffer_space(dev_data->tx_ringbuf)) {
698696
dev_data->tx_ready = false;
699697
}
700698

701-
if (ring_buf_is_empty(dev_data->rx_ringbuf)) {
699+
if (ring_buffer_empty(dev_data->rx_ringbuf)) {
702700
dev_data->rx_ready = false;
703701
}
704702

@@ -1033,7 +1031,7 @@ static void cdc_acm_poll_out(const struct device *dev, unsigned char c)
10331031

10341032
while (true) {
10351033
lock = irq_lock();
1036-
wrote = ring_buf_put(dev_data->tx_ringbuf, &c, 1);
1034+
wrote = ring_buffer_write(dev_data->tx_ringbuf, &c, 1);
10371035
irq_unlock(lock);
10381036
if (wrote == 1) {
10391037
break;
@@ -1202,9 +1200,9 @@ static DEVICE_API(uart, cdc_acm_driver_api) = {
12021200
.endpoint = cdc_acm_ep_data_##x, \
12031201
}; \
12041202
\
1205-
RING_BUF_DECLARE(cdc_acm_rx_rb_##x, \
1203+
RING_BUFFER_DECLARE(cdc_acm_rx_rb_##x, \
12061204
CONFIG_USB_CDC_ACM_RINGBUF_SIZE); \
1207-
RING_BUF_DECLARE(cdc_acm_tx_rb_##x, \
1205+
RING_BUFFER_DECLARE(cdc_acm_tx_rb_##x, \
12081206
CONFIG_USB_CDC_ACM_RINGBUF_SIZE); \
12091207
static struct cdc_acm_dev_data_t cdc_acm_dev_data_##x = { \
12101208
.line_coding = CDC_ACM_DEFAULT_BAUDRATE, \

subsys/usb/device_next/class/Kconfig.cdc_acm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ config USBD_CDC_ACM_CLASS
88
depends on DT_HAS_ZEPHYR_CDC_ACM_UART_ENABLED
99
select SERIAL_HAS_DRIVER
1010
select SERIAL_SUPPORT_INTERRUPT
11-
select RING_BUFFER
1211
select UART_INTERRUPT_DRIVEN
1312
default y
1413
help

subsys/usb/device_next/class/Kconfig.midi2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
config USBD_MIDI2_CLASS
66
bool "USB MIDI 2.0 class support [EXPERIMENTAL]"
7-
select RING_BUFFER
87
help
98
Enable the USB MIDI 2.0 device class support.
109

subsys/usb/device_next/class/usbd_cdc_acm.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ LOG_MODULE_REGISTER(usbd_cdc_acm, CONFIG_USBD_CDC_ACM_LOG_LEVEL);
4949
#define CDC_ACM_TX_FIFO_BUSY 5
5050

5151
struct cdc_acm_uart_fifo {
52-
struct ring_buf *rb;
52+
struct ring_buffer *rb;
5353
bool irq;
5454
bool altered;
5555
};
@@ -309,7 +309,7 @@ static int usbd_cdc_acm_request(struct usbd_class_data *const c_data,
309309
size_t done;
310310

311311
LOG_HEXDUMP_INF(buf->data, buf->len, "");
312-
done = ring_buf_put(data->rx_fifo.rb, buf->data, buf->len);
312+
done = ring_buffer_write(data->rx_fifo.rb, buf->data, buf->len);
313313
if (done && data->cb) {
314314
cdc_acm_work_submit(&data->irq_cb_work);
315315
}
@@ -326,7 +326,7 @@ static int usbd_cdc_acm_request(struct usbd_class_data *const c_data,
326326

327327
atomic_clear_bit(&data->state, CDC_ACM_TX_FIFO_BUSY);
328328

329-
if (!ring_buf_is_empty(data->tx_fifo.rb)) {
329+
if (!ring_buffer_empty(data->tx_fifo.rb)) {
330330
/* Queue pending TX data on IN endpoint */
331331
cdc_acm_work_schedule(&data->tx_fifo_work, K_NO_WAIT);
332332
}
@@ -361,7 +361,7 @@ static void usbd_cdc_acm_enable(struct usbd_class_data *const c_data)
361361
}
362362

363363
if (atomic_test_bit(&data->state, CDC_ACM_IRQ_TX_ENABLED)) {
364-
if (ring_buf_space_get(data->tx_fifo.rb)) {
364+
if (ring_buffer_space(data->tx_fifo.rb)) {
365365
/* Raise TX ready interrupt */
366366
cdc_acm_work_submit(&data->irq_cb_work);
367367
} else {
@@ -658,7 +658,7 @@ static void cdc_acm_tx_fifo_handler(struct k_work *work)
658658
return;
659659
}
660660

661-
len = ring_buf_get(data->tx_fifo.rb, buf->data, buf->size);
661+
len = ring_buffer_read(data->tx_fifo.rb, buf->data, buf->size);
662662
net_buf_add(buf, len);
663663

664664
data->zlp_needed = len != 0 && len % cdc_acm_get_bulk_mps(c_data) == 0;
@@ -697,7 +697,7 @@ static void cdc_acm_rx_fifo_handler(struct k_work *work)
697697
return;
698698
}
699699

700-
if (ring_buf_space_get(data->rx_fifo.rb) < cdc_acm_get_bulk_mps(c_data)) {
700+
if (ring_buffer_space(data->rx_fifo.rb) < cdc_acm_get_bulk_mps(c_data)) {
701701
LOG_INF("RX buffer to small, throttle");
702702
return;
703703
}
@@ -729,7 +729,7 @@ static void cdc_acm_irq_tx_enable(const struct device *dev)
729729

730730
atomic_set_bit(&data->state, CDC_ACM_IRQ_TX_ENABLED);
731731

732-
if (ring_buf_space_get(data->tx_fifo.rb)) {
732+
if (ring_buffer_space(data->tx_fifo.rb)) {
733733
LOG_INF("tx_en: trigger irq_cb_work");
734734
cdc_acm_work_submit(&data->irq_cb_work);
735735
}
@@ -749,7 +749,7 @@ static void cdc_acm_irq_rx_enable(const struct device *dev)
749749
atomic_set_bit(&data->state, CDC_ACM_IRQ_RX_ENABLED);
750750

751751
/* Permit buffer to be drained regardless of USB state */
752-
if (!ring_buf_is_empty(data->rx_fifo.rb)) {
752+
if (!ring_buffer_empty(data->rx_fifo.rb)) {
753753
LOG_INF("rx_en: trigger irq_cb_work");
754754
cdc_acm_work_submit(&data->irq_cb_work);
755755
}
@@ -782,14 +782,14 @@ static int cdc_acm_fifo_fill(const struct device *dev,
782782
}
783783

784784
key = k_spin_lock(&data->lock);
785-
done = ring_buf_put(data->tx_fifo.rb, tx_data, len);
785+
done = ring_buffer_write(data->tx_fifo.rb, tx_data, len);
786786
k_spin_unlock(&data->lock, key);
787787
if (done) {
788788
data->tx_fifo.altered = true;
789789
}
790790

791791
LOG_INF("UART dev %p, len %d, remaining space %u",
792-
dev, len, ring_buf_space_get(data->tx_fifo.rb));
792+
dev, len, ring_buffer_space(data->tx_fifo.rb));
793793

794794
return done;
795795
}
@@ -802,15 +802,15 @@ static int cdc_acm_fifo_read(const struct device *dev,
802802
uint32_t len;
803803

804804
LOG_INF("UART dev %p size %d length %u",
805-
dev, size, ring_buf_size_get(data->rx_fifo.rb));
805+
dev, size, ring_buffer_size(data->rx_fifo.rb));
806806

807807
if (!check_wq_ctx(dev)) {
808808
LOG_WRN("Invoked by inappropriate context");
809809
__ASSERT_NO_MSG(false);
810810
return 0;
811811
}
812812

813-
len = ring_buf_get(data->rx_fifo.rb, rx_data, size);
813+
len = ring_buffer_read(data->rx_fifo.rb, rx_data, size);
814814
if (len) {
815815
data->rx_fifo.altered = true;
816816
}
@@ -824,7 +824,7 @@ static int cdc_acm_irq_tx_ready(const struct device *dev)
824824

825825
if (check_wq_ctx(dev)) {
826826
if (data->tx_fifo.irq) {
827-
return ring_buf_space_get(data->tx_fifo.rb);
827+
return ring_buffer_space(data->tx_fifo.rb);
828828
}
829829
} else {
830830
LOG_WRN("Invoked by inappropriate context");
@@ -878,14 +878,14 @@ static int cdc_acm_irq_update(const struct device *dev)
878878
}
879879

880880
if (atomic_test_bit(&data->state, CDC_ACM_IRQ_RX_ENABLED) &&
881-
!ring_buf_is_empty(data->rx_fifo.rb)) {
881+
!ring_buffer_empty(data->rx_fifo.rb)) {
882882
data->rx_fifo.irq = true;
883883
} else {
884884
data->rx_fifo.irq = false;
885885
}
886886

887887
if (atomic_test_bit(&data->state, CDC_ACM_IRQ_TX_ENABLED) &&
888-
ring_buf_space_get(data->tx_fifo.rb)) {
888+
ring_buffer_space(data->tx_fifo.rb)) {
889889
data->tx_fifo.irq = true;
890890
} else {
891891
data->tx_fifo.irq = false;
@@ -947,13 +947,13 @@ static void cdc_acm_irq_cb_handler(struct k_work *work)
947947
}
948948

949949
if (atomic_test_bit(&data->state, CDC_ACM_IRQ_RX_ENABLED) &&
950-
!ring_buf_is_empty(data->rx_fifo.rb)) {
950+
!ring_buffer_empty(data->rx_fifo.rb)) {
951951
LOG_DBG("rx irq pending, submit irq_cb_work");
952952
cdc_acm_work_submit(&data->irq_cb_work);
953953
}
954954

955955
if (atomic_test_bit(&data->state, CDC_ACM_IRQ_TX_ENABLED) &&
956-
ring_buf_space_get(data->tx_fifo.rb)) {
956+
ring_buffer_space(data->tx_fifo.rb)) {
957957
LOG_DBG("tx irq pending, submit irq_cb_work");
958958
cdc_acm_work_submit(&data->irq_cb_work);
959959
}
@@ -975,11 +975,11 @@ static int cdc_acm_poll_in(const struct device *dev, unsigned char *const c)
975975
uint32_t len;
976976
int ret = -1;
977977

978-
if (ring_buf_is_empty(data->rx_fifo.rb)) {
978+
if (ring_buffer_empty(data->rx_fifo.rb)) {
979979
return ret;
980980
}
981981

982-
len = ring_buf_get(data->rx_fifo.rb, c, 1);
982+
len = ring_buffer_read(data->rx_fifo.rb, c, 1);
983983
if (len) {
984984
cdc_acm_work_submit(&data->rx_fifo_work);
985985
ret = 0;
@@ -996,7 +996,7 @@ static void cdc_acm_poll_out(const struct device *dev, const unsigned char c)
996996

997997
while (true) {
998998
key = k_spin_lock(&data->lock);
999-
wrote = ring_buf_put(data->tx_fifo.rb, &c, 1);
999+
wrote = ring_buffer_write(data->tx_fifo.rb, &c, 1);
10001000
k_spin_unlock(&data->lock, key);
10011001

10021002
if (wrote == 1) {
@@ -1119,8 +1119,8 @@ static int usbd_cdc_acm_preinit(const struct device *dev)
11191119
{
11201120
struct cdc_acm_uart_data *const data = dev->data;
11211121

1122-
ring_buf_reset(data->tx_fifo.rb);
1123-
ring_buf_reset(data->rx_fifo.rb);
1122+
ring_buffer_reset(data->tx_fifo.rb);
1123+
ring_buffer_reset(data->rx_fifo.rb);
11241124

11251125
k_work_init_delayable(&data->tx_fifo_work, cdc_acm_tx_fifo_handler);
11261126
k_work_init(&data->rx_fifo_work, cdc_acm_rx_fifo_handler);
@@ -1350,8 +1350,8 @@ const static struct usb_desc_header *cdc_acm_hs_desc_##n[] = { \
13501350
USBD_DUT_STRING_INTERFACE); \
13511351
)) \
13521352
\
1353-
RING_BUF_DECLARE(cdc_acm_rb_rx_##n, DT_INST_PROP(n, rx_fifo_size)); \
1354-
RING_BUF_DECLARE(cdc_acm_rb_tx_##n, DT_INST_PROP(n, tx_fifo_size)); \
1353+
RING_BUFFER_DECLARE(cdc_acm_rb_rx_##n, DT_INST_PROP(n, rx_fifo_size)); \
1354+
RING_BUFFER_DECLARE(cdc_acm_rb_tx_##n, DT_INST_PROP(n, tx_fifo_size)); \
13551355
\
13561356
static const struct cdc_acm_uart_config uart_config_##n = { \
13571357
.c_data = &cdc_acm_##n, \

0 commit comments

Comments
 (0)