Skip to content

Commit 2699c48

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 4da58cc commit 2699c48

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

include/zephyr/usb/usbh.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,36 @@ struct usbh_class_data {
133133
};
134134

135135
/**
136+
* @cond INTERNAL_HIDDEN
137+
*
138+
* Variables used by the USB host stack but not exposed to the class
139+
* through the class API.
136140
*/
137-
#define USBH_DEFINE_CLASS(name) \
138-
static STRUCT_SECTION_ITERABLE(usbh_class_data, name)
141+
struct usbh_class_node {
142+
/** Class information exposed to host class implementations (drivers). */
143+
struct usbh_class_data *const c_data;
144+
};
145+
/* @endcond */
139146

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

141167
/**
142168
* @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_core.c

Lines changed: 8 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_api.h"
1618

1719
#include <zephyr/logging/log.h>
1820
LOG_MODULE_REGISTER(uhs, CONFIG_USBH_LOG_LEVEL);
@@ -194,12 +196,12 @@ int usbh_init_device_intl(struct usbh_context *const uhs_ctx)
194196

195197
sys_dlist_init(&uhs_ctx->udevs);
196198

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;
199+
STRUCT_SECTION_FOREACH(usbh_class_node, c_node) {
200+
ret = usbh_class_init(c_node->c_data, uhs_ctx);
201+
if (ret != 0) {
202+
LOG_ERR("Failed to initialize all registered class instances");
203+
return ret;
204+
}
203205
}
204206

205207
return 0;

subsys/usb/host/usbh_data.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#include <zephyr/linker/iterable_sections.h>
22

33
ITERABLE_SECTION_RAM(usbh_context, Z_LINK_ITERABLE_SUBALIGN)
4-
ITERABLE_SECTION_RAM(usbh_class_data, Z_LINK_ITERABLE_SUBALIGN)
4+
ITERABLE_SECTION_RAM(usbh_class_node, Z_LINK_ITERABLE_SUBALIGN)

0 commit comments

Comments
 (0)