void ACPIBacklightPanel::onSmoothTimer()
{
    //DbgLog("%s::%s()\n", this->getName(),__FUNCTION__);

    IORecursiveLockLock(_lock);

    ////DbgLog("%s::%s(): _from_value=%d, _value=%d, _smoothIndex=%d\n", this->getName(), __FUNCTION__, _from_value, _value, _smoothIndex);

    // adjust smooth index based on current delta
    int diff = abs(_value - _from_value);
    if (_smoothIndex > 0 && diff <= smoothData[_smoothIndex-1].delta)
        --_smoothIndex;

    // move _from_value in the direction of _value
    SmoothData* data = &smoothData[_smoothIndex];
    if (_value > _from_value)
        _from_value = min(_value, _from_value + data->step);
    else
        _from_value = max(_value, _from_value - data->step);

    // set new brigthness level
    //DbgLog("%s::%s(): _from_value=%d, _value=%d\n", this->getName(), __FUNCTION__, _from_value, _value);
    setBrightnessLevel(_from_value);
    // set new timer if not reached desired brightness previously set
    if (_from_value != _value)
        _smoothTimer->setTimeoutUS(data->timeout);

    IORecursiveLockUnlock(_lock);
}
void ACPIBacklightPanel::scheduleWork(unsigned newWork)
{
    IORecursiveLockLock(_lock);
    _workPending |= newWork;
    _workSource->interruptOccurred(0, 0, 0);
    IORecursiveLockUnlock(_lock);
}
// IOFireWireIRMAllocation::free
//
//
void IOFireWireIRMAllocation::free( void )
{
	DebugLog( "IOFireWireIRMAllocation::free\n") ;

	// Take the lock
	IORecursiveLockLock(fLock);

	
	// If we need to release the isoch resources, do so now!
	if (isAllocated)
	{
		if (fReleaseIRMResourcesOnFree)
		{
			if (fBandwidthUnits > 0)
				fControl->releaseIRMBandwidthInGeneration(fBandwidthUnits,fAllocationGeneration);
			if (fIsochChannel < 64)
				fControl->releaseIRMChannelInGeneration(fIsochChannel,fAllocationGeneration);
		}
		// Note: we already removed this allocation from the controller's array! Don't need to do it here!
	}
	
	// Free the lock
	if ( fLock )
		IORecursiveLockFree( fLock ) ;

	OSObject::free();
}
// IOFireWireIRMAllocation::release
//
//
void IOFireWireIRMAllocation::release() const
{
	DebugLog( "IOFireWireIRMAllocation::release, retain count before release = %d\n",getRetainCount() ) ;

	// Take the lock
	IORecursiveLockLock(fLock);

	int retainCnt = getRetainCount();
	
	if ( retainCnt == 2 )
	{
		if( isAllocated == false )
		{
			fControl->removeFromIRMAllocationSet((IOFireWireIRMAllocation*)this);
		}
		else
		{
			// The controller has an extra retain on the IOFireWireIRMAllocation object
			// because it's in the array used to restore allocations after a bus-reset.
			// We now need to remove it from the controller's array, so it's no longer
			// auto-restored after bus-reset!
			fControl->removeIRMAllocation((IOFireWireIRMAllocation*)this);
		}
	}

	OSObject::release();

	// Bypass unlock if we just did the last release!
	if (retainCnt != 1)
		IORecursiveLockUnlock(fLock);
}
FakeSMCKey *FakeSMCDevice::getKey(const char *name)
{
    IORecursiveLockLock(device_lock);
    
    FakeSMCKey* key = 0;
    if (OSCollectionIterator *iterator = OSCollectionIterator::withCollection(keys)) {
        
        // Made the key name valid (4 char long): add trailing spaces if needed
        char validKeyNameBuffer[5];
        snprintf(validKeyNameBuffer, 5, "%-4s", name);
        
		while ((key = OSDynamicCast(FakeSMCKey, iterator->getNextObject()))) {
            
            UInt32 key1 = HWSensorsKeyToInt(&validKeyNameBuffer);
			UInt32 key2 = HWSensorsKeyToInt(key->getKey());
			if (key1 == key2) {
				break;
			}
		}
        
        OSSafeRelease(iterator);
	}
    IORecursiveLockUnlock(device_lock);
    
    if (!key)
        FakeSMCDebugLog("key %s not found", name);
    
	return key;
}
Example #6
0
KX_API(void,kx_lock_acquire(kx_hw *hw, spinlock_t *lock, unsigned long *,const char *file,int line))
{
	IORecursiveLockLock(lock->lock);
	lock->kx_lock++;
	lock->file=file;
	lock->line=line;
}
FakeSMCKey *FakeSMCDevice::addKeyWithHandler(const char *name, const char *type, unsigned char size, IOService *handler)
{
    IORecursiveLockLock(device_lock);

    FakeSMCKey* key;
	if ((key = getKey(name))) {
		HWSensorsErrorLog("key %s already handled", name);
        if (key->getHandler() != NULL) {
            // TODO: check priority?
            HWSensorsErrorLog("key %s already handled", name);
            key = 0;
        }
        else {
            key->setType(type);
            key->setSize(size);
            key->setHandler(handler);
        }
        
	}
    else {
    
        FakeSMCDebugLog("adding key %s with handler, type: %s, size: %d", name, type, size);
        
        if ((key = FakeSMCKey::withHandler(name, type, size, handler))) {
            keys->setObject(key);
            updateKeyCounterKey();
        }
	}
    IORecursiveLockUnlock(device_lock);
    
    if (!key)
        HWSensorsErrorLog("failed to create key %s", name);
        
	return key;
}
// IOFireWireIRMAllocation::deallocateIsochResources
//
//
IOReturn IOFireWireIRMAllocation::deallocateIsochResources(void)
{
	IOReturn res = kIOReturnError;

	// Take the lock
	IORecursiveLockLock(fLock);

	if (isAllocated)
	{
		if (fBandwidthUnits > 0)
			fControl->releaseIRMBandwidthInGeneration(fBandwidthUnits,fAllocationGeneration);
		if (fIsochChannel < 64)
			fControl->releaseIRMChannelInGeneration(fIsochChannel,fAllocationGeneration);
	
		// Unregister this object with the controller
		fControl->removeIRMAllocation(this);
		
		isAllocated = false;
		fBandwidthUnits = 0;
		fIsochChannel = 64;
		fAllocationGeneration = 0xFFFFFFFF;
	}
	
	// Unlock the lock
	IORecursiveLockUnlock(fLock);

	return res;
}
IOReturn IOFWWorkLoop::wake(void *token)
{
#if 0
    if( fSleepToken != token ) 
	{
        DEBUGLOG( "IOFWWorkLoop::wake: wrong token: %p<->%p\n", token, fSleepToken );
        return kIOReturnError;
    }
#endif
	
	if( fSleepToken )
	{
		IORecursiveLockLock( gateLock );
		
		void * the_token = fSleepToken;
		fSleepToken = NULL;
		
		// delete any event sources that were removed during sleep
		fRemoveSourceDeferredSet->flushCollection();
		
		// wake up waiting threads
		wakeupGate( the_token, false );
    }
	else 
	{
		closeGate();
	}

	return kIOReturnSuccess;
}
bool BatteryTracker::addBatteryManager(AppleSmartBatteryManager* pManager)
{
    DEBUG_LOG("BatteryTracker::addBatteryManager: entering addBatteryManager(%p)\n", pManager);
    
    bool result = false;
    
    IORecursiveLockLock(m_pLock);
    unsigned count = m_pBatteryList->getCount();
    unsigned i = 0;
    for (; i < count; ++i)
    {
        OSObject* pTemp = m_pBatteryList->getObject(i);
        if (pTemp == pManager)
            break;
        if (pTemp == NULL)
        {
            result = true;
            m_pBatteryList->setObject(i, pManager);
            break;
        }
    }
    if (i == count)
    {
        m_pBatteryList->setObject(pManager);
        result = true;
    }
    IORecursiveLockUnlock(m_pLock);
    
    return result;
}
IOReturn FakeSMCDevice::setProperties(OSObject * properties)
{
    IORecursiveLockLock(device_lock);
    
    IOReturn result = kIOReturnUnsupported;
    
    if (OSDictionary * msg = OSDynamicCast(OSDictionary, properties)) {
        if (OSString * name = OSDynamicCast(OSString, msg->getObject(kFakeSMCDeviceUpdateKeyValue))) {
            if (FakeSMCKey * key = getKey(name->getCStringNoCopy())) {
                
                OSArray *info = OSArray::withCapacity(2);
                
                info->setObject(OSString::withCString(key->getType()));
                info->setObject(OSData::withBytes(key->getValue(), key->getSize()));
                
                exposedValues->setObject(key->getKey(), info);
                
                OSDictionary *values = OSDictionary::withDictionary(exposedValues);
                
                this->setProperty(kFakeSMCDeviceValues, values);
                
                OSSafeRelease(values);
                
                result = kIOReturnSuccess;
            }
        }
        else if (OSArray* array = OSDynamicCast(OSArray, msg->getObject(kFakeSMCDevicePopulateValues))) {
            if (OSIterator* iterator = OSCollectionIterator::withCollection(array)) {
                while (OSString *keyName = OSDynamicCast(OSString, iterator->getNextObject()))
                    if (FakeSMCKey * key = getKey(keyName->getCStringNoCopy())) {
                        
                        OSArray *info = OSArray::withCapacity(2);
                        
                        info->setObject(OSString::withCString(key->getType()));
                        info->setObject(OSData::withBytes(key->getValue(), key->getSize()));
                        
                        exposedValues->setObject(key->getKey(), info);
                        
                        IOSleep(10);    //REVIEW: what is this for?
                    }
                
                OSDictionary *values = OSDictionary::withDictionary(exposedValues);
                
                this->setProperty(kFakeSMCDeviceValues, values);
                
                OSSafeRelease(values);
                OSSafeRelease(iterator);
                
                result = kIOReturnSuccess;
            }
        }
    }
    
    IORecursiveLockUnlock(device_lock);
    
	return result;
}
FakeSMCKey *FakeSMCDevice::getKey(unsigned int index)
{
    IORecursiveLockLock(device_lock);
    FakeSMCKey* key = OSDynamicCast(FakeSMCKey, keys->getObject(index));
    IORecursiveLockUnlock(device_lock);
    if (!key)
        FakeSMCDebugLog("key with index %d not found", index);
    
	return key;
}
// IOFireWireIRMAllocation::allocateIsochResources
//
//
IOReturn IOFireWireIRMAllocation::allocateIsochResources(UInt8 isochChannel, UInt32 bandwidthUnits)
{
	IOReturn res = kIOReturnError;
	UInt32 irmGeneration;
	UInt16 irmNodeID;
	
	// Take the lock
	IORecursiveLockLock(fLock);
	
	if (!isAllocated)
	{
		// Initialize some class members
		fAllocationGeneration = 0xFFFFFFFF;
		
		// Get the current generation
		fControl->getIRMNodeID(irmGeneration, irmNodeID);
		
		res = kIOReturnSuccess;
		
		if (isochChannel < 64)
		{
			// Attempt to allocate isoch channel
			res = fControl->allocateIRMChannelInGeneration(isochChannel,irmGeneration);
		}
		
		if ((res == kIOReturnSuccess) && (bandwidthUnits > 0))
		{
			// Attempt to allocate isoch bandwidth
			res = fControl->allocateIRMBandwidthInGeneration(bandwidthUnits,irmGeneration);
			if (res != kIOReturnSuccess) 
			{
				// Need to free the isoch channel (note: will fail if generation has changed)
				fControl->releaseIRMChannelInGeneration(isochChannel,irmGeneration);
			}
		}
		
		if (res == kIOReturnSuccess)
		{
			fIsochChannel = isochChannel;
			fBandwidthUnits = bandwidthUnits;
			fAllocationGeneration = irmGeneration;
			isAllocated = true;
			
			// Register this object with the controller
			fControl->addIRMAllocation(this);
		}
	}
	
	// Unlock the lock
	IORecursiveLockUnlock(fLock);
	
	FWTrace( kFWTIsoch, kTPIsochIRMAllocateIsochResources, (uintptr_t)(fControl->getLink()), fIsochChannel, fBandwidthUnits, res );
	
	return res;
}
void ACPIBacklightPanel::processWorkQueue(IOInterruptEventSource *, int)
{
    DbgLog("%s::%s() _workPending=%x\n", this->getName(),__FUNCTION__, _workPending);
    
    IORecursiveLockLock(_lock);
    if (_workPending & kWorkSave)
        saveACPIBrightnessLevelNVRAM(_committed_value);
    if (_workPending & kWorkSetBrightness)
        setBrightnessLevel(_committed_value);
    _workPending = 0;
    IORecursiveLockUnlock(_lock);
}
void BatteryTracker::notifyBatteryManagersGated(bool connected)
{
    IORecursiveLockLock(m_pLock);
    unsigned count = m_pBatteryList->getCount();
    for (unsigned i = 0; i < count; ++i)
    {
        AppleSmartBatteryManager* pManager = static_cast<AppleSmartBatteryManager*>(m_pBatteryList->getObject(i));
        if (NULL != pManager)
            pManager->notifyConnectedState(connected);
    }
    IORecursiveLockUnlock(m_pLock);
}
// IOFireWireIRMAllocation::threadFunc
//
//
void IOFireWireIRMAllocation::threadFunc( void * arg )
{
	IOReturn res = kIOReturnSuccess;
    IRMAllocationThreadInfo * threadInfo = (IRMAllocationThreadInfo *)arg;
    IOFireWireIRMAllocation *pIRMAllocation = threadInfo->fIRMAllocation;
	IORecursiveLock * fLock = threadInfo->fLock;
	UInt32 generation = threadInfo->fGeneration;
	UInt32 irmGeneration;
	UInt16 irmNodeID;
	
	// Take the lock
	IORecursiveLockLock(fLock);

	// Get the current generation
	threadInfo->fControl->getIRMNodeID(irmGeneration, irmNodeID);
	
	if ((irmGeneration == generation) && (pIRMAllocation->getAllocationGeneration() != 0xFFFFFFFF))
	{
		if (threadInfo->fIsochChannel < 64)
		{
			// Attempt to reallocate isoch channel
			res = threadInfo->fControl->allocateIRMChannelInGeneration(threadInfo->fIsochChannel,generation);
		}
		
		if ((res == kIOReturnSuccess) && (threadInfo->fBandwidthUnits > 0))
		{
			// Attempt to reallocate isoch bandwidth
			res = threadInfo->fControl->allocateIRMBandwidthInGeneration(threadInfo->fBandwidthUnits,generation);
			if (res != kIOReturnSuccess) 
			{
				// Need to free the isoch channel (note: will fail if generation has changed)
				threadInfo->fControl->releaseIRMChannelInGeneration(threadInfo->fIsochChannel,generation);
			}
		}

		if ((res != kIOReturnSuccess) && (res != kIOFireWireBusReset))
		{
			// We failed to reallocate (and not due to a bus-reset).
			pIRMAllocation->failedToRealloc();
		}
	}
	
	// Unlock the lock
	IORecursiveLockUnlock(fLock);
	
	// clean up thread info
	IOFree( threadInfo, sizeof(threadInfo) );
    pIRMAllocation->release();		// retain occurred in handleBusReset
	
	FWTrace( kFWTIsoch, kTPIsochIRMThreadFunc, (uintptr_t)(threadInfo->fControl->getLink()), threadInfo->fIsochChannel, threadInfo->fBandwidthUnits, res );
}
bool BatteryTracker::anyBatteriesDischarging(AppleSmartBattery* pExcept)
{
    bool result = false;
    
    IORecursiveLockLock(m_pLock);
    unsigned count = m_pBatteryList->getCount();
    for (unsigned i = 0; i < count; ++i)
    {
        AppleSmartBatteryManager* pManager = static_cast<AppleSmartBatteryManager*>(m_pBatteryList->getObject(i));
        if (pManager && pManager->fBattery && pExcept != pManager->fBattery && pManager->fBattery->fBatteryPresent && !pManager->fBattery->fACConnected)
        {
            result = true;
            break;
        }
    }
    IORecursiveLockUnlock(m_pLock);
    
    return result;
}
void ACPIBacklightPanel::setBrightnessLevelSmooth(UInt32 level)
{
    DbgLog("%s::%s(%d)\n", this->getName(), __FUNCTION__, level);

    //DbgLog("%s: _from_value=%d, _value=%d\n", this->getName(), _from_value, _value);

    if (_smoothTimer)
    {
        IORecursiveLockLock(_lock);

        if (level != _value)
        {
            // find appropriate movemement params in smoothData
            int diff = abs((int)level - _from_value);
            _smoothIndex = countof(smoothData)-1; // defensive
            for (int i = 0; i < countof(smoothData); i++)
            {
                if (diff <= smoothData[i].delta)
                {
                    _smoothIndex = i;
                    break;
                }
            }
            // kick off timer if not already started
            bool start = (_from_value == _value);
            _value = level;
            if (start)
                onSmoothTimer();
        }
        else if (_from_value == _value)
        {
            // in the case of already set to that value, set it for sure
            setBrightnessLevel(_value);
        }

        IORecursiveLockUnlock(_lock);
    }
    else
    {
        _from_value = _value = level;
        setBrightnessLevel(_value);
    }
}
bool BatteryTracker::removeBatteryManager(AppleSmartBatteryManager* pManager)
{
    DEBUG_LOG("BatteryTracker::removeBatteryManager: entering removeBatteryManager(%p)\n", pManager);
    
    bool result = false;
    
    IORecursiveLockLock(m_pLock);
    unsigned count = m_pBatteryList->getCount();
    for (unsigned i = 0; i < count; ++i)
    {
        if (m_pBatteryList->getObject(i) == pManager)
        {
            m_pBatteryList->setObject(i, NULL);
            result = true;
            break;
        }
    }
    IORecursiveLockUnlock(m_pLock);
    
    return result;
}
// IOFireWireIRMAllocation::handleBusReset
//
//
void IOFireWireIRMAllocation::handleBusReset(UInt32 generation)
{
	// Take the lock
	IORecursiveLockLock(fLock);

	if (!isAllocated)
	{
		IORecursiveLockUnlock(fLock);
		return;
	}
	
	if (fAllocationGeneration == generation)
	{
		IORecursiveLockUnlock(fLock);
		return;
	}
	
	// Spawn a thread to do the reallocation
	IRMAllocationThreadInfo * threadInfo = (IRMAllocationThreadInfo *)IOMalloc( sizeof(IRMAllocationThreadInfo) );
	if( threadInfo ) 
	{
		threadInfo->fGeneration = generation;
		threadInfo->fIRMAllocation = this;
		threadInfo->fControl = fControl;
		threadInfo->fLock = fLock;
		threadInfo->fIsochChannel = fIsochChannel; 
		threadInfo->fBandwidthUnits = fBandwidthUnits;

		retain();	// retain ourself for the thread to use

		thread_t		thread;
		if( kernel_thread_start((thread_continue_t)threadFunc, threadInfo, &thread ) == KERN_SUCCESS )
		{
			thread_deallocate(thread);
		}
	}
	
	// Unlock the lock
	IORecursiveLockUnlock(fLock);
}
Example #21
0
/**
 *  Engage global FakeSMCPlugin access lock
 */
void FakeSMCPlugin::lockAccessForOtherPlugins(void)
{
    IORecursiveLockLock(gFakeSMCPluginLock);
}
bool
IOFireWireUserClientIniter::start(
	IOService*	provider)
{
	//IOLog( "IOFireWireUserClientIniter<0x%08lx>::start - provider = 0x%08lx\n", this, provider );

	if( provider == NULL )
	{
		return false;
	}
	
	fProvider = provider ;
	fProvider->retain();

	OSObject*	dictObj = getProperty("IOProviderMergeProperties");	

	OSDictionary * merge_properties = OSDynamicCast(OSDictionary, dictObj);
	if( merge_properties != NULL )
	{
		merge_properties = dictionaryDeepCopy( merge_properties );
	}
	
	if ( !merge_properties )
	{
		IOLog("%s %u: couldn't get merge_properties\n", __FILE__, __LINE__ ) ;
		return false;
	}

	//
	// make sure the user client class object is an OSSymbol
	//
	
	OSObject * userClientClassObject = merge_properties->getObject( gIOUserClientClassKey );
	if( OSDynamicCast(OSString, userClientClassObject) != NULL )
	{
		// if the the user client class object is an OSString, turn it into an OSSymbol
		
		const OSSymbol * userClientClassSymbol = OSSymbol::withString((const OSString *) userClientClassObject);
		if( userClientClassSymbol != NULL )
		{
			merge_properties->setObject(gIOUserClientClassKey, (OSObject *) userClientClassSymbol);
			userClientClassSymbol->release();
		}
		
	}
	else if( OSDynamicCast(OSSymbol, userClientClassObject) == NULL )
	{
		// if its not an OSString or an OSymbol remove it from the merge properties
		
		merge_properties->removeObject(gIOUserClientClassKey);
	}
	
	// serialize all firwire user client initers
	
	IORecursiveLockLock( sIniterLock );
	
	mergeProperties( fProvider, merge_properties );

	IORecursiveLockUnlock( sIniterLock );

	merge_properties->release();

	//IOLog( "IOFireWireUserClientIniter<0x%08lx>::start - return\n", this );
	
    return true ;
}
FakeSMCKey *FakeSMCDevice::addKeyWithValue(const char *name, const char *type, unsigned char size, const void *value)
{
    IORecursiveLockLock(device_lock);
    
    FakeSMCKey* key;
	if ((key = getKey(name))) {
        
        if (value) {
            key->setType(type);
            key->setSize(size);
            key->setValueFromBuffer(value, size);
        }
        
        if (debug) {
            if (strncmp("NATJ", key->getKey(), 5) == 0) {
                UInt8 val = *(UInt8*)key->getValue();
                
                switch (val) {
                    case 0:
                        HWSensorsInfoLog("Ninja Action Timer Job: do nothing");
                        break;
                        
                    case 1:
                        HWSensorsInfoLog("Ninja Action Timer Job: force shutdown to S5");
                        break;
                        
                    case 2:
                        HWSensorsInfoLog("Ninja Action Timer Job: force restart");
                        break;
                        
                    case 3:
                        HWSensorsInfoLog("Ninja Action Timer Job: force startup");
                        break;
                        
                    default:
                        break;
                }
            }
            else if (strncmp("NATi", key->getKey(), 5) == 0) {
                UInt16 val = *(UInt16*)key->getValue();
                
                HWSensorsInfoLog("Ninja Action Timer is set to %d", val);
            }
            else if (strncmp("MSDW", key->getKey(), 5) == 0) {
                UInt8 val = *(UInt8*)key->getValue();
                
                switch (val) {
                    case 0:
                        HWSensorsInfoLog("display is now asleep");
                        break;
                        
                    case 1:
                        HWSensorsInfoLog("display is now awake");
                        break;
                        
                    default:
                        break;
                }
            }
        }
        
		FakeSMCDebugLog("updating value for key %s, type: %s, size: %d", name, type, size);
	}
    else {
    
        FakeSMCDebugLog("adding key %s with value, type: %s, size: %d", name, type, size);
        
        if ((key = FakeSMCKey::withValue(name, type, size, value))) {
            keys->setObject(key);
            updateKeyCounterKey();
        }
	}
    IORecursiveLockUnlock(device_lock);

    if (!key)
        HWSensorsErrorLog("failed to create key %s", name);
        
	return key;
}
IOReturn FakeSMCDevice::callPlatformFunction(const OSSymbol *functionName, bool waitForFunction, void *param1, void *param2, void *param3, void *param4 )
{
    ////if (waitForFunction)
        IORecursiveLockLock(device_lock);
    
    IOReturn result = kIOReturnUnsupported;
    
    if (functionName->isEqualTo(kFakeSMCAddKeyHandler)) {
        
        result = kIOReturnBadArgument;
        
        if (param1 && param2 && param3 && param4) {
            const char *name = (const char *)param1;
            const char *type = (const char *)param2;
            UInt8 size = (UInt64)param3;
            IOService *handler = (IOService*)param4;
            
            if (name && type && size > 0 && handler) {
                if (addKeyWithHandler(name, type, size, handler))
                    result = kIOReturnSuccess;
                else
                    result = kIOReturnError;
            }
        }
    }
    else if (functionName->isEqualTo(kFakeSMCGetKeyHandler)) {
        
        result = kIOReturnBadArgument;
        
        if (const char *name = (const char *)param1) {
            
            result = kIOReturnError;
            
            if (FakeSMCKey *key = OSDynamicCast(FakeSMCKey, getKey(name))) {
                if (key->getHandler()) {
                    
                    result = kIOReturnBadArgument;
                    
                    if (param2) {
                        IOService *handler = (IOService *)param2;
                        bcopy(key->getHandler(), handler, sizeof(handler));
                        result = kIOReturnSuccess;
                    }
                }
            }
        }
    }
    else if (functionName->isEqualTo(kFakeSMCRemoveKeyHandler)) {
        
        result = kIOReturnBadArgument;
        
        if (param1) {
            result = kIOReturnError;
            
            if (OSCollectionIterator *iterator = OSCollectionIterator::withCollection(keys)) {
                IOService *handler = (IOService *)param1;
                while (FakeSMCKey *key = OSDynamicCast(FakeSMCKey, iterator->getNextObject())) {
                    if (key->getHandler() == handler)
                        key->setHandler(NULL);
                }
                result = kIOReturnSuccess;
                OSSafeRelease(iterator);
            }
        }
    }
    else if (functionName->isEqualTo(kFakeSMCAddKeyValue)) {
        
        result = kIOReturnBadArgument;
        
        if (param1 && param2 && param3) {
            const char *name = (const char *)param1;
            const char *type = (const char *)param2;
            UInt8 size = (UInt64)param3;
            const void *value = (const void *)param4;
            
            if (name && type && size > 0) {
                if (addKeyWithValue(name, type, size, value))
                    result = kIOReturnSuccess;
                else
                    result = kIOReturnError;
            }
        }
    }
    else if (functionName->isEqualTo(kFakeSMCSetKeyValue)) {
        
        result = kIOReturnBadArgument;
        
        if (param1 && param2 && param3) {
            const char *name = (const char *)param1;
            UInt8 size = (UInt64)param2;
            const void *data = (const void *)param3;
            
            result = kIOReturnError;
            
            if (name && data && size > 0) {
                if (FakeSMCKey *key = OSDynamicCast(FakeSMCKey, getKey(name))) {
                    if (key->setValueFromBuffer(data, size)) {
                        result = kIOReturnSuccess;
                    }
                }
            }
        }
    }
    else if (functionName->isEqualTo(kFakeSMCGetKeyValue)) {
        
        result = kIOReturnBadArgument;
        
        if (const char *name = (const char *)param1) {
            
            result = kIOReturnError;
            
            if (FakeSMCKey *key = getKey(name)) {
                
                result = kIOReturnBadArgument;
                
                if (param2 && param3) {
                    UInt8 *size = (UInt8*)param2;
                    const void **value = (const void **)param3;
                    
                    *size = key->getSize();
                    *value = key->getValue();
                    
                    result = kIOReturnSuccess;
                }
            }
        }
    }
    else if (functionName->isEqualTo(kFakeSMCTakeVacantGPUIndex)) {
        
        result = kIOReturnBadArgument;
        
        if (SInt8 *index = (SInt8*)param1) {
            for (UInt8 i = 0; i <= 0xf; i++) {
                if (!bit_get(vacantGPUIndex, BIT(i))) {
                    bit_set(vacantGPUIndex, BIT(i));
                    *index = i;
                    result = kIOReturnSuccess;
                    break;
                }
            }
            
            if (result != kIOReturnSuccess)
                result = kIOReturnError;
        }
    }
    else if (functionName->isEqualTo(kFakeSMCReleaseGPUIndex)) {
        
        result = kIOReturnBadArgument;
        
        if (UInt8 *index = (UInt8*)param1) {
            if (*index <= 0xf) {
                bit_clear(vacantGPUIndex, BIT(*index));
                result = kIOReturnSuccess;
            }
        }
    }
    else if (functionName->isEqualTo(kFakeSMCTakeVacantFanIndex)) {
        
        result = kIOReturnBadArgument;
        
        if (SInt8 *index = (SInt8*)param1) {
            for (UInt8 i = 0; i <= 0xf; i++) {
                if (!bit_get(vacantFanIndex, BIT(i))) {
                    bit_set(vacantFanIndex, BIT(i));
                    *index = i;
                    updateFanCounterKey();
                    result = kIOReturnSuccess;
                    break;
                }
            }
            
            if (result != kIOReturnSuccess)
                result = kIOReturnError;
        }
    }
    else if (functionName->isEqualTo(kFakeSMCReleaseFanIndex)) {
        
        result = kIOReturnBadArgument;
        
        if (UInt8 *index = (UInt8*)param1) {
            if (*index <= 0xf) {
                bit_clear(vacantFanIndex, BIT(*index));
                updateFanCounterKey();
                result = kIOReturnSuccess;
            }
        }
    }
    else {
        ////if (waitForFunction)
            IORecursiveLockUnlock(device_lock);
        
        return super::callPlatformFunction(functionName, waitForFunction, param1, param2, param3, param4);
    }
    
    ////if (waitForFunction)
        IORecursiveLockUnlock(device_lock);
    
	return result;
}
Example #25
0
void AppleRAIDGlobals::lock(void)
{
    IORecursiveLockLock(raidGlobalLock);
}
Example #26
0
void IOWorkLoop::closeGate()
{
    IORecursiveLockLock(gateLock);
    IOStatisticsCloseGate();
}
Example #27
0
void IOWorkLoop::closeGate()
{
    IORecursiveLockLock(gateLock);
}