Skip to content

Commit 7a5adf2

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 d3aa30e commit 7a5adf2

File tree

3 files changed

+57
-38
lines changed

3 files changed

+57
-38
lines changed

subsys/usb/host/usbh_core.c

Lines changed: 8 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

@@ -45,53 +43,35 @@ static int usbh_event_carrier(const struct device *dev,
4543

4644
return err;
4745
}
46+
4847
static void dev_connected_handler(struct usbh_context *const ctx,
4948
const struct uhc_event *const event)
5049
{
5150
struct usb_device *udev;
52-
53-
LOG_DBG("Device connected event");
54-
55-
udev = usbh_device_alloc(ctx);
56-
if (udev == NULL) {
57-
LOG_ERR("Failed allocate new device");
58-
return;
59-
}
60-
61-
udev->state = USB_STATE_DEFAULT;
51+
uint8_t speed;
6252

6353
if (event->type == UHC_EVT_DEV_CONNECTED_HS) {
64-
udev->speed = USB_SPEED_SPEED_HS;
54+
speed = USB_SPEED_SPEED_HS;
6555
} else {
66-
udev->speed = USB_SPEED_SPEED_FS;
56+
speed = USB_SPEED_SPEED_FS;
6757
}
6858

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

8165
static void dev_removed_handler(struct usbh_context *const ctx)
8266
{
8367
struct usb_device *udev = NULL;
8468

8569
udev = usbh_device_get_root(ctx);
86-
8770
if (udev) {
88-
usbh_class_remove_all(udev);
89-
usbh_device_free(udev);
90-
LOG_DBG("Device removed");
71+
usbh_disconnect_device(ctx, udev);
9172
} else {
9273
LOG_DBG("Spurious device removed event");
9374
}
94-
/* TODO: handle remove for all of classes for the unattached device */
9575
}
9676

9777
static int discard_ep_request(struct usbh_context *const ctx,

subsys/usb/host/usbh_device.c

Lines changed: 43 additions & 10 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;
@@ -505,16 +548,6 @@ int usbh_device_init(struct usb_device *const udev)
505548
return err;
506549
}
507550
}
508-
k_mutex_unlock(&uhs_ctx->mutex);
509-
510-
/* Only reset bus if this is the root device */
511-
if (device_count == 1U) {
512-
err = uhc_bus_reset(uhs_ctx->dev);
513-
if (err) {
514-
LOG_ERR("Failed to signal bus reset");
515-
return err;
516-
}
517-
}
518551

519552
/*
520553
* Limit mps0 to the minimum supported by full-speed devices until the

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)