Files
CherryUSB/class/hub/usbh_hub.c

619 lines
20 KiB
C
Raw Normal View History

/*
* Copyright (c) 2022, sakumisu
2022-02-08 11:44:46 +08:00
*
* SPDX-License-Identifier: Apache-2.0
2022-02-08 11:44:46 +08:00
*/
#include "usbh_hub.h"
#define DEV_FORMAT "/dev/hub%d"
2022-02-08 11:44:46 +08:00
#define DEBOUNCE_TIMEOUT 400
#define DEBOUNCE_TIME_STEP 25
#define DELAY_TIME_AFTER_RESET 200
2022-09-12 13:00:45 +08:00
#define EXTHUB_FIRST_INDEX 2
2022-09-11 23:03:23 +08:00
2022-02-08 11:44:46 +08:00
static uint32_t g_devinuse = 0;
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_hub_buf[32];
usb_slist_t hub_event_head = USB_SLIST_OBJECT_INIT(hub_event_head);
2022-02-08 11:44:46 +08:00
usb_slist_t hub_class_head = USB_SLIST_OBJECT_INIT(hub_class_head);
usb_osal_sem_t hub_event_wait;
usb_osal_thread_t hub_thread;
2022-02-08 11:44:46 +08:00
USB_NOCACHE_RAM_SECTION struct usbh_hub roothub;
struct usbh_hubport roothub_parent_port;
2022-02-08 11:44:46 +08:00
2022-09-11 23:03:23 +08:00
USB_NOCACHE_RAM_SECTION struct usbh_hub exthub[CONFIG_USBHOST_EXTHUB_NUM];
extern int usbh_hport_activate_ep0(struct usbh_hubport *hport);
extern int usbh_hport_deactivate_ep0(struct usbh_hubport *hport);
extern int usbh_enumerate(struct usbh_hubport *hport);
2022-02-08 11:44:46 +08:00
static const char *speed_table[] = { "error-speed", "low-speed", "full-speed", "high-speed" };
2022-02-08 11:44:46 +08:00
/****************************************************************************
* Name: usbh_hub_devno_alloc
*
* Description:
* Allocate a unique /dev/hub[n] minor number in the range 2-31.
*
****************************************************************************/
2022-09-11 23:03:23 +08:00
static int usbh_hub_devno_alloc(void)
2022-02-08 11:44:46 +08:00
{
int devno;
2022-09-11 23:03:23 +08:00
for (devno = EXTHUB_FIRST_INDEX; devno < 32; devno++) {
2022-02-08 11:44:46 +08:00
uint32_t bitno = 1 << devno;
if ((g_devinuse & bitno) == 0) {
g_devinuse |= bitno;
2022-09-11 23:03:23 +08:00
return devno;
2022-02-08 11:44:46 +08:00
}
}
return -EMFILE;
}
/****************************************************************************
* Name: usbh_hub_devno_free
*
* Description:
* Free a /dev/hub[n] minor number so that it can be used.
*
****************************************************************************/
static void usbh_hub_devno_free(struct usbh_hub *hub)
2022-02-08 11:44:46 +08:00
{
int devno = hub->index;
2022-02-08 11:44:46 +08:00
2022-09-11 23:03:23 +08:00
if (devno >= EXTHUB_FIRST_INDEX && devno < 32) {
2022-02-08 11:44:46 +08:00
g_devinuse &= ~(1 << devno);
}
}
static int _usbh_hub_get_hub_descriptor(struct usbh_hub *hub, uint8_t *buffer)
2022-02-08 11:44:46 +08:00
{
struct usb_setup_packet *setup;
int ret;
2022-02-08 11:44:46 +08:00
setup = &hub->parent->setup;
2022-02-08 11:44:46 +08:00
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
setup->wValue = HUB_DESCRIPTOR_TYPE_HUB << 8;
setup->wIndex = 0;
setup->wLength = USB_SIZEOF_HUB_DESC;
ret = usbh_control_transfer(hub->parent->ep0, setup, g_hub_buf);
if (ret < 0) {
return ret;
}
memcpy(buffer, g_hub_buf, USB_SIZEOF_HUB_DESC);
return ret;
2022-02-08 11:44:46 +08:00
}
static int _usbh_hub_get_status(struct usbh_hub *hub, uint8_t *buffer)
2022-02-08 11:44:46 +08:00
{
struct usb_setup_packet *setup;
int ret;
2022-02-08 11:44:46 +08:00
setup = &hub->parent->setup;
2022-02-08 11:44:46 +08:00
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
setup->bRequest = HUB_REQUEST_GET_STATUS;
setup->wValue = 0;
setup->wIndex = 0;
setup->wLength = 2;
ret = usbh_control_transfer(hub->parent->ep0, setup, g_hub_buf);
if (ret < 0) {
return ret;
}
memcpy(buffer, g_hub_buf, 2);
return ret;
2022-02-08 11:44:46 +08:00
}
static int _usbh_hub_get_portstatus(struct usbh_hub *hub, uint8_t port, struct hub_port_status *port_status)
2022-02-08 11:44:46 +08:00
{
struct usb_setup_packet *setup;
int ret;
2022-02-08 11:44:46 +08:00
setup = &hub->parent->setup;
2022-02-08 11:44:46 +08:00
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_GET_STATUS;
setup->wValue = 0;
setup->wIndex = port;
setup->wLength = 4;
ret = usbh_control_transfer(hub->parent->ep0, setup, g_hub_buf);
if (ret < 0) {
return ret;
}
memcpy(port_status, g_hub_buf, 4);
return ret;
2022-02-08 11:44:46 +08:00
}
static int _usbh_hub_set_feature(struct usbh_hub *hub, uint8_t port, uint8_t feature)
2022-02-08 11:44:46 +08:00
{
struct usb_setup_packet *setup;
setup = &hub->parent->setup;
2022-02-08 11:44:46 +08:00
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_SET_FEATURE;
setup->wValue = feature;
setup->wIndex = port;
setup->wLength = 0;
return usbh_control_transfer(hub->parent->ep0, setup, NULL);
2022-02-08 11:44:46 +08:00
}
static int _usbh_hub_clear_feature(struct usbh_hub *hub, uint8_t port, uint8_t feature)
2022-02-08 11:44:46 +08:00
{
struct usb_setup_packet *setup;
setup = &hub->parent->setup;
2022-02-08 11:44:46 +08:00
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_CLEAR_FEATURE;
setup->wValue = feature;
setup->wIndex = port;
setup->wLength = 0;
return usbh_control_transfer(hub->parent->ep0, setup, NULL);
2022-02-08 11:44:46 +08:00
}
static int parse_hub_descriptor(struct usb_hub_descriptor *desc, uint16_t length)
{
if (desc->bLength != USB_SIZEOF_HUB_DESC) {
USB_LOG_ERR("invalid device bLength 0x%02x\r\n", desc->bLength);
return -1;
} else if (desc->bDescriptorType != HUB_DESCRIPTOR_TYPE_HUB) {
USB_LOG_ERR("unexpected descriptor 0x%02x\r\n", desc->bDescriptorType);
return -2;
} else {
2022-09-11 23:03:23 +08:00
USB_LOG_RAW("Hub Descriptor:\r\n");
USB_LOG_RAW("bLength: 0x%02x \r\n", desc->bLength);
USB_LOG_RAW("bDescriptorType: 0x%02x \r\n", desc->bDescriptorType);
USB_LOG_RAW("bNbrPorts: 0x%02x \r\n", desc->bNbrPorts);
USB_LOG_RAW("wHubCharacteristics: 0x%04x \r\n", desc->wHubCharacteristics);
USB_LOG_RAW("bPwrOn2PwrGood: 0x%02x \r\n", desc->bPwrOn2PwrGood);
USB_LOG_RAW("bHubContrCurrent: 0x%02x \r\n", desc->bHubContrCurrent);
USB_LOG_RAW("DeviceRemovable: 0x%02x \r\n", desc->DeviceRemovable);
USB_LOG_RAW("PortPwrCtrlMask: 0x%02x \r\n", desc->PortPwrCtrlMask);
2022-02-08 11:44:46 +08:00
}
return 0;
}
static int usbh_hub_get_portstatus(struct usbh_hub *hub, uint8_t port, struct hub_port_status *port_status)
{
struct usb_setup_packet roothub_setup;
struct usb_setup_packet *setup;
if (hub->is_roothub) {
setup = &roothub_setup;
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_GET_STATUS;
setup->wValue = 0;
setup->wIndex = port;
setup->wLength = 4;
return usbh_roothub_control(&roothub_setup, (uint8_t *)port_status);
} else {
return _usbh_hub_get_portstatus(hub, port, port_status);
}
}
static int usbh_hub_set_feature(struct usbh_hub *hub, uint8_t port, uint8_t feature)
{
struct usb_setup_packet roothub_setup;
struct usb_setup_packet *setup;
if (hub->is_roothub) {
setup = &roothub_setup;
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_SET_FEATURE;
setup->wValue = feature;
setup->wIndex = port;
setup->wLength = 0;
return usbh_roothub_control(setup, NULL);
} else {
return _usbh_hub_set_feature(hub, port, feature);
}
}
static int usbh_hub_clear_feature(struct usbh_hub *hub, uint8_t port, uint8_t feature)
{
struct usb_setup_packet roothub_setup;
struct usb_setup_packet *setup;
if (hub->is_roothub) {
setup = &roothub_setup;
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_OTHER;
setup->bRequest = HUB_REQUEST_CLEAR_FEATURE;
setup->wValue = feature;
setup->wIndex = port;
setup->wLength = 0;
return usbh_roothub_control(setup, NULL);
} else {
return _usbh_hub_clear_feature(hub, port, feature);
}
}
static void usbh_hub_thread_wakeup(struct usbh_hub *hub)
{
usb_slist_add_tail(&hub_event_head, &hub->hub_event_list);
usb_osal_sem_give(hub_event_wait);
}
2022-09-09 22:18:02 +08:00
static void hub_int_complete_callback(void *arg, int nbytes)
{
struct usbh_hub *hub = (struct usbh_hub *)arg;
if (nbytes > 0) {
usbh_hub_thread_wakeup(hub);
usbh_submit_urb(&hub->inturb);
}
}
static int usbh_hub_connect(struct usbh_hubport *hport, uint8_t intf)
2022-02-08 11:44:46 +08:00
{
struct usbh_endpoint_cfg ep_cfg = { 0 };
struct usb_endpoint_descriptor *ep_desc;
struct hub_port_status port_status;
2022-02-08 11:44:46 +08:00
int ret;
2022-09-11 23:03:23 +08:00
int index;
2022-02-08 11:44:46 +08:00
2022-09-11 23:03:23 +08:00
index = usbh_hub_devno_alloc();
if (index > (CONFIG_USBHOST_EXTHUB_NUM + EXTHUB_FIRST_INDEX)) {
2022-02-08 11:44:46 +08:00
return -ENOMEM;
}
2022-09-11 23:03:23 +08:00
struct usbh_hub *hub = &exthub[index - EXTHUB_FIRST_INDEX];
memset(hub, 0, sizeof(struct usbh_hub));
hub->hub_addr = hport->dev_addr;
hub->parent = hport;
2022-09-11 23:03:23 +08:00
hub->index = index;
hport->config.intf[intf].priv = hub;
2022-02-08 11:44:46 +08:00
ret = _usbh_hub_get_hub_descriptor(hub, (uint8_t *)&hub->hub_desc);
if (ret < 0) {
2022-02-08 11:44:46 +08:00
return ret;
}
parse_hub_descriptor(&hub->hub_desc, USB_SIZEOF_HUB_DESC);
2022-02-08 11:44:46 +08:00
for (uint8_t port = 0; port < hub->hub_desc.bNbrPorts; port++) {
hub->child[port].port = port + 1;
hub->child[port].parent = hub;
2022-02-08 11:44:46 +08:00
}
ep_desc = &hport->config.intf[intf].ep[0].ep_desc;
ep_cfg.ep_addr = ep_desc->bEndpointAddress;
ep_cfg.ep_type = ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK;
ep_cfg.ep_mps = ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_MASK;
2022-02-08 11:44:46 +08:00
ep_cfg.ep_interval = ep_desc->bInterval;
ep_cfg.hport = hport;
if (ep_desc->bEndpointAddress & 0x80) {
usbh_pipe_alloc(&hub->intin, &ep_cfg);
USB_LOG_INFO("Ep=%02x Attr=%02u Mps=%d Interval=%02u\r\n",
ep_desc->bEndpointAddress,
ep_desc->bmAttributes,
ep_desc->wMaxPacketSize,
ep_desc->bInterval);
2022-02-08 11:44:46 +08:00
} else {
return -1;
}
for (uint8_t port = 0; port < hub->hub_desc.bNbrPorts; port++) {
2022-09-11 22:43:51 +08:00
ret = usbh_hub_set_feature(hub, port + 1, HUB_PORT_FEATURE_POWER);
2022-02-08 11:44:46 +08:00
if (ret < 0) {
return ret;
}
}
for (uint8_t port = 0; port < hub->hub_desc.bNbrPorts; port++) {
ret = usbh_hub_get_portstatus(hub, port + 1, &port_status);
2022-09-11 23:03:23 +08:00
USB_LOG_INFO("port %u, status:0x%02x, change:0x%02x\r\n", port + 1, port_status.wPortStatus, port_status.wPortChange);
2022-02-08 11:44:46 +08:00
if (ret < 0) {
return ret;
}
}
2022-09-09 22:18:02 +08:00
hub->connected = true;
snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, hub->index);
usbh_hub_register(hub);
USB_LOG_INFO("Register HUB Class:%s\r\n", hport->config.intf[intf].devname);
2022-02-08 11:44:46 +08:00
2022-09-09 22:18:02 +08:00
usbh_int_urb_fill(&hub->inturb, hub->intin, hub->int_buffer, 1, 0, hub_int_complete_callback, hub);
usbh_submit_urb(&hub->inturb);
2022-02-08 11:44:46 +08:00
return 0;
}
static int usbh_hub_disconnect(struct usbh_hubport *hport, uint8_t intf)
2022-02-08 11:44:46 +08:00
{
struct usbh_hubport *child;
int ret = 0;
struct usbh_hub *hub = (struct usbh_hub *)hport->config.intf[intf].priv;
2022-02-08 11:44:46 +08:00
if (hub) {
usbh_hub_devno_free(hub);
2022-02-08 11:44:46 +08:00
if (hub->intin) {
usbh_pipe_free(hub->intin);
2022-02-08 11:44:46 +08:00
}
for (uint8_t port = 0; port < hub->hub_desc.bNbrPorts; port++) {
child = &hub->child[port];
usbh_hport_deactivate_ep0(child);
2022-02-08 11:44:46 +08:00
for (uint8_t i = 0; i < child->config.config_desc.bNumInterfaces; i++) {
if (child->config.intf[i].class_driver && child->config.intf[i].class_driver->disconnect) {
ret = CLASS_DISCONNECT(child, i);
}
}
child->config.config_desc.bNumInterfaces = 0;
child->parent = NULL;
}
usbh_hub_unregister(hub);
memset(hub, 0, sizeof(struct usbh_hub));
2022-02-08 11:44:46 +08:00
if (hport->config.intf[intf].devname[0] != '\0')
USB_LOG_INFO("Unregister HUB Class:%s\r\n", hport->config.intf[intf].devname);
2022-02-08 11:44:46 +08:00
}
return ret;
}
static void usbh_roothub_register(void)
2022-02-08 11:44:46 +08:00
{
memset(&roothub, 0, sizeof(struct usbh_hub));
memset(&roothub_parent_port, 0, sizeof(struct usbh_hubport));
roothub_parent_port.port = 1;
roothub_parent_port.dev_addr = 1;
roothub.connected = true;
roothub.index = 1;
roothub.is_roothub = true;
roothub.parent = &roothub_parent_port;
roothub.hub_addr = roothub_parent_port.dev_addr;
roothub.hub_desc.bNbrPorts = CONFIG_USBHOST_RHPORTS;
usbh_hub_register(&roothub);
}
static void usbh_hub_events(struct usbh_hub *hub)
{
struct usbh_hubport *child;
struct hub_port_status port_status;
uint8_t portchange_index;
uint16_t portstatus;
uint16_t portchange;
2022-02-08 11:44:46 +08:00
uint16_t mask;
uint16_t feat;
uint8_t speed;
2022-02-08 11:44:46 +08:00
int ret;
if (!hub->connected) {
2022-02-08 11:44:46 +08:00
return;
}
for (uint8_t port = 0; port < hub->hub_desc.bNbrPorts; port++) {
portchange_index = hub->int_buffer[0];
USB_LOG_DBG("Port change:0x%02x\r\n", portchange_index);
2022-02-08 11:44:46 +08:00
if (!(portchange_index & (1 << (port + 1)))) {
2022-02-08 11:44:46 +08:00
continue;
}
portchange_index &= ~(1 << (port + 1));
USB_LOG_DBG("Port %d change\r\n", port + 1);
2022-02-08 11:44:46 +08:00
/* Read hub port status */
ret = usbh_hub_get_portstatus(hub, port + 1, &port_status);
2022-02-08 11:44:46 +08:00
if (ret < 0) {
USB_LOG_ERR("Failed to read port %u status, errorcode: %d\r\n", port + 1, ret);
2022-02-08 11:44:46 +08:00
continue;
}
portstatus = port_status.wPortStatus;
portchange = port_status.wPortChange;
USB_LOG_DBG("port %u, status:0x%02x, change:0x%02x\r\n", port + 1, portstatus, portchange);
2022-02-08 11:44:46 +08:00
/* First, clear all change bits */
mask = 1;
feat = HUB_PORT_FEATURE_C_CONNECTION;
while (portchange) {
if (portchange & mask) {
ret = usbh_hub_clear_feature(hub, port + 1, feat);
2022-02-08 11:44:46 +08:00
if (ret < 0) {
USB_LOG_ERR("Failed to clear port %u, change mask:%04x, errorcode:%d\r\n", port + 1, mask, ret);
continue;
2022-02-08 11:44:46 +08:00
}
portchange &= (~mask);
2022-02-08 11:44:46 +08:00
}
mask <<= 1;
feat++;
}
portchange = port_status.wPortChange;
2022-02-08 11:44:46 +08:00
/* Second, if port changes, debounces first */
if (portchange & HUB_PORT_STATUS_C_CONNECTION) {
uint16_t connection = 0;
uint16_t debouncestable = 0;
for (uint32_t debouncetime = 0; debouncetime < DEBOUNCE_TIMEOUT; debouncetime += DEBOUNCE_TIME_STEP) {
usb_osal_msleep(DEBOUNCE_TIME_STEP);
/* Read hub port status */
ret = usbh_hub_get_portstatus(hub, port + 1, &port_status);
if (ret < 0) {
USB_LOG_ERR("Failed to read port %u status, errorcode: %d\r\n", port + 1, ret);
continue;
}
portstatus = port_status.wPortStatus;
portchange = port_status.wPortChange;
USB_LOG_DBG("Port %u, status:0x%02x, change:0x%02x\r\n", port + 1, portstatus, portchange);
if ((portstatus & HUB_PORT_STATUS_CONNECTION) == connection) {
if (connection) {
if (++debouncestable == 4) {
break;
}
}
} else {
debouncestable = 0;
}
connection = portstatus & HUB_PORT_STATUS_CONNECTION;
if (portchange & HUB_PORT_STATUS_C_CONNECTION) {
usbh_hub_clear_feature(hub, port + 1, HUB_PORT_FEATURE_C_CONNECTION);
}
2022-02-08 11:44:46 +08:00
}
/* Last, check connect status */
if (portstatus & HUB_PORT_STATUS_CONNECTION) {
ret = usbh_hub_set_feature(hub, port + 1, HUB_PORT_FEATURE_RESET);
2022-02-08 11:44:46 +08:00
if (ret < 0) {
USB_LOG_ERR("Failed to reset port %u,errorcode:%d\r\n", port, ret);
2022-02-08 11:44:46 +08:00
continue;
}
usb_osal_msleep(DELAY_TIME_AFTER_RESET);
/* Read hub port status */
ret = usbh_hub_get_portstatus(hub, port + 1, &port_status);
2022-02-08 11:44:46 +08:00
if (ret < 0) {
USB_LOG_ERR("Failed to read port %u status, errorcode: %d\r\n", port + 1, ret);
2022-02-08 11:44:46 +08:00
continue;
}
portstatus = port_status.wPortStatus;
portchange = port_status.wPortChange;
if (!(portstatus & HUB_PORT_STATUS_RESET) && (portstatus & HUB_PORT_STATUS_ENABLE)) {
if (portchange & HUB_PORT_STATUS_C_RESET) {
ret = usbh_hub_clear_feature(hub, port + 1, HUB_PORT_FEATURE_C_RESET);
2022-02-08 11:44:46 +08:00
if (ret < 0) {
USB_LOG_ERR("Failed to clear port %u reset change, errorcode: %d\r\n", port, ret);
2022-02-08 11:44:46 +08:00
}
}
if (portstatus & HUB_PORT_STATUS_HIGH_SPEED) {
speed = USB_SPEED_HIGH;
} else if (portstatus & HUB_PORT_STATUS_LOW_SPEED) {
speed = USB_SPEED_LOW;
2022-02-08 11:44:46 +08:00
} else {
speed = USB_SPEED_FULL;
2022-02-08 11:44:46 +08:00
}
child = &hub->child[port];
memset(child, 0, sizeof(struct usbh_hubport));
child->parent = hub;
child->connected = true;
child->port = port + 1;
child->speed = speed;
2022-02-08 11:44:46 +08:00
USB_LOG_INFO("New %s device on Hub %u, Port %u connected\r\n", speed_table[speed], hub->index, port + 1);
/* Allocate ep info for control endpoint */
usbh_hport_activate_ep0(child);
if (usbh_enumerate(child) < 0) {
USB_LOG_ERR("Port %u enumerate fail\r\n", port + 1);
}
2022-02-08 11:44:46 +08:00
} else {
USB_LOG_ERR("Failed to enable port %u\r\n", port + 1);
2022-02-08 11:44:46 +08:00
continue;
}
} else {
child = &hub->child[port];
child->connected = false;
usbh_hport_deactivate_ep0(child);
for (uint8_t i = 0; i < child->config.config_desc.bNumInterfaces; i++) {
if (child->config.intf[i].class_driver && child->config.intf[i].class_driver->disconnect) {
CLASS_DISCONNECT(child, i);
}
}
2022-09-12 13:00:45 +08:00
USB_LOG_INFO("Device on Hub %u, Port %u disconnected\r\n", hub->index, port + 1);
usbh_device_unmount_done_callback(child);
memset(child, 0, sizeof(struct usbh_hubport));
2022-02-08 11:44:46 +08:00
}
}
}
}
2022-02-08 11:44:46 +08:00
static void usbh_hub_thread(void *argument)
{
size_t flags;
int ret = 0;
2022-02-08 11:44:46 +08:00
usbh_roothub_register();
usb_hc_init();
while (1) {
ret = usb_osal_sem_take(hub_event_wait, 0xffffffff);
if (ret < 0) {
continue;
}
2022-03-17 16:40:54 +08:00
while (!usb_slist_isempty(&hub_event_head)) {
struct usbh_hub *hub = usb_slist_first_entry(&hub_event_head, struct usbh_hub, hub_event_list);
flags = usb_osal_enter_critical_section();
usb_slist_remove(&hub_event_head, &hub->hub_event_list);
usb_osal_leave_critical_section(flags);
usbh_hub_events(hub);
}
2022-02-08 11:44:46 +08:00
}
}
void usbh_roothub_thread_wakeup(uint8_t port)
2022-02-08 11:44:46 +08:00
{
roothub.int_buffer[0] |= (1 << port);
usbh_hub_thread_wakeup(&roothub);
}
void usbh_hub_register(struct usbh_hub *hub)
{
usb_slist_add_tail(&hub_class_head, &hub->list);
}
void usbh_hub_unregister(struct usbh_hub *hub)
{
usb_slist_remove(&hub_class_head, &hub->list);
}
int usbh_hub_initialize(void)
{
hub_event_wait = usb_osal_sem_create(0);
if (hub_event_wait == NULL) {
return -1;
2022-02-08 11:44:46 +08:00
}
hub_thread = usb_osal_thread_create("usbh_hub", CONFIG_USBHOST_PSC_STACKSIZE, CONFIG_USBHOST_PSC_PRIO, usbh_hub_thread, NULL);
if (hub_thread == NULL) {
return -1;
2022-02-08 11:44:46 +08:00
}
return 0;
2022-02-08 11:44:46 +08:00
}
const struct usbh_class_driver hub_driver = {
2022-02-08 11:44:46 +08:00
.driver_name = "hub",
.connect = usbh_hub_connect,
.disconnect = usbh_hub_disconnect
};
CLASS_INFO_DEFINE const struct usbh_class_info hub_info = {
.match_flags = USB_CLASS_MATCH_INTF_CLASS,
.class = USB_DEVICE_CLASS_HUB,
.subclass = 0,
.protocol = 0,
.vid = 0x00,
.pid = 0x00,
.class_driver = &hub_driver
2022-03-27 14:38:47 +08:00
};