os/kernelhwsrv/kernel/eka/drivers/pbus/pbusmedia.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32\drivers\pbus\pbusmedia.cpp
    15 // 
    16 //
    17 
    18 #include <drivers/pbusmedia.h>
    19 #include "OstTraceDefinitions.h"
    20 #ifdef OST_TRACE_COMPILER_IN_USE
    21 #include "locmedia_ost.h"
    22 #ifdef __VC32__
    23 #pragma warning(disable: 4127) // disabling warning "conditional expression is constant"
    24 #endif
    25 #include "pbusmediaTraces.h"
    26 #endif
    27 
    28 void mediaCallBack(TAny* aPtr, TInt aReason, TAny* a1, TAny* a2)
    29 	{
    30 	OstTraceFunctionEntry0( MEDIACALLBACK_ENTRY );
    31 	DPBusPrimaryMedia* pM=(DPBusPrimaryMedia*)aPtr;
    32 	__KTRACE_OPT(KLOCDRV,Kern::Printf("mediaCallBack media %d, reason %d, a1=0x%x, a2=0x%x",pM->iMediaId,aReason,a1,a2));
    33 	OstTraceExt4( TRACE_INTERNALS, MEDIACALLBACK, "aPtr=0x%x; aReason=%d; a1=0x%x; a2=0x%x", ( TUint )( aPtr ), aReason, ( TUint )( a1 ), ( TUint )( a2 ) );
    34 	
    35 	switch (aReason)
    36 		{
    37 		case TPBusCallBack::EPBusStateChange:
    38 			pM->PBusStateChange((TInt)a1,(TInt)a2);
    39 			break;
    40 		}
    41 	OstTraceFunctionExit0( MEDIACALLBACK_EXIT );
    42 	}
    43 
    44 /**
    45   Constructor for DPBusPrimaryMedia. Initializes the iSocket with aSocket.
    46   @param aSocket	Pointer to DPBusSocket object
    47   @see DPBusPrimaryMedia::iSocket
    48   */
    49 
    50 DPBusPrimaryMedia::DPBusPrimaryMedia(DPBusSocket* aSocket)
    51 	:	iSocket(aSocket)
    52 	{
    53 	OstTraceFunctionEntryExt( DPBUSPRIMARYMEDIA_DPBUSPRIMARYMEDIA_ENTRY, this );
    54 	}
    55 /**
    56   This function install a media call back for a removable media device.
    57   @param aDevice	Local media ID.
    58   @param aMediaId	Media Id (unique for a media subsystem)
    59   @param aLastMediaId	This indicates number of used media ids+ number of DMedia objects to be associated with the media driver
    60   @return KErrNone if successful,
    61           otherwise one of the other system wide error codes.
    62   @see DPrimaryMediaBase::Create()
    63   */
    64 TInt DPBusPrimaryMedia::Create(TMediaDevice aDevice, TInt aMediaId, TInt aLastMediaId)
    65 	{
    66 	OstTraceExt4(TRACE_FLOW, DPBUSPRIMARYMEDIA_CREATE_ENTRY ,"DPBusPrimaryMedia::Create;aDevice=%d;aMediaId=%d;aLastMediaId=%d;this=%x", (TInt) aDevice, aMediaId, aLastMediaId, (TUint) this);
    67 	
    68 	// Permanently install a media call back if for a removable media device
    69 	TInt r=KErrArgument;
    70 	iPBusState=EPBusCardAbsent;
    71 	if (__IS_REMOVABLE(aDevice))
    72 		{
    73 		iBusCallBack.iFunction=mediaCallBack;
    74 		iBusCallBack.iPtr=this;
    75 		iBusCallBack.SetSocket(iSocket->iSocketNumber);
    76 		iDfcQ=&iSocket->iDfcQ;
    77 		r=DPrimaryMediaBase::Create(aDevice,aMediaId,aLastMediaId);
    78 		if (r==KErrNone)
    79 			{
    80 			iBusCallBack.Add();
    81 			iPBusState=iSocket->State();
    82 			iMsgQ.Receive();
    83 			}
    84 		}
    85 	OstTraceFunctionExitExt( DPBUSPRIMARYMEDIA_CREATE_EXIT, this, r );
    86 	return r;
    87 	}
    88 
    89 /**
    90   Checks the PBUS state.
    91   @return KErrNone if successful,
    92           KErrNotReady if card is absent.
    93   @see TPBusState
    94   */
    95 TInt DPBusPrimaryMedia::QuickCheckStatus()
    96 	{
    97 	OstTraceFunctionEntry1( DPBUSPRIMARYMEDIA_QUICKCHECKSTATUS_ENTRY, this );
    98 	TInt r=KErrNone;
    99 	if (iSocket && iSocket->State()==EPBusCardAbsent)
   100 		r=KErrNotReady;
   101 	__KTRACE_OPT(KLOCDRV,Kern::Printf("DPBusPrimaryMedia::QuickCheckStatus media %d returns %d",iMediaId,r));
   102 	OstTraceExt2(TRACE_INTERNALS, DPBUSPRIMARYMEDIA_QUICKCHECKSTATUS, "iMediaId=%d; retval=%d",iMediaId,r);
   103 	OstTraceFunctionExit1( DPBUSPRIMARYMEDIA_QUICKCHECKSTATUS_EXIT, this );
   104 	return r;
   105 	}
   106 
   107 /**
   108   This function is called by the local media device driver to force a remount of the media device.
   109   @param  aFlags	Corresponds to force media change.
   110   @return KErrNone if successful, 
   111 		  otherwise one of the other system wide error codes. 
   112   @see TForceMediaChangeFlags
   113   */
   114 TInt DPBusPrimaryMedia::ForceMediaChange(TInt aFlags)
   115 	{
   116 	OstTraceFunctionEntryExt( DPBUSPRIMARYMEDIA_FORCEMEDIACHANGE_ENTRY, this );
   117 	if ((aFlags != KMediaRemountForceMediaChange) || (iPBusState == EPBusCardAbsent))
   118 		{
   119 		TInt pbusState = iPBusState;
   120 		
   121 		// This should ensure NotifyMediaChange() is called for ALL primary media attached to this socket
   122 		iSocket->ChangeState(EPBusCardAbsent, KErrNotReady);
   123 
   124 		// If a request was cancelled it's possible that the socket controller has been left in an 
   125 		// unusable state which might cause the next request to fail, so power down the socket to be safe
   126 		iSocket->ResetSocket(EFalse);
   127 
   128 		iSocket->ChangeState(pbusState == EPBusCardAbsent ? EPBusCardAbsent : EPBusOff, KErrNotReady);
   129 
   130 		OstTraceFunctionExitExt( DPBUSPRIMARYMEDIA_FORCEMEDIACHANGE_EXIT1, this, KErrCompletion );
   131 		return KErrCompletion;
   132 		}
   133 	
   134 	iSocket->ForceMediaChange();
   135 	OstTraceFunctionExitExt( DPBUSPRIMARYMEDIA_FORCEMEDIACHANGE_EXIT2, this, KErrNone );
   136 	return KErrNone;
   137 	}
   138 
   139 /**
   140   Called by clients to power up the PBUS.
   141   @return KErrNone if successful,
   142   		  otherwise one of the other system wide error codes.
   143   @see  DPBusSocket::PowerUp()
   144   */
   145 TInt DPBusPrimaryMedia::InitiatePowerUp()
   146 	{
   147 	OstTraceFunctionEntry1( DPBUSPRIMARYMEDIA_INITIATEPOWERUP_ENTRY, this );
   148 	TInt r = iSocket->PowerUp();  
   149 	OstTraceFunctionExitExt( DPBUSPRIMARYMEDIA_INITIATEPOWERUP_EXIT, this, r );
   150 	return r;
   151 	}
   152 
   153 /**
   154   Flags the media driver as entering a critical part of its processing.
   155   @return KErrNone if successful,
   156   		  otherwise one of the other system wide error codes.
   157   @see DPBusSocket::InCritical()
   158   */
   159 TInt DPBusPrimaryMedia::DoInCritical()
   160 	{
   161 	OstTraceFunctionEntry1( DPBUSPRIMARYMEDIA_DOINCRITICAL_ENTRY, this );
   162 	TInt r = iSocket->InCritical(); 
   163 	OstTraceFunctionExitExt( DPBUSPRIMARYMEDIA_DOINCRITICAL_EXIT, this, r );
   164 	return r;
   165 	}
   166 
   167 /**
   168   Flags the media driver as leaving a critical part of its processing.
   169   @return KErrNone if successful, 
   170   		  otherwise one of the other system wide error codes.
   171   @see DPBusSocket::EndInCritical()
   172   */
   173 void DPBusPrimaryMedia::DoEndInCritical()
   174 	{
   175 	OstTraceFunctionEntry1( DPBUSPRIMARYMEDIA_DOENDINCRITICAL_ENTRY, this );
   176 	iSocket->EndInCritical();
   177 	OstTraceFunctionExit1( DPBUSPRIMARYMEDIA_DOENDINCRITICAL_EXIT, this );
   178 	}
   179 
   180 /**
   181   Sets the incremental value of current consumption to aCurrent.
   182   @param aCurrent Delta Current in Milliamps.
   183   @see DPBusSocket::DeltaCurrentConsumption()
   184   */
   185 void DPBusPrimaryMedia::DeltaCurrentConsumption(TInt aCurrent)
   186 	{
   187 	OstTraceFunctionEntryExt( DPBUSPRIMARYMEDIA_DELTACURRENTCONSUMPTION_ENTRY, this );
   188 	iSocket->DeltaCurrentConsumption(aCurrent);
   189 	OstTraceFunctionExit1( DPBUSPRIMARYMEDIA_DELTACURRENTCONSUMPTION_EXIT, this );
   190 	}
   191 
   192 /**
   193   Gets the default drive capability/attributes.
   194   @param aCaps	A reference to a client-supplied TLocalDriveCapsV2 class to be filled by this function.
   195   @see TLocalDriveCapsV2
   196   @see TMediaType
   197   */
   198 void DPBusPrimaryMedia::DefaultDriveCaps(TLocalDriveCapsV2& aCaps)
   199 	{
   200 	OstTraceFunctionEntry1( DPBUSPRIMARYMEDIA_DEFAULTDRIVECAPS_ENTRY, this );
   201 	// aCaps is zeroed beforehand
   202 	aCaps.iType = EMediaNotPresent;
   203 	aCaps.iDriveAtt = KDriveAttLocal|KDriveAttRemovable;
   204 	OstTraceFunctionExit1( DPBUSPRIMARYMEDIA_DEFAULTDRIVECAPS_EXIT, this );
   205 	}
   206 
   207 /**
   208   Checks whether it is a removable media device or not.
   209   @param aSocketNum	This will be updated with socket number
   210   @return ETrue if Removable Device, EFalse if the device is Non-Removable.
   211   */
   212 TBool DPBusPrimaryMedia::IsRemovableDevice(TInt& aSocketNum)
   213 	{
   214 	OstTraceFunctionEntryExt( DPBUSPRIMARYMEDIA_ISREMOVABLEDEVICE_ENTRY, this );
   215 	aSocketNum=iSocket->iSocketNumber;
   216 	OstTraceFunctionExit1( DPBUSPRIMARYMEDIA_ISREMOVABLEDEVICE_EXIT, this );
   217 	return(ETrue);
   218 	}
   219 	
   220 void DPBusPrimaryMedia::PBusStateChange(TInt aState, TInt anError)
   221 	{
   222 	OstTraceFunctionEntryExt( DPBUSPRIMARYMEDIA_PBUSSTATECHANGE_ENTRY, this );
   223 	// receive power down and media change notifications
   224 	__KTRACE_OPT(KLOCDRV,Kern::Printf("DPBusPrimaryMedia(%d)::PBusStateChange state %d, err %d",iMediaId,aState,anError));
   225 	OstTraceExt3(TRACE_INTERNALS, DPBUSPRIMARYMEDIA_PBUSSTATECHANGE, "iMediaId=%d; aState=%d; anError=%d", iMediaId,aState,anError);
   226 	if (aState!=iPBusState)
   227 		{
   228 		TInt oldState = iPBusState;
   229 		iPBusState=aState;
   230 		switch (aState)
   231 			{
   232 			case EPBusCardAbsent:
   233 				NotifyMediaChange();
   234 				break;
   235 			case EPBusOff:
   236 				switch (anError)
   237 					{
   238 					case KErrNone:
   239 						// machine power down
   240 						NotifyPowerDown();
   241 						break;
   242 					case KErrTimedOut:					
   243 						// machine power down
   244 						NotifyPowerDown();
   245 						if(oldState == EPBusCardAbsent)
   246 							{
   247 							// powering down after power up with no card present.
   248 							// ...to prevent the bus powering up again, maintain
   249 							//    the card state as absent.  A media change will
   250 							//    update the status to allow the bus to power up.
   251 							iPBusState = EPBusCardAbsent;
   252 							}
   253 						break;
   254 					case KErrNotReady:
   255 						// card detected following door close
   256 						NotifyMediaPresent();
   257 						break;
   258 					case KErrAbort:
   259 						NotifyEmergencyPowerDown();
   260 						break;
   261 					default:
   262 						if (iState==EPoweringUp1 || iState==EPoweringUp2)
   263 							PowerUpComplete(anError);
   264 						break;
   265 					}
   266 			case EPBusPoweringUp:
   267 				// no action required
   268 				break;
   269 			case EPBusOn:
   270 				// bus is now powered up
   271 				if (iState==EPoweringUp1 || iState==EPoweringUp2)
   272 					PowerUpComplete(anError);
   273 				break;
   274 			case EPBusPsuFault:
   275 				NotifyPsuFault(anError);
   276 				break;
   277 			case EPBusPowerUpPending:
   278 				// no action required
   279 				break;
   280 			default:
   281 				break;
   282 			}
   283 		}
   284 	OstTraceFunctionExit1( DPBUSPRIMARYMEDIA_PBUSSTATECHANGE_EXIT, this );
   285 	}
   286