IOUSBControllerV2.cpp [plain text]
#include <IOKit/IOBufferMemoryDescriptor.h>
#include <IOKit/usb/IOUSBController.h>
#include <IOKit/usb/IOUSBControllerV2.h>
#include <IOKit/usb/IOUSBLog.h>
#define CONTROLLERV2_USE_KPRINTF 0
#if CONTROLLERV2_USE_KPRINTF
#undef USBLog
#undef USBError
void kprintf(const char *format, ...)
__attribute__((format(printf, 1, 2)));
#define USBLog( LEVEL, FORMAT, ARGS... ) if ((LEVEL) <= 3) { kprintf( FORMAT "\n", ## ARGS ) ; }
#define USBError( LEVEL, FORMAT, ARGS... ) { kprintf( FORMAT "\n", ## ARGS ) ; }
#endif
extern IOReturn CheckForDisjointDescriptor(IOUSBCommand *command, UInt16 maxPacketSize);
#define super IOUSBController
enum {
kSetupSent = 0x01,
kDataSent = 0x02,
kStatusSent = 0x04,
kSetupBack = 0x10,
kDataBack = 0x20,
kStatusBack = 0x40
};
OSDefineMetaClass( IOUSBControllerV2, IOUSBController )
OSDefineAbstractStructors(IOUSBControllerV2, IOUSBController)
bool
IOUSBControllerV2::init(OSDictionary * propTable)
{
if (!super::init(propTable)) return false;
if (!_v2ExpansionData)
{
_v2ExpansionData = (V2ExpansionData *)IOMalloc(sizeof(V2ExpansionData));
if (!_v2ExpansionData)
return false;
bzero(_v2ExpansionData, sizeof(V2ExpansionData));
}
return (true);
}
bool
IOUSBControllerV2::start( IOService * provider )
{
if( !super::start(provider))
return (false);
_returnIsochDoneQueueThread = thread_call_allocate((thread_call_func_t)ReturnIsochDoneQueueEntry, (thread_call_param_t)this);
if ( !_returnIsochDoneQueueThread )
{
USBError(1, "IOUSBControllerV2[%p]::start - could not allocate thread callout function.", this);
return false;
}
return true;
}
void
IOUSBControllerV2::free()
{
if (_returnIsochDoneQueueThread)
{
thread_call_cancel(_returnIsochDoneQueueThread);
thread_call_free(_returnIsochDoneQueueThread);
}
super::free();
}
void
IOUSBControllerV2::clearTTHandler(OSObject *target, void *parameter, IOReturn status, UInt32 bufferSizeRemaining)
{
IOUSBController * me = (IOUSBController *)target;
IOUSBCommand * command = (IOUSBCommand *)parameter;
IOMemoryDescriptor * memDesc = NULL;
IODMACommand * dmaCommand = NULL;
UInt8 sent, back, todo;
UInt8 hubAddr = command->GetAddress();
USBLog(5,"clearTTHandler: status (0x%x)", status);
if (!me || !command)
{
USBError(1,"clearTTHandler: missing controller or command");
}
sent = (command->GetStage() & 0x0f) << 4;
back = command->GetStage() & 0xf0;
todo = sent ^ back;
if((todo & kSetupBack) != 0)
{
USBLog(2,"clearTTHandler: Setup comming back to us, check and forget");
command->SetStage(command->GetStage() | kSetupBack);
}
else
{
dmaCommand = command->GetDMACommand();
memDesc = command->GetRequestMemoryDescriptor();
command->SetStage(0);
if (dmaCommand)
{
if (dmaCommand->getMemoryDescriptor())
{
if (dmaCommand->getMemoryDescriptor() != memDesc)
{
USBLog(1, "clearTTHandler: DMA Command Memory Descriptor (%p) does not match Request MemoryDescriptor (%p)", dmaCommand->getMemoryDescriptor(), memDesc);
}
USBLog(7, "clearTTHandler: clearing memory descriptor (%p) from dmaCommand (%p)", dmaCommand->getMemoryDescriptor(), dmaCommand);
dmaCommand->clearMemoryDescriptor();
}
else
{
USBLog(1, "clearTTHandler - dmaCommand (%p) already cleared", dmaCommand);
}
}
if (memDesc)
{
USBLog(1, "clearTTHandler - completing and freeing memory descriptor (%p)", memDesc);
memDesc->complete();
memDesc->release();
}
else
{
USBLog(1, "clearTTHandler - missing memory descriptor");
}
USBLog(5,"clearTTHandler: We've already seen the setup, deallocate command (%p)", command);
me->_freeUSBCommandPool->returnCommand(command);
}
if ((status != kIOReturnSuccess) && (status != kIOUSBTransactionReturned))
{
USBLog(1, "%s[%p]::clearTTHandler - error response from hub, clearing hub endpoint stall", me->getName(), me);
me->UIMClearEndpointStall(hubAddr, 0, kUSBAnyDirn);
}
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 6);
void
IOUSBControllerV2::ClearTT(USBDeviceAddress fnAddress, UInt8 endpt, Boolean IN)
{
UInt16 wValue;
IOBufferMemoryDescriptor *memDesc;
IOUSBDevRequest *clearRequest;
short hubAddress;
IOUSBCommand *clearCommand;
IOUSBCompletion completion;
int i;
IOReturn err;
IODMACommand *dmaCommand = NULL;
USBLog(5,"+%s[%p]::ClearTT", getName(), this);
hubAddress = _highSpeedHub[fnAddress]; if(hubAddress == 0) {
USBLog(1,"-%s[%p]::ClearTT high speed device, returning", getName(), this);
return;
}
memDesc = IOBufferMemoryDescriptor::withOptions(kIOMemoryUnshared | kIODirectionInOut, sizeof(IOUSBDevRequest));
if (!memDesc)
{
USBLog(1,"%s[%p]::ClearTT Could not get a memory descriptor",getName(),this);
return;
}
clearCommand = (IOUSBCommand *)_freeUSBCommandPool->getCommand(false);
if ( clearCommand == NULL )
{
IncreaseCommandPool();
clearCommand = (IOUSBCommand *)_freeUSBCommandPool->getCommand(false);
if ( clearCommand == NULL )
{
USBLog(1,"%s[%p]::ClearTT Could not get a IOUSBCommand",getName(),this);
memDesc->release();
return;
}
}
USBLog(7, "%s[%p]::ClearTT V2 got command (%p)", getName(), this, clearCommand);
dmaCommand = clearCommand->GetDMACommand();
if (!dmaCommand)
{
USBError(1,"%s[%p]::ClearTT - No dmaCommand in the usb command", getName(), this);
return;
}
if (dmaCommand->getMemoryDescriptor())
{
IOMemoryDescriptor *memDesc = (IOMemoryDescriptor *)dmaCommand->getMemoryDescriptor();
USBError(1,"%s[%p]::ClearTT - dmaCommand (%p) already had memory descriptor (%p) - clearing", getName(), this, dmaCommand, memDesc);
dmaCommand->clearMemoryDescriptor();
}
clearRequest = (IOUSBDevRequest*)memDesc->getBytesNoCopy();
if (!clearRequest)
{
USBLog(1,"%s[%p]::ClearTT Could not get a IOUSBDevRequest", getName(), this);
_freeUSBCommandPool->returnCommand(clearCommand);
return;
}
USBLog(5, "%s[%p]::ClearTT - got IOUSBDevRequest (%p)", getName(), this, clearRequest);
wValue = (endpt & 0xf) | ( (fnAddress & 0x7f) << 4);
if(IN)
{
wValue |= (1 << 15);
}
USBLog(5,"%s[%p]::ClearTT - V2 EP (%d) ADDR (%d) wValue (0x%x)", getName(), this, endpt, fnAddress, wValue);
clearRequest->bmRequestType = 0x23;
clearRequest->bRequest = 8;
clearRequest->wValue = HostToUSBWord(wValue);
if(_v2ExpansionData->_multiTT[hubAddress])
{ clearRequest->wIndex = HostToUSBWord(_highSpeedPort[fnAddress]);
}
else
{ clearRequest->wIndex = HostToUSBWord(1);
}
clearRequest->wLength = HostToUSBWord(0);
clearRequest->pData = NULL;
clearRequest->wLenDone = 0;
completion.target = (void *)this;
completion.action = (IOUSBCompletionAction) &clearTTHandler;
completion.parameter = clearCommand;
clearCommand->SetUSLCompletion(completion);
clearCommand->SetUseTimeStamp(false);
clearCommand->SetSelector(DEVICE_REQUEST);
clearCommand->SetRequest(clearRequest);
clearCommand->SetAddress(hubAddress);
clearCommand->SetEndpoint(0);
clearCommand->SetType(kUSBControl);
clearCommand->SetBuffer(0); clearCommand->SetClientCompletion(completion);
clearCommand->SetNoDataTimeout(5000);
clearCommand->SetCompletionTimeout(0);
clearCommand->SetStage(0);
clearCommand->SetBufferUSBCommand(NULL);
memDesc->prepare();
clearCommand->SetRequestMemoryDescriptor(memDesc);
clearCommand->SetReqCount(8);
USBLog(7,"%s[%p]::ClearTT - setting memory descriptor (%p) into dmaCommand (%p)", getName(), this, memDesc, dmaCommand);
err = dmaCommand->setMemoryDescriptor(memDesc);
if (err)
{
USBError(1,"%s[%p]::ClearTT - err (%p) setting memory descriptor (%p) into dmaCommand (%p)", getName(), this, (void*)err, memDesc, dmaCommand);
memDesc->complete();
memDesc->release();
return;
}
for (i=0; i < 10; i++)
clearCommand->SetUIMScratch(i, 0);
err = ControlTransaction(clearCommand); if (err)
{
USBLog(1, "%s[%p]::ClearTT - error 0x%x returned from ControlTransaction", getName(), this, err);
}
}
IOReturn IOUSBControllerV2::OpenPipe(USBDeviceAddress address, UInt8 speed,
Endpoint *endpoint)
{
return _commandGate->runAction(DoCreateEP, (void *)(UInt32)address,
(void *)(UInt32)speed, endpoint);
}
IOReturn
IOUSBControllerV2::DoCreateEP(OSObject *owner,
void *arg0, void *arg1,
void *arg2, void *arg3)
{
IOUSBControllerV2 *me = (IOUSBControllerV2 *)owner;
UInt8 address = (UInt8)(UInt32)arg0;
UInt8 speed = (UInt8)(UInt32)arg1;
Endpoint *endpoint = (Endpoint *)arg2;
IOReturn err;
USBLog(5,"%s[%p]::DoCreateEP, high speed ancestor hub:%d, port:%d", me->getName(), me, me->_highSpeedHub[address], me->_highSpeedPort[address]);
switch (endpoint->transferType)
{
case kUSBInterrupt:
err = me->UIMCreateInterruptEndpoint(address,
endpoint->number,
endpoint->direction,
speed,
endpoint->maxPacketSize,
endpoint->interval,
me->_highSpeedHub[address],
me->_highSpeedPort[address]);
break;
case kUSBBulk:
err = me->UIMCreateBulkEndpoint(address,
endpoint->number,
endpoint->direction,
speed,
endpoint->maxPacketSize,
me->_highSpeedHub[address],
me->_highSpeedPort[address]);
break;
case kUSBControl:
err = me->UIMCreateControlEndpoint(address,
endpoint->number,
endpoint->maxPacketSize,
speed,
me->_highSpeedHub[address],
me->_highSpeedPort[address]);
break;
case kUSBIsoc:
{
if (speed == kUSBDeviceSpeedHigh)
{
if ((endpoint->interval < 1) || (endpoint->interval > 16))
{
USBLog(1, "%s[%p]::DoCreateEP - The USB 2.0 spec only allows Isoch EP with bInterval values of 1 through 16 "
"(see Table 9-13), but the illegal interval %d [0x%x] was requested, returning kIOReturnBadArgument",
me->getName(), me, endpoint->interval, endpoint->interval);
err = kIOReturnBadArgument;
break;
}
USBLog(4, "%s[%p]::DoCreateEP - Creating a High-Speed Isoch EP with raw interval %u", me->getName(), me, (unsigned int )endpoint->interval);
}
else
{
endpoint->interval = 1;
USBLog(4, "%s[%p]::DoCreateEP - Creating a Full-Speed Isoch EP with interval %u", me->getName(), me, (unsigned int )endpoint->interval);
}
err = me->UIMCreateIsochEndpoint(address,
endpoint->number,
endpoint->maxPacketSize,
endpoint->direction,
me->_highSpeedHub[address],
me->_highSpeedPort[address],
endpoint->interval);
break;
}
default:
err = kIOReturnBadArgument;
break;
}
return (err);
}
IOReturn
IOUSBControllerV2::CreateDevice( IOUSBDevice *newDevice,
USBDeviceAddress deviceAddress,
UInt8 maxPacketSize,
UInt8 speed,
UInt32 powerAvailable,
USBDeviceAddress hub,
int port)
{
USBLog(5,"%s[%p]::CreateDevice, new method called with hub:%d, port:%d", getName(), this, hub, port);
if(speed != kUSBDeviceSpeedHigh)
{
if(_highSpeedHub[hub] == 0) {
_highSpeedHub[deviceAddress] = hub;
_highSpeedPort[deviceAddress] = port;
}
else
{
_highSpeedHub[deviceAddress] = _highSpeedHub[hub];
_highSpeedPort[deviceAddress] = _highSpeedPort[hub];
}
}
else
{
_highSpeedHub[deviceAddress] = 0;
_highSpeedPort[deviceAddress] = 0;
}
USBLog(5,"%s[%p]::CreateDevice, high speed ancestor hub:%d, port:%d",getName(), this, _highSpeedHub[deviceAddress], _highSpeedPort[deviceAddress]);
return (super::CreateDevice(newDevice, deviceAddress, maxPacketSize, speed, powerAvailable));
}
IOReturn
IOUSBControllerV2::ConfigureDeviceZero(UInt8 maxPacketSize, UInt8 speed, USBDeviceAddress hub, int port)
{
USBLog(5,"%s[%p]::ConfigureDeviceZero, new method called with hub:%d, port:%d", getName(), this, hub, port);
if(speed != kUSBDeviceSpeedHigh)
{
if(_highSpeedHub[hub] == 0) {
_highSpeedHub[0] = hub;
_highSpeedPort[0] = port;
}
else
{
_highSpeedHub[0] = _highSpeedHub[hub];
_highSpeedPort[0] = _highSpeedPort[hub];
}
}
else
{
_highSpeedHub[0] = 0;
_highSpeedPort[0] = 0;
}
USBLog(5, "%s[%p]::CreateDevice, high speed ancestor hub:%d, port:%d", getName(), this, _highSpeedHub[0], _highSpeedPort[0]);
return (super::ConfigureDeviceZero(maxPacketSize, speed));
}
IOReturn
IOUSBControllerV2::DOHSHubMaintenance(OSObject *owner, void *arg0, void *arg1, void *arg2, void *arg3)
{
IOUSBControllerV2 *me = (IOUSBControllerV2 *)owner;
USBDeviceAddress highSpeedHub = (USBDeviceAddress)(UInt32)arg0;
UInt32 command = (UInt32)arg1;
UInt32 flags = (UInt32)arg2;
UInt8 multi;
USBLog(5,"%s[%p]::DOHSHubMaintenance, command: %ld, flags: %ld", me->getName(), me, command, flags);
multi = ((flags & kUSBHSHubFlagsMultiTT) != 0);
me->_v2ExpansionData->_multiTT[highSpeedHub] = multi;
USBLog(3,"%s[%p]::DOHSHubMaintenance hub at %d is multiTT:%d", me->getName(), me, highSpeedHub, me->_v2ExpansionData->_multiTT[highSpeedHub]);
return me->UIMHubMaintenance(highSpeedHub, 0, command, flags);
}
IOReturn
IOUSBControllerV2::DOSetTestMode(OSObject *owner, void *arg0, void *arg1, void *arg2, void *arg3)
{
IOUSBControllerV2 *me = (IOUSBControllerV2 *)owner;
UInt32 mode = (UInt32)arg0;
UInt32 port = (UInt32)arg1;
USBLog(5,"%s[%p]::DOSetTestMode, mode: %ld, port: %ld", me->getName(), me, mode, port);
return me->UIMSetTestMode(mode, port);
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 0);
IOReturn
IOUSBControllerV2::AddHSHub(USBDeviceAddress highSpeedHub, UInt32 flags)
{
return _commandGate->runAction(DOHSHubMaintenance, (void *)(UInt32)highSpeedHub,
(void *)(UInt32)kUSBHSHubCommandAddHub, (void *)flags);
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 1);
IOReturn
IOUSBControllerV2::UIMHubMaintenance(USBDeviceAddress highSpeedHub, UInt32 highSpeedPort, UInt32 command, UInt32 flags)
{
return kIOReturnUnsupported; }
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 2);
IOReturn
IOUSBControllerV2::RemoveHSHub(USBDeviceAddress highSpeedHub)
{
return _commandGate->runAction(DOHSHubMaintenance, (void *)(UInt32)highSpeedHub,
(void *)(UInt32)kUSBHSHubCommandRemoveHub, NULL);
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 3);
IOReturn
IOUSBControllerV2::SetTestMode(UInt32 mode, UInt32 port)
{
return _commandGate->runAction(DOSetTestMode, (void *)mode, (void *)port);
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 4);
IOReturn
IOUSBControllerV2::UIMSetTestMode(UInt32 mode, UInt32 port)
{
return kIOReturnUnsupported; }
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 5);
UInt64
IOUSBControllerV2::GetMicroFrameNumber(void)
{
return 0; }
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 7);
IOReturn
IOUSBControllerV2::ReadV2(IOMemoryDescriptor *buffer, USBDeviceAddress address, Endpoint *endpoint, IOUSBCompletionWithTimeStamp *completion, UInt32 noDataTimeout, UInt32 completionTimeout, IOByteCount reqCount)
{
IOReturn err = kIOReturnSuccess;
IOUSBCommand *command;
IOUSBCompletion nullCompletion;
IOUSBCompletion theCompletion;
IOUSBCompletionAction theAction;
IODMACommand *dmaCommand = NULL;
int i;
USBLog(7, "%s[%p]::ReadV2 - reqCount = %ld", getName(), this, reqCount);
if ((endpoint->direction != kUSBIn) || !buffer || (buffer->getLength() < reqCount))
{
USBLog(5, "%s[%p]::ReadV2 - direction is not kUSBIn (%d), No Buffer, or buffer length < reqCount (%ld < %ld). Returning kIOReturnBadArgument(0x%x)", getName(), this, endpoint->direction, buffer->getLength(), reqCount, kIOReturnBadArgument);
return kIOReturnBadArgument;
}
if ((endpoint->transferType != kUSBBulk) && (noDataTimeout || completionTimeout))
{
USBLog(5, "%s[%p]::ReadV2 - Pipe is NOT kUSBBulk (%d) AND specified a timeout (%ld, %ld). Returning kIOReturnBadArgument(0x%x)", getName(), this, endpoint->transferType, noDataTimeout, completionTimeout, kIOReturnBadArgument);
return kIOReturnBadArgument; }
if (!completion)
{
USBLog(5, "%s[%p]::ReadV2 - No Completion routine. Returning kIOReturnNoCompletion(0x%x)", getName(), this, kIOReturnNoCompletion);
return kIOReturnNoCompletion;
}
if (!_commandGate)
{
USBLog(5, "%s[%p]::ReadV2 - Could not get _commandGate. Returning kIOReturnInternalError(0x%x)", getName(), this, kIOReturnInternalError);
return kIOReturnInternalError;
}
command = (IOUSBCommand *)_freeUSBCommandPool->getCommand(false);
if ( command == NULL )
{
IncreaseCommandPool();
command = (IOUSBCommand *)_freeUSBCommandPool->getCommand(false);
if ( command == NULL )
{
USBLog(3,"%s[%p]::ReadV2 Could not get a IOUSBCommand",getName(),this);
return kIOReturnNoResources;
}
}
if (reqCount)
{
dmaCommand = command->GetDMACommand();
if (!dmaCommand)
{
USBError(1, "%s[%p]::ReadV2 - no DMA COMMAND", getName(), this);
return kIOReturnNoResources;
}
if (dmaCommand->getMemoryDescriptor())
{
IOMemoryDescriptor *memDesc = (IOMemoryDescriptor *)dmaCommand->getMemoryDescriptor();
USBError(1, "%s[%p]::ReadV2 - dma command (%p) already contains memory descriptor (%p) - clearing", getName(), this, dmaCommand, memDesc);
dmaCommand->clearMemoryDescriptor();
}
if (0) {
USBError(1, "%s[%p]::ReadV2 - err(%p) attempting to prepare buffer (%p)", getName(), this, (void*)err, buffer);
return err;
}
USBLog(7, "%s[%p]::ReadV2 - setting memory descriptor (%p) into dmaCommand (%p)", getName(), this, buffer, dmaCommand);
err = dmaCommand->setMemoryDescriptor(buffer);
if (err)
{
USBError(1, "%s[%p]::ReadV2 - err(%p) attempting to set the memory descriptor to the dmaCommand", getName(), this, (void*)err);
_freeUSBCommandPool->returnCommand(command);
return err;
}
}
theCompletion.target = completion->target;
theCompletion.action = (IOUSBCompletionAction)completion->action;
theCompletion.parameter = completion->parameter;
if ( (UInt32) completion->action == (UInt32) &IOUSBSyncCompletion )
command->SetIsSyncTransfer(true);
else
command->SetIsSyncTransfer(false);
command->SetUseTimeStamp(true);
command->SetSelector(READ);
command->SetRequest(0); command->SetAddress(address);
command->SetEndpoint(endpoint->number);
command->SetDirection(kUSBIn);
command->SetType(endpoint->transferType);
command->SetBuffer(buffer);
command->SetReqCount(reqCount);
command->SetClientCompletion(theCompletion);
command->SetNoDataTimeout(noDataTimeout);
command->SetCompletionTimeout(completionTimeout);
for (i=0; i < 10; i++)
command->SetUIMScratch(i, 0);
nullCompletion.target = (void *) NULL;
nullCompletion.action = (IOUSBCompletionAction) NULL;
nullCompletion.parameter = (void *) NULL;
command->SetDisjointCompletion(nullCompletion);
err = CheckForDisjointDescriptor(command, endpoint->maxPacketSize);
if (kIOReturnSuccess == err)
{
err = _commandGate->runAction(DoIOTransfer, command);
if ( command->GetIsSyncTransfer() || (kIOReturnSuccess != err) )
{
IODMACommand *dmaCommand = command->GetDMACommand();
IOMemoryDescriptor *memDesc = dmaCommand ? (IOMemoryDescriptor *)dmaCommand->getMemoryDescriptor() : NULL;
nullCompletion = command->GetDisjointCompletion();
if (nullCompletion.action)
{
USBLog(1, "%s[%p]::ReadV2 - SYNC xfer or immediate error with Disjoint Completion", getName(), this);
}
if (memDesc)
{
USBLog(7, "%s[%p]::ReadV2 - SYNC xfer or immediate error - clearing memDesc (%p) from dmaCommand (%p)", getName(), this, memDesc, dmaCommand);
dmaCommand->clearMemoryDescriptor();
}
_freeUSBCommandPool->returnCommand(command);
}
}
else
{
IODMACommand *dmaCommand = command->GetDMACommand();
IOMemoryDescriptor *memDesc = dmaCommand ? (IOMemoryDescriptor *)dmaCommand->getMemoryDescriptor() : NULL;
if (memDesc)
{
USBLog(7, "%s[%p]::ReadV2 - CheckForDisjointDescriptor error (%p) - clearing memDesc (%p) from dmaCommand (%p)", getName(), this, (void*)err, memDesc, dmaCommand);
dmaCommand->clearMemoryDescriptor();
}
_freeUSBCommandPool->returnCommand(command);
}
return err;
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 8);
IOReturn
IOUSBControllerV2::UIMCreateIsochEndpoint(short functionAddress, short endpointNumber, UInt32 maxPacketSize, UInt8 direction, USBDeviceAddress highSpeedHub, int highSpeedPort, UInt8 interval)
{
return UIMCreateIsochEndpoint(functionAddress, endpointNumber, maxPacketSize, direction, highSpeedHub, highSpeedPort);
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 9);
IOUSBControllerIsochEndpoint*
IOUSBControllerV2::AllocateIsochEP()
{
USBError(1, "IOUSBControllerV2[%p]::AllocateIsochEP - should be overriden in a subclass", this);
return NULL;
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 10);
IOReturn
IOUSBControllerV2::DeallocateIsochEP(IOUSBControllerIsochEndpoint* pEP)
{
USBLog(4, "%s[%p]::DeallocateIsochEP (%p)",getName(), this, pEP);
pEP->nextEP = _freeIsochEPList;
_freeIsochEPList = pEP;
return kIOReturnSuccess;
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 11);
IOUSBControllerIsochEndpoint*
IOUSBControllerV2::FindIsochronousEndpoint(short functionAddress,
short endpointNumber,
short direction,
IOUSBControllerIsochEndpoint **ppEPBack)
{
IOUSBControllerIsochEndpoint *pEP, *pBack;
pEP = _isochEPList;
pBack = NULL;
while (pEP)
{
if ((pEP->functionAddress == functionAddress)
&& (pEP->endpointNumber == endpointNumber)
&& (pEP->direction == direction))
break;
pBack = pEP;
pEP = pEP->nextEP;
}
if (pEP && ppEPBack)
*ppEPBack = pBack;
return pEP;
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 12);
IOUSBControllerIsochEndpoint*
IOUSBControllerV2::CreateIsochronousEndpoint(short functionAddress,
short endpointNumber,
short direction)
{
IOUSBControllerIsochEndpoint* pEP;
int i;
USBLog(4, "+%s[%p]::CreateIsochronousEndpoint (%d:%d:%d)", getName(), this, functionAddress, endpointNumber, direction);
pEP = _freeIsochEPList;
if (!pEP)
{
pEP = AllocateIsochEP();
USBLog(4, "%s[%p]::CreateIsochronousEndpoint called AllocateIsochEP (%p)",getName(), this, pEP);
}
if (pEP)
{
_freeIsochEPList = pEP->nextEP; pEP->init(); pEP->nextEP = _isochEPList;
_isochEPList = pEP;
pEP->functionAddress = functionAddress;
pEP->endpointNumber = endpointNumber;
pEP->direction = direction;
}
return pEP;
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 13);
void
IOUSBControllerV2::PutTDonToDoList(IOUSBControllerIsochEndpoint* pED, IOUSBControllerIsochListElement *pTD)
{
USBLog(7, "AppleUSBEHCI[%p]::PutTDonToDoList - pED (%p) pTD (%p) frameNumber(%Lx)", this, pED, pTD, pTD->_frameNumber);
if(pED->toDoList == NULL)
{
pED->toDoList = pTD;
}
else
{
pED->toDoEnd->_logicalNext = pTD;
}
pED->toDoEnd = pTD;
pED->onToDoList++;
pED->activeTDs++;
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 14);
IOUSBControllerIsochListElement *
IOUSBControllerV2::GetTDfromToDoList(IOUSBControllerIsochEndpoint* pED)
{
IOUSBControllerIsochListElement *pTD;
pTD = pED->toDoList;
if (pTD)
{
if (pTD == pED->toDoEnd)
pED->toDoList = pED->toDoEnd = NULL;
else
pED->toDoList = OSDynamicCast(IOUSBControllerIsochListElement, pTD->_logicalNext);
pED->onToDoList--;
}
return pTD;
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 15);
void
IOUSBControllerV2::PutTDonDoneQueue(IOUSBControllerIsochEndpoint* pED, IOUSBControllerIsochListElement *pTD, bool checkDeferred)
{
IOUSBControllerIsochListElement *deferredTD;
if (checkDeferred)
{
while (pED->deferredQueue && (pED->deferredQueue->_frameNumber < pTD->_frameNumber))
{
deferredTD = GetTDfromDeferredQueue(pED);
PutTDonDoneQueue(pED, deferredTD, false);
}
}
if(pED->doneQueue == NULL)
{
pED->doneQueue = pTD;
}
else
{
pED->doneEnd->_logicalNext = pTD;
}
pED->doneEnd = pTD;
pED->onDoneQueue++;
if (checkDeferred && !pED->scheduledTDs && !pED->toDoList)
{
deferredTD = GetTDfromDeferredQueue(pED);
while (deferredTD)
{
PutTDonDoneQueue(pED, deferredTD, false);
deferredTD = GetTDfromDeferredQueue(pED);
}
}
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 16);
IOUSBControllerIsochListElement *
IOUSBControllerV2::GetTDfromDoneQueue(IOUSBControllerIsochEndpoint* pED)
{
IOUSBControllerIsochListElement *pTD;
pTD = pED->doneQueue;
if (pTD)
{
if (pTD == pED->doneEnd)
pED->doneQueue = pED->doneEnd = NULL;
else
pED->doneQueue = OSDynamicCast(IOUSBControllerIsochListElement, pTD->_logicalNext);
pED->onDoneQueue--;
pED->activeTDs--;
}
return pTD;
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 17);
void
IOUSBControllerV2::PutTDonDeferredQueue(IOUSBControllerIsochEndpoint* pED, IOUSBControllerIsochListElement *pTD)
{
if(pED->deferredQueue == NULL)
{
pED->deferredQueue = pTD;
}
else
{
pED->deferredEnd->_logicalNext = pTD;
}
pED->deferredEnd = pTD;
pED->deferredTDs++;
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 18);
IOUSBControllerIsochListElement *
IOUSBControllerV2::GetTDfromDeferredQueue(IOUSBControllerIsochEndpoint* pED)
{
IOUSBControllerIsochListElement *pTD;
pTD = pED->deferredQueue;
if (pTD)
{
if (pTD == pED->deferredEnd)
pED->deferredQueue = pED->deferredEnd = NULL;
else
pED->deferredQueue = OSDynamicCast(IOUSBControllerIsochListElement, pTD->_logicalNext);
pED->deferredTDs--;
}
USBLog(7, "AppleUSBEHCI[%p]::GetTDfromDeferredQueue(%p) returning %p", this, pED, pTD);
return pTD;
}
void
IOUSBControllerV2::ReturnIsochDoneQueueEntry(OSObject *target, thread_call_param_t endpointPtr)
{
IOUSBControllerV2 * me = OSDynamicCast(IOUSBControllerV2, target);
IOUSBControllerIsochEndpoint* pEP = (IOUSBControllerIsochEndpoint*) endpointPtr;
if (!me || !pEP)
return;
me->retain();
me->ReturnIsochDoneQueue(pEP);
me->release();
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 19);
void
IOUSBControllerV2::ReturnIsochDoneQueue(IOUSBControllerIsochEndpoint* pEP)
{
IOUSBControllerIsochListElement *pTD = GetTDfromDoneQueue(pEP);
IOUSBIsocFrame *pFrames = NULL;
IOUSBIsocCompletionAction pHandler;
USBLog(7, "IOUSBControllerV2[%p]::ReturnIsocDoneQueue (%p)", this, pEP);
if (pTD)
{
pFrames = pTD->_pFrames;
}
else
{
USBLog(7, "IOUSBControllerV2[%p]::ReturnIsocDoneQueue - no TDs to return", this);
}
while(pTD)
{
USBLog(7, "IOUSBControllerV2[%p]::ReturnIsocDoneQueue: TD %p", this, pTD);
if( pTD->_completion.action != NULL)
{
pHandler = pTD->_completion.action;
pTD->_completion.action = NULL;
if (pEP->accumulatedStatus == kIOUSBBufferUnderrunErr)
{
USBLog(1, "IOUSBControllerV2[%p]::ReturnIsocDoneQueue - kIOReturnBufferUnderrunErr (PCI issue perhaps)", this);
}
if (pEP->accumulatedStatus == kIOUSBBufferOverrunErr)
{
USBLog(1, "IOUSBControllerV2[%p]::ReturnIsocDoneQueue - kIOReturnBufferOverrunErr (PCI issue perhaps)", this);
}
if ((pEP->accumulatedStatus == kIOReturnOverrun) && (pEP->direction == kUSBIn))
{
USBLog(1, "IOUSBControllerV2[%p]::ReturnIsocDoneQueue - kIOReturnOverrun on IN - device babbling?", this);
}
USBLog(7, "IOUSBControllerV2[%p]::ReturnIsocDoneQueue- TD (%p) calling handler[%p](target: %p, comp.param: %p, status: %p, pFrames: %p)", this, pTD,
pHandler, pTD->_completion.target, pTD->_completion.parameter, (void*)pEP->accumulatedStatus, pFrames);
(*pHandler) (pTD->_completion.target, pTD->_completion.parameter, pEP->accumulatedStatus, pFrames);
_activeIsochTransfers--;
if ( _activeIsochTransfers < 0 )
{
USBLog(1, "IOUSBControllerV2[%p]::ReturnIsocDoneQueue - _activeIsochTransfers went negative (%d). We lost one somewhere", this, (int)_activeIsochTransfers);
}
else if (!_activeIsochTransfers)
requireMaxBusStall(0);
if (pEP->accumulatedStatus != kIOReturnAborted)
{
if (pEP->accumulatedStatus != kIOReturnSuccess && (pEP->accumulatedStatus != kIOReturnUnderrun) )
USBLog(6, "IOUSBControllerV2[%p]::ReturnIsocDoneQueue - resetting status from 0x%x", this, pEP->accumulatedStatus);
pEP->accumulatedStatus = kIOReturnSuccess;
}
pTD->Deallocate(this);
pTD = GetTDfromDoneQueue(pEP);
if (pTD)
pFrames = pTD->_pFrames;
}
else
{
pTD->Deallocate(this);
pTD = GetTDfromDoneQueue(pEP);
}
}
}
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 20);
IODMACommand*
IOUSBControllerV2::GetNewDMACommand()
{
return IODMACommand::withSpecification(kIODMACommandOutputHost64, 32, 0);
}
#define defaultOptionBits 0 // by default we don't need contiguous memory
#define defaultPhysicalMask 0x00000000FFFFF000ULL // be default we require memory 4K aligned memory below 4GB
OSMetaClassDefineReservedUsed(IOUSBControllerV2, 21);
IOReturn
IOUSBControllerV2::GetLowLatencyOptionsAndPhysicalMask(IOOptionBits *pOptionBits, mach_vm_address_t *pPhysicalMask)
{
*pOptionBits = defaultOptionBits;
*pPhysicalMask = defaultPhysicalMask;
return kIOReturnSuccess;
}
OSMetaClassDefineReservedUnused(IOUSBControllerV2, 22);
OSMetaClassDefineReservedUnused(IOUSBControllerV2, 23);
OSMetaClassDefineReservedUnused(IOUSBControllerV2, 24);
OSMetaClassDefineReservedUnused(IOUSBControllerV2, 25);
OSMetaClassDefineReservedUnused(IOUSBControllerV2, 26);
OSMetaClassDefineReservedUnused(IOUSBControllerV2, 27);
OSMetaClassDefineReservedUnused(IOUSBControllerV2, 28);
OSMetaClassDefineReservedUnused(IOUSBControllerV2, 29);