Skip to content

Commit 7445e68

Browse files
committed
drivers: usb: move UDC net_buf_data_cb implementation to USB common
Move it to USB common so that it can be used on the host side in the future. Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
1 parent 6596aad commit 7445e68

File tree

4 files changed

+45
-30
lines changed

4 files changed

+45
-30
lines changed

drivers/usb/common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# SPDX-License-Identifier: Apache-2.0
22

33
add_subdirectory_ifdef(CONFIG_HAS_NRFX nrf_usbd_common)
4+
add_subdirectory(buf)

drivers/usb/common/buf/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
zephyr_library()
5+
zephyr_library_sources_ifdef(CONFIG_UDC_DRIVER usb_buf.c)

drivers/usb/common/buf/usb_buf.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/net_buf.h>
9+
#include <zephyr/drivers/usb/udc_buf.h>
10+
11+
static inline uint8_t *usb_pool_data_alloc(struct net_buf *const buf,
12+
size_t *const size, k_timeout_t timeout)
13+
{
14+
struct net_buf_pool *const buf_pool = net_buf_pool_get(buf->pool_id);
15+
struct k_heap *const pool = buf_pool->alloc->alloc_data;
16+
void *b;
17+
18+
*size = UDC_ROUND_UP(*size);
19+
b = k_heap_aligned_alloc(pool, UDC_BUF_ALIGN, *size, timeout);
20+
if (b == NULL) {
21+
*size = 0;
22+
return NULL;
23+
}
24+
25+
return b;
26+
}
27+
28+
static inline void usb_pool_data_unref(struct net_buf *buf, uint8_t *const data)
29+
{
30+
struct net_buf_pool *buf_pool = net_buf_pool_get(buf->pool_id);
31+
struct k_heap *pool = buf_pool->alloc->alloc_data;
32+
33+
k_heap_free(pool, data);
34+
}
35+
36+
const struct net_buf_data_cb net_buf_dma_cb = {
37+
.alloc = usb_pool_data_alloc,
38+
.unref = usb_pool_data_unref,
39+
};

drivers/usb/udc/udc_common.c

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,6 @@
2121
#endif
2222
LOG_MODULE_REGISTER(udc, CONFIG_UDC_DRIVER_LOG_LEVEL);
2323

24-
static inline uint8_t *udc_pool_data_alloc(struct net_buf *const buf,
25-
size_t *const size, k_timeout_t timeout)
26-
{
27-
struct net_buf_pool *const buf_pool = net_buf_pool_get(buf->pool_id);
28-
struct k_heap *const pool = buf_pool->alloc->alloc_data;
29-
void *b;
30-
31-
*size = ROUND_UP(*size, UDC_BUF_GRANULARITY);
32-
b = k_heap_aligned_alloc(pool, UDC_BUF_ALIGN, *size, timeout);
33-
if (b == NULL) {
34-
*size = 0;
35-
return NULL;
36-
}
37-
38-
return b;
39-
}
40-
41-
static inline void udc_pool_data_unref(struct net_buf *buf, uint8_t *const data)
42-
{
43-
struct net_buf_pool *buf_pool = net_buf_pool_get(buf->pool_id);
44-
struct k_heap *pool = buf_pool->alloc->alloc_data;
45-
46-
k_heap_free(pool, data);
47-
}
48-
49-
const struct net_buf_data_cb net_buf_dma_cb = {
50-
.alloc = udc_pool_data_alloc,
51-
.unref = udc_pool_data_unref,
52-
};
53-
5424
static inline void udc_buf_destroy(struct net_buf *buf);
5525

5626
UDC_BUF_POOL_VAR_DEFINE(udc_ep_pool,

0 commit comments

Comments
 (0)