2011-03-01 19:26:31 +01:00
|
|
|
#include "usbhub.h"
|
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
bool USBHub::bResetInitiated = false;
|
|
|
|
|
2011-03-01 19:26:31 +01:00
|
|
|
USBHub::USBHub(USB *p) :
|
|
|
|
pUsb(p),
|
|
|
|
bAddress(0),
|
|
|
|
bNbrPorts(0),
|
|
|
|
bInitState(0),
|
|
|
|
qNextPollTime(0),
|
|
|
|
bPollEnable(false)
|
|
|
|
{
|
|
|
|
epInfo[0].epAddr = 0;
|
2011-04-16 07:24:10 +02:00
|
|
|
epInfo[0].maxPktSize = 8;
|
|
|
|
epInfo[0].epAttribs = 0;
|
|
|
|
epInfo[0].bmNakPower = USB_NAK_MAX_POWER;
|
2011-03-01 19:26:31 +01:00
|
|
|
|
|
|
|
epInfo[1].epAddr = 1;
|
2011-04-16 07:24:10 +02:00
|
|
|
epInfo[1].maxPktSize = 1;
|
|
|
|
epInfo[1].epAttribs = 0;
|
2011-03-01 19:26:31 +01:00
|
|
|
|
|
|
|
if (pUsb)
|
|
|
|
pUsb->RegisterDeviceClass(this);
|
|
|
|
}
|
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
uint8_t USBHub::Init(uint8_t parent, uint8_t port, bool lowspeed)
|
2011-03-01 19:26:31 +01:00
|
|
|
{
|
2011-03-05 08:33:02 +01:00
|
|
|
uint8_t buf[32];
|
|
|
|
uint8_t rcode;
|
|
|
|
UsbDevice *p = NULL;
|
2011-04-16 07:24:10 +02:00
|
|
|
EpInfo *oldep_ptr = NULL;
|
2011-03-05 08:33:02 +01:00
|
|
|
uint8_t len = 0;
|
|
|
|
uint16_t cd_len = 0;
|
|
|
|
|
|
|
|
AddressPool &addrPool = pUsb->GetAddressPool();
|
2011-03-01 19:26:31 +01:00
|
|
|
|
|
|
|
switch (bInitState)
|
|
|
|
{
|
|
|
|
case 0:
|
2011-03-05 08:33:02 +01:00
|
|
|
if (bAddress)
|
|
|
|
return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
|
2011-03-01 19:26:31 +01:00
|
|
|
|
2011-03-05 08:33:02 +01:00
|
|
|
// Get pointer to pseudo device with address 0 assigned
|
|
|
|
p = addrPool.GetUsbDevicePtr(0);
|
2011-03-01 19:26:31 +01:00
|
|
|
|
2011-03-05 08:33:02 +01:00
|
|
|
if (!p)
|
|
|
|
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
|
|
|
|
|
|
|
|
if (!p->epinfo)
|
|
|
|
return USB_ERROR_EPINFO_IS_NULL;
|
|
|
|
|
|
|
|
// Save old pointer to EP_RECORD of address 0
|
|
|
|
oldep_ptr = p->epinfo;
|
2011-03-01 19:26:31 +01:00
|
|
|
|
2011-03-05 08:33:02 +01:00
|
|
|
// Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
|
|
|
|
p->epinfo = epInfo;
|
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
p->lowspeed = lowspeed;
|
|
|
|
|
2011-03-05 08:33:02 +01:00
|
|
|
// Get device descriptor
|
|
|
|
rcode = pUsb->getDevDescr( 0, 0, 8, (uint8_t*)buf );
|
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
p->lowspeed = false;
|
|
|
|
|
2011-03-05 08:33:02 +01:00
|
|
|
if (!rcode)
|
|
|
|
len = (buf[0] > 32) ? 32 : buf[0];
|
|
|
|
|
|
|
|
if( rcode )
|
|
|
|
{
|
|
|
|
// Restore p->epinfo
|
|
|
|
p->epinfo = oldep_ptr;
|
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract device class from device descriptor
|
|
|
|
// If device class is not a hub return
|
2011-03-01 19:26:31 +01:00
|
|
|
if (((USB_DEVICE_DESCRIPTOR*)buf)->bDeviceClass != 0x09)
|
|
|
|
return USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
|
|
|
|
|
2011-03-05 08:33:02 +01:00
|
|
|
// Allocate new address according to device class
|
|
|
|
bAddress = addrPool.AllocAddress(parent, (((USB_DEVICE_DESCRIPTOR*)buf)->bDeviceClass == 0x09) ? true : false, port);
|
|
|
|
|
|
|
|
if (!bAddress)
|
|
|
|
return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
|
|
|
|
|
|
|
|
// Extract Max Packet Size from the device descriptor
|
2011-04-16 07:24:10 +02:00
|
|
|
epInfo[0].maxPktSize = ((USB_DEVICE_DESCRIPTOR*)buf)->bMaxPacketSize0;
|
2011-03-05 08:33:02 +01:00
|
|
|
|
|
|
|
// Assign new address to the device
|
|
|
|
rcode = pUsb->setAddr( 0, 0, bAddress );
|
|
|
|
|
|
|
|
if (rcode)
|
|
|
|
{
|
|
|
|
// Restore p->epinfo
|
|
|
|
p->epinfo = oldep_ptr;
|
|
|
|
addrPool.FreeAddress(bAddress);
|
|
|
|
bAddress = 0;
|
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore p->epinfo
|
|
|
|
p->epinfo = oldep_ptr;
|
|
|
|
|
|
|
|
if (len)
|
|
|
|
rcode = pUsb->getDevDescr( bAddress, 0, len, (uint8_t*)buf );
|
|
|
|
|
|
|
|
if(rcode)
|
|
|
|
goto FailGetDevDescr;
|
|
|
|
|
|
|
|
// Assign epInfo to epinfo pointer
|
2011-04-16 07:24:10 +02:00
|
|
|
rcode = pUsb->setEpInfoEntry(bAddress, 2, epInfo);
|
2011-03-01 19:26:31 +01:00
|
|
|
|
|
|
|
if (rcode)
|
|
|
|
goto FailSetDevTblEntry;
|
|
|
|
|
|
|
|
bInitState = 1;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// Get hub descriptor
|
2011-03-05 08:33:02 +01:00
|
|
|
rcode = pUsb->GetHubDescriptor(bAddress, 0, 0, 8, buf);
|
2011-03-01 19:26:31 +01:00
|
|
|
|
|
|
|
if (rcode)
|
|
|
|
goto FailGetHubDescr;
|
|
|
|
|
|
|
|
// Save number of ports for future use
|
|
|
|
bNbrPorts = ((HubDescriptor*)buf)->bNbrPorts;
|
2011-03-05 08:33:02 +01:00
|
|
|
|
2011-03-01 19:26:31 +01:00
|
|
|
bInitState = 2;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// Read configuration Descriptor in Order To Obtain Proper Configuration Value
|
2011-03-05 08:33:02 +01:00
|
|
|
rcode = pUsb->getConfDescr(bAddress, 0, 8, 0, buf);
|
2011-03-01 19:26:31 +01:00
|
|
|
|
2011-03-05 08:33:02 +01:00
|
|
|
if (!rcode)
|
|
|
|
{
|
|
|
|
cd_len = ((USB_CONFIGURATION_DESCRIPTOR*)buf)->wTotalLength;
|
|
|
|
rcode = pUsb->getConfDescr(bAddress, 0, cd_len, 0, buf);
|
|
|
|
}
|
2011-03-01 19:26:31 +01:00
|
|
|
if (rcode)
|
|
|
|
goto FailGetConfDescr;
|
|
|
|
|
2011-03-05 08:33:02 +01:00
|
|
|
// The following code is of no practical use in real life applications.
|
|
|
|
// It only intended for the usb protocol sniffer to properly parse hub-class requests.
|
|
|
|
{
|
|
|
|
uint8_t buf2[24];
|
|
|
|
|
|
|
|
rcode = pUsb->getConfDescr(bAddress, 0, buf[0], 0, buf2);
|
|
|
|
|
|
|
|
if (rcode)
|
|
|
|
goto FailGetConfDescr;
|
|
|
|
}
|
|
|
|
|
2011-03-01 19:26:31 +01:00
|
|
|
// Set Configuration Value
|
2011-03-05 08:33:02 +01:00
|
|
|
rcode = pUsb->setConf(bAddress, 0, buf[5]);
|
2011-03-01 19:26:31 +01:00
|
|
|
|
|
|
|
if (rcode)
|
|
|
|
goto FailSetConfDescr;
|
|
|
|
|
|
|
|
bInitState = 3;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
// Power on all ports
|
|
|
|
for (uint8_t j=1; j<=bNbrPorts; j++)
|
2011-03-05 08:33:02 +01:00
|
|
|
pUsb->HubPortPowerOn(bAddress, j);
|
2011-03-01 19:26:31 +01:00
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
pUsb->SetHubPreMask();
|
2011-03-01 19:26:31 +01:00
|
|
|
bPollEnable = true;
|
|
|
|
bInitState = 0;
|
|
|
|
}
|
|
|
|
bInitState = 0;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
FailGetDevDescr:
|
|
|
|
goto Fail;
|
|
|
|
|
|
|
|
FailSetDevTblEntry:
|
|
|
|
goto Fail;
|
|
|
|
|
|
|
|
FailGetHubDescr:
|
|
|
|
goto Fail;
|
|
|
|
|
|
|
|
FailGetConfDescr:
|
|
|
|
goto Fail;
|
|
|
|
|
|
|
|
FailSetConfDescr:
|
|
|
|
goto Fail;
|
|
|
|
|
|
|
|
FailGetPortStatus:
|
|
|
|
goto Fail;
|
|
|
|
|
|
|
|
Fail:
|
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
|
2011-03-05 08:33:02 +01:00
|
|
|
uint8_t USBHub::Release()
|
2011-03-01 19:26:31 +01:00
|
|
|
{
|
2011-03-05 08:33:02 +01:00
|
|
|
pUsb->GetAddressPool().FreeAddress(bAddress);
|
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
if (bAddress == 0x41)
|
|
|
|
pUsb->SetHubPreMask();
|
|
|
|
|
2011-03-01 19:26:31 +01:00
|
|
|
bAddress = 0;
|
|
|
|
bNbrPorts = 0;
|
|
|
|
qNextPollTime = 0;
|
|
|
|
bPollEnable = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t USBHub::Poll()
|
|
|
|
{
|
|
|
|
uint8_t rcode = 0;
|
|
|
|
|
|
|
|
if (!bPollEnable)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (qNextPollTime <= millis())
|
|
|
|
{
|
2011-03-05 08:33:02 +01:00
|
|
|
Serial.println(bAddress, HEX);
|
2011-03-01 19:26:31 +01:00
|
|
|
|
|
|
|
rcode = GetHubStatus(bAddress);
|
2011-03-05 08:33:02 +01:00
|
|
|
qNextPollTime = millis() + 100;
|
2011-03-01 19:26:31 +01:00
|
|
|
}
|
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t USBHub::GetHubStatus(uint8_t addr)
|
|
|
|
{
|
|
|
|
uint8_t rcode;
|
|
|
|
uint8_t buf[8];
|
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
rcode = pUsb->inTransfer(addr, 1, 1, buf);
|
2011-03-01 19:26:31 +01:00
|
|
|
|
|
|
|
if (rcode)
|
|
|
|
return rcode;
|
|
|
|
|
|
|
|
if (buf[0] & 0x01) // Hub Status Change
|
|
|
|
{
|
2011-04-16 07:24:10 +02:00
|
|
|
//pUsb->PrintHubStatus(addr);
|
2011-03-01 19:26:31 +01:00
|
|
|
//rcode = GetHubStatus(1, 0, 1, 4, buf);
|
|
|
|
//if (rcode)
|
|
|
|
//{
|
|
|
|
// Serial.print("GetHubStatus Error");
|
|
|
|
// Serial.println(rcode, HEX);
|
|
|
|
// return rcode;
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
for (uint8_t port=1,mask=0x02; port<8; mask<<=1, port++)
|
|
|
|
{
|
|
|
|
if (buf[0] & mask)
|
|
|
|
{
|
|
|
|
HubEvent evt;
|
|
|
|
evt.bmEvent = 0;
|
|
|
|
|
|
|
|
rcode = pUsb->GetPortStatus(addr, 0, port, 4, evt.evtBuff);
|
|
|
|
|
|
|
|
if (rcode)
|
|
|
|
continue;
|
2011-04-16 07:24:10 +02:00
|
|
|
|
|
|
|
rcode = HubPortStatusChange(addr, port, evt);
|
|
|
|
|
|
|
|
if (rcode == HUB_ERROR_PORT_HAS_BEEN_RESET)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (rcode)
|
|
|
|
return rcode;
|
2011-03-01 19:26:31 +01:00
|
|
|
}
|
|
|
|
} // for
|
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
for (uint8_t port=1; port<=bNbrPorts; port++)
|
|
|
|
{
|
|
|
|
HubEvent evt;
|
|
|
|
evt.bmEvent = 0;
|
|
|
|
|
|
|
|
rcode = pUsb->GetPortStatus(addr, 0, port, 4, evt.evtBuff);
|
|
|
|
|
|
|
|
if (rcode)
|
|
|
|
continue;
|
2011-03-01 19:26:31 +01:00
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
if ((evt.bmStatus & bmHUB_PORT_STATE_CHECK_DISABLED) != bmHUB_PORT_STATE_DISABLED)
|
|
|
|
continue;
|
2011-03-01 19:26:31 +01:00
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
// Emulate connection event for the port
|
|
|
|
evt.bmChange |= bmHUB_PORT_STATUS_C_PORT_CONNECTION;
|
2011-03-01 19:26:31 +01:00
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
rcode = HubPortStatusChange(addr, port, evt);
|
2011-03-01 19:26:31 +01:00
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
if (rcode == HUB_ERROR_PORT_HAS_BEEN_RESET)
|
|
|
|
return 0;
|
2011-03-01 19:26:31 +01:00
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
if (rcode)
|
|
|
|
return rcode;
|
|
|
|
} // for
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t USBHub::HubPortStatusChange(uint8_t addr, uint8_t port, HubEvent &evt)
|
|
|
|
{
|
2011-03-01 19:26:31 +01:00
|
|
|
switch (evt.bmEvent)
|
|
|
|
{
|
|
|
|
// Device connected event
|
|
|
|
case bmHUB_PORT_EVENT_CONNECT:
|
2011-04-16 07:24:10 +02:00
|
|
|
case bmHUB_PORT_EVENT_LS_CONNECT:
|
|
|
|
if (bResetInitiated)
|
|
|
|
return 0;
|
|
|
|
|
2011-03-01 19:26:31 +01:00
|
|
|
pUsb->HubClearPortFeatures(addr, port, HUB_FEATURE_C_PORT_ENABLE);
|
|
|
|
pUsb->HubClearPortFeatures(addr, port, HUB_FEATURE_C_PORT_CONNECTION);
|
|
|
|
pUsb->HubPortReset(addr, port);
|
2011-04-16 07:24:10 +02:00
|
|
|
bResetInitiated = true;
|
|
|
|
return HUB_ERROR_PORT_HAS_BEEN_RESET;
|
|
|
|
|
|
|
|
// Device disconnected event
|
|
|
|
case bmHUB_PORT_EVENT_DISCONNECT:
|
|
|
|
pUsb->HubClearPortFeatures(addr, port, HUB_FEATURE_C_PORT_ENABLE);
|
|
|
|
pUsb->HubClearPortFeatures(addr, port, HUB_FEATURE_C_PORT_CONNECTION);
|
|
|
|
bResetInitiated = false;
|
|
|
|
|
|
|
|
UsbDeviceAddress a;
|
|
|
|
a.bmHub = 0;
|
|
|
|
a.bmParent = addr;
|
|
|
|
a.bmAddress = port;
|
|
|
|
pUsb->ReleaseDevice(a.devAddress);
|
|
|
|
return 0;
|
2011-03-01 19:26:31 +01:00
|
|
|
|
|
|
|
// Reset complete event
|
|
|
|
case bmHUB_PORT_EVENT_RESET_COMPLETE:
|
2011-04-16 07:24:10 +02:00
|
|
|
case bmHUB_PORT_EVENT_LS_RESET_COMPLETE:
|
2011-03-01 19:26:31 +01:00
|
|
|
pUsb->HubClearPortFeatures(addr, port, HUB_FEATURE_C_PORT_RESET | HUB_FEATURE_C_PORT_CONNECTION);
|
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
delay(20);
|
2011-03-01 19:26:31 +01:00
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
a.devAddress = addr;
|
2011-03-01 19:26:31 +01:00
|
|
|
|
2011-04-16 07:24:10 +02:00
|
|
|
pUsb->Configuring(a.bmAddress, port, (evt.bmStatus & bmHUB_PORT_STATUS_PORT_LOW_SPEED) );
|
|
|
|
bResetInitiated = false;
|
2011-03-01 19:26:31 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
} // switch (evt.bmEvent)
|
2011-04-16 07:24:10 +02:00
|
|
|
return 0;
|
2011-03-01 19:26:31 +01:00
|
|
|
}
|
|
|
|
|
2011-03-05 08:33:02 +01:00
|
|
|
void PrintHubPortStatus(USB *usbptr, uint8_t addr, uint8_t port, bool print_changes)
|
2011-03-01 19:26:31 +01:00
|
|
|
{
|
|
|
|
uint8_t rcode = 0;
|
|
|
|
HubEvent evt;
|
|
|
|
|
|
|
|
rcode = usbptr->GetPortStatus(addr, 0, port, 4, evt.evtBuff);
|
|
|
|
|
|
|
|
if (rcode)
|
|
|
|
{
|
|
|
|
Serial.println("ERROR!!!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Serial.print("\r\nPort ");
|
|
|
|
Serial.println(port, DEC);
|
|
|
|
|
|
|
|
Serial.println("Status");
|
|
|
|
Serial.print("CONNECTION:\t");
|
|
|
|
Serial.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_CONNECTION) > 0, DEC);
|
|
|
|
Serial.print("ENABLE:\t\t");
|
|
|
|
Serial.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_ENABLE) > 0, DEC);
|
|
|
|
Serial.print("SUSPEND:\t");
|
|
|
|
Serial.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_SUSPEND) > 0, DEC);
|
|
|
|
Serial.print("OVER_CURRENT:\t");
|
|
|
|
Serial.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_OVER_CURRENT) > 0, DEC);
|
|
|
|
Serial.print("RESET:\t\t");
|
|
|
|
Serial.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_RESET) > 0, DEC);
|
|
|
|
Serial.print("POWER:\t\t");
|
|
|
|
Serial.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_POWER) > 0, DEC);
|
|
|
|
Serial.print("LOW_SPEED:\t");
|
|
|
|
Serial.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_LOW_SPEED) > 0, DEC);
|
|
|
|
Serial.print("HIGH_SPEED:\t");
|
|
|
|
Serial.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_HIGH_SPEED) > 0, DEC);
|
|
|
|
Serial.print("TEST:\t\t");
|
|
|
|
Serial.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_TEST) > 0, DEC);
|
|
|
|
Serial.print("INDICATOR:\t");
|
|
|
|
Serial.println((evt.bmStatus & bmHUB_PORT_STATUS_PORT_INDICATOR) > 0, DEC);
|
|
|
|
|
2011-03-05 08:33:02 +01:00
|
|
|
if (!print_changes)
|
|
|
|
return;
|
|
|
|
|
2011-03-01 19:26:31 +01:00
|
|
|
Serial.println("\nChange");
|
|
|
|
Serial.print("CONNECTION:\t");
|
|
|
|
Serial.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_CONNECTION) > 0, DEC);
|
|
|
|
Serial.print("ENABLE:\t\t");
|
|
|
|
Serial.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_ENABLE) > 0, DEC);
|
|
|
|
Serial.print("SUSPEND:\t");
|
|
|
|
Serial.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_SUSPEND) > 0, DEC);
|
|
|
|
Serial.print("OVER_CURRENT:\t");
|
|
|
|
Serial.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_OVER_CURRENT) > 0, DEC);
|
|
|
|
Serial.print("RESET:\t\t");
|
|
|
|
Serial.println((evt.bmChange & bmHUB_PORT_STATUS_C_PORT_RESET) > 0, DEC);
|
|
|
|
}
|