IOHIDLibUserClient.cpp [plain text]
#include <sys/systm.h>
#include <IOKit/IOCommandGate.h>
#include <IOKit/IOMemoryDescriptor.h>
#include <IOKit/IOService.h>
#include <IOKit/IOSyncer.h>
#include <IOKit/IOWorkLoop.h>
#include "IOHIDLibUserClient.h"
#include "IOHIDDevice.h"
#include "IOHIDEventQueue.h"
#define super IOUserClient
OSDefineMetaClassAndStructors(IOHIDLibUserClient, IOUserClient);
const IOExternalMethod IOHIDLibUserClient::
sMethods[kIOHIDLibUserClientNumCommands] = {
{ 0,
(IOMethod) &IOHIDLibUserClient::open,
kIOUCScalarIScalarO,
0,
0
},
{ 0,
(IOMethod) &IOHIDLibUserClient::close,
kIOUCScalarIScalarO,
0,
0
},
{ 0,
(IOMethod) &IOHIDLibUserClient::createQueue,
kIOUCScalarIScalarO,
2,
1
},
{ 0,
(IOMethod) &IOHIDLibUserClient::disposeQueue,
kIOUCScalarIScalarO,
1,
0
},
{ 0,
(IOMethod) &IOHIDLibUserClient::addElementToQueue,
kIOUCScalarIScalarO,
3,
0
},
{ 0,
(IOMethod) &IOHIDLibUserClient::removeElementFromQueue,
kIOUCScalarIScalarO,
2,
0
},
{ 0,
(IOMethod) &IOHIDLibUserClient::queueHasElement,
kIOUCScalarIScalarO,
2,
1
},
{ 0,
(IOMethod) &IOHIDLibUserClient::startQueue,
kIOUCScalarIScalarO,
1,
0
},
{ 0,
(IOMethod) &IOHIDLibUserClient::stopQueue,
kIOUCScalarIScalarO,
1,
0
},
};
const IOExternalAsyncMethod IOHIDLibUserClient::
sAsyncMethods[kIOHIDLibUserClientNumAsyncCommands] = {
{ 0,
(IOAsyncMethod) &IOHIDLibUserClient::setAsyncPort,
kIOUCScalarIScalarO,
0,
0
}
};
bool IOHIDLibUserClient::
initWithTask(task_t owningTask, void * , UInt32 )
{
if (!super::init())
return false;
fClient = owningTask;
task_reference (fClient);
return true;
}
IOReturn IOHIDLibUserClient::clientClose(void)
{
if (fClient) {
task_deallocate(fClient);
fClient = 0;
}
if (fNub) {
fNub->close(this);
detach(fNub);
fNub = 0;
}
return kIOReturnSuccess;
}
bool IOHIDLibUserClient::start(IOService *provider)
{
if (!super::start(provider))
return false;
fNub = OSDynamicCast(IOHIDDevice, provider);
if (!fNub)
return false;
return true;
}
IOExternalMethod *IOHIDLibUserClient::
getTargetAndMethodForIndex(IOService **target, UInt32 index)
{
if (index < (UInt32) kIOHIDLibUserClientNumCommands)
{
*target = this;
return (IOExternalMethod *) &sMethods[index];
}
else
return 0;
}
IOExternalAsyncMethod * IOHIDLibUserClient::
getAsyncTargetAndMethodForIndex(IOService **target, UInt32 index)
{
if (index < (UInt32) kIOHIDLibUserClientNumAsyncCommands)
{
*target = this;
return (IOExternalAsyncMethod *) &sAsyncMethods[index];
}
else
return 0;
}
IOReturn IOHIDLibUserClient::
setAsyncPort(OSAsyncReference asyncRef, void *, void *, void *,
void *, void *, void *)
{
fWakePort = (mach_port_t) asyncRef[0];
return kIOReturnSuccess;
}
IOReturn IOHIDLibUserClient::
open(void *, void *, void *, void *, void *, void *)
{
IOReturn res = kIOReturnSuccess;
IOWorkLoop *wl;
if (!fNub->open(this))
return kIOReturnExclusiveAccess;
wl = getWorkLoop();
if (!wl) {
res = kIOReturnNoResources;
goto abortOpen;
}
fGate = IOCommandGate::commandGate(this);
if (!fGate) {
res = kIOReturnNoMemory;
goto abortOpen;
}
wl->retain();
wl->addEventSource(fGate);
return kIOReturnSuccess;
abortOpen:
if (fGate) {
wl->removeEventSource(fGate);
wl->release();
fGate->release();
fGate = 0;
}
fNub->close(this);
return res;
}
IOReturn IOHIDLibUserClient::
close(void *, void *, void *, void *, void *, void *gated)
{
if ( ! (bool) gated ) {
IOReturn res;
IOWorkLoop *wl;
res = fGate->runAction(closeAction);
wl = fGate->getWorkLoop();
wl->removeEventSource(fGate);
wl->release();
fGate->release();
fGate = 0;
return res;
}
else {
fNub->close(this);
return kIOReturnSuccess;
}
return kIOReturnSuccess;
}
IOReturn IOHIDLibUserClient::closeAction
(OSObject *self, void *, void *, void *, void *)
{
IOHIDLibUserClient *me = (IOHIDLibUserClient *) self;
return me->close(0, 0, 0, 0, 0, (void *) true);
}
IOReturn IOHIDLibUserClient::
clientMemoryForType ( UInt32 type,
IOOptionBits * options,
IOMemoryDescriptor ** memory )
{
IOReturn ret = kIOReturnNoMemory;
IOMemoryDescriptor * memoryToShare = NULL;
if (type == IOHIDLibUserClientElementValuesType)
{
memoryToShare = fNub->getMemoryWithCurrentElementValues();
}
else
{
IOHIDEventQueue * queue = (IOHIDEventQueue *) type;
memoryToShare = queue->getMemoryDescriptor();
}
if (memoryToShare)
{
memoryToShare->retain();
ret = kIOReturnSuccess;
}
*options = 0;
*memory = memoryToShare;
return ret;
}
IOReturn IOHIDLibUserClient::
createQueue(void * vInFlags, void * vInDepth, void * vOutQueue, void *, void *, void * gated)
{
UInt32 flags = (UInt32) vInFlags;
UInt32 depth = (UInt32) vInDepth;
void ** outQueue = (void **) vOutQueue;
IOHIDEventQueue * eventQueue = IOHIDEventQueue::withEntries (depth+1,
sizeof(IOHIDElementValue) + sizeof(void *));
*outQueue = eventQueue;
return kIOReturnSuccess;
}
IOReturn IOHIDLibUserClient::
disposeQueue(void * vInQueue, void *, void *, void *, void *, void * gated)
{
IOReturn ret = kIOReturnSuccess;
IOHIDEventQueue * queue = (IOHIDEventQueue *) vInQueue;
ret = fNub->stopEventDelivery (queue);
queue->release();
return kIOReturnSuccess;
}
IOReturn IOHIDLibUserClient::
addElementToQueue(void * vInQueue, void * vInElementCookie,
void * vInFlags, void *, void *, void * gated)
{
IOReturn ret = kIOReturnSuccess;
IOHIDEventQueue * queue = (IOHIDEventQueue *) vInQueue;
IOHIDElementCookie elementCookie = (IOHIDElementCookie) vInElementCookie;
UInt32 flags = (UInt32) vInFlags;
ret = fNub->startEventDelivery (queue, elementCookie);
return ret;
}
IOReturn IOHIDLibUserClient::
removeElementFromQueue (void * vInQueue, void * vInElementCookie,
void *, void *, void *, void * gated)
{
IOReturn ret = kIOReturnSuccess;
IOHIDEventQueue * queue = (IOHIDEventQueue *) vInQueue;
IOHIDElementCookie elementCookie = (IOHIDElementCookie) vInElementCookie;
ret = fNub->stopEventDelivery (queue, elementCookie);
return ret;
}
IOReturn IOHIDLibUserClient::
queueHasElement (void * vInQueue, void * vInElementCookie,
void * vOutHasElement, void *, void *, void * gated)
{
IOReturn ret = kIOReturnSuccess;
IOHIDEventQueue * queue = (IOHIDEventQueue *) vInQueue;
IOHIDElementCookie elementCookie = (IOHIDElementCookie) vInElementCookie;
Boolean * outHasElement = (Boolean *) vOutHasElement;
bool hasElement = false;
ret = fNub->checkEventDelivery (queue, elementCookie, &hasElement);
*outHasElement = hasElement;
return ret;
}
IOReturn IOHIDLibUserClient::
startQueue (void * vInQueue, void *, void *,
void *, void *, void * gated)
{
IOHIDEventQueue * queue = (IOHIDEventQueue *) vInQueue;
queue->start();
return kIOReturnSuccess;
}
IOReturn IOHIDLibUserClient::
stopQueue (void * vInQueue, void *, void *,
void *, void *, void * gated)
{
IOHIDEventQueue * queue = (IOHIDEventQueue *) vInQueue;
queue->stop();
return kIOReturnSuccess;
}