Skip to content

Commit 05415e1

Browse files
AidenHuJosuah Demangeon
authored andcommitted
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 bbe0d06 commit 05415e1

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

subsys/usb/host/usbh_core.c

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

1414
#include "usbh_class.h"
15-
#include "usbh_class_api.h"
1615
#include "usbh_device.h"
1716
#include "usbh_internal.h"
1817

@@ -49,49 +48,30 @@ 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "usbh_device.h"
1010
#include "usbh_ch9.h"
11+
#include "usbh_class.h"
12+
#include "usbh_class_api.h"
1113

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

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

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

0 commit comments

Comments
 (0)