2022-11-10 22:22:48 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright : (C) 2022 Phytium Information Technology, Inc.
|
|
|
|
|
* All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* This program is OPEN SOURCE software: you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
|
|
|
|
|
* either version 1.0 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
|
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
* See the Phytium Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* FilePath: fusb_generic_hub.c
|
|
|
|
|
* Date: 2022-02-11 13:33:11
|
|
|
|
|
* LastEditTime: 2022-02-18 09:19:27
|
|
|
|
|
* Description: This files is for implmentation of generic hub function
|
|
|
|
|
*
|
|
|
|
|
* Modify History:
|
|
|
|
|
* Ver Who Date Changes
|
|
|
|
|
* ----- ------ -------- --------------------------------------
|
2023-05-11 10:25:21 +08:00
|
|
|
|
* 1.0 zhugengyu 2022/2/7 init commit
|
2022-11-10 22:22:48 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "fsleep.h"
|
|
|
|
|
#include "fdebug.h"
|
|
|
|
|
#include "fusb.h"
|
|
|
|
|
#include "fusb_generic_hub.h"
|
|
|
|
|
|
|
|
|
|
#define FUSB_DEBUG_TAG "FUSB_HUB"
|
|
|
|
|
#define FUSB_ERROR(format, ...) FT_DEBUG_PRINT_E(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
|
|
|
|
|
#define FUSB_WARN(format, ...) FT_DEBUG_PRINT_W(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
|
|
|
|
|
#define FUSB_INFO(format, ...) FT_DEBUG_PRINT_I(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
|
|
|
|
|
#define FUSB_DEBUG(format, ...) FT_DEBUG_PRINT_D(FUSB_DEBUG_TAG, format, ##__VA_ARGS__)
|
|
|
|
|
|
|
|
|
|
void FUsbGenericHubDestory(FUsbDev *const dev)
|
|
|
|
|
{
|
|
|
|
|
FUsbGenericHub *const hub = FUSB_GEN_HUB_GET(dev);
|
|
|
|
|
FUsb *instace = dev->controller->usb;
|
|
|
|
|
if (!hub)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return;
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
|
|
|
|
|
/* First, detach all devices behind this hub */
|
|
|
|
|
int port;
|
|
|
|
|
for (port = 1; port <= hub->num_ports; ++port)
|
|
|
|
|
{
|
|
|
|
|
if (hub->ports[port] >= 0)
|
|
|
|
|
{
|
2023-05-11 10:25:21 +08:00
|
|
|
|
FUSB_INFO("Generic hub: Detachment at port %d ", port);
|
2022-11-10 22:22:48 +08:00
|
|
|
|
FUsbDetachDev(dev->controller, hub->ports[port]);
|
|
|
|
|
hub->ports[port] = FUSB_NO_DEV_ADDR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Disable all ports */
|
|
|
|
|
if (hub->ops->disable_port)
|
|
|
|
|
{
|
|
|
|
|
for (port = 1; port <= hub->num_ports; ++port)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
hub->ops->disable_port(dev, port);
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FUSB_FREE(instace, hub->ports); // free(hub->ports);
|
|
|
|
|
FUSB_FREE(instace, hub); // free(hub);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int FUsbGenericHubDebounce(FUsbDev *const dev, const int port)
|
|
|
|
|
{
|
|
|
|
|
FUsbGenericHub *const hub = FUSB_GEN_HUB_GET(dev);
|
|
|
|
|
|
|
|
|
|
const int step_ms = 1; /* linux uses 25ms, we're busy anyway */
|
|
|
|
|
const int at_least_ms = 100; /* 100ms as in usb20 spec 9.1.2 */
|
|
|
|
|
const int timeout_ms = 1500; /* linux uses this value */
|
|
|
|
|
|
|
|
|
|
int total_ms = 0;
|
|
|
|
|
int stable_ms = 0;
|
|
|
|
|
while (stable_ms < at_least_ms && total_ms < timeout_ms)
|
|
|
|
|
{
|
|
|
|
|
fsleep_millisec(step_ms);
|
|
|
|
|
|
|
|
|
|
const int changed = hub->ops->port_status_changed(dev, port);
|
|
|
|
|
const int connected = hub->ops->port_connected(dev, port);
|
|
|
|
|
if (changed < 0 || connected < 0)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return -1;
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
|
|
|
|
|
if (!changed && connected)
|
|
|
|
|
{
|
|
|
|
|
stable_ms += step_ms;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-05-11 10:25:21 +08:00
|
|
|
|
FUSB_INFO("Generic hub: Unstable connection at %d ",
|
2022-11-10 22:22:48 +08:00
|
|
|
|
port);
|
|
|
|
|
stable_ms = 0;
|
|
|
|
|
}
|
|
|
|
|
total_ms += step_ms;
|
|
|
|
|
}
|
|
|
|
|
if (total_ms >= timeout_ms)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
FUSB_INFO("Generic hub: Debouncing timed out at %d ", port);
|
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return 0; /* ignore timeouts, try to always go on */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FUsbGenericHubWaitForPort(FUsbDev *const dev, const int port,
|
|
|
|
|
const int wait_for,
|
|
|
|
|
int (*const port_op)(FUsbDev *, int),
|
|
|
|
|
int timeout_steps, const int step_us)
|
|
|
|
|
{
|
|
|
|
|
int state;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
state = port_op(dev, port);
|
|
|
|
|
if (state < 0)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return -1;
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
else if (!!state == wait_for)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return timeout_steps;
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
fsleep_microsec(step_us);
|
|
|
|
|
--timeout_steps;
|
|
|
|
|
}
|
|
|
|
|
while (timeout_steps);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FUsbGenericHubResetPort(FUsbDev *const dev, const int port)
|
|
|
|
|
{
|
|
|
|
|
FUsbGenericHub *const hub = FUSB_GEN_HUB_GET(dev);
|
|
|
|
|
|
|
|
|
|
if (hub->ops->start_port_reset(dev, port) < 0)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return -1;
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
|
|
|
|
|
/* wait for 10ms (usb20 spec 11.5.1.5: reset should take 10 to 20ms) */
|
|
|
|
|
fsleep_millisec(10);
|
|
|
|
|
|
|
|
|
|
/* now wait 12ms for the hub to finish the reset */
|
|
|
|
|
const int ret = FUsbGenericHubWaitForPort(
|
|
|
|
|
/* time out after 120 * 100us = 12ms */
|
|
|
|
|
dev, port, 0, hub->ops->port_in_reset, 120, 100);
|
|
|
|
|
if (ret < 0)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return -1;
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
else if (!ret)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
FUSB_INFO("Generic hub: Reset timed out at port %d ", port);
|
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
|
|
|
|
|
return 0; /* ignore timeouts, try to always go on */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int FUsbGenericHubDetachDev(FUsbDev *const dev, const int port)
|
|
|
|
|
{
|
|
|
|
|
FUsbGenericHub *const hub = FUSB_GEN_HUB_GET(dev);
|
|
|
|
|
|
|
|
|
|
FUsbDetachDev(dev->controller, hub->ports[port]);
|
|
|
|
|
hub->ports[port] = FUSB_NO_DEV_ADDR;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int FUsbGenericHubAttachDev(FUsbDev *const dev, const int port)
|
|
|
|
|
{
|
|
|
|
|
FUsbGenericHub *const hub = FUSB_GEN_HUB_GET(dev);
|
|
|
|
|
|
|
|
|
|
if (FUsbGenericHubDebounce(dev, port) < 0)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return -1;
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
|
|
|
|
|
if (hub->ops->reset_port)
|
|
|
|
|
{
|
|
|
|
|
if (hub->ops->reset_port(dev, port) < 0)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return -1;
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
|
|
|
|
|
if (!hub->ops->port_connected(dev, port))
|
|
|
|
|
{
|
|
|
|
|
FUSB_INFO(
|
2023-05-11 10:25:21 +08:00
|
|
|
|
"Generic hub: Port %d disconnected after "
|
2022-11-10 22:22:48 +08:00
|
|
|
|
"reset. Possibly upgraded, rescan required. ",
|
|
|
|
|
port);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* after reset the port will be enabled automatically */
|
|
|
|
|
const int ret = FUsbGenericHubWaitForPort(
|
|
|
|
|
/* time out after 1,000 * 10us = 10ms */
|
|
|
|
|
dev, port, 1, hub->ops->port_enabled, 1000, 10);
|
|
|
|
|
if (ret < 0)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return -1;
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
else if (!ret)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
FUSB_INFO("Generic hub: Port %d still "
|
2022-11-10 22:22:48 +08:00
|
|
|
|
"disabled after 10ms ",
|
|
|
|
|
port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const FUsbSpeed speed = hub->ops->port_speed(dev, port);
|
|
|
|
|
if (speed >= 0)
|
|
|
|
|
{
|
2023-05-11 10:25:21 +08:00
|
|
|
|
FUSB_DEBUG("Generic hub: Success at port %d ", port);
|
2022-11-10 22:22:48 +08:00
|
|
|
|
if (hub->ops->reset_port)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
fsleep_millisec(10);
|
|
|
|
|
} /* Reset recovery time
|
2022-11-10 22:22:48 +08:00
|
|
|
|
(usb20 spec 7.1.7.5) */
|
|
|
|
|
hub->ports[port] = FUsbAttachDev(
|
|
|
|
|
dev->controller, dev->address, port, speed);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FUsbGenericHubScanPort(FUsbDev *const dev, const int port)
|
|
|
|
|
{
|
|
|
|
|
FUsbGenericHub *const hub = FUSB_GEN_HUB_GET(dev);
|
|
|
|
|
|
|
|
|
|
if (hub->ports[port] >= 0)
|
|
|
|
|
{
|
2023-05-11 10:25:21 +08:00
|
|
|
|
FUSB_INFO("Generic hub: Detachment at port %d ", port);
|
2022-11-10 22:22:48 +08:00
|
|
|
|
const int ret = FUsbGenericHubDetachDev(dev, port);
|
|
|
|
|
if (ret < 0)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return ret;
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hub->ops->port_connected(dev, port))
|
|
|
|
|
{
|
2023-05-11 10:25:21 +08:00
|
|
|
|
FUSB_INFO("Generic hub: Attachment at port %d ", port);
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return FUsbGenericHubAttachDev(dev, port);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void FUsbGenericHubPoll(FUsbDev *const dev)
|
|
|
|
|
{
|
|
|
|
|
FUsbGenericHub *const hub = FUSB_GEN_HUB_GET(dev);
|
|
|
|
|
if (!hub)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return;
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
|
|
|
|
|
if (hub->ops->hub_status_changed &&
|
2023-05-11 10:25:21 +08:00
|
|
|
|
hub->ops->hub_status_changed(dev) != FUSB_CC_SUCCESS)
|
2022-11-10 22:22:48 +08:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int port;
|
|
|
|
|
for (port = 1; port <= hub->num_ports; ++port)
|
|
|
|
|
{
|
|
|
|
|
const FUsbTransCode ret = hub->ops->port_status_changed(dev, port);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
FUSB_WARN("Transcode %d", ret);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (ret == FUSB_CC_SUCCESS)
|
|
|
|
|
{
|
2023-05-11 10:25:21 +08:00
|
|
|
|
FUSB_INFO("Generic hub: Port change at %d ", port);
|
2022-11-10 22:22:48 +08:00
|
|
|
|
if (FUsbGenericHubScanPort(dev, port) < 0)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return;
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FUsbGenericHubInit(FUsbDev *const dev, const int num_ports,
|
|
|
|
|
const FUsbGenericHubOps *const ops)
|
|
|
|
|
{
|
|
|
|
|
int port;
|
|
|
|
|
FUsb *instance = dev->controller->usb;
|
|
|
|
|
|
|
|
|
|
dev->destroy = FUsbGenericHubDestory;
|
|
|
|
|
dev->poll = FUsbGenericHubPoll;
|
|
|
|
|
FASSERT(NULL == dev->data);
|
|
|
|
|
dev->data = FUSB_ALLOCATE(instance, sizeof(FUsbGenericHub), FUSB_DEFAULT_ALIGN);
|
|
|
|
|
if (NULL == dev->data)
|
|
|
|
|
{
|
2023-05-11 10:25:21 +08:00
|
|
|
|
FUSB_ERROR("Generic hub: Out of memory ");
|
2022-11-10 22:22:48 +08:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FUsbGenericHub *const hub = FUSB_GEN_HUB_GET(dev);
|
|
|
|
|
hub->num_ports = num_ports;
|
|
|
|
|
FASSERT(NULL == hub->ports);
|
|
|
|
|
hub->ports = FUSB_ALLOCATE(instance, sizeof(*hub->ports) * (num_ports + 1), FUSB_DEFAULT_ALIGN);
|
|
|
|
|
hub->ops = ops;
|
|
|
|
|
if (NULL == hub->ports)
|
|
|
|
|
{
|
2023-05-11 10:25:21 +08:00
|
|
|
|
FUSB_ERROR("Generic hub: Out of memory ");
|
2022-11-10 22:22:48 +08:00
|
|
|
|
FUSB_FREE(instance, dev->data);
|
|
|
|
|
dev->data = NULL;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (port = 1; port <= num_ports; ++port)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
hub->ports[port] = FUSB_NO_DEV_ADDR;
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
|
|
|
|
|
/* Enable all ports */
|
|
|
|
|
if (ops->enable_port)
|
|
|
|
|
{
|
|
|
|
|
for (port = 1; port <= num_ports; ++port)
|
2023-05-11 10:25:21 +08:00
|
|
|
|
{
|
2022-11-10 22:22:48 +08:00
|
|
|
|
ops->enable_port(dev, port);
|
2023-05-11 10:25:21 +08:00
|
|
|
|
}
|
2022-11-10 22:22:48 +08:00
|
|
|
|
|
|
|
|
|
/* wait once for all ports */
|
|
|
|
|
fsleep_millisec(20);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|