iokit.cpp revision 6bcf940eb67d473f35dfbffbccc3b3d9c577f5ae
/** $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 InnoTek Systemberatung 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 *
*******************************************************************************/
#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;
}
/**
* 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 two attach notifications.
*
* @param pvNotify Our data.
* @param NotifyIterator The notification iterator.
*/
{
}
/**
* Callback for the detach notifications.
*
* @param pvNotify Our data.
* @param NotifyIterator The notification iterator.
*/
{
}
/**
* 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;
}
/**
* 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.
*/
{
/*
* 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;
{
/*
* 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
*/
/* 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
/*
* We're good. Link the device.
*/
if (pTail)
else
fOk = true;
} while (0);
/* cleanup on failure / skipped device. */
{
/** @todo */
}
}
else
i++;
}
/*
* 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.
*/
{
/*
* 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;
}