#include <AssertMacros.h>
#include <pthread.h>
#include <mach/mach.h>
#include <CoreFoundation/CFRuntime.h>
#include <CoreFoundation/CFBase.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFSerialize.h>
#include <IOKit/hid/IOHIDKeys.h>
#include <IOKit/hid/IOHIDResourceUserClient.h>
#include <IOKit/IODataQueueClient.h>
#include "IOHIDUserDevice.h"
#include <IOKit/IOKitLibPrivate.h>
static IOHIDUserDeviceRef __IOHIDUserDeviceCreate(
CFAllocatorRef allocator,
CFAllocatorContext * context __unused);
static void __IOHIDUserDeviceRelease( CFTypeRef object );
static void __IOHIDUserDeviceRegister(void);
static void __IOHIDUserDeviceQueueCallback(CFMachPortRef port, void *msg, CFIndex size, void *info);
static void __IOHIDUserDeviceHandleReportAsyncCallback(void *refcon, IOReturn result);
typedef struct __IOHIDUserDevice
{
CFRuntimeBase cfBase;
io_service_t service;
io_connect_t connect;
CFDictionaryRef properties;
CFRunLoopRef runLoop;
CFStringRef runLoopMode;
struct {
CFMachPortRef port;
CFRunLoopSourceRef source;
IODataQueueMemory * data;
} queue;
struct {
CFMachPortRef port;
CFRunLoopSourceRef source;
IODataQueueMemory * data;
} async;
struct {
IOHIDUserDeviceReportCallback callback;
void * refcon;
} setReport, getReport;
} __IOHIDUserDevice, *__IOHIDUserDeviceRef;
static const CFRuntimeClass __IOHIDUserDeviceClass = {
0, "IOHIDUserDevice", NULL, NULL, __IOHIDUserDeviceRelease, NULL, NULL, NULL, NULL,
NULL,
NULL
};
static pthread_once_t __deviceTypeInit = PTHREAD_ONCE_INIT;
static CFTypeID __kIOHIDUserDeviceTypeID = _kCFRuntimeNotATypeID;
static mach_port_t __masterPort = MACH_PORT_NULL;
typedef struct __IOHIDDeviceHandleReportAsyncContext {
IOHIDUserDeviceHandleReportAsyncCallback callback;
void * refcon;
} IOHIDDeviceHandleReportAsyncContext;
void __IOHIDUserDeviceRegister(void)
{
__kIOHIDUserDeviceTypeID = _CFRuntimeRegisterClass(&__IOHIDUserDeviceClass);
IOMasterPort(bootstrap_port, &__masterPort);
}
IOHIDUserDeviceRef __IOHIDUserDeviceCreate(
CFAllocatorRef allocator,
CFAllocatorContext * context __unused)
{
IOHIDUserDeviceRef device = NULL;
void * offset = NULL;
uint32_t size;
size = sizeof(__IOHIDUserDevice) - sizeof(CFRuntimeBase);
device = (IOHIDUserDeviceRef)_CFRuntimeCreateInstance(allocator, IOHIDUserDeviceGetTypeID(), size, NULL);
if (!device)
return NULL;
offset = device;
bzero(offset + sizeof(CFRuntimeBase), size);
return device;
}
void __IOHIDUserDeviceRelease( CFTypeRef object )
{
IOHIDUserDeviceRef device = (IOHIDUserDeviceRef)object;
if ( device->queue.data )
{
#if !__LP64__
vm_address_t mappedMem = (vm_address_t)device->queue.data;
#else
mach_vm_address_t mappedMem = (mach_vm_address_t)device->queue.data;
#endif
IOConnectUnmapMemory ( device->connect,
0,
mach_task_self(),
mappedMem);
device->queue.data = NULL;
}
if ( device->queue.source ) {
CFRelease(device->queue.source);
device->queue.source = NULL;
}
if ( device->queue.port ) {
mach_port_t port = CFMachPortGetPort(device->queue.port);
CFMachPortInvalidate(device->queue.port);
CFRelease(device->queue.port);
mach_port_mod_refs(mach_task_self(),
port,
MACH_PORT_RIGHT_RECEIVE,
-1);
device->queue.port = NULL;
}
if ( device->async.source ) {
CFRelease(device->async.source);
device->async.source = NULL;
}
if ( device->async.port ) {
mach_port_t port = CFMachPortGetPort(device->async.port);
CFMachPortInvalidate(device->async.port);
CFRelease(device->async.port);
mach_port_mod_refs(mach_task_self(),
port,
MACH_PORT_RIGHT_RECEIVE,
-1);
device->async.port = NULL;
}
if ( device->properties ) {
CFRelease(device->properties);
device->properties = NULL;
}
if ( device->connect ) {
IOObjectRelease(device->connect);
device->connect = 0;
}
if ( device->service ) {
IOObjectRelease(device->service);
device->service = 0;
}
}
CFTypeID IOHIDUserDeviceGetTypeID(void)
{
if ( _kCFRuntimeNotATypeID == __kIOHIDUserDeviceTypeID )
pthread_once(&__deviceTypeInit, __IOHIDUserDeviceRegister);
return __kIOHIDUserDeviceTypeID;
}
IOHIDUserDeviceRef IOHIDUserDeviceCreate(
CFAllocatorRef allocator,
CFDictionaryRef properties)
{
IOHIDUserDeviceRef device = NULL;
CFDataRef data;
kern_return_t kr;
require(properties, error);
device = __IOHIDUserDeviceCreate(allocator, NULL);
require(device, error);
device->service = IOServiceGetMatchingService(__masterPort, IOServiceMatching( "IOHIDResource" ));
require(device->service, error);
kr = IOServiceOpen(device->service, mach_task_self(), kIOHIDResourceUserClientTypeDevice, &device->connect);
require_noerr(kr, error);
data = IOCFSerialize(properties, 0);
require(data, error);
kr = IOConnectCallStructMethod(device->connect, kIOHIDResourceDeviceUserClientMethodCreate, CFDataGetBytePtr(data), CFDataGetLength(data), NULL, NULL);
CFRelease(data);
require_noerr(kr, error);
return device;
error:
if ( device )
CFRelease(device);
return NULL;
}
void IOHIDUserDeviceScheduleWithRunLoop(IOHIDUserDeviceRef device, CFRunLoopRef runLoop, CFStringRef runLoopMode)
{
if ( !device->queue.data ) {
IOReturn ret;
#if !__LP64__
vm_address_t address = 0;
vm_size_t size = 0;
#else
mach_vm_address_t address = 0;
mach_vm_size_t size = 0;
#endif
ret = IOConnectMapMemory ( device->connect,
0,
mach_task_self(),
&address,
&size,
kIOMapAnywhere );
if (ret != kIOReturnSuccess)
return;
device->queue.data = (IODataQueueMemory *) address;
}
if ( !device->queue.port ) {
mach_port_t port = IODataQueueAllocateNotificationPort();
if ( port != MACH_PORT_NULL ) {
CFMachPortContext context = {0, device, NULL, NULL, NULL};
device->queue.port = CFMachPortCreateWithPort(kCFAllocatorDefault, port, __IOHIDUserDeviceQueueCallback, &context, FALSE);
}
}
if ( !device->queue.source ) {
if ( device->queue.port ) {
device->queue.source = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, device->queue.port, 0);
}
}
if ( !device->async.port ) {
mach_port_t port = MACH_PORT_NULL;
IOReturn ret = IOCreateReceivePort(kOSAsyncCompleteMessageID, &port);
if ( ret == kIOReturnSuccess && port != MACH_PORT_NULL ) {
CFMachPortContext context = {0, device, NULL, NULL, NULL};
device->async.port = CFMachPortCreateWithPort(kCFAllocatorDefault, port, IODispatchCalloutFromCFMessage, &context, FALSE);
}
}
if ( !device->async.source ) {
if ( device->async.port ) {
device->async.source = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, device->async.port, 0);
}
}
CFRunLoopAddSource(runLoop, device->async.source, runLoopMode);
CFRunLoopAddSource(runLoop, device->queue.source, runLoopMode);
IOConnectSetNotificationPort(device->connect, 0, CFMachPortGetPort(device->queue.port), (uintptr_t)NULL);
}
void IOHIDUserDeviceUnscheduleFromRunLoop(IOHIDUserDeviceRef device, CFRunLoopRef runLoop, CFStringRef runLoopMode)
{
if ( !device->queue.port )
return;
IOConnectSetNotificationPort(device->connect, 0, MACH_PORT_NULL, (uintptr_t)NULL);
CFRunLoopRemoveSource(runLoop, device->queue.source, runLoopMode);
CFRunLoopRemoveSource(runLoop, device->async.source, runLoopMode);
}
void IOHIDUserDeviceRegisterGetReportCallback(IOHIDUserDeviceRef device, IOHIDUserDeviceReportCallback callback, void * refcon)
{
device->getReport.callback = callback;
device->getReport.refcon = refcon;
}
void IOHIDUserDeviceRegisterSetReportCallback(IOHIDUserDeviceRef device, IOHIDUserDeviceReportCallback callback, void * refcon)
{
device->setReport.callback = callback;
device->setReport.refcon = refcon;
}
#ifndef min
#define min(a, b) \
((a < b) ? a:b)
#endif
void __IOHIDUserDeviceQueueCallback(CFMachPortRef port __unused, void *msg __unused, CFIndex size __unused, void *info)
{
IOHIDUserDeviceRef device = (IOHIDUserDeviceRef)info;
if ( !device->queue.data )
return;
IODataQueueEntry * nextEntry;
uint32_t dataSize;
while ((nextEntry = IODataQueuePeek(device->queue.data))) {
IOHIDResourceDataQueueHeader * header = (IOHIDResourceDataQueueHeader*)&(nextEntry->data);
uint64_t response[kIOHIDResourceUserClientResponseIndexCount] = {kIOReturnUnsupported,header->token};
uint8_t * responseReport = NULL;
uint32_t responseLength = 0;
if ( header->direction == kIOHIDResourceReportDirectionOut ) {
CFIndex reportLength = min(header->length, (nextEntry->size - sizeof(IOHIDResourceDataQueueHeader)));
uint8_t * report = ((uint8_t*)header)+sizeof(IOHIDResourceDataQueueHeader);
if ( device->setReport.callback )
response[kIOHIDResourceUserClientResponseIndexResult] = (*device->setReport.callback)(device->setReport.refcon, header->type, header->reportID, report, reportLength);
}
else if ( header->direction == kIOHIDResourceReportDirectionIn ) {
responseReport = (uint8_t *)malloc(header->length);
responseLength = header->length;
if ( device->setReport.callback )
response[kIOHIDResourceUserClientResponseIndexResult] = (*device->getReport.callback)(device->getReport.refcon, header->type, header->reportID, responseReport, responseLength);
}
IOConnectCallMethod(device->connect, kIOHIDResourceDeviceUserClientMethodPostReportResponse, response, sizeof(response)/sizeof(uint64_t), responseReport, responseLength, NULL, NULL, NULL, NULL);
if ( responseReport )
free(responseReport);
dataSize = 0;
IODataQueueDequeue(device->queue.data, NULL, &dataSize);
}
}
void __IOHIDUserDeviceHandleReportAsyncCallback(void *refcon, IOReturn result)
{
IOHIDDeviceHandleReportAsyncContext *pContext = (IOHIDDeviceHandleReportAsyncContext *)refcon;
if (pContext->callback)
pContext->callback(pContext->refcon, result);
free(pContext);
}
IOReturn IOHIDUserDeviceHandleReportAsync(
IOHIDUserDeviceRef device,
uint8_t * report,
CFIndex reportLength,
IOHIDUserDeviceHandleReportAsyncCallback callback,
void * refcon)
{
IOHIDDeviceHandleReportAsyncContext *pContext = malloc(sizeof(IOHIDDeviceHandleReportAsyncContext));
if (!pContext)
return kIOReturnNoMemory;
pContext->callback = callback;
pContext->refcon = refcon;
mach_port_t wakePort = MACH_PORT_NULL;
uint64_t asyncRef[kOSAsyncRef64Count];
wakePort = CFMachPortGetPort(device->async.port);
asyncRef[kIOAsyncCalloutFuncIndex] = (uint64_t)(uintptr_t)__IOHIDUserDeviceHandleReportAsyncCallback;
asyncRef[kIOAsyncCalloutRefconIndex] = (uint64_t)(uintptr_t)pContext;
return IOConnectCallAsyncStructMethod(device->connect, kIOHIDResourceDeviceUserClientMethodHandleReport, wakePort, asyncRef, kOSAsyncRef64Count, report, reportLength, NULL, NULL);
}
IOReturn IOHIDUserDeviceHandleReport(
IOHIDUserDeviceRef device,
uint8_t * report,
CFIndex reportLength)
{
return IOConnectCallStructMethod(device->connect, kIOHIDResourceDeviceUserClientMethodHandleReport, report, reportLength, NULL, NULL);
}