kernel/eka/drivers/resourceman/resourcecontrol.cpp
changeset 4 56f325a607ea
parent 0 a41df078684a
child 43 c1f20ce4abcf
equal deleted inserted replaced
2:4122176ea935 4:56f325a607ea
   231 	return r;
   231 	return r;
   232     }
   232     }
   233 
   233 
   234 TInt TInterface::GetResourceState(TUint aClientId, TUint aResourceId, TBool aCached, TPowerResourceCb& aCb)
   234 TInt TInterface::GetResourceState(TUint aClientId, TUint aResourceId, TBool aCached, TPowerResourceCb& aCb)
   235     {
   235     {
   236 	GET_CRITICAL_SECTION_COUNT
       
   237 	TInt r;
   236 	TInt r;
   238     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">TInterface::GetResourceState"));
   237     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">TInterface::GetResourceState"));
   239     r = PowerResourceController->GetResourceState(aClientId, aResourceId, aCached, aCb);
   238     r = PowerResourceController->GetResourceState(aClientId, aResourceId, aCached, aCb);
   240 	if(!&aCb) //Not checking incase of asynchronous function as mutex might be held in RC thread, when this is checked.
       
   241 		{
       
   242 		LOCK_AND_CRITICAL_SECTION_COUNT_CHECK
       
   243 		}
       
   244 	return r;
   239 	return r;
   245     }
   240     }
   246 
   241 
   247 TInt TInterface::CancelAsyncRequestCallBack(TUint aClientId, TUint aResourceId, TPowerResourceCb& aCb)
   242 TInt TInterface::CancelAsyncRequestCallBack(TUint aClientId, TUint aResourceId, TPowerResourceCb& aCb)
   248     {
   243     {
   343 
   338 
   344 /** Destructor for power controller. Frees the memory allocated in kernel heap. */
   339 /** Destructor for power controller. Frees the memory allocated in kernel heap. */
   345 DPowerResourceController::~DPowerResourceController()
   340 DPowerResourceController::~DPowerResourceController()
   346 	{
   341 	{
   347 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("DPowerResourceController::~DPowerResourceController()"));
   342 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("DPowerResourceController::~DPowerResourceController()"));
   348 	//Push Power controller ID back to Pool
   343 #ifdef RESOURCE_MANAGER_SIMULATED_PSL
   349 	SPowerResourceClient* pC = NULL;
   344 	iCleanList.ResetAndDestroy();
   350 	pC = iClientList[(TUint16)(iPowerControllerId & ID_INDEX_BIT_MASK)];
   345 #endif
   351 	if(pC)
       
   352 		{
       
   353 		iClientCount--; //Decrement client count
       
   354 		LIST_PUSH(iClientPool, pC, iNextInList);
       
   355 		}
       
   356 	iClientList.Delete();
   346 	iClientList.Delete();
   357 	iUserSideClientList.Delete();
   347 	iUserSideClientList.Delete();
       
   348 
       
   349 
       
   350 		
   358 #ifdef PRM_ENABLE_EXTENDED_VERSION
   351 #ifdef PRM_ENABLE_EXTENDED_VERSION
   359 	iDynamicResourceList.Delete();
   352 	iDynamicResourceList.Delete();
   360 	iDynamicResDependencyList.Delete();
   353 	iDynamicResDependencyList.Delete();
   361 #endif
   354 #endif
   362 	pC = iClientPool;
   355 
   363 	while(iClientPool) //Find the starting position of array to delete
       
   364 		{
       
   365 		if(iClientPool < pC)
       
   366 			pC = iClientPool;
       
   367 		iClientPool = iClientPool->iNextInList;
       
   368 		}
       
   369 	//delete pC;
       
   370 	delete []pC;
       
   371 	SPowerResourceClientLevel *pCL = iClientLevelPool;
   356 	SPowerResourceClientLevel *pCL = iClientLevelPool;
   372 	while(iClientLevelPool) //Find the starting position of array to delete
   357 	while(iClientLevelPool) //Find the starting position of array to delete
   373 		{
   358 		{
   374 		if(iClientLevelPool < pCL)
   359 		if(iClientLevelPool < pCL)
   375 			pCL = iClientLevelPool;
   360 			pCL = iClientLevelPool;
   420 			{
   405 			{
   421 			pN->iCallback.iResult = aReturnCode;
   406 			pN->iCallback.iResult = aReturnCode;
   422 			pN->iCallback.iLevel = aState;
   407 			pN->iCallback.iLevel = aState;
   423 			pN->iCallback.iClientId = aClientId;
   408 			pN->iCallback.iClientId = aClientId;
   424 			pN->iCallback.iLevelOwnerId = aLevelOwnerId;
   409 			pN->iCallback.iLevelOwnerId = aLevelOwnerId;
   425 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("Notification ClientId = 0x%x, ResourceId = %d, state = %d, \
   410 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("Notification ClientId = 0x%x, ResourceId = %d, state = %d, Result = %d", 
   426 				             Result = %d", pN->iCallback.iClientId, pN->iCallback.iResourceId, aState, aReturnCode));
   411 									pN->iCallback.iClientId, pN->iCallback.iResourceId, aState, aReturnCode));
   427 			PRM_POSTNOTIFICATION_SENT_TRACE
   412 			PRM_POSTNOTIFICATION_SENT_TRACE
   428 			pN->iCallback.Enque();
   413 			pN->iCallback.Enque();
   429 			continue;
   414 			continue;
   430 			}	
   415 			}	
   431 #endif
   416 #endif
   435 			{
   420 			{
   436             pN->iCallback.iResult=aReturnCode;
   421             pN->iCallback.iResult=aReturnCode;
   437             pN->iCallback.iLevel=aState;
   422             pN->iCallback.iLevel=aState;
   438             pN->iCallback.iClientId = aClientId;
   423             pN->iCallback.iClientId = aClientId;
   439 			pN->iCallback.iLevelOwnerId = aLevelOwnerId;
   424 			pN->iCallback.iLevelOwnerId = aLevelOwnerId;
   440 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("Notifications ClientId = 0x%x, ResourceId = %d, State = %d, \
   425 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("Notifications ClientId = 0x%x, ResourceId = %d, State = %d, Result = %d",
   441 				              Result = %d", pN->iCallback.iClientId, pN->iCallback.iResourceId, aState, aReturnCode));
   426 										pN->iCallback.iClientId, pN->iCallback.iResourceId, aState, aReturnCode));
   442 			PRM_POSTNOTIFICATION_SENT_TRACE
   427 			PRM_POSTNOTIFICATION_SENT_TRACE
   443             pN->iCallback.Enque();
   428             pN->iCallback.Enque();
   444 			}
   429 			}
   445 		pN->iPreviousLevel = aState; //Update the state
   430 		pN->iPreviousLevel = aState; //Update the state
   446 		}
   431 		}
   513 		if(aClientId & USER_SIDE_CLIENT_BIT_MASK)
   498 		if(aClientId & USER_SIDE_CLIENT_BIT_MASK)
   514 			pC = iUserSideClientList[TUint16(aClientId & ID_INDEX_BIT_MASK)];
   499 			pC = iUserSideClientList[TUint16(aClientId & ID_INDEX_BIT_MASK)];
   515 		else
   500 		else
   516 			pC = iClientList[TUint16(aClientId & ID_INDEX_BIT_MASK)];
   501 			pC = iClientList[TUint16(aClientId & ID_INDEX_BIT_MASK)];
   517 #endif		
   502 #endif		
   518 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("Request Type = %d, ClientId = 0x%x, ResourceId = %d",  \
   503 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("Request Type = %d, ClientId = 0x%x, ResourceId = %d",
   519 		                             aRequest.ReqType(), aRequest.ClientId(), aRequest.ResourceId()));
   504 		                             aRequest.ReqType(), aRequest.ClientId(), aRequest.ResourceId()));
   520     if(aRequest.ReqType()==TPowerRequest::EChange)
   505     if(aRequest.ReqType()==TPowerRequest::EChange)
   521 		{
   506 		{
   522         if(pR->Usage()) //Handling shared resource
   507         if(pR->Usage()) //Handling shared resource
   523 			{
   508 			{
   602 			{
   587 			{
   603 			aRequest.ClientId() = -1;
   588 			aRequest.ClientId() = -1;
   604 			aRequest.Level() = pR->iDefaultLevel;
   589 			aRequest.Level() = pR->iDefaultLevel;
   605 			}
   590 			}
   606 		}
   591 		}
   607 	aRequest.ReturnCode() = pR->DoRequest(aRequest);
   592 	if((aRequest.ReqType() == TPowerRequest::EGet) || (pR->iCachedLevel != aRequest.Level()))
       
   593 		aRequest.ReturnCode() = pR->DoRequest(aRequest);
   608 	CompleteRequest(aRequest);
   594 	CompleteRequest(aRequest);
   609 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::HandleMsg"));
   595 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::HandleMsg"));
   610 	}
   596 	}
   611 
   597 
   612 #ifdef PRM_ENABLE_EXTENDED_VERSION
   598 #ifdef PRM_ENABLE_EXTENDED_VERSION
   628 		if(aClientId & USER_SIDE_CLIENT_BIT_MASK)
   614 		if(aClientId & USER_SIDE_CLIENT_BIT_MASK)
   629 			pC = iUserSideClientList[TUint16(aClientId & ID_INDEX_BIT_MASK)];
   615 			pC = iUserSideClientList[TUint16(aClientId & ID_INDEX_BIT_MASK)];
   630 		else
   616 		else
   631 			pC = iClientList[TUint16(aClientId & ID_INDEX_BIT_MASK)];
   617 			pC = iClientList[TUint16(aClientId & ID_INDEX_BIT_MASK)];
   632 		}
   618 		}
   633 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("Request Type = %d, ClientId = 0x%x, ResourceId = %d",  \
   619 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("Request Type = %d, ClientId = 0x%x, ResourceId = %d",  
   634 		                             aRequest.ReqType(), aRequest.ClientId(), aRequest.ResourceId()));
   620 		                             aRequest.ReqType(), aRequest.ClientId(), aRequest.ResourceId()));
   635 	if((aRequest.ResourceId() & KIdMaskResourceWithDependencies) && (aRequest.ReqType() != TPowerRequest::EGet))
   621 	if((aRequest.ResourceId() & KIdMaskResourceWithDependencies) && (aRequest.ReqType() != TPowerRequest::EGet))
   636 		{
   622 		{
   637 		Lock();
   623 		Lock();
   638 		iDfcQDependencyLock = ETrue;
   624 		iDfcQDependencyLock = ETrue;
   654 void DPowerResourceController::MsgQFunc(TAny* aPtr)
   640 void DPowerResourceController::MsgQFunc(TAny* aPtr)
   655 	{
   641 	{
   656     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::MsgQFunc"));
   642     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::MsgQFunc"));
   657     DPowerResourceController* pRC=(DPowerResourceController*)aPtr;
   643     DPowerResourceController* pRC=(DPowerResourceController*)aPtr;
   658     TPowerRequest* aReq=(TPowerRequest*)pRC->iMsgQ->iMessage;
   644     TPowerRequest* aReq=(TPowerRequest*)pRC->iMsgQ->iMessage;
       
   645 	DStaticPowerResource *pR = aReq->Resource();
   659 	if(aReq->ReqType() == TPowerRequest::EAllocReserve)
   646 	if(aReq->ReqType() == TPowerRequest::EAllocReserve)
   660 		{
   647 		{
   661 		aReq->ReturnCode() = pRC->HandleReservationOfObjects(*aReq);
   648 		aReq->ReturnCode() = pRC->HandleReservationOfObjects(*aReq);
   662 		aReq->Complete(aReq->ReturnCode(),ETrue);
   649 		aReq->Complete(aReq->ReturnCode(),ETrue);
   663 		return;
   650 		return;
   702     else if(aReq->ClientId() & USER_SIDE_CLIENT_BIT_MASK)
   689     else if(aReq->ClientId() & USER_SIDE_CLIENT_BIT_MASK)
   703 		pC = pRC->iUserSideClientList[TUint16(aReq->ClientId() & ID_INDEX_BIT_MASK)];
   690 		pC = pRC->iUserSideClientList[TUint16(aReq->ClientId() & ID_INDEX_BIT_MASK)];
   704 	else
   691 	else
   705 		pC = pRC->iClientList[TUint16(aReq->ClientId() & ID_INDEX_BIT_MASK)];
   692 		pC = pRC->iClientList[TUint16(aReq->ClientId() & ID_INDEX_BIT_MASK)];
   706 
   693 
   707 	DStaticPowerResource *pR = aReq->Resource();
       
   708     TUint aResourceId = aReq->ResourceId();
   694     TUint aResourceId = aReq->ResourceId();
   709     TInt r = aReq->ReturnCode();
   695     TInt r = aReq->ReturnCode();
   710     if(aReq->ReqType()==TPowerRequest::EGet)
   696     if(aReq->ReqType()==TPowerRequest::EGet)
   711 		{
   697 		{
   712 		TInt aState = aReq->Level();
   698 		TInt aState = aReq->Level();
   741 void DPowerResourceController::MsgQDependencyFunc(TAny* aPtr)
   727 void DPowerResourceController::MsgQDependencyFunc(TAny* aPtr)
   742 	{
   728 	{
   743     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::MsgQDependencyFunc"));
   729     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::MsgQDependencyFunc"));
   744     DPowerResourceController* pRC=(DPowerResourceController*)aPtr;
   730     DPowerResourceController* pRC=(DPowerResourceController*)aPtr;
   745     TPowerRequest* aReq=(TPowerRequest*)pRC->iMsgQDependency->iMessage;
   731     TPowerRequest* aReq=(TPowerRequest*)pRC->iMsgQDependency->iMessage;
       
   732 	DStaticPowerResource *pR = aReq->Resource();
   746 	pRC->HandleDependencyMsg(*aReq);
   733 	pRC->HandleDependencyMsg(*aReq);
   747 	if((aReq->ResourceId() & KIdMaskDynamic) && (aReq->ResourceCb()))
   734 	if((aReq->ResourceId() & KIdMaskDynamic) && (aReq->ResourceCb()))
   748 		{
   735 		{
   749 		pRC->Lock();
   736 		pRC->Lock();
   750 		((DDynamicPowerResource*)aReq->Resource())->UnLock();
   737 		((DDynamicPowerResource*)aReq->Resource())->UnLock();
   775     else if(aReq->ClientId() & USER_SIDE_CLIENT_BIT_MASK)
   762     else if(aReq->ClientId() & USER_SIDE_CLIENT_BIT_MASK)
   776 		pC = pRC->iUserSideClientList[TUint16(aReq->ClientId() & ID_INDEX_BIT_MASK)];
   763 		pC = pRC->iUserSideClientList[TUint16(aReq->ClientId() & ID_INDEX_BIT_MASK)];
   777 	else
   764 	else
   778 		pC = pRC->iClientList[TUint16(aReq->ClientId() & ID_INDEX_BIT_MASK)];
   765 		pC = pRC->iClientList[TUint16(aReq->ClientId() & ID_INDEX_BIT_MASK)];
   779 
   766 
   780 	DStaticPowerResource *pR = aReq->Resource();
       
   781     TUint aResourceId = aReq->ResourceId();
   767     TUint aResourceId = aReq->ResourceId();
   782     TInt r = aReq->ReturnCode();
   768     TInt r = aReq->ReturnCode();
   783     if(aReq->ReqType()==TPowerRequest::EGet)
   769     if(aReq->ReqType()==TPowerRequest::EGet)
   784 		{
   770 		{
   785 		TInt aState = aReq->Level();
   771 		TInt aState = aReq->Level();
   862 	//Create a Kernel client object for Power Controller
   848 	//Create a Kernel client object for Power Controller
   863 	Lock();
   849 	Lock();
   864 	SPowerResourceClient * pC = NULL;
   850 	SPowerResourceClient * pC = NULL;
   865 	// By now client pool should be created
   851 	// By now client pool should be created
   866 	LIST_POP(iClientPool, pC, iNextInList);
   852 	LIST_POP(iClientPool, pC, iNextInList);
       
   853 	TUint16 growBy = iClientList.GrowBy();
   867 	if(!pC)
   854 	if(!pC)
   868 		{
   855 		{
   869 		UnLock();
   856 		UnLock();
   870 		TUint16 growBy = iClientList.GrowBy();
       
   871 		// coverity[alloc_fn]
   857 		// coverity[alloc_fn]
   872 		SPowerResourceClient *pCL = new SPowerResourceClient[growBy];
   858 		SPowerResourceClient *pCL = new SPowerResourceClient[growBy];
   873 		if(!pCL)
   859 		if(!pCL)
   874 			{
   860 			{
   875 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("No memory to grow client pool"));
   861 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("No memory to grow client pool"));
   876 			Panic(ENoMemToCreatePowerControllerClient);
   862 			Panic(ENoMemToCreatePowerControllerClient);
   877 			}
   863 			}
   878 		if(iClientList.ReSize(growBy))
   864 #ifdef RESOURCE_MANAGER_SIMULATED_PSL
   879 			Panic(ENoMemToCreatePowerControllerClient);
   865 		iCleanList.Append(pCL);
       
   866 #endif
   880 		Lock();
   867 		Lock();
   881         for(count = 0; count < growBy-1; count++)
   868         for(count = 0; count < growBy-1; count++)
   882 			LIST_PUSH(iClientPool, &pCL[count], iNextInList);
   869 			LIST_PUSH(iClientPool, &pCL[count], iNextInList);
   883 		pC = &pCL[count];
   870 		pC = &pCL[count];
   884 #ifdef PRM_INSTRUMENTATION_MACRO
   871 #ifdef PRM_INSTRUMENTATION_MACRO
   885 	TUint size = growBy *sizeof(SPowerResourceClient);
   872 	TUint size = growBy *sizeof(SPowerResourceClient);
   886 	PRM_MEMORY_USAGE_TRACE
   873 	PRM_MEMORY_USAGE_TRACE
   887 #endif
   874 #endif
   888 		}
   875 		}
   889 	pC->iName = (const TDesC8*)&KPowerController;
   876 	pC->iName = (const TDesC8*)&KPowerController;
   890     iClientList.Add(pC, iPowerControllerId);
   877 	UnLock();
       
   878 	if(iClientList.Allocd()==iClientList.Count())
       
   879 		{
       
   880 		if(iClientList.ReSize(growBy))
       
   881 			{
       
   882 			Panic(ENoMemToCreatePowerControllerClient);
       
   883 			}
       
   884 		}
       
   885 	Lock();
       
   886 	iClientList.Add(pC, iPowerControllerId);
   891 	pC->iClientId = iPowerControllerId | CLIENT_POWER_CONTROLLER_BIT_MASK;
   887 	pC->iClientId = iPowerControllerId | CLIENT_POWER_CONTROLLER_BIT_MASK;
   892 	iPowerControllerId = pC->iClientId;
   888 	iPowerControllerId = pC->iClientId;
   893     iClientCount++;
   889     iClientCount++;
   894     if(TPowerController::PowerController())
   890     if(TPowerController::PowerController())
   895        TPowerController::PowerController()->RegisterResourceController(this, iPowerControllerId);
   891        TPowerController::PowerController()->RegisterResourceController(this, iPowerControllerId);
   949    This takes care of updating the resource level for each client. 
   945    This takes care of updating the resource level for each client. 
   950 */
   946 */
   951 TInt DPowerResourceController::CheckLevelAndAddClient(SPowerResourceClient* pC, TPowerRequest* aReq)
   947 TInt DPowerResourceController::CheckLevelAndAddClient(SPowerResourceClient* pC, TPowerRequest* aReq)
   952 	{
   948 	{
   953     //Client level addition in state change needs to be taken care.
   949     //Client level addition in state change needs to be taken care.
   954     __KTRACE_OPT(KRESMANAGER, Kern::Printf("DPowerResourceController::CheckLevelAndAddClient, ClientId = 0x%x, \
   950     __KTRACE_OPT(KRESMANAGER, Kern::Printf("DPowerResourceController::CheckLevelAndAddClient, ClientId = 0x%x, ResourceId = %d, ReqType = %d",
   955 		                     ResourceId = %d, ReqType = %d", pC->iClientId, aReq->ResourceId(), aReq->ReqType()));
   951 													pC->iClientId, aReq->ResourceId(), aReq->ReqType()));
   956 	
   952 	
   957 	SPowerResourceClientLevel* pSCL=NULL; //Place to hold the current client
   953 	SPowerResourceClientLevel* pSCL=NULL; //Place to hold the current client
   958 	SPowerResourceClientLevel* pMCL=NULL; //Place to hold the prevailing client.
   954 	SPowerResourceClientLevel* pMCL=NULL; //Place to hold the prevailing client.
   959 	DStaticPowerResource* aResource = aReq->Resource();
   955 	DStaticPowerResource* aResource = aReq->Resource();
   960     aReq->RequiresChange() = EFalse;
   956     aReq->RequiresChange() = EFalse;
  1007 		{
  1003 		{
  1008 		// If the client is new, get free client level from pool and populate with client information
  1004 		// If the client is new, get free client level from pool and populate with client information
  1009 		// and add it to the client list and in resource list.
  1005 		// and add it to the client list and in resource list.
  1010         if((pC->iReservedCl ==0) && !iClientLevelPoolCount)
  1006         if((pC->iReservedCl ==0) && !iClientLevelPoolCount)
  1011 			{
  1007 			{
  1012 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("Client level quota exhausted and its free pool empty, \
  1008 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("Client level quota exhausted and its free pool empty, iReservedCl = %d, iClientLevelPoolCount = %d", 
  1013 				        iReservedCl = %d, iClientLevelPoolCount = %d", pC->iReservedCl, iClientLevelPoolCount));
  1009 													pC->iReservedCl, iClientLevelPoolCount));
  1014 			return KErrUnderflow;
  1010 			return KErrUnderflow;
  1015 			}
  1011 			}
  1016         LIST_POP(iClientLevelPool, pSCL, iNextInList);
  1012         LIST_POP(iClientLevelPool, pSCL, iNextInList);
  1017         pSCL->iClientId=pC->iClientId;
  1013         pSCL->iClientId=pC->iClientId;
  1018         pSCL->iResourceId=aReq->ResourceId();
  1014         pSCL->iResourceId=aReq->ResourceId();
  1130 
  1126 
  1131 	//If the level is in increasing order with respect to sense the change is allowed.
  1127 	//If the level is in increasing order with respect to sense the change is allowed.
  1132     if(((aResource->Sense() == DStaticPowerResource::ENegative) && aReq->Level()<CurrentLevel) || 
  1128     if(((aResource->Sense() == DStaticPowerResource::ENegative) && aReq->Level()<CurrentLevel) || 
  1133 		                 ((aResource->Sense() == DStaticPowerResource::EPositive) && aReq->Level()>CurrentLevel))
  1129 		                 ((aResource->Sense() == DStaticPowerResource::EPositive) && aReq->Level()>CurrentLevel))
  1134 		{
  1130 		{
  1135 		__KTRACE_OPT(KRESMANAGER, Kern::Printf("Resource is in increasing order with respect to sense and level is %d",\
  1131 		__KTRACE_OPT(KRESMANAGER, Kern::Printf("Resource is in increasing order with respect to sense and level is %d",
  1136 			                                                                                           aReq->Level()));
  1132 			                                                                                           aReq->Level()));
  1137         aReq->RequiresChange()=ETrue;
  1133         aReq->RequiresChange()=ETrue;
  1138         return KErrNone;
  1134         return KErrNone;
  1139 		}
  1135 		}
  1140     if((TInt)pC->iClientId == aResource->iLevelOwnerId)
  1136     if((TInt)pC->iClientId == aResource->iLevelOwnerId)
  1188     @return KErrNoMemory if one the prealocations fails
  1184     @return KErrNoMemory if one the prealocations fails
  1189     */
  1185     */
  1190 TInt DPowerResourceController::InitPools(TUint16 aKClients, TUint16 aUClients, TUint16 aNClientLevels, TUint16 aNRequests)
  1186 TInt DPowerResourceController::InitPools(TUint16 aKClients, TUint16 aUClients, TUint16 aNClientLevels, TUint16 aNRequests)
  1191 	{
  1187 	{
  1192     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::InitPools"));
  1188     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::InitPools"));
  1193 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("aKClients = %d, aUClients = %d, aNClientLevels = %d, aNRequests = %d", \
  1189 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("aKClients = %d, aUClients = %d, aNClientLevels = %d, aNRequests = %d",
  1194 		                                                     aKClients, aUClients, aNClientLevels, aNRequests));
  1190 		                                                     aKClients, aUClients, aNClientLevels, aNRequests));
  1195     __ASSERT_ALWAYS((iInitialised == EResConCreated) && !(iClientPool || iRequestPool || iClientLevelPool), Kern::Fault("Already initialized"
  1191     __ASSERT_ALWAYS((iInitialised == EResConCreated) && !(iClientPool || iRequestPool || iClientLevelPool), Kern::Fault("Already initialized"
  1196 		                                                     __FILE__, __LINE__));
  1192 		                                                     __FILE__, __LINE__));
  1197 
  1193 
  1198     // Create client pool
  1194     // Create client pool
  1265 			iClientList.Delete();
  1261 			iClientList.Delete();
  1266 			iUserSideClientList.Delete();
  1262 			iUserSideClientList.Delete();
  1267 			return KErrNoMemory;
  1263 			return KErrNoMemory;
  1268 			}
  1264 			}
  1269 		}
  1265 		}
       
  1266 #ifdef RESOURCE_MANAGER_SIMULATED_PSL
       
  1267 	iCleanList.Append(pC);
       
  1268 #endif
  1270 	Lock();
  1269 	Lock();
  1271 	TUint16 c;
  1270 	TUint16 c;
  1272 	for(c = 0; c < iStaticResDependencyCount; c++)
  1271 	for(c = 0; c < iStaticResDependencyCount; c++)
  1273 		{
  1272 		{
  1274 		LIST_PUSH(iResourceLevelPool, &pRL[c], iNextInList);
  1273 		LIST_PUSH(iResourceLevelPool, &pRL[c], iNextInList);
  1275 		}
  1274 		}
  1276 	iResourceLevelPoolCount = iStaticResDependencyCount;
  1275 	iResourceLevelPoolCount = iStaticResDependencyCount;
  1277 #else
  1276 #else
       
  1277 #ifdef RESOURCE_MANAGER_SIMULATED_PSL
       
  1278     iCleanList.Append(pC);
       
  1279 #endif
  1278     Lock();
  1280     Lock();
  1279     TUint16 c;
  1281     TUint16 c;
  1280 #endif
  1282 #endif
  1281     // Create Client pool list
  1283     // Create Client pool list
  1282     for(c = 0; c< (aKClients + aUClients); c++)
  1284     for(c = 0; c< (aKClients + aUClients); c++)
  1342 @pre Call in a thread context, but not from null thread or DFC thread1.
  1344 @pre Call in a thread context, but not from null thread or DFC thread1.
  1343 @pre Can be used in a device driver
  1345 @pre Can be used in a device driver
  1344 */
  1346 */
  1345 TInt DPowerResourceController::RegisterClient(TUint& aClientId, const TDesC8& aName, TOwnerType aType)
  1347 TInt DPowerResourceController::RegisterClient(TUint& aClientId, const TDesC8& aName, TOwnerType aType)
  1346 	{
  1348 	{
  1347 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::RegisterClient, Name = %S, \
  1349 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::RegisterClient, Name = %S, Type = %d", &aName, aType));
  1348 		                                                                 Type = %d", &aName, aType));
       
  1349 	DThread& thread = Kern::CurrentThread();	
  1350 	DThread& thread = Kern::CurrentThread();	
  1350 	CHECK_CONTEXT(thread)
  1351 	CHECK_CONTEXT(thread)
  1351 	//If number of expected kernel side clients is set to 0 during initial configuration
  1352 	//If number of expected kernel side clients is set to 0 during initial configuration
  1352 	//then dont allow to configure kernel side clients.
  1353 	//then dont allow to configure kernel side clients.
  1353 	if(!iClientList.GrowBy())
  1354 	if(!iClientList.GrowBy())
  1412 	TUint clientId;
  1413 	TUint clientId;
  1413 	if(aRequest.ReqType() == TPowerRequest::ERegisterKernelClient)
  1414 	if(aRequest.ReqType() == TPowerRequest::ERegisterKernelClient)
  1414 		{
  1415 		{
  1415 		//Get Next client from FreePool
  1416 		//Get Next client from FreePool
  1416 		LIST_POP(iClientPool, pC, iNextInList);
  1417 		LIST_POP(iClientPool, pC, iNextInList);
       
  1418 
       
  1419 		TUint16 growBy = iClientList.GrowBy();
  1417 		if(!pC)
  1420 		if(!pC)
  1418 			{
  1421 			{
  1419 			//Free Pool is empty, so try to grow the pool.
  1422 			//Free Pool is empty, so try to grow the pool.
  1420 			TUint16 growBy = iClientList.GrowBy();
       
  1421 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("Client pool exhausted so growing client Pool by %d", growBy));
  1423 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("Client pool exhausted so growing client Pool by %d", growBy));
  1422 			// coverity[alloc_fn]
  1424 			// coverity[alloc_fn]
  1423 			SPowerResourceClient *pCL = new SPowerResourceClient[growBy];
  1425 			SPowerResourceClient *pCL = (SPowerResourceClient*)Kern::Alloc(sizeof(SPowerResourceClient) * growBy);
  1424 			if(!pCL)
  1426 			if(!pCL)
  1425 				{
  1427 				{
  1426 				__KTRACE_OPT(KRESMANAGER, Kern::Printf("No memory to grow client pool"));
  1428 				__KTRACE_OPT(KRESMANAGER, Kern::Printf("No memory to grow client pool"));
  1427 				return(KErrNoMemory);
  1429 				return(KErrNoMemory);
  1428 				}
  1430 				}
  1429 			//Resize the container for holding client list
  1431 #ifdef RESOURCE_MANAGER_SIMULATED_PSL
  1430 			if(!iClientList.ReSize(growBy))
  1432 			iCleanList.Append(pCL);
  1431 				{
  1433 #endif
  1432 				__KTRACE_OPT(KRESMANAGER, Kern::Printf("No memory for client container allocation"));
       
  1433 				delete []pCL;
       
  1434 				return(KErrNoMemory);
       
  1435 				}
       
  1436 			Lock();
  1434 			Lock();
  1437 			TUint16 count;
  1435 			TUint16 count;
  1438 			for(count = 0; count < growBy-1; count++)
  1436 			for(count = 0; count < growBy-1; count++)
  1439 				LIST_PUSH(iClientPool, &pCL[count], iNextInList);
  1437 				LIST_PUSH(iClientPool, &pCL[count], iNextInList);
  1440 			UnLock();
  1438 			UnLock();
  1443 	PRM_MEMORY_USAGE_TRACE
  1441 	PRM_MEMORY_USAGE_TRACE
  1444 #endif
  1442 #endif
  1445 			pC = &pCL[count];
  1443 			pC = &pCL[count];
  1446 			}
  1444 			}
  1447 		pC->iClientId = 0;
  1445 		pC->iClientId = 0;
       
  1446 		if(iClientList.Allocd()==iClientList.Count())
       
  1447 			{
       
  1448 			//Resize the container for holding client list
       
  1449 			if(iClientList.ReSize(growBy)!=KErrNone)
       
  1450 				{
       
  1451 				__KTRACE_OPT(KRESMANAGER, Kern::Printf("No memory for client container allocation"));
       
  1452 				return(KErrNoMemory);
       
  1453 				}
       
  1454 			}
  1448 		Lock();
  1455 		Lock();
  1449 		iClientList.Add(pC, clientId);
  1456 		iClientList.Add(pC, clientId);
  1450 		++iClientCount;
  1457 		++iClientCount;
  1451 		UnLock();
  1458 		UnLock();
  1452 		}
  1459 		}
  1453 	else // Request is registration of user side client
  1460 	else // Request is registration of user side client
  1454 		{
  1461 		{
  1455 		//Get Next client from FreePool
  1462 		//Get Next client from FreePool
  1456 		LIST_POP(iClientPool, pC, iNextInList);
  1463 		LIST_POP(iClientPool, pC, iNextInList);
       
  1464 		TUint16 growBy = iUserSideClientList.GrowBy();
  1457 		if(!pC)
  1465 		if(!pC)
  1458 			{
  1466 			{
  1459 			//Free Pool is empty, so try to grow the pool.
  1467 			//Free Pool is empty, so try to grow the pool.
  1460 			TUint16 growBy = iUserSideClientList.GrowBy();
  1468 			SPowerResourceClient *pCL = (SPowerResourceClient*)Kern::Alloc(sizeof(SPowerResourceClient) * growBy);
  1461 			SPowerResourceClient* pCL = new SPowerResourceClient[growBy];
       
  1462 			if(!pCL)
  1469 			if(!pCL)
  1463 				{
  1470 				{
  1464 				return KErrNoMemory;
  1471 				return KErrNoMemory;
  1465 				}
  1472 				}
  1466 			//Resize the container for holding client list
  1473 #ifdef RESOURCE_MANAGER_SIMULATED_PSL
  1467 			if(!iUserSideClientList.ReSize(growBy))
  1474 			iCleanList.Append(pCL);
  1468 				{
  1475 #endif
  1469 				__KTRACE_OPT(KRESMANAGER, Kern::Printf("No memory for container class allocation"));
       
  1470 				delete []pCL;
       
  1471 				return KErrNoMemory;
       
  1472 				}
       
  1473 			Lock();
  1476 			Lock();
  1474 			TUint16 count;
  1477 			TUint16 count;
  1475 			for(count = 0; count < growBy - 1; count++)
  1478 			for(count = 0; count < growBy - 1; count++)
  1476 				LIST_PUSH(iClientPool, &pCL[count], iNextInList);
  1479 				LIST_PUSH(iClientPool, &pCL[count], iNextInList);
  1477 			UnLock();
  1480 			UnLock();
  1483 			}
  1486 			}
  1484 		pC->iClientId = 0;
  1487 		pC->iClientId = 0;
  1485 		//User side clients are always thread relative as they execute in the context of proxy driver.
  1488 		//User side clients are always thread relative as they execute in the context of proxy driver.
  1486 		pC->iClientId = CLIENT_THREAD_RELATIVE_BIT_MASK; //Set 31st bit;
  1489 		pC->iClientId = CLIENT_THREAD_RELATIVE_BIT_MASK; //Set 31st bit;
  1487 		pC->iClientId|=USER_SIDE_CLIENT_BIT_MASK;
  1490 		pC->iClientId|=USER_SIDE_CLIENT_BIT_MASK;
       
  1491 		if(iUserSideClientList.Allocd()==iUserSideClientList.Count())
       
  1492 			{
       
  1493 			//Resize the container for holding client list
       
  1494 			if(iUserSideClientList.ReSize(growBy)!=KErrNone)
       
  1495 				{
       
  1496 				__KTRACE_OPT(KRESMANAGER, Kern::Printf("No memory for container class allocation"));
       
  1497 				return KErrNoMemory;
       
  1498 				}
       
  1499 			}
  1488 		Lock();
  1500 		Lock();
  1489 		iUserSideClientList.Add(pC, clientId);
  1501 		iUserSideClientList.Add(pC, clientId);
  1490 		++iUserSideClientCount;
  1502 		++iUserSideClientCount;
  1491 		UnLock();
  1503 		UnLock();
  1492 		}
  1504 		}
  1520 void DPowerResourceController::ResourceStateChangeOfClientLevels(SPowerResourceClient* pC)
  1532 void DPowerResourceController::ResourceStateChangeOfClientLevels(SPowerResourceClient* pC)
  1521 	{
  1533 	{
  1522 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::ResourceStateChangeOfClientLevels"));
  1534 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::ResourceStateChangeOfClientLevels"));
  1523     TPowerRequest* pReq = (TPowerRequest*)&TPowerRequest::Get();
  1535     TPowerRequest* pReq = (TPowerRequest*)&TPowerRequest::Get();
  1524     DStaticPowerResource* pR = NULL;
  1536     DStaticPowerResource* pR = NULL;
  1525     TInt r = KErrNone;
       
  1526     SPowerResourceClientLevel* pCL = pC->iLevelList;
  1537     SPowerResourceClientLevel* pCL = pC->iLevelList;
  1527     SPowerResourceClientLevel* pCLL = NULL;
  1538     SPowerResourceClientLevel* pCLL = NULL;
  1528 	while(pCL != NULL)
  1539 	while(pCL != NULL)
  1529 		{
  1540 		{
  1530         __KTRACE_OPT(KRESMANAGER, Kern::Printf("Client 0x%x has requirement on resource %d", pCL->iClientId, \
  1541         __KTRACE_OPT(KRESMANAGER, Kern::Printf("Client 0x%x has requirement on resource %d", pCL->iClientId, pCL->iResourceId));
  1531 			                                                                                 pCL->iResourceId));
       
  1532 #ifdef PRM_ENABLE_EXTENDED_VERSION
  1542 #ifdef PRM_ENABLE_EXTENDED_VERSION
  1533 		switch((pCL->iResourceId >>RESOURCE_BIT_IN_ID_CHECK) & 0x3)													
  1543 		switch((pCL->iResourceId >>RESOURCE_BIT_IN_ID_CHECK) & 0x3)													
  1534 		{																		
  1544 		{																		
  1535 		case PRM_STATIC_RESOURCE:												
  1545 		case PRM_STATIC_RESOURCE:												
  1536 			pR = iStaticResourceArray[pCL->iResourceId - 1];								
  1546 			pR = iStaticResourceArray[pCL->iResourceId - 1];								
  1547 		}
  1557 		}
  1548 #else
  1558 #else
  1549 		pR = iStaticResourceArray[pCL->iResourceId -1];
  1559 		pR = iStaticResourceArray[pCL->iResourceId -1];
  1550 #endif
  1560 #endif
  1551 #ifdef PRM_ENABLE_EXTENDED_VERSION
  1561 #ifdef PRM_ENABLE_EXTENDED_VERSION
  1552 		if((((TInt)pCL->iClientId == pR->iLevelOwnerId) || (pR->Sense() == DStaticPowerResource::ECustom)) && (!(pCL->iResourceId & KIdMaskDynamic) ||
  1562 		if(((pR->Sense() == DStaticPowerResource::ECustom) || ((TInt)pCL->iClientId == pR->iLevelOwnerId)) && (!(pCL->iResourceId & KIdMaskDynamic) ||
  1553 			         ((pCL->iResourceId & KIdMaskDynamic) && (((DDynamicPowerResource*)pR)->LockCount() != 0))))
  1563 			         ((pCL->iResourceId & KIdMaskDynamic) && (((DDynamicPowerResource*)pR)->LockCount() != 0))))
  1554 #else
  1564 #else
  1555 			if(((TInt)pCL->iClientId == pR->iLevelOwnerId) || (pR->Sense() == DStaticPowerResource::ECustom))
  1565 		if((pR->Sense() == DStaticPowerResource::ECustom) || ((TInt)pCL->iClientId == pR->iLevelOwnerId)) 
  1556 #endif
  1566 #endif
  1557 		    {
  1567 		    {
  1558             pReq->ReqType() = TPowerRequest::ESetDefaultLevel;
  1568             pReq->ReqType() = TPowerRequest::ESetDefaultLevel;
  1559             pReq->ResourceId() = pCL->iResourceId;
  1569             pReq->ResourceId() = pCL->iResourceId;
  1560             pReq->ClientId() = pCL->iClientId;
  1570             pReq->ClientId() = pCL->iClientId;
  1561             pReq->Resource() = pR;
  1571             pReq->Resource() = pR;
  1562 			pReq->Level() = pR->iCachedLevel;
  1572 			pReq->Level() = pR->iCachedLevel;
  1563             pReq->ResourceCb() = NULL;
  1573             pReq->ResourceCb() = NULL;
  1564 			pReq->ReturnCode() = KErrNone;
  1574 			pReq->ReturnCode() = KErrNone;
  1565             r = KErrNone;
       
  1566 #ifdef PRM_INSTRUMENTATION_MACRO
  1575 #ifdef PRM_INSTRUMENTATION_MACRO
  1567 			//Setting level to current level as correct level will be known only at the end,
  1576 			//Setting level to current level as correct level will be known only at the end,
  1568 			TInt aNewState = pR->iCachedLevel; 
  1577 			TInt aNewState = pR->iCachedLevel; 
  1569 			TUint aResourceId = pReq->ResourceId(); 
  1578 			TUint aResourceId = pReq->ResourceId(); 
  1570 			PRM_CLIENT_CHANGE_STATE_START_TRACE
  1579 			PRM_CLIENT_CHANGE_STATE_START_TRACE
  1571 #endif
  1580 #endif
       
  1581 			TInt r = KErrNone;
  1572 			if(pR->LatencySet())
  1582 			if(pR->LatencySet())
  1573 				{
  1583 				{
  1574 #ifdef PRM_ENABLE_EXTENDED_VERSION
  1584 #ifdef PRM_ENABLE_EXTENDED_VERSION
  1575 				if(pCL->iResourceId & KIdMaskDynamic)
  1585 				if(pCL->iResourceId & KIdMaskDynamic)
  1576 					((DDynamicPowerResource*)pR)->Lock();
  1586 					((DDynamicPowerResource*)pR)->Lock();
  1598 				else
  1608 				else
  1599 					{
  1609 					{
  1600 					pReq->ClientId() = -1;
  1610 					pReq->ClientId() = -1;
  1601 					pReq->Level() = pR->iDefaultLevel;
  1611 					pReq->Level() = pR->iDefaultLevel;
  1602 					}
  1612 					}
       
  1613 
  1603 				if((!pR->Usage()) || (pR->Usage() && pReq->RequiresChange()))
  1614 				if((!pR->Usage()) || (pR->Usage() && pReq->RequiresChange()))
  1604 					{
  1615 					{
  1605 					// NOTE:Not checking error here as no action can be taken based on error.
  1616 					// NOTE:Not checking error here as no action can be taken based on error.
  1606 					UnLock();
  1617 					if(pR->iCachedLevel != pReq->Level())
  1607 					r = pR->DoRequest(*pReq);
  1618 						{
  1608 					Lock();
  1619 						UnLock();
       
  1620 						r = pR->DoRequest(*pReq);
       
  1621 						Lock();
       
  1622 						}
  1609 					CompleteNotifications(pReq->ClientId(), pReq->Resource(), pReq->Level(), 
  1623 					CompleteNotifications(pReq->ClientId(), pReq->Resource(), pReq->Level(), 
  1610 						                            pReq->ReturnCode(), pReq->ClientId(), EFalse);
  1624 						                            pReq->ReturnCode(), pReq->ClientId(), EFalse);
       
  1625 #ifdef PRM_INSTRUMENTATION_MACRO
  1611 					PRM_CLIENT_CHANGE_STATE_END_TRACE
  1626 					PRM_CLIENT_CHANGE_STATE_END_TRACE
       
  1627 #endif
  1612 					pR->iLevelOwnerId = pReq->ClientId();
  1628 					pR->iLevelOwnerId = pReq->ClientId();
  1613 					pR->iCachedLevel = pReq->Level();
  1629 					pR->iCachedLevel = pReq->Level();
  1614 					if(pR->iIdleListEntry)
  1630 					if(pR->iIdleListEntry)
  1615 						{
  1631 						{
  1616 						SIdleResourceInfo* pI = (SIdleResourceInfo*)pR->iIdleListEntry;
  1632 						SIdleResourceInfo* pI = (SIdleResourceInfo*)pR->iIdleListEntry;
  1617 						pI->iLevelOwnerId = pReq->ClientId();
  1633 						pI->iLevelOwnerId = pReq->ClientId();
  1618 						pI->iCurrentLevel = pReq->Level();
  1634 						pI->iCurrentLevel = pReq->Level();
  1619 						}
  1635 						}
  1620 					}
  1636 					}
  1621 				}
  1637 				}
       
  1638 
  1622 			}
  1639 			}
  1623 		/* Deque from resource */
  1640 		/* Deque from resource */
  1624 		pCLL = pCL;
  1641 		pCLL = pCL;
  1625 		pCL = pCL->iNextInList;
  1642 		pCL = pCL->iNextInList;
  1626 		pCLL->Deque();
  1643 		pCLL->Deque();
  1742 @pre Call in a thread context but not from null thread or DFC thread1
  1759 @pre Call in a thread context but not from null thread or DFC thread1
  1743 @pre Can be used in a device driver
  1760 @pre Can be used in a device driver
  1744 */
  1761 */
  1745 TInt DPowerResourceController::GetClientName(TUint aClientId, TUint aTargetClientId, TDes8& aName)
  1762 TInt DPowerResourceController::GetClientName(TUint aClientId, TUint aTargetClientId, TDes8& aName)
  1746 	{
  1763 	{
  1747 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetClientName, CallingClientId = 0x%x, \
  1764 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetClientName, CallingClientId = 0x%x, TargetClientId = 0x%x", aClientId, aTargetClientId));
  1748 		                                                       TargetClientId = 0x%x", aClientId, aTargetClientId));
       
  1749 	DThread& thread = Kern::CurrentThread();	
  1765 	DThread& thread = Kern::CurrentThread();	
  1750 	CHECK_CONTEXT(thread)
  1766 	CHECK_CONTEXT(thread)
  1751 	if((aName.MaxLength() - aName.Length()) < KMaxClientNameLength)
  1767 	if((aName.MaxLength() - aName.Length()) < KMaxClientNameLength)
  1752 		return KErrArgument;
  1768 		return KErrArgument;
  1753 	SPowerResourceClient* pC = NULL;
  1769 	SPowerResourceClient* pC = NULL;
  1789 @pre Call in a thread context but not from null thread or DFC thread1
  1805 @pre Call in a thread context but not from null thread or DFC thread1
  1790 @pre Can be used in a device driver
  1806 @pre Can be used in a device driver
  1791 */
  1807 */
  1792 TInt DPowerResourceController::GetClientId(TUint aClientId, TDesC8& aClientName, TUint& aTargetClientId)
  1808 TInt DPowerResourceController::GetClientId(TUint aClientId, TDesC8& aClientName, TUint& aTargetClientId)
  1793 	{
  1809 	{
  1794 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetClientId CallingClientId = 0x%x, \
  1810 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetClientId CallingClientId = 0x%x, ClientName = %S", aClientId, &aClientName));
  1795 		                                                           ClientName = %S", aClientId, &aClientName));
       
  1796 	DThread& thread = Kern::CurrentThread();	
  1811 	DThread& thread = Kern::CurrentThread();	
  1797 	CHECK_CONTEXT(thread)
  1812 	CHECK_CONTEXT(thread)
  1798 	if(aClientName.Length() > KMaxClientNameLength)
  1813 	if(aClientName.Length() > KMaxClientNameLength)
  1799 		return KErrTooBig;
  1814 		return KErrTooBig;
  1800 	SPowerResourceClient* pC = NULL;
  1815 	SPowerResourceClient* pC = NULL;
  1852 	for(count = 0; count < iStaticResourceArrayEntries; count++)
  1867 	for(count = 0; count < iStaticResourceArrayEntries; count++)
  1853 		{
  1868 		{
  1854 		if((iStaticResourceArray[count]) && (!(aResourceName.Compare(*(const TDesC8*)iStaticResourceArray[count]->iName))))
  1869 		if((iStaticResourceArray[count]) && (!(aResourceName.Compare(*(const TDesC8*)iStaticResourceArray[count]->iName))))
  1855 			{
  1870 			{
  1856 			aResourceId = ++count;
  1871 			aResourceId = ++count;
  1857 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetResourceId, ResourceId = 0x%x", \
  1872 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetResourceId, ResourceId = 0x%x", aResourceId));
  1858 				                                                                                    aResourceId));
       
  1859 			UNLOCK_RETURN(KErrNone);
  1873 			UNLOCK_RETURN(KErrNone);
  1860 			}
  1874 			}
  1861 		}
  1875 		}
  1862 #ifdef PRM_ENABLE_EXTENDED_VERSION
  1876 #ifdef PRM_ENABLE_EXTENDED_VERSION
  1863 	//Search in dynamic resource with no dependencies array for specified resource name.
  1877 	//Search in dynamic resource with no dependencies array for specified resource name.
  1873 	for(count = 0; count < iStaticResDependencyCount; count++)
  1887 	for(count = 0; count < iStaticResDependencyCount; count++)
  1874 		{
  1888 		{
  1875 		if(!(aResourceName.Compare(*(const TDesC8*)iStaticResDependencyArray[count]->iName)))
  1889 		if(!(aResourceName.Compare(*(const TDesC8*)iStaticResDependencyArray[count]->iName)))
  1876 			{
  1890 			{
  1877 			aResourceId = iStaticResDependencyArray[count]->iResourceId;
  1891 			aResourceId = iStaticResDependencyArray[count]->iResourceId;
  1878 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetResourceId, ResourceId = 0x%x", \
  1892 			__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetResourceId, ResourceId = 0x%x", aResourceId));
  1879 				                                                                                    aResourceId));
       
  1880 			UNLOCK_RETURN(KErrNone);
  1893 			UNLOCK_RETURN(KErrNone);
  1881 			}
  1894 			}
  1882 		}
  1895 		}
  1883 	//Search in dynamic resource with dependencies (if exists) for specified resource name
  1896 	//Search in dynamic resource with dependencies (if exists) for specified resource name
  1884 	DDynamicPowerResourceD* pDRD;
  1897 	DDynamicPowerResourceD* pDRD;
  1949 		UNLOCK_RETURN(KErrNotFound);
  1962 		UNLOCK_RETURN(KErrNotFound);
  1950 		}
  1963 		}
  1951 #else
  1964 #else
  1952 	if(!aResourceId)
  1965 	if(!aResourceId)
  1953 		{
  1966 		{
  1954 		__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetResourceInfo, return value = %d", \
  1967 		__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetResourceInfo, return value = %d", KErrNotFound));
  1955 			                                                                                          KErrNotFound));
       
  1956 		UNLOCK_RETURN(KErrNotFound);
  1968 		UNLOCK_RETURN(KErrNotFound);
  1957 		}
  1969 		}
  1958 	//Get resource from corresponding container
  1970 	//Get resource from corresponding container
  1959 	GET_RESOURCE_FROM_LIST(aResourceId, pR) 
  1971 	GET_RESOURCE_FROM_LIST(aResourceId, pR) 
  1960 #endif
  1972 #endif
  2015 		aNumResource = iStaticResourceCount + iDynamicResourceCount + iStaticResDependencyCount + 
  2027 		aNumResource = iStaticResourceCount + iDynamicResourceCount + iStaticResDependencyCount + 
  2016 			                                                          iDynamicResDependencyCount; 
  2028 			                                                          iDynamicResDependencyCount; 
  2017 #else
  2029 #else
  2018 		aNumResource = iStaticResourceCount;
  2030 		aNumResource = iStaticResourceCount;
  2019 #endif
  2031 #endif
  2020 		__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetNumResourcesInUseByClient, \
  2032 		__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetNumResourcesInUseByClient, numResources = %d", aNumResource));
  2021 			                                                              numResources = %d", aNumResource));
       
  2022 		UNLOCK_RETURN(KErrNone);
  2033 		UNLOCK_RETURN(KErrNone);
  2023 		}
  2034 		}
  2024 	GET_TARGET_CLIENT();
  2035 	GET_TARGET_CLIENT();
  2025 	SPowerResourceClientLevel* pCL = pC->iLevelList;
  2036 	SPowerResourceClientLevel* pCL = pC->iLevelList;
  2026 	aNumResource = 0;
  2037 	aNumResource = 0;
  2027 	while(pCL)
  2038 	while(pCL)
  2028 		{
  2039 		{
  2029 		aNumResource++;
  2040 		aNumResource++;
  2030 		pCL = pCL->iNextInList;
  2041 		pCL = pCL->iNextInList;
  2031 		}
  2042 		}
  2032 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetNumResourcesInUseByClient, \
  2043 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetNumResourcesInUseByClient, numResources = %d", aNumResource));
  2033 		                                                              numResources = %d", aNumResource));
       
  2034 	UNLOCK_RETURN(KErrNone);
  2044 	UNLOCK_RETURN(KErrNone);
  2035 	}
  2045 	}
  2036 
  2046 
  2037 /**
  2047 /**
  2038 @publishedPartner
  2048 @publishedPartner
  2191 		if(r != KErrNone)
  2201 		if(r != KErrNone)
  2192 			UNLOCK_RETURN(r);
  2202 			UNLOCK_RETURN(r);
  2193 		pInfo->Append(buf);
  2203 		pInfo->Append(buf);
  2194 		}
  2204 		}
  2195 	aNumResources = count;
  2205 	aNumResources = count;
  2196 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetInfoOnResourcesInUseByClient, \
  2206 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetInfoOnResourcesInUseByClient, AcutalNoOfResources = %d", aNumResources));
  2197 		                                                     AcutalNoOfResources = %d", aNumResources));
       
  2198 	UNLOCK_RETURN(KErrNone);
  2207 	UNLOCK_RETURN(KErrNone);
  2199 	}
  2208 	}
  2200 
  2209 
  2201 /**
  2210 /**
  2202 @publishedPartner
  2211 @publishedPartner
  2253 		UNLOCK_RETURN(KErrNotFound);
  2262 		UNLOCK_RETURN(KErrNotFound);
  2254 #endif
  2263 #endif
  2255 	aNumClients = 0;
  2264 	aNumClients = 0;
  2256 	for(SDblQueLink*pCL = pR->iClientList.First(); pCL != &pR->iClientList.iA; pCL=pCL->iNext)
  2265 	for(SDblQueLink*pCL = pR->iClientList.First(); pCL != &pR->iClientList.iA; pCL=pCL->iNext)
  2257 	   aNumClients++;
  2266 	   aNumClients++;
  2258 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetNumClientsUsingResource, \
  2267 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetNumClientsUsingResource, NumClients = %d", aNumClients));
  2259 		                                                                NumClients = %d", aNumClients));
       
  2260 	UNLOCK_RETURN(KErrNone);
  2268 	UNLOCK_RETURN(KErrNone);
  2261 	}
  2269 	}
  2262 
  2270 
  2263 /**
  2271 /**
  2264 @publishedPartner
  2272 @publishedPartner
  2371 		info.iClientId = pC->iClientId;
  2379 		info.iClientId = pC->iClientId;
  2372 		info.iClientName =  (TDesC8*)pC->iName;
  2380 		info.iClientName =  (TDesC8*)pC->iName;
  2373         pInfo->Append(TPckgC<TPowerClientInfoV01>(info));
  2381         pInfo->Append(TPckgC<TPowerClientInfoV01>(info));
  2374 		}
  2382 		}
  2375 	aNumClients = c;
  2383 	aNumClients = c;
  2376 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetInfoOnClientsUsingResource, \
  2384 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("<DPowerResourceController::GetInfoOnClientsUsingResource, NumClients = %d", aNumClients));
  2377 		                                                                NumClients = %d", aNumClients));
       
  2378 	UNLOCK_RETURN(KErrNone);
  2385 	UNLOCK_RETURN(KErrNone);
  2379 	}
  2386 	}
  2380 
  2387 
  2381 /**
  2388 /**
  2382 @publishedPartner
  2389 @publishedPartner
  2444 												                                TPowerResourceCb* aCb)
  2451 												                                TPowerResourceCb* aCb)
  2445 	{
  2452 	{
  2446 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::ChangeResourceState"));
  2453 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::ChangeResourceState"));
  2447 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("ClientId = 0x%x, ResourceId = %d, NewState = %d", aClientId, \
  2454 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("ClientId = 0x%x, ResourceId = %d, NewState = %d", aClientId, \
  2448 		                                                                          aResourceId, aNewState));
  2455 		                                                                          aResourceId, aNewState));
       
  2456 	DThread& thread = Kern::CurrentThread();	
  2449 	CHECK_CONTEXT(thread)
  2457 	CHECK_CONTEXT(thread)
  2450 	if(iInitialised <= EResConCreated)
  2458 	if(iInitialised <= EResConCreated)
  2451 		return KErrNotReady;
  2459 		return KErrNotReady;
  2452 	DThread& thread = Kern::CurrentThread();	
       
  2453 	if(!aResourceId)
  2460 	if(!aResourceId)
  2454 		return KErrNotFound;
  2461 		return KErrNotFound;
  2455 	SPowerResourceClient* pC = NULL;
  2462 	SPowerResourceClient* pC = NULL;
  2456 	TInt r = KErrNone;
  2463 	TInt r = KErrNone;
  2457 	Lock();
  2464 	Lock();
  2464 		UNLOCK_RETURN(KErrNotFound);
  2471 		UNLOCK_RETURN(KErrNotFound);
  2465 	DStaticPowerResource* pR = iStaticResourceArray[aResourceId-1];
  2472 	DStaticPowerResource* pR = iStaticResourceArray[aResourceId-1];
  2466 	if(!pR)
  2473 	if(!pR)
  2467 		UNLOCK_RETURN(KErrNotFound);
  2474 		UNLOCK_RETURN(KErrNotFound);
  2468 #endif
  2475 #endif
       
  2476 	//Return if the resource is already in that state and client is also the same.
       
  2477 	if((aNewState == pR->iCachedLevel) && ((TInt)aClientId == pR->iLevelOwnerId))
       
  2478 		UNLOCK_RETURN(KErrNone);
       
  2479 
       
  2480 	
  2469 	PRM_CLIENT_CHANGE_STATE_START_TRACE
  2481 	PRM_CLIENT_CHANGE_STATE_START_TRACE
  2470 	//If long latency resource requested synchronously from DFC thread 0 Panic
  2482 	//If long latency resource requested synchronously from DFC thread 0 Panic
  2471 
  2483 
  2472     const TDesC8* pDfc0 = &KDfcThread0Name;
  2484     const TDesC8* pDfc0 = &KDfcThread0Name;
  2473 	if((pR->LatencySet() && !aCb) && !(pDfc0->Compare(*(TDesC8*)thread.iName)))
  2485 	if((pR->LatencySet() && !aCb) && !(pDfc0->Compare(*(TDesC8*)thread.iName)))
  2721 */
  2733 */
  2722 TInt DPowerResourceController::GetResourceState(TUint aClientId, TUint aResourceId, TBool aCached, TInt& aState, 
  2734 TInt DPowerResourceController::GetResourceState(TUint aClientId, TUint aResourceId, TBool aCached, TInt& aState, 
  2723 																						TInt& aLevelOwnerId)
  2735 																						TInt& aLevelOwnerId)
  2724 	{
  2736 	{
  2725 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetResourceState(synchronous)"));
  2737 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetResourceState(synchronous)"));
  2726 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("ClientId = 0x%x, ResourceId = %d, Cached = %d", aClientId, aResourceId,\
  2738 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("ClientId = 0x%x, ResourceId = %d, Cached = %d", aClientId, aResourceId, aCached));
  2727 																										aCached));
  2739 
       
  2740 	DThread& thread = Kern::CurrentThread();	
  2728 	CHECK_CONTEXT(thread)
  2741 	CHECK_CONTEXT(thread)
  2729 	if(iInitialised <= EResConCreated) 
  2742 	if(iInitialised <= EResConCreated) 
  2730 		return KErrNotReady;
  2743 		return KErrNotReady;
  2731 	DThread& thread = Kern::CurrentThread();	
       
  2732 	SPowerResourceClient* pC = NULL;
  2744 	SPowerResourceClient* pC = NULL;
  2733 	TInt r = KErrNone;
  2745 	TInt r = KErrNone;
  2734 	Lock();
  2746 	Lock();
  2735 	VALIDATE_CLIENT(thread);
  2747 	VALIDATE_CLIENT(thread);
  2736 	if(!aResourceId)
  2748 	if(!aResourceId)
  2815 #ifdef PRM_ENABLE_EXTENDED_VERSION
  2827 #ifdef PRM_ENABLE_EXTENDED_VERSION
  2816 	if(aResourceId & KIdMaskDynamic)
  2828 	if(aResourceId & KIdMaskDynamic)
  2817 		((DDynamicPowerResource*)pR)->UnLock();
  2829 		((DDynamicPowerResource*)pR)->UnLock();
  2818 #endif
  2830 #endif
  2819 	UnLock();
  2831 	UnLock();
  2820     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetResourceState(synchronous), Level = %d",\
  2832     __KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetResourceState(synchronous), Level = %d", aState));
  2821 																											aState));
       
  2822 	if(pR->LatencyGet()) //For long latency resource btrace is done in controller thread.
  2833 	if(pR->LatencyGet()) //For long latency resource btrace is done in controller thread.
  2823 		return r;
  2834 		return r;
  2824 	PRM_RESOURCE_GET_STATE_END_TRACE
  2835 	PRM_RESOURCE_GET_STATE_END_TRACE
  2825 	return r;
  2836 	return r;
  2826 	}
  2837 	}
  2867 @pre Can be used in a device driver
  2878 @pre Can be used in a device driver
  2868 */
  2879 */
  2869 TInt DPowerResourceController::GetResourceState(TUint aClientId, TUint aResourceId, TBool aCached,  TPowerResourceCb& aCb)
  2880 TInt DPowerResourceController::GetResourceState(TUint aClientId, TUint aResourceId, TBool aCached,  TPowerResourceCb& aCb)
  2870 	{
  2881 	{
  2871 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetResourceState(asynchronous)"));
  2882 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetResourceState(asynchronous)"));
  2872 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("ClientId = 0x%x, ResourceId = %d, Cached = %d", aClientId, aResourceId, \
  2883 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("ClientId = 0x%x, ResourceId = %d, Cached = %d", aClientId, aResourceId, aCached));
  2873 																											aCached));
  2884 
       
  2885 	DThread& thread = Kern::CurrentThread();	
  2874 	CHECK_CONTEXT(thread)
  2886 	CHECK_CONTEXT(thread)
  2875 	if(iInitialised <= EResConCreated) 
  2887 	if(iInitialised <= EResConCreated) 
  2876 		return KErrNotReady;
  2888 		return KErrNotReady;
  2877 	DThread& thread = Kern::CurrentThread();	
       
  2878 	SPowerResourceClient* pC = NULL;
  2889 	SPowerResourceClient* pC = NULL;
  2879 	TInt r = KErrNone;
  2890 	TInt r = KErrNone;
  2880 	Lock();
  2891 	Lock();
  2881 	VALIDATE_CLIENT(thread);
  2892 	VALIDATE_CLIENT(thread);
  2882 	if(!aResourceId)
  2893 	if(!aResourceId)
  2883 		UNLOCK_RETURN(KErrNotFound);
  2894 		UNLOCK_RETURN(KErrNotFound);
  2884 	if(!&aCb) //Need to specify a callback for this asynchronous API
       
  2885 		UNLOCK_RETURN(KErrArgument);
       
  2886 #ifdef PRM_ENABLE_EXTENDED_VERSION
  2895 #ifdef PRM_ENABLE_EXTENDED_VERSION
  2887 	DStaticPowerResource *pR = NULL;
  2896 	DStaticPowerResource *pR = NULL;
  2888 	GET_RESOURCE_FROM_LIST(aResourceId, pR) 
  2897 	GET_RESOURCE_FROM_LIST(aResourceId, pR) 
  2889 	if(aResourceId & KIdMaskDynamic)
  2898 	if(aResourceId & KIdMaskDynamic)
  2890 		{
  2899 		{
  2897 		UNLOCK_RETURN(KErrNotFound);
  2906 		UNLOCK_RETURN(KErrNotFound);
  2898 	DStaticPowerResource *pR = iStaticResourceArray[aResourceId-1];
  2907 	DStaticPowerResource *pR = iStaticResourceArray[aResourceId-1];
  2899 	if(!pR)
  2908 	if(!pR)
  2900 		UNLOCK_RETURN(KErrNotFound);
  2909 		UNLOCK_RETURN(KErrNotFound);
  2901 #endif
  2910 #endif
  2902 	if(&aCb)
  2911 	aCb.iResult = KErrNone;
  2903 		{
  2912 	aCb.iResourceId = aResourceId;
  2904 		aCb.iResult = KErrNone;
  2913 	aCb.iClientId = aClientId;
  2905 		aCb.iResourceId = aResourceId;
  2914 
  2906 		aCb.iClientId = aClientId;
       
  2907 		}
       
  2908 	PRM_RESOURCE_GET_STATE_START_TRACE
  2915 	PRM_RESOURCE_GET_STATE_START_TRACE
  2909 	if(aCached) //Call the callback directly
  2916 	if(aCached) //Call the callback directly
  2910 		{
  2917 		{
  2911 		UnLock();
  2918 		UnLock();
  2912 		aCb.iCallback(aClientId, aResourceId, pR->iCachedLevel, pR->iLevelOwnerId, KErrNone, aCb.iParam);
  2919 		aCb.iCallback(aClientId, aResourceId, pR->iCachedLevel, pR->iLevelOwnerId, KErrNone, aCb.iParam);
  2983 		UnLock();
  2990 		UnLock();
  2984 		// Call the client callback function directly as it is already executing in the context of client thread.
  2991 		// Call the client callback function directly as it is already executing in the context of client thread.
  2985 		aCb.iCallback(aClientId, aResourceId, req->Level(), pR->iLevelOwnerId, r, aCb.iParam);
  2992 		aCb.iCallback(aClientId, aResourceId, req->Level(), pR->iLevelOwnerId, r, aCb.iParam);
  2986 		aCb.iResult = KErrCompletion; //Mark the callback object to act properly during cancellation of this request.
  2993 		aCb.iResult = KErrCompletion; //Mark the callback object to act properly during cancellation of this request.
  2987 		}
  2994 		}
  2988 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetResourceState(asynchronous), Level = %d", \
  2995 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetResourceState(asynchronous), Level = %d", req->Level()));
  2989 																										req->Level()));
       
  2990 	if(pR->LatencyGet())
  2996 	if(pR->LatencyGet())
  2991 		return r;
  2997 		return r;
  2992 #ifdef PRM_INSTRUMENTATION_MACRO
  2998 #ifdef PRM_INSTRUMENTATION_MACRO
  2993 	TInt aState = req->Level();
  2999 	TInt aState = req->Level();
  2994 	PRM_RESOURCE_GET_STATE_END_TRACE
  3000 	PRM_RESOURCE_GET_STATE_END_TRACE
  3049 #endif
  3055 #endif
  3050 	if(aCb.iClientId != aClientId)
  3056 	if(aCb.iClientId != aClientId)
  3051 		{
  3057 		{
  3052         __KTRACE_OPT(KRESMANAGER, Kern::Printf("aCb.iClientId = 0x%x, aClientId = 0x%x", aCb.iClientId, aClientId));
  3058         __KTRACE_OPT(KRESMANAGER, Kern::Printf("aCb.iClientId = 0x%x, aClientId = 0x%x", aCb.iClientId, aClientId));
  3053         r = KErrAccessDenied;
  3059         r = KErrAccessDenied;
       
  3060 #ifdef PRM_INSTRUMENTATION_MACRO
  3054         PRM_RESOURCE_CANCEL_LONGLATENCY_OPERATION_TRACE
  3061         PRM_RESOURCE_CANCEL_LONGLATENCY_OPERATION_TRACE
       
  3062 #endif
  3055         UNLOCK_RETURN(r);
  3063         UNLOCK_RETURN(r);
  3056 		}
  3064 		}
  3057 	if(aCb.iResult == KErrCompletion)
  3065 	if(aCb.iResult == KErrCompletion)
  3058 		{
  3066 		{
  3059         r = KErrCompletion;
  3067         r = KErrCompletion;
       
  3068 #ifdef PRM_INSTRUMENTATION_MACRO
  3060         PRM_RESOURCE_CANCEL_LONGLATENCY_OPERATION_TRACE
  3069         PRM_RESOURCE_CANCEL_LONGLATENCY_OPERATION_TRACE
       
  3070 #endif
  3061 		UNLOCK_RETURN(r);
  3071 		UNLOCK_RETURN(r);
  3062 		}
  3072 		}
  3063 	//Search in the controller message queue for this message
  3073 	//Search in the controller message queue for this message
  3064 #ifdef PRM_ENABLE_EXTENDED_VERSION
  3074 #ifdef PRM_ENABLE_EXTENDED_VERSION
  3065 	if(aResourceId & KIdMaskResourceWithDependencies) //long latency resource with dependency and will be processed in dependency thread.
  3075 	if(aResourceId & KIdMaskResourceWithDependencies) //long latency resource with dependency and will be processed in dependency thread.
  3073 				{
  3083 				{
  3074 				r = KErrCancel;
  3084 				r = KErrCancel;
  3075 				pRM->Deque();
  3085 				pRM->Deque();
  3076 				pMsgQ->iState = TMessageBase::EFree; //Reset the state
  3086 				pMsgQ->iState = TMessageBase::EFree; //Reset the state
  3077 				MoveRequestToFreePool(pReq);
  3087 				MoveRequestToFreePool(pReq);
       
  3088 #ifdef PRM_INSTRUMENTATION_MACRO
  3078 				PRM_RESOURCE_CANCEL_LONGLATENCY_OPERATION_TRACE
  3089 				PRM_RESOURCE_CANCEL_LONGLATENCY_OPERATION_TRACE
       
  3090 #endif
  3079 				UNLOCK_RETURN(r);
  3091 				UNLOCK_RETURN(r);
  3080 				}
  3092 				}
  3081 			}
  3093 			}
  3082 		}
  3094 		}
  3083 	else // long latency resource without dependency and will be processed in RC thread.
  3095 	else // long latency resource without dependency and will be processed in RC thread.
  3091 				{
  3103 				{
  3092 				r = KErrCancel;
  3104 				r = KErrCancel;
  3093 				pRM->Deque();
  3105 				pRM->Deque();
  3094 				pMsgQ->iState = TMessageBase::EFree; //Reset the state
  3106 				pMsgQ->iState = TMessageBase::EFree; //Reset the state
  3095 				MoveRequestToFreePool(pReq);
  3107 				MoveRequestToFreePool(pReq);
       
  3108 #ifdef PRM_INSTRUMENTATION_MACRO
  3096 				PRM_RESOURCE_CANCEL_LONGLATENCY_OPERATION_TRACE
  3109 				PRM_RESOURCE_CANCEL_LONGLATENCY_OPERATION_TRACE
       
  3110 #endif
  3097 				UNLOCK_RETURN(r);
  3111 				UNLOCK_RETURN(r);
  3098 				}
  3112 				}
  3099 			}
  3113 			}
  3100 		}
  3114 		}
       
  3115 #ifdef PRM_INSTRUMENTATION_MACRO
  3101     PRM_RESOURCE_CANCEL_LONGLATENCY_OPERATION_TRACE
  3116     PRM_RESOURCE_CANCEL_LONGLATENCY_OPERATION_TRACE
       
  3117 #endif
  3102 	UNLOCK_RETURN(r);
  3118 	UNLOCK_RETURN(r);
  3103 	}
  3119 	}
  3104 
  3120 
  3105 /**
  3121 /**
  3106 @publishedPartner
  3122 @publishedPartner
  3667    state information it is interested in for accessing from null thread. This
  3683    state information it is interested in for accessing from null thread. This
  3668    list needs to be accessed from the Idle thread using direct access. */
  3684    list needs to be accessed from the Idle thread using direct access. */
  3669 TInt DPowerResourceController::RegisterResourcesForIdle(TInt aPowerControllerId, TUint aNumResources, TPtr* aBuf)
  3685 TInt DPowerResourceController::RegisterResourcesForIdle(TInt aPowerControllerId, TUint aNumResources, TPtr* aBuf)
  3670 	{
  3686 	{
  3671 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::RegisterResourceForIdle"));
  3687 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::RegisterResourceForIdle"));
  3672 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("PowerControllerId = 0x%x, NumResources = %d", aPowerControllerId, \
  3688 	__KTRACE_OPT(KRESMANAGER, Kern::Printf("PowerControllerId = 0x%x, NumResources = %d", aPowerControllerId, aNumResources));
  3673 																							aNumResources));
       
  3674 #ifdef DEBUG_VERSION //Surrounding with macro to avoid warnings.
  3689 #ifdef DEBUG_VERSION //Surrounding with macro to avoid warnings.
  3675 	DThread& t = Kern::CurrentThread();	
  3690 	DThread& t = Kern::CurrentThread();	
  3676 	CHECK_CONTEXT(t)
  3691 	CHECK_CONTEXT(t)
  3677 #endif
  3692 #endif
  3678 	if(!aBuf)
  3693 	if(!aBuf)
  3683 		return KErrInUse;
  3698 		return KErrInUse;
  3684 	if((TUint)(aBuf->MaxLength() - aBuf->Length()) < (sizeof(SIdleResourceInfo) * aNumResources))
  3699 	if((TUint)(aBuf->MaxLength() - aBuf->Length()) < (sizeof(SIdleResourceInfo) * aNumResources))
  3685 		return KErrArgument;
  3700 		return KErrArgument;
  3686 	GET_CRITICAL_SECTION_COUNT
  3701 	GET_CRITICAL_SECTION_COUNT
  3687 	Lock();
  3702 	Lock();
  3688 	SIdleResourceInfo* pS=(SIdleResourceInfo*)aBuf;
  3703 	SIdleResourceInfo* pS=(SIdleResourceInfo*)aBuf->Ptr();
  3689 	DStaticPowerResource* pR=NULL;
  3704 	DStaticPowerResource* pR=NULL;
  3690 	TUint count=0, id=0;
  3705 	TUint count=0;
       
  3706 	TUint id=0;
  3691 	for(count=0;count<aNumResources;count++) //Check for valid resource ID.
  3707 	for(count=0;count<aNumResources;count++) //Check for valid resource ID.
  3692 		{
  3708 		{
  3693 #ifndef PRM_ENABLE_EXTENDED_VERSION
  3709 #ifndef PRM_ENABLE_EXTENDED_VERSION
  3694 		if((!pS->iResourceId) || (pS->iResourceId > iStaticResourceArrayEntries) || (!iStaticResourceArray[pS->iResourceId-1]))
  3710 		if((!pS->iResourceId) || (pS->iResourceId > iStaticResourceArrayEntries) || (!iStaticResourceArray[pS->iResourceId-1]))
  3695 			{
  3711 			{
  3703 			UnLock();
  3719 			UnLock();
  3704 			LOCK_AND_CRITICAL_SECTION_COUNT_CHECK
  3720 			LOCK_AND_CRITICAL_SECTION_COUNT_CHECK
  3705 			return KErrNotSupported;
  3721 			return KErrNotSupported;
  3706 			}
  3722 			}
  3707 		if((!pS->iResourceId) || ((pS->iResourceId & KIdMaskResourceWithDependencies) && 
  3723 		if((!pS->iResourceId) || ((pS->iResourceId & KIdMaskResourceWithDependencies) && 
  3708 			     (pS->iResourceId > iStaticResDependencyCount)) || (pS->iResourceId > iStaticResourceArrayEntries)
  3724 			     (pS->iResourceId > iStaticResDependencyCount)) || (!(pS->iResourceId & KIdMaskResourceWithDependencies) && 
  3709 				                                 || (!iStaticResourceArray[pS->iResourceId-1]))
  3725 					((pS->iResourceId > iStaticResourceArrayEntries) || (!iStaticResourceArray[pS->iResourceId-1]))))
  3710 			{
  3726 			{
  3711 			UnLock();
  3727 			UnLock();
  3712 			LOCK_AND_CRITICAL_SECTION_COUNT_CHECK
  3728 			LOCK_AND_CRITICAL_SECTION_COUNT_CHECK
  3713 			return KErrNotFound;
  3729 			return KErrNotFound;
  3714 			}
  3730 			}
  3715 #endif
  3731 #endif
  3716 		pS++;
  3732 		pS++;
  3717 		}
  3733 		}
  3718 	pS = (SIdleResourceInfo*)aBuf;
  3734 	pS = (SIdleResourceInfo*)aBuf->Ptr();
  3719 	for(count=0;count<aNumResources;count++)
  3735 	for(count=0;count<aNumResources;count++)
  3720 		{
  3736 		{
  3721 		id=pS->iResourceId;
  3737 		id=pS->iResourceId;
  3722 #ifdef PRM_ENABLE_EXTENDED_VERSION
  3738 #ifdef PRM_ENABLE_EXTENDED_VERSION
  3723 		if(id & KIdMaskResourceWithDependencies) //Dependency resource
  3739 		if(id & KIdMaskResourceWithDependencies) //Dependency resource
  3724 			pR = iStaticResDependencyArray[(id & ID_INDEX_BIT_MASK)-1];
  3740 			pR = iStaticResDependencyArray[(id & ID_INDEX_BIT_MASK)-1];
  3725 		else
  3741 		else
  3726 #endif
  3742 #endif
  3727 		pR=iStaticResourceArray[id-1];
  3743 		pR=iStaticResourceArray[id-1];
       
  3744 		pS->iLevelOwnerId = pR->iLevelOwnerId;
       
  3745 		pS->iCurrentLevel = pR->iCachedLevel;
  3728 		pR->iIdleListEntry=pS;
  3746 		pR->iIdleListEntry=pS;
  3729 		pS++;
  3747 		pS++;
  3730 		}
  3748 		}
  3731 	iListForIdle=(SIdleResourceInfo*)aBuf;
  3749 	iListForIdle=(SIdleResourceInfo*)aBuf->Ptr();
       
  3750 	pS = (SIdleResourceInfo*)aBuf->Ptr();
  3732 	UnLock();
  3751 	UnLock();
  3733 	LOCK_AND_CRITICAL_SECTION_COUNT_CHECK
  3752 	LOCK_AND_CRITICAL_SECTION_COUNT_CHECK
  3734 	return KErrNone;
  3753 	return KErrNone;
  3735 	}
  3754 	}
  3736 	
  3755 	
  3901 */
  3920 */
  3902 TInt DPowerResourceController::GetInterface(TUint aClientId, TUint aInterfaceId, TAny* aParam1, TAny* aParam2, 
  3921 TInt DPowerResourceController::GetInterface(TUint aClientId, TUint aInterfaceId, TAny* aParam1, TAny* aParam2, 
  3903 											                                             TAny* aParam3)
  3922 											                                             TAny* aParam3)
  3904 	{
  3923 	{
  3905 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetInterface"));
  3924 	__KTRACE_OPT(KRESMANAGER, Kern::Printf(">DPowerResourceController::GetInterface"));
       
  3925 	DThread& thread = Kern::CurrentThread();	
  3906 	CHECK_CONTEXT(thread)
  3926 	CHECK_CONTEXT(thread)
  3907 	if((iInitialised != EResConStartupCompleted) && (aInterfaceId != KResManControlIoGetVersion))
  3927 	if((iInitialised != EResConStartupCompleted) && (aInterfaceId != KResManControlIoGetVersion))
  3908 		return KErrNotSupported;
  3928 		return KErrNotSupported;
  3909 	TInt r = KErrNone;
  3929 	TInt r = KErrNone;
  3910 	DThread& thread = Kern::CurrentThread();	
       
  3911 	Lock();
  3930 	Lock();
  3912 	SPowerResourceClient* pC = NULL;
  3931 	SPowerResourceClient* pC = NULL;
  3913 	VALIDATE_CLIENT(thread);
  3932 	VALIDATE_CLIENT(thread);
  3914 #ifndef PRM_ENABLE_EXTENDED_VERSION
  3933 #ifndef PRM_ENABLE_EXTENDED_VERSION
  3915 	if(aInterfaceId == KResManControlIoGetVersion)
  3934 	if(aInterfaceId == KResManControlIoGetVersion)