Skip to content

Commit 059ede5

Browse files
Josuah DemangeonAidenHu
andcommitted
usb: host: introduce usbh_class with init functions
Add functions to register all classes as part of a new usbh_class.c and a matching usbh_class.h. These functions are called from the function usbh_init_device_intl() in usbh_core.c to initialize every class upon connection of a device. The class instances are registered and connected together as a linked list. Co-authored-by: Aiden Hu <[email protected]> Signed-off-by: Josuah Demangeon <[email protected]>
1 parent f688a81 commit 059ede5

File tree

5 files changed

+99
-7
lines changed

5 files changed

+99
-7
lines changed

include/zephyr/usb/usbh.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ struct usbh_class_data {
119119
const char *name;
120120
/** Pointer to USB host stack context structure */
121121
struct usbh_context *uhs_ctx;
122+
/** System linked list node for registered classes */
123+
sys_snode_t node;
122124
/** Class code supported by this instance */
123125
struct usbh_code_triple code;
124126
/** Pointer to host support class API */

subsys/usb/host/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ zephyr_library()
55
zephyr_library_include_directories(${CMAKE_CURRENT_SOURCE_DIR})
66

77
zephyr_library_sources(
8+
usbh_api.c
89
usbh_ch9.c
10+
usbh_class.c
911
usbh_core.c
10-
usbh_api.c
1112
usbh_device.c
1213
)
1314

subsys/usb/host/usbh_class.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
* Copyright 2025 NXP
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include <zephyr/usb/usbh.h>
9+
#include <zephyr/logging/log.h>
10+
11+
#include "usbh_class.h"
12+
#include "usbh_class_api.h"
13+
14+
LOG_MODULE_REGISTER(usbh_class, CONFIG_USBH_LOG_LEVEL);
15+
16+
int usbh_register_all_classes(struct usbh_context *uhs_ctx)
17+
{
18+
int registered_count = 0;
19+
20+
STRUCT_SECTION_FOREACH(usbh_class_data, cdata_existing) {
21+
struct usbh_class_data *cdata_registered;
22+
bool already_registered = false;
23+
24+
/* Check if already registered */
25+
SYS_SLIST_FOR_EACH_CONTAINER(&uhs_ctx->class_list, cdata_registered, node) {
26+
if (cdata_existing == cdata_registered) {
27+
already_registered = true;
28+
break;
29+
}
30+
}
31+
32+
if (!already_registered) {
33+
sys_slist_append(&uhs_ctx->class_list, &cdata_existing->node);
34+
registered_count++;
35+
LOG_DBG("Auto-registered class: %s", cdata_existing->name);
36+
}
37+
}
38+
39+
LOG_INF("Auto-registered %d classes to controller %s",
40+
registered_count, uhs_ctx->name);
41+
42+
return 0;
43+
}
44+
45+
int usbh_init_registered_classes(struct usbh_context *uhs_ctx)
46+
{
47+
struct usbh_class_data *cdata;
48+
int ret;
49+
50+
SYS_SLIST_FOR_EACH_CONTAINER(&uhs_ctx->class_list, cdata, node) {
51+
ret = usbh_class_init(cdata);
52+
if (ret != 0) {
53+
LOG_ERR("Failed to initialize class instance");
54+
return ret;
55+
}
56+
}
57+
58+
return 0;
59+
}

subsys/usb/host/usbh_class.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
* Copyright 2025 NXP
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#ifndef ZEPHYR_INCLUDE_USBD_CLASS_H
9+
#define ZEPHYR_INCLUDE_USBD_CLASS_H
10+
11+
#include <zephyr/usb/usbh.h>
12+
13+
/**
14+
* @brief Auto-register all compile-time defined class drivers
15+
*/
16+
int usbh_register_all_classes(struct usbh_context *uhs_ctx);
17+
18+
/**
19+
* @brief Initialize registered class drivers
20+
*/
21+
int usbh_init_registered_classes(struct usbh_context *uhs_ctx);
22+
23+
#endif /* ZEPHYR_INCLUDE_USBD_CLASS_H */

subsys/usb/host/usbh_core.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include <zephyr/devicetree.h>
1111
#include <zephyr/init.h>
1212
#include <zephyr/sys/iterable_sections.h>
13+
#include <zephyr/usb/usbh.h>
1314

1415
#include "usbh_internal.h"
1516
#include "usbh_device.h"
17+
#include "usbh_class.h"
1618

1719
#include <zephyr/logging/log.h>
1820
LOG_MODULE_REGISTER(uhs, CONFIG_USBH_LOG_LEVEL);
@@ -193,13 +195,18 @@ int usbh_init_device_intl(struct usbh_context *const uhs_ctx)
193195
}
194196

195197
sys_dlist_init(&uhs_ctx->udevs);
198+
sys_slist_init(&uhs_ctx->class_list);
196199

197-
STRUCT_SECTION_FOREACH(usbh_class_data, cdata) {
198-
/*
199-
* For now, we have not implemented any class drivers,
200-
* so just keep it as placeholder.
201-
*/
202-
break;
200+
ret = usbh_register_all_classes(uhs_ctx);
201+
if (ret != 0) {
202+
LOG_ERR("Failed to auto-register class instances");
203+
return ret;
204+
}
205+
206+
ret = usbh_init_registered_classes(uhs_ctx);
207+
if (ret != 0) {
208+
LOG_ERR("Failed to initialize all registered class instances");
209+
return ret;
203210
}
204211

205212
return 0;

0 commit comments

Comments
 (0)