Skip to content

Commit b9717c5

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 052870f commit b9717c5

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

include/zephyr/usb/usbh.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,35 @@ struct usbh_class_data {
135135
};
136136

137137
/**
138+
* @cond INTERNAL_HIDDEN
138139
*/
139-
#define USBH_DEFINE_CLASS(name) \
140-
static STRUCT_SECTION_ITERABLE(usbh_class_data, name)
140+
struct usbh_class_node {
141+
/** System linked list node for registered classes */
142+
sys_snode_t node;
143+
/** Node information for the slist. */
144+
struct usbh_class_data *const c_data;
145+
};
146+
/* @endcond */
141147

148+
/**
149+
* @brief Define USB host support class data
150+
*
151+
* Macro defines class (function) data, as well as corresponding node
152+
* structures used internally by the stack.
153+
*
154+
* @param class_name[in] Class name
155+
* @param class_api[in] Pointer to struct usbh_class_api
156+
* @param class_priv[in] Class private data
157+
*/
158+
#define USBH_DEFINE_CLASS(class_name, class_api, class_priv) \
159+
static const struct usbh_class_data class_data_##class_name = { \
160+
.name = STRINGIFY(class_name), \
161+
.api = class_api, \
162+
.priv = class_priv, \
163+
}; \
164+
static STRUCT_SECTION_ITERABLE(usbh_class_node, class_name) = { \
165+
.c_data = &class_data_##class_name, \
166+
}; \
142167

143168
/**
144169
* @brief Initialize the USB host support;

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.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)