Skip to content

Commit 90a6f22

Browse files
committed
subsys: usb: host: remove root member for usbh_context
Remove root member from usbh_context. Add usbh_device_get_root and usbh_device_is_root. Make bus reset do only one time for root device. Signed-off-by: Aiden Hu <[email protected]>
1 parent 3ce2e33 commit 90a6f22

File tree

4 files changed

+56
-14
lines changed

4 files changed

+56
-14
lines changed

include/zephyr/usb/usbh.h

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

subsys/usb/host/usbh_core.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ static int usbh_event_carrier(const struct device *dev,
4848
static void dev_connected_handler(struct usbh_context *const ctx,
4949
const struct uhc_event *const event)
5050
{
51+
struct usb_device *udev;
52+
5153
LOG_DBG("Device connected event");
5254

5355
udev = usbh_device_alloc(ctx);
@@ -66,30 +68,30 @@ static void dev_connected_handler(struct usbh_context *const ctx,
6668

6769
k_mutex_lock(&ctx->mutex, K_FOREVER);
6870
sys_dlist_append(&ctx->udevs, &udev->node);
69-
70-
if (ctx->root == NULL) {
71-
ctx->root = udev;
72-
}
7371
k_mutex_unlock(&ctx->mutex);
7472

7573
if (usbh_device_init(udev)) {
7674
LOG_ERR("Failed to reset new USB device");
7775
sys_dlist_remove(&udev->node);
7876
}
7977

80-
usbh_class_probe_device(ctx->root);
78+
usbh_class_probe_device(usbh_device_get_root(ctx));
8179
}
8280

8381
static void dev_removed_handler(struct usbh_context *const ctx)
8482
{
85-
if (ctx->root != NULL) {
86-
usbh_class_remove_all(ctx->root);
87-
usbh_device_free(ctx->root);
88-
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);
8990
LOG_DBG("Device removed");
9091
} else {
9192
LOG_DBG("Spurious device removed event");
9293
}
94+
/* TODO: handle remove for all of classes for the unattached device */
9395
}
9496

9597
static int discard_ep_request(struct usbh_context *const ctx,

subsys/usb/host/usbh_device.c

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,12 +447,39 @@ int usbh_device_set_configuration(struct usb_device *const udev, const uint8_t n
447447
return err;
448448
}
449449

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

458485
if (udev->state != USB_STATE_DEFAULT) {
@@ -467,8 +494,16 @@ int usbh_device_init(struct usb_device *const udev)
467494
}
468495

469496
k_mutex_lock(&uhs_ctx->mutex, K_FOREVER);
470-
SYS_DLIST_FOR_EACH_NODE(&uhs_ctx->udevs, node) {
471-
device_count++;
497+
device_count = sys_dlist_len(&uhs_ctx->udevs);
498+
k_mutex_unlock(&uhs_ctx->mutex);
499+
500+
/* Only reset bus if this is the root device. */
501+
if (device_count == 1U) {
502+
err = uhc_bus_reset(uhs_ctx->dev);
503+
if (err) {
504+
LOG_ERR("Failed to signal bus reset");
505+
return err;
506+
}
472507
}
473508
k_mutex_unlock(&uhs_ctx->mutex);
474509

subsys/usb/host/usbh_device.h

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

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

0 commit comments

Comments
 (0)