Skip to content

Commit c042ba0

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 c042ba0

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

subsys/usb/host/usbh_api.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <errno.h>
88
#include <zephyr/sys/util.h>
9+
#include "usbh_host.h"
910
#include "usbh_internal.h"
1011

1112
#include <zephyr/logging/log.h>
@@ -15,7 +16,7 @@ int usbh_init(struct usbh_context *uhs_ctx)
1516
{
1617
int ret;
1718

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

2021
if (!device_is_ready(uhs_ctx->dev)) {
2122
LOG_ERR("USB host controller is not ready");
@@ -32,15 +33,15 @@ int usbh_init(struct usbh_context *uhs_ctx)
3233
ret = usbh_init_device_intl(uhs_ctx);
3334

3435
init_exit:
35-
k_mutex_unlock(&uhs_ctx->mutex);
36+
usbh_host_unlock(uhs_ctx);
3637
return ret;
3738
}
3839

3940
int usbh_enable(struct usbh_context *uhs_ctx)
4041
{
4142
int ret;
4243

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

4546
if (!uhc_is_initialized(uhs_ctx->dev)) {
4647
LOG_WRN("USB host controller is not initialized");
@@ -61,7 +62,7 @@ int usbh_enable(struct usbh_context *uhs_ctx)
6162
}
6263

6364
enable_exit:
64-
k_mutex_unlock(&uhs_ctx->mutex);
65+
usbh_host_unlock(uhs_ctx);
6566
return ret;
6667
}
6768

@@ -74,14 +75,14 @@ int usbh_disable(struct usbh_context *uhs_ctx)
7475
return 0;
7576
}
7677

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

7980
ret = uhc_disable(uhs_ctx->dev);
8081
if (ret) {
8182
LOG_ERR("Failed to disable USB controller");
8283
}
8384

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

8687
return 0;
8788
}
@@ -90,14 +91,14 @@ int usbh_shutdown(struct usbh_context *const uhs_ctx)
9091
{
9192
int ret;
9293

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

9596
ret = uhc_shutdown(uhs_ctx->dev);
9697
if (ret) {
9798
LOG_ERR("Failed to shutdown USB device");
9899
}
99100

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

102103
return ret;
103104
}

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)