Skip to content

Commit f3d7098

Browse files
AidenHuJosuah Demangeon
authored andcommitted
subsys: usb: host: support multiple devices under hub
When hub is used, need to consider about multiple devices are attached. Signed-off-by: Aiden Hu <[email protected]>
1 parent 47b9ed9 commit f3d7098

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

subsys/usb/host/usbh_core.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,35 @@ 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");
52-
if (ctx->root != NULL) {
53-
LOG_ERR("Device already connected");
54-
usbh_device_free(ctx->root);
55-
ctx->root = NULL;
56-
}
5754

58-
ctx->root = usbh_device_alloc(ctx);
59-
if (ctx->root == NULL) {
55+
udev = usbh_device_alloc(ctx);
56+
if (udev == NULL) {
6057
LOG_ERR("Failed allocate new device");
6158
return;
6259
}
6360

64-
ctx->root->state = USB_STATE_DEFAULT;
61+
udev->state = USB_STATE_DEFAULT;
6562

6663
if (event->type == UHC_EVT_DEV_CONNECTED_HS) {
67-
ctx->root->speed = USB_SPEED_SPEED_HS;
64+
udev->speed = USB_SPEED_SPEED_HS;
6865
} else {
69-
ctx->root->speed = USB_SPEED_SPEED_FS;
66+
udev->speed = USB_SPEED_SPEED_FS;
67+
}
68+
69+
k_mutex_lock(&ctx->mutex, K_FOREVER);
70+
sys_dlist_append(&ctx->udevs, &udev->node);
71+
72+
if (ctx->root == NULL) {
73+
ctx->root = udev;
7074
}
75+
k_mutex_unlock(&ctx->mutex);
7176

72-
if (usbh_device_init(ctx->root)) {
77+
if (usbh_device_init(udev)) {
7378
LOG_ERR("Failed to reset new USB device");
79+
sys_dlist_remove(&udev->node);
7480
}
7581

7682
usbh_class_probe_device(ctx->root);

subsys/usb/host/usbh_device.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ int usbh_device_init(struct usb_device *const udev)
451451
struct usbh_context *const uhs_ctx = udev->ctx;
452452
uint8_t new_addr;
453453
int err;
454+
uint8_t device_count = 0;
454455

455456
if (udev->state != USB_STATE_DEFAULT) {
456457
LOG_ERR("USB device is not in default state");
@@ -463,11 +464,17 @@ int usbh_device_init(struct usb_device *const udev)
463464
return err;
464465
}
465466

466-
/* FIXME: The port to which the device is connected should be reset. */
467-
err = uhc_bus_reset(uhs_ctx->dev);
468-
if (err) {
469-
LOG_ERR("Failed to signal bus reset");
470-
return err;
467+
k_mutex_lock(&uhs_ctx->mutex, K_FOREVER);
468+
device_count = sys_dlist_len(&uhs_ctx->udevs);
469+
k_mutex_unlock(&uhs_ctx->mutex);
470+
471+
/* Only reset bus if this is the root device. */
472+
if (device_count == 1U) {
473+
err = uhc_bus_reset(uhs_ctx->dev);
474+
if (err) {
475+
LOG_ERR("Failed to signal bus reset");
476+
return err;
477+
}
471478
}
472479

473480
/*

0 commit comments

Comments
 (0)