baseport/syborg/mednvmemory/syborg_mednvmemory.cpp
branchnvmemory
changeset 74 eb3d0111f868
child 118 b0012dceb754
equal deleted inserted replaced
73:b6aa150091ee 74:eb3d0111f868
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Minimalistic non volatile memory driver
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "locmedia.h"
       
    20 #include "platform.h"
       
    21 #include "variantmediadef.h"
       
    22 #include "syborg_mednvmemory.h"
       
    23 #include "syborg.h"
       
    24 
       
    25 _LIT(KNVMemPddName, "Media.MEDNVMEMORY");
       
    26 _LIT(KNVMemDriveName,NVMEM1_DRIVENAME);
       
    27 
       
    28 class DPhysicalDeviceMediaNVMemory : public DPhysicalDevice
       
    29 	{
       
    30 public:
       
    31 	DPhysicalDeviceMediaNVMemory();
       
    32 public:
       
    33 	// Implementing the interface
       
    34 	virtual TInt Install();
       
    35 	virtual void GetCaps(TDes8& aDes) const;
       
    36 	virtual TInt Create(DBase*& aChannel, TInt aMediaId, const TDesC8* anInfo, const TVersion& aVer);
       
    37 	virtual TInt Validate(TInt aDeviceType, const TDesC8* anInfo, const TVersion& aVer);
       
    38 	virtual TInt Info(TInt aFunction, TAny* a1);
       
    39 	};
       
    40 								
       
    41 class DMediaDriverNVMemory : public DMediaDriver
       
    42 	{
       
    43 public:
       
    44 	DMediaDriverNVMemory(TInt aMediaId);
       
    45 public:
       
    46 	// Implementing the interface
       
    47 	virtual TInt Request(TLocDrvRequest& aRequest);
       
    48 	virtual void Disconnect(DLocalDrive* aLocalDrive, TThreadMessage* aMsg);
       
    49 	virtual TInt PartitionInfo(TPartitionInfo &anInfo);
       
    50 	virtual void NotifyPowerDown();
       
    51 	virtual void NotifyEmergencyPowerDown();
       
    52 public:
       
    53 	void CompleteRequest(TInt aReason);
       
    54 	TInt DoCreate(TInt aMediaId);
       
    55 	TInt Caps(TLocDrvRequest& aRequest);
       
    56 	TInt Read();
       
    57 	TInt Write();
       
    58 	TInt Format();
       
    59 	static void TransactionLaunchDfc(TAny* aMediaDriver);
       
    60 	void DoTransactionLaunchDfc();
       
    61 	static void SessionEndDfc(TAny* aMediaDriver);
       
    62 	void DoSessionEndDfc();
       
    63 	TUint32 GetNVMemSize( void );
       
    64 
       
    65 public:
       
    66 	TUint32 iLatestTransferSectorCount;
       
    67 	TDfc iSessionEndDfc;
       
    68 
       
    69 private:
       
    70 	TInt ContinueTransaction( TUint32 aTransactionSectorOffset, TUint32 aTransactionSectorCount, TUint32 aDirection );
       
    71 	static void Isr(TAny* aPtr);
       
    72 
       
    73 private:
       
    74 	TLocDrvRequest* iCurrentRequest;				// Current Request
       
    75 	Int64 iTotalLength;
       
    76 	Int64 iProsessedLength;
       
    77 	Int64 iPos;
       
    78 	TPhysAddr iTransferBufferPhys;
       
    79 	TUint8* iTransferBufferLin;
       
    80 	TUint32 iHead;
       
    81 	TUint32 iTail;
       
    82 	TUint32 iSplitted;
       
    83 	TUint32 iAlignmentOverhead;
       
    84 	TBool iReadModifyWrite;
       
    85 	TDfc iTransactionLaunchDfc;
       
    86 	};
       
    87 
       
    88 DPhysicalDeviceMediaNVMemory::DPhysicalDeviceMediaNVMemory()
       
    89 //
       
    90 // Constructor
       
    91 //
       
    92 	{
       
    93 	__DEBUG_PRINT(">DPhysicalDeviceMediaNVMemory::DPhysicalDeviceMediaNVMemory");
       
    94 	iUnitsMask=0x1;
       
    95 	iVersion=TVersion(KMediaDriverInterfaceMajorVersion,KMediaDriverInterfaceMinorVersion,KMediaDriverInterfaceBuildVersion);
       
    96 	}
       
    97 
       
    98 TInt DPhysicalDeviceMediaNVMemory::Install()
       
    99 //
       
   100 // Install the Internal NVMem PDD.
       
   101 //
       
   102 	{
       
   103 	__DEBUG_PRINT("DPhysicalDeviceMediaNVMemory::Install()");
       
   104 	return SetName(&KNVMemPddName);
       
   105 	}
       
   106 
       
   107 void DPhysicalDeviceMediaNVMemory::GetCaps(TDes8& /*aDes*/) const
       
   108 //
       
   109 // Return the media drivers capabilities.
       
   110 //
       
   111 	{
       
   112 	__DEBUG_PRINT("DPhysicalDeviceMediaNVMemory::GetCaps()");
       
   113 	}
       
   114 
       
   115 TInt DPhysicalDeviceMediaNVMemory::Create(DBase*& aChannel, TInt aMediaId, const TDesC8* /* anInfo */,const TVersion &aVer)
       
   116 //
       
   117 // Create an Internal Ram media driver.
       
   118 //
       
   119 	{
       
   120 	__DEBUG_PRINT("DPhysicalDeviceMediaNVMemory::Create()");
       
   121 	if (!Kern::QueryVersionSupported(iVersion,aVer))
       
   122 		return KErrNotSupported;
       
   123 	TInt r=KErrNoMemory;
       
   124 	DMediaDriverNVMemory* pD=new DMediaDriverNVMemory(aMediaId);
       
   125 	aChannel=pD;
       
   126 	if (pD)
       
   127 		{
       
   128 		r=pD->DoCreate(aMediaId);
       
   129 		}
       
   130 	if( r==KErrNone )
       
   131 		{
       
   132 		pD->OpenMediaDriverComplete(KErrNone);
       
   133 		}
       
   134 
       
   135 	return r;
       
   136 	}
       
   137 
       
   138 TInt DPhysicalDeviceMediaNVMemory::Validate(TInt aDeviceType, const TDesC8* /*anInfo*/, const TVersion& aVer)
       
   139 	{
       
   140 	__DEBUG_PRINT("DPhysicalDeviceMediaNVMemory::Validate()");
       
   141 	if (!Kern::QueryVersionSupported(iVersion,aVer))
       
   142 		return KErrNotSupported;
       
   143 	if (aDeviceType!=EFixedMedia1)
       
   144 		return KErrNotSupported;
       
   145 	return KErrNone;
       
   146 	}
       
   147 
       
   148 TInt DPhysicalDeviceMediaNVMemory::Info(TInt aFunction, TAny*)
       
   149 //
       
   150 // Return the priority of this media driver
       
   151 //
       
   152 	{
       
   153 	__DEBUG_PRINT("DPhysicalDeviceMediaNVMemory::Info()");
       
   154 	if (aFunction==EPriority)
       
   155 		return KMediaDriverPriorityNormal;
       
   156 	return KErrNotSupported;
       
   157 	}
       
   158 
       
   159 DMediaDriverNVMemory::DMediaDriverNVMemory(TInt aMediaId)
       
   160 //
       
   161 // Constructor.
       
   162 //
       
   163 	:	DMediaDriver(aMediaId),
       
   164 		iSessionEndDfc(DMediaDriverNVMemory::SessionEndDfc, this, 1),
       
   165 		iTransferBufferPhys(0),
       
   166 		iTransactionLaunchDfc(DMediaDriverNVMemory::TransactionLaunchDfc, this, KMaxDfcPriority)
       
   167 	{
       
   168 	__DEBUG_PRINT("DMediaDriverNVMemory::DMediaDriverNVMemory()");
       
   169 	}
       
   170 
       
   171 TInt DMediaDriverNVMemory::DoCreate(TInt /*aMediaId*/)
       
   172 //
       
   173 // Create the media driver.
       
   174 //
       
   175 	{
       
   176 	__DEBUG_PRINT(">DMediaDriverNVMemory::DoCreate");
       
   177 	TInt r = KErrNone; 
       
   178 	// Inform our size
       
   179 	Int64 size=GetNVMemSize()<<KDiskSectorShift;
       
   180 	if( size<=0 )
       
   181 		{
       
   182 		Kern::Fault("DMediaDriverNVMemory zero size nv memory array", 0);
       
   183 		}
       
   184 	SetTotalSizeInBytes(size);
       
   185 	// Some dfc initialization
       
   186 	if( r==KErrNone )
       
   187 		{
       
   188 		iSessionEndDfc.SetDfcQ( this->iPrimaryMedia->iDfcQ );
       
   189 		iTransactionLaunchDfc.SetDfcQ( this->iPrimaryMedia->iDfcQ );
       
   190 		}
       
   191 	// Create our piece of physically contiguous transfer buffer. 
       
   192 	r = Epoc::AllocPhysicalRam( KNVMemTransferBufferSize, iTransferBufferPhys );
       
   193 	if( r != KErrNone )
       
   194 		{
       
   195 		Kern::Fault("DMediaDriverNVMemory Allocate Ram %d",r);
       
   196 		}
       
   197 
       
   198     DPlatChunkHw* bufChunk = NULL;
       
   199 
       
   200     // Create HW Memory Chunk
       
   201     r = DPlatChunkHw::New( bufChunk, iTransferBufferPhys, KNVMemTransferBufferSize, EMapAttrUserRw | EMapAttrFullyBlocking );
       
   202 
       
   203     if( r != KErrNone )
       
   204         {
       
   205         // Check Physical Memory
       
   206         if( iTransferBufferPhys )
       
   207 			{
       
   208             // Free Physical Memory
       
   209             Epoc::FreePhysicalRam( iTransferBufferPhys, KNVMemTransferBufferSize );
       
   210 			}
       
   211 		Kern::Fault("DMediaDriverNVMemory error in creating transfer buffer", r);
       
   212         }
       
   213 
       
   214     // Set Main Buffer Pointer
       
   215     iTransferBufferLin = reinterpret_cast<TUint8*>(bufChunk->LinearAddress());
       
   216 
       
   217   	// Inform "hardware" about the shared memory
       
   218   	WriteReg( KHwNVMemoryDevice, R_NVMEM_SHARED_MEMORY_BASE, iTransferBufferPhys );
       
   219   	WriteReg( KHwNVMemoryDevice, R_NVMEM_SHARED_MEMORY_SIZE, KNVMemTransferBufferSize );
       
   220   	WriteReg( KHwNVMemoryDevice, R_NVMEM_ENABLE, 1 );
       
   221 
       
   222 	// Set up interrupt service
       
   223   	r = Interrupt::Bind( EIntNVMemoryDevice, Isr, this );
       
   224   	Interrupt::Enable( EIntNVMemoryDevice );
       
   225 
       
   226 
       
   227 	__DEBUG_PRINT("<DMediaDriverNVMemory::DoCreate %d", r);
       
   228 	return(r);
       
   229 	}
       
   230 
       
   231 TInt DMediaDriverNVMemory::Request(TLocDrvRequest& m)
       
   232 	{
       
   233 	TInt request=m.Id();
       
   234 	__DEBUG_PRINT(">DMediaDriverNVMemory::Request %d",request);
       
   235 	TInt r=KErrNone;
       
   236 	
       
   237 	// Requests that can be handled synchronously
       
   238 	if( request == DLocalDrive::ECaps ) 
       
   239 		{
       
   240 		r=Caps(m);
       
   241 		return r;
       
   242 		}
       
   243 
       
   244 	// All other requests must be deferred if a request is currently in progress
       
   245 	if (iCurrentRequest)
       
   246 		{
       
   247 		// a request is already in progress, so hold on to this one
       
   248 		r = KMediaDriverDeferRequest;
       
   249 		}
       
   250 	else
       
   251 		{
       
   252 
       
   253 		iCurrentRequest=&m;
       
   254 		iProsessedLength = 0;
       
   255 		iHead = 0;
       
   256 		iTail = 0;
       
   257 		iSplitted = 0;
       
   258 		iAlignmentOverhead = 0;
       
   259 
       
   260 		iPos = m.Pos();
       
   261 		iTotalLength = m.Length();
       
   262 
       
   263 
       
   264 		__DEBUG_PRINT(">DMediaDriverNVMemory::Request pos:0x%lx len:0x%lx", iPos, iTotalLength);
       
   265 		if( iTotalLength<0 || iPos<0 )
       
   266 			{
       
   267 			Kern::Fault("DMediaDriverNVMemory::Request: illegal access!", 0);
       
   268 			}
       
   269 		// Handle unaligned operations
       
   270 		iHead = iPos % KDiskSectorSize;
       
   271 		TUint32 tailAlignment = ((iHead + iTotalLength) % KDiskSectorSize);
       
   272 		if( tailAlignment )
       
   273 			{
       
   274 			iTail = KDiskSectorSize - tailAlignment;
       
   275 			}
       
   276 
       
   277 		iSplitted = (iTotalLength + iHead + iTail) / KNVMemTransferBufferSize;
       
   278 
       
   279 		__DEBUG_PRINT(">DMediaDriverNVMemory::Request head: %d tail: %d splitted: %d\n", iHead, iTail, iSplitted );
       
   280 		__DEBUG_PRINT(">DMediaDriverNVMemory::Request partitionlen: %lx", iCurrentRequest->Drive()->iPartitionLen );
       
   281 
       
   282 		if( (iTotalLength + iPos) > iCurrentRequest->Drive()->iPartitionLen )
       
   283 			{
       
   284 			Kern::Fault("DMediaDriverNVMemory::Request: Access over partition boundary!", 0);
       
   285 			}
       
   286 		if( iTotalLength > KMaxTInt32 )
       
   287 			{
       
   288 			Kern::Fault("DMediaDriverNVMemory::Request: Access length overflow!", 0);
       
   289 			}
       
   290 		switch (request)
       
   291 			{
       
   292 			case DLocalDrive::ERead:
       
   293 			case DLocalDrive::EWrite:
       
   294 			case DLocalDrive::EFormat:
       
   295 				__DEBUG_PRINT("DMediaDriverNVMemory::Request iTransactionLaunchDfc.Enque()>");
       
   296 				iTransactionLaunchDfc.Enque();
       
   297 				__DEBUG_PRINT("DMediaDriverNVMemory::Request iTransactionLaunchDfc.Enque()<");
       
   298 			break;
       
   299 			case DLocalDrive::EEnlarge:
       
   300 			case DLocalDrive::EReduce:
       
   301 			default:
       
   302 				r=KErrNotSupported;
       
   303 			break;
       
   304 			}
       
   305 		}
       
   306 
       
   307 	__DEBUG_PRINT("<DMediaDriverNVMemory::Request %d",r);
       
   308 	return r;
       
   309 	}
       
   310 
       
   311 void DMediaDriverNVMemory::CompleteRequest(TInt aReason)
       
   312 //
       
   313 // completes the request which is being currently processed
       
   314 //
       
   315 	{
       
   316 	__DEBUG_PRINT("DMediaDriverNVMemory::CompleteRequest() reason: %d", aReason);
       
   317 	TLocDrvRequest* pR=iCurrentRequest;
       
   318 	if (pR)
       
   319 		{
       
   320 		iCurrentRequest=NULL;
       
   321 		DMediaDriver::Complete(*pR,aReason);
       
   322 		}
       
   323 	}
       
   324 
       
   325 void DMediaDriverNVMemory::Disconnect( DLocalDrive* aLocalDrive, TThreadMessage* aMsg )
       
   326 	{
       
   327 	__DEBUG_PRINT(">DMediaDriverNVMemory::Disconnect()");
       
   328 	// Complete using the default implementation
       
   329 	DMediaDriver::Disconnect(aLocalDrive, aMsg);
       
   330 	__DEBUG_PRINT("<DMediaDriverNVMemory::Disconnect()");
       
   331 	}
       
   332 
       
   333 void DMediaDriverNVMemory::NotifyPowerDown()
       
   334 	{
       
   335 	__DEBUG_PRINT("DMediaDriverNVMemory::NotifyPowerDown()");
       
   336 	// no action required
       
   337 	}
       
   338 
       
   339 void DMediaDriverNVMemory::NotifyEmergencyPowerDown()
       
   340 	{
       
   341 	__DEBUG_PRINT("DMediaDriverNVMemory::NotifyEmergencyPowerDown()");
       
   342 	// no action required
       
   343 	}
       
   344 
       
   345 TInt DMediaDriverNVMemory::Caps(TLocDrvRequest& m)
       
   346 	{
       
   347 	__DEBUG_PRINT("DMediaDriverNVMemory::Caps()");
       
   348 	TLocalDriveCapsV6& caps=*(TLocalDriveCapsV6*)m.RemoteDes();
       
   349 	caps.iType=EMediaHardDisk;
       
   350 	caps.iConnectionBusType=EConnectionBusInternal;
       
   351 	caps.iDriveAtt=KDriveAttLocal|KDriveAttInternal;
       
   352 	caps.iMediaAtt=KMediaAttFormattable;
       
   353 	caps.iFileSystemId=KDriveFileSysFAT;
       
   354 	caps.iPartitionType=KPartitionTypeFAT16;
       
   355 	caps.iSize=m.Drive()->iPartitionLen;
       
   356 	caps.iHiddenSectors=0;
       
   357 	caps.iEraseBlockSize = KNVMemTransferBufferSize; 
       
   358 	caps.iBlockSize=KDiskSectorSize;
       
   359 	caps.iMaxBytesPerFormat = KNVMemTransferBufferSize;
       
   360 
       
   361 	return KErrCompletion;									
       
   362 	}
       
   363 
       
   364 TInt DMediaDriverNVMemory::Read()
       
   365 	{
       
   366 	__DEBUG_PRINT(">DMediaDriverNVMemory::Read() pos: %lx, size: %lx", iPos, iTotalLength);
       
   367 	// Set our sector offset
       
   368 	TUint32 transactionSectorOffset = (TUint32)(iPos / KDiskSectorSize); 
       
   369 	TUint32 transactionLength = 0;
       
   370 	TUint32 transactionDirection = NVMEM_TRANSACTION_READ;
       
   371 	// Do we have an operation longer than our shared memory?
       
   372 	if( iSplitted > 0 )
       
   373 		{
       
   374 		transactionLength = KNVMemTransferBufferSize;
       
   375 		}
       
   376 	else
       
   377 		{
       
   378 		// Do the whole operation in one go since we have enough room in our memory
       
   379 		transactionLength = I64LOW(iTotalLength);
       
   380 		// Read the "broken" tail sector
       
   381 		if( iTail )
       
   382 			{
       
   383 			transactionLength += iTail;
       
   384 			iAlignmentOverhead += iTail;
       
   385 			}
       
   386 		}
       
   387 	// Read the "broken" head sector
       
   388 	if( iHead > 0 )
       
   389 		{
       
   390 		transactionLength += iHead;
       
   391 		iAlignmentOverhead += iHead;
       
   392 		}
       
   393 
       
   394 	// We should be ok to continue
       
   395 	ContinueTransaction( transactionSectorOffset, transactionLength/KDiskSectorSize, transactionDirection );
       
   396 	__DEBUG_PRINT("<DMediaDriverNVMemory::Read()");
       
   397 	return KErrNone;
       
   398 	}
       
   399 
       
   400 TInt DMediaDriverNVMemory::Write()
       
   401 	{
       
   402 	__DEBUG_PRINT("DMediaDriverNVMemory::Write() pos: 0x%lx, size: 0x%lx", iPos, iTotalLength);
       
   403 	TInt r = KErrNone;
       
   404 	// Set our sector offset
       
   405 	TUint32 transactionSectorOffset = (TUint32)(iPos / KDiskSectorSize); 
       
   406 	TUint32 transactionLength = 0;
       
   407 	TUint32 transactionDirection = NVMEM_TRANSACTION_WRITE;
       
   408 	// Do we have an operation longer than our shared memory?
       
   409 	if( iSplitted > 0 )
       
   410 		{
       
   411 		transactionLength = KNVMemTransferBufferSize;
       
   412 		}
       
   413 	else
       
   414 		{
       
   415 		// Do the whole operation in one go since we have enough room in our memory
       
   416 		transactionLength = I64LOW(iTotalLength);
       
   417 		if( iTail )
       
   418 			{
       
   419 			iReadModifyWrite = ETrue;
       
   420 			// Read the "broken" tail sector
       
   421 			transactionLength += iTail;
       
   422 			iAlignmentOverhead += iTail;
       
   423 			}
       
   424 		}
       
   425 	// Is there a need to read modify write the "broken" head sector of the operation
       
   426 	if( iHead > 0 )
       
   427 		{
       
   428 		iReadModifyWrite = ETrue;
       
   429 		// If splitted operation we only need the broken sector
       
   430 		if( iSplitted > 0 )
       
   431 			{
       
   432 			transactionLength = KDiskSectorSize;
       
   433 			iAlignmentOverhead += iHead;
       
   434 			}
       
   435 		else
       
   436 			{
       
   437 			// Read the "broken" head sector in addition to everything else
       
   438 			transactionLength += iHead;
       
   439 			iAlignmentOverhead += iHead;
       
   440 			}
       
   441 		}
       
   442 	
       
   443 	// Was there a need to read-modify before writing
       
   444 	if( iReadModifyWrite )
       
   445 		{
       
   446 		transactionDirection = NVMEM_TRANSACTION_READ;
       
   447 		}
       
   448 	else
       
   449 		{
       
   450 		// Handle format here
       
   451 		if( iCurrentRequest->Id() == DLocalDrive::EFormat )
       
   452 			{
       
   453 			// Not much handling just flow through since we have filled the shared memory with zeroes already
       
   454 			}
       
   455 		else
       
   456 			{
       
   457 			// Read from client
       
   458 			TPtr8 targetDescriptor(iTransferBufferLin, transactionLength);
       
   459 			r = iCurrentRequest->ReadRemote(&targetDescriptor,0);
       
   460 			}
       
   461 		}
       
   462 	
       
   463 	// We should be ok to continue
       
   464 	ContinueTransaction( transactionSectorOffset, transactionLength/KDiskSectorSize, transactionDirection );
       
   465 
       
   466 	return r;
       
   467 	}
       
   468 
       
   469 
       
   470 TInt DMediaDriverNVMemory::Format()
       
   471 	{
       
   472 	__DEBUG_PRINT("DMediaDriverNVMemory::Format() pos: 0x%lx, size: 0x%lx", iPos, iTotalLength);
       
   473 	memset( iTransferBufferLin, 0x00, KNVMemTransferBufferSize );
       
   474 	// Stop the nonsense here. Write operations should be used for partial sector data removal operations
       
   475 	if( iHead > 0 || iTail > 0 )
       
   476 		{
       
   477 		Kern::Fault("DMediaDriverNVMemory::Format: alignment violation!", 0);
       
   478 		}
       
   479 	Write();
       
   480 //	DoTransaction( m, NVMEM_TRANSACTION_WRITE );
       
   481 	return KErrNone;
       
   482 	}
       
   483 
       
   484 TInt DMediaDriverNVMemory::ContinueTransaction( TUint32 aTransactionSectorOffset, TUint32 aTransactionSectorCount, TUint32 aTransactionDirection )
       
   485 	{
       
   486 	__DEBUG_PRINT("DMediaDriverNVMemory::ContinueTransaction() sectoroffset: %d, sectorcount: %d, direction: %d", aTransactionSectorOffset, aTransactionSectorCount, aTransactionDirection);
       
   487 	if( aTransactionDirection != NVMEM_TRANSACTION_UNDEFINED )
       
   488 		{
       
   489 	  	WriteReg( KHwNVMemoryDevice, R_NVMEM_TRANSACTION_OFFSET, aTransactionSectorOffset );
       
   490 	  	WriteReg( KHwNVMemoryDevice, R_NVMEM_TRANSACTION_SIZE, aTransactionSectorCount );
       
   491 	  	WriteReg( KHwNVMemoryDevice, R_NVMEM_TRANSACTION_DIRECTION, aTransactionDirection );
       
   492 	  	WriteReg( KHwNVMemoryDevice, R_NVMEM_TRANSACTION_EXECUTE, aTransactionDirection );
       
   493 		}
       
   494 	else
       
   495 		{
       
   496 		Kern::Fault("DMediaDriverNVMemory::ContinueTransaction: Undefined transaction!", 0);
       
   497 		}
       
   498 	return KErrNone;
       
   499 	}
       
   500 
       
   501 
       
   502 TInt DMediaDriverNVMemory::PartitionInfo(TPartitionInfo& anInfo)
       
   503 //
       
   504 // Return partition information on the media.
       
   505 //
       
   506 	{
       
   507 	__DEBUG_PRINT("DMediaDriverNVMemory::PartitionInfo()");
       
   508 	anInfo.iPartitionCount=1;
       
   509 	anInfo.iEntry[0].iPartitionBaseAddr=0;
       
   510 	anInfo.iEntry[0].iPartitionLen=anInfo.iMediaSizeInBytes=TotalSizeInBytes();
       
   511 	anInfo.iEntry[0].iPartitionType=KPartitionTypeFAT16;
       
   512 	return KErrCompletion;
       
   513 	}
       
   514 
       
   515 void DMediaDriverNVMemory::TransactionLaunchDfc(TAny* aMediaDriver)
       
   516 	{
       
   517 	static_cast<DMediaDriverNVMemory*>(aMediaDriver)->DoTransactionLaunchDfc();
       
   518 	}
       
   519 
       
   520 void DMediaDriverNVMemory::DoTransactionLaunchDfc()
       
   521 	{
       
   522 	__DEBUG_PRINT(">DMediaDriverNVMemory::DoTransactionLaunchDfc()");
       
   523 	TInt request = iCurrentRequest->Id();
       
   524 	TInt r(KErrNone);
       
   525 	switch (request)
       
   526 		{
       
   527 		case DLocalDrive::ERead:
       
   528 			r=Read();
       
   529 		break;
       
   530 		case DLocalDrive::EWrite:
       
   531 			r=Write();
       
   532 		break;
       
   533 		case DLocalDrive::EFormat:
       
   534 			r=Format();
       
   535 		break;
       
   536 		case DLocalDrive::EEnlarge:
       
   537 		case DLocalDrive::EReduce:
       
   538 		default:
       
   539 			r=KErrNotSupported;
       
   540 		break;
       
   541 		}
       
   542 	if( r != KErrNone )
       
   543 		{
       
   544 		// TODO some proper error handling here
       
   545 		}
       
   546 	__DEBUG_PRINT("<MediaDriverNVMemory::DoTransactionLaunchDfc %d",r);
       
   547 	}
       
   548 
       
   549 void DMediaDriverNVMemory::SessionEndDfc(TAny* aMediaDriver)
       
   550 	{
       
   551 	static_cast<DMediaDriverNVMemory*>(aMediaDriver)->DoSessionEndDfc();
       
   552 	}
       
   553 
       
   554 void DMediaDriverNVMemory::DoSessionEndDfc()
       
   555 	{
       
   556 	__DEBUG_PRINT(">DMediaDriverNVMemory::DoSessionEndDfc()");
       
   557 	TInt r = KErrNone;
       
   558 	// Check that we have a request in process
       
   559 	if( iCurrentRequest )
       
   560 		{
       
   561 		// Transaction variables
       
   562 		TUint32 transactionSectorOffset(0);
       
   563 		TUint32 transactionLength(0);
       
   564 		TUint32 transactionDirection( NVMEM_TRANSACTION_UNDEFINED );
       
   565 		// How much did we actually transfer? 
       
   566 		TUint32 latestTransferSize = (iLatestTransferSectorCount * KDiskSectorSize);
       
   567 		__DEBUG_PRINT("DMediaDriverNVMemory::DoSessionEndDfc() latestTransferSize: %d", latestTransferSize );
       
   568 		// Subtract alignment overhead
       
   569 		latestTransferSize = latestTransferSize - iAlignmentOverhead;
       
   570 		// For decision whether the buffer is ready for operation already
       
   571 		TBool bufferReady(EFalse);
       
   572 		// For decision whether we have finished the latest request
       
   573 		TBool sessionComplete(EFalse);
       
   574 
       
   575 		// Was there a read-modify-write (RWM) for which we need to do some buffer manipulation before proceeding?
       
   576 		// Note that in case of format we triggered to alignment violation in earlier method already and can not enter to following 
       
   577 		// condition when there is a format operation going on
       
   578 		if( iReadModifyWrite )
       
   579 			{
       
   580 			bufferReady = ETrue;
       
   581 			iReadModifyWrite = EFalse;
       
   582 			// Was it a splitted operation for which we only need to take care of the broken head sector.
       
   583 			if( iSplitted > 0 )
       
   584 				{
       
   585 				// We have a sector here here filled with data from mass memory. Modify with client data.
       
   586 				__DEBUG_PRINT("DMediaDriverNVMemory::DoSessionEndDfc() readremote splitted: %d head: %d", latestTransferSize, iHead );
       
   587 				TPtr8 targetDescriptor(&iTransferBufferLin[iHead], KNVMemTransferBufferSize - iHead);
       
   588 				r = iCurrentRequest->ReadRemote(&targetDescriptor,0);
       
   589 				}
       
   590 			// Else we need to take care of both head and tail
       
   591 			else
       
   592 				{
       
   593 				// We have a piece of data read from mass memory. Modify with client data.
       
   594 				__DEBUG_PRINT("DMediaDriverNVMemory::DoSessionEndDfc() readremote: %d head: %d", I64LOW(iTotalLength - iProsessedLength), iHead );
       
   595 				TPtr8 targetDescriptor(&iTransferBufferLin[iHead], I64LOW(iTotalLength - iProsessedLength));
       
   596 				r = iCurrentRequest->ReadRemote(&targetDescriptor,0);
       
   597 //				latestTransferSize -= (KDiskSectorSize - iTail);
       
   598 				iTail = 0;
       
   599 				}
       
   600 			}
       
   601 		else
       
   602 			{
       
   603 			// Overhead is processed when we enter here
       
   604 			iAlignmentOverhead = 0;
       
   605 			// Update position
       
   606 			iPos += latestTransferSize;
       
   607 			// Save the information on how many bytes we transferred already
       
   608 			iProsessedLength += latestTransferSize; 
       
   609 			// Update the splitted information. We don't take head into account here anymore since it is already taken care of
       
   610 			iSplitted = (iTotalLength - iProsessedLength + iTail) / KNVMemTransferBufferSize;
       
   611 			// Check if we have done already
       
   612 			if( iProsessedLength >= iTotalLength )
       
   613 				{
       
   614 				// If this was the final transfer for this request let's take tail into account as well (if not taken already)
       
   615 				// iProsessedLength -= iTail;
       
   616 				// latestTransferSize -= iTail;
       
   617 				if( iProsessedLength > iTotalLength )
       
   618 					{
       
   619 					Kern::Fault("DMediaDriverNVMemory: Illegal transfer operation!", 0);
       
   620 					}
       
   621 				sessionComplete = ETrue;
       
   622 				}
       
   623 			}
       
   624 
       
   625 		TInt request = iCurrentRequest->Id();
       
   626 
       
   627 		// Set our sector offset
       
   628 		transactionSectorOffset = (TUint32)(iPos / KDiskSectorSize); 
       
   629 		
       
   630 		if( bufferReady )
       
   631 			{
       
   632 			// Write as much as we read in RMW operation
       
   633 			transactionLength = (iLatestTransferSectorCount * KDiskSectorSize);
       
   634 			}
       
   635 		else
       
   636 			{
       
   637 			// Do we have an operation longer than our shared memory?
       
   638 			if( iSplitted > 0 )
       
   639 				{
       
   640 				transactionLength = KNVMemTransferBufferSize;
       
   641 				}
       
   642 			else
       
   643 				{
       
   644 				// Do the whole operation in one go since we have enough room in our memory
       
   645 				transactionLength = I64LOW(iTotalLength - iProsessedLength);
       
   646 				// Read the "broken" tail sector
       
   647 				if( iTail > 0 && request == DLocalDrive::EWrite )
       
   648 					{
       
   649 					iReadModifyWrite = ETrue;
       
   650 					// Read the "broken" tail sector
       
   651 					transactionLength += iTail;
       
   652 	   				iAlignmentOverhead = iTail;
       
   653 	   				}
       
   654 				}
       
   655 			}
       
   656 		
       
   657 		// Was there a need to read-modify before writing
       
   658 		if( iReadModifyWrite )
       
   659 			{
       
   660 			transactionDirection = NVMEM_TRANSACTION_READ;
       
   661 			}
       
   662 		else
       
   663 			{
       
   664 			if( request == DLocalDrive::ERead )
       
   665 				{
       
   666 				transactionDirection = NVMEM_TRANSACTION_READ;
       
   667 				// Write to client
       
   668 				__DEBUG_PRINT("DMediaDriverNVMemory::DoSessionEndDfc() WriteRemote: %d head: %d", latestTransferSize, iHead );
       
   669 				TPtrC8 sourceDescriptor(&iTransferBufferLin[iHead], latestTransferSize);
       
   670 				r = iCurrentRequest->WriteRemote( &sourceDescriptor, 0 );
       
   671 				}
       
   672 			// Head is processed
       
   673 			iHead = 0;
       
   674 			if( request == DLocalDrive::EWrite && !sessionComplete )
       
   675 				{
       
   676 				transactionDirection = NVMEM_TRANSACTION_WRITE;
       
   677 				if( bufferReady )
       
   678 					{
       
   679 					// Actually no need for any actions here
       
   680 					}
       
   681 				else
       
   682 					{
       
   683 					// Prepare a buffer for transfer
       
   684 					__DEBUG_PRINT("DMediaDriverNVMemory::DoSessionEndDfc() ReadRemote: %d head: %d", latestTransferSize, iHead );
       
   685 					TPtr8 targetDescriptor(iTransferBufferLin, transactionLength);
       
   686 					r = iCurrentRequest->ReadRemote(&targetDescriptor,0);
       
   687 					}
       
   688 				}
       
   689 			if( request == DLocalDrive::EFormat )
       
   690 				{
       
   691 				transactionDirection = NVMEM_TRANSACTION_WRITE;
       
   692 				}
       
   693 			}
       
   694 		if( sessionComplete )
       
   695 			{
       
   696 			CompleteRequest( r );
       
   697 			}
       
   698 		else
       
   699 			{
       
   700 			ContinueTransaction( transactionSectorOffset, transactionLength/KDiskSectorSize, transactionDirection );
       
   701 			}
       
   702 		}
       
   703 	else
       
   704 		{
       
   705 		// Let's just flow through for now
       
   706 		}
       
   707 
       
   708 	__DEBUG_PRINT("<DMediaDriverNVMemory::DoSessionEndDfc()" );
       
   709 	}
       
   710 
       
   711 
       
   712 TUint32  DMediaDriverNVMemory::GetNVMemSize( void )
       
   713 	{
       
   714 	__DEBUG_PRINT("DMediaDriverNVMemory::GetNVMemSize()");
       
   715 	TUint32 sizeInSectors = ReadReg( KHwNVMemoryDevice, R_NVMEM_NV_MEMORY_SIZE );
       
   716 	return sizeInSectors; 
       
   717 	}
       
   718 
       
   719 void DMediaDriverNVMemory::Isr(TAny* aPtr)
       
   720 	{
       
   721 	__DEBUG_PRINT(">DMediaDriverNVMemory::Isr");
       
   722 
       
   723     DMediaDriverNVMemory* nvMem = reinterpret_cast<DMediaDriverNVMemory*>(aPtr);
       
   724 
       
   725 	// Save the amount of transferred sectors. This clears the interrupt from HW as well
       
   726 	nvMem->iLatestTransferSectorCount = ReadReg( KHwNVMemoryDevice, R_NVMEM_STATUS );
       
   727 
       
   728 	// Clear from framework
       
   729 	Interrupt::Clear( EIntNVMemoryDevice );
       
   730 
       
   731 	nvMem->iSessionEndDfc.Add();
       
   732 	}
       
   733 
       
   734 
       
   735 DECLARE_EXTENSION_PDD()
       
   736 	{
       
   737 	__DEBUG_PRINT(">MediaDriverNVMemory create device");
       
   738 	return new DPhysicalDeviceMediaNVMemory;
       
   739 	}
       
   740 
       
   741 static const TInt NVMemDriveNumbers[NVMEM1_DRIVECOUNT]={NVMEM1_DRIVELIST};	
       
   742 
       
   743 DECLARE_STANDARD_EXTENSION()
       
   744 	{
       
   745 	__DEBUG_PRINT("Registering NVMEM drive");
       
   746 	TInt r=KErrNoMemory;
       
   747 
       
   748 	DPrimaryMediaBase* pM=new DPrimaryMediaBase;
       
   749 
       
   750 	TDynamicDfcQue* NVMemoryDfcQ;
       
   751 	 
       
   752 	r = Kern::DynamicDfcQCreate( NVMemoryDfcQ, KNVMemDfcThreadPriority, KNVMemDriveName );
       
   753 
       
   754 	if( r == KErrNone )
       
   755 		{
       
   756 		pM->iDfcQ = NVMemoryDfcQ;
       
   757 		}
       
   758 	else
       
   759 		{
       
   760 		__DEBUG_PRINT("NVMEM DFCQ initialization failed");
       
   761 		}
       
   762 
       
   763 	if (pM)
       
   764 		{
       
   765 		r=LocDrv::RegisterMediaDevice(EFixedMedia1,NVMEM1_DRIVECOUNT,&NVMemDriveNumbers[0],pM,NVMEM1_NUMMEDIA,KNVMemDriveName);
       
   766 		}
       
   767 
       
   768 	pM->iMsgQ.Receive();
       
   769 
       
   770 	__DEBUG_PRINT("Registering NVMEM drive - return %d",r);
       
   771 
       
   772 	return r;
       
   773 	}