+ if(featuresDictLock) IOLockLock(featuresDictLock);
+
+ OSDictionary *features =
+ (OSDictionary *) getProperty(kRootDomainSupportedFeatures);
+
+ if ( features && OSDynamicCast(OSDictionary, features) )
+ {
+ // Any modifications to the dictionary are made to the copy to prevent
+ // races & crashes with userland clients. Dictionary updated
+ // automically later.
+ features = OSDictionary::withDictionary(features);
+ } else {
+ features = NULL;
+ ret = kIOReturnNotFound;
+ goto exit;
+ }
+
+ // We iterate 'features' dictionary looking for an entry tagged
+ // with 'removeFeatureID'. If found, we remove it from our tracking
+ // structures and notify the OS via a general interest message.
+
+ dictIterator = OSCollectionIterator::withCollection(features);
+ if(!dictIterator) {
+ goto exit;
+ }
+
+ while( (dictKey = OSDynamicCast(OSSymbol, dictIterator->getNextObject())) )
+ {
+ osObj = features->getObject(dictKey);
+
+ // Each Feature is either tracked by an OSNumber
+ if( osObj && (numberMember = OSDynamicCast(OSNumber, osObj)) )
+ {
+ feature_value = numberMember->unsigned32BitValue();
+ feature_id = (uint16_t)(feature_value >> 16);
+
+ if( feature_id == (uint16_t)removeFeatureID )
+ {
+ // Remove this node
+ features->removeObject(dictKey);
+ madeAChange = true;
+ break;
+ }
+
+ // Or tracked by an OSArray of OSNumbers
+ } else if( osObj && (arrayMember = OSDynamicCast(OSArray, osObj)) )
+ {
+ unsigned int arrayCount = arrayMember->getCount();
+
+ for(unsigned int i=0; i<arrayCount; i++)
+ {
+ osNum = OSDynamicCast(OSNumber, arrayMember->getObject(i));
+ if(!osNum) {
+ continue;
+ }
+
+ feature_value = osNum->unsigned32BitValue();
+ feature_id = (uint16_t)(feature_value >> 16);
+
+ if( feature_id == (uint16_t)removeFeatureID )
+ {
+ // Remove this node
+ if( 1 == arrayCount ) {
+ // If the array only contains one element, remove
+ // the whole thing.
+ features->removeObject(dictKey);
+ } else {
+ // Otherwise just remove the element in question.
+ arrayMember->removeObject(i);
+ }
+
+ madeAChange = true;
+ break;
+ }
+ }
+ }
+ }
+
+
+ dictIterator->release();
+
+ if( madeAChange )
+ {
+ ret = kIOReturnSuccess;
+
+ setProperty(kRootDomainSupportedFeatures, features);
+
+ // Notify EnergySaver and all those in user space so they might
+ // re-populate their feature specific UI
+ if(pmPowerStateQueue) {
+ pmPowerStateQueue->featureChangeOccurred(
+ kIOPMMessageFeatureChange, this);
+ }
+ } else {
+ ret = kIOReturnNotFound;
+ }
+
+exit:
+ if(features) features->release();
+ if(featuresDictLock) IOLockUnlock(featuresDictLock);
+ return ret;
+}
+
+
+// **********************************************************************************
+// unIdleDevice
+//
+// Enqueues unidle event to be performed later in a serialized context.
+//
+// **********************************************************************************
+void IOPMrootDomain::unIdleDevice( IOService *theDevice, unsigned long theState )
+{
+ if(pmPowerStateQueue)
+ pmPowerStateQueue->unIdleOccurred(theDevice, theState);
+}
+
+// **********************************************************************************
+// announcePowerSourceChange
+//
+// Notifies "interested parties" that the batteries have changed state
+//
+// **********************************************************************************
+void IOPMrootDomain::announcePowerSourceChange( void )
+{
+ IORegistryEntry *_batteryRegEntry = (IORegistryEntry *) getProperty("BatteryEntry");
+
+ // (if possible) re-publish power source state under IOPMrootDomain;
+ // only do so if the battery controller publishes an IOResource
+ // defining battery location. Called from ApplePMU battery driver.
+
+ if(_batteryRegEntry)
+ {
+ OSArray *batt_info;
+ batt_info = (OSArray *) _batteryRegEntry->getProperty(kIOBatteryInfoKey);
+ if(batt_info)
+ setProperty(kIOBatteryInfoKey, batt_info);
+ }
+
+}
+
+
+// *****************************************************************************
+// setPMSetting (private)
+//
+// Internal helper to relay PM settings changes from user space to individual
+// drivers. Should be called only by IOPMrootDomain::setProperties.
+//
+// *****************************************************************************
+IOReturn IOPMrootDomain::setPMSetting(
+ const OSSymbol *type,
+ OSObject *obj)
+{
+ OSArray *arr = NULL;
+ PMSettingObject *p_obj = NULL;
+ int count;
+ int i;
+
+ if(NULL == type) return kIOReturnBadArgument;
+
+ IORecursiveLockLock(settingsCtrlLock);
+
+ fPMSettingsDict->setObject(type, obj);
+
+ arr = (OSArray *)settingsCallbacks->getObject(type);
+ if(NULL == arr) goto exit;
+ count = arr->getCount();
+ for(i=0; i<count; i++) {
+ p_obj = (PMSettingObject *)OSDynamicCast(PMSettingObject, arr->getObject(i));
+ if(p_obj) p_obj->setPMSetting(type, obj);
+ }
+
+exit:
+ IORecursiveLockUnlock(settingsCtrlLock);
+ return kIOReturnSuccess;
+}
+
+// *****************************************************************************
+// copyPMSetting (public)
+//
+// Allows kexts to safely read setting values, without being subscribed to
+// notifications.
+//
+// *****************************************************************************
+OSObject * IOPMrootDomain::copyPMSetting(
+ OSSymbol *whichSetting)
+{
+ OSObject *obj = NULL;
+
+ if(!whichSetting) return NULL;
+
+ IORecursiveLockLock(settingsCtrlLock);
+ obj = fPMSettingsDict->getObject(whichSetting);
+ if(obj) {
+ obj->retain();
+ }
+ IORecursiveLockUnlock(settingsCtrlLock);
+
+ return obj;
+}
+
+// *****************************************************************************
+// registerPMSettingController (public)
+//
+// direct wrapper to registerPMSettingController with uint32_t power source arg
+// *****************************************************************************
+IOReturn IOPMrootDomain::registerPMSettingController(
+ const OSSymbol * settings[],
+ IOPMSettingControllerCallback func,
+ OSObject *target,
+ uintptr_t refcon,
+ OSObject **handle)
+{
+ return registerPMSettingController(
+ settings,
+ (kIOPMSupportedOnAC | kIOPMSupportedOnBatt | kIOPMSupportedOnUPS),
+ func, target, refcon, handle);
+}
+
+// *****************************************************************************
+// registerPMSettingController (public)
+//
+// Kexts may register for notifications when a particular setting is changed.
+// A list of settings is available in IOPM.h.
+// Arguments:
+// * settings - An OSArray containing OSSymbols. Caller should populate this
+// array with a list of settings caller wants notifications from.
+// * func - A C function callback of the type IOPMSettingControllerCallback
+// * target - caller may provide an OSObject *, which PM will pass as an
+// target to calls to "func"
+// * refcon - caller may provide an void *, which PM will pass as an
+// argument to calls to "func"
+// * handle - This is a return argument. We will populate this pointer upon
+// call success. Hold onto this and pass this argument to
+// IOPMrootDomain::deRegisterPMSettingCallback when unloading your kext
+// Returns:
+// kIOReturnSuccess on success
+// *****************************************************************************
+IOReturn IOPMrootDomain::registerPMSettingController(
+ const OSSymbol * settings[],
+ uint32_t supportedPowerSources,
+ IOPMSettingControllerCallback func,
+ OSObject *target,
+ uintptr_t refcon,
+ OSObject **handle)
+{
+ PMSettingObject *pmso = NULL;
+ OSArray *list = NULL;
+ IOReturn ret = kIOReturnSuccess;
+ int i;
+
+ if( NULL == settings ||
+ NULL == func ||
+ NULL == handle)
+ {
+ return kIOReturnBadArgument;
+ }
+
+ pmso = PMSettingObject::pmSettingObject(
+ (IOPMrootDomain *)this, func, target,
+ refcon, supportedPowerSources, settings);
+
+ if(!pmso) {
+ ret = kIOReturnInternalError;
+ goto bail_no_unlock;
+ }
+
+ IORecursiveLockLock(settingsCtrlLock);
+ for(i=0; settings[i]; i++)
+ {
+ list = (OSArray *)settingsCallbacks->getObject(settings[i]);
+ if(!list) {
+ // New array of callbacks for this setting
+ list = OSArray::withCapacity(1);
+ settingsCallbacks->setObject(settings[i], list);
+ list->release();
+ }
+
+ // Add caller to the callback list
+ list->setObject(pmso);
+ }
+
+ IORecursiveLockUnlock(settingsCtrlLock);
+
+ ret = kIOReturnSuccess;
+
+ // Track this instance by its OSData ptr from now on
+ *handle = pmso;
+
+bail_no_unlock:
+ if(kIOReturnSuccess != ret)
+ {
+ // Error return case
+ if(pmso) pmso->release();
+ if(handle) *handle = NULL;
+ }
+ return ret;
+}
+
+
+//******************************************************************************
+// sleepOnClamshellClosed
+//
+// contains the logic to determine if the system should sleep when the clamshell
+// is closed.
+//******************************************************************************
+
+bool IOPMrootDomain::shouldSleepOnClamshellClosed ( void )
+{
+ return ( !ignoringClamshell
+ && !ignoringClamshellDuringWakeup
+ && !(desktopMode && acAdaptorConnect) );
+}
+
+void IOPMrootDomain::sendClientClamshellNotification ( void )
+{
+ /* Only broadcast clamshell alert if clamshell exists. */
+ if(!clamshellExists)
+ return;
+
+ setProperty(kAppleClamshellStateKey,
+ clamshellIsClosed ? kOSBooleanTrue : kOSBooleanFalse);
+
+ setProperty(kAppleClamshellCausesSleepKey,
+ shouldSleepOnClamshellClosed() ? kOSBooleanTrue : kOSBooleanFalse);
+
+
+ /* Argument to message is a bitfiel of
+ * ( kClamshellStateBit | kClamshellSleepBit )
+ */
+ messageClients(kIOPMMessageClamshellStateChange,
+ (void *) ( (clamshellIsClosed ? kClamshellStateBit : 0)
+ | ( shouldSleepOnClamshellClosed() ? kClamshellSleepBit : 0)) );
+}
+
+//******************************************************************************
+// informCPUStateChange
+//
+// Call into PM CPU code so that CPU power savings may dynamically adjust for
+// running on battery, with the lid closed, etc.
+//
+// informCPUStateChange is a no-op on non x86 systems
+// only x86 has explicit support in the IntelCPUPowerManagement kext
+//******************************************************************************
+
+void IOPMrootDomain::informCPUStateChange(
+ uint32_t type,
+ uint32_t value )
+{
+#ifdef __i386__
+
+ pmioctlVariableInfo_t varInfoStruct;
+ int pmCPUret = 0;
+ const char *varNameStr = NULL;
+ int32_t *varIndex = NULL;
+
+ if (kInformAC == type) {
+ varNameStr = kIOPMRootDomainBatPowerCString;
+ varIndex = &idxPMCPULimitedPower;
+ } else if (kInformLid == type) {
+ varNameStr = kIOPMRootDomainLidCloseCString;
+ varIndex = &idxPMCPUClamshell;
+ } else {
+ return;
+ }
+
+ // Set the new value!
+ // pmCPUControl will assign us a new ID if one doesn't exist yet
+ bzero(&varInfoStruct, sizeof(pmioctlVariableInfo_t));
+ varInfoStruct.varID = *varIndex;
+ varInfoStruct.varType = vBool;
+ varInfoStruct.varInitValue = value;
+ varInfoStruct.varCurValue = value;
+ strncpy( (char *)varInfoStruct.varName,
+ (const char *)varNameStr,
+ strlen(varNameStr) + 1 );
+
+ // Set!
+ pmCPUret = pmCPUControl( PMIOCSETVARINFO, (void *)&varInfoStruct );
+
+ // pmCPU only assigns numerical id's when a new varName is specified
+ if ((0 == pmCPUret)
+ && (*varIndex == kCPUUnknownIndex))
+ {
+ // pmCPUControl has assigned us a new variable ID.
+ // Let's re-read the structure we just SET to learn that ID.
+ pmCPUret = pmCPUControl( PMIOCGETVARNAMEINFO, (void *)&varInfoStruct );
+
+ if (0 == pmCPUret)
+ {
+ // Store it in idxPMCPUClamshell or idxPMCPULimitedPower
+ *varIndex = varInfoStruct.varID;
+ }
+ }
+
+ return;
+
+#endif __i386__
+}
+
+//******************************************************************************
+// systemPowerEventOccurred
+//
+// The power controller is notifying us of a hardware-related power management
+// event that we must handle.
+//
+// systemPowerEventOccurred covers the same functionality that receivePowerNotification
+// does; it simply provides a richer API for conveying more information.
+//******************************************************************************
+IOReturn IOPMrootDomain::systemPowerEventOccurred(
+ const OSSymbol *event,
+ uint32_t intValue)
+{
+ IOReturn attempt = kIOReturnSuccess;
+ OSNumber *newNumber = NULL;
+
+ if (!event)
+ return kIOReturnBadArgument;
+
+ newNumber = OSNumber::withNumber(intValue, 8*sizeof(intValue));
+ if (!newNumber)
+ return kIOReturnInternalError;
+
+ attempt = systemPowerEventOccurred(event, (OSObject *)newNumber);
+
+ newNumber->release();
+
+ return attempt;
+}
+
+IOReturn IOPMrootDomain::systemPowerEventOccurred(
+ const OSSymbol *event,
+ OSObject *value)
+{
+ OSDictionary *thermalsDict = NULL;
+ bool shouldUpdate = true;
+
+ if (!event || !value)
+ return kIOReturnBadArgument;
+
+ // LOCK
+ // We reuse featuresDict Lock because it already exists and guards
+ // the very infrequently used publish/remove feature mechanism; so there's zero rsk
+ // of stepping on that lock.
+ if (featuresDictLock) IOLockLock(featuresDictLock);
+
+ thermalsDict = (OSDictionary *)getProperty(kIOPMRootDomainPowerStatusKey);
+
+ if (thermalsDict && OSDynamicCast(OSDictionary, thermalsDict)) {
+ thermalsDict = OSDictionary::withDictionary(thermalsDict);
+ } else {
+ thermalsDict = OSDictionary::withCapacity(1);
+ }
+
+ if (!thermalsDict) {
+ shouldUpdate = false;
+ goto exit;
+ }
+
+ thermalsDict->setObject (event, value);
+
+ setProperty (kIOPMRootDomainPowerStatusKey, thermalsDict);
+
+ thermalsDict->release();
+
+exit:
+ // UNLOCK
+ if (featuresDictLock) IOLockUnlock(featuresDictLock);
+
+ if (shouldUpdate)
+ messageClients (kIOPMMessageSystemPowerEventOccurred, (void *)NULL);
+
+ return kIOReturnSuccess;
+}
+
+
+//******************************************************************************
+// receivePowerNotification
+//
+// The power controller is notifying us of a hardware-related power management
+// event that we must handle. This may be a result of an 'environment' interrupt from
+// the power mgt micro.
+//******************************************************************************
+
+IOReturn IOPMrootDomain::receivePowerNotification (UInt32 msg)
+{
+ bool eval_clamshell = false;
+
+ /*
+ * Local (IOPMrootDomain only) eval clamshell command
+ */
+ if (msg & kLocalEvalClamshellCommand)
+ {
+ eval_clamshell = true;
+ }
+
+ /*
+ * Overtemp
+ */
+ if (msg & kIOPMOverTemp)
+ {
+ IOLog("PowerManagement emergency overtemp signal. Going to sleep!");
+
+ privateSleepSystem (kIOPMThermalEmergencySleepKey);
+ }
+
+ /*
+ * PMU Processor Speed Change
+ */
+ if (msg & kIOPMProcessorSpeedChange)
+ {
+ IOService *pmu = waitForService(serviceMatching("ApplePMU"));
+ pmu->callPlatformFunction("prepareForSleep", false, 0, 0, 0, 0);
+ getPlatform()->sleepKernel();
+ pmu->callPlatformFunction("recoverFromSleep", false, 0, 0, 0, 0);
+ }
+
+ /*
+ * Sleep Now!
+ */
+ if (msg & kIOPMSleepNow)
+ {
+ privateSleepSystem (kIOPMSoftwareSleepKey);
+ }
+
+ /*
+ * Power Emergency
+ */
+ if (msg & kIOPMPowerEmergency)
+ {
+ privateSleepSystem (kIOPMLowPowerSleepKey);
+ }
+
+
+ /*
+ * Clamshell OPEN
+ */
+ if (msg & kIOPMClamshellOpened)
+ {
+ // Received clamshel open message from clamshell controlling driver
+ // Update our internal state and tell general interest clients
+ clamshellIsClosed = false;
+ clamshellExists = true;
+
+ // Tell PMCPU
+ informCPUStateChange(kInformLid, 0);
+
+ // Tell general interest clients
+ sendClientClamshellNotification();
+ }
+
+ /*
+ * Clamshell CLOSED
+ * Send the clamshell interest notification since the lid is closing.
+ */
+ if (msg & kIOPMClamshellClosed)
+ {
+ // Received clamshel open message from clamshell controlling driver
+ // Update our internal state and tell general interest clients
+ clamshellIsClosed = true;
+ clamshellExists = true;
+
+ // Tell PMCPU
+ informCPUStateChange(kInformLid, 1);
+
+ // Tell general interest clients
+ sendClientClamshellNotification();
+
+ // And set eval_clamshell = so we can attempt
+ eval_clamshell = true;
+ }
+
+ /*
+ * Set Desktop mode (sent from graphics)
+ *
+ * -> reevaluate lid state
+ */
+ if (msg & kIOPMSetDesktopMode)
+ {
+ desktopMode = (0 != (msg & kIOPMSetValue));
+ msg &= ~(kIOPMSetDesktopMode | kIOPMSetValue);
+
+ sendClientClamshellNotification();
+
+ // Re-evaluate the lid state
+ if( clamshellIsClosed )
+ {
+ eval_clamshell = true;
+ }
+ }
+
+ /*
+ * AC Adaptor connected
+ *
+ * -> reevaluate lid state
+ */
+ if (msg & kIOPMSetACAdaptorConnected)
+ {
+ acAdaptorConnect = (0 != (msg & kIOPMSetValue));
+ msg &= ~(kIOPMSetACAdaptorConnected | kIOPMSetValue);
+
+ // Tell PMCPU
+ informCPUStateChange(kInformAC, !acAdaptorConnect);
+
+ sendClientClamshellNotification();
+
+ // Re-evaluate the lid state
+ if( clamshellIsClosed )
+ {
+ eval_clamshell = true;
+ }
+
+ }
+
+ /*
+ * Enable Clamshell (external display disappear)
+ *
+ * -> reevaluate lid state
+ */
+ if (msg & kIOPMEnableClamshell)
+ {
+ // Re-evaluate the lid state
+ // System should sleep on external display disappearance
+ // in lid closed operation.
+ if( clamshellIsClosed && (true == ignoringClamshell) )
+ {
+ eval_clamshell = true;
+ }
+
+ ignoringClamshell = false;
+
+ sendClientClamshellNotification();
+ }
+
+ /*
+ * Disable Clamshell (external display appeared)
+ * We don't bother re-evaluating clamshell state. If the system is awake,
+ * the lid is probably open.
+ */
+ if (msg & kIOPMDisableClamshell)
+ {
+ ignoringClamshell = true;
+
+ sendClientClamshellNotification();
+ }
+
+ /*
+ * Evaluate clamshell and SLEEP if appropiate
+ */
+ if ( eval_clamshell && shouldSleepOnClamshellClosed() )
+ {
+
+
+ // SLEEP!
+ privateSleepSystem (kIOPMClamshellSleepKey);
+ }
+
+ /*
+ * Power Button
+ */
+ if (msg & kIOPMPowerButton)
+ {
+ // toggle state of sleep/wake
+ // are we dozing?
+ if ( getPowerState() == DOZE_STATE )
+ {
+ // yes, tell the tree we're waking
+ systemWake();
+ // wake the Display Wrangler
+ reportUserInput();
+ }
+ else {
+ OSString *pbs = OSString::withCString("DisablePowerButtonSleep");
+ // Check that power button sleep is enabled
+ if( pbs ) {
+ if( kOSBooleanTrue != getProperty(pbs))
+ privateSleepSystem (kIOPMPowerButtonSleepKey);
+ }
+ }
+ }
+
+ /*
+ * Allow Sleep
+ *
+ */
+ if ( (msg & kIOPMAllowSleep) && !allowSleep )
+ {
+ allowSleep = true;
+ adjustPowerState();
+ }
+
+ /*
+ * Prevent Sleep
+ *
+ */
+ if (msg & kIOPMPreventSleep) {
+ allowSleep = false;
+ // are we dozing?
+ if ( getPowerState() == DOZE_STATE ) {
+ // yes, tell the tree we're waking
+ systemWake();