kernel/eka/drivers/medmmc/medmmc.cpp
changeset 13 46fffbe7b5a7
parent 10 36bfc973b146
child 28 5b5d147c7838
equal deleted inserted replaced
12:0bf4040442f9 13:46fffbe7b5a7
    16 //
    16 //
    17 
    17 
    18 #include "mmc.h" 
    18 #include "mmc.h" 
    19 #include "pbusmedia.h"
    19 #include "pbusmedia.h"
    20 #include <drivers/emmcptn.h>
    20 #include <drivers/emmcptn.h>
       
    21 
       
    22 #include "OstTraceDefinitions.h"
       
    23 #ifdef OST_TRACE_COMPILER_IN_USE
       
    24 #include "locmedia_ost.h"
       
    25 #ifdef __VC32__
       
    26 #pragma warning(disable: 4127) // disabling warning "conditional expression is constant"
       
    27 #endif
       
    28 #include "medmmcTraces.h"
       
    29 #endif
    21 
    30 
    22 #if defined(__DEMAND_PAGING__)
    31 #if defined(__DEMAND_PAGING__)
    23 	// If in debug mode, enable paging stats and their retrieval using DLocalDrive::EControlIO
    32 	// If in debug mode, enable paging stats and their retrieval using DLocalDrive::EControlIO
    24 	#if defined( _DEBUG)
    33 	#if defined( _DEBUG)
    25 		#define __TEST_PAGING_MEDIA_DRIVER__
    34 		#define __TEST_PAGING_MEDIA_DRIVER__
   315 // ======== DPhysicalDeviceMediaMmcFlash ========
   324 // ======== DPhysicalDeviceMediaMmcFlash ========
   316 
   325 
   317 
   326 
   318 DPhysicalDeviceMediaMmcFlash::DPhysicalDeviceMediaMmcFlash()
   327 DPhysicalDeviceMediaMmcFlash::DPhysicalDeviceMediaMmcFlash()
   319 	{
   328 	{
       
   329 	OstTraceFunctionEntry1( DPHYSICALDEVICEMEDIAMMCFLASH_DPHYSICALDEVICEMEDIAMMCFLASH_ENTRY, this );
   320 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:ctr"));
   330 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:ctr"));
   321 
   331 
   322 	iUnitsMask = 0x01;
   332 	iUnitsMask = 0x01;
   323 	iVersion = TVersion(KMediaDriverInterfaceMajorVersion,KMediaDriverInterfaceMinorVersion,KMediaDriverInterfaceBuildVersion);
   333 	iVersion = TVersion(KMediaDriverInterfaceMajorVersion,KMediaDriverInterfaceMinorVersion,KMediaDriverInterfaceBuildVersion);
       
   334 	OstTraceFunctionExit1( DPHYSICALDEVICEMEDIAMMCFLASH_DPHYSICALDEVICEMEDIAMMCFLASH_EXIT, this );
   324 	}
   335 	}
   325 
   336 
   326 
   337 
   327 TInt DPhysicalDeviceMediaMmcFlash::Install()
   338 TInt DPhysicalDeviceMediaMmcFlash::Install()
   328 	{
   339 	{
       
   340 	OstTraceFunctionEntry1( DPHYSICALDEVICEMEDIAMMCFLASH_INSTALL_ENTRY, this );
   329 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:ins"));
   341 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:ins"));
   330 
   342 
   331 	_LIT(KDrvNm, "Media.MmcF");
   343 	_LIT(KDrvNm, "Media.MmcF");
   332 	return SetName(&KDrvNm);
   344 	TInt r = SetName(&KDrvNm);
       
   345 	OstTraceFunctionExitExt( DPHYSICALDEVICEMEDIAMMCFLASH_INSTALL_EXIT, this, r );
       
   346 	return r;
   333 	}
   347 	}
   334 
   348 
   335 
   349 
   336 void DPhysicalDeviceMediaMmcFlash::GetCaps(TDes8& /* aDes */) const
   350 void DPhysicalDeviceMediaMmcFlash::GetCaps(TDes8& /* aDes */) const
   337 	{
   351 	{
   342 TInt DPhysicalDeviceMediaMmcFlash::Info(TInt aFunction, TAny* /*a1*/)
   356 TInt DPhysicalDeviceMediaMmcFlash::Info(TInt aFunction, TAny* /*a1*/)
   343 //
   357 //
   344 // Return the priority of this media driver
   358 // Return the priority of this media driver
   345 //
   359 //
   346 	{
   360 	{
       
   361 	OstTraceExt2(TRACE_FLOW, DPHYSICALDEVICEMEDIAMMCFLASH_INFO_ENTRY ,"DPhysicalDeviceMediaMmcFlash::Info;aFunction=%d;this=%x", aFunction, (TUint) this);
   347 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:info"));
   362 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:info"));
   348 	if (aFunction==EPriority)
   363 	if (aFunction==EPriority)
       
   364 	    {
       
   365 		OstTraceFunctionExitExt( DPHYSICALDEVICEMEDIAMMCFLASH_INFO_EXIT1, this, KMediaDriverPriorityNormal );
   349 		return KMediaDriverPriorityNormal;
   366 		return KMediaDriverPriorityNormal;
       
   367 	    }
   350 	// Don't close media driver when peripheral bus powers down. This avoids the need for Caps() to power up the stack.
   368 	// Don't close media driver when peripheral bus powers down. This avoids the need for Caps() to power up the stack.
   351 	if (aFunction==EMediaDriverPersistent)
   369 	if (aFunction==EMediaDriverPersistent)
       
   370 	    {
       
   371 		OstTraceFunctionExitExt( DPHYSICALDEVICEMEDIAMMCFLASH_INFO_EXIT2, this, KErrNone );
   352 		return KErrNone;
   372 		return KErrNone;
       
   373 	    }
       
   374 	
       
   375 	OstTraceFunctionExitExt( DPHYSICALDEVICEMEDIAMMCFLASH_INFO_EXIT3, this, KErrNotSupported );
   353 	return KErrNotSupported;
   376 	return KErrNotSupported;
   354 	}
   377 	}
   355 								 
   378 								 
   356 TInt DPhysicalDeviceMediaMmcFlash::Validate(TInt aDeviceType, const TDesC8* /*aInfo*/, const TVersion& aVer)
   379 TInt DPhysicalDeviceMediaMmcFlash::Validate(TInt aDeviceType, const TDesC8* /*aInfo*/, const TVersion& aVer)
   357 	{
   380 	{
       
   381 	OstTraceExt2(TRACE_FLOW, DPHYSICALDEVICEMEDIAMMCFLASH_VALIDATE_ENTRY ,"DPhysicalDeviceMediaMmcFlash::Validate;aDeviceType=%d;this=%x", aDeviceType, (TUint) this);
   358 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:validate aDeviceType %d", aDeviceType));
   382 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:validate aDeviceType %d", aDeviceType));
   359 	if (!Kern::QueryVersionSupported(iVersion,aVer))
   383 	if (!Kern::QueryVersionSupported(iVersion,aVer))
       
   384 	    {
       
   385 		OstTraceFunctionExitExt( DPHYSICALDEVICEMEDIAMMCFLASH_VALIDATE_EXIT1, this, KErrNotSupported );
   360 		return KErrNotSupported;
   386 		return KErrNotSupported;
       
   387 	    }
   361 	if (aDeviceType!=MEDIA_DEVICE_MMC)
   388 	if (aDeviceType!=MEDIA_DEVICE_MMC)
       
   389 	    {
       
   390 		OstTraceFunctionExitExt( DPHYSICALDEVICEMEDIAMMCFLASH_VALIDATE_EXIT2, this, KErrNotSupported );
   362 		return KErrNotSupported;
   391 		return KErrNotSupported;
       
   392 	    }
       
   393 
       
   394 	OstTraceFunctionExitExt( DPHYSICALDEVICEMEDIAMMCFLASH_VALIDATE_EXIT3, this, KErrNone );
   363 	return KErrNone;
   395 	return KErrNone;
   364 	}
   396 	}
   365 								 
   397 								 
   366 TInt DPhysicalDeviceMediaMmcFlash::Create(DBase*& aChannel, TInt aMediaId, const TDesC8* /*aInfo*/, const TVersion& aVer)
   398 TInt DPhysicalDeviceMediaMmcFlash::Create(DBase*& aChannel, TInt aMediaId, const TDesC8* /*aInfo*/, const TVersion& aVer)
   367 //
   399 //
   368 // Create an MMC Card media driver.
   400 // Create an MMC Card media driver.
   369 //
   401 //
   370 	{
   402 	{
       
   403 	OstTraceExt2(TRACE_FLOW, DPHYSICALDEVICEMEDIAMMCFLASH_CREATE_ENTRY, "DPhysicalDeviceMediaMmcFlash::Create;aMediaId=%d;this=%x", aMediaId, (TUint) this);
   371 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:crt"));
   404 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:crt"));
   372 
   405 
   373 	if (!Kern::QueryVersionSupported(iVersion,aVer))
   406 	if (!Kern::QueryVersionSupported(iVersion,aVer))
       
   407 	    {
       
   408 		OstTraceFunctionExitExt( DPHYSICALDEVICEMEDIAMMCFLASH_CREATE_EXIT1, this, KErrNotSupported );
   374 		return KErrNotSupported;
   409 		return KErrNotSupported;
       
   410 	    }
   375 
   411 
   376 	DMmcMediaDriverFlash* pD = new DMmcMediaDriverFlash(aMediaId);
   412 	DMmcMediaDriverFlash* pD = new DMmcMediaDriverFlash(aMediaId);
   377 	aChannel=pD;
   413 	aChannel=pD;
   378 
   414 
   379 	TInt r=KErrNoMemory;
   415 	TInt r=KErrNoMemory;
   381 		r=pD->DoCreate(aMediaId);
   417 		r=pD->DoCreate(aMediaId);
   382 	if (r==KErrNone)
   418 	if (r==KErrNone)
   383 		pD->OpenMediaDriverComplete(KErrNone);
   419 		pD->OpenMediaDriverComplete(KErrNone);
   384 
   420 
   385 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:mdf"));
   421 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:mdf"));
       
   422 
       
   423 	OstTraceFunctionExitExt( DPHYSICALDEVICEMEDIAMMCFLASH_CREATE_EXIT2, this, r );
   386 	return r;
   424 	return r;
   387 	}
   425 	}
   388 
   426 
   389 
   427 
   390 // ======== DMmcMediaDriverFlash ========
   428 // ======== DMmcMediaDriverFlash ========
   429 	iDataTransferCallBackDfc(DMmcMediaDriverFlash::DataTransferCallBackDfc, this, 1)
   467 	iDataTransferCallBackDfc(DMmcMediaDriverFlash::DataTransferCallBackDfc, this, 1)
   430 	{
   468 	{
   431 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:mmd"));
   469 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:mmd"));
   432 	// NB aMedia Id = the media ID of the primary media, iMediaId = the media ID of this media
   470 	// NB aMedia Id = the media ID of the primary media, iMediaId = the media ID of this media
   433 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("DMmcMediaDriverFlash(), iMediaId %d, aMediaId %d\n", iMediaId, aMediaId));
   471 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("DMmcMediaDriverFlash(), iMediaId %d, aMediaId %d\n", iMediaId, aMediaId));
       
   472 	OstTraceExt2( TRACE_FLOW, DMMCMEDIADRIVERFLASH_DMMCMEDIADRIVERFLASH, "> DMmcMediaDriverFlash::DMmcMediaDriverFlash;aMediaId=%d;iMediaId=%d", (TInt) aMediaId, (TInt) iMediaId );
       
   473 	
   434 	}
   474 	}
   435 
   475 
   436 #pragma warning( default : 4355 )
   476 #pragma warning( default : 4355 )
   437 TInt DMmcMediaDriverFlash::DoCreate(TInt /*aMediaId*/)
   477 TInt DMmcMediaDriverFlash::DoCreate(TInt /*aMediaId*/)
   438 	{
   478 	{
       
   479 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_DOCREATE_ENTRY, this );
   439 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:opn"));
   480 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:opn"));
   440 
   481 
   441 	iSocket = ((DMMCSocket*)((DPBusPrimaryMedia*)iPrimaryMedia)->iSocket);
   482 	iSocket = ((DMMCSocket*)((DPBusPrimaryMedia*)iPrimaryMedia)->iSocket);
   442 	if(iSocket == NULL)
   483 	if(iSocket == NULL)
   443 		return(KErrNoMemory);
   484 	    {
       
   485 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOCREATE_EXIT1, this, KErrNoMemory );
       
   486 		return KErrNoMemory;
       
   487 	    }
   444 
   488 
   445 	iCardNumber = ((DPBusPrimaryMedia*)iPrimaryMedia)->iSlotNumber;
   489 	iCardNumber = ((DPBusPrimaryMedia*)iPrimaryMedia)->iSlotNumber;
   446 
   490 
   447 	iStack = iSocket->Stack(KStackNumber);
   491 	iStack = iSocket->Stack(KStackNumber);
   448 	iCard = iStack->CardP(CardNum());
   492 	iCard = iStack->CardP(CardNum());
   451 	Stack().MachineInfo(machineInfo);
   495 	Stack().MachineInfo(machineInfo);
   452 	TInt slotFlag = iCardNumber == 0 ? TMMCMachineInfo::ESlot1Internal : TMMCMachineInfo::ESlot2Internal;
   496 	TInt slotFlag = iCardNumber == 0 ? TMMCMachineInfo::ESlot1Internal : TMMCMachineInfo::ESlot2Internal;
   453 	iInternalSlot = machineInfo.iFlags & slotFlag;
   497 	iInternalSlot = machineInfo.iFlags & slotFlag;
   454 
   498 
   455 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("DMmcMediaDriverFlash::DoCreate() slotNumber %d iInternalSlot %d", iCardNumber, iInternalSlot));
   499 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("DMmcMediaDriverFlash::DoCreate() slotNumber %d iInternalSlot %d", iCardNumber, iInternalSlot));
   456 
   500 	OstTraceExt2(TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DOCREATE_SLOT, "slotNumber=%d; iInternalSlot=%d", iCardNumber, iInternalSlot);
   457 
   501 
   458 	iSessionEndDfc.SetDfcQ(&iSocket->iDfcQ);
   502 	iSessionEndDfc.SetDfcQ(&iSocket->iDfcQ);
   459 	iDataTransferCallBackDfc.SetDfcQ(&iSocket->iDfcQ);
   503 	iDataTransferCallBackDfc.SetDfcQ(&iSocket->iDfcQ);
   460 
   504 
   461 	// check right type of card
   505 	// check right type of card
   462 	if ((iMediaType=iCard->MediaType())==EMultiMediaNotSupported)	
   506 	if ((iMediaType=iCard->MediaType())==EMultiMediaNotSupported)	
   463 		return(KErrNotReady);
   507 	    {
       
   508 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOCREATE_EXIT2, this, KErrNotReady );
       
   509 		return KErrNotReady;
       
   510 	    }
   464 
   511 
   465 	// get card characteristics
   512 	// get card characteristics
   466 	const TCSD& csd = iCard->CSD();
   513 	const TCSD& csd = iCard->CSD();
   467 	iBlkLenLog2 = iCard->MaxReadBlLen();
   514 	iBlkLenLog2 = iCard->MaxReadBlLen();
   468 	iBlkLen = 1 << iBlkLenLog2;
   515 	iBlkLen = 1 << iBlkLenLog2;
   477 	iReadBlPartial = iCard->IsHighCapacity() ? EFalse : csd.ReadBlPartial();
   524 	iReadBlPartial = iCard->IsHighCapacity() ? EFalse : csd.ReadBlPartial();
   478 
   525 
   479 	// allocate and initialize session object
   526 	// allocate and initialize session object
   480 	TInt r = AllocateSession();
   527 	TInt r = AllocateSession();
   481 	if (r!= KErrNone)
   528 	if (r!= KErrNone)
       
   529 	    {
       
   530 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOCREATE_EXIT3, this, r );
   482 		return r;
   531 		return r;
       
   532 	    }
   483 
   533 
   484 	// get buffer memory from EPBUS
   534 	// get buffer memory from EPBUS
   485 	TUint8* buf;
   535 	TUint8* buf;
   486 	TInt bufLen;
   536 	TInt bufLen;
   487 	TInt minorBufLen;
   537 	TInt minorBufLen;
   502 
   552 
   503 	TInt mediaIndex = iMediaId - primaryMedia->iMediaId;
   553 	TInt mediaIndex = iMediaId - primaryMedia->iMediaId;
   504 	TInt bufIndex = (iCardNumber * numMedia)  + mediaIndex;
   554 	TInt bufIndex = (iCardNumber * numMedia)  + mediaIndex;
   505 	
   555 	
   506 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("physicalCardSlots %d, iCardNumber %d\n",  physicalCardSlots, iCardNumber));
   556 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("physicalCardSlots %d, iCardNumber %d\n",  physicalCardSlots, iCardNumber));
       
   557 	OstTraceExt2(TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DOCREATE_VARS1, "physicalCardSlots=%d; iCardNumber=%d", physicalCardSlots, iCardNumber);
   507 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("iMediaId %d numMedia %d, mediaIndex %d, totalNumMedia %d, bufIndex %d\n", 
   558 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("iMediaId %d numMedia %d, mediaIndex %d, totalNumMedia %d, bufIndex %d\n", 
   508 			  iMediaId, numMedia, mediaIndex, totalNumMedia, bufIndex));
   559 			  iMediaId, numMedia, mediaIndex, totalNumMedia, bufIndex));
       
   560 	OstTraceExt5(TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DOCREATE_VARS2, "iMediaId=%d; numMedia=%d; mediaIndex=%d; totalNumMedia=%d; bufIndex=%d", iMediaId, numMedia, mediaIndex, totalNumMedia, bufIndex);
   509 
   561 
   510 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("bufLen1 %08X iCacheBuf1 %08X", bufLen, iCacheBuf));
   562 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("bufLen1 %08X iCacheBuf1 %08X", bufLen, iCacheBuf));
       
   563 	OstTraceExt2(TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DOCREATE_CACHEBUF1, "bufLen1=0x%08x; iCacheBuf1=0x%08x", (TUint) bufLen, (TUint) iCacheBuf);
   511 	bufLen/= totalNumMedia;
   564 	bufLen/= totalNumMedia;
   512 	iCacheBuf+= bufIndex * bufLen;
   565 	iCacheBuf+= bufIndex * bufLen;
   513 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("bufLen2 %08X iCacheBuf2 %08X", bufLen, iCacheBuf));
   566 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("bufLen2 %08X iCacheBuf2 %08X", bufLen, iCacheBuf));
       
   567 	OstTraceExt2(TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DOCREATE_CACHEBUF2, "bufLen2=0x%08x; iCacheBuf2=0x%08x", (TUint) bufLen, (TUint) iCacheBuf);
   514 
   568 
   515 	iBlocksInBuffer = bufLen >> iBlkLenLog2;	// may lose partial block
   569 	iBlocksInBuffer = bufLen >> iBlkLenLog2;	// may lose partial block
   516 	if(iSocket->SupportsDoubleBuffering())
   570 	if(iSocket->SupportsDoubleBuffering())
   517 		{
   571 		{
   518 		// Ensure that there's always an even number of buffered blocks when double-buffering
   572 		// Ensure that there's always an even number of buffered blocks when double-buffering
   535 
   589 
   536 	iPrWtGpLen = iCard->PreferredWriteGroupLength();
   590 	iPrWtGpLen = iCard->PreferredWriteGroupLength();
   537 
   591 
   538 	// check the preferred write group length is a power of two
   592 	// check the preferred write group length is a power of two
   539 	if(iPrWtGpLen == 0 || (iPrWtGpLen & (~iPrWtGpLen + 1)) != iPrWtGpLen)
   593 	if(iPrWtGpLen == 0 || (iPrWtGpLen & (~iPrWtGpLen + 1)) != iPrWtGpLen)
   540 		return(KErrNotReady);
   594 	    {
       
   595 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOCREATE_EXIT4, this, KErrNotReady );
       
   596 		return KErrNotReady;
       
   597 	    }
   541 
   598 
   542 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("iMaxBufSize %d iPrWtGpLen %d\n", iMaxBufSize, iPrWtGpLen));
   599 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("iMaxBufSize %d iPrWtGpLen %d\n", iMaxBufSize, iPrWtGpLen));
       
   600 	OstTraceExt2(TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DOCREATE_IPRWTGPLEN1, "iMaxBufSize=%d; iPrWtGpLen1=%d", iMaxBufSize, iPrWtGpLen);
   543 	// ensure the preferred write group length is as large as possible
   601 	// ensure the preferred write group length is as large as possible
   544 	// so we can write to more than one write group at once
   602 	// so we can write to more than one write group at once
   545 	while (iPrWtGpLen < (TUint32) iMaxBufSize)
   603 	while (iPrWtGpLen < (TUint32) iMaxBufSize)
   546 		iPrWtGpLen <<= 1;
   604 		iPrWtGpLen <<= 1;
   547 
   605 
   549 	while (iPrWtGpLen > (TUint32) iMaxBufSize)
   607 	while (iPrWtGpLen > (TUint32) iMaxBufSize)
   550 		iPrWtGpLen >>= 1;
   608 		iPrWtGpLen >>= 1;
   551 	iPrWtGpMsk = TInt64(iPrWtGpLen - 1);
   609 	iPrWtGpMsk = TInt64(iPrWtGpLen - 1);
   552 
   610 
   553 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("iPrWtGpLen #2 %d\n", iPrWtGpLen));
   611 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("iPrWtGpLen #2 %d\n", iPrWtGpLen));
       
   612 	OstTrace1(TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DOCREATE_IPRWTGPLEN2, "iPrWtGpLen2=%d", iPrWtGpLen);
   554 
   613 
   555 	// allocate index for cached blocks
   614 	// allocate index for cached blocks
   556 	iCachedBlocks =	KernAlloc<TInt64>(iBlocksInBuffer);
   615 	iCachedBlocks =	KernAlloc<TInt64>(iBlocksInBuffer);
   557 	if (iCachedBlocks == 0)
   616 	if (iCachedBlocks == 0)
   558 		return(KErrNoMemory);
   617 	    {
       
   618 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOCREATE_EXIT5, this, KErrNoMemory );
       
   619 		return KErrNoMemory;
       
   620 	    }
   559 
   621 
   560 	InvalidateCache();
   622 	InvalidateCache();
   561 	iLstUsdCchEnt = iBlocksInBuffer - 1;		// use entry 0 first
   623 	iLstUsdCchEnt = iBlocksInBuffer - 1;		// use entry 0 first
   562 
   624 
   563 	// allocate read lookup index
   625 	// allocate read lookup index
   564 	iGamma = KernAlloc<TInt>(iBlocksInBuffer);
   626 	iGamma = KernAlloc<TInt>(iBlocksInBuffer);
   565 	if (iGamma == 0)
   627 	if (iGamma == 0)
   566 		return(KErrNoMemory);
   628 	    {
       
   629 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOCREATE_EXIT6, this, KErrNoMemory );
       
   630 		return KErrNoMemory;
       
   631 	    }
   567 
   632 
   568 	// get current requirements
   633 	// get current requirements
   569 	iReadCurrentInMilliAmps = csd.MaxReadCurrentInMilliamps();
   634 	iReadCurrentInMilliAmps = csd.MaxReadCurrentInMilliamps();
   570 	iWriteCurrentInMilliAmps = csd.MaxWriteCurrentInMilliamps();
   635 	iWriteCurrentInMilliAmps = csd.MaxWriteCurrentInMilliamps();
   571 
   636 
   572 	// get preferred erase information for format operations
   637 	// get preferred erase information for format operations
   573 	const TInt err = iCard->GetEraseInfo(iEraseInfo);
   638 	const TInt err = iCard->GetEraseInfo(iEraseInfo);
   574 	if(err != KErrNone)
   639 	if(err != KErrNone)
   575 		return(err);
   640 	    {
       
   641 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOCREATE_EXIT7, this, err );
       
   642 		return err;
       
   643 	    }
   576 
   644 
   577 	iEraseUnitMsk = TInt64(iEraseInfo.iPreferredEraseUnitSize) - 1;
   645 	iEraseUnitMsk = TInt64(iEraseInfo.iPreferredEraseUnitSize) - 1;
   578 
   646 
   579 	// Retrieve the demand paging info from the PSL of the stack
   647 	// Retrieve the demand paging info from the PSL of the stack
   580 	Stack().DemandPagingInfo(iDemandPagingInfo);
   648 	Stack().DemandPagingInfo(iDemandPagingInfo);
   586 	//
   654 	//
   587 	if(iInternalSlot)
   655 	if(iInternalSlot)
   588 		{
   656 		{
   589 		iMmcPartitionInfo = CreateEmmcPartitionInfo();
   657 		iMmcPartitionInfo = CreateEmmcPartitionInfo();
   590 		if(iMmcPartitionInfo == NULL)
   658 		if(iMmcPartitionInfo == NULL)
       
   659 		    {
       
   660 			OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOCREATE_EXIT8, this, KErrNoMemory );
   591 			return KErrNoMemory;
   661 			return KErrNoMemory;
   592 
   662 		    }
   593 		TInt err = iMmcPartitionInfo->Initialise(this);
   663 		TInt err = iMmcPartitionInfo->Initialise(this);
   594 		if(err != KErrNone)
   664 		if(err != KErrNone)
       
   665 		    {
       
   666 			OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOCREATE_EXIT9, this, err );
   595 			return err;
   667 			return err;
       
   668 		    }
   596 		}
   669 		}
   597 
   670 
   598 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:opn"));
   671 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:opn"));
   599 
   672 
   600 	return(KErrNone);
   673 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOCREATE_EXIT10, this, KErrNone );
       
   674 	return KErrNone;
   601 	}
   675 	}
   602 
   676 
   603 void DMmcMediaDriverFlash::Close()
   677 void DMmcMediaDriverFlash::Close()
   604 //
   678 //
   605 // Close the media driver - also called on media change
   679 // Close the media driver - also called on media change
   606 //
   680 //
   607 	{
   681 	{
       
   682 	OstTraceFunctionEntry0( DMMCMEDIADRIVERFLASH_CLOSE_ENTRY );
   608 	__KTRACE_OPT(KPBUSDRV,Kern::Printf("=mmd:cls"));
   683 	__KTRACE_OPT(KPBUSDRV,Kern::Printf("=mmd:cls"));
   609 	
   684 	
   610 	EndInCritical();
   685 	EndInCritical();
   611 	iSessionEndDfc.Cancel();
   686 	iSessionEndDfc.Cancel();
   612 	iDataTransferCallBackDfc.Cancel();
   687 	iDataTransferCallBackDfc.Cancel();
   613 	CompleteRequest(KErrNotReady);
   688 	CompleteRequest(KErrNotReady);
   614 	DMediaDriver::Close();
   689 	DMediaDriver::Close();
       
   690 	OstTraceFunctionExit0( DMMCMEDIADRIVERFLASH_CLOSE_EXIT );
   615 	}
   691 	}
   616 
   692 
   617 
   693 
   618 DMmcMediaDriverFlash::~DMmcMediaDriverFlash()
   694 DMmcMediaDriverFlash::~DMmcMediaDriverFlash()
   619 	{
   695 	{
       
   696 	OstTraceFunctionEntry0( DMMCMEDIADRIVERFLASH_DMMCMEDIADRIVERFLASH_ENTRY );
   620 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:dtr"));
   697 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:dtr"));
   621 
   698 
   622 	iSessionEndDfc.Cancel();
   699 	iSessionEndDfc.Cancel();
   623 	iDataTransferCallBackDfc.Cancel();
   700 	iDataTransferCallBackDfc.Cancel();
   624 
   701 
   627 	Kern::Free(iGamma);
   704 	Kern::Free(iGamma);
   628 
   705 
   629 	delete iMmcPartitionInfo;
   706 	delete iMmcPartitionInfo;
   630 
   707 
   631 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:dtr"));
   708 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:dtr"));
       
   709 	OstTraceFunctionExit0( DMMCMEDIADRIVERFLASH_DMMCMEDIADRIVERFLASH_EXIT );
   632 	}
   710 	}
   633 
   711 
   634 
   712 
   635 TInt DMmcMediaDriverFlash::AllocateSession()
   713 TInt DMmcMediaDriverFlash::AllocateSession()
   636 	{
   714 	{
       
   715 OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_ALLOCATESESSION_ENTRY, this );
       
   716 
       
   717 	
   637 	// already allocated ?
   718 	// already allocated ?
   638 	if (iSession != NULL)
   719 	if (iSession != NULL)
       
   720 	    {
       
   721 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_ALLOCATESESSION_EXIT1, this, KErrNone );
   639 		return KErrNone;
   722 		return KErrNone;
       
   723 	    }
   640 
   724 
   641 	iSession = iStack->AllocSession(iSessionEndCallBack);
   725 	iSession = iStack->AllocSession(iSessionEndCallBack);
   642 	if (iSession == NULL)
   726 	if (iSession == NULL)
       
   727 	    {
       
   728 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_ALLOCATESESSION_EXIT2, this, KErrNoMemory );
   643 		return KErrNoMemory;
   729 		return KErrNoMemory;
       
   730 	    }
   644 
   731 
   645 	iSession->SetStack(iStack);
   732 	iSession->SetStack(iStack);
   646 	iSession->SetCard(iCard);
   733 	iSession->SetCard(iCard);
   647 	iSession->SetDataTransferCallback(iDataTransferCallBack);
   734 	iSession->SetDataTransferCallback(iDataTransferCallBack);
   648 
   735 
       
   736 
       
   737 
       
   738 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_ALLOCATESESSION_EXIT3, this, KErrNone );
   649 	return KErrNone;
   739 	return KErrNone;
   650 	}
   740 	}
   651 
   741 
   652 // ---- media access ----
   742 // ---- media access ----
   653 
   743 
   655 //
   745 //
   656 // set up iReqStart, iReqEnd and iReqCur and launch first read.  Subsequent reads
   746 // set up iReqStart, iReqEnd and iReqCur and launch first read.  Subsequent reads
   657 // will be launched from the callback DFC.
   747 // will be launched from the callback DFC.
   658 //
   748 //
   659 	{
   749 	{
       
   750 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_DOREAD_ENTRY, this );
   660 	TInt r = CheckDevice(EMReqTypeNormalRd); 
   751 	TInt r = CheckDevice(EMReqTypeNormalRd); 
   661 	if (r != KErrNone) return r;
   752 	if (r != KErrNone)
       
   753 	    {
       
   754 	    OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOREAD_EXIT1, this, r );
       
   755 	    return r;
       
   756 	    }
   662 	
   757 	
   663 	const TInt64 pos(iCurrentReq->Pos());
   758 	const TInt64 pos(iCurrentReq->Pos());
   664 	TUint32 length(I64LOW(iCurrentReq->Length()));
   759 	TUint32 length(I64LOW(iCurrentReq->Length()));
   665 
   760 
   666 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:dr:0x%lx,0x%x", pos, length));
   761 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:dr:0x%lx,0x%x", pos, length));
       
   762 	OstTraceExt2( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DO_READ, "Position=0x%lx; Length=0x%x", (TUint) pos, (TUint) length);
   667 	__ASSERT_DEBUG(CurrentRequest() == EMReqIdle, Panic(EDRInUse));
   763 	__ASSERT_DEBUG(CurrentRequest() == EMReqIdle, Panic(EDRInUse));
   668 	__ASSERT_DEBUG(pos < TotalSizeInBytes(), Panic(EDRStart));
   764 	__ASSERT_DEBUG(pos < TotalSizeInBytes(), Panic(EDRStart));
   669 	__ASSERT_DEBUG(iCurrentReq->Length() >= 0, Panic(EDRNotPositive));
   765 	__ASSERT_DEBUG(iCurrentReq->Length() >= 0, Panic(EDRNotPositive));
   670 	__ASSERT_DEBUG(TotalSizeInBytes() >= pos + length, Panic(EDREnd));
   766 	__ASSERT_DEBUG(TotalSizeInBytes() >= pos + length, Panic(EDREnd));
   671 
   767 
   690 			else if ( (iReqEnd - iPhysStart) > iMaxBufSize && iSocket->SupportsDoubleBuffering() && !iReadToEndOfCard)
   786 			else if ( (iReqEnd - iPhysStart) > iMaxBufSize && iSocket->SupportsDoubleBuffering() && !iReadToEndOfCard)
   691 				r = LaunchDBRead();
   787 				r = LaunchDBRead();
   692 			else
   788 			else
   693 				r = LaunchRead(iReqCur, length);
   789 				r = LaunchRead(iReqCur, length);
   694 
   790 
   695 			if (r == KErrNone) return r;
   791 			if (r == KErrNone)
       
   792 			    {
       
   793 			    OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOREAD_EXIT2, this, r );
       
   794 			    return r;
       
   795 			    }
   696 			}
   796 			}
   697 		}
   797 		}
   698 	else
   798 	else
   699 		{
   799 		{
   700 #if defined(__DEMAND_PAGING__) && !defined(__WINS__)
   800 #if defined(__DEMAND_PAGING__) && !defined(__WINS__)
   714 	if(r == KErrNone)
   814 	if(r == KErrNone)
   715 		r = KErrCompletion;
   815 		r = KErrCompletion;
   716 
   816 
   717 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:dr:%d", r));
   817 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:dr:%d", r));
   718 	
   818 	
       
   819 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOREAD_EXIT3, this, r );
   719 	return r;
   820 	return r;
   720 	}
   821 	}
   721 
   822 
   722 
   823 
   723 TInt DMmcMediaDriverFlash::LaunchRead(TInt64 aStart, TUint32 aLength)
   824 TInt DMmcMediaDriverFlash::LaunchRead(TInt64 aStart, TUint32 aLength)
   726 // iReq* instance variables.  It sets iPhysStart and iPhysEnd to the region that was actually
   827 // iReq* instance variables.  It sets iPhysStart and iPhysEnd to the region that was actually
   727 // read into iIntBuf. iIntBuf can be set to a cached entry or to the minor buffer.  It is
   828 // read into iIntBuf. iIntBuf can be set to a cached entry or to the minor buffer.  It is
   728 // assumed that before this function is called that ReadDataUntilCacheExhausted() has been used.
   829 // assumed that before this function is called that ReadDataUntilCacheExhausted() has been used.
   729 //
   830 //
   730 	{
   831 	{
       
   832 	OstTraceFunctionEntryExt( DMMCMEDIADRIVERFLASH_LAUNCHREAD_ENTRY, this );
       
   833 	OstTraceExt2( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHREAD, "position=0x%lx; length=0x%x", (TUint) iCurrentReq->Pos(), (TUint) I64LOW(iCurrentReq->Length()));
   731 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:lr:0x%lx,0x%x", aStart, aLength));
   834 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:lr:0x%lx,0x%x", aStart, aLength));
   732 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(ELRStart));
   835 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(ELRStart));
   733 	__ASSERT_DEBUG(aLength > 0, Panic(ELRNotPositive));
   836 	__ASSERT_DEBUG(aLength > 0, Panic(ELRNotPositive));
   734 	__ASSERT_DEBUG(TotalSizeInBytes() >= aStart + aLength, Panic(ELREnd));
   837 	__ASSERT_DEBUG(TotalSizeInBytes() >= aStart + aLength, Panic(ELREnd));
   735 	__ASSERT_CACHE(GetCachedBlock(aStart & ~iBlkMsk) == 0, Panic(ELRCached));
   838 	__ASSERT_CACHE(GetCachedBlock(aStart & ~iBlkMsk) == 0, Panic(ELRCached));
   777 		}
   880 		}
   778 	
   881 	
   779 	TInt r = EngageAndSetReadRequest(EMReqRead);
   882 	TInt r = EngageAndSetReadRequest(EMReqRead);
   780 	
   883 	
   781 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lr:%d", r));
   884 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lr:%d", r));
       
   885 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_LAUNCHREAD_EXIT, this, r );
   782 	return r;
   886 	return r;
   783 	}
   887 	}
   784 
   888 
   785 TInt DMmcMediaDriverFlash::LaunchDBRead()
   889 TInt DMmcMediaDriverFlash::LaunchDBRead()
   786 //
   890 //
   788 // iReq* instance variables.  It sets iPhysStart and iPhysEnd to the region that was actually
   892 // iReq* instance variables.  It sets iPhysStart and iPhysEnd to the region that was actually
   789 // read into iIntBuf. iIntBuf can be set to a cached entry or to the minor buffer.  It is
   893 // read into iIntBuf. iIntBuf can be set to a cached entry or to the minor buffer.  It is
   790 // assumed that before this function is called that ReadDataUntilCacheExhausted() has been used.
   894 // assumed that before this function is called that ReadDataUntilCacheExhausted() has been used.
   791 //
   895 //
   792 	{
   896 	{
       
   897 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_LAUNCHDBREAD_ENTRY, this );
   793 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:ldbr:0x%lx,0x%x", iReqCur, I64LOW(iReqEnd - iReqCur)));
   898 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:ldbr:0x%lx,0x%x", iReqCur, I64LOW(iReqEnd - iReqCur)));
       
   899 	OstTraceExt2( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHDBREAD, "position=0x%lx; length=0x%x", (TInt) iReqCur, (TInt) I64LOW(iReqEnd - iReqCur));
   794 	__ASSERT_DEBUG(TotalSizeInBytes() > iReqCur, Panic(ELRStart));
   900 	__ASSERT_DEBUG(TotalSizeInBytes() > iReqCur, Panic(ELRStart));
   795 	__ASSERT_DEBUG(I64LOW(iReqEnd - iReqCur) > 0, Panic(ELRNotPositive));
   901 	__ASSERT_DEBUG(I64LOW(iReqEnd - iReqCur) > 0, Panic(ELRNotPositive));
   796 	__ASSERT_DEBUG(TotalSizeInBytes() >= iReqEnd, Panic(ELREnd));
   902 	__ASSERT_DEBUG(TotalSizeInBytes() >= iReqEnd, Panic(ELREnd));
   797 	__ASSERT_CACHE(GetCachedBlock(iReqCur & ~iBlkMsk) == 0, Panic(ELRCached));
   903 	__ASSERT_CACHE(GetCachedBlock(iReqCur & ~iBlkMsk) == 0, Panic(ELRCached));
   798 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
   904 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
   836 
   942 
   837 	TInt r = EngageAndSetReadRequest(EMReqRead);
   943 	TInt r = EngageAndSetReadRequest(EMReqRead);
   838 
   944 
   839 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:ldbr:%d", r));
   945 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:ldbr:%d", r));
   840 
   946 
       
   947 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_LAUNCHDBREAD_EXIT, this, r );
   841 	return r;
   948 	return r;
   842 	}
   949 	}
   843 
   950 
   844 
   951 
   845 TInt DMmcMediaDriverFlash::LaunchPhysRead(TInt64 aStart, TUint32 aLength)
   952 TInt DMmcMediaDriverFlash::LaunchPhysRead(TInt64 aStart, TUint32 aLength)
   847 // This function does not maintain the iReq* instance variables.  
   954 // This function does not maintain the iReq* instance variables.  
   848 // It is assumed that before this function is called that 
   955 // It is assumed that before this function is called that 
   849 // ReadDataUntilCacheExhausted() has been used.
   956 // ReadDataUntilCacheExhausted() has been used.
   850 //
   957 //
   851 	{
   958 	{
       
   959 	OstTraceFunctionEntryExt( DMMCMEDIADRIVERFLASH_LAUNCHPHYSREAD_ENTRY, this );
       
   960 	OstTraceExt2( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHPHYSREAD, "position=0x%lx; length=0x%x", (TInt) iReqCur, (TInt) I64LOW(iReqEnd - iReqCur));
   852 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:physr:0x%lx,0x%x", aStart, aLength));
   961 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:physr:0x%lx,0x%x", aStart, aLength));
   853 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(ELRStart));
   962 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(ELRStart));
   854 	__ASSERT_DEBUG(aLength > 0, Panic(ELRNotPositive));
   963 	__ASSERT_DEBUG(aLength > 0, Panic(ELRNotPositive));
   855 	__ASSERT_DEBUG(TotalSizeInBytes() >= aStart + aLength, Panic(ELREnd));
   964 	__ASSERT_DEBUG(TotalSizeInBytes() >= aStart + aLength, Panic(ELREnd));
   856 	__ASSERT_CACHE(GetCachedBlock(aStart & ~iBlkMsk) == 0, Panic(ELRCached));
   965 	__ASSERT_CACHE(GetCachedBlock(aStart & ~iBlkMsk) == 0, Panic(ELRCached));
   877 	TUint32 physLen(I64LOW(iPhysEnd - iPhysStart));
   986 	TUint32 physLen(I64LOW(iPhysEnd - iPhysStart));
   878 	
   987 	
   879 	if (firstPartial)
   988 	if (firstPartial)
   880 		{
   989 		{
   881 		__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:physr:FirstPartial"));
   990 		__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:physr:FirstPartial"));
       
   991 		OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCH_PHYSREAD_FP, "FirstPartial");
   882 		// first index does not start on block boundary
   992 		// first index does not start on block boundary
   883 		// iIntBuf linear address is used for IPC within DoReadDataTransferCallBack()
   993 		// iIntBuf linear address is used for IPC within DoReadDataTransferCallBack()
   884 		iRdROB |= KIPCWrite;
   994 		iRdROB |= KIPCWrite;
   885 		
   995 		
   886 		iIntBuf = ReserveReadBlocks(iPhysStart, iPhysStart+iBlkLen,(TUint32*)&physLength);
   996 		iIntBuf = ReserveReadBlocks(iPhysStart, iPhysStart+iBlkLen,(TUint32*)&physLength);
   920 		r = EngageAndSetReadRequest(EMReqRead);
  1030 		r = EngageAndSetReadRequest(EMReqRead);
   921    		} 
  1031    		} 
   922 		
  1032 		
   923 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lphysr:%d", r));
  1033 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lphysr:%d", r));
   924 
  1034 
       
  1035 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_LAUNCHPHYSREAD_EXIT, this, r );
   925 	return r;
  1036 	return r;
   926 	}
  1037 	}
   927 
  1038 
   928 
  1039 
   929 TInt DMmcMediaDriverFlash::DoWrite()
  1040 TInt DMmcMediaDriverFlash::DoWrite()
   931 // set up iReqStart, iReqEnd, and iReqCur, and launch first write.  Any subsequent
  1042 // set up iReqStart, iReqEnd, and iReqCur, and launch first write.  Any subsequent
   932 // writes are launched from the session end DFC.  LaunchWrite() handles pre-reading
  1043 // writes are launched from the session end DFC.  LaunchWrite() handles pre-reading
   933 // any sectors that are only partially modified.
  1044 // any sectors that are only partially modified.
   934 //
  1045 //
   935 	{
  1046 	{
       
  1047 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_DOWRITE_ENTRY, this );
   936 	const TInt64 pos = iCurrentReq->Pos();
  1048 	const TInt64 pos = iCurrentReq->Pos();
   937 	const TUint32 length = I64LOW(iCurrentReq->Length());
  1049 	const TUint32 length = I64LOW(iCurrentReq->Length());
   938 
  1050 
   939 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:dw:0x%lx,0x%x", pos, length));
  1051 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:dw:0x%lx,0x%x", pos, length));
       
  1052 	OstTraceExt2( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DOWRITE, "position=0x%lx; length=0x%x", (TUint) pos, (TUint) length);
   940 	__ASSERT_DEBUG(CurrentRequest() == EMReqIdle, Panic(EDWInUse));
  1053 	__ASSERT_DEBUG(CurrentRequest() == EMReqIdle, Panic(EDWInUse));
   941 	__ASSERT_DEBUG(pos < TotalSizeInBytes(), Panic(EDWStart));
  1054 	__ASSERT_DEBUG(pos < TotalSizeInBytes(), Panic(EDWStart));
   942 	__ASSERT_DEBUG(length > 0, Panic(EDWNotPositive));
  1055 	__ASSERT_DEBUG(length > 0, Panic(EDWNotPositive));
   943 	__ASSERT_DEBUG(TotalSizeInBytes() >= pos + length, Panic(EDWEnd));
  1056 	__ASSERT_DEBUG(TotalSizeInBytes() >= pos + length, Panic(EDWEnd));
   944 
  1057 
   957 	
  1070 	
   958 	const TInt r = LaunchWrite(iReqStart, length, EMReqWrite);
  1071 	const TInt r = LaunchWrite(iReqStart, length, EMReqWrite);
   959 
  1072 
   960 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:dw:%d", r));
  1073 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:dw:%d", r));
   961 
  1074 
       
  1075 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOWRITE_EXIT, this, r );
   962 	return r;
  1076 	return r;
   963 	}
  1077 	}
   964 
  1078 
   965 
  1079 
   966 TInt DMmcMediaDriverFlash::DoFormat()
  1080 TInt DMmcMediaDriverFlash::DoFormat()
   967 	{
  1081 	{
       
  1082 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_DOFORMAT_ENTRY, this );
   968 	const TInt64 pos = iCurrentReq->Pos();
  1083 	const TInt64 pos = iCurrentReq->Pos();
   969 	const TUint32 length = I64LOW(iCurrentReq->Length());
  1084 	const TUint32 length = I64LOW(iCurrentReq->Length());
   970 
  1085 
   971 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:df:0x%lx,0x%x", pos, length));
  1086 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:df:0x%lx,0x%x", pos, length));
       
  1087 	OstTraceExt2( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DOFORMAT, "position=0x%lx; length=0x%x", (TUint) pos, (TUint) length);
   972 	__ASSERT_DEBUG(CurrentRequest() == EMReqIdle, Panic(EDFInUse));
  1088 	__ASSERT_DEBUG(CurrentRequest() == EMReqIdle, Panic(EDFInUse));
   973 	__ASSERT_DEBUG(pos < TotalSizeInBytes(), Panic(EDFStart));
  1089 	__ASSERT_DEBUG(pos < TotalSizeInBytes(), Panic(EDFStart));
   974 	__ASSERT_DEBUG(length > 0, Panic(EDFNotPositive));
  1090 	__ASSERT_DEBUG(length > 0, Panic(EDFNotPositive));
   975 	__ASSERT_DEBUG(TotalSizeInBytes() >= pos + length, Panic(EDFEnd));
  1091 	__ASSERT_DEBUG(TotalSizeInBytes() >= pos + length, Panic(EDFEnd));
   976 
  1092 
   995 
  1111 
   996 	const TInt r = LaunchFormat(iReqStart, I64LOW(iReqEnd - iReqStart));
  1112 	const TInt r = LaunchFormat(iReqStart, I64LOW(iReqEnd - iReqStart));
   997 
  1113 
   998 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:df:%d", r));
  1114 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:df:%d", r));
   999 
  1115 
       
  1116 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOFORMAT_EXIT, this, r );
  1000 	return r;
  1117 	return r;
  1001 	}
  1118 	}
  1002 
  1119 
  1003 
  1120 
  1004 TInt DMmcMediaDriverFlash::LaunchFormat(TInt64 aStart, TUint32 aLength)
  1121 TInt DMmcMediaDriverFlash::LaunchFormat(TInt64 aStart, TUint32 aLength)
  1005 //
  1122 //
  1006 // starts writes from DoWrite(), DoFormat() and the session end DFC.  This function does not
  1123 // starts writes from DoWrite(), DoFormat() and the session end DFC.  This function does not
  1007 // maintain the iReq* instance variables.  It sets iIntBuf, iPhysStart and iPhysEnd.
  1124 // maintain the iReq* instance variables.  It sets iIntBuf, iPhysStart and iPhysEnd.
  1008 //
  1125 //
  1009 	{
  1126 	{
       
  1127 	OstTraceFunctionEntryExt( DMMCMEDIADRIVERFLASH_LAUNCHFORMAT_ENTRY, this );
       
  1128 	OstTraceExt2( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHFORMAT, "position=0x%lx; length=0x%x", (TInt) iReqCur, (TInt) I64LOW(iReqEnd - iReqCur));
  1010 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:lf:0x%lx,0x%x", aStart, aLength));
  1129 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:lf:0x%lx,0x%x", aStart, aLength));
  1011 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(ELFStart));
  1130 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(ELFStart));
  1012 	__ASSERT_DEBUG((aStart & iBlkMsk) == 0, Panic(ELWFmtStAlign));
  1131 	__ASSERT_DEBUG((aStart & iBlkMsk) == 0, Panic(ELWFmtStAlign));
  1013 	__ASSERT_DEBUG(aLength > 0, Panic(ELFNotPositive));
  1132 	__ASSERT_DEBUG(aLength > 0, Panic(ELFNotPositive));
  1014 	__ASSERT_DEBUG(TotalSizeInBytes() >= aStart + aLength, Panic(ELFEnd));
  1133 	__ASSERT_DEBUG(TotalSizeInBytes() >= aStart + aLength, Panic(ELFEnd));
  1090 			iSession->SetupCIMWriteBlock(I64LOW(iPhysStart >> KMMCardHighCapBlockSizeLog2), iCacheBuf, writeLen >> KMMCardHighCapBlockSizeLog2);
  1209 			iSession->SetupCIMWriteBlock(I64LOW(iPhysStart >> KMMCardHighCapBlockSizeLog2), iCacheBuf, writeLen >> KMMCardHighCapBlockSizeLog2);
  1091 			}
  1210 			}
  1092 		
  1211 		
  1093 		r = EngageAndSetWriteRequest(EMReqFormat);
  1212 		r = EngageAndSetWriteRequest(EMReqFormat);
  1094 		}
  1213 		}
       
  1214 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_LAUNCHFORMAT_EXIT, this, r );
  1095 		return r;
  1215 		return r;
  1096 	}
  1216 	}
  1097 
  1217 
  1098 
  1218 
  1099 
  1219 
  1101 //
  1221 //
  1102 // starts writes from DoWrite(), DoFormat() and the session end DFC.  This function does not
  1222 // starts writes from DoWrite(), DoFormat() and the session end DFC.  This function does not
  1103 // maintain the iReq* instance variables.  It sets iIntBuf, iPhysStart and iPhysEnd.
  1223 // maintain the iReq* instance variables.  It sets iIntBuf, iPhysStart and iPhysEnd.
  1104 //
  1224 //
  1105 	{
  1225 	{
       
  1226 	OstTraceExt4(TRACE_FLOW, DMMCMEDIADRIVERFLASH_LAUNCHWRITE_ENTRY, "DMmcMediaDriverFlash::LaunchWrite;aStart=%Ld;aLength=%x;aMedReq=%d;this=%x", aStart, (TUint) aLength, (TInt) aMedReq, (TUint) this);
       
  1227 	OstTraceExt2( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHWRITE, "position=0x%lx; length=0x%x", (TInt) iReqCur, (TInt) I64LOW(iReqEnd - iReqCur));
  1106 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("\n>mmd:lw:0x%lx,%d,%d", aStart, aLength, aMedReq));
  1228 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("\n>mmd:lw:0x%lx,%d,%d", aStart, aLength, aMedReq));
  1107 	__ASSERT_DEBUG(aMedReq == EMReqWrite || aMedReq == EMReqFormat, Panic(ELWRequest));
  1229 	__ASSERT_DEBUG(aMedReq == EMReqWrite || aMedReq == EMReqFormat, Panic(ELWRequest));
  1108 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(ELWStart));
  1230 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(ELWStart));
  1109 	__ASSERT_DEBUG(!(aMedReq == EMReqFormat) || (aStart & iBlkMsk) == 0, Panic(ELWFmtStAlign));
  1231 	__ASSERT_DEBUG(!(aMedReq == EMReqFormat) || (aStart & iBlkMsk) == 0, Panic(ELWFmtStAlign));
  1110 	__ASSERT_DEBUG(aLength > 0, Panic(ELWNotPositive));
  1232 	__ASSERT_DEBUG(aLength > 0, Panic(ELWNotPositive));
  1205 			} //if (iWtRBM == 0)
  1327 			} //if (iWtRBM == 0)
  1206 
  1328 
  1207 		if (iWtRBM & KWtRBMFst)
  1329 		if (iWtRBM & KWtRBMFst)
  1208 			{			
  1330 			{			
  1209 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("mmd:lw: read-before-modify required on first block"));
  1331 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("mmd:lw: read-before-modify required on first block"));
       
  1332 			OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHWRITE_RBMF, "Read-before-modify required on first block");
  1210 			if (iDoPhysicalAddress)
  1333 			if (iDoPhysicalAddress)
  1211 				iSession->SetupCIMReadBlock(I64LOW(iPhysStart >> KMMCardHighCapBlockSizeLog2), iMinorBuf, iBlkLen >> KMMCardHighCapBlockSizeLog2);
  1334 				iSession->SetupCIMReadBlock(I64LOW(iPhysStart >> KMMCardHighCapBlockSizeLog2), iMinorBuf, iBlkLen >> KMMCardHighCapBlockSizeLog2);
  1212 			else
  1335 			else
  1213 				iSession->SetupCIMReadBlock(I64LOW(iPhysStart >> KMMCardHighCapBlockSizeLog2), iIntBuf, iBlkLen >> KMMCardHighCapBlockSizeLog2);
  1336 				iSession->SetupCIMReadBlock(I64LOW(iPhysStart >> KMMCardHighCapBlockSizeLog2), iIntBuf, iBlkLen >> KMMCardHighCapBlockSizeLog2);
  1214 			return EngageAndSetReadRequest(aMedReq);
  1337 			r = EngageAndSetReadRequest(aMedReq);
       
  1338 			OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_LAUNCHWRITE_EXIT1, this, r );
       
  1339 			return r;
  1215 			}
  1340 			}
  1216 
  1341 
  1217 		else if (iWtRBM & KWtRBMLst)
  1342 		else if (iWtRBM & KWtRBMLst)
  1218 			{
  1343 			{
  1219 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("mmd:lw: read-before-modify required on last block"));			
  1344 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("mmd:lw: read-before-modify required on last block"));			
       
  1345 			OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHWRITE_RBML, "Read-before-modify required on last block");
  1220 			if(iDoDoubleBuffer || iDoPhysicalAddress)
  1346 			if(iDoDoubleBuffer || iDoPhysicalAddress)
  1221 				{
  1347 				{
  1222 				//
  1348 				//
  1223 				// When double-buffering, the result of the RMW-read operation shall be stored
  1349 				// When double-buffering, the result of the RMW-read operation shall be stored
  1224 				// in the minor buffer, otherwise the data would be overwritten before the last
  1350 				// in the minor buffer, otherwise the data would be overwritten before the last
  1240 				const TInt64 lastBlock = iPhysEnd - iBlkLen;		// start posn in media to read from
  1366 				const TInt64 lastBlock = iPhysEnd - iBlkLen;		// start posn in media to read from
  1241 				iSession->SetupCIMReadBlock(I64LOW(lastBlock >> KMMCardHighCapBlockSizeLog2), iIntBuf + (lastBlock - iPhysStart), iBlkLen >> KMMCardHighCapBlockSizeLog2);
  1367 				iSession->SetupCIMReadBlock(I64LOW(lastBlock >> KMMCardHighCapBlockSizeLog2), iIntBuf + (lastBlock - iPhysStart), iBlkLen >> KMMCardHighCapBlockSizeLog2);
  1242 				}
  1368 				}
  1243 
  1369 
  1244 			// Kick off the RMW-read operation for the last block...
  1370 			// Kick off the RMW-read operation for the last block...
  1245 			return EngageAndSetReadRequest(aMedReq);
  1371 			r = EngageAndSetReadRequest(aMedReq);
       
  1372 			OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_LAUNCHWRITE_EXIT2, this, r );
       
  1373 			return r;
  1246 			}
  1374 			}
  1247 		
  1375 		
  1248 		if (iWtRBM & KWtMinFst)
  1376 		if (iWtRBM & KWtMinFst)
  1249 			{
  1377 			{
  1250 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("mmd:lw:Phys write-first-block-only"));			
  1378 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("mmd:lw:Phys write-first-block-only"));			
       
  1379 			OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHWRITE_FBO, "Write first block only");
  1251 			//Overwrite first block with the new data			
  1380 			//Overwrite first block with the new data			
  1252 			TInt32 tlen = I64LOW(aStart & iBlkMsk);
  1381 			TInt32 tlen = I64LOW(aStart & iBlkMsk);
  1253 			TInt32 wlen = UMin(I64LOW((iBlkMsk+1) - tlen), aLength);			
  1382 			TInt32 wlen = UMin(I64LOW((iBlkMsk+1) - tlen), aLength);			
  1254 			
  1383 			
  1255 			const TInt64 usrOfst = (aStart - iReqStart);
  1384 			const TInt64 usrOfst = (aStart - iReqStart);
  1256 			TPtr8 tgt(&iMinorBuf[tlen], I64LOW(wlen));
  1385 			TPtr8 tgt(&iMinorBuf[tlen], I64LOW(wlen));
  1257 
  1386 
  1258 			if ( (r = iCurrentReq->ReadRemote(&tgt,I64LOW(usrOfst))) != KErrNone)
  1387 			if ( (r = iCurrentReq->ReadRemote(&tgt,I64LOW(usrOfst))) != KErrNone)
       
  1388 			    {
       
  1389 				OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_LAUNCHWRITE_EXIT3, this, r );
  1259 				return r;			
  1390 				return r;			
       
  1391 			    }
  1260 			}
  1392 			}
  1261 		
  1393 		
  1262 		if (iWtRBM & KWtMinLst)
  1394 		if (iWtRBM & KWtMinLst)
  1263 			{
  1395 			{
  1264 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("mmd:lw:Phys write-last-block-only"));
  1396 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("mmd:lw:Phys write-last-block-only"));
       
  1397 			OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHWRITE_LBO, "Write last block only");
  1265 			iWtRBM &= ~KWtMinLst;
  1398 			iWtRBM &= ~KWtMinLst;
  1266 			//Overwrite last block with the new data
  1399 			//Overwrite last block with the new data
  1267 			const TInt64 medEnds = aStart + aLength;
  1400 			const TInt64 medEnds = aStart + aLength;
  1268 			TInt64 tlen = medEnds & iBlkMsk;
  1401 			TInt64 tlen = medEnds & iBlkMsk;
  1269 			
  1402 			
  1270 			const TInt64 usrOfst = (aStart - iReqStart);					
  1403 			const TInt64 usrOfst = (aStart - iReqStart);					
  1271 			TPtr8 tgt(iCacheBuf, I64LOW(tlen));			
  1404 			TPtr8 tgt(iCacheBuf, I64LOW(tlen));			
  1272 						
  1405 						
  1273 			if ( (r = iCurrentReq->ReadRemote(&tgt,I64LOW(usrOfst+aLength-tlen))) !=KErrNone)
  1406 			if ( (r = iCurrentReq->ReadRemote(&tgt,I64LOW(usrOfst+aLength-tlen))) !=KErrNone)
       
  1407 			    {
       
  1408 				OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_LAUNCHWRITE_EXIT4, this, r );
  1274 				return r;			
  1409 				return r;			
       
  1410 			    }
  1275 			}
  1411 			}
  1276 		
  1412 		
  1277 		// no reads required - read data from user buffer and launch write
  1413 		// no reads required - read data from user buffer and launch write
  1278 		const TInt64 usrOfst = (aStart - iReqStart);
  1414 		const TInt64 usrOfst = (aStart - iReqStart);
  1279 		const TInt64 bufOfst = aStart - iPhysStart;		// offset into first sector, not whole buffer
  1415 		const TInt64 bufOfst = aStart - iPhysStart;		// offset into first sector, not whole buffer
  1311 				{
  1447 				{
  1312 				iDbEnd = iPhysEnd;
  1448 				iDbEnd = iPhysEnd;
  1313            		iPhysEnd = iPhysStart+physLength;
  1449            		iPhysEnd = iPhysStart+physLength;
  1314            		
  1450            		
  1315            		if ((TUint32)physLength > physLen) physLength = physLen; // more memory in fragment than required!	
  1451            		if ((TUint32)physLength > physLen) physLength = physLen; // more memory in fragment than required!	
  1316            		
  1452            		OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHWRITE_PHYSICAL, "Physical write request" );
  1317 				iSession->SetupCIMWriteBlock(I64LOW(iPhysStart >> KMMCardHighCapBlockSizeLog2), (TUint8*) physAddr, physLen >> KMMCardHighCapBlockSizeLog2);
  1453 				iSession->SetupCIMWriteBlock(I64LOW(iPhysStart >> KMMCardHighCapBlockSizeLog2), (TUint8*) physAddr, physLen >> KMMCardHighCapBlockSizeLog2);
  1318 				iSession->Command().iFlags|= KMMCCmdFlagPhysAddr;
  1454 				iSession->Command().iFlags|= KMMCCmdFlagPhysAddr;
  1319 				iSession->EnableDoubleBuffering(physLength >> KDiskSectorShift);
  1455 				iSession->EnableDoubleBuffering(physLength >> KDiskSectorShift);
  1320 				}
  1456 				}
  1321 			else 
  1457 			else 
  1322 				{
  1458 				{
  1323 				__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lw:Phys:%d", r));
  1459 				__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lw:Phys:%d", r));
       
  1460 				
       
  1461 				OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_LAUNCHWRITE_EXIT5, this, r );
  1324 				return r;					
  1462 				return r;					
  1325 				}
  1463 				}
  1326 			} // if (iDoPhysicalAddress)
  1464 			} // if (iDoPhysicalAddress)
  1327 		else
  1465 		else
  1328 			{
  1466 			{
  1332 			if (r == KErrNone)
  1470 			if (r == KErrNone)
  1333 				{
  1471 				{
  1334 				if(!iDoDoubleBuffer)
  1472 				if(!iDoDoubleBuffer)
  1335 					{
  1473 					{
  1336 					// EPBUSM automatically uses CMD24 instead of CMD25 for single block writes
  1474 					// EPBUSM automatically uses CMD24 instead of CMD25 for single block writes
       
  1475 					OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHWRITE_STANDARD, "Standard write request" );
  1337 					iSession->SetupCIMWriteBlock(I64LOW(iPhysStart >> KMMCardHighCapBlockSizeLog2), iIntBuf, I64LOW((iPhysEnd - iPhysStart) >> KMMCardHighCapBlockSizeLog2));
  1476 					iSession->SetupCIMWriteBlock(I64LOW(iPhysStart >> KMMCardHighCapBlockSizeLog2), iIntBuf, I64LOW((iPhysEnd - iPhysStart) >> KMMCardHighCapBlockSizeLog2));
  1338 					}
  1477 					}
  1339 				else
  1478 				else
  1340 					{
  1479 					{
  1341 					// 
  1480 					// 
  1343 					// request range. and flag the session to enable double-buffering (as well
  1482 					// request range. and flag the session to enable double-buffering (as well
  1344 					// as specifying the length of each double-buffered transfer as calculated
  1483 					// as specifying the length of each double-buffered transfer as calculated
  1345 					// in 'len' above).  This is performed only once - the double-buffering 
  1484 					// in 'len' above).  This is performed only once - the double-buffering 
  1346 					// is subsequently handled within the DoDataTransferCallback function.
  1485 					// is subsequently handled within the DoDataTransferCallback function.
  1347 					//
  1486 					//
       
  1487 					OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHWRITE_DB, "Double-buffered write request" );
  1348 					iSession->SetupCIMWriteBlock(I64LOW(iPhysStart >> KMMCardHighCapBlockSizeLog2), iIntBuf, I64LOW((((iDbEnd + iBlkMsk) & ~iBlkMsk) - iPhysStart) >> KMMCardHighCapBlockSizeLog2));
  1488 					iSession->SetupCIMWriteBlock(I64LOW(iPhysStart >> KMMCardHighCapBlockSizeLog2), iIntBuf, I64LOW((((iDbEnd + iBlkMsk) & ~iBlkMsk) - iPhysStart) >> KMMCardHighCapBlockSizeLog2));
  1349 					iSession->EnableDoubleBuffering(I64LOW((len + iBlkMsk) & ~iBlkMsk) >> KDiskSectorShift);
  1489 					iSession->EnableDoubleBuffering(I64LOW((len + iBlkMsk) & ~iBlkMsk) >> KDiskSectorShift);
  1350 	
  1490 	
  1351 					// ...and switch to the 'second' buffer, which will be populated in the
  1491 					// ...and switch to the 'second' buffer, which will be populated in the
  1352 					// data transfer callback in parallel with hardware transfer of the first.
  1492 					// data transfer callback in parallel with hardware transfer of the first.
  1371 			r = EngageAndSetWriteRequest(aMedReq);
  1511 			r = EngageAndSetWriteRequest(aMedReq);
  1372 		}	// if ((r = CheckDevice(EMReqTypeNormalWr)) == KErrNone)
  1512 		}	// if ((r = CheckDevice(EMReqTypeNormalWr)) == KErrNone)
  1373 
  1513 
  1374 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lw:%d", r));
  1514 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lw:%d", r));
  1375 
  1515 
       
  1516 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_LAUNCHWRITE_EXIT6, this, r );
  1376 	return r;
  1517 	return r;
  1377 	}
  1518 	}
  1378 
  1519 
  1379 TInt DMmcMediaDriverFlash::PartitionInfo(TPartitionInfo& anInfo)
  1520 TInt DMmcMediaDriverFlash::PartitionInfo(TPartitionInfo& anInfo)
  1380 //
  1521 //
  1381 // Read the partition information for the media.  If the user supplied a password,
  1522 // Read the partition information for the media.  If the user supplied a password,
  1382 // then unlock the card before trying to read the first sector.
  1523 // then unlock the card before trying to read the first sector.
  1383 //
  1524 //
  1384 	{
  1525 	{
       
  1526 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_PARTITIONINFO_ENTRY, this );
  1385 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:rpi"));
  1527 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:rpi"));
  1386 	__ASSERT_DEBUG(CurrentRequest() == EMReqIdle, Panic(ERPIInUse));
  1528 	__ASSERT_DEBUG(CurrentRequest() == EMReqIdle, Panic(ERPIInUse));
  1387 
  1529 
  1388 	iPartitionInfo = &anInfo;
  1530 	iPartitionInfo = &anInfo;
  1389 
  1531 
  1395 		iHiddenSectors = 0; // Not used for internal media
  1537 		iHiddenSectors = 0; // Not used for internal media
  1396 		
  1538 		
  1397 		if (KErrNone == r)
  1539 		if (KErrNone == r)
  1398 			iMedReq = EMReqEMMCPtnInfo;
  1540 			iMedReq = EMReqEMMCPtnInfo;
  1399 		
  1541 		
  1400 		return(r);
  1542 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_PARTITIONINFO_EXIT1, this, r );
       
  1543 		return r;
  1401 		}
  1544 		}
  1402 	
  1545 	
  1403 	// Assume MBR will be present or is not required
  1546 	// Assume MBR will be present or is not required
  1404 	iMbrMissing = EFalse;
  1547 	iMbrMissing = EFalse;
  1405 
  1548 
  1415 		{
  1558 		{
  1416 		// If the media is locked, we present a default partition entry to the local
  1559 		// If the media is locked, we present a default partition entry to the local
  1417 		// media subsystem, which will be updated when the media is finally unlocked.
  1560 		// media subsystem, which will be updated when the media is finally unlocked.
  1418 		r = CreateDefaultPartition();
  1561 		r = CreateDefaultPartition();
  1419 		if (r != KErrNone)
  1562 		if (r != KErrNone)
       
  1563 		    {
       
  1564 			OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_PARTITIONINFO_EXIT2, this, r );
  1420 			return r;
  1565 			return r;
       
  1566 		    }
       
  1567 		
       
  1568 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_PARTITIONINFO_EXIT3, this, KErrLocked );
  1421 		return KErrLocked;
  1569 		return KErrLocked;
  1422 		}
  1570 		}
  1423 
  1571 
  1424 	// KErrNone indicates asynchronous completion
  1572 	// KErrNone indicates asynchronous completion
       
  1573 	
       
  1574 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_PARTITIONINFO_EXIT4, this, r );
  1425 	return r;
  1575 	return r;
  1426 	}
  1576 	}
  1427 
  1577 
  1428 TInt DMmcMediaDriverFlash::LaunchRPIUnlock(TLocalDrivePasswordData& aPasswordData)
  1578 TInt DMmcMediaDriverFlash::LaunchRPIUnlock(TLocalDrivePasswordData& aPasswordData)
  1429 	{
  1579 	{
       
  1580 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_LAUNCHRPIUNLOCK_ENTRY, this );
  1430 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:lru:%d,%d", iCard->IsReady(), iCard->IsLocked()));
  1581 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:lru:%d,%d", iCard->IsReady(), iCard->IsLocked()));
       
  1582 	OstTraceExt2( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHRPIUNLOCK_ICARD, "iCard->IsReady=%d; iCard->IsLocked=%d", iCard->IsReady(), iCard->IsLocked());
  1431 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
  1583 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
  1432 
  1584 
  1433 	TInt r = KErrNone;
  1585 	TInt r = KErrNone;
  1434 
  1586 
  1435 	// CMD42 is an adtc, so check state in same way as for write
  1587 	// CMD42 is an adtc, so check state in same way as for write
  1455 			r = EngageAndSetWriteRequest(EMReqUpdatePtnInfo);
  1607 			r = EngageAndSetWriteRequest(EMReqUpdatePtnInfo);
  1456 			}
  1608 			}
  1457 		}
  1609 		}
  1458 
  1610 
  1459 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lru:%d", r));
  1611 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lru:%d", r));
       
  1612 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_LAUNCHRPIUNLOCK_EXIT, this, r );
  1460 	return r;
  1613 	return r;
  1461 	}
  1614 	}
  1462 
  1615 
  1463 
  1616 
  1464 TInt DMmcMediaDriverFlash::LaunchRPIRead()
  1617 TInt DMmcMediaDriverFlash::LaunchRPIRead()
  1465 //
  1618 //
  1466 // launch read request on first KDiskSectorSize (512) bytes
  1619 // launch read request on first KDiskSectorSize (512) bytes
  1467 //
  1620 //
  1468 	{
  1621 	{
       
  1622 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_LAUNCHRPIREAD_ENTRY, this );
  1469 	__KTRACE_OPT(KPBUSDRV, Kern::Printf((">mmd:lrr")));
  1623 	__KTRACE_OPT(KPBUSDRV, Kern::Printf((">mmd:lrr")));
  1470 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
  1624 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
  1471 
  1625 
  1472 	// the partition information is read before any other area is read from /
  1626 	// the partition information is read before any other area is read from /
  1473 	// written to, and so does not need to maintain cache coherence.  Therefore
  1627 	// written to, and so does not need to maintain cache coherence.  Therefore
  1480 		iSession->SetupCIMReadBlock(0, iIntBuf);	// aBlocks = 1
  1634 		iSession->SetupCIMReadBlock(0, iIntBuf);	// aBlocks = 1
  1481 		r = EngageAndSetReadRequest(EMReqPtnInfo);
  1635 		r = EngageAndSetReadRequest(EMReqPtnInfo);
  1482 		}
  1636 		}
  1483 
  1637 
  1484 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lrr:%d", r));
  1638 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lrr:%d", r));
       
  1639 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_LAUNCHRPIREAD_EXIT, this, r );
  1485 	return r;
  1640 	return r;
  1486 	}
  1641 	}
  1487 
  1642 
  1488 
  1643 
  1489 TInt DMmcMediaDriverFlash::LaunchRPIErase()
  1644 TInt DMmcMediaDriverFlash::LaunchRPIErase()
  1490 	{
  1645 	{
       
  1646 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_LAUNCHRPIERASE_ENTRY, this );
  1491 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:lre:%d,%d", iCard->IsReady(), iCard->IsLocked()));
  1647 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:lre:%d,%d", iCard->IsReady(), iCard->IsLocked()));
  1492 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
  1648 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
  1493 
  1649 
  1494 	TInt r = KErrNone;
  1650 	TInt r = KErrNone;
  1495 
  1651 
  1501 			r = KErrAccessDenied;
  1657 			r = KErrAccessDenied;
  1502 			}
  1658 			}
  1503 		else
  1659 		else
  1504 			{
  1660 			{
  1505 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("mmd:df:EMReqForceErase"));
  1661 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("mmd:df:EMReqForceErase"));
       
  1662 			OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_LAUNCHRPIERASE_FORCE_ERASE, "Force erase");
  1506 			iMinorBuf[0] = KMMCLockUnlockErase;
  1663 			iMinorBuf[0] = KMMCLockUnlockErase;
  1507 			iSession->SetupCIMLockUnlock(1, iMinorBuf);
  1664 			iSession->SetupCIMLockUnlock(1, iMinorBuf);
  1508 			r = EngageAndSetWriteRequest(EMReqForceErase);
  1665 			r = EngageAndSetWriteRequest(EMReqForceErase);
  1509 			}
  1666 			}
  1510 		}
  1667 		}
  1511 
  1668 
  1512 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lru:%d", r));
  1669 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:lru:%d", r));
       
  1670 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_LAUNCHRPIERASE_EXIT, this, r );
  1513 	return r;
  1671 	return r;
  1514 	}
  1672 	}
  1515 
  1673 
  1516 
  1674 
  1517 TInt DMmcMediaDriverFlash::DecodePartitionInfo()
  1675 TInt DMmcMediaDriverFlash::DecodePartitionInfo()
  1518 //
  1676 //
  1519 // decode partition info that was read into internal buffer 
  1677 // decode partition info that was read into internal buffer 
  1520 //
  1678 //
  1521 	{
  1679 	{
       
  1680 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_DECODEPARTITIONINFO_ENTRY, this );
  1522 	TInt partitionCount=iPartitionInfo->iPartitionCount=0;
  1681 	TInt partitionCount=iPartitionInfo->iPartitionCount=0;
  1523 	TInt defaultPartitionNumber=-1;
  1682 	TInt defaultPartitionNumber=-1;
  1524 	TMBRPartitionEntry* pe;
  1683 	TMBRPartitionEntry* pe;
  1525 	const TUint KMBRFirstPartitionOffsetAligned = KMBRFirstPartitionOffset & ~3;
  1684 	const TUint KMBRFirstPartitionOffsetAligned = KMBRFirstPartitionOffset & ~3;
  1526 	TInt i;
  1685 	TInt i;
  1561 		// FAT partition ?
  1720 		// FAT partition ?
  1562 		else if (pe->IsValidDosPartition() || pe->IsValidFAT32Partition() || pe->IsValidExFATPartition())
  1721 		else if (pe->IsValidDosPartition() || pe->IsValidFAT32Partition() || pe->IsValidExFATPartition())
  1563 			{
  1722 			{
  1564 			SetPartitionEntry(&iPartitionInfo->iEntry[partitionCount],pe->iFirstSector,pe->iNumSectors);
  1723 			SetPartitionEntry(&iPartitionInfo->iEntry[partitionCount],pe->iFirstSector,pe->iNumSectors);
  1565 			__KTRACE_OPT(KLOCDPAGING, Kern::Printf("Mmc: FAT partition found at sector #%u", pe->iFirstSector));
  1724 			__KTRACE_OPT(KLOCDPAGING, Kern::Printf("Mmc: FAT partition found at sector #%u", pe->iFirstSector));
       
  1725 			OstTrace1(TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DECODEPARTITIONINFO_FS,"FAT partition found at sector #%u", pe->iFirstSector);
  1566 			partitionCount++;
  1726 			partitionCount++;
  1567 			}
  1727 			}
  1568 		else
  1728 		else
  1569 			{
  1729 			{
  1570 			validPartition = EFalse;
  1730 			validPartition = EFalse;
  1584 		// Check that the card address space boundary is not exceeded by the last partition
  1744 		// Check that the card address space boundary is not exceeded by the last partition
  1585 		// In case of only 1 partition in the media check also it
  1745 		// In case of only 1 partition in the media check also it
  1586 		if(part.iPartitionBaseAddr + part.iPartitionLen > deviceSize)
  1746 		if(part.iPartitionBaseAddr + part.iPartitionLen > deviceSize)
  1587 			{
  1747 			{
  1588 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: MBR partition exceeds card memory space"));
  1748 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: MBR partition exceeds card memory space"));
       
  1749 			OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DECODEPARTITIONINFO_PARTCOUNT1, "MBR partition exceeds card memory space" );
  1589 			// Adjust the partition length to card address boundary
  1750 			// Adjust the partition length to card address boundary
  1590 			part.iPartitionLen = (deviceSize - part.iPartitionBaseAddr);
  1751 			part.iPartitionLen = (deviceSize - part.iPartitionBaseAddr);
  1591 
  1752 
  1592 			// Check that the base address contained valid information
  1753 			// Check that the base address contained valid information
  1593 			if(part.iPartitionLen <= 0)
  1754 			if(part.iPartitionLen <= 0)
  1594 				{
  1755 				{
  1595 				__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: Invalid base address"));
  1756 				__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: Invalid base address"));
       
  1757 				OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DECODEPARTITIONINFO_PARTCOUNT2, "Invalid base address" );
  1596 				// Invalid MBR - assume the boot sector is in the first sector
  1758 				// Invalid MBR - assume the boot sector is in the first sector
  1597 				defaultPartitionNumber =-1; 
  1759 				defaultPartitionNumber =-1; 
  1598 				partitionCount=0;
  1760 				partitionCount=0;
  1599 				}
  1761 				}
  1600 			}
  1762 			}
  1607 				TPartitionEntry& prev = iPartitionInfo->iEntry[i-1];
  1769 				TPartitionEntry& prev = iPartitionInfo->iEntry[i-1];
  1608 				// Check if partitions overlap
  1770 				// Check if partitions overlap
  1609 				if(curr.iPartitionBaseAddr < (prev.iPartitionBaseAddr + prev.iPartitionLen))
  1771 				if(curr.iPartitionBaseAddr < (prev.iPartitionBaseAddr + prev.iPartitionLen))
  1610 					{
  1772 					{
  1611 					__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: Overlapping partitions"));
  1773 					__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: Overlapping partitions"));
       
  1774 					OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DECODEPARTITIONINFO_PARTCOUNT3, "Overlapping partitions" );
  1612 					// Adjust the partition length to not overlap the next partition
  1775 					// Adjust the partition length to not overlap the next partition
  1613 					prev.iPartitionLen = (curr.iPartitionBaseAddr - prev.iPartitionBaseAddr);
  1776 					prev.iPartitionLen = (curr.iPartitionBaseAddr - prev.iPartitionBaseAddr);
  1614 
  1777 
  1615 					// Check that the base address contained valid information
  1778 					// Check that the base address contained valid information
  1616 					if(prev.iPartitionLen <= 0)
  1779 					if(prev.iPartitionLen <= 0)
  1617 						{
  1780 						{
  1618 						__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: Invalid base address"));
  1781 						__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc: Invalid base address"));
       
  1782 						OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DECODEPARTITIONINFO_PARTCOUNT4, "Invalid base address" );
  1619 						// Invalid MBR - assume the boot sector is in the first sector
  1783 						// Invalid MBR - assume the boot sector is in the first sector
  1620 						defaultPartitionNumber=(-1); 
  1784 						defaultPartitionNumber=(-1); 
  1621 						partitionCount=0;
  1785 						partitionCount=0;
  1622 						}
  1786 						}
  1623 					}
  1787 					}
  1627 
  1791 
  1628 mbr_done:
  1792 mbr_done:
  1629 	if (defaultPartitionNumber==(-1) && partitionCount==0)
  1793 	if (defaultPartitionNumber==(-1) && partitionCount==0)
  1630 		{
  1794 		{
  1631 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc:PartitionInfo no MBR"));
  1795 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("Mmc:PartitionInfo no MBR"));
       
  1796 		OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DECODEPARTITIONINFO_MBRDONE1, "No MBR" );
  1632 		if (MBRMandatory(iCard))
  1797 		if (MBRMandatory(iCard))
  1633 			{
  1798 			{
  1634 			// If the MBR is missing AND is required, we present a default partition entry to the local
  1799 			// If the MBR is missing AND is required, we present a default partition entry to the local
  1635 			// media subsystem, which will be updated when the media is finally formatted
  1800 			// media subsystem, which will be updated when the media is finally formatted
  1636 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("MBR mandatory, defining space for MBR + default partition"));
  1801 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("MBR mandatory, defining space for MBR + default partition"));
       
  1802 			OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DECODEPARTITIONINFO_MBRDONE2, "MBR mandatory, defining space for MBR + default partition" );
  1637 			iMbrMissing = ETrue;
  1803 			iMbrMissing = ETrue;
  1638 			TInt r = CreateDefaultPartition();
  1804 			TInt r = CreateDefaultPartition();
  1639 			if (r != KErrNone)
  1805 			if (r != KErrNone)
       
  1806 			    {
       
  1807 				OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DECODEPARTITIONINFO_EXIT1, this, r );
  1640 				return r;
  1808 				return r;
       
  1809 			    }
  1641 			}
  1810 			}
  1642 		else
  1811 		else
  1643 			{
  1812 			{
  1644 			// Assume it has no MBR, and the Boot Sector is in the 1st sector
  1813 			// Assume it has no MBR, and the Boot Sector is in the 1st sector
  1645 			SetPartitionEntry(&iPartitionInfo->iEntry[0],0,I64LOW(iCard->DeviceSize64()>>KDiskSectorShift));
  1814 			SetPartitionEntry(&iPartitionInfo->iEntry[0],0,I64LOW(iCard->DeviceSize64()>>KDiskSectorShift));
  1654 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<Mmc:PartitionInfo (C:%d)",iPartitionInfo->iPartitionCount));
  1823 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<Mmc:PartitionInfo (C:%d)",iPartitionInfo->iPartitionCount));
  1655 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("     Partition1 (B:%xH L:%xH)",I64LOW(iPartitionInfo->iEntry[0].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[0].iPartitionLen)));
  1824 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("     Partition1 (B:%xH L:%xH)",I64LOW(iPartitionInfo->iEntry[0].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[0].iPartitionLen)));
  1656 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("     Partition2 (B:%xH L:%xH)",I64LOW(iPartitionInfo->iEntry[1].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[1].iPartitionLen)));
  1825 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("     Partition2 (B:%xH L:%xH)",I64LOW(iPartitionInfo->iEntry[1].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[1].iPartitionLen)));
  1657 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("     Partition3 (B:%xH L:%xH)",I64LOW(iPartitionInfo->iEntry[2].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[2].iPartitionLen)));
  1826 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("     Partition3 (B:%xH L:%xH)",I64LOW(iPartitionInfo->iEntry[2].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[2].iPartitionLen)));
  1658 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("     Partition4 (B:%xH L:%xH)",I64LOW(iPartitionInfo->iEntry[3].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[3].iPartitionLen)));
  1827 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("     Partition4 (B:%xH L:%xH)",I64LOW(iPartitionInfo->iEntry[3].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[3].iPartitionLen)));
       
  1828 	OstTraceDefExt4(OST_TRACE_CATEGORY_RND, TRACE_MMCDEBUG, DMMCMEDIADRIVERFLASH_DECODEPARTITIONINFO_PARTINFO1, "Partition1 (B:0x%x L:0x%x); Partition2 (B:0x%x L:0x%x)", I64LOW(iPartitionInfo->iEntry[0].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[0].iPartitionLen),I64LOW(iPartitionInfo->iEntry[1].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[1].iPartitionLen));
       
  1829 	OstTraceDefExt4(OST_TRACE_CATEGORY_RND, TRACE_MMCDEBUG, DMMCMEDIADRIVERFLASH_DECODEPARTITIONINFO_PARTINFO2, "Partition3 (B:0x%x L:0x%x); Partition4 (B:0x%x L:0x%x)", I64LOW(iPartitionInfo->iEntry[2].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[2].iPartitionLen),I64LOW(iPartitionInfo->iEntry[3].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[3].iPartitionLen));
  1659 
  1830 
  1660 #ifdef _DEBUG
  1831 #ifdef _DEBUG
  1661 	TMBRPartitionEntry cPe;
  1832 	TMBRPartitionEntry cPe;
  1662 	if(GetDefaultPartitionInfo(cPe) == KErrNone)
  1833 	if(GetDefaultPartitionInfo(cPe) == KErrNone)
  1663 		{
  1834 		{
  1678 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("--       iNumSectors [%08x:%08x] %c       -", pe->iNumSectors,       cPe.iNumSectors,       pe->iNumSectors       == cPe.iNumSectors       ? ' ' : 'X'));
  1849 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("--       iNumSectors [%08x:%08x] %c       -", pe->iNumSectors,       cPe.iNumSectors,       pe->iNumSectors       == cPe.iNumSectors       ? ' ' : 'X'));
  1679 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("-------------------------------------------"));
  1850 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("-------------------------------------------"));
  1680 		}
  1851 		}
  1681 #endif
  1852 #endif
  1682 
  1853 
  1683 	return(KErrNone);
  1854 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DECODEPARTITIONINFO_EXIT2, this, KErrNone );
       
  1855 	return KErrNone;
  1684 	}
  1856 	}
  1685 
  1857 
  1686 
  1858 
  1687 TInt DMmcMediaDriverFlash::WritePartitionInfo()
  1859 TInt DMmcMediaDriverFlash::WritePartitionInfo()
  1688 /**
  1860 /**
  1689 	Write the default partition table to freshly formatted media
  1861 	Write the default partition table to freshly formatted media
  1690 	@return Standard Symbian OS Error Code
  1862 	@return Standard Symbian OS Error Code
  1691  */
  1863  */
  1692 	{
  1864 	{
       
  1865 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_WRITEPARTITIONINFO_ENTRY, this );
  1693 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:wpi"));
  1866 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:wpi"));
  1694 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
  1867 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
  1695 
  1868 
  1696 	TMBRPartitionEntry partitionEntry;
  1869 	TMBRPartitionEntry partitionEntry;
  1697 	TInt err = GetDefaultPartitionInfo(partitionEntry);
  1870 	TInt err = GetDefaultPartitionInfo(partitionEntry);
  1706 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("    End Head         : %02xh", partitionEntry.iEndHead));
  1879 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("    End Head         : %02xh", partitionEntry.iEndHead));
  1707 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("    End Sector       : %02xh", partitionEntry.iEndSector));
  1880 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("    End Sector       : %02xh", partitionEntry.iEndSector));
  1708 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("    End Cyclinder    : %02xh", partitionEntry.iEndCylinder));
  1881 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("    End Cyclinder    : %02xh", partitionEntry.iEndCylinder));
  1709 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("    Relative Sector  : %08xh", partitionEntry.iFirstSector));
  1882 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("    Relative Sector  : %08xh", partitionEntry.iFirstSector));
  1710 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("    Number of Sectors: %08xh", partitionEntry.iNumSectors));
  1883 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("    Number of Sectors: %08xh", partitionEntry.iNumSectors));
  1711 
  1884 		OstTraceExt5(TRACE_MMCDEBUG, DMMCMEDIADRIVERFLASH_WRITEPARTITIONINFO_PARTINFO1, "Boot ID=0x%02x; Start Head=0x%02x; Start Sector=0x%02x; Start Cyclinder=0x%02x; System ID=0x%02x", (TUint) partitionEntry.iX86BootIndicator, (TUint) partitionEntry.iStartHead, (TUint) partitionEntry.iStartSector, (TUint) partitionEntry.iStartCylinder, (TUint) partitionEntry.iPartitionType);
       
  1885 		OstTraceExt5(TRACE_MMCDEBUG, DMMCMEDIADRIVERFLASH_WRITEPARTITIONINFO_PARTINFO2, "End Head=0x%02x; End Sector=0x%02x; End Cyclinder=0x%02x; Relative Sector=0x%08x; Number of Sectors=0x%08x", (TUint) partitionEntry.iEndHead, (TUint) partitionEntry.iEndSector, (TUint) partitionEntry.iEndCylinder, (TUint) partitionEntry.iFirstSector, (TUint) partitionEntry.iNumSectors);
  1712 		//
  1886 		//
  1713 		// Clear all other partition entries and align the partition info into the minor buffer for writing...
  1887 		// Clear all other partition entries and align the partition info into the minor buffer for writing...
  1714 		//
  1888 		//
  1715 		memclr(iMinorBuf, KDiskSectorSize);
  1889 		memclr(iMinorBuf, KDiskSectorSize);
  1716 		memcpy(&iMinorBuf[KMBRFirstPartitionEntry], &partitionEntry, sizeof(TMBRPartitionEntry));
  1890 		memcpy(&iMinorBuf[KMBRFirstPartitionEntry], &partitionEntry, sizeof(TMBRPartitionEntry));
  1727 		err = EngageAndSetWriteRequest(EMReqUpdatePtnInfo);
  1901 		err = EngageAndSetWriteRequest(EMReqUpdatePtnInfo);
  1728 		}
  1902 		}
  1729 
  1903 
  1730 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:wpi:%d", err));
  1904 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:wpi:%d", err));
  1731 
  1905 
  1732 	return(err);
  1906 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_WRITEPARTITIONINFO_EXIT, this, err );
       
  1907 	return err;
  1733 	}
  1908 	}
  1734 
  1909 
  1735 
  1910 
  1736 TInt DMmcMediaDriverFlash::CreateDefaultPartition()
  1911 TInt DMmcMediaDriverFlash::CreateDefaultPartition()
  1737 	{
  1912 	{
       
  1913 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_CREATEDEFAULTPARTITION_ENTRY, this );
  1738 	TMBRPartitionEntry defPartition;
  1914 	TMBRPartitionEntry defPartition;
  1739 	TInt r = GetDefaultPartitionInfo(defPartition);
  1915 	TInt r = GetDefaultPartitionInfo(defPartition);
  1740 	if (r == KErrNone)
  1916 	if (r == KErrNone)
  1741 		{
  1917 		{
  1742 		SetPartitionEntry(&iPartitionInfo->iEntry[0], defPartition.iFirstSector, defPartition.iNumSectors);
  1918 		SetPartitionEntry(&iPartitionInfo->iEntry[0], defPartition.iFirstSector, defPartition.iNumSectors);
  1743 		iHiddenSectors = defPartition.iFirstSector;
  1919 		iHiddenSectors = defPartition.iFirstSector;
  1744 		iPartitionInfo->iPartitionCount   = 1;
  1920 		iPartitionInfo->iPartitionCount   = 1;
  1745 		iPartitionInfo->iMediaSizeInBytes = TotalSizeInBytes();
  1921 		iPartitionInfo->iMediaSizeInBytes = TotalSizeInBytes();
  1746 		}
  1922 		}
       
  1923 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_CREATEDEFAULTPARTITION_EXIT, this, r );
  1747 	return r;
  1924 	return r;
  1748 	}
  1925 	}
  1749 
  1926 
  1750 TInt DMmcMediaDriverFlash::GetDefaultPartitionInfo(TMBRPartitionEntry& aPartitionEntry)
  1927 TInt DMmcMediaDriverFlash::GetDefaultPartitionInfo(TMBRPartitionEntry& aPartitionEntry)
  1751 /**
  1928 /**
  1754 	@return Standard Symbian OS Error Code
  1931 	@return Standard Symbian OS Error Code
  1755  */
  1932  */
  1756 	{
  1933 	{
  1757 	memclr(&aPartitionEntry, sizeof(TMBRPartitionEntry));
  1934 	memclr(&aPartitionEntry, sizeof(TMBRPartitionEntry));
  1758 	TUint16 reservedSectors; // Not used
  1935 	TUint16 reservedSectors; // Not used
  1759 	return GetMediaDefaultPartitionInfo(aPartitionEntry, reservedSectors, iCard);
  1936 	TInt r = GetMediaDefaultPartitionInfo(aPartitionEntry, reservedSectors, iCard);
       
  1937 	return r;
  1760 	}
  1938 	}
  1761 
  1939 
  1762 
  1940 
  1763 void DMmcMediaDriverFlash::SetPartitionEntry(TPartitionEntry* aEntry, TUint aFirstSector, TUint aNumSectors)
  1941 void DMmcMediaDriverFlash::SetPartitionEntry(TPartitionEntry* aEntry, TUint aFirstSector, TUint aNumSectors)
  1764 //
  1942 //
  1765 // auxiliary static function to record partition information in TPartitionEntry object
  1943 // auxiliary static function to record partition information in TPartitionEntry object
  1766 //
  1944 //
  1767 	{
  1945 	{
       
  1946 	OstTraceFunctionEntry0( DMMCMEDIADRIVERFLASH_SETPARTITIONENTRY_ENTRY );
  1768 	aEntry->iPartitionBaseAddr=aFirstSector;
  1947 	aEntry->iPartitionBaseAddr=aFirstSector;
  1769 	aEntry->iPartitionBaseAddr<<=KDiskSectorShift;
  1948 	aEntry->iPartitionBaseAddr<<=KDiskSectorShift;
  1770 	aEntry->iPartitionLen=aNumSectors;
  1949 	aEntry->iPartitionLen=aNumSectors;
  1771 	aEntry->iPartitionLen<<=KDiskSectorShift;
  1950 	aEntry->iPartitionLen<<=KDiskSectorShift;
  1772 	aEntry->iPartitionType=KPartitionTypeFAT12;	
  1951 	aEntry->iPartitionType=KPartitionTypeFAT12;	
       
  1952 	OstTraceFunctionExit0( DMMCMEDIADRIVERFLASH_SETPARTITIONENTRY_EXIT );
  1773 	}
  1953 	}
  1774 
  1954 
  1775 TInt DMmcMediaDriverFlash::DoPasswordOp()
  1955 TInt DMmcMediaDriverFlash::DoPasswordOp()
  1776 	{
  1956 	{
       
  1957 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_DOPASSWORDOP_ENTRY, this );
  1777 	// Reconstruct password data structure in our address space
  1958 	// Reconstruct password data structure in our address space
  1778 	TLocalDrivePasswordData clientData;
  1959 	TLocalDrivePasswordData clientData;
  1779 	TInt r = iCurrentReq->ReadRemoteRaw(&clientData, sizeof(TLocalDrivePasswordData));
  1960 	TInt r = iCurrentReq->ReadRemoteRaw(&clientData, sizeof(TLocalDrivePasswordData));
  1780 
  1961 
  1781 	TMediaPassword oldPassword;
  1962 	TMediaPassword oldPassword;
  1806 		
  1987 		
  1807 	// This will complete the request in the event of an error
  1988 	// This will complete the request in the event of an error
  1808 	if(r != KErrNone)
  1989 	if(r != KErrNone)
  1809 		PartitionInfoComplete(r);
  1990 		PartitionInfoComplete(r);
  1810 
  1991 
       
  1992 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_DOPASSWORDOP_EXIT, this, KErrNone );
  1811 	return KErrNone; // ensures to indicate asynchronoous completion
  1993 	return KErrNone; // ensures to indicate asynchronoous completion
  1812 	}
  1994 	}
  1813 
  1995 
  1814 void DMmcMediaDriverFlash::PasswordControl(TInt aFunc, TLocalDrivePasswordData& aData)
  1996 void DMmcMediaDriverFlash::PasswordControl(TInt aFunc, TLocalDrivePasswordData& aData)
  1815 //
  1997 //
  1817 // must be unlocked for this function.  A locked card is unlocked when it is mounted,
  1999 // must be unlocked for this function.  A locked card is unlocked when it is mounted,
  1818 // to read the partition information.  This is done from ReadPartitionInfo() and
  2000 // to read the partition information.  This is done from ReadPartitionInfo() and
  1819 // LaunchRPIUnlock().
  2001 // LaunchRPIUnlock().
  1820 //
  2002 //
  1821 	{
  2003 	{
       
  2004 	OstTraceExt2(TRACE_FLOW, DMMCMEDIADRIVERFLASH_PASSWORDCONTROL_ENTRY ,"DMmcMediaDriverFlash::PasswordControl;aFunc=%d;this=%x", aFunc, (TUint) this);
  1822 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:pc:%d", (TInt) aFunc));
  2005 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:pc:%d", (TInt) aFunc));
  1823 	__ASSERT_DEBUG(CurrentRequest() == EMReqIdle, Panic(EPCInUse));
  2006 	__ASSERT_DEBUG(CurrentRequest() == EMReqIdle, Panic(EPCInUse));
  1824 	__ASSERT_DEBUG(aFunc == DLocalDrive::EPasswordLock || aFunc == DLocalDrive::EPasswordClear, Panic(EPCFunc));
  2007 	__ASSERT_DEBUG(aFunc == DLocalDrive::EPasswordLock || aFunc == DLocalDrive::EPasswordClear, Panic(EPCFunc));
  1825 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
  2008 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
  1826 
  2009 
  1900 	// complete immediately if error occured
  2083 	// complete immediately if error occured
  1901 	if (r != KErrNone)
  2084 	if (r != KErrNone)
  1902 		CompleteRequest(r);
  2085 		CompleteRequest(r);
  1903 
  2086 
  1904 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:pc:%d", r));
  2087 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:pc:%d", r));
       
  2088 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_PASSWORDCONTROL_EXIT, this );
  1905 	}
  2089 	}
  1906 
  2090 
  1907 
  2091 
  1908 // ---- device status, callback DFC ----
  2092 // ---- device status, callback DFC ----
  1909 
  2093 
  1910 TInt DMmcMediaDriverFlash::CheckDevice(TMediaReqType aReqType)
  2094 TInt DMmcMediaDriverFlash::CheckDevice(TMediaReqType aReqType)
  1911 //
  2095 //
  1912 // Check the device before initiating a command
  2096 // Check the device before initiating a command
  1913 //
  2097 //
  1914 	{
  2098 	{
  1915 	
  2099 	OstTraceExt2(TRACE_FLOW, DMMCMEDIADRIVERFLASH_CHECKDEVICE_ENTRY, "DMmcMediaDriverFlash::CheckDevice;aReqType=%d;this=%x", (TInt) aReqType, (TUint) this);
  1916 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:cd:%d",aReqType));
  2100 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:cd:%d",aReqType));
  1917 
  2101 
  1918 	TInt r=KErrNone;
  2102 	TInt r=KErrNone;
  1919 
  2103 
  1920 	if (!iCard->IsReady())
  2104 	if (!iCard->IsReady())
  1947 	// Don't perform write/format operations on MMC ROM cards
  2131 	// Don't perform write/format operations on MMC ROM cards
  1948 	else if (iMediaType==EMultiMediaROM && aReqType == EMReqTypeNormalWr)
  2132 	else if (iMediaType==EMultiMediaROM && aReqType == EMReqTypeNormalWr)
  1949 		r=KErrAccessDenied;
  2133 		r=KErrAccessDenied;
  1950 
  2134 
  1951 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:cd:%d", r));
  2135 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:cd:%d", r));
  1952 	return(r);
  2136 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_CHECKDEVICE_EXIT, this, r );
       
  2137 	return r;
  1953 	}
  2138 	}
  1954 
  2139 
  1955 void DMmcMediaDriverFlash::SessionEndCallBack(TAny* aMediaDriver)
  2140 void DMmcMediaDriverFlash::SessionEndCallBack(TAny* aMediaDriver)
  1956 //
  2141 //
  1957 // called by EPBUS when a single session has finished.  Queues DFC to launch
  2142 // called by EPBUS when a single session has finished.  Queues DFC to launch
  1958 // next session or to complete client request.
  2143 // next session or to complete client request.
  1959 //
  2144 //
  1960 	{
  2145 	{
       
  2146 	OstTraceFunctionEntry0( DMMCMEDIADRIVERFLASH_SESSIONENDCALLBACK_ENTRY );
  1961 	DMmcMediaDriverFlash& md = *static_cast<DMmcMediaDriverFlash*>(aMediaDriver);
  2147 	DMmcMediaDriverFlash& md = *static_cast<DMmcMediaDriverFlash*>(aMediaDriver);
  1962 	__ASSERT_DEBUG(! md.iSessionEndDfc.Queued(), Panic(ESECBQueued));
  2148 	__ASSERT_DEBUG(! md.iSessionEndDfc.Queued(), Panic(ESECBQueued));
  1963 	md.iSessionEndDfc.Enque();
  2149 	md.iSessionEndDfc.Enque();
       
  2150 	OstTraceFunctionExit0( DMMCMEDIADRIVERFLASH_SESSIONENDCALLBACK_EXIT );
  1964 	}
  2151 	}
  1965 
  2152 
  1966 
  2153 
  1967 void DMmcMediaDriverFlash::SessionEndDfc(TAny* aMediaDriver)
  2154 void DMmcMediaDriverFlash::SessionEndDfc(TAny* aMediaDriver)
  1968 	{
  2155 	{
  1973 void DMmcMediaDriverFlash::DoSessionEndDfc()
  2160 void DMmcMediaDriverFlash::DoSessionEndDfc()
  1974 //
  2161 //
  1975 // launch next session or complete client request
  2162 // launch next session or complete client request
  1976 //
  2163 //
  1977 	{
  2164 	{
       
  2165 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_DOSESSIONENDDFC_ENTRY, this );
  1978 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:dsed:%d", CurrentRequest()));
  2166 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:dsed:%d", CurrentRequest()));
       
  2167 	OstTrace1( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DOSESSIONENDDFC_REQUEST, "Current Request=%d", CurrentRequest());
  1979 
  2168 
  1980 	TInt r=KErrNone;
  2169 	TInt r=KErrNone;
  1981 
  2170 
  1982 	EndInCritical();
  2171 	EndInCritical();
  1983 
  2172 
  2215 		{
  2404 		{
  2216 		if (r != KErrNone)
  2405 		if (r != KErrNone)
  2217 			InvalidateCache();
  2406 			InvalidateCache();
  2218 
  2407 
  2219 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mdf:dsed:cmp:%d", r));
  2408 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mdf:dsed:cmp:%d", r));
       
  2409 		OstTrace1( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DOSESSIONENDDFC_COMPLETE, "Complete request; retval=%d", r);
  2220 		CompleteRequest(r);
  2410 		CompleteRequest(r);
  2221 		}
  2411 		}
  2222 	else
  2412 	else
  2223 		{
  2413 		{
  2224 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mdf:dsed:ncmp"));
  2414 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mdf:dsed:ncmp"));
  2225 		}
  2415 		OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_DOSESSIONENDDFC_NOT_COMPLETE, "Request not complete");
       
  2416 		}
       
  2417 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_DOSESSIONENDDFC_EXIT, this );
  2226 	}
  2418 	}
  2227 
  2419 
  2228 void DMmcMediaDriverFlash::DataTransferCallBack(TAny* aMediaDriver)
  2420 void DMmcMediaDriverFlash::DataTransferCallBack(TAny* aMediaDriver)
  2229 	{
  2421 	{
       
  2422 	OstTraceFunctionEntry0( DMMCMEDIADRIVERFLASH_DATATRANSFERCALLBACK_ENTRY );
  2230 	DMmcMediaDriverFlash& md = *static_cast<DMmcMediaDriverFlash*>(aMediaDriver);
  2423 	DMmcMediaDriverFlash& md = *static_cast<DMmcMediaDriverFlash*>(aMediaDriver);
  2231 	__ASSERT_DEBUG(! md.iDataTransferCallBackDfc.Queued(), Panic(EDBCBQueued));
  2424 	__ASSERT_DEBUG(! md.iDataTransferCallBackDfc.Queued(), Panic(EDBCBQueued));
  2232 	md.iDataTransferCallBackDfc.Enque();
  2425 	md.iDataTransferCallBackDfc.Enque();
       
  2426 	OstTraceFunctionExit0( DMMCMEDIADRIVERFLASH_DATATRANSFERCALLBACK_EXIT );
  2233 	}
  2427 	}
  2234 
  2428 
  2235 void DMmcMediaDriverFlash::DataTransferCallBackDfc(TAny* aMediaDriver)
  2429 void DMmcMediaDriverFlash::DataTransferCallBackDfc(TAny* aMediaDriver)
  2236 	{
  2430 	{
       
  2431 	OstTraceFunctionEntry0( DMMCMEDIADRIVERFLASH_DATATRANSFERCALLBACKDFC_ENTRY );
  2237 	DMmcMediaDriverFlash& md = *static_cast<DMmcMediaDriverFlash*>(aMediaDriver);
  2432 	DMmcMediaDriverFlash& md = *static_cast<DMmcMediaDriverFlash*>(aMediaDriver);
  2238 
  2433 
  2239 	if (md.iDoPhysicalAddress)
  2434 	if (md.iDoPhysicalAddress)
  2240 		{
  2435 		{
  2241 		if(md.CurrentRequest() == EMReqWrite)
  2436 		if(md.CurrentRequest() == EMReqWrite)
  2256 		else
  2451 		else
  2257 			{
  2452 			{
  2258 			md.DoReadDataTransferCallBack();
  2453 			md.DoReadDataTransferCallBack();
  2259 			}
  2454 			}
  2260 		}
  2455 		}
       
  2456 	OstTraceFunctionExit0( DMMCMEDIADRIVERFLASH_DATATRANSFERCALLBACKDFC_EXIT );
  2261 	}
  2457 	}
  2262 
  2458 
  2263 void DMmcMediaDriverFlash::DoPhysWriteDataTransferCallBack()
  2459 void DMmcMediaDriverFlash::DoPhysWriteDataTransferCallBack()
  2264 	{
  2460 	{
       
  2461 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_DOPHYSWRITEDATATRANSFERCALLBACK_ENTRY, this );
  2265 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("++DMmcMediaDriverFlash::DoPhysWriteDataTransferCallBack()"));
  2462 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("++DMmcMediaDriverFlash::DoPhysWriteDataTransferCallBack()"));
  2266 
  2463 
  2267 	TInt err = KErrNone;
  2464 	TInt err = KErrNone;
  2268 		
  2465 		
  2269 	if ( (iRdROB & KIPCSetup) || ((iReqEnd - iPhysEnd) < iBlkLen) )
  2466 	if ( (iRdROB & KIPCSetup) || ((iReqEnd - iPhysEnd) < iBlkLen) )
  2291 		iSession->MoreDataAvailable( (TInt)(iBlkLen >> KDiskSectorShift), (TUint8*)Epoc::LinearToPhysical((TLinAddr) iIntBuf), err);			
  2488 		iSession->MoreDataAvailable( (TInt)(iBlkLen >> KDiskSectorShift), (TUint8*)Epoc::LinearToPhysical((TLinAddr) iIntBuf), err);			
  2292 #else
  2489 #else
  2293 		iSession->MoreDataAvailable( (TInt)(iBlkLen >> KDiskSectorShift), iIntBuf, err);
  2490 		iSession->MoreDataAvailable( (TInt)(iBlkLen >> KDiskSectorShift), iIntBuf, err);
  2294 #endif
  2491 #endif
  2295 		__KTRACE_OPT(KPBUSDRV, 	Kern::Printf("--iDoPhysicalAddress(KIPCSetup)"));
  2492 		__KTRACE_OPT(KPBUSDRV, 	Kern::Printf("--iDoPhysicalAddress(KIPCSetup)"));
       
  2493 		OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_DOPHYSWRITEDATATRANSFERCALLBACK_EXIT1, this );
  2296 		return;
  2494 		return;
  2297 		}
  2495 		}
  2298 	
  2496 	
  2299 	PrepareNextPhysicalFragment();
  2497 	PrepareNextPhysicalFragment();
  2300 
  2498 
  2301 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("--DMmcMediaDriverFlash::DoPhysWriteDataTransferCallBack()"));
  2499 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("--DMmcMediaDriverFlash::DoPhysWriteDataTransferCallBack()"));
       
  2500 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_DOPHYSWRITEDATATRANSFERCALLBACK_EXIT2, this );
  2302 	}
  2501 	}
  2303 
  2502 
  2304 
  2503 
  2305 void DMmcMediaDriverFlash::DoPhysReadDataTransferCallBack()
  2504 void DMmcMediaDriverFlash::DoPhysReadDataTransferCallBack()
  2306 	{
  2505 	{
       
  2506 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_DOPHYSREADDATATRANSFERCALLBACK_ENTRY, this );
  2307 	__KTRACE_OPT(KPBUSDRV, 	Kern::Printf("++DMmcMediaDriverFlash::DoPhysReadTransferCallBack()"));
  2507 	__KTRACE_OPT(KPBUSDRV, 	Kern::Printf("++DMmcMediaDriverFlash::DoPhysReadTransferCallBack()"));
  2308 
  2508 
  2309 	TInt err = KErrNone;
  2509 	TInt err = KErrNone;
  2310 	
  2510 	
  2311 	if ((iRdROB & KIPCWrite) && !iSecondBuffer)
  2511 	if ((iRdROB & KIPCWrite) && !iSecondBuffer)
  2346 #else
  2546 #else
  2347 		iSession->MoreDataAvailable( (TInt)(iIPCLen  >> KDiskSectorShift), iIntBuf, err);			
  2547 		iSession->MoreDataAvailable( (TInt)(iIPCLen  >> KDiskSectorShift), iIntBuf, err);			
  2348 #endif
  2548 #endif
  2349 		iSecondBuffer = ETrue;
  2549 		iSecondBuffer = ETrue;
  2350 		__KTRACE_OPT(KPBUSDRV, 	Kern::Printf("--iDoPhysicalAddress(KIPCWrite)"));
  2550 		__KTRACE_OPT(KPBUSDRV, 	Kern::Printf("--iDoPhysicalAddress(KIPCWrite)"));
       
  2551 		OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_DOPHYSREADDATATRANSFERCALLBACK_EXIT1, this );
  2351 		return;
  2552 		return;
  2352 		}
  2553 		}
  2353 
  2554 
  2354 	PrepareNextPhysicalFragment();
  2555 	PrepareNextPhysicalFragment();
  2355 
  2556 
  2356 	__KTRACE_OPT(KPBUSDRV, 	Kern::Printf("--DMmcMediaDriverFlash::DoPhysReadTransferCallBack()"));
  2557 	__KTRACE_OPT(KPBUSDRV, 	Kern::Printf("--DMmcMediaDriverFlash::DoPhysReadTransferCallBack()"));
       
  2558 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_DOPHYSREADDATATRANSFERCALLBACK_EXIT2, this );
  2357 	}
  2559 	}
  2358 
  2560 
  2359 void DMmcMediaDriverFlash::DoWriteDataTransferCallBack()
  2561 void DMmcMediaDriverFlash::DoWriteDataTransferCallBack()
  2360 	{
  2562 	{
       
  2563 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_DOWRITEDATATRANSFERCALLBACK_ENTRY, this );
  2361 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("++DMmcMediaDriverFlash::DoWriteDataTransferCallBack()"));
  2564 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("++DMmcMediaDriverFlash::DoWriteDataTransferCallBack()"));
  2362 
  2565 
  2363 	TInt err = KErrNone;
  2566 	TInt err = KErrNone;
  2364 	
  2567 	
  2365 	// Advance current request progress...
  2568 	// Advance current request progress...
  2420 	// ...and signal that data is available to the PSL.
  2623 	// ...and signal that data is available to the PSL.
  2421 	//
  2624 	//
  2422 	iSession->MoreDataAvailable(numBlocks, bufPtr, err);
  2625 	iSession->MoreDataAvailable(numBlocks, bufPtr, err);
  2423 
  2626 
  2424 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("--DMmcMediaDriverFlash::DoWriteDataTransferCallBack()"));
  2627 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("--DMmcMediaDriverFlash::DoWriteDataTransferCallBack()"));
       
  2628 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_DOWRITEDATATRANSFERCALLBACK_EXIT, this );
  2425 	}
  2629 	}
  2426 
  2630 
  2427 
  2631 
  2428 void DMmcMediaDriverFlash::DoReadDataTransferCallBack()
  2632 void DMmcMediaDriverFlash::DoReadDataTransferCallBack()
  2429 	{
  2633 	{
       
  2634 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_DOREADDATATRANSFERCALLBACK_ENTRY, this );
  2430 	__KTRACE_OPT(KPBUSDRV, 	Kern::Printf("++DMmcMediaDriverFlash::DoReadTransferCallBack()"));
  2635 	__KTRACE_OPT(KPBUSDRV, 	Kern::Printf("++DMmcMediaDriverFlash::DoReadTransferCallBack()"));
  2431 
  2636 
  2432 	TInt err = KErrNone;
  2637 	TInt err = KErrNone;
  2433 	
  2638 	
  2434 	const TUint32 doubleBufferSize = iMaxBufSize >> 1;
  2639 	const TUint32 doubleBufferSize = iMaxBufSize >> 1;
  2447 			TUint8* bufPtr = iIntBuf + doubleBufferSize;
  2652 			TUint8* bufPtr = iIntBuf + doubleBufferSize;
  2448 
  2653 
  2449 			iSecondBuffer = EFalse;
  2654 			iSecondBuffer = EFalse;
  2450 
  2655 
  2451 			iSession->MoreDataAvailable(numBlocks, bufPtr, KErrNone);
  2656 			iSession->MoreDataAvailable(numBlocks, bufPtr, KErrNone);
       
  2657 			OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_DOREADDATATRANSFERCALLBACK_EXIT1, this );
  2452 			return;
  2658 			return;
  2453 			}
  2659 			}
  2454 		else
  2660 		else
  2455 			{
  2661 			{
  2456 			//
  2662 			//
  2487 	iSecondBuffer = iSecondBuffer ? (TBool)EFalse : (TBool)ETrue;
  2693 	iSecondBuffer = iSecondBuffer ? (TBool)EFalse : (TBool)ETrue;
  2488 
  2694 
  2489 	iSession->MoreDataAvailable(numBlocks, bufPtr, err);
  2695 	iSession->MoreDataAvailable(numBlocks, bufPtr, err);
  2490 
  2696 
  2491 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("--DMmcMediaDriverFlash::DoDataTransferCallBack()"));
  2697 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("--DMmcMediaDriverFlash::DoDataTransferCallBack()"));
       
  2698 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_DOREADDATATRANSFERCALLBACK_EXIT2, this );
  2492 	}
  2699 	}
  2493 
  2700 
  2494 
  2701 
  2495 // ---- request management ----
  2702 // ---- request management ----
  2496 
  2703 
  2497 
  2704 
  2498 TInt DMmcMediaDriverFlash::EngageAndSetReadRequest(DMmcMediaDriverFlash::TMediaRequest aRequest)
  2705 TInt DMmcMediaDriverFlash::EngageAndSetReadRequest(DMmcMediaDriverFlash::TMediaRequest aRequest)
  2499 	{
  2706 	{
  2500 	return EngageAndSetRequest(aRequest, iReadCurrentInMilliAmps);
  2707 	OstTraceExt2(TRACE_FLOW, DMMCMEDIADRIVERFLASH_ENGAGEANDSETREADREQUEST_ENTRY, "DMmcMediaDriverFlash::EngageAndSetReadRequest;aRequest=%d;this=%x", (TInt) aRequest, (TUint) this);
       
  2708 	TInt r = EngageAndSetRequest(aRequest, iReadCurrentInMilliAmps);
       
  2709 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_ENGAGEANDSETREADREQUEST_EXIT, this, r );
       
  2710 	return r;
  2501 	}
  2711 	}
  2502 
  2712 
  2503 
  2713 
  2504 TInt DMmcMediaDriverFlash::EngageAndSetWriteRequest(DMmcMediaDriverFlash::TMediaRequest aRequest)
  2714 TInt DMmcMediaDriverFlash::EngageAndSetWriteRequest(DMmcMediaDriverFlash::TMediaRequest aRequest)
  2505 	{
  2715 	{
  2506 	return EngageAndSetRequest(aRequest, iWriteCurrentInMilliAmps);
  2716 	OstTraceExt2(TRACE_FLOW, DMMCMEDIADRIVERFLASH_ENGAGEANDSETWRITEREQUEST_ENTRY, "DMmcMediaDriverFlash::EngageAndSetReadRequest;aRequest=%d;this=%x", (TInt) aRequest, (TUint) this);
       
  2717 	TInt r = EngageAndSetRequest(aRequest, iWriteCurrentInMilliAmps);
       
  2718 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_ENGAGEANDSETWRITEREQUEST_EXIT, this, r );
       
  2719 	return r;
  2507 	}
  2720 	}
  2508 
  2721 
  2509 
  2722 
  2510 TInt DMmcMediaDriverFlash::EngageAndSetRequest(DMmcMediaDriverFlash::TMediaRequest aRequest, TInt aCurrent)
  2723 TInt DMmcMediaDriverFlash::EngageAndSetRequest(DMmcMediaDriverFlash::TMediaRequest aRequest, TInt aCurrent)
  2511 //
  2724 //
  2512 // In WINS, all of the processing, including the callbacks, is done when Engage() is called,
  2725 // In WINS, all of the processing, including the callbacks, is done when Engage() is called,
  2513 // so the request value must be set up in advanced.  Both the request and the current are
  2726 // so the request value must be set up in advanced.  Both the request and the current are
  2514 // cleared in the corresponding call to CompleteRequest().
  2727 // cleared in the corresponding call to CompleteRequest().
  2515 //
  2728 //
  2516 	{
  2729 	{
       
  2730 	OstTraceExt3(TRACE_FLOW, DMMCMEDIADRIVERFLASH_ENGAGEANDSETREQUEST_ENTRY, "DMmcMediaDriverFlash::EngageAndSetRequest;aRequest=%d;aCurrent=%d;this=%x", (TInt) aRequest, aCurrent, (TUint) this);
  2517 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
  2731 	__ASSERT_DEBUG(iSession != NULL, Panic(ECFSessPtrNull));
  2518 
  2732 
  2519 	iMedReq = aRequest;
  2733 	iMedReq = aRequest;
  2520 	SetCurrentConsumption(aCurrent);
  2734 	SetCurrentConsumption(aCurrent);
  2521 
  2735 
  2532 		if (!iCard->IsReady())
  2746 		if (!iCard->IsReady())
  2533 			r=KErrNotReady; // If media change - return not ready rather than anything else.
  2747 			r=KErrNotReady; // If media change - return not ready rather than anything else.
  2534 		EndInCritical();
  2748 		EndInCritical();
  2535 		}
  2749 		}
  2536 
  2750 
       
  2751 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_ENGAGEANDSETREQUEST_EXIT, this, r );
  2537 	return r;
  2752 	return r;
  2538 	}
  2753 	}
  2539 
  2754 
  2540 
  2755 
  2541 void DMmcMediaDriverFlash::CompleteRequest(TInt aReason)
  2756 void DMmcMediaDriverFlash::CompleteRequest(TInt aReason)
  2542 //
  2757 //
  2543 // completes the specified request
  2758 // completes the specified request
  2544 //
  2759 //
  2545 	{
  2760 	{
       
  2761 	OstTraceFunctionEntryExt( DMMCMEDIADRIVERFLASH_COMPLETEREQUEST_ENTRY, this );
  2546 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:cr0x%08x,%d", iCurrentReq, aReason));
  2762 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:cr0x%08x,%d", iCurrentReq, aReason));
  2547 	
  2763 	
  2548 	iMedReq = EMReqIdle;
  2764 	iMedReq = EMReqIdle;
  2549 	SetCurrentConsumption(KIdleCurrentInMilliAmps);
  2765 	SetCurrentConsumption(KIdleCurrentInMilliAmps);
  2550 
  2766 
  2557 #endif		// __TEST_PAGING_MEDIA_DRIVER__
  2773 #endif		// __TEST_PAGING_MEDIA_DRIVER__
  2558 #endif		// __DEMAND_PAGING__
  2774 #endif		// __DEMAND_PAGING__
  2559 		iCurrentReq=NULL;
  2775 		iCurrentReq=NULL;
  2560 		DMediaDriver::Complete(*pR,aReason);
  2776 		DMediaDriver::Complete(*pR,aReason);
  2561 		}
  2777 		}
       
  2778 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_COMPLETEREQUEST_EXIT, this );
  2562 	}
  2779 	}
  2563 
  2780 
  2564 TInt DMmcMediaDriverFlash::Caps(TLocDrv& aDrive, TLocalDriveCapsV6& aInfo)
  2781 TInt DMmcMediaDriverFlash::Caps(TLocDrv& aDrive, TLocalDriveCapsV6& aInfo)
  2565 	{
  2782 	{
       
  2783 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_CAPS_ENTRY, this );
  2566 	// Fill buffer with current media caps.
  2784 	// Fill buffer with current media caps.
  2567 	aInfo.iType = EMediaHardDisk;
  2785 	aInfo.iType = EMediaHardDisk;
  2568 	aInfo.iConnectionBusType = EConnectionBusInternal;
  2786 	aInfo.iConnectionBusType = EConnectionBusInternal;
  2569 	aInfo.iDriveAtt = KDriveAttLocal;
  2787 	aInfo.iDriveAtt = KDriveAttLocal;
  2570 	aInfo.iMediaAtt	= KMediaAttFormattable;
  2788 	aInfo.iMediaAtt	= KMediaAttFormattable;
  2588 		{
  2806 		{
  2589 		TUint16 reservedSectors;
  2807 		TUint16 reservedSectors;
  2590 		TMBRPartitionEntry dummy;	// Not used here
  2808 		TMBRPartitionEntry dummy;	// Not used here
  2591 		const TInt r = GetMediaDefaultPartitionInfo(dummy, reservedSectors, iCard);
  2809 		const TInt r = GetMediaDefaultPartitionInfo(dummy, reservedSectors, iCard);
  2592 		if(r != KErrNone)
  2810 		if(r != KErrNone)
       
  2811 		    {
       
  2812 			OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_CAPS_EXIT1, this, r );
  2593 			return r;
  2813 			return r;
       
  2814 		    }
  2594 
  2815 
  2595 		aInfo.iFormatInfo.iReservedSectors = reservedSectors;
  2816 		aInfo.iFormatInfo.iReservedSectors = reservedSectors;
  2596 		aInfo.iExtraInfo = ETrue;
  2817 		aInfo.iExtraInfo = ETrue;
  2597 		}
  2818 		}
  2598 
  2819 
  2651 		aInfo.iMediaAtt&= ~KMediaAttFormattable;
  2872 		aInfo.iMediaAtt&= ~KMediaAttFormattable;
  2652 		}
  2873 		}
  2653 	
  2874 	
  2654 	// Must return KErrCompletion to indicate that this 
  2875 	// Must return KErrCompletion to indicate that this 
  2655 	// is a synchronous version of the function
  2876 	// is a synchronous version of the function
       
  2877 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_CAPS_EXIT2, this, KErrCompletion );
  2656 	return KErrCompletion;
  2878 	return KErrCompletion;
  2657 	}
  2879 	}
  2658 
  2880 
  2659 
  2881 
  2660 // ---- cache ----
  2882 // ---- cache ----
  2668 // return value is not KErrNone.
  2890 // return value is not KErrNone.
  2669 //
  2891 //
  2670 // This function is linear in the number of blocks in the cache.
  2892 // This function is linear in the number of blocks in the cache.
  2671 //
  2893 //
  2672 	{
  2894 	{
       
  2895 	OstTraceFunctionEntryExt( DMMCMEDIADRIVERFLASH_READDATAUNTILCACHEEXHAUSTED_ENTRY, this );
  2673 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:rdc:%x,%x", iReqCur, iReqEnd));
  2896 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:rdc:%x,%x", iReqCur, iReqEnd));
       
  2897 	OstTraceExt2( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_READDATAUNTILCACHEEXHAUSTED, "iReqCur=0x%x; iReqEnd=0x%x", (TUint) iReqCur, (TUint) iReqEnd );
  2674 	
  2898 	
  2675 	if ( iCurrentReq->IsPhysicalAddress()
  2899 	if ( iCurrentReq->IsPhysicalAddress()
  2676 #if defined(__DEMAND_PAGING__) && !defined(__WINS__)
  2900 #if defined(__DEMAND_PAGING__) && !defined(__WINS__)
  2677 	     || DMediaPagingDevice::PageInRequest(*iCurrentReq)
  2901 	     || DMediaPagingDevice::PageInRequest(*iCurrentReq)
  2678 #endif //DEMAND_PAGING 
  2902 #endif //DEMAND_PAGING 
  2679         )
  2903         )
  2680 		{
  2904 		{
  2681 		*aAllDone = EFalse;
  2905 		*aAllDone = EFalse;
       
  2906 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_READDATAUNTILCACHEEXHAUSTED_EXIT1, this, KErrNone );
  2682 		return KErrNone;
  2907 		return KErrNone;
  2683 		}
  2908 		}
  2684 	
  2909 	
  2685 	TInt64 physStart = iReqCur & ~iBlkMsk;
  2910 	TInt64 physStart = iReqCur & ~iBlkMsk;
  2686 	TInt64 physEnd = Min(physStart + iMaxBufSize, (iReqEnd + iBlkMsk) & ~iBlkMsk);
  2911 	TInt64 physEnd = Min(physStart + iMaxBufSize, (iReqEnd + iBlkMsk) & ~iBlkMsk);
  2708 		}
  2933 		}
  2709 
  2934 
  2710 	*aAllDone = (iReqCur >= iReqEnd);
  2935 	*aAllDone = (iReqCur >= iReqEnd);
  2711 
  2936 
  2712 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:rdc:%d,%d", *aAllDone, r));
  2937 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:rdc:%d,%d", *aAllDone, r));
       
  2938 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_READDATAUNTILCACHEEXHAUSTED_EXIT2, this, r );
  2713 	return r;
  2939 	return r;
  2714 	}
  2940 	}
  2715 
  2941 
  2716 
  2942 
  2717 TInt DMmcMediaDriverFlash::WriteDataToUser(TUint8* aBufPtr)
  2943 TInt DMmcMediaDriverFlash::WriteDataToUser(TUint8* aBufPtr)
  2718 //
  2944 //
  2719 // write the data from the most recent read operation to the user descriptor
  2945 // write the data from the most recent read operation to the user descriptor
  2720 //
  2946 //
  2721 	{
  2947 	{
       
  2948 	OstTraceFunctionEntryExt( DMMCMEDIADRIVERFLASH_WRITEDATATOUSER_ENTRY, this );
  2722 	TInt r = KErrNotSupported;
  2949 	TInt r = KErrNotSupported;
  2723 
  2950 
  2724 	// get range of data to read out of internal buffer
  2951 	// get range of data to read out of internal buffer
  2725 
  2952 
  2726 	TInt len = I64LOW(UMin(iPhysEnd, iReqEnd) - iReqCur);
  2953 	TInt len = I64LOW(UMin(iPhysEnd, iReqEnd) - iReqCur);
  2727 	TPtrC8 extrView(aBufPtr, len);
  2954 	TPtrC8 extrView(aBufPtr, len);
  2728 
  2955 
  2729 	// write data from internal buffer
  2956 	// write data from internal buffer
  2730 	TUint usrOfst = I64LOW(iReqCur - iReqStart);
  2957 	TUint usrOfst = I64LOW(iReqCur - iReqStart);
  2731 
  2958 
       
  2959 	OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_WRITEDATATOUSER_LATENCY1, "Begin writing user data" );
  2732 #if defined(__DEMAND_PAGING__) && !defined(__WINS__)
  2960 #if defined(__DEMAND_PAGING__) && !defined(__WINS__)
  2733 	if (DMediaPagingDevice::PageInRequest(*iCurrentReq))
  2961 	if (DMediaPagingDevice::PageInRequest(*iCurrentReq))
  2734 		r=iCurrentReq->WriteToPageHandler((TUint8 *)(&extrView[0]), len, usrOfst);
  2962 		r=iCurrentReq->WriteToPageHandler((TUint8 *)(&extrView[0]), len, usrOfst);
  2735 	else
  2963 	else
  2736 #endif	// __DEMAND_PAGING__
  2964 #endif	// __DEMAND_PAGING__
  2737 		r = iCurrentReq->WriteRemote(&extrView,usrOfst);
  2965 		r = iCurrentReq->WriteRemote(&extrView,usrOfst);
  2738 
  2966 	
       
  2967 	OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_WRITEDATATOUSER_LATENCY2, "End writing user data" );
       
  2968 
       
  2969 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_WRITEDATATOUSER_EXIT, this, r );
  2739 	return r;
  2970 	return r;
  2740 	}
  2971 	}
  2741 
  2972 
  2742 TInt DMmcMediaDriverFlash::ReadDataFromUser(TDes8& aDes, TInt aOffset)
  2973 TInt DMmcMediaDriverFlash::ReadDataFromUser(TDes8& aDes, TInt aOffset)
  2743 	{
  2974 	{
       
  2975 	OstTraceExt2(TRACE_FLOW, DMMCMEDIADRIVERFLASH_READDATAFROMUSER_ENTRY ,"DMmcMediaDriverFlash::ReadDataFromUser;aOffset=%d;this=%x", aOffset, (TUint) this);
       
  2976 	TInt r = KErrNotSupported;
  2744 #ifndef __WINS__
  2977 #ifndef __WINS__
  2745 	if (DMediaPagingDevice::PageOutRequest(*iCurrentReq))
  2978 	if (DMediaPagingDevice::PageOutRequest(*iCurrentReq))
  2746 		return iCurrentReq->ReadFromPageHandler((TAny*) aDes.Ptr(), aDes.MaxLength(), aOffset);
  2979 	    {
       
  2980 		r = iCurrentReq->ReadFromPageHandler((TAny*) aDes.Ptr(), aDes.MaxLength(), aOffset);
       
  2981 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_READDATAFROMUSER_EXIT1, this, r );
       
  2982 		return r;
       
  2983 	    }
  2747 	else
  2984 	else
  2748 #endif // #ifndef __WINS__
  2985 #endif // #ifndef __WINS__
  2749 		return iCurrentReq->ReadRemote(&aDes, aOffset);
  2986 		r = iCurrentReq->ReadRemote(&aDes, aOffset);
       
  2987 	
       
  2988 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_READDATAFROMUSER_EXIT2, this, r );
       
  2989 	return r;
  2750 	}
  2990 	}
  2751 
  2991 
  2752 TInt DMmcMediaDriverFlash::AdjustPhysicalFragment(TPhysAddr &aPhysAddr, TInt &aPhysLength)
  2992 TInt DMmcMediaDriverFlash::AdjustPhysicalFragment(TPhysAddr &aPhysAddr, TInt &aPhysLength)
  2753 //
  2993 //
  2754 // Retrieve next Physical memory fragment and adjust the start pointer and length with
  2994 // Retrieve next Physical memory fragment and adjust the start pointer and length with
  2755 // respect to the set offset {iFragOfset}.
  2995 // respect to the set offset {iFragOfset}.
  2756 // Note the offset may encompass multiple memory fragments.
  2996 // Note the offset may encompass multiple memory fragments.
  2757 //
  2997 //
  2758 	{
  2998 	{
       
  2999 	OstTraceExt3(TRACE_FLOW, DMMCMEDIADRIVERFLASH_ADJUSTPHYSICALFRAGMENT_ENTRY, "DMmcMediaDriverFlash::AdjustPhysicalFragment;aPhysAddr=%x;aPhysLength=%d;this=%x", (TUint) aPhysAddr, aPhysLength, (TUint) this);
  2759 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:APF"));
  3000 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:APF"));
  2760 	
  3001 	
  2761 	TInt err = KErrNone;
  3002 	TInt err = KErrNone;
  2762 	TInt offset = iFragOfset;
  3003 	TInt offset = iFragOfset;
  2763 
  3004 
  2764 	do 
  3005 	do 
  2765 		{				
  3006 		{				
  2766 		err = iCurrentReq->GetNextPhysicalAddress(aPhysAddr, aPhysLength);
  3007 		err = iCurrentReq->GetNextPhysicalAddress(aPhysAddr, aPhysLength);
  2767 
  3008 
  2768 		if (err != KErrNone)
  3009 		if (err != KErrNone)
       
  3010 		    {
       
  3011 			OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_ADJUSTPHYSICALFRAGMENT_EXIT1, this, err );
  2769 			return err;
  3012 			return err;
       
  3013 		    }
  2770 		
  3014 		
  2771 		if (offset >= aPhysLength) // more offset than in this physical chunk
  3015 		if (offset >= aPhysLength) // more offset than in this physical chunk
  2772 			{
  3016 			{
  2773 			offset -= aPhysLength;
  3017 			offset -= aPhysLength;
  2774 			}
  3018 			}
  2786 	
  3030 	
  2787 	iFragOfset = 0; // reset offset now complete
  3031 	iFragOfset = 0; // reset offset now complete
  2788 	
  3032 	
  2789 	if (aPhysAddr == 0)
  3033 	if (aPhysAddr == 0)
  2790 		{
  3034 		{
       
  3035 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_ADJUSTPHYSICALFRAGMENT_EXIT2, this, KErrNoMemory );
  2791 		return KErrNoMemory;
  3036 		return KErrNoMemory;
  2792 		}
  3037 		}
  2793 
  3038 
  2794 #ifdef _DEBUG
  3039 #ifdef _DEBUG
  2795 	// DMAHelper ensures memory is dma aligned
  3040 	// DMAHelper ensures memory is dma aligned
  2796 	if ( (aPhysAddr & (iSocket->DmaAlignment()-1) ) )
  3041 	if ( (aPhysAddr & (iSocket->DmaAlignment()-1) ) )
  2797 		{
  3042 		{
  2798 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("mmd:lr:Memory Fragment Not Word Aligned!"));
  3043 		__KTRACE_OPT(KPBUSDRV, Kern::Printf("mmd:lr:Memory Fragment Not Word Aligned!"));
       
  3044 		OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_ADJUSTPHYSICALFRAGMENT_DMA, "Memory fragment not word aligned");
  2799 		Panic(ENotDMAAligned);
  3045 		Panic(ENotDMAAligned);
  2800 		}
  3046 		}
  2801 #endif	//_DEBUG
  3047 #endif	//_DEBUG
  2802 	
  3048 	
  2803 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:APF physAddr(0x%x), physLength(%d)",aPhysAddr, aPhysLength));
  3049 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:APF physAddr(0x%x), physLength(%d)",aPhysAddr, aPhysLength));
       
  3050 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_ADJUSTPHYSICALFRAGMENT_EXIT3, this, err );
  2804 	return err;
  3051 	return err;
  2805 	}
  3052 	}
  2806 
  3053 
  2807 TInt DMmcMediaDriverFlash::PrepareFirstPhysicalFragment(TPhysAddr &aPhysAddr, TInt &aPhysLength, TUint32 aLength)
  3054 TInt DMmcMediaDriverFlash::PrepareFirstPhysicalFragment(TPhysAddr &aPhysAddr, TInt &aPhysLength, TUint32 aLength)
  2808 //
  3055 //
  2809 // Retrieves the first Physical memory fragment and determines the type of the next transfer
  3056 // Retrieves the first Physical memory fragment and determines the type of the next transfer
  2810 // Next transfer may either be the last block (end not block aligned) or a block may straddle
  3057 // Next transfer may either be the last block (end not block aligned) or a block may straddle
  2811 // memory fragments.
  3058 // memory fragments.
  2812 //
  3059 //
  2813 	{
  3060 	{
       
  3061 	OstTraceExt4(TRACE_FLOW, DMMCMEDIADRIVERFLASH_PREPAREFIRSTPHYSICALFRAGMENT_ENTRY, "DMmcMediaDriverFlash::PrepareFirstPhysicalFragment;aPhysAddr=%x;aPhysLength=%d;aLength=%x;this=%x", (TUint) aPhysAddr, aPhysLength, (TUint) aLength, (TUint) this);
  2814 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:PFPF"));
  3062 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:PFPF"));
  2815 	TInt r = KErrNone;
  3063 	TInt r = KErrNone;
  2816 	
  3064 	
  2817 	r = AdjustPhysicalFragment(aPhysAddr, aPhysLength);
  3065 	r = AdjustPhysicalFragment(aPhysAddr, aPhysLength);
  2818 	
  3066 	
  2820 		{
  3068 		{
  2821 		TUint len = I64LOW(iReqEnd & iBlkMsk);
  3069 		TUint len = I64LOW(iReqEnd & iBlkMsk);
  2822 		if ( ((TUint32)aPhysLength >= aLength) && len )
  3070 		if ( ((TUint32)aPhysLength >= aLength) && len )
  2823 			{
  3071 			{
  2824 			__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:PFPF-end block"));
  3072 			__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:PFPF-end block"));
       
  3073 			OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_PREPAREFIRSTPHYSICALFRAGMENT_EB, "End block");
  2825 			//next iteration will be an IPC for the end block
  3074 			//next iteration will be an IPC for the end block
  2826 			//There is enough space in physical memory to fit
  3075 			//There is enough space in physical memory to fit
  2827 			//the extended read, but exceeds boundary for this request.
  3076 			//the extended read, but exceeds boundary for this request.
  2828 			iIPCLen = len;
  3077 			iIPCLen = len;
  2829 			iRdROB |= KIPCSetup; // IPC setup for next iteration
  3078 			iRdROB |= KIPCSetup; // IPC setup for next iteration
  2831 			}
  3080 			}
  2832 		
  3081 		
  2833 		if (aPhysLength & iBlkMsk)
  3082 		if (aPhysLength & iBlkMsk)
  2834 			{
  3083 			{
  2835 			__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:PFPF-straddles boundary"));
  3084 			__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:PFPF-straddles boundary"));
       
  3085 			OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_PREPAREFIRSTPHYSICALFRAGMENT_SB, "Straddles boundary");
  2836 			// block must be straddling a fragment boundary
  3086 			// block must be straddling a fragment boundary
  2837 			// Next iteration must be an IPC 
  3087 			// Next iteration must be an IPC 
  2838 			iRdROB |= KIPCSetup;
  3088 			iRdROB |= KIPCSetup;
  2839 			
  3089 			
  2840 			// Calculate the offset into the next memory block
  3090 			// Calculate the offset into the next memory block
  2842 			aPhysLength &= ~iBlkMsk;
  3092 			aPhysLength &= ~iBlkMsk;
  2843 			}
  3093 			}
  2844 		}
  3094 		}
  2845 	
  3095 	
  2846 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:PFPF err(%d), physAddr(0x%x), physLength(%d)",r, aPhysAddr, aPhysLength));	
  3096 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:PFPF err(%d), physAddr(0x%x), physLength(%d)",r, aPhysAddr, aPhysLength));	
       
  3097 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_PREPAREFIRSTPHYSICALFRAGMENT_EXIT, this, r );
  2847 	return r;
  3098 	return r;
  2848 	}
  3099 	}
  2849 	
  3100 	
  2850 
  3101 
  2851 void DMmcMediaDriverFlash::PrepareNextPhysicalFragment()
  3102 void DMmcMediaDriverFlash::PrepareNextPhysicalFragment()
  2853 // Retrieves next Physical memory fragment and determines the type of the next transfer
  3104 // Retrieves next Physical memory fragment and determines the type of the next transfer
  2854 // Next transfer may either be the last block (end not block aligned) or a block may straddle
  3105 // Next transfer may either be the last block (end not block aligned) or a block may straddle
  2855 // memory fragments.
  3106 // memory fragments.
  2856 //
  3107 //
  2857 	{
  3108 	{
       
  3109 	OstTraceFunctionEntry0( DMMCMEDIADRIVERFLASH_PREPARENEXTPHYSICALFRAGMENT_ENTRY );
  2858 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:PNPF"));
  3110 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:PNPF"));
  2859 	TInt err = KErrNone;
  3111 	TInt err = KErrNone;
  2860 	TPhysAddr physAddr = 0;
  3112 	TPhysAddr physAddr = 0;
  2861 	TInt physLength = 0;
  3113 	TInt physLength = 0;
  2862 	
  3114 	
  2869 			//Last physical transfer ...
  3121 			//Last physical transfer ...
  2870 			TUint len = I64LOW(iReqEnd & iBlkMsk);
  3122 			TUint len = I64LOW(iReqEnd & iBlkMsk);
  2871 			if (len)
  3123 			if (len)
  2872 				{
  3124 				{
  2873 				__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:PNPF-end block"));
  3125 				__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:PNPF-end block"));
       
  3126 				OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_PREPARENEXTPHYSICALFRAGMENT_EB, "End block" );
       
  3127 				
  2874 				// end point not block aligned!
  3128 				// end point not block aligned!
  2875 				// next iteration must be an IPC call
  3129 				// next iteration must be an IPC call
  2876 				iRdROB |= KIPCSetup;
  3130 				iRdROB |= KIPCSetup;
  2877 				iIPCLen = len;
  3131 				iIPCLen = len;
  2878 				physLength -= len;
  3132 				physLength -= len;
  2883 			}
  3137 			}
  2884 			
  3138 			
  2885 		if (physLength & iBlkMsk)
  3139 		if (physLength & iBlkMsk)
  2886 			{
  3140 			{
  2887 			__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:PNPF-straddles boundary"));
  3141 			__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:PNPF-straddles boundary"));
       
  3142 			OstTrace0( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_PREPARENEXTPHYSICALFRAGMENT_SB, "Straddles boundary" );
       
  3143 			
  2888 			// block must be straddling a fragment boundary
  3144 			// block must be straddling a fragment boundary
  2889 			// Next iteration must be an IPC 
  3145 			// Next iteration must be an IPC 
  2890 			iRdROB |= KIPCSetup;
  3146 			iRdROB |= KIPCSetup;
  2891 			
  3147 			
  2892 			// Calculate the offset into the next memory block
  3148 			// Calculate the offset into the next memory block
  2898 		}
  3154 		}
  2899 		
  3155 		
  2900 	iSession->MoreDataAvailable( (physLength  >> KDiskSectorShift), (TUint8*) physAddr, err);		
  3156 	iSession->MoreDataAvailable( (physLength  >> KDiskSectorShift), (TUint8*) physAddr, err);		
  2901 	iSecondBuffer = EFalse;
  3157 	iSecondBuffer = EFalse;
  2902 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:PNPF"));
  3158 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:PNPF"));
       
  3159 	OstTraceFunctionExit0( DMMCMEDIADRIVERFLASH_PREPARENEXTPHYSICALFRAGMENT_EXIT );
  2903 	}
  3160 	}
  2904 
  3161 
  2905 TUint8* DMmcMediaDriverFlash::ReserveReadBlocks(TInt64 aStart, TInt64 aEnd, TUint32* aLength)
  3162 TUint8* DMmcMediaDriverFlash::ReserveReadBlocks(TInt64 aStart, TInt64 aEnd, TUint32* aLength)
  2906 //
  3163 //
  2907 // Assume the cache has been drained before this function is called and so
  3164 // Assume the cache has been drained before this function is called and so
  2909 // either aEnd - aStart, or enough blocks such that the next block to read
  3166 // either aEnd - aStart, or enough blocks such that the next block to read
  2910 // is already available in the cache, and so will be read when
  3167 // is already available in the cache, and so will be read when
  2911 // ReadDataUntilCacheExhausted() is called from the callback DFC.
  3168 // ReadDataUntilCacheExhausted() is called from the callback DFC.
  2912 //
  3169 //
  2913 	{
  3170 	{
       
  3171 	OstTraceFunctionEntryExt( DMMCMEDIADRIVERFLASH_RESERVEREADBLOCKS_ENTRY, this );
  2914 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:rrb:%lx,%lx", aStart, aEnd));
  3172 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:rrb:%lx,%lx", aStart, aEnd));
  2915 
  3173 
  2916 	__ASSERT_DEBUG((aStart & iBlkMsk) == 0, Panic(ERRBStAlign));
  3174 	__ASSERT_DEBUG((aStart & iBlkMsk) == 0, Panic(ERRBStAlign));
  2917 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(ERRBStPos));
  3175 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(ERRBStPos));
  2918 	__ASSERT_DEBUG(aEnd > aStart, Panic(ERRBNotPositive));
  3176 	__ASSERT_DEBUG(aEnd > aStart, Panic(ERRBNotPositive));
  2958 
  3216 
  2959 	iLstUsdCchEnt = startIndex + blkCnt - 1;
  3217 	iLstUsdCchEnt = startIndex + blkCnt - 1;
  2960 
  3218 
  2961 	if (blkCnt < 1) blkCnt = 1; //RBW required < 1 block to be read
  3219 	if (blkCnt < 1) blkCnt = 1; //RBW required < 1 block to be read
  2962 	
  3220 	
       
  3221 	OstTraceExt2( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_RESERVEREADBLOCKS_RANGE, "blocksInRange=%d; blkCnt=%d", blocksInRange, blkCnt );
       
  3222 	
  2963 	TUint32 lengthInBytes = blkCnt << iBlkLenLog2;
  3223 	TUint32 lengthInBytes = blkCnt << iBlkLenLog2;
  2964 	*aLength = lengthInBytes;
  3224 	*aLength = lengthInBytes;
  2965 	MarkBlocks(aStart, aStart + lengthInBytes, startIndex);
  3225 	MarkBlocks(aStart, aStart + lengthInBytes, startIndex);
  2966 
  3226 
  2967 	raby = IdxToCchMem(startIndex);
  3227 	raby = IdxToCchMem(startIndex);
  2968 
  3228 
  2969 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:rrb:%x", (TUint32) raby));
  3229 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:rrb:%x", (TUint32) raby));
  2970 
  3230 
       
  3231 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_RESERVEREADBLOCKS_EXIT, this, ( TUint )( raby ) );
  2971 	return raby;
  3232 	return raby;
  2972 	}
  3233 	}
  2973 
  3234 
  2974 
  3235 
  2975 TUint8* DMmcMediaDriverFlash::ReserveWriteBlocks(TInt64 aStart, TInt64 aEnd, TUint* aRBM)
  3236 TUint8* DMmcMediaDriverFlash::ReserveWriteBlocks(TInt64 aStart, TInt64 aEnd, TUint* aRBM)
  2983 //
  3244 //
  2984 // aStart and aEnd are not necessarily block aligned - the function uses alignment
  3245 // aStart and aEnd are not necessarily block aligned - the function uses alignment
  2985 // information to minimize RBMs.
  3246 // information to minimize RBMs.
  2986 //
  3247 //
  2987 	{
  3248 	{
       
  3249 	OstTraceFunctionEntryExt( DMMCMEDIADRIVERFLASH_RESERVEWRITEBLOCKS_ENTRY, this );
  2988 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:rwb:%lx,%lx", aStart, aEnd));
  3250 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:rwb:%lx,%lx", aStart, aEnd));
  2989 
  3251 
  2990 	TInt64 physStart = aStart & ~iBlkMsk;
  3252 	TInt64 physStart = aStart & ~iBlkMsk;
  2991 	TInt64 physEnd = (aEnd + iBlkMsk) & ~iBlkMsk;
  3253 	TInt64 physEnd = (aEnd + iBlkMsk) & ~iBlkMsk;
  2992 
  3254 
  2998 	
  3260 	
  2999 	const TBool firstPartial = (aStart & iBlkMsk) != 0;
  3261 	const TBool firstPartial = (aStart & iBlkMsk) != 0;
  3000 	const TBool lastPartial  = (aEnd & iBlkMsk)   != 0;
  3262 	const TBool lastPartial  = (aEnd & iBlkMsk)   != 0;
  3001 	
  3263 	
  3002 	const TInt blkCnt = I64LOW((physEnd - physStart) >> iBlkLenLog2);
  3264 	const TInt blkCnt = I64LOW((physEnd - physStart) >> iBlkLenLog2);
       
  3265 	OstTrace1( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_RESERVEWRITEBLOCKS_RANGE, "blkCnt=%d", blkCnt );
  3003 	
  3266 	
  3004 	TBool startUsed = EFalse;
  3267 	TBool startUsed = EFalse;
  3005 	TBool endUsed   = EFalse;
  3268 	TBool endUsed   = EFalse;
  3006 	
  3269 	
  3007 	TUint8* raby = NULL;
  3270 	TUint8* raby = NULL;
  3042 			if ( (firstPartial || lastPartial) && blkCnt <= 2)
  3305 			if ( (firstPartial || lastPartial) && blkCnt <= 2)
  3043 				{
  3306 				{
  3044 				//Physical addressing not to be used.
  3307 				//Physical addressing not to be used.
  3045 				//more efficent to use local Cache copying
  3308 				//more efficent to use local Cache copying
  3046 				iDoPhysicalAddress = EFalse;
  3309 				iDoPhysicalAddress = EFalse;
       
  3310 				OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_RESERVEWRITEBLOCKS_EXIT1, this, ( TUint )( raby ) );
  3047 				return raby;
  3311 				return raby;
  3048 				}
  3312 				}
  3049 			else
  3313 			else
  3050 				{						
  3314 				{						
  3051 				raby = iMinorBuf;
  3315 				raby = iMinorBuf;
  3087 					
  3351 					
  3088 					if (lastPartial && !lastPres) 
  3352 					if (lastPartial && !lastPres) 
  3089 						*aRBM |= KWtRBMLst;
  3353 						*aRBM |= KWtRBMLst;
  3090 					}
  3354 					}
  3091 				
  3355 				
       
  3356 				OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_RESERVEWRITEBLOCKS_EXIT2, this, ( TUint )( raby ) );
  3092 				return raby;
  3357 				return raby;
  3093 				}
  3358 				}
  3094 			} // if (iDoPhysicalAddress)			
  3359 			} // if (iDoPhysicalAddress)			
  3095 		
  3360 		
  3096 		if (!firstUsable && !lastUsable)
  3361 		if (!firstUsable && !lastUsable)
  3195 			iDoLastRMW = ETrue;
  3460 			iDoLastRMW = ETrue;
  3196 		}
  3461 		}
  3197 
  3462 
  3198 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:rwb:%x", (TUint32) raby));
  3463 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:rwb:%x", (TUint32) raby));
  3199 
  3464 
       
  3465 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_RESERVEWRITEBLOCKS_EXIT3, this, ( TUint )( raby ) );
  3200 	return raby;
  3466 	return raby;
  3201 	}
  3467 	}
  3202 
  3468 
  3203 void DMmcMediaDriverFlash::MarkBlocks(TInt64 aStart, TInt64 aEnd, TInt aStartIndex)
  3469 void DMmcMediaDriverFlash::MarkBlocks(TInt64 aStart, TInt64 aEnd, TInt aStartIndex)
  3204 //
  3470 //
  3205 // mark range of blocks for media range.  If existing cache entries for any part of
  3471 // mark range of blocks for media range.  If existing cache entries for any part of
  3206 // the cache are already in the buffer they are invalidated.
  3472 // the cache are already in the buffer they are invalidated.
  3207 //
  3473 //
  3208 	{
  3474 	{
       
  3475 	OstTraceFunctionEntryExt( DMMCMEDIADRIVERFLASH_MARKBLOCKS_ENTRY, this );
  3209 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:mb:%lx,%lx,%d", aStart, aEnd, aStartIndex));
  3476 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:mb:%lx,%lx,%d", aStart, aEnd, aStartIndex));
  3210 
  3477 
  3211 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(EMBStPos));
  3478 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(EMBStPos));
  3212 	__ASSERT_DEBUG((aStart & iBlkMsk) == 0, Panic(EMBStAlign));
  3479 	__ASSERT_DEBUG((aStart & iBlkMsk) == 0, Panic(EMBStAlign));
  3213 	__ASSERT_DEBUG(aEnd > aStart, Panic(EMBNotPositive));
  3480 	__ASSERT_DEBUG(aEnd > aStart, Panic(EMBNotPositive));
  3223 		if (iCachedBlocks[i] >= aStart && iCachedBlocks[i] < aEnd)
  3490 		if (iCachedBlocks[i] >= aStart && iCachedBlocks[i] < aEnd)
  3224 			iCachedBlocks[i] = KInvalidBlock;
  3491 			iCachedBlocks[i] = KInvalidBlock;
  3225 		}
  3492 		}
  3226 
  3493 
  3227 	TInt blkCnt = I64LOW((aEnd - aStart) >> iBlkLenLog2);
  3494 	TInt blkCnt = I64LOW((aEnd - aStart) >> iBlkLenLog2);
       
  3495 	OstTrace1( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_MARKBLOCKS_RANGE, "blkCnt=%d", blkCnt );
  3228 	for (i = aStartIndex; i < aStartIndex + blkCnt; ++i)
  3496 	for (i = aStartIndex; i < aStartIndex + blkCnt; ++i)
  3229 		iCachedBlocks[i] = aStart + (static_cast<TUint32>(i - aStartIndex) << iBlkLenLog2);
  3497 		iCachedBlocks[i] = aStart + (static_cast<TUint32>(i - aStartIndex) << iBlkLenLog2);
  3230 
  3498 
  3231 	for (i = aStartIndex + blkCnt; i < iBlocksInBuffer; ++i)
  3499 	for (i = aStartIndex + blkCnt; i < iBlocksInBuffer; ++i)
  3232 		{
  3500 		{
  3233 		if (iCachedBlocks[i] >= aStart && iCachedBlocks[i] < aEnd)
  3501 		if (iCachedBlocks[i] >= aStart && iCachedBlocks[i] < aEnd)
  3234 			iCachedBlocks[i] = KInvalidBlock;
  3502 			iCachedBlocks[i] = KInvalidBlock;
  3235 		}
  3503 		}
  3236 
  3504 
  3237 	__ASSERT_CACHE(CacheInvariant(), Panic(EMBCchInvPost));
  3505 	__ASSERT_CACHE(CacheInvariant(), Panic(EMBCchInvPost));
       
  3506 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_MARKBLOCKS_EXIT, this );
  3238 	}
  3507 	}
  3239 
  3508 
  3240 
  3509 
  3241 void DMmcMediaDriverFlash::BuildGammaArray(TInt64 aStart, TInt64 aEnd)
  3510 void DMmcMediaDriverFlash::BuildGammaArray(TInt64 aStart, TInt64 aEnd)
  3242 //
  3511 //
  3245 // aStart + iBlkLen, and so on.  Building an array here means that all of
  3514 // aStart + iBlkLen, and so on.  Building an array here means that all of
  3246 // the available cached entries can be found in linear time instead of
  3515 // the available cached entries can be found in linear time instead of
  3247 // quadratically searching through the array for each block.
  3516 // quadratically searching through the array for each block.
  3248 //
  3517 //
  3249 	{
  3518 	{
       
  3519 	OstTraceFunctionEntryExt( DMMCMEDIADRIVERFLASH_BUILDGAMMAARRAY_ENTRY, this );
  3250 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:bga:%lx,%lx", aStart, aEnd));
  3520 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:bga:%lx,%lx", aStart, aEnd));
  3251 
  3521 
  3252 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(EBGAStPos));
  3522 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(EBGAStPos));
  3253 	__ASSERT_DEBUG((aStart & iBlkMsk) == 0, Panic(EBGAStAlign));
  3523 	__ASSERT_DEBUG((aStart & iBlkMsk) == 0, Panic(EBGAStAlign));
  3254 	__ASSERT_DEBUG(aEnd > aStart, Panic(EBGANotPositive));
  3524 	__ASSERT_DEBUG(aEnd > aStart, Panic(EBGANotPositive));
  3257 	__ASSERT_DEBUG(aEnd - aStart <= (TInt64) iMaxBufSize, Panic(EBGAOverflow));
  3527 	__ASSERT_DEBUG(aEnd - aStart <= (TInt64) iMaxBufSize, Panic(EBGAOverflow));
  3258 	__ASSERT_CACHE(CacheInvariant(), Panic(EBGACchInv));
  3528 	__ASSERT_CACHE(CacheInvariant(), Panic(EBGACchInv));
  3259 
  3529 
  3260 	// KNoCacheBlock = (0xff) x 4
  3530 	// KNoCacheBlock = (0xff) x 4
  3261 	TUint blocksInRange = I64LOW((aEnd - aStart) >> iBlkLenLog2);
  3531 	TUint blocksInRange = I64LOW((aEnd - aStart) >> iBlkLenLog2);
       
  3532 	OstTrace1( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_BUILDGAMMAARRAY_RANGE, "blocksInRange=%d", blocksInRange );
  3262 	memset(iGamma, 0xff, sizeof(*iGamma) * blocksInRange);
  3533 	memset(iGamma, 0xff, sizeof(*iGamma) * blocksInRange);
  3263 
  3534 
  3264 	TInt64 blkAddr = 0;
  3535 	TInt64 blkAddr = 0;
  3265 	for (TInt i = 0; ( (blocksInRange > 0 ) && (i < iBlocksInBuffer) ); ++i)
  3536 	for (TInt i = 0; ( (blocksInRange > 0 ) && (i < iBlocksInBuffer) ); ++i)
  3266 		{
  3537 		{
  3269 			{
  3540 			{
  3270 			iGamma[I64LOW((blkAddr - aStart) >> iBlkLenLog2)] = i;
  3541 			iGamma[I64LOW((blkAddr - aStart) >> iBlkLenLog2)] = i;
  3271 			blocksInRange--;
  3542 			blocksInRange--;
  3272 			}
  3543 			}
  3273 		}
  3544 		}
       
  3545 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_BUILDGAMMAARRAY_EXIT, this );
  3274 	}
  3546 	}
  3275 
  3547 
  3276 void DMmcMediaDriverFlash::InvalidateCache()
  3548 void DMmcMediaDriverFlash::InvalidateCache()
  3277 	{
  3549 	{
       
  3550 	OstTraceFunctionEntry0( DMMCMEDIADRIVERFLASH_INVALIDATECACHE1_ENTRY );
  3278 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:ich"));
  3551 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:ich"));
  3279 
  3552 
  3280 	// KInvalidBlock = (0xff) x 4
  3553 	// KInvalidBlock = (0xff) x 4
  3281 	memset(iCachedBlocks, 0xff, sizeof(*iCachedBlocks) * iBlocksInBuffer);
  3554 	memset(iCachedBlocks, 0xff, sizeof(*iCachedBlocks) * iBlocksInBuffer);
       
  3555 	OstTraceFunctionExit0( DMMCMEDIADRIVERFLASH_INVALIDATECACHE1_EXIT );
  3282 	}
  3556 	}
  3283 
  3557 
  3284 // Invalidate any cache entries from aStart to aEnd
  3558 // Invalidate any cache entries from aStart to aEnd
  3285 // This is for DMA writes and is to prevent the cache becoming inconsistent with the media.
  3559 // This is for DMA writes and is to prevent the cache becoming inconsistent with the media.
  3286 void DMmcMediaDriverFlash::InvalidateCache(TInt64 aStart, TInt64 aEnd)
  3560 void DMmcMediaDriverFlash::InvalidateCache(TInt64 aStart, TInt64 aEnd)
  3287 	{
  3561 	{
       
  3562 	OstTraceFunctionEntryExt( DMMCMEDIADRIVERFLASH_INVALIDATECACHE2_ENTRY, this );
  3288 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:ich:%lx,%lx", aStart, aEnd));
  3563 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:ich:%lx,%lx", aStart, aEnd));
  3289 
  3564 
  3290 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(EBGAStPos));
  3565 	__ASSERT_DEBUG(TotalSizeInBytes() > aStart, Panic(EBGAStPos));
  3291 	__ASSERT_DEBUG(aEnd > aStart, Panic(EBGANotPositive));
  3566 	__ASSERT_DEBUG(aEnd > aStart, Panic(EBGANotPositive));
  3292 	__ASSERT_DEBUG(TotalSizeInBytes() >= aEnd, Panic(EBGAEndPos));
  3567 	__ASSERT_DEBUG(TotalSizeInBytes() >= aEnd, Panic(EBGAEndPos));
  3293 
  3568 
  3294 	const TInt blkCnt = I64LOW((aStart - aEnd) >> iBlkLenLog2);
  3569 	const TInt blkCnt = I64LOW((aStart - aEnd) >> iBlkLenLog2);
       
  3570 	OstTrace1( TRACE_INTERNALS, DMMCMEDIADRIVERFLASH_INVALIDATECACHE_RANGE, "blocksInRange=%d", blkCnt );
  3295 
  3571 
  3296 	__ASSERT_CACHE(CacheInvariant(), Panic(EBGACchInv));
  3572 	__ASSERT_CACHE(CacheInvariant(), Panic(EBGACchInv));
  3297 	
  3573 	
  3298 	TInt64 endBlk = (blkCnt == 0) ? (aStart+iBlkLen) : aEnd;			
  3574 	TInt64 endBlk = (blkCnt == 0) ? (aStart+iBlkLen) : aEnd;			
  3299 	
  3575 	
  3301 		{
  3577 		{
  3302 		const TInt64 blkAddr = iCachedBlocks[i];
  3578 		const TInt64 blkAddr = iCachedBlocks[i];
  3303 		if (blkAddr >= aStart && blkAddr < endBlk)
  3579 		if (blkAddr >= aStart && blkAddr < endBlk)
  3304 			iCachedBlocks[i] = KInvalidBlock;
  3580 			iCachedBlocks[i] = KInvalidBlock;
  3305 		}
  3581 		}
       
  3582 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_INVALIDATECACHE2_EXIT, this );
  3306 	}
  3583 	}
  3307 
  3584 
  3308 TUint8* DMmcMediaDriverFlash::IdxToCchMem(TInt aIdx) const
  3585 TUint8* DMmcMediaDriverFlash::IdxToCchMem(TInt aIdx) const
  3309 	{
  3586 	{
       
  3587 	OstTraceFunctionEntryExt( DMMCMEDIADRIVERFLASH_IDXTOCCHMEM_ENTRY, this );
  3310 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:icm:%d", aIdx));
  3588 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("=mmd:icm:%d", aIdx));
  3311 
  3589 
  3312 	__ASSERT_DEBUG(aIdx >= 0, Panic(EICMNegative));
  3590 	__ASSERT_DEBUG(aIdx >= 0, Panic(EICMNegative));
  3313 	__ASSERT_DEBUG(aIdx < iBlocksInBuffer, Panic(EICMOverflow));
  3591 	__ASSERT_DEBUG(aIdx < iBlocksInBuffer, Panic(EICMOverflow));
  3314 	
  3592 	
       
  3593 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_IDXTOCCHMEM_EXIT, this );
  3315 	return &iCacheBuf[aIdx << iBlkLenLog2];
  3594 	return &iCacheBuf[aIdx << iBlkLenLog2];
  3316 	}
  3595 	}
  3317 
  3596 
  3318 TInt DMmcMediaDriverFlash::CchMemToIdx(TUint8* aMemP) const
  3597 TInt DMmcMediaDriverFlash::CchMemToIdx(TUint8* aMemP) const
  3319 	{
  3598 	{
       
  3599 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_CCHMEMTOIDX_ENTRY, this );
  3320 	__ASSERT_DEBUG((aMemP >= iCacheBuf) && (aMemP < iCacheBuf + (iBlocksInBuffer << iBlkLenLog2)), Panic(ECMIOverflow));
  3600 	__ASSERT_DEBUG((aMemP >= iCacheBuf) && (aMemP < iCacheBuf + (iBlocksInBuffer << iBlkLenLog2)), Panic(ECMIOverflow));
  3321 
  3601 
  3322 	return((aMemP - iCacheBuf) >> iBlkLenLog2);
  3602 	return((aMemP - iCacheBuf) >> iBlkLenLog2);
  3323 	}
  3603 	}
  3324 
  3604 
  3328 // check each cache entry refers to a valid block and that no two
  3608 // check each cache entry refers to a valid block and that no two
  3329 // entries cover the same block.  This algorithm is quadratic in
  3609 // entries cover the same block.  This algorithm is quadratic in
  3330 // the cache length.
  3610 // the cache length.
  3331 //
  3611 //
  3332 	{
  3612 	{
       
  3613 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_CACHEINVARIANT_ENTRY, this );
  3333 	for (TInt i = 0; i < iBlocksInBuffer; ++i)
  3614 	for (TInt i = 0; i < iBlocksInBuffer; ++i)
  3334 		{
  3615 		{
  3335 		if (iCachedBlocks[i] == KInvalidBlock)
  3616 		if (iCachedBlocks[i] == KInvalidBlock)
  3336 			continue;
  3617 			continue;
  3337 
  3618 
  3346 			if (iCachedBlocks[i] == iCachedBlocks[j])
  3627 			if (iCachedBlocks[i] == iCachedBlocks[j])
  3347 				return EFalse;
  3628 				return EFalse;
  3348 			}
  3629 			}
  3349 		}
  3630 		}
  3350 
  3631 
       
  3632 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_CACHEINVARIANT_EXIT, this );
  3351 	return ETrue;
  3633 	return ETrue;
  3352 	}
  3634 	}
  3353 #endif
  3635 #endif
  3354 
  3636 
  3355 void DMmcMediaDriverFlash::NotifyPowerDown()
  3637 void DMmcMediaDriverFlash::NotifyPowerDown()
  3356 	{
  3638 	{
       
  3639 	OstTraceFunctionEntry0( DMMCMEDIADRIVERFLASH_NOTIFYPOWERDOWN_ENTRY );
  3357 	__KTRACE_OPT(KPBUSDRV,Kern::Printf(">Mmc:NotifyPowerDown"));
  3640 	__KTRACE_OPT(KPBUSDRV,Kern::Printf(">Mmc:NotifyPowerDown"));
  3358 
  3641 
  3359 	iSessionEndDfc.Cancel();
  3642 	iSessionEndDfc.Cancel();
  3360 	iDataTransferCallBackDfc.Cancel();
  3643 	iDataTransferCallBackDfc.Cancel();
  3361 
  3644 
  3365 	if (iSession)
  3648 	if (iSession)
  3366 		iStack->CancelSession(iSession);
  3649 		iStack->CancelSession(iSession);
  3367 
  3650 
  3368 	CompleteRequest(KErrNotReady);
  3651 	CompleteRequest(KErrNotReady);
  3369 	iMedReq = EMReqIdle;
  3652 	iMedReq = EMReqIdle;
       
  3653 	OstTraceFunctionExit0( DMMCMEDIADRIVERFLASH_NOTIFYPOWERDOWN_EXIT );
  3370 	}
  3654 	}
  3371 
  3655 
  3372 void DMmcMediaDriverFlash::NotifyEmergencyPowerDown()
  3656 void DMmcMediaDriverFlash::NotifyEmergencyPowerDown()
  3373 	{
  3657 	{
       
  3658 	OstTraceFunctionEntry0( DMMCMEDIADRIVERFLASH_NOTIFYEMERGENCYPOWERDOWN_ENTRY );
  3374 	__KTRACE_OPT(KPBUSDRV,Kern::Printf(">Ata:NotifyEmergencyPowerDown"));
  3659 	__KTRACE_OPT(KPBUSDRV,Kern::Printf(">Ata:NotifyEmergencyPowerDown"));
  3375 
  3660 
  3376 	iSessionEndDfc.Cancel();
  3661 	iSessionEndDfc.Cancel();
  3377 	iDataTransferCallBackDfc.Cancel();
  3662 	iDataTransferCallBackDfc.Cancel();
  3378 
  3663 
  3385 	if (iSession)
  3670 	if (iSession)
  3386 		iStack->CancelSession(iSession);
  3671 		iStack->CancelSession(iSession);
  3387 
  3672 
  3388 	CompleteRequest(r);
  3673 	CompleteRequest(r);
  3389 	iMedReq = EMReqIdle;
  3674 	iMedReq = EMReqIdle;
       
  3675 	OstTraceFunctionExit0( DMMCMEDIADRIVERFLASH_NOTIFYEMERGENCYPOWERDOWN_EXIT );
  3390 	}
  3676 	}
  3391 
  3677 
  3392 TInt DMmcMediaDriverFlash::Request(TLocDrvRequest& aRequest)
  3678 TInt DMmcMediaDriverFlash::Request(TLocDrvRequest& aRequest)
  3393 	{
  3679 	{
       
  3680 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_REQUEST_ENTRY, this );
  3394 	__KTRACE_OPT(KLOCDRV,Kern::Printf("MmcMd:Req %08x id %d",&aRequest,aRequest.Id()));
  3681 	__KTRACE_OPT(KLOCDRV,Kern::Printf("MmcMd:Req %08x id %d",&aRequest,aRequest.Id()));
  3395 	TInt r=KErrNotSupported;
  3682 	TInt r=KErrNotSupported;
  3396 	TInt id=aRequest.Id();
  3683 	TInt id=aRequest.Id();
       
  3684 	OstTraceDefExt2( OST_TRACE_CATEGORY_RND, TRACE_REQUEST, DMMCMEDIADRIVERFLASH_REQUEST_ID, "Request=0x%08x; Request ID=%d", (TUint) &aRequest, id);
  3397 
  3685 
  3398 #if defined (__TEST_PAGING_MEDIA_DRIVER__)
  3686 #if defined (__TEST_PAGING_MEDIA_DRIVER__)
  3399 	DThread* client=aRequest.Client();
  3687 	DThread* client=aRequest.Client();
  3400 	__KTRACE_OPT(KLOCDPAGING,Kern::Printf("Client:0x%08x",client));
  3688 	__KTRACE_OPT(KLOCDPAGING,Kern::Printf("Client:0x%08x",client));
       
  3689 	OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_REQUEST, DMMCMEDIADRIVERFLASH_REQUEST_CLIENT, "Request client=0x%08x", (TUint) client);
  3401 #endif // __TEST_PAGING_MEDIA_DRIVER__
  3690 #endif // __TEST_PAGING_MEDIA_DRIVER__
  3402 
  3691 
  3403 	// First handle requests that can be handled without deferring
  3692 	// First handle requests that can be handled without deferring
  3404 	if(id==DLocalDrive::ECaps)
  3693 	if(id==DLocalDrive::ECaps)
  3405 		{
  3694 		{
  3407 		TLocDrv& drive = *aRequest.Drive();
  3696 		TLocDrv& drive = *aRequest.Drive();
  3408 		r = Caps(drive, c);
  3697 		r = Caps(drive, c);
  3409 		c.iSize = drive.iPartitionLen;
  3698 		c.iSize = drive.iPartitionLen;
  3410 		c.iPartitionType = drive.iPartitionType;	
  3699 		c.iPartitionType = drive.iPartitionType;	
  3411 		c.iHiddenSectors = (TUint) (drive.iPartitionBaseAddr >> KDiskSectorShift);
  3700 		c.iHiddenSectors = (TUint) (drive.iPartitionBaseAddr >> KDiskSectorShift);
       
  3701 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_REQUEST_EXIT1, this, r );
  3412 		return r;
  3702 		return r;
  3413 		}
  3703 		}
  3414 
  3704 
  3415 	// All other requests must be deferred if a request is currently in progress
  3705 	// All other requests must be deferred if a request is currently in progress
  3416 	if (iCurrentReq)
  3706 	if (iCurrentReq)
  3423 			iMmcStats.iReqNormal++;
  3713 			iMmcStats.iReqNormal++;
  3424 #endif // __TEST_PAGING_MEDIA_DRIVER__
  3714 #endif // __TEST_PAGING_MEDIA_DRIVER__
  3425 
  3715 
  3426 		// a request is already in progress, so hold on to this one
  3716 		// a request is already in progress, so hold on to this one
  3427 		__KTRACE_OPT(KLOCDRV,Kern::Printf("MmcMd:Req %08x ret 1",&aRequest));
  3717 		__KTRACE_OPT(KLOCDRV,Kern::Printf("MmcMd:Req %08x ret 1",&aRequest));
       
  3718 		OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_REQUEST, DMMCMEDIADRIVERFLASH_REQUEST_IN_PROGRESS, "Request in progress=0x%08x", (TUint) &aRequest);
       
  3719 		OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_REQUEST_EXIT2, this, KMediaDriverDeferRequest );
  3428 		return KMediaDriverDeferRequest;
  3720 		return KMediaDriverDeferRequest;
  3429 		}
  3721 		}
  3430 	else
  3722 	else
  3431 		{
  3723 		{
  3432 		iCurrentReq=&aRequest;
  3724 		iCurrentReq=&aRequest;
  3439 
  3731 
  3440 #if defined(__DEMAND_PAGING__)
  3732 #if defined(__DEMAND_PAGING__)
  3441 			case DMediaPagingDevice::ERomPageInRequest:
  3733 			case DMediaPagingDevice::ERomPageInRequest:
  3442 				__KTRACE_OPT(KLOCDPAGING,Kern::Printf("DMediaDriverFlash::Request(ERomPageInRequest)"));
  3734 				__KTRACE_OPT(KLOCDPAGING,Kern::Printf("DMediaDriverFlash::Request(ERomPageInRequest)"));
  3443 				BTraceContext8(BTrace::EPagingMedia,BTrace::EPagingMediaPagingMedDrvBegin,MEDIA_DEVICE_MMC,iCurrentReq);
  3735 				BTraceContext8(BTrace::EPagingMedia,BTrace::EPagingMediaPagingMedDrvBegin,MEDIA_DEVICE_MMC,iCurrentReq);
       
  3736 				OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_REQUEST, DMMCMEDIADRIVERFLASH_REQUEST_ROM_PAGE_IN, "ROM page-in request");
  3444 				r=DoRead();
  3737 				r=DoRead();
  3445 				break;
  3738 				break;
  3446 			case DMediaPagingDevice::ECodePageInRequest:
  3739 			case DMediaPagingDevice::ECodePageInRequest:
  3447 				__KTRACE_OPT(KLOCDPAGING,Kern::Printf("DMediaDriverFlash::Request(ECodePageInRequest)"));
  3740 				__KTRACE_OPT(KLOCDPAGING,Kern::Printf("DMediaDriverFlash::Request(ECodePageInRequest)"));
  3448 				BTraceContext8(BTrace::EPagingMedia,BTrace::EPagingMediaPagingMedDrvBegin,MEDIA_DEVICE_MMC,iCurrentReq);
  3741 				BTraceContext8(BTrace::EPagingMedia,BTrace::EPagingMediaPagingMedDrvBegin,MEDIA_DEVICE_MMC,iCurrentReq);
       
  3742 				OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_REQUEST, DMMCMEDIADRIVERFLASH_REQUEST_CODE_PAGE_IN, "Code page-in request");
  3449 				r=DoRead();
  3743 				r=DoRead();
  3450 				break;
  3744 				break;
  3451 #endif	// __DEMAND_PAGING__
  3745 #endif	// __DEMAND_PAGING__
  3452 
  3746 
  3453 			case DLocalDrive::EQueryDevice:
  3747 			case DLocalDrive::EQueryDevice:
  3457 			case DLocalDrive::ERead:
  3751 			case DLocalDrive::ERead:
  3458 				r=DoRead();
  3752 				r=DoRead();
  3459 				break;
  3753 				break;
  3460 			case DLocalDrive::EWrite:
  3754 			case DLocalDrive::EWrite:
  3461 				if (readOnly)
  3755 				if (readOnly)
       
  3756 				    {
       
  3757 					OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_REQUEST_EXIT3, this, KErrNotSupported );
  3462 					return KErrNotSupported;
  3758 					return KErrNotSupported;
       
  3759 				    }
  3463 				r=DoWrite();
  3760 				r=DoWrite();
  3464 				break;
  3761 				break;
  3465 			case DLocalDrive::EFormat:
  3762 			case DLocalDrive::EFormat:
  3466 				if (readOnly)
  3763 				if (readOnly)
       
  3764 				    {
       
  3765 					OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_REQUEST_EXIT4, this, KErrNotSupported );
  3467 					return KErrNotSupported;
  3766 					return KErrNotSupported;
       
  3767 				    }
  3468 				r=DoFormat();
  3768 				r=DoFormat();
  3469 				break;
  3769 				break;
  3470 
  3770 
  3471 #if defined __TEST_PAGING_MEDIA_DRIVER__
  3771 #if defined __TEST_PAGING_MEDIA_DRIVER__
  3472 			case DLocalDrive::EControlIO:
  3772 			case DLocalDrive::EControlIO:
  3537 		iMedReq = EMReqIdle;
  3837 		iMedReq = EMReqIdle;
  3538 		iCurrentReq=NULL;
  3838 		iCurrentReq=NULL;
  3539 		SetCurrentConsumption(KIdleCurrentInMilliAmps);
  3839 		SetCurrentConsumption(KIdleCurrentInMilliAmps);
  3540 		}
  3840 		}
  3541 	
  3841 	
       
  3842 	OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_REQUEST_EXIT5, this, r );
  3542 	return r;
  3843 	return r;
  3543 	}
  3844 	}
  3544 
  3845 
  3545 void DMmcMediaDriverFlash::Disconnect(DLocalDrive* aLocalDrive, TThreadMessage* aMsg)
  3846 void DMmcMediaDriverFlash::Disconnect(DLocalDrive* aLocalDrive, TThreadMessage* aMsg)
  3546 	{
  3847 	{
       
  3848 	OstTraceFunctionEntry1( DMMCMEDIADRIVERFLASH_DISCONNECT_ENTRY, this );
  3547 	// Complete using the default implementation
  3849 	// Complete using the default implementation
  3548 	DMediaDriver::Disconnect(aLocalDrive, aMsg);
  3850 	DMediaDriver::Disconnect(aLocalDrive, aMsg);
       
  3851 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_DISCONNECT_EXIT, this );
  3549 	}
  3852 	}
  3550 
  3853 
  3551 #ifdef _DEBUG_CACHE
  3854 #ifdef _DEBUG_CACHE
  3552 TUint8* DMmcMediaDriverFlash::GetCachedBlock(TInt64 aMdAddr)
  3855 TUint8* DMmcMediaDriverFlash::GetCachedBlock(TInt64 aMdAddr)
  3553 // 
  3856 // 
  3556 // the cache.  It should not be used for general block retrieval.
  3859 // the cache.  It should not be used for general block retrieval.
  3557 // If there are m blocks in the cache, and n in the requested range,
  3860 // If there are m blocks in the cache, and n in the requested range,
  3558 // this function is o(mn), whereas BuildGammaArray() is theta(m).
  3861 // this function is o(mn), whereas BuildGammaArray() is theta(m).
  3559 //
  3862 //
  3560 	{
  3863 	{
       
  3864 	OstTraceFunctionEntryExt( DMMCMEDIADRIVERFLASH_GETCACHEDBLOCK_ENTRY, this );
  3561 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:gcb:%lx", aMdAddr));
  3865 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">mmd:gcb:%lx", aMdAddr));
  3562 
  3866 
  3563 	__ASSERT_DEBUG((aMdAddr & iBlkMsk) == 0, Panic(EGCBAlign));
  3867 	__ASSERT_DEBUG((aMdAddr & iBlkMsk) == 0, Panic(EGCBAlign));
  3564 	__ASSERT_DEBUG(TotalSizeInBytes() > aMdAddr, Panic(EGCBPos));
  3868 	__ASSERT_DEBUG(TotalSizeInBytes() > aMdAddr, Panic(EGCBPos));
  3565 	__ASSERT_CACHE(CacheInvariant(), Panic(EGCBCchInv));
  3869 	__ASSERT_CACHE(CacheInvariant(), Panic(EGCBCchInv));
  3568 		{
  3872 		{
  3569 		if (iCachedBlocks[i] == aMdAddr)
  3873 		if (iCachedBlocks[i] == aMdAddr)
  3570 			{
  3874 			{
  3571 			TUint8* raby = IdxToCchMem(i);
  3875 			TUint8* raby = IdxToCchMem(i);
  3572 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:gcb:%x", (TUint32) raby));
  3876 			__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:gcb:%x", (TUint32) raby));
       
  3877 			OstTraceFunctionExitExt( DMMCMEDIADRIVERFLASH_GETCACHEDBLOCK_EXIT1, this, ( TUint )( raby ) );
  3573 			return raby;
  3878 			return raby;
  3574 			}
  3879 			}
  3575 		}
  3880 		}
  3576 
  3881 
  3577 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:gcb:0"));
  3882 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:gcb:0"));
       
  3883 	OstTraceFunctionExit1( DMMCMEDIADRIVERFLASH_GETCACHEDBLOCK_EXIT2, this );
  3578 	return 0;
  3884 	return 0;
  3579 	}
  3885 	}
  3580 #endif // _DEBUG_CACHE
  3886 #endif // _DEBUG_CACHE
  3581 
  3887 
  3582 
  3888