Skip to content

Commit e0f2b7d

Browse files
author
Josuah Demangeon
committed
usb: host: wrap the mutex lock with a helper
Hide the mutex lock details inside a wrapper call, like done for the device stack API. This is not used for the per-device lock of the host stack which use a different mutex lock. Signed-off-by: Josuah Demangeon <[email protected]>
1 parent 9f1d92a commit e0f2b7d

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

subsys/usb/host/usbh_api.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ int usbh_init(struct usbh_context *uhs_ctx)
1515
{
1616
int ret;
1717

18-
k_mutex_lock(&uhs_ctx->mutex, K_FOREVER);
18+
usbh_host_lock(uhs_ctx);
1919

2020
if (!device_is_ready(uhs_ctx->dev)) {
2121
LOG_ERR("USB host controller is not ready");
@@ -32,15 +32,15 @@ int usbh_init(struct usbh_context *uhs_ctx)
3232
ret = usbh_init_device_intl(uhs_ctx);
3333

3434
init_exit:
35-
k_mutex_unlock(&uhs_ctx->mutex);
35+
usbh_host_unlock(uhs_ctx);
3636
return ret;
3737
}
3838

3939
int usbh_enable(struct usbh_context *uhs_ctx)
4040
{
4141
int ret;
4242

43-
k_mutex_lock(&uhs_ctx->mutex, K_FOREVER);
43+
usbh_host_lock(uhs_ctx);
4444

4545
if (!uhc_is_initialized(uhs_ctx->dev)) {
4646
LOG_WRN("USB host controller is not initialized");
@@ -61,7 +61,7 @@ int usbh_enable(struct usbh_context *uhs_ctx)
6161
}
6262

6363
enable_exit:
64-
k_mutex_unlock(&uhs_ctx->mutex);
64+
usbh_host_unlock(uhs_ctx);
6565
return ret;
6666
}
6767

@@ -74,14 +74,14 @@ int usbh_disable(struct usbh_context *uhs_ctx)
7474
return 0;
7575
}
7676

77-
k_mutex_lock(&uhs_ctx->mutex, K_FOREVER);
77+
usbh_host_lock(uhs_ctx);
7878

7979
ret = uhc_disable(uhs_ctx->dev);
8080
if (ret) {
8181
LOG_ERR("Failed to disable USB controller");
8282
}
8383

84-
k_mutex_unlock(&uhs_ctx->mutex);
84+
usbh_host_unlock(uhs_ctx);
8585

8686
return 0;
8787
}
@@ -90,14 +90,14 @@ int usbh_shutdown(struct usbh_context *const uhs_ctx)
9090
{
9191
int ret;
9292

93-
k_mutex_lock(&uhs_ctx->mutex, K_FOREVER);
93+
usbh_host_lock(uhs_ctx);
9494

9595
ret = uhc_shutdown(uhs_ctx->dev);
9696
if (ret) {
9797
LOG_ERR("Failed to shutdown USB device");
9898
}
9999

100-
k_mutex_unlock(&uhs_ctx->mutex);
100+
usbh_host_unlock(uhs_ctx);
101101

102102
return ret;
103103
}

subsys/usb/host/usbh_host.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_USBH_HOST_H
8+
#define ZEPHYR_INCLUDE_USBH_HOST_H
9+
10+
#include <stdbool.h>
11+
#include <zephyr/usb/usbh.h>
12+
13+
/**
14+
* @brief Lock the USB host stack context
15+
*
16+
* @param[in] uhs_ctx Pointer to a host context
17+
*/
18+
static inline void usbh_host_lock(struct usbh_context *const uhs_ctx)
19+
{
20+
k_mutex_lock(&uhs_ctx->mutex, K_FOREVER);
21+
}
22+
23+
/**
24+
* @brief Unlock the USB host stack context
25+
*
26+
* @param[in] uhs_ctx Pointer to a host context
27+
*/
28+
static inline void usbh_host_unlock(struct usbh_context *const uhs_ctx)
29+
{
30+
k_mutex_unlock(&uhs_ctx->mutex);
31+
}
32+
33+
#endif /* ZEPHYR_INCLUDE_USBH_HOST_H */

0 commit comments

Comments
 (0)