Skip to content

Commit 4f2a48f

Browse files
committed
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 8785093 commit 4f2a48f

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

include/zephyr/usb/usbh.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ struct usbh_context {
4545
const struct device *dev;
4646
/** USB device list */
4747
sys_dlist_t udevs;
48-
/** USB root device */
49-
struct usb_device *root;
5048
/** Allocated device addresses bit array */
5149
struct sys_bitarray *addr_ba;
5250
};

subsys/usb/host/usbh_core.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static int usbh_event_carrier(const struct device *dev,
4646
static void dev_connected_handler(struct usbh_context *const ctx,
4747
const struct uhc_event *const event)
4848
{
49+
int err;
4950
struct usb_device *udev;
5051

5152
LOG_DBG("Device connected event");
@@ -66,27 +67,28 @@ static void dev_connected_handler(struct usbh_context *const ctx,
6667

6768
k_mutex_lock(&ctx->mutex, K_FOREVER);
6869
sys_dlist_append(&ctx->udevs, &udev->node);
69-
70-
if (ctx->root == NULL) {
71-
ctx->root = udev;
72-
}
7370
k_mutex_unlock(&ctx->mutex);
7471

75-
if (usbh_device_init(udev)) {
72+
err = usbh_device_init(udev);
73+
if (ret != 0) {
7674
LOG_ERR("Failed to reset new USB device");
7775
sys_dlist_remove(&udev->node);
7876
}
7977
}
8078

8179
static void dev_removed_handler(struct usbh_context *const ctx)
8280
{
83-
if (ctx->root != NULL) {
84-
usbh_device_free(ctx->root);
85-
ctx->root = NULL;
81+
struct usb_device *udev = NULL;
82+
83+
udev = usbh_device_get_root(ctx);
84+
85+
if (NULL != udev) {
86+
usbh_device_free(udev);
8687
LOG_DBG("Device removed");
8788
} else {
8889
LOG_DBG("Spurious device removed event");
8990
}
91+
/* TODO: handle remove for all of classes for the unattached device */
9092
}
9193

9294
static int discard_ep_request(struct usbh_context *const ctx,

subsys/usb/host/usbh_device.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,26 @@ 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+
node = sys_dlist_peek_head(&ctx->udevs);
455+
if (node == NULL) {
456+
/* No devices in the list */
457+
return NULL;
458+
}
459+
460+
/* Get the usb_device structure from the node */
461+
return CONTAINER_OF(node, struct usb_device, node);
462+
}
463+
464+
bool usbh_device_is_root(struct usbh_context *const ctx,
465+
struct usb_device *const udev)
466+
{
467+
return sys_dlist_peek_head(&ctx->udevs) == &udev->node;
468+
}
469+
450470
int usbh_device_init(struct usb_device *const udev)
451471
{
452472
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
@@ -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)