#include "usbhub.h"
bool USBHub::bResetInitiated = false;
USBHub::USBHub(USB *p) :
pUsb(p),
bAddress(0),
bNbrPorts(0),
qNextPollTime(0),
bPollEnable(false) {
epInfo[0].epAddr = 0;
epInfo[0].maxPktSize = 8;
epInfo[0].epAttribs = 0;
epInfo[0].bmNakPower = USB_NAK_MAX_POWER;
epInfo[1].epAddr = 1;
epInfo[1].maxPktSize = 8; epInfo[1].epAttribs = 0;
epInfo[1].bmNakPower = USB_NAK_NOWAIT;
if(pUsb)
pUsb->RegisterDeviceClass(this);
}
uint8_t USBHub::Init(uint8_t parent, uint8_t port, bool lowspeed) {
uint8_t buf[32];
USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
HubDescriptor* hd = reinterpret_cast<HubDescriptor*>(buf);
USB_CONFIGURATION_DESCRIPTOR * ucd = reinterpret_cast<USB_CONFIGURATION_DESCRIPTOR*>(buf);
uint8_t rcode;
UsbDevice *p = NULL;
EpInfo *oldep_ptr = NULL;
uint8_t len = 0;
uint16_t cd_len = 0;
AddressPool &addrPool = pUsb->GetAddressPool();
if(bAddress)
return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
p = addrPool.GetUsbDevicePtr(0);
if(!p)
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
if(!p->epinfo)
return USB_ERROR_EPINFO_IS_NULL;
oldep_ptr = p->epinfo;
p->epinfo = epInfo;
p->lowspeed = lowspeed;
rcode = pUsb->getDevDescr(0, 0, 8, (uint8_t*)buf);
p->lowspeed = false;
if(!rcode)
len = (buf[0] > 32) ? 32 : buf[0];
if(rcode) {
p->epinfo = oldep_ptr;
return rcode;
}
if(udd->bDeviceClass != 0x09)
return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
bAddress = addrPool.AllocAddress(parent, (udd->bDeviceClass == 0x09) ? true : false, port);
if(!bAddress)
return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
epInfo[0].maxPktSize = udd->bMaxPacketSize0;
rcode = pUsb->setAddr(0, 0, bAddress);
if(rcode) {
p->epinfo = oldep_ptr;
addrPool.FreeAddress(bAddress);
bAddress = 0;
return rcode;
}
p->epinfo = oldep_ptr;
if(len)
rcode = pUsb->getDevDescr(bAddress, 0, len, (uint8_t*)buf);
if(rcode)
goto FailGetDevDescr;
rcode = pUsb->setEpInfoEntry(bAddress, 2, epInfo);
if(rcode)
goto FailSetDevTblEntry;
rcode = GetHubDescriptor(0, 8, buf);
if(rcode)
goto FailGetHubDescr;
bNbrPorts = hd->bNbrPorts;
rcode = pUsb->getConfDescr(bAddress, 0, 8, 0, buf);
if(!rcode) {
cd_len = ucd->wTotalLength;
rcode = pUsb->getConfDescr(bAddress, 0, cd_len, 0, buf);
}
if(rcode)
goto FailGetConfDescr;
{
uint8_t buf2[24];
rcode = pUsb->getConfDescr(bAddress, 0, buf[0], 0, buf2);
if(rcode)
goto FailGetConfDescr;
}
rcode = pUsb->setConf(bAddress, 0, buf[5]);
if(rcode)
goto FailSetConfDescr;
for(uint8_t j = 1; j <= bNbrPorts; j++)
SetPortFeature(HUB_FEATURE_PORT_POWER, j, 0);
pUsb->SetHubPreMask();
bPollEnable = true;
return 0;
FailGetDevDescr:
goto Fail;
FailSetDevTblEntry:
goto Fail;
FailGetHubDescr:
goto Fail;
FailGetConfDescr:
goto Fail;
FailSetConfDescr:
goto Fail;
Fail:
USBTRACE("...FAIL\r\n");
return rcode;
}
uint8_t USBHub::Release() {
pUsb->GetAddressPool().FreeAddress(bAddress);
if(bAddress == 0x41)
pUsb->SetHubPreMask();
bAddress = 0;
bNbrPorts = 0;
qNextPollTime = 0;
bPollEnable = false;
return 0;
}
uint8_t USBHub::Poll() {
uint8_t rcode = 0;
if(!bPollEnable)
return 0;
if(((long)(millis() - qNextPollTime) >= 0L)) {
rcode = CheckHubStatus();
qNextPollTime = millis() + 100;
}
return rcode;
}
uint8_t USBHub::CheckHubStatus() {
uint8_t rcode;
uint8_t buf[8];
uint16_t read = 1;
rcode = pUsb->inTransfer(bAddress, 1, &read, buf);
if(rcode)
return rcode;
for(uint8_t port = 1, mask = 0x02; port < 8; mask <<= 1, port++) {
if(buf[0] & mask) {
HubEvent evt;
evt.bmEvent = 0;
rcode = GetPortStatus(port, 4, evt.evtBuff);
if(rcode)
continue;
rcode = PortStatusChange(port, evt);
if(rcode == HUB_ERROR_PORT_HAS_BEEN_RESET)
return 0;
if(rcode)
return rcode;
}
}
for(uint8_t port = 1; port <= bNbrPorts; port++) {
HubEvent evt;
evt.bmEvent = 0;
rcode = GetPortStatus(port, 4, evt.evtBuff);
if(rcode)
continue;
if((evt.bmStatus & bmHUB_PORT_STATE_CHECK_DISABLED) != bmHUB_PORT_STATE_DISABLED)
continue;
evt.bmChange |= bmHUB_PORT_STATUS_C_PORT_CONNECTION;
rcode = PortStatusChange(port, evt);
if(rcode == HUB_ERROR_PORT_HAS_BEEN_RESET)
return 0;
if(rcode)
return rcode;
} return 0;
}
void USBHub::ResetHubPort(uint8_t port) {
HubEvent evt;
evt.bmEvent = 0;
uint8_t rcode;
ClearPortFeature(HUB_FEATURE_C_PORT_ENABLE, port, 0);
ClearPortFeature(HUB_FEATURE_C_PORT_CONNECTION, port, 0);
SetPortFeature(HUB_FEATURE_PORT_RESET, port, 0);
for(int i = 0; i < 3; i++) {
rcode = GetPortStatus(port, 4, evt.evtBuff);
if(rcode) break; if(evt.bmEvent == bmHUB_PORT_EVENT_RESET_COMPLETE || evt.bmEvent == bmHUB_PORT_EVENT_LS_RESET_COMPLETE) {
break;
}
delay(100); }
ClearPortFeature(HUB_FEATURE_C_PORT_RESET, port, 0);
ClearPortFeature(HUB_FEATURE_C_PORT_CONNECTION, port, 0);
delay(20);
}
uint8_t USBHub::PortStatusChange(uint8_t port, HubEvent &evt) {
switch(evt.bmEvent) {
case bmHUB_PORT_EVENT_CONNECT:
case bmHUB_PORT_EVENT_LS_CONNECT:
if(bResetInitiated)
return 0;
ClearPortFeature(HUB_FEATURE_C_PORT_ENABLE, port, 0);
ClearPortFeature(HUB_FEATURE_C_PORT_CONNECTION, port, 0);
SetPortFeature(HUB_FEATURE_PORT_RESET, port, 0);
bResetInitiated = true;
return HUB_ERROR_PORT_HAS_BEEN_RESET;
case bmHUB_PORT_EVENT_DISCONNECT:
ClearPortFeature(HUB_FEATURE_C_PORT_ENABLE, port, 0);
ClearPortFeature(HUB_FEATURE_C_PORT_CONNECTION, port, 0);
bResetInitiated = false;
UsbDeviceAddress a;
a.devAddress = 0;
a.bmHub = 0;
a.bmParent = bAddress;
a.bmAddress = port;
pUsb->ReleaseDevice(a.devAddress);
return 0;
case bmHUB_PORT_EVENT_RESET_COMPLETE:
case bmHUB_PORT_EVENT_LS_RESET_COMPLETE:
ClearPortFeature(HUB_FEATURE_C_PORT_RESET, port, 0);
ClearPortFeature(HUB_FEATURE_C_PORT_CONNECTION, port, 0);
delay(20);
a.devAddress = bAddress;
pUsb->Configuring(a.bmAddress, port, (evt.bmStatus & bmHUB_PORT_STATUS_PORT_LOW_SPEED));
bResetInitiated = false;
break;
} return 0;
}
void PrintHubPortStatus(USBHub *hubptr, uint8_t addr, uint8_t port, bool print_changes) {
uint8_t rcode = 0;
HubEvent evt;
rcode = hubptr->GetPortStatus(port, 4, evt.evtBuff);
if(rcode) {
USB_HOST_SERIAL.println("ERROR!");
return;
}
USB_HOST_SERIAL.print("\r\nPort ");
USB_HOST_SERIAL.println(port, DEC);
USB_HOST_SERIAL.println("Status");
USB_HOST_SERIAL.print("CONNECTION:\t");
USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_CONNECTION) > 0, DEC);
USB_HOST_SERIAL.print("ENABLE:\t\t");
USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_ENABLE) > 0, DEC);
USB_HOST_SERIAL.print("SUSPEND:\t");
USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_SUSPEND) > 0, DEC);
USB_HOST_SERIAL.print("OVER_CURRENT:\t");
USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_OVER_CURRENT) > 0, DEC);
USB_HOST_SERIAL.print("RESET:\t\t");
USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_RESET) > 0, DEC);
USB_HOST_SERIAL.print("POWER:\t\t");
USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_POWER) > 0, DEC);
USB_HOST_SERIAL.print("LOW_SPEED:\t");
USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_LOW_SPEED) > 0, DEC);
USB_HOST_SERIAL.print("HIGH_SPEED:\t");
USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_HIGH_SPEED) > 0, DEC);
USB_HOST_SERIAL.print("TEST:\t\t");
USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_TEST) > 0, DEC);
USB_HOST_SERIAL.print("INDICATOR:\t");
USB_HOST_SERIAL.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_INDICATOR) > 0, DEC);
if(!print_changes)
return;
USB_HOST_SERIAL.println("\r\nChange");
USB_HOST_SERIAL.print("CONNECTION:\t");
USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_CONNECTION) > 0, DEC);
USB_HOST_SERIAL.print("ENABLE:\t\t");
USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_ENABLE) > 0, DEC);
USB_HOST_SERIAL.print("SUSPEND:\t");
USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_SUSPEND) > 0, DEC);
USB_HOST_SERIAL.print("OVER_CURRENT:\t");
USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_OVER_CURRENT) > 0, DEC);
USB_HOST_SERIAL.print("RESET:\t\t");
USB_HOST_SERIAL.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_RESET) > 0, DEC);
}