kernel/eka/drivers/pbus/mmc/sdcard/sdcard3c/sdcard.cpp
changeset 47 46fffbe7b5a7
parent 43 96e5fb8b040d
child 62 4a8fed1c0ef6
equal deleted inserted replaced
46:0bf4040442f9 47:46fffbe7b5a7
    12 //
    12 //
    13 // Description:
    13 // Description:
    14 //
    14 //
    15 
    15 
    16 #include <drivers/sdcard.h>
    16 #include <drivers/sdcard.h>
       
    17 #include "OstTraceDefinitions.h"
       
    18 #ifdef OST_TRACE_COMPILER_IN_USE
       
    19 #include "locmedia_ost.h"
       
    20 #ifdef __VC32__
       
    21 #pragma warning(disable: 4127) // disabling warning "conditional expression is constant"
       
    22 #endif
       
    23 #include "sdcardTraces.h"
       
    24 #endif
    17 
    25 
    18 
    26 
    19 // ======== TSDCard ========
    27 // ======== TSDCard ========
    20 
    28 
    21 TSDCard::TSDCard()
    29 TSDCard::TSDCard()
    27 TInt64 TSDCard::DeviceSize64() const
    35 TInt64 TSDCard::DeviceSize64() const
    28 //
    36 //
    29 // returns the SD device size
    37 // returns the SD device size
    30 //
    38 //
    31 	{
    39 	{
       
    40 	OstTraceFunctionEntry1( TSDCARD_DEVICESIZE64_ENTRY, this );
    32 	if(iFlags & KSDCardIsSDCard)
    41 	if(iFlags & KSDCardIsSDCard)
    33 		{	
    42 		{	
    34 		return (IsHighCapacity()) ? 512 * 1024 * (TInt64)(1 + CSD().CSDField(69, 48)) : TMMCard::DeviceSize64();
    43 		return (IsHighCapacity()) ? 512 * 1024 * (TInt64)(1 + CSD().CSDField(69, 48)) : TMMCard::DeviceSize64();
    35 		}
    44 		}
    36 		
    45 		
    40 TUint32 TSDCard::PreferredWriteGroupLength() const
    49 TUint32 TSDCard::PreferredWriteGroupLength() const
    41 //
    50 //
    42 // return SD erase sector size, (SECTOR_SIZE + 1) * 2 ** WRITE_BLK_LEN
    51 // return SD erase sector size, (SECTOR_SIZE + 1) * 2 ** WRITE_BLK_LEN
    43 //
    52 //
    44 	{
    53 	{
       
    54 	OstTraceFunctionEntry1( TSDCARD_PREFERREDWRITEGROUPLENGTH_ENTRY, this );
    45 	if(iFlags & KSDCardIsSDCard)
    55 	if(iFlags & KSDCardIsSDCard)
    46 		{	
    56 		{	
    47 		TSDCSD sdcsd(CSD());
    57 		TSDCSD sdcsd(CSD());
    48 		return (sdcsd.SDSectorSize() + 1) * (1 << sdcsd.WriteBlLen());
    58 		return (sdcsd.SDSectorSize() + 1) * (1 << sdcsd.WriteBlLen());
    49 		}
    59 		}
    92 TInt TSDCard::GetEraseInfo(TMMCEraseInfo& aEraseInfo) const
   102 TInt TSDCard::GetEraseInfo(TMMCEraseInfo& aEraseInfo) const
    93 //
   103 //
    94 // Return info. on erase services for this card
   104 // Return info. on erase services for this card
    95 //
   105 //
    96 	{
   106 	{
       
   107 	OstTraceFunctionEntry1( TSDCARD_GETERASEINFO_ENTRY, this );
    97 	
   108 	
    98 	// SD Controllers support MMC cards too. Check if we are really dealing with an SD card
   109 	// SD Controllers support MMC cards too. Check if we are really dealing with an SD card
    99 	if(!(iFlags&KSDCardIsSDCard))
   110 	if(!(iFlags&KSDCardIsSDCard))
   100 		return(TMMCard::GetEraseInfo(aEraseInfo));
   111 		return(TMMCard::GetEraseInfo(aEraseInfo));
   101 		
   112 		
   102 	if (CSD().CCC() & KMMCCmdClassErase)
   113 	if (CSD().CCC() & KMMCCmdClassErase)
   103 		{
   114 		{
   104 		// This card supports erase cmds. However, SD cards don't support Erase Group commands (i.e. CMD35, CMD36).
   115 		// This card supports erase cmds. However, SD cards don't support Erase Group commands (i.e. CMD35, CMD36).
       
   116 		OstTrace0( TRACE_INTERNALS, TSDCARD_GETERASEINFO, "Card supports erase class commands" );		
   105 		aEraseInfo.iEraseFlags=KMMCEraseClassCmdsSupported; 
   117 		aEraseInfo.iEraseFlags=KMMCEraseClassCmdsSupported; 
   106 		
   118 		
   107 		// Return the preferred size to be used as the unit for erase operations.
   119 		// Return the preferred size to be used as the unit for erase operations.
   108 		TSDCSD sdcsd(CSD());
   120 		TSDCSD sdcsd(CSD());
   109 		TUint32 prefSize=((sdcsd.SDSectorSize() + 1) * sdcsd.WriteBlockLength());
   121 		TUint32 prefSize=((sdcsd.SDSectorSize() + 1) * sdcsd.WriteBlockLength());
   121 			}
   133 			}
   122 		}
   134 		}
   123 	else		
   135 	else		
   124 		aEraseInfo.iEraseFlags=0;
   136 		aEraseInfo.iEraseFlags=0;
   125 		
   137 		
   126 	return(KErrNone);	
   138 	OstTraceFunctionExitExt( TSDCARD_GETERASEINFO_EXIT, this, KErrNone );
       
   139 	return KErrNone;	
   127 	}
   140 	}
   128 
   141 
   129 TInt TSDCard::MaxReadBlLen() const
   142 TInt TSDCard::MaxReadBlLen() const
   130 /**
   143 /**
   131  * Returns the maximum read block length supported by the card encoded as a logarithm
   144  * Returns the maximum read block length supported by the card encoded as a logarithm
   132  * Normally this is the same as the READ_BL_LEN field in the CSD register,
   145  * Normally this is the same as the READ_BL_LEN field in the CSD register,
   133  * but for high capacity cards (> 2GB) this is set to a maximum of 512 bytes,
   146  * but for high capacity cards (> 2GB) this is set to a maximum of 512 bytes,
   134  * if possible, to try to avoid compatibility issues.
   147  * if possible, to try to avoid compatibility issues.
   135  */
   148  */
   136 	{
   149 	{
       
   150 	OstTraceFunctionEntry1( TSDCARD_MAXREADBLLEN_ENTRY, this );
   137 	if (IsSDCard())
   151 	if (IsSDCard())
   138 		{
   152 		{
   139 		TInt blkLenLog2 = CSD().ReadBlLen();
   153 		TInt blkLenLog2 = CSD().ReadBlLen();
   140 		if (blkLenLog2 == KTwoGbyteSDBlockLen || blkLenLog2 == KFourGbyteSDBlockLen)
   154 		if (blkLenLog2 == KTwoGbyteSDBlockLen || blkLenLog2 == KFourGbyteSDBlockLen)
   141 			{
   155 			{
   142 			// The SD card spec. makes a special case for 2GByte cards,
   156 			// The SD card spec. makes a special case for 2GByte cards,
   143 			// ...and some manufacturers apply the same method to support 4G cards
   157 			// ...and some manufacturers apply the same method to support 4G cards
   144 			__KTRACE_OPT(KPBUS1, Kern::Printf("=mmc:mrbl > 2GB SD"));
   158 			__KTRACE_OPT(KPBUS1, Kern::Printf("=mmc:mrbl > 2GB SD"));
       
   159 			OstTrace0( TRACE_INTERNALS, TSDCARD_MAXREADBLLEN, "SD Card > 2GB" );
   145 			blkLenLog2 = KDefaultBlockLen;
   160 			blkLenLog2 = KDefaultBlockLen;
   146 			}
   161 			}
       
   162 		OstTraceFunctionExitExt( TSDCARD_MAXREADBLLEN_EXIT, this, blkLenLog2 );
   147 		return blkLenLog2;
   163 		return blkLenLog2;
   148 		}
   164 		}
   149 	else		// MMC card
   165 	else		// MMC card
   150 		{
   166 		{
   151 		return (TMMCard::MaxReadBlLen());
   167 		TInt ret = TMMCard::MaxReadBlLen();
       
   168 		OstTraceFunctionExitExt( DUP1_TSDCARD_MAXREADBLLEN_EXIT, this, ret );
       
   169 		return ret;
   152 		}
   170 		}
   153 	}
   171 	}
   154 
   172 
   155 TInt TSDCard::MaxWriteBlLen() const
   173 TInt TSDCard::MaxWriteBlLen() const
   156 /**
   174 /**
   158  * Normally this is the same as the WRITE_BL_LEN field in the CSD register,
   176  * Normally this is the same as the WRITE_BL_LEN field in the CSD register,
   159  * but for high capacity cards (> 2GB) this is set to a maximum of 512 bytes,
   177  * but for high capacity cards (> 2GB) this is set to a maximum of 512 bytes,
   160  * if possible, to try to avoid compatibility issues.
   178  * if possible, to try to avoid compatibility issues.
   161  */
   179  */
   162 	{
   180 	{
       
   181 	OstTraceFunctionEntry1( TSDCARD_MAXWRITEBLLEN_ENTRY, this );
   163 	if (IsSDCard())
   182 	if (IsSDCard())
   164 		{
   183 		{
   165 		TInt blkLenLog2 = CSD().WriteBlLen();
   184 		TInt blkLenLog2 = CSD().WriteBlLen();
   166 		if (blkLenLog2 == KTwoGbyteSDBlockLen || blkLenLog2 == KFourGbyteSDBlockLen)
   185 		if (blkLenLog2 == KTwoGbyteSDBlockLen || blkLenLog2 == KFourGbyteSDBlockLen)
   167 			{
   186 			{
   168 			// The SD card spec. makes a special case for 2GByte cards,
   187 			// The SD card spec. makes a special case for 2GByte cards,
   169 			// ...and some manufacturers apply the same method to support 4G cards
   188 			// ...and some manufacturers apply the same method to support 4G cards
   170 			__KTRACE_OPT(KPBUS1, Kern::Printf("=mmc:mwbl > 2GB SD"));
   189 			__KTRACE_OPT(KPBUS1, Kern::Printf("=mmc:mwbl > 2GB SD"));
       
   190 			OstTrace0( TRACE_INTERNALS, TSDCARD_MAXWRITEBLLEN, "SD Card > 2GB" );
   171 			blkLenLog2 = KDefaultBlockLen;
   191 			blkLenLog2 = KDefaultBlockLen;
   172 			}
   192 			}
       
   193 		OstTraceFunctionExitExt( TSDCARD_MAXWRITEBLLEN_EXIT, this, blkLenLog2 );
   173 		return blkLenLog2;
   194 		return blkLenLog2;
   174 		}
   195 		}
   175 	else		// MMC card
   196 	else		// MMC card
   176 		{
   197 		{
   177 		return (TMMCard::MaxWriteBlLen());
   198 		TInt ret = TMMCard::MaxWriteBlLen();
       
   199 		OstTraceFunctionExitExt( DUP1_TSDCARD_MAXWRITEBLLEN_EXIT, this, ret );
       
   200 		return ret;
   178 		}
   201 		}
   179 	}
   202 	}
   180 	
   203 	
   181 TUint TSDCard::MaxTranSpeedInKilohertz() const
   204 TUint TSDCard::MaxTranSpeedInKilohertz() const
   182 /**
   205 /**
   183  * Returns the maximum supported clock rate for the card, in Kilohertz.
   206  * Returns the maximum supported clock rate for the card, in Kilohertz.
   184  * @return Speed, in Kilohertz
   207  * @return Speed, in Kilohertz
   185  */
   208  */
   186 	{
   209 	{
       
   210 	OstTraceFunctionEntry1( TSDCARD_MAXTRANSPEEDINKILOHERTZ_ENTRY, this );
   187 	TUint maxClk = TMMCard::MaxTranSpeedInKilohertz();
   211 	TUint maxClk = TMMCard::MaxTranSpeedInKilohertz();
   188 	
   212 	
   189 	if (IsSDCard())
   213 	if (IsSDCard())
   190 		{
   214 		{
   191 		__KTRACE_OPT(KPBUS1, Kern::Printf("\t >TSDCard(%d): MaxTranSpeedInKilohertz: %d",(iIndex-1),maxClk));
   215 		__KTRACE_OPT(KPBUS1, Kern::Printf("\t >TSDCard(%d): MaxTranSpeedInKilohertz: %d",(iIndex-1),maxClk));
   193 #ifdef _DEBUG
   217 #ifdef _DEBUG
   194 		//MaxClk for SD should only be either 25000KHz or 50000KHz
   218 		//MaxClk for SD should only be either 25000KHz or 50000KHz
   195 		if ( (maxClk != KSDDTClk25MHz) && (maxClk != KSDDTClk50MHz) )
   219 		if ( (maxClk != KSDDTClk25MHz) && (maxClk != KSDDTClk50MHz) )
   196 			{
   220 			{
   197 			__KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack: Non-Compliant DT Clock"));
   221 			__KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack: Non-Compliant DT Clock"));
       
   222 			OstTrace0( TRACE_INTERNALS, TSDCARD_MAXTRANSPEEDINKILOHERTZ, "Non-Compliant DT Clock" );			
   198 			}
   223 			}
   199 #endif
   224 #endif
   200 		if (maxClk > KSDDTClk50MHz)
   225 		if (maxClk > KSDDTClk50MHz)
   201 			{
   226 			{
   202 			//Clock rate exceeds SD possible max clock rate
   227 			//Clock rate exceeds SD possible max clock rate
   203 			__KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack: Tuning DT Clock down to 50MHz"));
   228 			__KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack: Tuning DT Clock down to 50MHz"));
       
   229 			OstTrace0( TRACE_INTERNALS, TSDCARD_MAXTRANSPEEDINKILOHERTZ1, "Tuning DT Clock down to 50MHz" );			
   204 			maxClk = KSDDTClk50MHz;
   230 			maxClk = KSDDTClk50MHz;
   205 			}
   231 			}
   206 		}
   232 		}
   207 		
   233 		
       
   234 	OstTraceFunctionExitExt( TSDCARD_MAXTRANSPEEDINKILOHERTZ_EXIT, this, maxClk );
   208 	return maxClk;
   235 	return maxClk;
   209 	}
   236 	}
   210 
   237 
   211 // ======== TSDCardArray ========
   238 // ======== TSDCardArray ========
   212 
   239 
   215 // allocate TSDCard objects for iCards and iNewCardsArray.  This function
   242 // allocate TSDCard objects for iCards and iNewCardsArray.  This function
   216 // is called at bootup as part of stack allocation so there is no cleanup
   243 // is called at bootup as part of stack allocation so there is no cleanup
   217 // if it fails.
   244 // if it fails.
   218 //
   245 //
   219 	{
   246 	{
       
   247 	OstTraceFunctionEntry1( TSDCARDARRAY_ALLOCCARDS_ENTRY, this );
   220 	for (TInt i = 0; i < (TInt) KMaxMMCardsPerStack; ++i)
   248 	for (TInt i = 0; i < (TInt) KMaxMMCardsPerStack; ++i)
   221 		{
   249 		{
   222 		// zeroing the card data used to be implicit because embedded in
   250 		// zeroing the card data used to be implicit because embedded in
   223 		// CBase-derived DMMCStack.
   251 		// CBase-derived DMMCStack.
   224 		if ((iCards[i] = new TSDCard) == 0)
   252 		if ((iCards[i] = new TSDCard) == 0)
       
   253 		    {
       
   254 			OstTraceFunctionExitExt( TSDCARDARRAY_ALLOCCARDS_EXIT, this, KErrNoMemory );
   225 			return KErrNoMemory;
   255 			return KErrNoMemory;
       
   256 		    }
   226 		iCards[i]->iUsingSessionP = 0;
   257 		iCards[i]->iUsingSessionP = 0;
   227 		if ((iNewCards[i] = new TSDCard) == 0)
   258 		if ((iNewCards[i] = new TSDCard) == 0)
       
   259 		    {
       
   260 			OstTraceFunctionExitExt( DUP1_TSDCARDARRAY_ALLOCCARDS_EXIT, this, KErrNoMemory );
   228 			return KErrNoMemory;
   261 			return KErrNoMemory;
       
   262 			}
   229 		}
   263 		}
   230 
   264 
       
   265 	OstTraceFunctionExitExt( DUP2_TSDCARDARRAY_ALLOCCARDS_EXIT, this, KErrNone );
   231 	return KErrNone;
   266 	return KErrNone;
   232 	}
   267 	}
   233 
   268 
   234 void TSDCardArray::AddCardSDMode(TUint aCardNumber,const TUint8* aCID,TRCA* aNewRCA)
   269 void TSDCardArray::AddCardSDMode(TUint aCardNumber,const TUint8* aCID,TRCA* aNewRCA)
   235 //
   270 //
   236 // Add an MMC card straight to the main card array in slot 'aCardNumber'. Save
   271 // Add an MMC card straight to the main card array in slot 'aCardNumber'. Save
   237 // the CID value in the slot. Return a RCA for the card.
   272 // the CID value in the slot. Return a RCA for the card.
   238 //
   273 //
   239 	{
   274 	{
       
   275 	OstTraceFunctionEntryExt( TSDCARDARRAY_ADDCARDSDMODE_ENTRY, this );
   240 
   276 
   241 	TRCA rca=0;
   277 	TRCA rca=0;
   242 	
   278 	
   243 	// First, lets check if the same card was here before. If it was, keep the same RCA
   279 	// First, lets check if the same card was here before. If it was, keep the same RCA
   244 	if (Card(aCardNumber).IsPresent() && Card(aCardNumber).iCID==aCID)
   280 	if (Card(aCardNumber).IsPresent() && Card(aCardNumber).iCID==aCID)
   254 		iOwningStack->iRCAPool.LockRCA(Card(aCardNumber).iRCA);
   290 		iOwningStack->iRCAPool.LockRCA(Card(aCardNumber).iRCA);
   255 		}
   291 		}
   256 
   292 
   257 	Card(aCardNumber).iIndex=(aCardNumber+1); // Mark card as being present
   293 	Card(aCardNumber).iIndex=(aCardNumber+1); // Mark card as being present
   258 	*aNewRCA=rca;
   294 	*aNewRCA=rca;
       
   295 	OstTraceFunctionExit1( TSDCARDARRAY_ADDCARDSDMODE_EXIT, this );
   259 	}
   296 	}
   260 
   297 
   261 TInt TSDCardArray::StoreRCAIfUnique(TUint aCardNumber,TRCA& anRCA)
   298 TInt TSDCardArray::StoreRCAIfUnique(TUint aCardNumber,TRCA& anRCA)
   262 //
   299 //
   263 // Check that no other array element has the same RCA value 'anRCA'. If no
   300 // Check that no other array element has the same RCA value 'anRCA'. If no
   264 // no duplication then store in slot 'aCardNumber'.
   301 // no duplication then store in slot 'aCardNumber'.
   265 //
   302 //
   266 	{
   303 	{
       
   304 	OstTraceExt3(TRACE_FLOW, TSDCARDARRAY_STORERCAIFUNIQUE_ENTRY ,"TSDCardArray::StoreRCAIfUnique;aCardNumber=%x;anRCA=%x;this=%x", aCardNumber, (TUint) anRCA, (TUint) this);
   267 
   305 
   268 	if (anRCA==0)
   306 	if (anRCA==0)
   269 		return(KErrGeneral);
   307 		{
       
   308 		OstTraceFunctionExitExt( TSDCARDARRAY_STORERCAIFUNIQUE_EXIT, this, KErrGeneral );
       
   309 		return KErrGeneral;
       
   310 		}
   270 	Card(aCardNumber).iRCA=0;
   311 	Card(aCardNumber).iRCA=0;
   271 
   312 
   272 	// Now let's look if we've seen this card before
   313 	// Now let's look if we've seen this card before
   273 	for ( TUint i=0 ; i<iOwningStack->iMaxCardsInStack ; i++ )
   314 	for ( TUint i=0 ; i<iOwningStack->iMaxCardsInStack ; i++ )
   274 		{
   315 		{
   275 		if ( Card(i).IsPresent() && Card(i).iRCA==anRCA )
   316 		if ( Card(i).IsPresent() && Card(i).iRCA==anRCA )
   276 			return(KErrInUse);
   317 			{
       
   318 			OstTraceFunctionExitExt( DUP1_TSDCARDARRAY_STORERCAIFUNIQUE_EXIT, this, KErrInUse );
       
   319 			return KErrInUse;
       
   320 			}
   277 		}
   321 		}
   278 	Card(aCardNumber).iRCA=anRCA;
   322 	Card(aCardNumber).iRCA=anRCA;
   279 	Card(aCardNumber).iIndex=(aCardNumber+1); // Mark card as being present
   323 	Card(aCardNumber).iIndex=(aCardNumber+1); // Mark card as being present
   280 	return(KErrNone);
   324 	OstTraceFunctionExitExt( DUP2_TSDCARDARRAY_STORERCAIFUNIQUE_EXIT, this, KErrNone );
       
   325 	return KErrNone;
   281 	}
   326 	}
   282 
   327 
   283 EXPORT_C void TSDCardArray::DeclareCardAsGone(TUint aCardNumber)
   328 EXPORT_C void TSDCardArray::DeclareCardAsGone(TUint aCardNumber)
   284 //
   329 //
   285 // reset SD specific fields to initial values and then reset generic MultiMediaCard
   330 // reset SD specific fields to initial values and then reset generic MultiMediaCard
   286 //
   331 //
   287 	{
   332 	{
       
   333 	OstTraceFunctionEntryExt( TSDCARDARRAY_DECLARECARDASGONE_ENTRY, this );
   288 	Card(aCardNumber).SetBusWidth(1);
   334 	Card(aCardNumber).SetBusWidth(1);
   289 	TMMCardArray::DeclareCardAsGone(aCardNumber);
   335 	TMMCardArray::DeclareCardAsGone(aCardNumber);
       
   336 	OstTraceFunctionExit1( TSDCARDARRAY_DECLARECARDASGONE_EXIT, this );
   290 	}
   337 	}
   291 
   338 
   292 // ======== DSDSession ========
   339 // ======== DSDSession ========
   293 
   340 
   294 void DSDSession::FillAppCommandDesc(TMMCCommandDesc& aDesc, TSDAppCmd aCmd)
   341 void DSDSession::FillAppCommandDesc(TMMCCommandDesc& aDesc, TSDAppCmd aCmd)
   295 	{
   342 	{
       
   343 	OstTraceFunctionEntry0( DSDSESSION_FILLAPPCOMMANDDESC_ENTRY );
   296 	aDesc.iCommand = (TMMCCommandEnum) aCmd;
   344 	aDesc.iCommand = (TMMCCommandEnum) aCmd;
   297 	aDesc.iArgument = 0;						// set stuff bits to zero
   345 	aDesc.iArgument = 0;						// set stuff bits to zero
   298 	FillAppCommandDesc(aDesc);
   346 	FillAppCommandDesc(aDesc);
       
   347 	OstTraceFunctionExit0( DSDSESSION_FILLAPPCOMMANDDESC_EXIT );
   299 	}
   348 	}
   300 
   349 
   301 void DSDSession::FillAppCommandDesc(TMMCCommandDesc& aDesc, TSDAppCmd aCmd, TMMCArgument aArg)
   350 void DSDSession::FillAppCommandDesc(TMMCCommandDesc& aDesc, TSDAppCmd aCmd, TMMCArgument aArg)
   302 	{
   351 	{
       
   352 	OstTraceFunctionEntry0( DUP1_DSDSESSION_FILLAPPCOMMANDDESC_ENTRY );
   303 	aDesc.iCommand = (TMMCCommandEnum) aCmd;
   353 	aDesc.iCommand = (TMMCCommandEnum) aCmd;
   304 	aDesc.iArgument = aArg;
   354 	aDesc.iArgument = aArg;
   305 	FillAppCommandDesc(aDesc);
   355 	FillAppCommandDesc(aDesc);
       
   356 	OstTraceFunctionExit0( DUP1_DSDSESSION_FILLAPPCOMMANDDESC_EXIT );
   306 	}
   357 	}
   307 
   358 
   308 const TUint32 CCA = KMMCCmdClassApplication;
   359 const TUint32 CCA = KMMCCmdClassApplication;
   309 const TMMCIdxCommandSpec AppCmdSpecTable[] =
   360 const TMMCIdxCommandSpec AppCmdSpecTable[] =
   310 	{						//	Class	Type		Dir			MBlk	StopT	Rsp Type		Len
   361 	{						//	Class	Type		Dir			MBlk	StopT	Rsp Type		Len
   317 	{ESDACmdSendSCR,			{CCA,ECmdTypeADTCS,	EDirRead,	EFalse, EFalse, ERespTypeR1,	4}}  //ACMD51
   368 	{ESDACmdSendSCR,			{CCA,ECmdTypeADTCS,	EDirRead,	EFalse, EFalse, ERespTypeR1,	4}}  //ACMD51
   318 };
   369 };
   319 
   370 
   320 void DSDSession::FillAppCommandDesc(TMMCCommandDesc& aDesc)
   371 void DSDSession::FillAppCommandDesc(TMMCCommandDesc& aDesc)
   321 	{
   372 	{
       
   373 	OstTraceFunctionEntry0( DUP2_DSDSESSION_FILLAPPCOMMANDDESC_ENTRY );
   322 	aDesc.iSpec = FindCommandSpec(AppCmdSpecTable, aDesc.iCommand);
   374 	aDesc.iSpec = FindCommandSpec(AppCmdSpecTable, aDesc.iCommand);
   323 	aDesc.iFlags = 0;
   375 	aDesc.iFlags = 0;
   324 	aDesc.iBytesDone = 0;
   376 	aDesc.iBytesDone = 0;
       
   377 	OstTraceFunctionExit0( DUP2_DSDSESSION_FILLAPPCOMMANDDESC_EXIT );
   325 	}
   378 	}
   326 
   379 
   327 const TMMCIdxCommandSpec SdSpecificCmdSpecTable[] =
   380 const TMMCIdxCommandSpec SdSpecificCmdSpecTable[] =
   328 /**
   381 /**
   329  * SD Specific Command Table
   382  * SD Specific Command Table
   338 	{ESDCmdSendIfCond,			{KMMCCmdClassBasic,	ECmdTypeBCR,	EDirNone,	EFalse,	EFalse,	ERespTypeR7,	4}}		// CMD8 : SEND_IF_COND
   391 	{ESDCmdSendIfCond,			{KMMCCmdClassBasic,	ECmdTypeBCR,	EDirNone,	EFalse,	EFalse,	ERespTypeR7,	4}}		// CMD8 : SEND_IF_COND
   339 	};
   392 	};
   340 
   393 
   341 void DSDSession::FillSdSpecificCommandDesc(TMMCCommandDesc& aDesc, TSDSpecificCmd aCmd, TMMCArgument aArg)
   394 void DSDSession::FillSdSpecificCommandDesc(TMMCCommandDesc& aDesc, TSDSpecificCmd aCmd, TMMCArgument aArg)
   342 	{
   395 	{
       
   396 	OstTraceFunctionEntry0( DSDSESSION_FILLSDSPECIFICCOMMANDDESC_ENTRY );
   343 	aDesc.iCommand = (TMMCCommandEnum) aCmd;
   397 	aDesc.iCommand = (TMMCCommandEnum) aCmd;
   344 	aDesc.iArgument = aArg;
   398 	aDesc.iArgument = aArg;
   345 	FillSdSpecificCommandDesc(aDesc);
   399 	FillSdSpecificCommandDesc(aDesc);
       
   400 	OstTraceFunctionExit0( DSDSESSION_FILLSDSPECIFICCOMMANDDESC_EXIT );
   346 	}
   401 	}
   347 
   402 
   348 void DSDSession::FillSdSpecificCommandDesc(TMMCCommandDesc& aDesc, TSDSpecificCmd aCmd)
   403 void DSDSession::FillSdSpecificCommandDesc(TMMCCommandDesc& aDesc, TSDSpecificCmd aCmd)
   349 	{
   404 	{
       
   405 	OstTraceFunctionEntry0( DUP1_DSDSESSION_FILLSDSPECIFICCOMMANDDESC_ENTRY );
   350 	aDesc.iCommand = (TMMCCommandEnum) aCmd;
   406 	aDesc.iCommand = (TMMCCommandEnum) aCmd;
   351 	aDesc.iArgument = 0;						// set stuff bits to zero
   407 	aDesc.iArgument = 0;						// set stuff bits to zero
   352 	FillSdSpecificCommandDesc(aDesc);
   408 	FillSdSpecificCommandDesc(aDesc);
       
   409 	OstTraceFunctionExit0( DUP1_DSDSESSION_FILLSDSPECIFICCOMMANDDESC_EXIT );
   353 	}
   410 	}
   354 
   411 
   355 void DSDSession::FillSdSpecificCommandDesc(TMMCCommandDesc& aDesc)
   412 void DSDSession::FillSdSpecificCommandDesc(TMMCCommandDesc& aDesc)
   356 	{
   413 	{
       
   414 	OstTraceFunctionEntry0( DUP2_DSDSESSION_FILLSDSPECIFICCOMMANDDESC_ENTRY );
   357 	aDesc.iSpec = FindCommandSpec(SdSpecificCmdSpecTable, aDesc.iCommand);
   415 	aDesc.iSpec = FindCommandSpec(SdSpecificCmdSpecTable, aDesc.iCommand);
   358 	aDesc.iFlags = 0;
   416 	aDesc.iFlags = 0;
   359 	aDesc.iBytesDone = 0;
   417 	aDesc.iBytesDone = 0;
       
   418 	OstTraceFunctionExit0( DUP2_DSDSESSION_FILLSDSPECIFICCOMMANDDESC_EXIT );
   360 	}
   419 	}
   361 
   420 
   362 
   421 
   363 // ======== DSDStack ========
   422 // ======== DSDStack ========
   364 
   423 
   365 EXPORT_C TInt DSDStack::Init()
   424 EXPORT_C TInt DSDStack::Init()
   366 	{
   425 	{
   367 	return DMMCStack::Init();
   426 	OstTraceFunctionEntry1( DSDSTACK_INIT_ENTRY, this );
       
   427 	TInt ret = DMMCStack::Init();
       
   428 	OstTraceFunctionExitExt( DSDSTACK_INIT_EXIT, this, ret );
       
   429 	return ret;
   368 	}
   430 	}
   369 
   431 
   370 
   432 
   371 const TInt KMaxRCASendLoops=3;
   433 const TInt KMaxRCASendLoops=3;
   372 const TUint KSDMaxPollAttempts=25;
   434 const TUint KSDMaxPollAttempts=25;
   397 			EStMoreCardsCheck,
   459 			EStMoreCardsCheck,
   398 			EStEnd
   460 			EStEnd
   399 			};
   461 			};
   400 
   462 
   401 		DMMCSession& s=Session();
   463 		DMMCSession& s=Session();
       
   464 		OstTrace1( TRACE_INTERNALS, DSDSTACK_ATTACHCARDSM, "Current session = 0x%x", &s );
   402 
   465 
   403 	SMF_BEGIN
   466 	SMF_BEGIN
   404 
   467 
       
   468 		OstTrace0( TRACE_INTERNALS, DSDSTACK_ATTACHCARDSM1, "EStBegin" );
   405         __KTRACE_OPT(KPBUS1, Kern::Printf(">DSDStack::AcquireStackSM()"));
   469         __KTRACE_OPT(KPBUS1, Kern::Printf(">DSDStack::AcquireStackSM()"));
   406         
   470         
   407 		iRCAPool.ReleaseUnlocked();
   471 		iRCAPool.ReleaseUnlocked();
   408 		iCxCardCount=0; 		// Reset current card number
   472 		iCxCardCount=0; 		// Reset current card number
   409 
   473 
   410 	SMF_STATE(EStNextFullRange)
   474 	SMF_STATE(EStNextFullRange)
   411 
   475 
       
   476 		OstTrace0( TRACE_INTERNALS, DSDSTACK_ATTACHCARDSM2, "EStNextFullRange" );
   412 		iCxCardType = ESDCardTypeUnknown;
   477 		iCxCardType = ESDCardTypeUnknown;
   413 
   478 
   414 		AddressCard(iCxCardCount); 	// Address the next card
   479 		AddressCard(iCxCardCount); 	// Address the next card
   415 
   480 
   416 		// Before issueing commands, see if there's actually a card present
   481 		// Before issueing commands, see if there's actually a card present
   420 		m.SetTraps(KMMCErrResponseTimeOut);
   485 		m.SetTraps(KMMCErrResponseTimeOut);
   421 		SMF_INVOKES(InitialiseMemoryCardSMST, EStSendCIDIssued)
   486 		SMF_INVOKES(InitialiseMemoryCardSMST, EStSendCIDIssued)
   422 
   487 
   423 	SMF_STATE(EStSendCIDIssued)
   488 	SMF_STATE(EStSendCIDIssued)
   424 
   489 
       
   490 		OstTrace0( TRACE_INTERNALS, DSDSTACK_ATTACHCARDSM3, "EStSendCIDIssued" );
   425 		if( !err )
   491 		if( !err )
   426 			{
   492 			{
   427 			// The card responded with a CID. We need to initialise the
   493 			// The card responded with a CID. We need to initialise the
   428 			// appropriate entry in the card array with the CID. 
   494 			// appropriate entry in the card array with the CID. 
   429 			if (iCxCardType==ESDCardTypeIsSD)
   495 			if (iCxCardType==ESDCardTypeIsSD)
   455 			SMF_GOTOS(EStMoreCardsCheck) // Timed out, try the next card slot
   521 			SMF_GOTOS(EStMoreCardsCheck) // Timed out, try the next card slot
   456 			}
   522 			}
   457 
   523 
   458 	SMF_STATE(EStIssueSendRCA)
   524 	SMF_STATE(EStIssueSendRCA)
   459 
   525 
       
   526 		OstTrace0( TRACE_INTERNALS, DSDSTACK_ATTACHCARDSM4, "EStIssueSendRCA" );
   460 		SMF_INVOKES(ExecCommandSMST,EStSendRCACheck)
   527 		SMF_INVOKES(ExecCommandSMST,EStSendRCACheck)
   461 
   528 
   462 	SMF_STATE(EStSendRCACheck)
   529 	SMF_STATE(EStSendRCACheck)
   463 
   530 
       
   531 		OstTrace0( TRACE_INTERNALS, DSDSTACK_ATTACHCARDSM5, "EStSendRCACheck" );
   464 		// We need to check that the RCA recieved from the card doesn't clash
   532 		// We need to check that the RCA recieved from the card doesn't clash
   465 		// with any others in this stack. RCA is first 2 bytes of response buffer (in big endian)
   533 		// with any others in this stack. RCA is first 2 bytes of response buffer (in big endian)
   466 		TRCA rca=(TUint16)((s.ResponseP()[0]<<8) | s.ResponseP()[1]);
   534 		TRCA rca=(TUint16)((s.ResponseP()[0]<<8) | s.ResponseP()[1]);
   467 		if (CardArray().StoreRCAIfUnique(iCxCardCount,rca)!=KErrNone)
   535 		if (CardArray().StoreRCAIfUnique(iCxCardCount,rca)!=KErrNone)
   468 			SMF_GOTOS( ((++iCxPollRetryCount<KMaxRCASendLoops)?EStIssueSendRCA:EStMoreCardsCheck) )
   536 			SMF_GOTOS( ((++iCxPollRetryCount<KMaxRCASendLoops)?EStIssueSendRCA:EStMoreCardsCheck) )
   469 
   537 
   470 	SMF_STATE(EStRCADone)
   538 	SMF_STATE(EStRCADone)
   471 
   539 
       
   540 		OstTrace0( TRACE_INTERNALS, DSDSTACK_ATTACHCARDSM6, "EStRCADone" );
   472 		SMF_INVOKES(ConfigureMemoryCardSMST, EStMoreCardsCheck)
   541 		SMF_INVOKES(ConfigureMemoryCardSMST, EStMoreCardsCheck)
   473 
   542 
   474 	SMF_STATE(EStMoreCardsCheck)
   543 	SMF_STATE(EStMoreCardsCheck)
   475 
   544 
       
   545 		OstTrace0( TRACE_INTERNALS, DSDSTACK_ATTACHCARDSM7, "EStMoreCardsCheck" );
   476 		if (++iCxCardCount < (TInt)iMaxCardsInStack)
   546 		if (++iCxCardCount < (TInt)iMaxCardsInStack)
   477 		    {
   547 		    {
   478 		    __KTRACE_OPT(KPBUS1, Kern::Printf(">DSDStack::AcquireStackSM(): More Cards to check: %d",iCxCardCount));
   548 		    __KTRACE_OPT(KPBUS1, Kern::Printf(">DSDStack::AcquireStackSM(): More Cards to check: %d",iCxCardCount));
       
   549 			OstTrace1( TRACE_INTERNALS, DSDSTACK_ACQUIRESTACKSM8, "More Cards to check: iCxCardCount=%d", iCxCardCount );		    
   479 			SMF_GOTOS(EStNextFullRange)
   550 			SMF_GOTOS(EStNextFullRange)
   480 		    }
   551 		    }
   481 		else
   552 		else
   482 		    {		   
   553 		    {		   
   483 			AddressCard(KBroadcastToAllCards); // Set back to broadcast mode
   554 			AddressCard(KBroadcastToAllCards); // Set back to broadcast mode
   514 			EStEnd
   585 			EStEnd
   515 			};
   586 			};
   516 
   587 
   517 		DMMCSession& s=Session();
   588 		DMMCSession& s=Session();
   518 		DMMCPsu* psu=(DMMCPsu*)MMCSocket()->iVcc;
   589 		DMMCPsu* psu=(DMMCPsu*)MMCSocket()->iVcc;
       
   590 		OstTrace1( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM, "Current session = 0x%x", &s );
   519 			
   591 			
   520 		static const TUint32 KCmd8Param		= 0x0100 | 0x00AA;	// Voltage supplied : 2.7-3.6V, Check Pattern 10101010b
   592 		static const TUint32 KCmd8Param		= 0x0100 | 0x00AA;	// Voltage supplied : 2.7-3.6V, Check Pattern 10101010b
   521 		static const TUint32 KCmd8CheckMask = 0x00000FFF;
   593 		static const TUint32 KCmd8CheckMask = 0x00000FFF;
   522 
   594 
   523 	SMF_BEGIN
   595 	SMF_BEGIN
   524 
   596 
       
   597 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM1, "EStBegin" );
   525 		iCxCardType = ESDCardTypeUnknown;
   598 		iCxCardType = ESDCardTypeUnknown;
   526 		s.iCardP = NULL;	// This stops ExecCommandSM() from setting old RCA when sending CMD55
   599 		s.iCardP = NULL;	// This stops ExecCommandSM() from setting old RCA when sending CMD55
   527 
   600 
   528 		// Send CMD0 to initialise memory
   601 		// Send CMD0 to initialise memory
   529 		SMF_INVOKES(GoIdleSMST, EStSendInterfaceCondition);
   602 		SMF_INVOKES(GoIdleSMST, EStSendInterfaceCondition);
   530 
   603 
   531 	SMF_STATE(EStSendInterfaceCondition)
   604 	SMF_STATE(EStSendInterfaceCondition)
   532 
   605 
       
   606 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM2, "EStSendInterfaceCondition" );
   533 		iCxPollRetryCount=0; 						 // Reset max number of poll attempts on card busy
   607 		iCxPollRetryCount=0; 						 // Reset max number of poll attempts on card busy
   534 		iConfig.SetPollAttempts(KSDMaxPollAttempts); // Increase card busy timeout to 1 Sec for SD Cards
   608 		iConfig.SetPollAttempts(KSDMaxPollAttempts); // Increase card busy timeout to 1 Sec for SD Cards
   535 
   609 
   536 		iConfig.RemoveMode( KMMCModeEnableTimeOutRetry ); // Temporarily disable timeout retries - since we use a timeout event to distinguish between MMC and SD
   610 		iConfig.RemoveMode( KMMCModeEnableTimeOutRetry ); // Temporarily disable timeout retries - since we use a timeout event to distinguish between MMC and SD
   537 
   611 
   540 		// SD2.0 defines CMD8 as having a new response type - R7
   614 		// SD2.0 defines CMD8 as having a new response type - R7
   541 		// if the PSL doesn't indicate support for R7, use R1 instead
   615 		// if the PSL doesn't indicate support for R7, use R1 instead
   542 		if (!(MMCSocket()->MachineInfo().iFlags & TMMCMachineInfo::ESupportsR7))
   616 		if (!(MMCSocket()->MachineInfo().iFlags & TMMCMachineInfo::ESupportsR7))
   543 			{
   617 			{
   544 			__KTRACE_OPT(KPBUS1, Kern::Printf("R7 not supported."));
   618 			__KTRACE_OPT(KPBUS1, Kern::Printf("R7 not supported."));
       
   619 			OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM3, "R7 not supported" );
   545 			Command().iSpec.iResponseType = ERespTypeR1;
   620 			Command().iSpec.iResponseType = ERespTypeR1;
   546 			}
   621 			}
   547 
   622 
   548  
   623  
   549  		m.SetTraps(KMMCErrAll);
   624  		m.SetTraps(KMMCErrAll);
   550  		SMF_INVOKES(ExecCommandSMST, EStSentInterfaceCondition)
   625  		SMF_INVOKES(ExecCommandSMST, EStSentInterfaceCondition)
   551  
   626  
   552  	SMF_STATE(EStSentInterfaceCondition)
   627  	SMF_STATE(EStSentInterfaceCondition)
   553  
   628  
       
   629  		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM4, "EStSentInterfaceCondition" );
   554  		if (err == KMMCErrNone)
   630  		if (err == KMMCErrNone)
   555  			{
   631  			{
   556  			// Check the response for voltage and check pattern
   632  			// Check the response for voltage and check pattern
   557  			const TUint32 status = TMMC::BigEndian32(s.ResponseP());
   633  			const TUint32 status = TMMC::BigEndian32(s.ResponseP());
   558  			if((status & KCmd8CheckMask) == KCmd8Param)
   634  			if((status & KCmd8CheckMask) == KCmd8Param)
   559  				{
   635  				{
   560  				__KTRACE_OPT(KPBUS1, Kern::Printf("Found v2 card."));
   636  				__KTRACE_OPT(KPBUS1, Kern::Printf("Found v2 card."));
       
   637  				OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM5, "Found v2 card" );
   561 				iCurrentOpRange |= KMMCOCRAccessModeHCS;
   638 				iCurrentOpRange |= KMMCOCRAccessModeHCS;
   562  				}
   639  				}
   563  			else
   640  			else
   564  				{
   641  				{
   565  				// Pattern Mis-match, card does not support the specified voltage range
   642  				// Pattern Mis-match, card does not support the specified voltage range
   566  				return( KMMCErrNotSupported );
   643  				OstTraceFunctionExitExt( DSDSTACK_INITIALISEMEMORYCARDSM_EXIT, this, (TInt) KMMCErrNotSupported );
       
   644  				return KMMCErrNotSupported;
   567  				}
   645  				}
   568 
   646 
   569 			SMF_GOTOS(EStCheckVoltage);
   647 			SMF_GOTOS(EStCheckVoltage);
   570  			}
   648  			}
   571 
   649 
   573 		SMF_INVOKES(GoIdleSMST, EStCheckVoltage);
   651 		SMF_INVOKES(GoIdleSMST, EStCheckVoltage);
   574 
   652 
   575 
   653 
   576 	SMF_STATE(EStCheckVoltage)
   654 	SMF_STATE(EStCheckVoltage)
   577 
   655 
   578 
   656 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM6, "EStCheckVoltage" );
   579 		// If platform doesn't support an adjustable voltage PSU then there's no
   657 		// If platform doesn't support an adjustable voltage PSU then there's no
   580 		// point in doing a full range for its supported range. To support range
   658 		// point in doing a full range for its supported range. To support range
   581 		// checking on a multi-card stack would require a complete scan of all
   659 		// checking on a multi-card stack would require a complete scan of all
   582 		// cards before actually setting the range. This would over-complicate things
   660 		// cards before actually setting the range. This would over-complicate things
   583 		// and make the more normal single card/none adjustable cases less efficient.
   661 		// and make the more normal single card/none adjustable cases less efficient.
   589 			SMF_GOTOS(EStSetRangeCmd)
   667 			SMF_GOTOS(EStSetRangeCmd)
   590 			}
   668 			}
   591 
   669 
   592 	SMF_STATE(EStSetFullRangeCmd)
   670 	SMF_STATE(EStSetFullRangeCmd)
   593 
   671 
       
   672 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM7, "EStSetFullRangeCmd" );
   594 		// Issue ACMD41/CMD1 with omitted voltage range
   673 		// Issue ACMD41/CMD1 with omitted voltage range
   595 		if (iCxCardType==ESDCardTypeIsMMC)
   674 		if (iCxCardType==ESDCardTypeIsMMC)
   596 			{
   675 			{
   597 			s.FillCommandDesc(ECmdSendOpCond, KMMCOCRAccessModeHCS | KMMCOCRBusy); // Full range + Sector Access + Busy bit (iArgument==KBit31)
   676 			s.FillCommandDesc(ECmdSendOpCond, KMMCOCRAccessModeHCS | KMMCOCRBusy); // Full range + Sector Access + Busy bit (iArgument==KBit31)
   598 			SMF_NEXTS(EStFullRangeDone)
   677 			SMF_NEXTS(EStFullRangeDone)
   606 		m.SetTraps(KMMCErrResponseTimeOut);
   685 		m.SetTraps(KMMCErrResponseTimeOut);
   607 		SMF_CALL(ExecCommandSMST)
   686 		SMF_CALL(ExecCommandSMST)
   608 
   687 
   609 	SMF_STATE(EStCheckForFullRangeCmd41Timeout)
   688 	SMF_STATE(EStCheckForFullRangeCmd41Timeout)
   610 	
   689 	
       
   690 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM8, "EStCheckForFullRangeCmd41Timeout" );
   611 		if (err==KMMCErrResponseTimeOut)	
   691 		if (err==KMMCErrResponseTimeOut)	
   612 			{
   692 			{
   613 			__KTRACE_OPT(KPBUS1, Kern::Printf("ACMD 41 not supported - Assuming MMC"));
   693 			__KTRACE_OPT(KPBUS1, Kern::Printf("ACMD 41 not supported - Assuming MMC"));
       
   694 			OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM9, "ACMD 41 not supported - Assuming MMC" );
   614 			iCxCardType=ESDCardTypeIsMMC;
   695 			iCxCardType=ESDCardTypeIsMMC;
   615 
   696 
   616 			// Send CMD0 to re-initialise the card - otherwise we may get 
   697 			// Send CMD0 to re-initialise the card - otherwise we may get 
   617 			// KMMCStatErrIllegalCommand returned for the next command
   698 			// KMMCStatErrIllegalCommand returned for the next command
   618 			// expecting an R1 response. NB The SD spec recommends ignoring the error
   699 			// expecting an R1 response. NB The SD spec recommends ignoring the error
   628 			iCxCardType=ESDCardTypeIsSD;
   709 			iCxCardType=ESDCardTypeIsSD;
   629 			}
   710 			}
   630 
   711 
   631 	SMF_STATE(EStFullRangeDone)
   712 	SMF_STATE(EStFullRangeDone)
   632 
   713 
       
   714 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM10, "EStFullRangeDone" );
   633 		if (!err)												
   715 		if (!err)												
   634 			{
   716 			{
   635 			// Card responded with Op range - evaluate the common subset with the current setting.
   717 			// Card responded with Op range - evaluate the common subset with the current setting.
   636 			// Dont worry about the busy bit for now, we'll check that when we repeat the command
   718 			// Dont worry about the busy bit for now, we'll check that when we repeat the command
   637 			const TUint32 range = (iCurrentOpRange & ~KMMCOCRAccessModeHCS) & (TMMC::BigEndian32(s.ResponseP()) & ~KMMCOCRBusy);
   719 			const TUint32 range = (iCurrentOpRange & ~KMMCOCRAccessModeHCS) & (TMMC::BigEndian32(s.ResponseP()) & ~KMMCOCRBusy);
   638 			if(range == 0)
   720 			if(range == 0)
   639 				{
   721 				{
   640 				return( KMMCErrNotSupported ); // Card is incompatible with our h/w
   722 				OstTraceFunctionExitExt( DSDSTACK_INITIALISEMEMORYCARDSM_EXIT1, this, (TInt) KMMCErrNotSupported );
       
   723 				return KMMCErrNotSupported; // Card is incompatible with our h/w
   641 				}
   724 				}
   642 			iCurrentOpRange = range | (iCurrentOpRange & KMMCOCRAccessModeHCS);
   725 			iCurrentOpRange = range | (iCurrentOpRange & KMMCOCRAccessModeHCS);
   643 			}
   726 			}
   644 
   727 
   645 		// Repeat SEND_OP_COND this time setting Current Op Range
   728 		// Repeat SEND_OP_COND this time setting Current Op Range
   654 				}
   737 				}
   655 			}
   738 			}
   656 
   739 
   657 	SMF_STATE(EStSetRangeCmd)
   740 	SMF_STATE(EStSetRangeCmd)
   658 
   741 
       
   742 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM11, "EStSetRangeCmd" );
   659 		// Issue ACMD41/CMD1 with voltage range
   743 		// Issue ACMD41/CMD1 with voltage range
   660 		if (iCxCardType==ESDCardTypeIsMMC)
   744 		if (iCxCardType==ESDCardTypeIsMMC)
   661 			{
   745 			{
   662 			s.FillCommandDesc(ECmdSendOpCond,(iCurrentOpRange | KMMCOCRAccessModeHCS | KMMCOCRBusy)); // Range supported + Sector Access Busy bit (iArgument==KBit31)
   746 			s.FillCommandDesc(ECmdSendOpCond,(iCurrentOpRange | KMMCOCRAccessModeHCS | KMMCOCRBusy)); // Range supported + Sector Access Busy bit (iArgument==KBit31)
   663 			SMF_NEXTS(EStSetRangeBusyCheck)
   747 			SMF_NEXTS(EStSetRangeBusyCheck)
   676 		m.SetTraps(KMMCErrResponseTimeOut);
   760 		m.SetTraps(KMMCErrResponseTimeOut);
   677 		SMF_CALL(ExecCommandSMST)
   761 		SMF_CALL(ExecCommandSMST)
   678 
   762 
   679 	SMF_STATE(EStCheckForRangeCmd41Timeout)
   763 	SMF_STATE(EStCheckForRangeCmd41Timeout)
   680 	
   764 	
       
   765 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM12, "EStCheckForRangeCmd41Timeout" );
   681 		__KTRACE_OPT(KPBUS1, Kern::Printf("-mst:ascs:crct:%d", err));
   766 		__KTRACE_OPT(KPBUS1, Kern::Printf("-mst:ascs:crct:%d", err));
       
   767 		OstTrace1( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM13, "err=%d", (TInt) err);
   682 		if (err==KMMCErrResponseTimeOut)	
   768 		if (err==KMMCErrResponseTimeOut)	
   683 			{
   769 			{
   684 			iCxCardType=ESDCardTypeIsMMC;
   770 			iCxCardType=ESDCardTypeIsMMC;
   685 			// Send CMD0 to re-initialise the card - otherwise we may get 
   771 			// Send CMD0 to re-initialise the card - otherwise we may get 
   686 			// KMMCStatErrIllegalCommand returned for the next command
   772 			// KMMCStatErrIllegalCommand returned for the next command
   694 			{
   780 			{
   695 			// No response timeout - so it must be an SD Card
   781 			// No response timeout - so it must be an SD Card
   696 			__KTRACE_OPT(KPBUS1, Kern::Printf("-mst:ascs:crct2:%x", iCardArray));
   782 			__KTRACE_OPT(KPBUS1, Kern::Printf("-mst:ascs:crct2:%x", iCardArray));
   697 			__KTRACE_OPT(KPBUS1, Kern::Printf("-mst:ascs:crct3:%x", iCxCardCount));
   783 			__KTRACE_OPT(KPBUS1, Kern::Printf("-mst:ascs:crct3:%x", iCxCardCount));
   698 			__KTRACE_OPT(KPBUS1, Kern::Printf("-mst:ascs:crct4:%x", CardArray().CardP(iCxCardCount)));
   784 			__KTRACE_OPT(KPBUS1, Kern::Printf("-mst:ascs:crct4:%x", CardArray().CardP(iCxCardCount)));
       
   785 			OstTraceExt3(TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM14, "iCardArray=0x%x;iCxCardCount=%d;CardArray().CardP(iCxCardCount)=%d", (TUint) iCardArray, (TInt) iCxCardCount, (TInt) CardArray().CardP(iCxCardCount));
   699 
   786 
   700 			(CardArray().CardP(iCxCardCount)->iFlags)|=KSDCardIsSDCard;
   787 			(CardArray().CardP(iCxCardCount)->iFlags)|=KSDCardIsSDCard;
   701 			iCxCardType=ESDCardTypeIsSD;
   788 			iCxCardType=ESDCardTypeIsSD;
   702 			}
   789 			}
   703 			
   790 			
   704 	SMF_STATE(EStSetRangeBusyCheck)
   791 	SMF_STATE(EStSetRangeBusyCheck)
   705 
   792 
       
   793 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM15, "EStSetRangeBusyCheck" );
   706 		__KTRACE_OPT(KPBUS1, Kern::Printf("-mst:ascs:src:%d",iCxCardType)); // 1:MMC, 2:SD
   794 		__KTRACE_OPT(KPBUS1, Kern::Printf("-mst:ascs:src:%d",iCxCardType)); // 1:MMC, 2:SD
       
   795 		OstTrace1( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM16, "iCxCardType=%d", iCxCardType);
   707 		
   796 		
   708 		if ( !err )
   797 		if ( !err )
   709 			{
   798 			{
   710 			const TUint32 ocrResponse = TMMC::BigEndian32(s.ResponseP());
   799 			const TUint32 ocrResponse = TMMC::BigEndian32(s.ResponseP());
   711 
   800 
   714 				__KTRACE_OPT(KPBUS1,Kern::Printf("-sd:upd:bsy"));
   803 				__KTRACE_OPT(KPBUS1,Kern::Printf("-sd:upd:bsy"));
   715 				// Card is still busy powering up. Check if we should timeout
   804 				// Card is still busy powering up. Check if we should timeout
   716 				if ( ++iCxPollRetryCount > iConfig.OpCondBusyTimeout() )
   805 				if ( ++iCxPollRetryCount > iConfig.OpCondBusyTimeout() )
   717 					{
   806 					{
   718 					__KTRACE_OPT2(KPBUS1, KPANIC, Kern::Printf("-sd:ocr busy timed out"));
   807 					__KTRACE_OPT2(KPBUS1, KPANIC, Kern::Printf("-sd:ocr busy timed out"));
   719 					return( KMMCErrBusTimeOut );
   808 					OstTraceFunctionExitExt( DSDSTACK_INITIALISEMEMORYCARDSM_EXIT2, this, (TInt) KMMCErrBusTimeOut );
       
   809 					return KMMCErrBusTimeOut;
   720 					}
   810 					}
   721 					
   811 					
   722 #ifdef _DEBUG
   812 #ifdef _DEBUG
   723 				if ( iCxPollRetryCount > KMMCSpecOpCondBusyTimeout )
   813 				if ( iCxPollRetryCount > KMMCSpecOpCondBusyTimeout )
   724 					{
   814 					{
   725 					__KTRACE_OPT2(KPBUS1, KPANIC, Kern::Printf("-sd:ocr exceeded spec timeout!! (%d ms)", (iCxPollRetryCount*KMMCRetryGapInMilliseconds)));
   815 					__KTRACE_OPT2(KPBUS1, KPANIC, Kern::Printf("-sd:ocr exceeded spec timeout!! (%d ms)", (iCxPollRetryCount*KMMCRetryGapInMilliseconds)));
       
   816 					OstTrace1( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM17, "Exceeded spec timeout (%d ms)", (iCxPollRetryCount*KMMCRetryGapInMilliseconds));
   726 					}
   817 					}
   727 #endif
   818 #endif
   728 				m.ResetTraps(); 
   819 				m.ResetTraps(); 
   729 
   820 
   730 				SMF_INVOKES(RetryGapTimerSMST,EStSetRangeCmd)
   821 				SMF_INVOKES(RetryGapTimerSMST,EStSetRangeCmd)
   736 					CardArray().CardP(iCxCardCount)->iFlags |= KMMCardIsHighCapacity;
   827 					CardArray().CardP(iCxCardCount)->iFlags |= KMMCardIsHighCapacity;
   737 #ifdef _DEBUG				
   828 #ifdef _DEBUG				
   738 					if(iCxCardType == ESDCardTypeIsSD)
   829 					if(iCxCardType == ESDCardTypeIsSD)
   739 						{
   830 						{
   740 						__KTRACE_OPT(KPBUS1, Kern::Printf("Found large SD card."));
   831 						__KTRACE_OPT(KPBUS1, Kern::Printf("Found large SD card."));
       
   832 						OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM18, "Found large SD card" );
   741 						}
   833 						}
   742 					else if(iCxCardType == ESDCardTypeIsMMC)
   834 					else if(iCxCardType == ESDCardTypeIsMMC)
   743 						{
   835 						{
   744 						__KTRACE_OPT(KPBUS1, Kern::Printf("Found large MMC card."));
   836 						__KTRACE_OPT(KPBUS1, Kern::Printf("Found large MMC card."));
       
   837 						OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM19, "Found large MMC card" );
   745 						}
   838 						}
   746 #endif
   839 #endif
   747 					}
   840 					}
   748 				}
   841 				}
   749 			}
   842 			}
   763 			}
   856 			}
   764 
   857 
   765 		psu->SetVoltage(iCurrentOpRange);
   858 		psu->SetVoltage(iCurrentOpRange);
   766 		if (psu->SetState(EPsuOnFull) != KErrNone)
   859 		if (psu->SetState(EPsuOnFull) != KErrNone)
   767 			{
   860 			{
   768 			return(KMMCErrHardware);
   861 			OstTraceFunctionExitExt( DSDSTACK_INITIALISEMEMORYCARDSM_EXIT3, this, (TInt) KMMCErrHardware );
       
   862 			return KMMCErrHardware;
   769 			}
   863 			}
   770 
   864 
   771 	SMF_STATE(EStCIDCmd)
   865 	SMF_STATE(EStCIDCmd)
   772 
   866 
       
   867 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM20, "EStCIDCmd" );
   773 		s.FillCommandDesc(ECmdAllSendCID,0);
   868 		s.FillCommandDesc(ECmdAllSendCID,0);
   774 		m.ResetTraps();
   869 		m.ResetTraps();
   775 		SMF_INVOKES(ExecCommandSMST,EStSendCIDIssued)
   870 		SMF_INVOKES(ExecCommandSMST,EStSendCIDIssued)
   776 
   871 
   777 	SMF_STATE(EStSendCIDIssued)
   872 	SMF_STATE(EStSendCIDIssued)
   778 
   873 
   779 
   874 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITIALISEMEMORYCARDSM21, "EStSendCIDIssued" );
   780 		// All done - Higher level state machine expects CID in s.ResponseP()
   875 		// All done - Higher level state machine expects CID in s.ResponseP()
   781 
   876 
   782 	SMF_END
   877 	SMF_END
   783 	}
   878 	}
   784 
   879 
   795 			EStSendCSDDone,
   890 			EStSendCSDDone,
   796 			EStEnd
   891 			EStEnd
   797 			};
   892 			};
   798 
   893 
   799 		DMMCSession& s=Session();
   894 		DMMCSession& s=Session();
       
   895 		OstTrace1( TRACE_INTERNALS, DSDSTACK_CONFIGUREMEMORYCARDSM, "Current session = 0x%x", &s );
   800 
   896 
   801 	//coverity[UNREACHABLE]
   897 	//coverity[UNREACHABLE]
   802 	//Part of state machine design.
   898 	//Part of state machine design.
   803 	SMF_BEGIN
   899 	SMF_BEGIN
   804 
   900 
       
   901 		OstTrace0( TRACE_INTERNALS, DSDSTACK_CONFIGUREMEMORYCARDSM1, "EStBegin" );
   805 		// Cards is initialised so get its CSD
   902 		// Cards is initialised so get its CSD
   806 
   903 
   807 		s.FillCommandDesc(ECmdSendCSD, TUint32(CardArray().CardP(iCxCardCount)->iRCA) << 16);
   904 		s.FillCommandDesc(ECmdSendCSD, TUint32(CardArray().CardP(iCxCardCount)->iRCA) << 16);
   808 		SMF_INVOKES(ExecCommandSMST, EStSendCSDDone)
   905 		SMF_INVOKES(ExecCommandSMST, EStSendCSDDone)
   809 
   906 
   810 	SMF_STATE(EStSendCSDDone)
   907 	SMF_STATE(EStSendCSDDone)
   811 
   908 
       
   909 		OstTrace0( TRACE_INTERNALS, DSDSTACK_CONFIGUREMEMORYCARDSM2, "EStSendCSDDone" );
   812 		// Store the CSD in the new card entry
   910 		// Store the CSD in the new card entry
   813 		TMMCard* cardP = CardArray().CardP(iCxCardCount);
   911 		TMMCard* cardP = CardArray().CardP(iCxCardCount);
   814 		cardP->iCSD = s.ResponseP();
   912 		cardP->iCSD = s.ResponseP();
   815 
   913 
   816 		if(CardArray().Card(iCxCardCount).IsSDCard())
   914 		if(CardArray().Card(iCxCardCount).IsSDCard())
   861 			EStMoreCardsCheck,
   959 			EStMoreCardsCheck,
   862 			EStEnd
   960 			EStEnd
   863 			};
   961 			};
   864 
   962 
   865 		DMMCSession& s=Session();
   963 		DMMCSession& s=Session();
       
   964 		OstTrace1( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM, "Current session = 0x%x", &s );
   866 
   965 
   867 	SMF_BEGIN
   966 	SMF_BEGIN
   868 
   967 
       
   968 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM1, "EStBegin" );
   869         __KTRACE_OPT(KPBUS1, Kern::Printf(">DSDStack::InitStackAfterUnlockSM()"));
   969         __KTRACE_OPT(KPBUS1, Kern::Printf(">DSDStack::InitStackAfterUnlockSM()"));
   870 		iRCAPool.ReleaseUnlocked();
   970 		iRCAPool.ReleaseUnlocked();
   871 		iCxCardCount=0; 		// Reset current card number
   971 		iCxCardCount=0; 		// Reset current card number
   872 
   972 
   873 	SMF_STATE(EStNextCard)	    
   973 	SMF_STATE(EStNextCard)	    
       
   974 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM2, "EStNextCard" );
   874 		AddressCard(iCxCardCount); 	// Address the next card
   975 		AddressCard(iCxCardCount); 	// Address the next card
   875 
   976 
   876 		if (!CardDetect(iCxCardCount))
   977 		if (!CardDetect(iCxCardCount))
   877 			SMF_GOTOS(EStMoreCardsCheck)
   978 			SMF_GOTOS(EStMoreCardsCheck)
   878 
   979 
   883 			SMF_INVOKES( DMMCStack::InitCurrentCardAfterUnlockSMST, EStMoreCardsCheck )
   984 			SMF_INVOKES( DMMCStack::InitCurrentCardAfterUnlockSMST, EStMoreCardsCheck )
   884 			}
   985 			}
   885 
   986 
   886 	SMF_STATE(EStSelectCard)
   987 	SMF_STATE(EStSelectCard)
   887 
   988 
       
   989 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM3, "EStSelectCard" );
   888 		TRCA targetRCA = CardArray().Card(iCxCardCount).RCA();
   990 		TRCA targetRCA = CardArray().Card(iCxCardCount).RCA();
   889 		if (targetRCA == SelectedCard())
   991 		if (targetRCA == SelectedCard())
   890 			{
   992 			{
   891 			SMF_GOTOS(EStSetBusWidth)
   993 			SMF_GOTOS(EStSetBusWidth)
   892 			}
   994 			}
   893 
   995 
   894 		s.FillCommandDesc(ECmdSelectCard, targetRCA);
   996 		s.FillCommandDesc(ECmdSelectCard, targetRCA);
   895 		SMF_INVOKES(ExecCommandSMST,EStSetBusWidth)
   997 		SMF_INVOKES(ExecCommandSMST,EStSetBusWidth)
   896 
   998 
   897 	SMF_STATE(EStSetBusWidth)
   999 	SMF_STATE(EStSetBusWidth)
       
  1000 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM4, "EStSetBusWidth" );
   898 		const TMMCStatus status = s.LastStatus();
  1001 		const TMMCStatus status = s.LastStatus();
   899 		if((status & KMMCStatCardIsLocked) != 0)
  1002 		if((status & KMMCStatCardIsLocked) != 0)
   900 			SMF_GOTOS(EStDeselectCard)
  1003 			SMF_GOTOS(EStDeselectCard)
   901 
  1004 
   902 		// set bus width with ACMD6
  1005 		// set bus width with ACMD6
   903 		TUint32 arg = TUint32(CardArray().Card(iCxCardCount).RCA()) << 16;
  1006 		TUint32 arg = TUint32(CardArray().Card(iCxCardCount).RCA()) << 16;
   904 		s.FillCommandDesc(ECmdAppCmd, arg);
  1007 		s.FillCommandDesc(ECmdAppCmd, arg);
   905 		SMF_INVOKES(IssueCommandCheckResponseSMST,EStSetBusWidth1)
  1008 		SMF_INVOKES(IssueCommandCheckResponseSMST,EStSetBusWidth1)
   906 
  1009 
   907 	SMF_STATE(EStSetBusWidth1)
  1010 	SMF_STATE(EStSetBusWidth1)
       
  1011 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM5, "EStSetBusWidth1" );
   908 		CardArray().Card(iCxCardCount).SetBusWidth(4);
  1012 		CardArray().Card(iCxCardCount).SetBusWidth(4);
   909 		DSDSession::FillAppCommandDesc(Command(), ESDACmdSetBusWidth, KSDBusWidth4);
  1013 		DSDSession::FillAppCommandDesc(Command(), ESDACmdSetBusWidth, KSDBusWidth4);
   910 		SMF_INVOKES(IssueCommandCheckResponseSMST,EStGetSDStatus)
  1014 		SMF_INVOKES(IssueCommandCheckResponseSMST,EStGetSDStatus)
   911 
  1015 
   912 	SMF_STATE(EStGetSDStatus)
  1016 	SMF_STATE(EStGetSDStatus)
       
  1017 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM6, "EStGetSDStatus" );
   913 		// Now we have sent ACMD6, ask the controller to set the bus width to 4
  1018 		// Now we have sent ACMD6, ask the controller to set the bus width to 4
   914 		DoSetBusWidth(EBusWidth4);
  1019 		DoSetBusWidth(EBusWidth4);
   915 
  1020 
   916 		// get protected area size with ACMD13
  1021 		// get protected area size with ACMD13
   917 		TUint32 arg = TUint32(CardArray().Card(iCxCardCount).RCA()) << 16;
  1022 		TUint32 arg = TUint32(CardArray().Card(iCxCardCount).RCA()) << 16;
   918 		s.FillCommandDesc(ECmdAppCmd,arg);
  1023 		s.FillCommandDesc(ECmdAppCmd,arg);
   919 		SMF_INVOKES(IssueCommandCheckResponseSMST,EStGetSDStatus1)
  1024 		SMF_INVOKES(IssueCommandCheckResponseSMST,EStGetSDStatus1)
   920 
  1025 
   921 	SMF_STATE(EStGetSDStatus1)
  1026 	SMF_STATE(EStGetSDStatus1)
       
  1027 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM7, "EStGetSDStatus1" );
   922 		DSDSession::FillAppCommandDesc(Command(), ESDACmdSDStatus);
  1028 		DSDSession::FillAppCommandDesc(Command(), ESDACmdSDStatus);
   923 		s.FillCommandArgs(0, KSDStatusBlockLength, iPSLBuf, KSDStatusBlockLength);
  1029 		s.FillCommandArgs(0, KSDStatusBlockLength, iPSLBuf, KSDStatusBlockLength);
   924 		SMF_INVOKES(IssueCommandCheckResponseSMST,EStDecodeSDStatus);
  1030 		SMF_INVOKES(IssueCommandCheckResponseSMST,EStDecodeSDStatus);
   925 
  1031 
   926 	SMF_STATE(EStDecodeSDStatus)
  1032 	SMF_STATE(EStDecodeSDStatus)
       
  1033 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM8, "EStDecodeSDStatus" );
   927 #ifdef _DEBUG
  1034 #ifdef _DEBUG
   928 		for (TUint i = 0; i < KSDStatusBlockLength; ++i)
  1035 		for (TUint i = 0; i < KSDStatusBlockLength; ++i)
   929 			{
  1036 			{
   930 			__KTRACE_OPT(KPBUS1, Kern::Printf("SD_STATUS[0x%x] = %x", i, iPSLBuf[i]));
  1037 			__KTRACE_OPT(KPBUS1, Kern::Printf("SD_STATUS[0x%x] = %x", i, iPSLBuf[i]));
       
  1038 			OstTraceExt2( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM9, "SD_STATUS[0x%x]=0x%x", i, (TUint) iPSLBuf[i]);
   931 			}
  1039 			}
   932 #endif
  1040 #endif
   933 		// bits 495:480 are SD_CARD_TYPE.  Check this is 00xxh (x = don't care).
  1041 		// bits 495:480 are SD_CARD_TYPE.  Check this is 00xxh (x = don't care).
   934 
  1042 
   935 		if (iPSLBuf[2] != 0)
  1043 		if (iPSLBuf[2] != 0)
       
  1044 		    {
       
  1045 			OstTraceFunctionExitExt( DSDSTACK_INITSTACKAFTERUNLOCKSM_EXIT, this, (TInt) KMMCErrNotSupported );
   936 			return KMMCErrNotSupported;
  1046 			return KMMCErrNotSupported;
       
  1047 		    }
   937 
  1048 
   938 		// bits 479:448 contain SIZE_OF_PROTECTED_AREA.  
  1049 		// bits 479:448 contain SIZE_OF_PROTECTED_AREA.  
   939 		// (This is bytes 4 to 7 in big-endian format.)
  1050 		// (This is bytes 4 to 7 in big-endian format.)
   940 
  1051 
   941 		TSDCard& sdc = CardArray().Card(iCxCardCount);
  1052 		TSDCard& sdc = CardArray().Card(iCxCardCount);
   942 		__KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack: Card %d", iCxCardCount));
  1053 		__KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack: Card %d", iCxCardCount));
   943 		TUint32 size_of_protected_area = TMMC::BigEndian32(&iPSLBuf[4]);
  1054 		TUint32 size_of_protected_area = TMMC::BigEndian32(&iPSLBuf[4]);
   944 		__KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack: SizeOfProtectedArea: %d", size_of_protected_area));
  1055 		__KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack: SizeOfProtectedArea: %d", size_of_protected_area));
       
  1056 		OstTraceExt2( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM10, "iCxCardCount=%d;SizeOfProtectedArea=%d", iCxCardCount, (TInt) size_of_protected_area);
   945 		const TCSD& csd = sdc.CSD();
  1057 		const TCSD& csd = sdc.CSD();
   946 		TUint32 pas = 0;
  1058 		TUint32 pas = 0;
   947 		
  1059 		
   948 		if (sdc.IsHighCapacity())
  1060 		if (sdc.IsHighCapacity())
   949 			{
  1061 			{
   950 			// High Capacity Card
  1062 			// High Capacity Card
   951 			// Protected Area = SIZE_OF_PROTECTED_AREA
  1063 			// Protected Area = SIZE_OF_PROTECTED_AREA
   952 			pas = size_of_protected_area;
  1064 			pas = size_of_protected_area;
   953 			__KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack(SDHC): SetProtectedAreaSize: %d", pas));
  1065 			__KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack(SDHC): SetProtectedAreaSize: %d", pas));
       
  1066 			OstTrace1( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM11, "SDHC: SetProtectedAreaSize=%d", pas);
   954 			}
  1067 			}
   955 		else
  1068 		else
   956 			{
  1069 			{
   957 			// Standard Capacity Card
  1070 			// Standard Capacity Card
   958 			// Protected Area = SIZE_OF_PROTECTED_AREA * C_SIZE_MULT * BLOCK_LEN
  1071 			// Protected Area = SIZE_OF_PROTECTED_AREA * C_SIZE_MULT * BLOCK_LEN
   959 			pas = size_of_protected_area * (1 << (csd.CSizeMult() + 2 + csd.ReadBlLen()));
  1072 			pas = size_of_protected_area * (1 << (csd.CSizeMult() + 2 + csd.ReadBlLen()));
   960 			__KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack(SDSC): SetProtectedAreaSize: %d", pas));
  1073 			__KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack(SDSC): SetProtectedAreaSize: %d", pas));
       
  1074 			OstTrace1( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM12, "SDSC: SetProtectedAreaSize=%d", pas);
   961 			}		
  1075 			}		
   962 
  1076 
   963 		sdc.SetProtectedAreaSize(pas);
  1077 		sdc.SetProtectedAreaSize(pas);
   964 
  1078 
   965 		//bits 431:428 contain AU_SIZE
  1079 		//bits 431:428 contain AU_SIZE
   970 		sdc.SetAUSize(au);
  1084 		sdc.SetAUSize(au);
   971 
  1085 
   972 		SMF_INVOKES(SwitchToHighSpeedModeSMST, EStDeselectCard)
  1086 		SMF_INVOKES(SwitchToHighSpeedModeSMST, EStDeselectCard)
   973 
  1087 
   974 	SMF_STATE(EStDeselectCard)
  1088 	SMF_STATE(EStDeselectCard)
       
  1089 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM13, "EStDeselectCard" );
   975 		s.FillCommandDesc(ECmdSelectCard, 0);
  1090 		s.FillCommandDesc(ECmdSelectCard, 0);
   976 		SMF_INVOKES(ExecCommandSMST, EStCardDeselectedReadCSD)
  1091 		SMF_INVOKES(ExecCommandSMST, EStCardDeselectedReadCSD)
   977     
  1092     
   978 	SMF_STATE(EStCardDeselectedReadCSD)
  1093 	SMF_STATE(EStCardDeselectedReadCSD)
       
  1094 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM14, "EStCardDeselectedReadCSD" );
   979 		//
  1095 		//
   980       	// Read the card's CSD register (again)
  1096       	// Read the card's CSD register (again)
   981 		//
  1097 		//
   982 		//  - We re-read the CSD, as the TRAN_SPEED field may have changed due to a switch to HS Mode
  1098 		//  - We re-read the CSD, as the TRAN_SPEED field may have changed due to a switch to HS Mode
   983 		//
  1099 		//
   984       	TUint32 arg = TUint32(CardArray().Card(iCxCardCount).RCA()) << 16;
  1100       	TUint32 arg = TUint32(CardArray().Card(iCxCardCount).RCA()) << 16;
   985       	s.FillCommandDesc( ECmdSendCSD, arg );
  1101       	s.FillCommandDesc( ECmdSendCSD, arg );
   986       	SMF_INVOKES(ExecCommandSMST, EStCSDCmdSent)
  1102       	SMF_INVOKES(ExecCommandSMST, EStCSDCmdSent)
   987 
  1103 
   988 	SMF_STATE(EStCSDCmdSent)
  1104 	SMF_STATE(EStCSDCmdSent)
       
  1105 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM15, "EStCSDCmdSent" );
   989 		//
  1106 		//
   990       	// Store the CSD in the card entry
  1107       	// Store the CSD in the card entry
   991 		//
  1108 		//
   992       	TMMCard* cardP = iCardArray->CardP(iCxCardCount);
  1109       	TMMCard* cardP = iCardArray->CardP(iCxCardCount);
   993       	cardP->iCSD = s.ResponseP();
  1110       	cardP->iCSD = s.ResponseP();
   994 
  1111 
   995 	SMF_STATE(EStMoreCardsCheck)
  1112 	SMF_STATE(EStMoreCardsCheck)
       
  1113 		OstTrace0( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM16, "EStMoreCardsCheck" );
   996 		if (++iCxCardCount < (TInt)iMaxCardsInStack)
  1114 		if (++iCxCardCount < (TInt)iMaxCardsInStack)
   997 		    {
  1115 		    {
   998 		    __KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack: Address Next card: %d",iCxCardCount));
  1116 		    __KTRACE_OPT(KPBUS1, Kern::Printf("\t >DSDStack: Address Next card: %d",iCxCardCount));
       
  1117 		    OstTrace1( TRACE_INTERNALS, DSDSTACK_INITSTACKAFTERUNLOCKSM17, "Address Next card=%d", iCxCardCount);
   999 			SMF_GOTOS(EStNextCard)
  1118 			SMF_GOTOS(EStNextCard)
  1000 		    }
  1119 		    }
  1001 		else
  1120 		else
  1002 		    {
  1121 		    {
  1003 			AddressCard(KBroadcastToAllCards);
  1122 			AddressCard(KBroadcastToAllCards);
  1039 			EStDone,
  1158 			EStDone,
  1040 			EStEnd
  1159 			EStEnd
  1041 			};
  1160 			};
  1042 
  1161 
  1043 		DMMCSession& s = Session();
  1162 		DMMCSession& s = Session();
       
  1163 		OstTrace1( TRACE_INTERNALS, DSDSTACK_CIMREADWRITEBLOCKSSM, "Current session = 0x%x", &s );
  1044 
  1164 
  1045 		__KTRACE_OPT(KPBUS1,Kern::Printf(">SD:RWBlocksSM %x",TUint(s.iLastStatus)));
  1165 		__KTRACE_OPT(KPBUS1,Kern::Printf(">SD:RWBlocksSM %x",TUint(s.iLastStatus)));
  1046 
  1166 
  1047 	SMF_BEGIN
  1167 	SMF_BEGIN
  1048 
  1168 
       
  1169         OstTrace0( TRACE_INTERNALS, DSDSTACK_CIMREADWRITEBLOCKSSM1, "EStBegin" );
  1049 		TSDCard& sdCard = *static_cast<TSDCard*>(s.iCardP);
  1170 		TSDCard& sdCard = *static_cast<TSDCard*>(s.iCardP);
  1050 		AddressCard(sdCard.iIndex-1);
  1171 		AddressCard(sdCard.iIndex-1);
  1051 
  1172 
  1052 		if(sdCard.IsSDCard() == EFalse)
  1173 		if(sdCard.IsSDCard() == EFalse)
  1053 			{
  1174 			{
  1061 		if(s.iSessionID == ECIMWriteBlock || s.iSessionID == ECIMWriteMBlock)
  1182 		if(s.iSessionID == ECIMWriteBlock || s.iSessionID == ECIMWriteMBlock)
  1062 			{
  1183 			{
  1063 			// Check that the card supports class 4 (Write) commands
  1184 			// Check that the card supports class 4 (Write) commands
  1064 			const TUint ccc = s.iCardP->CSD().CCC();
  1185 			const TUint ccc = s.iCardP->CSD().CCC();
  1065 			if(!(ccc & KMMCCmdClassBlockWrite))
  1186 			if(!(ccc & KMMCCmdClassBlockWrite))
  1066 				return( KMMCErrNotSupported );
  1187 			    {
       
  1188 				OstTraceFunctionExitExt( DSDSTACK_CIMREADWRITEBLOCKSSM_EXIT, this, (TInt) KMMCErrNotSupported );
       
  1189 				return KMMCErrNotSupported;
       
  1190 			    }
  1067 			}
  1191 			}
  1068 
  1192 
  1069 		Command().iCustomRetries = 0;			// MBW retries
  1193 		Command().iCustomRetries = 0;			// MBW retries
  1070 		s.iState |= KMMCSessStateInProgress;
  1194 		s.iState |= KMMCSessStateInProgress;
  1071 		m.SetTraps(KMMCErrInitContext);
  1195 		m.SetTraps(KMMCErrInitContext);
  1072 
  1196 
  1073 	SMF_STATE(EStRestart)		// NB: ErrBypass is not processed here
  1197 	SMF_STATE(EStRestart)		// NB: ErrBypass is not processed here
  1074 
  1198 
       
  1199         OstTrace0( TRACE_INTERNALS, DSDSTACK_CIMREADWRITEBLOCKSSM2, "EStRestart" );
  1075 		SMF_CALLMEWR(EStRestart) // Create a recursive call entry to recover from the errors trapped
  1200 		SMF_CALLMEWR(EStRestart) // Create a recursive call entry to recover from the errors trapped
  1076 		m.SetTraps(KMMCErrStatus);
  1201 		m.SetTraps(KMMCErrStatus);
  1077 		if (s.Command().iSpec.iCommandClass!=KMMCCmdClassApplication || s.Command().iCommand==ECmdAppCmd )
  1202 		if (s.Command().iSpec.iCommandClass!=KMMCCmdClassApplication || s.Command().iCommand==ECmdAppCmd )
  1078 			{
  1203 			{
  1079 			s.ResetCommandStack();
  1204 			s.ResetCommandStack();
  1080 			SMF_INVOKES( AttachCardSMST, EStAttached )	// attachment is mandatory here
  1205 			SMF_INVOKES( AttachCardSMST, EStAttached )	// attachment is mandatory here
  1081 			}
  1206 			}
  1082 
  1207 
  1083 	SMF_BPOINT(EStAttached)
  1208 	SMF_BPOINT(EStAttached)
  1084 
  1209 
       
  1210         OstTrace0( TRACE_INTERNALS, DSDSTACK_CIMREADWRITEBLOCKSSM3, "EStAttached" );
  1085 		TMMCCommandDesc& cmd = s.Command();
  1211 		TMMCCommandDesc& cmd = s.Command();
  1086 
  1212 
  1087 		const TUint32 blockLength = cmd.BlockLength();
  1213 		const TUint32 blockLength = cmd.BlockLength();
  1088 		if((blockLength == 0) || (blockLength > (TUint)KDefaultBlockLenInBytes))
  1214 		if((blockLength == 0) || (blockLength > (TUint)KDefaultBlockLenInBytes))
  1089 			{
  1215 			{
  1090 			__KTRACE_OPT(KPBUS1,Kern::Printf(">SD:RWBlocksSM err BlockLen:%d",blockLength));
  1216 			__KTRACE_OPT(KPBUS1,Kern::Printf(">SD:RWBlocksSM err BlockLen:%d",blockLength));
       
  1217 			OstTrace1( TRACE_INTERNALS, DSDSTACK_CIMREADWRITEBLOCKSSM4, "blockLength=%d", blockLength );
       
  1218 			OstTraceFunctionExitExt( DSDSTACK_CIMREADWRITEBLOCKSSM_EXIT1, this, (TInt) KMMCErrArgument );
  1091 			return KMMCErrArgument;
  1219 			return KMMCErrArgument;
  1092 			}
  1220 			}
  1093 
  1221 
  1094 		if(s.iSessionID == ECIMReadBlock	||
  1222 		if(s.iSessionID == ECIMReadBlock	||
  1095 		   s.iSessionID == ECIMWriteBlock	||
  1223 		   s.iSessionID == ECIMWriteBlock	||
  1098 			{	
  1226 			{	
  1099 			// read/write operation
  1227 			// read/write operation
  1100 			if(!cmd.AdjustForBlockOrByteAccess(s))
  1228 			if(!cmd.AdjustForBlockOrByteAccess(s))
  1101 				{
  1229 				{
  1102 				// unable to convert command arguments to suit the underlying block/byte access mode
  1230 				// unable to convert command arguments to suit the underlying block/byte access mode
       
  1231 				OstTraceFunctionExitExt( DSDSTACK_CIMREADWRITEBLOCKSSM_EXIT2, this, (TInt) KMMCErrArgument );
  1103 				return KMMCErrArgument;
  1232 				return KMMCErrArgument;
  1104 				}
  1233 				}
  1105 			}
  1234 			}
  1106 
  1235 
  1107 		// Set the block length if it has changed. Always set for ECIMLockUnlock.
  1236 		// Set the block length if it has changed. Always set for ECIMLockUnlock.
  1115 		s.FillCommandDesc( ECmdSetBlockLen, blockLength );
  1244 		s.FillCommandDesc( ECmdSetBlockLen, blockLength );
  1116 		SMF_INVOKES( ExecCommandSMST, EStLength1 )
  1245 		SMF_INVOKES( ExecCommandSMST, EStLength1 )
  1117 
  1246 
  1118 	SMF_STATE(EStLength1)
  1247 	SMF_STATE(EStLength1)
  1119 
  1248 
       
  1249         OstTrace0( TRACE_INTERNALS, DSDSTACK_CIMREADWRITEBLOCKSSM5, "EStLength1" );
  1120 		const TMMCStatus status(s.ResponseP());
  1250 		const TMMCStatus status(s.ResponseP());
  1121 		s.PopCommandStack();
  1251 		s.PopCommandStack();
  1122 		if (status.Error())
  1252 		if (status.Error())
       
  1253 		    {
       
  1254 		    OstTraceFunctionExitExt( DSDSTACK_CIMREADWRITEBLOCKSSM_EXIT3, this, (TInt) KMMCErrStatus );
  1123 			SMF_RETURN(KMMCErrStatus)
  1255 			SMF_RETURN(KMMCErrStatus)
       
  1256 		    }
  1124 		s.iCardP->iSetBlockLen = s.Command().BlockLength();
  1257 		s.iCardP->iSetBlockLen = s.Command().BlockLength();
  1125 
  1258 
  1126 	SMF_STATE(EStLengthSet)
  1259 	SMF_STATE(EStLengthSet)
  1127 
  1260 
       
  1261         OstTrace0( TRACE_INTERNALS, DSDSTACK_CIMREADWRITEBLOCKSSM6, "EStLengthSet" );
  1128 		TMMCCommandDesc& cmd = s.Command();
  1262 		TMMCCommandDesc& cmd = s.Command();
  1129 		TUint opType = 0;
  1263 		TUint opType = 0;
  1130 		const TUint kTypeWrite =	KBit0;
  1264 		const TUint kTypeWrite =	KBit0;
  1131 		const TUint kTypeMultiple =	KBit1;
  1265 		const TUint kTypeMultiple =	KBit1;
  1132 		const TUint kTypeSpecial =	KBit2;
  1266 		const TUint kTypeSpecial =	KBit2;
  1152 				break;
  1286 				break;
  1153 			}
  1287 			}
  1154 
  1288 
  1155 		const TUint blocks = cmd.iTotalLength / cmd.BlockLength();
  1289 		const TUint blocks = cmd.iTotalLength / cmd.BlockLength();
  1156 		if ( blocks * cmd.BlockLength() != cmd.iTotalLength )
  1290 		if ( blocks * cmd.BlockLength() != cmd.iTotalLength )
  1157 			return( KMMCErrArgument );
  1291 		    {
       
  1292 			OstTraceFunctionExitExt( DSDSTACK_CIMREADWRITEBLOCKSSM_EXIT4, this, (TInt) KMMCErrArgument );
       
  1293 			return KMMCErrArgument;
       
  1294 		    }
  1158 
  1295 
  1159 		if ( !(opType & kTypeSpecial) )	// A special session has already set its command descriptor
  1296 		if ( !(opType & kTypeSpecial) )	// A special session has already set its command descriptor
  1160 			{
  1297 			{
  1161 			if (blocks==1)
  1298 			if (blocks==1)
  1162 				opType &= ~kTypeMultiple;
  1299 				opType &= ~kTypeMultiple;
  1181 
  1318 
  1182 		SMF_INVOKES( ExecCommandSMST, EStIssued )
  1319 		SMF_INVOKES( ExecCommandSMST, EStIssued )
  1183 
  1320 
  1184 	SMF_STATE(EStIssued)
  1321 	SMF_STATE(EStIssued)
  1185 
  1322 
       
  1323         OstTrace0( TRACE_INTERNALS, DSDSTACK_CIMREADWRITEBLOCKSSM7, "EStIssued" );
  1186 		// check state of card after data transfer with CMD13.
  1324 		// check state of card after data transfer with CMD13.
  1187 		if (s.Command().Direction() != 0)
  1325 		if (s.Command().Direction() != 0)
  1188 			{
  1326 			{
  1189 			SMF_GOTOS(EStWaitFinish)
  1327 			SMF_GOTOS(EStWaitFinish)
  1190 			}
  1328 			}
  1191 
  1329 
  1192 		SMF_GOTOS(EStRWFinish);
  1330 		SMF_GOTOS(EStRWFinish);
  1193 
  1331 
  1194 	SMF_STATE(EStWaitFinish)
  1332 	SMF_STATE(EStWaitFinish)
       
  1333         OstTrace0( TRACE_INTERNALS, DSDSTACK_CIMREADWRITEBLOCKSSM8, "EStWaitFinish" );
  1195 		// if MBW fail, then recover by rewriting ALL blocks...
  1334 		// if MBW fail, then recover by rewriting ALL blocks...
  1196 		// (used to recover using ACMD22, but this has been changed
  1335 		// (used to recover using ACMD22, but this has been changed
  1197 		// as is difficult to test for little gain in efficiency)
  1336 		// as is difficult to test for little gain in efficiency)
  1198 		if (Command().iCommand == ECmdWriteMultipleBlock && err != 0)
  1337 		if (Command().iCommand == ECmdWriteMultipleBlock && err != 0)
  1199 			{
  1338 			{
  1200 			if (Command().iCustomRetries++ >= (TInt) KSDMaxMBWRetries)
  1339 			if (Command().iCustomRetries++ >= (TInt) KSDMaxMBWRetries)
  1201 				{
  1340 				{
       
  1341 				OstTraceFunctionExitExt( DSDSTACK_CIMREADWRITEBLOCKSSM_EXIT5, this, (TInt) err );
  1202 				SMF_RETURN(err)
  1342 				SMF_RETURN(err)
  1203 				}
  1343 				}
  1204 
  1344 
  1205 			m.Pop();		// remove recursive call to EStRestart
  1345 			m.Pop();		// remove recursive call to EStRestart
  1206 			SMF_GOTOS(EStRestart)			
  1346 			SMF_GOTOS(EStRestart)			
  1216 		s.FillCommandDesc(ECmdSendStatus, 0);
  1356 		s.FillCommandDesc(ECmdSendStatus, 0);
  1217 		SMF_INVOKES(ExecCommandSMST, EStWaitFinish1)
  1357 		SMF_INVOKES(ExecCommandSMST, EStWaitFinish1)
  1218 
  1358 
  1219 	SMF_STATE(EStWaitFinish1)
  1359 	SMF_STATE(EStWaitFinish1)
  1220 
  1360 
       
  1361         OstTrace0( TRACE_INTERNALS, DSDSTACK_CIMREADWRITEBLOCKSSM9, "EStWaitFinish1" );
  1221 		const TMMCStatus status(s.ResponseP());
  1362 		const TMMCStatus status(s.ResponseP());
  1222 		s.PopCommandStack();
  1363 		s.PopCommandStack();
  1223 
  1364 
  1224 #ifdef __WINS__
  1365 #ifdef __WINS__
  1225 		SMF_GOTOS(EStRWFinish);
  1366 		SMF_GOTOS(EStRWFinish);
  1230 			{
  1371 			{
  1231 			SMF_INVOKES(ProgramTimerSMST, EStWaitFinish);
  1372 			SMF_INVOKES(ProgramTimerSMST, EStWaitFinish);
  1232 			}
  1373 			}
  1233 
  1374 
  1234 		if (status.Error())
  1375 		if (status.Error())
       
  1376 		    {
       
  1377 			OstTraceFunctionExitExt( DUP7_DSDSTACK_CIMREADWRITEBLOCKSSM_EXIT, this, (TInt) KMMCErrStatus );
  1235 			SMF_RETURN(KMMCErrStatus)
  1378 			SMF_RETURN(KMMCErrStatus)
       
  1379 		    }
  1236 #endif
  1380 #endif
  1237 		
  1381 		
  1238 		// Fall through if CURRENT_STATE is not PGM or DATA
  1382 		// Fall through if CURRENT_STATE is not PGM or DATA
  1239 	SMF_STATE(EStRWFinish)
  1383 	SMF_STATE(EStRWFinish)
  1240 
  1384 
       
  1385         OstTrace0( TRACE_INTERNALS, DSDSTACK_CIMREADWRITEBLOCKSSM10, "EStRWFinish" );
  1241 		if (TMMCStatus(s.ResponseP()).Error() != 0)
  1386 		if (TMMCStatus(s.ResponseP()).Error() != 0)
       
  1387 		    {
       
  1388 		    OstTraceFunctionExitExt( DSDSTACK_CIMREADWRITEBLOCKSSM_EXIT6, this, (TInt) KMMCErrStatus );
  1242 			SMF_RETURN(KMMCErrStatus);
  1389 			SMF_RETURN(KMMCErrStatus);
       
  1390 		    }
  1243 
  1391 
  1244 		s.iState &= ~KMMCSessStateInProgress;
  1392 		s.iState &= ~KMMCSessStateInProgress;
  1245 
  1393 
  1246 		// skip over recursive entry or throw error and catch in CIMLockUnlockSM()
  1394 		// skip over recursive entry or throw error and catch in CIMLockUnlockSM()
  1247 		return (s.Command().iCommand == ECmdLockUnlock) ? KMMCErrUpdPswd : KMMCErrBypass;
  1395 		TMMCErr ret = (s.Command().iCommand == ECmdLockUnlock) ? KMMCErrUpdPswd : KMMCErrBypass; 
       
  1396 		OstTraceFunctionExitExt( DSDSTACK_CIMREADWRITEBLOCKSSM_EXIT7, this, (TInt) ret );
       
  1397 		return ret;
  1248 
  1398 
  1249 	SMF_STATE(EStDone)
  1399 	SMF_STATE(EStDone)
  1250 	    
  1400 	    
       
  1401         OstTrace0( TRACE_INTERNALS, DSDSTACK_CIMREADWRITEBLOCKSSM11, "EStDone" );
  1251 	    __KTRACE_OPT(KPBUS1,Kern::Printf("<SD:RWBlocksSM()"));
  1402 	    __KTRACE_OPT(KPBUS1,Kern::Printf("<SD:RWBlocksSM()"));
  1252 
  1403 
  1253 	SMF_END
  1404 	SMF_END
  1254 	}
  1405 	}
  1255 
  1406 
  1270 
  1421 
  1271 	//coverity[unreachable]
  1422 	//coverity[unreachable]
  1272 	//Part of state machine design.
  1423 	//Part of state machine design.
  1273 	SMF_BEGIN
  1424 	SMF_BEGIN
  1274 
  1425 
       
  1426         OstTrace0( TRACE_INTERNALS, DSDSTACK_MODIFYCARDCAPABILITYSM, "EStBegin" );
  1275     	SMF_INVOKES( DMMCStack::BaseModifyCardCapabilitySMST, EStDone )
  1427     	SMF_INVOKES( DMMCStack::BaseModifyCardCapabilitySMST, EStDone )
  1276 
  1428 
  1277     SMF_STATE(EStDone)
  1429     SMF_STATE(EStDone)
       
  1430     
       
  1431         OstTrace0( TRACE_INTERNALS, DSDSTACK_MODIFYCARDCAPABILITYSM1, "EStDone" );
  1278 
  1432 
  1279     SMF_END
  1433     SMF_END
  1280 	}
  1434 	}
  1281 
  1435 
  1282 inline TMMCErr DSDStack::SwitchToHighSpeedModeSMST( TAny* aStackP )
  1436 inline TMMCErr DSDStack::SwitchToHighSpeedModeSMST( TAny* aStackP )
  1298 			};
  1452 			};
  1299 
  1453 
  1300 		__KTRACE_OPT(KPBUS1,Kern::Printf(">SD:SwitchToHighSpeedModeSM "));
  1454 		__KTRACE_OPT(KPBUS1,Kern::Printf(">SD:SwitchToHighSpeedModeSM "));
  1301 
  1455 
  1302 		DMMCSession& s = Session();
  1456 		DMMCSession& s = Session();
       
  1457 		OstTrace1( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM, "Current session = 0x%x", &s );
  1303 
  1458 
  1304 	SMF_BEGIN
  1459 	SMF_BEGIN
  1305 
  1460 
       
  1461         OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM1, "EStBegin");
       
  1462 	
  1306 	SMF_STATE(EstCheckController) 	
  1463 	SMF_STATE(EstCheckController) 	
       
  1464         OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM2, "EstCheckController");
  1307 	  	// Get the clock speed supported by the controller
  1465 	  	// Get the clock speed supported by the controller
  1308 		TMMCMachineInfoV4 machineInfo;
  1466 		TMMCMachineInfoV4 machineInfo;
  1309 		TMMCMachineInfoV4Pckg machineInfoPckg(machineInfo);
  1467 		TMMCMachineInfoV4Pckg machineInfoPckg(machineInfo);
  1310 		MachineInfo(machineInfoPckg);
  1468 		MachineInfo(machineInfoPckg);
  1311 		
  1469 		
  1312 		if (machineInfo.iVersion >= TMMCMachineInfoV4::EVersion4)
  1470 		if (machineInfo.iVersion >= TMMCMachineInfoV4::EVersion4)
  1313 			{
  1471 			{
  1314 			if (machineInfo.iMaxClockSpeedInMhz < (KSDDTClk50MHz/1000) )
  1472 			if (machineInfo.iMaxClockSpeedInMhz < (KSDDTClk50MHz/1000) )
  1315 				{
  1473 				{
  1316 				__KTRACE_OPT(KPBUS1, Kern::Printf("High speed mode not supported by controller"));
  1474 				__KTRACE_OPT(KPBUS1, Kern::Printf("High speed mode not supported by controller"));
       
  1475 				OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM3, "High speed mode not supported by controller");
  1317 				SMF_GOTOS(EStDone);
  1476 				SMF_GOTOS(EStDone);
  1318 				}
  1477 				}
  1319 			}	
  1478 			}	
  1320 
  1479 
  1321 	SMF_STATE(EStSendSCRCmd)
  1480 	SMF_STATE(EStSendSCRCmd)
       
  1481         OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM4, "EStSendSCRCmd");
  1322 		//
  1482 		//
  1323       	// ACMD51 Read the SD Configuration Register
  1483       	// ACMD51 Read the SD Configuration Register
  1324       	//
  1484       	//
  1325 		DSDSession::FillAppCommandDesc(Command(), ESDACmdSendSCR);
  1485 		DSDSession::FillAppCommandDesc(Command(), ESDACmdSendSCR);
  1326       	s.FillCommandArgs(0, KSDSCRLength, iPSLBuf, KSDSCRLength);
  1486       	s.FillCommandArgs(0, KSDSCRLength, iPSLBuf, KSDSCRLength);
  1327       	SMF_INVOKES(ExecCommandSMST, EStCheckSpecVer);
  1487       	SMF_INVOKES(ExecCommandSMST, EStCheckSpecVer);
  1328 
  1488 
  1329 	SMF_STATE(EStCheckSpecVer)
  1489 	SMF_STATE(EStCheckSpecVer)
       
  1490         OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM5, "EStCheckSpecVer");
  1330       	//	
  1491       	//	
  1331       	// Check the SD version
  1492       	// Check the SD version
  1332 		//
  1493 		//
  1333       	// 0 : version 1.0-1.01	: SDHS Is NOT Supported
  1494       	// 0 : version 1.0-1.01	: SDHS Is NOT Supported
  1334       	// 1 : version 1.10+	: SDHS Is Supported
  1495       	// 1 : version 1.10+	: SDHS Is Supported
  1335 		//
  1496 		//
  1336       	__KTRACE_OPT(KPBUS1,Kern::Printf("   SD Configuration Register received"));
  1497       	__KTRACE_OPT(KPBUS1,Kern::Printf("   SD Configuration Register received"));
  1337       	__KTRACE_OPT(KPBUS1,Kern::Printf("   ...card_status=%x", TUint(s.iLastStatus)));
  1498       	__KTRACE_OPT(KPBUS1,Kern::Printf("   ...card_status=%x", TUint(s.iLastStatus)));
       
  1499       	OstTrace1( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM6, "SD Configuration Register received: card_status=0x%x", (TUint) s.iLastStatus);
  1338 
  1500 
  1339 #ifdef _DEBUG
  1501 #ifdef _DEBUG
  1340       	for (TUint32 i = 0; i < KSDSCRLength; ++i)
  1502       	for (TUint32 i = 0; i < KSDSCRLength; ++i)
  1341 			{
  1503 			{
  1342 			__KTRACE_OPT(KPBUS1, Kern::Printf("   ...SCR_STATUS[0x%x] = %x", i, iPSLBuf[i]));
  1504 			__KTRACE_OPT(KPBUS1, Kern::Printf("   ...SCR_STATUS[0x%x] = %x", i, iPSLBuf[i]));
  1344 #endif
  1506 #endif
  1345 
  1507 
  1346       	if(iPSLBuf[0]==2)
  1508       	if(iPSLBuf[0]==2)
  1347 			{
  1509 			{
  1348 			__KTRACE_OPT(KPBUS1,Kern::Printf("   ...SD Spec Version 2"));
  1510 			__KTRACE_OPT(KPBUS1,Kern::Printf("   ...SD Spec Version 2"));
       
  1511 			OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM7, "SD Spec Version 2");
  1349 			SMF_GOTOS(EStCheckFunction);
  1512 			SMF_GOTOS(EStCheckFunction);
  1350 			}
  1513 			}
  1351   
  1514   
  1352       	if(iPSLBuf[0]==1)
  1515       	if(iPSLBuf[0]==1)
  1353 			{
  1516 			{
  1354 			__KTRACE_OPT(KPBUS1,Kern::Printf("   ...SD Spec Version 1.10"));
  1517 			__KTRACE_OPT(KPBUS1,Kern::Printf("   ...SD Spec Version 1.10"));
       
  1518 			OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM8, "SD Spec Version 1.10");
  1355 			SMF_GOTOS(EStCheckFunction);
  1519 			SMF_GOTOS(EStCheckFunction);
  1356 			}
  1520 			}
  1357   
  1521   
  1358       	if(iPSLBuf[0]==0)
  1522       	if(iPSLBuf[0]==0)
  1359 			{
  1523 			{
  1360 			__KTRACE_OPT(KPBUS1,Kern::Printf("   ...SD Spec Version 1.01"));
  1524 			__KTRACE_OPT(KPBUS1,Kern::Printf("   ...SD Spec Version 1.01"));
       
  1525 			OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM9, "SD Spec Version 1.01");
  1361 			SMF_GOTOS(EStDone);
  1526 			SMF_GOTOS(EStDone);
  1362         	}
  1527         	}
  1363 
  1528 
  1364 	__KTRACE_OPT(KPBUS1,Kern::Printf("   ...SD Spec Version > 2 !"));
  1529 	__KTRACE_OPT(KPBUS1,Kern::Printf("   ...SD Spec Version > 2 !"));
       
  1530 	OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM10, "SD Spec Version > 2");
  1365 
  1531 
  1366 	SMF_STATE(EStCheckFunction)
  1532 	SMF_STATE(EStCheckFunction)
  1367 
  1533 
       
  1534         OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM11, "EStCheckFunction");
  1368 		m.SetTraps(KMMCErrResponseTimeOut | KMMCErrNotSupported);
  1535 		m.SetTraps(KMMCErrResponseTimeOut | KMMCErrNotSupported);
  1369 
  1536 
  1370  		//
  1537  		//
  1371 		// SD1.1 uses CMD6 which is not defined by the MMCA
  1538 		// SD1.1 uses CMD6 which is not defined by the MMCA
  1372 		//  - fill in command details using the SD Specific command description table
  1539 		//  - fill in command details using the SD Specific command description table
  1377 
  1544 
  1378 		SMF_INVOKES(IssueCommandCheckResponseSMST,EStCheckFunctionSent)
  1545 		SMF_INVOKES(IssueCommandCheckResponseSMST,EStCheckFunctionSent)
  1379 
  1546 
  1380 	SMF_STATE(EStCheckFunctionSent)
  1547 	SMF_STATE(EStCheckFunctionSent)
  1381  
  1548  
       
  1549         OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM12, "EStCheckFunctionSent");
  1382        	__KTRACE_OPT(KPBUS1,Kern::Printf("   CheckFunctionSent %x",TUint(s.iLastStatus)));
  1550        	__KTRACE_OPT(KPBUS1,Kern::Printf("   CheckFunctionSent %x",TUint(s.iLastStatus)));
       
  1551        	OstTrace1( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM13, "CheckFunctionSent=0x%x", (TUint) s.iLastStatus);
  1383 
  1552 
  1384 		m.ResetTraps();
  1553 		m.ResetTraps();
  1385 
  1554 
  1386 		if(err == KMMCErrResponseTimeOut)
  1555 		if(err == KMMCErrResponseTimeOut)
  1387 			{
  1556 			{
  1388 	       	__KTRACE_OPT(KPBUS1,Kern::Printf("   ...CMD6 [Read] Response Timeout"));
  1557 	       	__KTRACE_OPT(KPBUS1,Kern::Printf("   ...CMD6 [Read] Response Timeout"));
       
  1558 	       	OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM14, "CMD6 [Read] Response Timeout");
  1389 			SMF_GOTOS(EStDone);
  1559 			SMF_GOTOS(EStDone);
  1390 			}
  1560 			}
  1391 		else if(err == KMMCErrNotSupported)
  1561 		else if(err == KMMCErrNotSupported)
  1392 			{
  1562 			{
  1393 	       	__KTRACE_OPT(KPBUS1,Kern::Printf("   ...CMD6 [Read] Not Supported"));
  1563 	       	__KTRACE_OPT(KPBUS1,Kern::Printf("   ...CMD6 [Read] Not Supported"));
       
  1564 	       	OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM15, "CMD6 [Read] Not Supported");
  1394 			SMF_GOTOS(EStDone);
  1565 			SMF_GOTOS(EStDone);
  1395 			}
  1566 			}
  1396 
  1567 
  1397 #ifdef _DEBUG
  1568 #ifdef _DEBUG
  1398 		for (TUint32 i = 0; i < KSDSwitchFuncLength; ++i)
  1569 		for (TUint32 i = 0; i < KSDSwitchFuncLength; ++i)
  1413 
  1584 
  1414       	SMF_INVOKES(IssueCommandCheckResponseSMST,EStSwitchFunctionSent)
  1585       	SMF_INVOKES(IssueCommandCheckResponseSMST,EStSwitchFunctionSent)
  1415 	
  1586 	
  1416 	SMF_STATE(EStSwitchFunctionSent)
  1587 	SMF_STATE(EStSwitchFunctionSent)
  1417 
  1588 
       
  1589         OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM16, "EStSwitchFunctionSent");
  1418 #ifdef _DEBUG
  1590 #ifdef _DEBUG
  1419 		m.ResetTraps();
  1591 		m.ResetTraps();
  1420 
  1592 
  1421 		if(err == KMMCErrResponseTimeOut)
  1593 		if(err == KMMCErrResponseTimeOut)
  1422 			{
  1594 			{
  1423 	       	__KTRACE_OPT(KPBUS1,Kern::Printf("   ...CMD6 [Write] Response Timeout"));
  1595 	       	__KTRACE_OPT(KPBUS1,Kern::Printf("   ...CMD6 [Write] Response Timeout"));
       
  1596 	       	OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM17, "CMD6 [Write] Response Timeout");
  1424 			}
  1597 			}
  1425 
  1598 
  1426 		for (TUint32 i = 0; i < KSDSwitchFuncLength; ++i)
  1599 		for (TUint32 i = 0; i < KSDSwitchFuncLength; ++i)
  1427 			{
  1600 			{
  1428 	  		__KTRACE_OPT(KPBUS1, Kern::Printf("   ...SD Switch[0x%x] = %x", i, iPSLBuf[i]));
  1601 	  		__KTRACE_OPT(KPBUS1, Kern::Printf("   ...SD Switch[0x%x] = %x", i, iPSLBuf[i]));
       
  1602 	  		OstTraceExt2( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM18, "SD Switch[0x%x]=0x%x", (TUint) i, (TUint) iPSLBuf[i]);
  1429 			}
  1603 			}
  1430 #endif
  1604 #endif
  1431 
  1605 
  1432 	SMF_STATE(EStDone)
  1606 	SMF_STATE(EStDone)
       
  1607 	
       
  1608         OstTrace0( TRACE_INTERNALS, DSDSTACK_SWITCHTOHIGHSPEEDMODESM19, "EStSwitchFunctionSent");
  1433       
  1609       
  1434 	SMF_END
  1610 	SMF_END
  1435 	}
  1611 	}
  1436 
  1612 
  1437 
  1613 
  1441 * controllers can override this to generate more specific objects.
  1617 * controllers can override this to generate more specific objects.
  1442 * @param aCallBack Callback function to notify the client that a session has completed
  1618 * @param aCallBack Callback function to notify the client that a session has completed
  1443 * @return A pointer to the new session
  1619 * @return A pointer to the new session
  1444 */
  1620 */
  1445 	{
  1621 	{
       
  1622 	OstTraceFunctionEntry1( DSDSTACK_ALLOCSESSION_ENTRY, this );
  1446 	return new DSDSession(aCallBack);
  1623 	return new DSDSession(aCallBack);
  1447 	}
  1624 	}
  1448 
  1625 
  1449 EXPORT_C void DSDStack::Dummy1() {}
  1626 EXPORT_C void DSDStack::Dummy1() {}
  1450 EXPORT_C void DSDStack::Dummy2() {}
  1627 EXPORT_C void DSDStack::Dummy2() {}