feat: host add event callback mechanism similar to device

* fix: fix warning for speed_table
* fix(port/dwc2/usb_hc_dwc2): add roothub.speed init
* feat(usbh_core): add event_callback
* fix(usbh_hub): fix event device reset port
* fix(usbh_hub): remove event init when init failed
* feat(usbh_core): add default dummy_event_callback
* fix(usbh_hub): emit reset event only on successful reset
* fix(usbh_core): emit interface start only on successful connect class driver
* feat(usbh_core): change event_callback to typedef
* feat(port): update port usbh init params
* doc: update usbh_initialize desc
* fix(usbh_core): check result from ret == 0 change to ret >= 0
---------

Signed-off-by: egahp <2687434412@qq.com>
This commit is contained in:
Egahp
2025-08-30 19:24:02 +08:00
committed by GitHub
parent 257b1d4d20
commit 5d5b61a606
11 changed files with 95 additions and 17 deletions

View File

@@ -31,6 +31,15 @@ struct usbh_bus g_usbhost_bus[CONFIG_USBHOST_MAX_BUS];
#define USB_DEV_ADDR_MARK_OFFSET 5
#define USB_DEV_ADDR_MARK_MASK 0x1f
static void dummy_event_handler(uint8_t busid, uint8_t hub_index, uint8_t hub_port, uint8_t intf, uint8_t event)
{
(void)busid;
(void)hub_index;
(void)hub_port;
(void)intf;
(void)event;
}
static int usbh_allocate_devaddr(struct usbh_devaddr_map *devgen)
{
uint8_t lastaddr = devgen->last;
@@ -593,6 +602,7 @@ int usbh_enumerate(struct usbh_hubport *hport)
}
#endif
USB_LOG_INFO("Enumeration success, start loading class driver\r\n");
hport->bus->event_handler(hport->bus->busid, hport->parent->index, hport->port, USB_INTERFACE_ANY, USBH_EVENT_DEVICE_CONFIGURED);
/*search supported class driver*/
for (uint8_t i = 0; i < hport->config.config_desc.bNumInterfaces; i++) {
intf_desc = &hport->config.intf[i].altsetting[0].intf_desc;
@@ -604,12 +614,15 @@ int usbh_enumerate(struct usbh_hubport *hport)
intf_desc->bInterfaceClass,
intf_desc->bInterfaceSubClass,
intf_desc->bInterfaceProtocol);
hport->bus->event_handler(hport->bus->busid, hport->parent->index, hport->port, i, USBH_EVENT_INTERFACE_UNSUPPORTED);
continue;
}
hport->config.intf[i].class_driver = class_driver;
USB_LOG_INFO("Loading %s class driver\r\n", class_driver->driver_name);
ret = CLASS_CONNECT(hport, i);
if (ret >= 0) {
hport->bus->event_handler(hport->bus->busid, hport->parent->index, hport->port, i, USBH_EVENT_INTERFACE_START);
}
}
errout:
@@ -629,12 +642,15 @@ void usbh_hubport_release(struct usbh_hubport *hport)
if (hport->config.intf[i].class_driver && hport->config.intf[i].class_driver->disconnect) {
CLASS_DISCONNECT(hport, i);
}
hport->bus->event_handler(hport->bus->busid, hport->parent->index, hport->port, i, USBH_EVENT_INTERFACE_STOP);
}
hport->config.config_desc.bNumInterfaces = 0;
usbh_kill_urb(&hport->ep0_urb);
if (hport->mutex) {
usb_osal_mutex_delete(hport->mutex);
}
USB_LOG_INFO("Device on Bus %u, Hub %u, Port %u disconnected\r\n", hport->bus->busid, hport->parent->index, hport->port);
hport->bus->event_handler(hport->bus->busid, hport->parent->index, hport->port, USB_INTERFACE_ANY, USBH_EVENT_DEVICE_DISCONNECTED);
}
}
@@ -651,7 +667,7 @@ static void usbh_bus_init(struct usbh_bus *bus, uint8_t busid, uintptr_t reg_bas
usb_slist_add_tail(&g_bus_head, &bus->list);
}
int usbh_initialize(uint8_t busid, uintptr_t reg_base)
int usbh_initialize(uint8_t busid, uintptr_t reg_base, usbh_event_handler_t event_handler)
{
struct usbh_bus *bus;
@@ -665,6 +681,12 @@ int usbh_initialize(uint8_t busid, uintptr_t reg_base)
usbh_bus_init(bus, busid, reg_base);
if (event_handler) {
bus->event_handler = event_handler;
} else {
bus->event_handler = dummy_event_handler;
}
#ifdef __ARMCC_VERSION /* ARM C Compiler */
extern const int usbh_class_info$$Base;
extern const int usbh_class_info$$Limit;
@@ -695,6 +717,8 @@ int usbh_deinitialize(uint8_t busid)
bus = &g_usbhost_bus[busid];
bus->event_handler(bus->busid, USB_HUB_INDEX_ANY, USB_HUB_PORT_ANY, USB_INTERFACE_ANY, USBH_EVENT_DEINIT);
usbh_hub_deinitialize(bus);
usb_slist_remove(&g_bus_head, &bus->list);

View File

@@ -28,6 +28,35 @@
extern "C" {
#endif
enum usbh_event_type {
/* USB HCD IRQ */
USBH_EVENT_ERROR,
USBH_EVENT_SOF,
/* USB DEVICE STATUS */
USBH_EVENT_DEVICE_RESET,
USBH_EVENT_DEVICE_CONNECTED,
USBH_EVENT_DEVICE_DISCONNECTED,
USBH_EVENT_DEVICE_CONFIGURED,
USBH_EVENT_DEVICE_WAKEUP,
USBH_EVENT_DEVICE_SUSPEND,
USBH_EVENT_DEVICE_RESUME,
/* USB DEVICE INTERFACE STATUS */
USBH_EVENT_INTERFACE_UNSUPPORTED,
USBH_EVENT_INTERFACE_START,
USBH_EVENT_INTERFACE_STOP,
/* USB FRAMEWORK STATUS */
USBH_EVENT_INIT,
USBH_EVENT_DEINIT,
USBH_EVENT_UNKNOWN,
};
#define USB_HUB_PORT_ANY 0
#define USB_HUB_INDEX_ANY 0
#define USB_INTERFACE_ANY 0xff
#define USB_CLASS_MATCH_VENDOR 0x0001
#define USB_CLASS_MATCH_PRODUCT 0x0002
#define USB_CLASS_MATCH_INTF_CLASS 0x0004
@@ -60,6 +89,8 @@ extern "C" {
USB_GET_MULT(ep_desc->wMaxPacketSize)); \
} while (0)
typedef void (*usbh_event_handler_t)(uint8_t busid, uint8_t hub_index, uint8_t hub_port, uint8_t intf, uint8_t event);
struct usbh_class_info {
uint8_t match_flags; /* Used for product specific matches; range is inclusive */
uint8_t bInterfaceClass; /* Base device class code */
@@ -131,7 +162,7 @@ struct usbh_hub {
uint8_t powerdelay;
uint8_t tt_think;
bool ismtt;
struct usb_hub_descriptor hub_desc; /* USB 2.0 only */
struct usb_hub_descriptor hub_desc; /* USB 2.0 only */
struct usb_hub_ss_descriptor hub_ss_desc; /* USB 3.0 only */
struct usbh_hubport child[CONFIG_USBHOST_MAX_EHPORTS];
struct usbh_hubport *parent;
@@ -168,6 +199,8 @@ struct usbh_bus {
struct usbh_devaddr_map devgen;
usb_osal_thread_t hub_thread;
usb_osal_mq_t hub_mq;
void (*event_handler)(uint8_t busid, uint8_t hub_index, uint8_t hub_port, uint8_t intf, uint8_t event);
};
static inline void usbh_control_urb_fill(struct usbh_urb *urb,
@@ -274,7 +307,7 @@ int usbh_get_string_desc(struct usbh_hubport *hport, uint8_t index, uint8_t *out
*/
int usbh_set_interface(struct usbh_hubport *hport, uint8_t intf, uint8_t altsetting);
int usbh_initialize(uint8_t busid, uintptr_t reg_base);
int usbh_initialize(uint8_t busid, uintptr_t reg_base, usbh_event_handler_t event_handler);
int usbh_deinitialize(uint8_t busid);
void *usbh_find_class_instance(const char *devname);
struct usbh_hubport *usbh_find_hubport(uint8_t busid, uint8_t hub_index, uint8_t hub_port);