Skip to content

Commit 7b0aa54

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 cad9ff3 commit 7b0aa54

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

include/zephyr/usb/usbh.h

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

129129
/**
130+
* @cond INTERNAL_HIDDEN
131+
*
132+
* Variables used by the USB host stack but not exposed to the class
133+
* through the class API.
130134
*/
131-
#define USBH_DEFINE_CLASS(name) \
132-
static STRUCT_SECTION_ITERABLE(usbh_class_data, name)
135+
struct usbh_class_node {
136+
/** Class information exposed to host class implementations (drivers). */
137+
struct usbh_class_data *const c_data;
138+
};
139+
/* @endcond */
133140

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

135161
/**
136162
* @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: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
#include <zephyr/devicetree.h>
1111
#include <zephyr/init.h>
1212
#include <zephyr/sys/iterable_sections.h>
13+
#include <zephyr/usb/usbh.h>
1314

14-
#include "usbh_internal.h"
15+
#include "usbh_class.h"
16+
#include "usbh_class_api.h"
1517
#include "usbh_device.h"
18+
#include "usbh_internal.h"
1619

1720
#include <zephyr/logging/log.h>
1821
LOG_MODULE_REGISTER(uhs, CONFIG_USBH_LOG_LEVEL);
@@ -46,6 +49,7 @@ static int usbh_event_carrier(const struct device *dev,
4649
static void dev_connected_handler(struct usbh_context *const ctx,
4750
const struct uhc_event *const event)
4851
{
52+
int ret;
4953

5054
LOG_DBG("Device connected event");
5155
if (ctx->root != NULL) {
@@ -71,6 +75,11 @@ static void dev_connected_handler(struct usbh_context *const ctx,
7175
if (usbh_device_init(ctx->root)) {
7276
LOG_ERR("Failed to reset new USB device");
7377
}
78+
79+
ret = usbh_class_probe_all(ctx->root);
80+
if (ret != 0) {
81+
LOG_ERR("Failed to probe all classes for this new USB device");
82+
}
7483
}
7584

7685
static void dev_removed_handler(struct usbh_context *const ctx)
@@ -194,12 +203,10 @@ int usbh_init_device_intl(struct usbh_context *const uhs_ctx)
194203

195204
sys_dlist_init(&uhs_ctx->udevs);
196205

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;
206+
ret = usbh_class_init_all(uhs_ctx);
207+
if (ret != 0) {
208+
LOG_ERR("Failed to initialize all classes");
209+
return ret;
203210
}
204211

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