Skip to content

Commit fbe29a6

Browse files
AidenHuJosuah Demangeon
authored andcommitted
subsys: usb: host: remove root member for usbh_context
add usbh_device_get_root and usbh_device_is_root function to check root device Signed-off-by: Aiden Hu <[email protected]>
1 parent f3d7098 commit fbe29a6

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

include/zephyr/usb/usbh.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ struct usbh_context {
5858
struct usbh_status status;
5959
/** USB device list */
6060
sys_dlist_t udevs;
61-
/** USB root device */
62-
struct usb_device *root;
6361
/** Allocated device addresses bit array */
6462
struct sys_bitarray *addr_ba;
6563
};

subsys/usb/host/usbh_core.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,30 @@ static void dev_connected_handler(struct usbh_context *const ctx,
6868

6969
k_mutex_lock(&ctx->mutex, K_FOREVER);
7070
sys_dlist_append(&ctx->udevs, &udev->node);
71-
72-
if (ctx->root == NULL) {
73-
ctx->root = udev;
74-
}
7571
k_mutex_unlock(&ctx->mutex);
7672

7773
if (usbh_device_init(udev)) {
7874
LOG_ERR("Failed to reset new USB device");
7975
sys_dlist_remove(&udev->node);
8076
}
8177

82-
usbh_class_probe_device(ctx->root);
78+
usbh_class_probe_device(usbh_device_get_root(ctx));
8379
}
8480

8581
static void dev_removed_handler(struct usbh_context *const ctx)
8682
{
87-
if (ctx->root != NULL) {
88-
usbh_class_remove_all(ctx->root);
89-
usbh_device_free(ctx->root);
90-
ctx->root = NULL;
83+
struct usb_device *udev = NULL;
84+
85+
udev = usbh_device_get_root(ctx);
86+
87+
if (udev) {
88+
usbh_class_remove_all(udev);
89+
usbh_device_free(udev);
9190
LOG_DBG("Device removed");
9291
} else {
9392
LOG_DBG("Spurious device removed event");
9493
}
94+
/* TODO: handle remove for all of classes for the unattached device */
9595
}
9696

9797
static int discard_ep_request(struct usbh_context *const ctx,

subsys/usb/host/usbh_device.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,34 @@ int usbh_device_set_configuration(struct usb_device *const udev, const uint8_t n
446446
return err;
447447
}
448448

449+
struct usb_device *usbh_device_get_root(struct usbh_context *const ctx)
450+
{
451+
sys_dnode_t *node;
452+
453+
if (ctx == NULL) {
454+
return NULL;
455+
}
456+
457+
node = sys_dlist_peek_head(&ctx->udevs);
458+
if (node == NULL) {
459+
/* No devices in the list */
460+
return NULL;
461+
}
462+
463+
/* Get the usb_device structure from the node */
464+
return CONTAINER_OF(node, struct usb_device, node);
465+
}
466+
467+
bool usbh_device_is_root(struct usbh_context *const ctx,
468+
struct usb_device *const udev)
469+
{
470+
if (ctx == NULL || udev == NULL) {
471+
return false;
472+
}
473+
474+
return sys_dlist_peek_head(&ctx->udevs) == &udev->node;
475+
}
476+
449477
int usbh_device_init(struct usb_device *const udev)
450478
{
451479
struct usbh_context *const uhs_ctx = udev->ctx;

subsys/usb/host/usbh_device.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ int usbh_device_interface_set(struct usb_device *const udev,
3535
const uint8_t iface, const uint8_t alt,
3636
const bool dry);
3737

38+
/* Get root USB device */
39+
struct usb_device *usbh_device_get_root(struct usbh_context *const ctx);
40+
41+
/* Check if USB device is root */
42+
bool usbh_device_is_root(struct usbh_context *const ctx,
43+
struct usb_device *const udev);
44+
3845
/* Wrappers around to avoid glue UHC calls. */
3946
static inline struct uhc_transfer *usbh_xfer_alloc(struct usb_device *udev,
4047
const uint8_t ep,

0 commit comments

Comments
 (0)