Skip to content

Commit

Permalink
[nrf fromtree] usb: device_next: Respect granularity in UDC buf pool …
Browse files Browse the repository at this point in the history
…define

Round up buffer size used in UDC_BUF_POOL_DEFINE() to respect the
required buffer granularity. The issue was observed with UAC2 explicit
feedback data, but the problem applies to any UDC_BUF_POOL_DEFINE() use.

In order for every buffer returned by UDC_BUF_POOL_DEFINE() to be both
aligned and to have required granurality, it is required to allocate the
buffers for ROUND_UP(size, LCM(UDC_BUF_GRANULARITY, UDC_BUF_ALIGN)).
Because we do not have Least Common Multiple nor Greatest Common Divisor
compile time macros, assume that granularity is multiple of alignment.
Validate the assumption with a build time assert. When we get a target
where this assumption fails we would have to come up with a solution to
compute LCM and/or GCD at compile time.

Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
(cherry picked from commit 0f41950)
Signed-off-by: Tomasz Moń <tomasz.mon@nordicsemi.no>
  • Loading branch information
tmon-nordic committed Sep 23, 2024
1 parent 583ce6e commit d7881c1
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions include/zephyr/drivers/usb/udc_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,17 @@ extern const struct net_buf_data_cb net_buf_dma_cb;
*/
#define UDC_BUF_POOL_DEFINE(pname, count, size, ud_size, fdestroy) \
_NET_BUF_ARRAY_DEFINE(pname, count, ud_size); \
BUILD_ASSERT((UDC_BUF_GRANULARITY) % (UDC_BUF_ALIGN) == 0, \
"Code assumes granurality is multiple of alignment"); \
static uint8_t __nocache __aligned(UDC_BUF_ALIGN) \
net_buf_data_##pname[count][size]; \
net_buf_data_##pname[count][ROUND_UP(size, UDC_BUF_GRANULARITY)];\
static const struct net_buf_pool_fixed net_buf_fixed_##pname = { \
.data_pool = (uint8_t *)net_buf_data_##pname, \
}; \
static const struct net_buf_data_alloc net_buf_fixed_alloc_##pname = { \
.cb = &net_buf_fixed_cb, \
.alloc_data = (void *)&net_buf_fixed_##pname, \
.max_alloc_size = size, \
.max_alloc_size = ROUND_UP(size, UDC_BUF_GRANULARITY), \
}; \
static STRUCT_SECTION_ITERABLE(net_buf_pool, pname) = \
NET_BUF_POOL_INITIALIZER(pname, &net_buf_fixed_alloc_##pname, \
Expand Down

0 comments on commit d7881c1

Please sign in to comment.