Skip to content

Commit d194419

Browse files
committed
subsys: usb: host: refine connect/disconnect handling
Add two functions: usbh_connect_device() for device connection usbh_disconnect_device() for device disconnection These functions centralize the logic for device attach/detach, including class probe and remove handling. They can be invoked by the hub class as well as dev_connected_handler and dev_removed_handler, improving code clarity and reuse. Signed-off-by: Aiden Hu <[email protected]>
1 parent 11fab0b commit d194419

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

subsys/usb/host/usbh_core.c

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include <zephyr/sys/iterable_sections.h>
1313
#include <zephyr/usb/usbh.h>
1414

15-
#include "usbh_class.h"
16-
#include "usbh_class_api.h"
1715
#include "usbh_device.h"
1816
#include "usbh_internal.h"
1917

@@ -50,49 +48,30 @@ static void dev_connected_handler(struct usbh_context *const ctx,
5048
const struct uhc_event *const event)
5149
{
5250
struct usb_device *udev;
53-
54-
LOG_DBG("Device connected event");
55-
56-
udev = usbh_device_alloc(ctx);
57-
if (udev == NULL) {
58-
LOG_ERR("Failed allocate new device");
59-
return;
60-
}
61-
62-
udev->state = USB_STATE_DEFAULT;
51+
uint8_t speed;
6352

6453
if (event->type == UHC_EVT_DEV_CONNECTED_HS) {
65-
udev->speed = USB_SPEED_SPEED_HS;
54+
speed = USB_SPEED_SPEED_HS;
6655
} else {
67-
udev->speed = USB_SPEED_SPEED_FS;
56+
speed = USB_SPEED_SPEED_FS;
6857
}
6958

70-
k_mutex_lock(&ctx->mutex, K_FOREVER);
71-
sys_dlist_append(&ctx->udevs, &udev->node);
72-
k_mutex_unlock(&ctx->mutex);
73-
74-
if (usbh_device_init(udev)) {
75-
LOG_ERR("Failed to reset new USB device");
76-
sys_dlist_remove(&udev->node);
59+
udev = usbh_connect_device(ctx, speed);
60+
if (udev == NULL) {
61+
return;
7762
}
78-
79-
usbh_class_probe_device(usbh_device_get_root(ctx));
8063
}
8164

8265
static void dev_removed_handler(struct usbh_context *const ctx)
8366
{
8467
struct usb_device *udev = NULL;
8568

8669
udev = usbh_device_get_root(ctx);
87-
8870
if (udev) {
89-
usbh_class_remove_all(udev);
90-
usbh_device_free(udev);
91-
LOG_DBG("Device removed");
71+
usbh_disconnect_device(ctx, udev);
9272
} else {
9373
LOG_DBG("Spurious device removed event");
9474
}
95-
/* TODO: handle remove for all of classes for the unattached device */
9675
}
9776

9877
static int discard_ep_request(struct usbh_context *const ctx,

subsys/usb/host/usbh_device.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include "usbh_device.h"
1111
#include "usbh_ch9.h"
12+
#include "usbh_class.h"
13+
#include "usbh_class_api.h"
1214

1315
#include <zephyr/logging/log.h>
1416
LOG_MODULE_REGISTER(usbh_dev, CONFIG_USBH_LOG_LEVEL);
@@ -475,6 +477,47 @@ bool usbh_device_is_root(struct usbh_context *const ctx,
475477
return sys_dlist_peek_head(&ctx->udevs) == &udev->node;
476478
}
477479

480+
struct usb_device *usbh_connect_device(struct usbh_context *const ctx,
481+
uint8_t speed)
482+
{
483+
struct usb_device *udev;
484+
485+
LOG_DBG("Device connected event");
486+
487+
/* Allocate new device */
488+
udev = usbh_device_alloc(ctx);
489+
if (udev == NULL) {
490+
LOG_ERR("Failed allocate new device");
491+
return NULL;
492+
}
493+
494+
udev->state = USB_STATE_DEFAULT;
495+
udev->speed = speed;
496+
497+
if (usbh_device_init(udev)) {
498+
LOG_ERR("Failed to init new USB device");
499+
usbh_device_free(udev);
500+
return NULL;
501+
}
502+
503+
usbh_class_probe_device(udev);
504+
505+
return udev;
506+
}
507+
508+
void usbh_disconnect_device(struct usbh_context *ctx, struct usb_device *udev)
509+
{
510+
if (!ctx || !udev) {
511+
return;
512+
}
513+
514+
usbh_class_remove_all(udev);
515+
516+
usbh_device_free(udev);
517+
518+
LOG_DBG("Device removed");
519+
}
520+
478521
int usbh_device_init(struct usb_device *const udev)
479522
{
480523
struct usbh_context *const uhs_ctx = udev->ctx;

subsys/usb/host/usbh_device.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ struct usb_device *usbh_device_get_root(struct usbh_context *const ctx);
4343
bool usbh_device_is_root(struct usbh_context *const ctx,
4444
struct usb_device *const udev);
4545

46+
/* Connect a new USB device */
47+
struct usb_device *usbh_connect_device(struct usbh_context *const ctx,
48+
uint8_t device_speed);
49+
/* Disconnect USB device */
50+
void usbh_disconnect_device(struct usbh_context *ctx, struct usb_device *udev);
51+
4652
/* Wrappers around to avoid glue UHC calls. */
4753
static inline struct uhc_transfer *usbh_xfer_alloc(struct usb_device *udev,
4854
const uint8_t ep,

0 commit comments

Comments
 (0)