iokit.cpp revision 344eea599dbaaf2882b90bc6e8f25d02e7195657
/* $Id$ */
/** @file
* Main - Darwin IOKit Routines.
*
* Because IOKit makes use of COM like interfaces, it does not mix very
* simpler C interface.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License as published by the Free Software Foundation,
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
* distribution. VirtualBox OSE is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY of any kind.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_MAIN
#include <IOKit/IOKitLib.h>
#include <mach/mach_error.h>
#ifdef VBOX_WITH_USB
# include <IOKit/IOCFPlugIn.h>
#endif
#include "iokit.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** An attempt at catching reference leaks. */
#define MY_CHECK_CREFS(cRefs) do { AssertMsg(cRefs < 25, ("%ld\n", cRefs)); NOREF(cRefs); } while (0)
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The IO Master Port. */
/**
* Lazily opens the master port.
*
* @returns true if the port is open, false on failure (very unlikely).
*/
static bool darwinOpenMasterPort(void)
{
if (!g_MasterPort)
{
}
return true;
}
#ifdef VBOX_WITH_USB
/**
* Gets an unsigned 8-bit integer value.
*
* @param DictRef The dictionary.
* @param KeyStrRef The key name.
* @param pu8 Where to store the key value.
*/
{
if (ValRef)
{
return true;
}
*pu8 = 0;
return false;
}
/**
* Gets an unsigned 16-bit integer value.
*
* @param DictRef The dictionary.
* @param KeyStrRef The key name.
* @param pu16 Where to store the key value.
*/
{
if (ValRef)
{
return true;
}
*pu16 = 0;
return false;
}
/**
* Gets an unsigned 32-bit integer value.
*
* @param DictRef The dictionary.
* @param KeyStrRef The key name.
* @param pu32 Where to store the key value.
*/
{
if (ValRef)
{
return true;
}
*pu32 = 0;
return false;
}
/**
* Gets an unsigned 64-bit integer value.
*
* @param DictRef The dictionary.
* @param KeyStrRef The key name.
* @param pu64 Where to store the key value.
*/
{
if (ValRef)
{
return true;
}
*pu64 = 0;
return false;
}
/**
* Gets string value, converted to UTF-8 and put in a IPRT string buffer.
*
* @param DictRef The dictionary.
* @param KeyStrRef The key name.
* @param ppsz Where to store the key value. Free with RTStrFree. Set to NULL on failure.
*/
{
if (ValRef)
{
char szBuf[512];
{
if (*ppsz)
return true;
}
}
return false;
}
#if 1 /* dumping disabled */
# define DARWIN_IOKIT_LOG(a) Log(a)
# define DARWIN_IOKIT_LOG_FLUSH() do {} while (0)
# define DARWIN_IOKIT_DUMP_OBJ(o) do {} while (0)
#else
# if 0
# define DARWIN_IOKIT_LOG(a) RTPrintf a
# else
# define DARWIN_IOKIT_LOG(a) RTLogPrintf a
# define DARWIN_IOKIT_LOG(a) RTLogFlush()
# endif
# define DARWIN_IOKIT_DUMP_OBJ(o) darwinDumpObj(o)
/**
* Callback for dumping a dictionary key.
*
* @param pvKey The key name.
* @param pvValue The key value
* @param pvUser The recursion depth.
*/
{
/* display the key name. */
/* display the value type */
/* display the value */
if (Type == CFDictionaryGetTypeID())
{
DARWIN_IOKIT_LOG(("dictionary] =\n"
CFDictionaryApplyFunction((CFDictionaryRef)pvValue, darwinDumpDictCallback, (void *)((uintptr_t)pvUser + 4));
}
else if (Type == CFNumberGetTypeID())
{
union
{
char ch;
short s;
int i;
long l;
long long ll;
float rf;
double rd;
} u;
memset(&u, 0, sizeof(u));
{
{
case kCFNumberSInt16Type: DARWIN_IOKIT_LOG(("SInt16] = %RI16 (%#RX16)\n", NumType, u.s16, u.s16)); break;
case kCFNumberSInt32Type: DARWIN_IOKIT_LOG(("SInt32] = %RI32 (%#RX32)\n", NumType, u.s32, u.s32)); break;
case kCFNumberSInt64Type: DARWIN_IOKIT_LOG(("SInt64] = %RI64 (%#RX64)\n", NumType, u.s64, u.s64)); break;
case kCFNumberLongLongType: DARWIN_IOKIT_LOG(("long long] = %lld (%#llx)\n", NumType, u.ll, u.ll)); break;
case kCFNumberCFIndexType: DARWIN_IOKIT_LOG(("CFIndex] = %lld (%#llx)\n", NumType, (long long)u.iCF, (long long)u.iCF)); break;
break;
}
}
else
DARWIN_IOKIT_LOG(("number] = CFNumberGetValue failed\n"));
}
else if (Type == CFBooleanGetTypeID())
else if (Type == CFStringGetTypeID())
{
DARWIN_IOKIT_LOG(("string] = "));
}
else
}
/**
* Dumps a dictionary to the log.
*
* @param DictRef The dictionary to dump.
*/
{
}
/**
* Dumps an I/O kit registry object and all it children.
* @param Object The object to dump.
* @param cIndents The number of indents to use.
*/
{
static io_string_t s_szPath;
if (krc != KERN_SUCCESS)
if (krc == KERN_SUCCESS)
{
}
/*
* Children.
*/
if (krc == KERN_SUCCESS)
{
{
}
}
else
}
/**
* Dumps an I/O kit registry object and all it children.
* @param Object The object to dump.
*/
{
darwinDumpObjInt(Object, 0);
}
#endif
/**
* Notification data created by DarwinSubscribeUSBNotifications, used by
* the callbacks and finally freed by DarwinUnsubscribeUSBNotifications.
*/
typedef struct DARWINUSBNOTIFY
{
/** The notification port.
* It's shared between the notification callbacks. */
/** The run loop source for NotifyPort. */
/** The attach notification iterator. */
/** The 2nd attach notification iterator. */
/** The detach notificaiton iterator. */
/**
* Run thru an interrator.
*
* The docs says this is necessary to start getting notifications,
* so this function is called in the callbacks and right after
* registering the notification.
*
* @param pIterator The iterator reference.
*/
{
{
}
}
/**
* Callback for the 1st attach notification.
*
* @param pvNotify Our data.
* @param NotifyIterator The notification iterator.
*/
{
DARWIN_IOKIT_LOG(("USB Attach Notification1\n"));
}
/**
* Callback for the 2nd attach notification.
*
* @param pvNotify Our data.
* @param NotifyIterator The notification iterator.
*/
{
DARWIN_IOKIT_LOG(("USB Attach Notification2\n"));
}
/**
* Callback for the detach notifications.
*
* @param pvNotify Our data.
* @param NotifyIterator The notification iterator.
*/
{
DARWIN_IOKIT_LOG(("USB Detach Notification\n"));
}
/**
* Subscribes the run loop to USB notification events relevant to
*
* The source mode for these events is defined as VBOX_IOKIT_MODE_STRING
* so that the caller can listen to events from this mode only and
* re-evalutate the list of attached devices whenever an event arrives.
*
* @returns opaque for passing to the unsubscribe function. If NULL
* something unexpectedly failed during subscription.
*/
void *DarwinSubscribeUSBNotifications(void)
{
/*
* Create the notification port, bake it into a runloop source which we
* then add to our run loop.
*/
if (pNotify->NotifyPort)
{
if (pNotify->NotifyRLSrc)
{
/*
* Create the notifcation callbacks.
*/
if (rc == KERN_SUCCESS)
{
if (rc == KERN_SUCCESS)
{
{
return pNotify;
}
}
}
}
}
return NULL;
}
/**
* Unsubscribe the run loop from USB notification subscribed to
* by DarwinSubscribeUSBNotifications.
*
* @param pvOpaque The return value from DarwinSubscribeUSBNotifications.
*/
void DarwinUnsubscribeUSBNotifications(void *pvOpaque)
{
if (!pNotify)
return;
}
/**
* Worker function for DarwinGetUSBDevices() that tries to figure out
* what state the device is in.
*
* This is mostly a matter of distinguishing between devices that nobody
* uses, devices that can be seized and devices that cannot be grabbed.
*
* @param pCur The USB device data.
* @param USBDevice The USB device object.
* @param PropsRef The USB device properties.
*/
static void darwinDeterminUSBDeviceState(PUSBDEVICE pCur, io_object_t USBDevice, CFMutableDictionaryRef PropsRef)
{
///@todo
}
/**
* Enumerate the USB devices returning a FIFO of them.
*
* @returns Pointer to the head.
* USBProxyService::freeDevice is expected to free each of the list elements.
*/
{
//DARWIN_IOKIT_LOG(("DarwinGetUSBDevices\n"));
/*
* Create a matching dictionary for searching for USB Devices in the IOKit.
*/
/*
* Perform the search and get a collection of USB Device back.
*/
/*
* Enumerate the USB Devices.
*/
unsigned i = 0;
{
//DARWIN_IOKIT_DUMP_OBJ(USBDevice);
/*
* Query the device properties from the registry.
*
* We could alternatively use the device and such, but that will be
* slower and we would have to resort to the registry for the three
* string anyway.
*/
kern_return_t krc = IORegistryEntryCreateCFProperties(USBDevice, &PropsRef, kCFAllocatorDefault, kNilOptions);
if (krc == KERN_SUCCESS)
{
bool fOk = false;
do /* loop for breaking out of on failure. */
{
AssertBreak(pCur,);
/*
* Mandatory
*/
pCur->enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE; /* just a default, we'll try harder in a bit. */
/* skip hubs */
break;
char szAddress[64];
/*
* Optional.
* There are some nameless device in the iMac, apply names to them.
*/
if ( !pCur->pszManufacturer
if ( !pCur->pszProduct
#if 0 /* leave the remainder as zero for now. */
/*
* Create a plugin interface for the service and query its USB Device interface.
*/
if (rc == kIOReturnSuccess)
{
{
/** @todo enumerate configurations and interfaces if we actually need them. */
//IOReturn (*GetNumberOfConfigurations)(void *self, UInt8 *numConfig);
//IOReturn (*GetConfigurationDescriptorPtr)(void *self, UInt8 configIndex, IOUSBConfigurationDescriptorPtr *desc);
//IOReturn (*CreateInterfaceIterator)(void *self, IOUSBFindInterfaceRequest *req, io_iterator_t *iter);
}
}
#endif
/*
* Try determin the state.
*/
/*
* We're good. Link the device.
*/
if (pTail)
else
fOk = true;
} while (0);
/* cleanup on failure / skipped device. */
}
else
i++;
}
//DARWIN_IOKIT_LOG_FLUSH();
/*
* Some post processing. There are a couple of things we have to
* make 100% sure about, and that is that the (Apple) keyboard
* and mouse most likely to be in use by the user aren't available
* for capturing. If there is no Apple mouse or keyboard we'll
* take the first one from another vendor.
*/
/* As it turns out, the HID service will take all keyboards and mice
and we're not currently able to seize them. */
{
/*
* we don't have interface info yet so that might be a bit tricky.
*/
if ( ( !pKeyboard
&& pCur->pszProduct
else if ( ( !pMouse
&& pCur->pszProduct
)
}
{
/** @todo examin interfaces */
}
if (pKeyboard)
if (pMouse)
return pHead;
}
#endif /* VBOX_WITH_USB */
/**
* Enumerate the DVD drives returning a FIFO of device name strings.
*
* @returns Pointer to the head.
* The caller is responsible for calling RTMemFree() on each of the nodes.
*/
PDARWINDVD DarwinGetDVDDrives(void)
{
/*
* Create a matching dictionary for searching for DVD services in the IOKit.
*
* [If I understand this correctly, plain CDROMs doesn't show up as
* IODVDServices. Too keep things simple, we will only support DVDs
* until somebody complains about it and we get hardware to test it on.
* (Unless I'm much mistaken, there aren't any (orignal) intel macs with
* plain cdroms.)]
*/
/*
* Perform the search and get a collection of DVD services.
*/
/*
* Enumerate the DVD services.
* (This enumeration must be identical to the one performed in DrvHostBase.cpp.)
*/
unsigned i = 0;
{
/*
* Get the properties we use to identify the DVD drive.
*
* While there is a (weird 12 byte) GUID, it isn't persistent
* accross boots. So, we have to use a combination of the
* vendor name and product name properties with an optional
* sequence number for identification.
*/
kern_return_t krc = IORegistryEntryCreateCFProperties(DVDService, &PropsRef, kCFAllocatorDefault, kNilOptions);
if (krc == KERN_SUCCESS)
{
/* Get the Device Characteristics dictionary. */
CFDictionaryRef DevCharRef = (CFDictionaryRef)CFDictionaryGetValue(PropsRef, CFSTR(kIOPropertyDeviceCharacteristicsKey));
if (DevCharRef)
{
/* The vendor name. */
char szVendor[128];
if ( ValueRef
else
*pszVendor = '\0';
/* The product name. */
char szProduct[128];
char *pszProduct = &szProduct[0];
if ( ValueRef
else
*pszProduct = '\0';
/* Construct the name and check for duplicates. */
if (*pszVendor || *pszProduct)
{
if (*pszVendor && *pszProduct)
else
{
{
if (*pszVendor && *pszProduct)
else
break;
}
}
}
else
/* Create the device. */
if (pNew)
{
if (pTail)
else
}
}
}
else
i++;
}
return pHead;
}