Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions include/zephyr/usb/usbh.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ struct usbh_class_filter {
*/
struct usbh_class_api {
/** Host initialization handler, before any device is connected */
int (*init)(struct usbh_class_data *const c_data,
struct usbh_context *const uhs_ctx);
int (*init)(struct usbh_class_data *const c_data);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only bind to a controller after probing.

Something like c_data->uhs_ctx = udev->ctx; is missed here

c_node->state = USBH_CLASS_STATE_BOUND;
c_data->udev = udev;
c_data->iface = iface;

Also, c_data->uhs_ctx seems to be redundant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to remove the redundacy by removing the uhs_ctx field, available from udev->ctx.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need a helper to get the host support context, like

diff --git a/subsys/usb/host/usbh_class.h b/subsys/usb/host/usbh_class.h
index 0b3d673c058..eb005b0922e 100644
--- a/subsys/usb/host/usbh_class.h
+++ b/subsys/usb/host/usbh_class.h
@@ -60,4 +60,13 @@ void usbh_class_probe_device(struct usb_device *const udev);
  */
 void usbh_class_remove_all(struct usb_device *const udev);
 
+static inline struct usbh_context *usbh_class_get_ctx(struct usbh_class_data *const c_data)
+{
+	if (c_data->udev != NULL) {
+		return c_data->udev->ctx;
+	}
+
+	return NULL;
+}
+
 #endif /* ZEPHYR_INCLUDE_USBH_CLASS_H */

Copy link
Copy Markdown
Contributor Author

@josuah josuah Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added and rebased on top of latest main.

Edit: that was not the intent to add it now, so removed it in order to keep it for later.

/** Request completion handler */
int (*completion_cb)(struct usbh_class_data *const c_data,
struct uhc_transfer *const xfer);
Expand All @@ -121,8 +120,6 @@ struct usbh_class_api {
struct usbh_class_data {
/** Name of the USB host class instance */
const char *name;
/** Pointer to USB host stack context structure */
struct usbh_context *uhs_ctx;
/** Pointer to USB device this class is used for */
struct usb_device *udev;
/** First interface number or claimed function */
Expand Down
3 changes: 1 addition & 2 deletions subsys/usb/host/class/usbh_uvc.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ static int usbh_uvc_removed(struct usbh_class_data *const c_data)
return 0;
}

static int usbh_uvc_init(struct usbh_class_data *const c_data,
struct usbh_context *const uhs_ctx)
static int usbh_uvc_init(struct usbh_class_data *const c_data)
{
return 0;
}
Expand Down
8 changes: 2 additions & 6 deletions subsys/usb/host/usbh_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@

LOG_MODULE_REGISTER(usbh_class, CONFIG_USBH_LOG_LEVEL);

void usbh_class_init_all(struct usbh_context *const uhs_ctx)
void usbh_class_init_all(void)
{
int ret;

usbh_host_lock(uhs_ctx);

Comment thread
josuah marked this conversation as resolved.
STRUCT_SECTION_FOREACH(usbh_class_node, c_node) {
struct usbh_class_data *const c_data = c_node->c_data;

Expand All @@ -29,15 +27,13 @@ void usbh_class_init_all(struct usbh_context *const uhs_ctx)
continue;
}

ret = usbh_class_init(c_data, uhs_ctx);
ret = usbh_class_init(c_data);
if (ret != 0) {
LOG_WRN("Failed to initialize class %s (%d)",
c_data->name, ret);
c_node->state = USBH_CLASS_STATE_ERROR;
}
}

usbh_host_unlock(uhs_ctx);
}

void usbh_class_remove_all(struct usb_device *const udev)
Expand Down
4 changes: 1 addition & 3 deletions subsys/usb/host/usbh_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ bool usbh_class_is_matching(const struct usbh_class_filter *const filter_rules,

/**
* @brief Initialize all available host class instances.
*
* @param[in] uhs_ctx USB Host context to pass to the class.
*/
void usbh_class_init_all(struct usbh_context *const uhs_ctx);
void usbh_class_init_all(void);

/**
* @brief Probe an USB device function against all available host class instances.
Expand Down
6 changes: 2 additions & 4 deletions subsys/usb/host/usbh_class_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@
* It can be used to initialize underlying systems.
*
* @param[in] c_data Pointer to USB host class data
* @param[in] uhs_ctx USB host context to assign to this class
* @return 0 on success, negative error code on failure.
*/
static inline int usbh_class_init(struct usbh_class_data *const c_data,
struct usbh_context *const uhs_ctx)
static inline int usbh_class_init(struct usbh_class_data *const c_data)
{
const struct usbh_class_api *api = c_data->api;

if (api->init != NULL) {
return api->init(c_data, uhs_ctx);
return api->init(c_data);
}

return -ENOTSUP;
Expand Down
4 changes: 2 additions & 2 deletions subsys/usb/host/usbh_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ int usbh_init_device_intl(struct usbh_context *const uhs_ctx)

sys_dlist_init(&uhs_ctx->udevs);

usbh_class_init_all(uhs_ctx);

return 0;
}

Expand All @@ -221,6 +219,8 @@ static int uhs_pre_init(void)

k_thread_name_set(&usbh_bus_thread_data, "usbh_bus");

usbh_class_init_all();

return 0;
}

Expand Down
3 changes: 1 addition & 2 deletions tests/subsys/usb/host/src/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ static struct usbh_foo_priv usbh_foo_priv = {
.state = FOO_CLASS_PRIV_INACTIVE,
};

static int usbh_foo_init(struct usbh_class_data *const c_data,
struct usbh_context *const uhs_ctx)
static int usbh_foo_init(struct usbh_class_data *const c_data)
Comment thread
josuah marked this conversation as resolved.
{
struct usbh_foo_priv *priv = c_data->priv;

Expand Down