#include <libkern/OSByteOrder.h>
#include <libkern/c++/OSDictionary.h>
#include <libkern/c++/OSData.h>
#include <IOKit/IOMemoryDescriptor.h>
#include <IOKit/IOKitKeys.h>
#include <IOKit/usb/USB.h>
#include <IOKit/usb/IOUSBController.h>
#include <IOKit/usb/IOUSBNub.h>
#include <IOKit/usb/IOUSBPipe.h>
#include <IOKit/usb/IOUSBHubPolicyMaker.h>
#include <IOKit/usb/IOUSBLog.h>
#define super IOService
OSDefineMetaClass( IOUSBNub, IOService )
OSDefineAbstractStructors(IOUSBNub, IOService)
bool
IOUSBNub::USBCompareProperty( OSDictionary * matching, const char * key )
{
OSObject *value;
bool matches = false;
OSObject *myProperty = NULL;
value = matching->getObject( key );
if ( value)
{
myProperty = copyProperty(key);
if (myProperty)
{
matches = value->isEqualTo( myProperty);
myProperty->release();
}
}
else
matches = false;
return matches;
}
bool
IOUSBNub::USBComparePropertyInArray( OSDictionary *matching, const char * arrayName, const char * key, UInt32 * theProductIDThatMatched )
{
OSArray * propertyIDArray = NULL;
OSNumber * registryProperty = NULL;
OSNumber * propertyFromArrayItem = NULL;
bool matches = false;
unsigned int index;
*theProductIDThatMatched = 0;
registryProperty = OSDynamicCast(OSNumber, getProperty(key));
propertyIDArray = OSDynamicCast(OSArray, matching->getObject(arrayName));
if (propertyIDArray && registryProperty)
{
USBLog(7, "%s[%p]::USBComparePropertyInArray - found array with capacity of %d", getName(), this, propertyIDArray->getCount());
for (index = 0; index < propertyIDArray->getCount(); index++)
{
propertyFromArrayItem = OSDynamicCast(OSNumber, propertyIDArray->getObject(index));
if (propertyFromArrayItem)
{
matches = propertyFromArrayItem->isEqualTo( registryProperty);
if (matches)
{
*theProductIDThatMatched = propertyFromArrayItem->unsigned32BitValue();
USBLog(7, "%s[%p]::USBComparePropertyInArray - item %d matched: id = 0x%x", getName(), this, index, (uint32_t) *theProductIDThatMatched);
break;
}
else
{
USBLog(7, "%s[%p]::USBComparePropertyInArray - item %d did not match", getName(), this, index);
}
}
}
}
return matches;
}
bool
IOUSBNub::USBComparePropertyInArrayWithMask( OSDictionary *matching, const char * arrayName, const char * key, const char * maskKey, UInt32 * theProductIDThatMatched )
{
OSArray * propertyIDArray = NULL;
OSNumber * registryProperty = NULL;
OSNumber * propertyFromArrayItem = NULL;
OSNumber * dictionaryMask = NULL;
bool matches = false;
unsigned int index;
*theProductIDThatMatched = 0;
registryProperty = OSDynamicCast(OSNumber, getProperty(key));
propertyIDArray = OSDynamicCast(OSArray, matching->getObject(arrayName));
dictionaryMask = OSDynamicCast(OSNumber, matching->getObject(maskKey));
if (propertyIDArray && registryProperty && dictionaryMask)
{
USBLog(7, "%s[%p]::USBComparePropertyInArrayWithMask - found array with capacity of %d", getName(), this, propertyIDArray->getCount());
for (index = 0; index < propertyIDArray->getCount(); index++)
{
propertyFromArrayItem = OSDynamicCast(OSNumber, propertyIDArray->getObject(index));
if (propertyFromArrayItem)
{
UInt32 registryValue = registryProperty->unsigned32BitValue();
UInt32 arrayValue = propertyFromArrayItem->unsigned32BitValue();
UInt32 mask = dictionaryMask->unsigned32BitValue();
if ( (registryValue & mask) == (arrayValue & mask) )
{
USBLog(7, "%s[%p]::USBComparePropertyInArrayWithMask - 0x%x, 0x%x, mask 0x%x matched", getName(), this, (uint32_t)arrayValue, (uint32_t)registryValue, (uint32_t)mask);
*theProductIDThatMatched = registryValue;
matches = true;
}
}
}
}
return matches;
}
bool
IOUSBNub::IsWildCardMatch( OSDictionary * matching, const char * key )
{
OSString *theString;
bool matches;
theString = OSDynamicCast(OSString, matching->getObject( key ));
if ( theString)
matches = theString->isEqualTo("*");
else
matches = false;
return matches;
}
bool
IOUSBNub::USBComparePropertyWithMask( OSDictionary *matching, const char *key, const char * maskKey )
{
OSNumber * registryProperty = NULL;
OSNumber * dictionaryProperty = NULL;
OSNumber * dictionaryMask = NULL;
registryProperty = OSDynamicCast(OSNumber, getProperty(key));
dictionaryProperty = OSDynamicCast(OSNumber, matching->getObject(key));
dictionaryMask = OSDynamicCast(OSNumber, matching->getObject(maskKey));
if ( registryProperty && dictionaryProperty && dictionaryMask )
{
UInt32 registryValue = registryProperty->unsigned32BitValue();
UInt32 dictionaryValue = dictionaryProperty->unsigned32BitValue();
UInt32 mask = dictionaryMask->unsigned32BitValue();
if ( (registryValue & mask) == (dictionaryValue & mask) )
{
return true;
}
}
return false;
}
void
IOUSBNub::joinPMtree ( IOService * driver )
{
const IORegistryPlane *usbPlane = NULL;
IOUSBHubDevice *hubDevice = NULL;
IOUSBHubPolicyMaker *hubPolicyMaker = NULL;
usbPlane = getPlane(kIOUSBPlane);
if (usbPlane)
{
hubDevice = OSDynamicCast(IOUSBHubDevice, getParentEntry(usbPlane));
if (!hubDevice && getProvider())
{
hubDevice = OSDynamicCast(IOUSBHubDevice, getProvider()->getParentEntry(usbPlane));
}
if (hubDevice)
{
hubPolicyMaker = hubDevice->GetPolicyMaker();
}
else
{
USBError(1, "%s[%p]::joinPMtree - could not find the hub device", getName(), this);
}
}
if (hubPolicyMaker)
{
hubPolicyMaker->joinPMtree(driver);
}
else
{
USBLog(1, "%s[%p]::joinPMtree - no hub policy maker - calling through to super::joinPMtree", getName(), this);
super::joinPMtree(driver);
}
}