IODMAEventSource.cpp [plain text]
#include <IOKit/IODMAEventSource.h>
#include <IOKit/IOService.h>
#include "IOKitKernelInternal.h"
#define super IOEventSource
OSDefineMetaClassAndStructors(IODMAEventSource, IOEventSource);
bool IODMAEventSource::init(OSObject *inOwner,
IOService *inProvider,
Action inCompletion,
Action inNotification,
UInt32 inDMAIndex)
{
IOReturn result;
if (!super::init(inOwner)) return false;
if (inProvider == 0) return false;
dmaProvider = inProvider;
dmaIndex = 0xFFFFFFFF;
dmaCompletionAction = inCompletion;
dmaNotificationAction = inNotification;
dmaController = IODMAController::getController(dmaProvider, inDMAIndex);
if (dmaController == 0) return false;
dmaController->retain();
result = dmaController->initDMAChannel(dmaProvider, this, &dmaIndex, inDMAIndex);
if (result != kIOReturnSuccess) return false;
queue_init(&dmaCommandsCompleted);
dmaCommandsCompletedLock = IOSimpleLockAlloc();
return true;
}
IODMAEventSource *IODMAEventSource::dmaEventSource(OSObject *inOwner,
IOService *inProvider,
Action inCompletion,
Action inNotification,
UInt32 inDMAIndex)
{
IODMAEventSource *dmaES = new IODMAEventSource;
if (dmaES && !dmaES->init(inOwner, inProvider, inCompletion, inNotification, inDMAIndex)) {
dmaES->release();
return 0;
}
return dmaES;
}
IOReturn IODMAEventSource::startDMACommand(IODMACommand *dmaCommand, IODirection direction, IOByteCount byteCount, IOByteCount byteOffset)
{
IOReturn result;
if ((dmaController == 0) || (dmaIndex == 0xFFFFFFFF)) return kIOReturnError;
if (dmaSynchBusy) return kIOReturnBusy;
if (dmaCompletionAction == 0) dmaSynchBusy = true;
result = dmaController->startDMACommand(dmaIndex, dmaCommand, direction, byteCount, byteOffset);
if (result != kIOReturnSuccess) {
dmaSynchBusy = false;
return result;
}
while (dmaSynchBusy) sleepGate(&dmaSynchBusy, THREAD_UNINT);
return kIOReturnSuccess;
}
IOReturn IODMAEventSource::stopDMACommand(bool flush, uint64_t timeout)
{
if ((dmaController == 0) || (dmaIndex == 0xFFFFFFFF)) return kIOReturnError;
return dmaController->stopDMACommand(dmaIndex, flush, timeout);
}
IOReturn IODMAEventSource::queryDMACommand(IODMACommand **dmaCommand, IOByteCount *transferCount, bool waitForIdle)
{
if ((dmaController == 0) || (dmaIndex == 0xFFFFFFFF)) return kIOReturnError;
return dmaController->queryDMACommand(dmaIndex, dmaCommand, transferCount, waitForIdle);
}
IOByteCount IODMAEventSource::getFIFODepth()
{
if ((dmaController == 0) || (dmaIndex == 0xFFFFFFFF)) return kIOReturnError;
return dmaController->getFIFODepth(dmaIndex);
}
bool IODMAEventSource::checkForWork(void)
{
IODMACommand *dmaCommand = NULL;
bool work, again;
IOSimpleLockLock(dmaCommandsCompletedLock);
work = !queue_empty(&dmaCommandsCompleted);
if (work) {
queue_remove_first(&dmaCommandsCompleted, dmaCommand, IODMACommand *, fCommandChain);
again = !queue_empty(&dmaCommandsCompleted);
} else {
again = false;
}
IOSimpleLockUnlock(dmaCommandsCompletedLock);
if (work) {
(*dmaCompletionAction)(owner, this, dmaCommand, dmaCommand->reserved->fStatus, dmaCommand->reserved->fActualByteCount);
}
return again;
}
void IODMAEventSource::completeDMACommand(IODMACommand *dmaCommand)
{
if (dmaCompletionAction != 0) {
IOSimpleLockLock(dmaCommandsCompletedLock);
queue_enter(&dmaCommandsCompleted, dmaCommand, IODMACommand *, fCommandChain);
IOSimpleLockUnlock(dmaCommandsCompletedLock);
signalWorkAvailable();
} else {
dmaSynchBusy = false;
wakeupGate(&dmaSynchBusy, true);
}
}
void IODMAEventSource::notifyDMACommand(IODMACommand *dmaCommand, IOReturn status, IOByteCount actualByteCount)
{
dmaCommand->reserved->fStatus = status;
dmaCommand->reserved->fActualByteCount = actualByteCount;
if (dmaNotificationAction != 0) (*dmaNotificationAction)(owner, this, dmaCommand, status, actualByteCount);
}