Skip to content

Commit ebec837

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 2fd145c commit ebec837

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

subsys/usb/host/usbh_core.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,35 @@ static int usbh_event_carrier(const struct device *dev,
4949
static void dev_connected_handler(struct usbh_context *const ctx,
5050
const struct uhc_event *const event)
5151
{
52+
struct usb_device *udev;
53+
5254
LOG_DBG("Device connected event");
53-
if (ctx->root != NULL) {
54-
LOG_ERR("Device already connected");
55-
usbh_device_free(ctx->root);
56-
ctx->root = NULL;
57-
}
5855

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

65-
ctx->root->state = USB_STATE_DEFAULT;
62+
udev->state = USB_STATE_DEFAULT;
6663

6764
if (event->type == UHC_EVT_DEV_CONNECTED_HS) {
68-
ctx->root->speed = USB_SPEED_SPEED_HS;
65+
udev->speed = USB_SPEED_SPEED_HS;
6966
} else {
70-
ctx->root->speed = USB_SPEED_SPEED_FS;
67+
udev->speed = USB_SPEED_SPEED_FS;
68+
}
69+
70+
k_mutex_lock(&ctx->mutex, K_FOREVER);
71+
sys_dlist_append(&ctx->udevs, &udev->node);
72+
73+
if (ctx->root == NULL) {
74+
ctx->root = udev;
7175
}
76+
k_mutex_unlock(&ctx->mutex);
7277

73-
if (usbh_device_init(ctx->root)) {
78+
if (usbh_device_init(udev)) {
7479
LOG_ERR("Failed to reset new USB device");
80+
sys_dlist_remove(&udev->node);
7581
}
7682

7783
usbh_class_probe_device(ctx->root);

subsys/usb/host/usbh_device.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ int usbh_device_init(struct usb_device *const udev)
452452
struct usbh_context *const uhs_ctx = udev->ctx;
453453
uint8_t new_addr;
454454
int err;
455+
sys_dnode_t *node;
456+
uint8_t device_count = 0;
455457

456458
if (udev->state != USB_STATE_DEFAULT) {
457459
LOG_ERR("USB device is not in default state");
@@ -464,11 +466,17 @@ int usbh_device_init(struct usb_device *const udev)
464466
return err;
465467
}
466468

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

474482
/*

0 commit comments

Comments
 (0)