omap3530/beagle_drivers/medwb/medwb.cpp
changeset 27 117faf51deac
child 35 2905910218db
equal deleted inserted replaced
26:b7e488c49d0d 27:117faf51deac
       
     1 /* Cypress West Bridge API source file 
       
     2 ## ===========================
       
     3 ##
       
     4 ##  Copyright Cypress Semiconductor Corporation, 2006-2009,
       
     5 ##  All Rights Reserved
       
     6 ##  UNPUBLISHED, LICENSED SOFTWARE.
       
     7 ##
       
     8 ##  CONFIDENTIAL AND PROPRIETARY INFORMATION
       
     9 ##  WHICH IS THE PROPERTY OF CYPRESS.
       
    10 ##
       
    11 ##  Use of this file is governed
       
    12 ##  by the license agreement included in the file
       
    13 ##
       
    14 ##     <install>/license/license.txt
       
    15 ##
       
    16 ##  where <install> is the Cypress software
       
    17 ##  installation root directory path.
       
    18 ##
       
    19 ## ===========================
       
    20 */
       
    21 
       
    22 #include "locmedia.h"
       
    23 #include "platform.h"
       
    24 //#include "variantmediadef.h"
       
    25 #include <assp\omap3530_assp\CyAsSymbianStorageDriver.h>
       
    26 
       
    27 //#define REGIST_MEDIA_USE_MMC
       
    28 #define _MEDWB_DEBUG_1_
       
    29 //#define _MEDWB_DEBUG_2_
       
    30 
       
    31 //#define INTERVAL_FOR_WB 15 // 15 -- OK
       
    32 #define WB_BUFFER_SIZE	2*(65536 + 512)
       
    33 #define WB_RETRY_COUNT 2
       
    34 
       
    35 //const TInt KStackNumber = 0;
       
    36 const TInt KDiskSectorSize=512;
       
    37 const TInt KDiskSectorShift=9;
       
    38 //const TInt KIdleCurrentInMilliAmps = 1;
       
    39 
       
    40 const TInt KMBRFirstPartitionEntry=0x1BE;
       
    41 
       
    42 TUint8	ptrWBBuffer[WB_BUFFER_SIZE];
       
    43 
       
    44 
       
    45 template <class T>
       
    46 inline T UMin(T aLeft,T aRight)
       
    47 	{return(aLeft<aRight ? aLeft : aRight);}
       
    48 
       
    49 
       
    50 class DPhysicalDeviceMediaWB : public DPhysicalDevice
       
    51 	{
       
    52 public:
       
    53 	DPhysicalDeviceMediaWB();
       
    54 
       
    55 	virtual TInt Install();
       
    56 	virtual void GetCaps(TDes8& aDes) const;
       
    57 	virtual TInt Create(DBase*& aChannel, TInt aMediaId, const TDesC8* aInfo, const TVersion& aVer);
       
    58 	virtual TInt Validate(TInt aDeviceType, const TDesC8* aInfo, const TVersion& aVer);
       
    59 	virtual TInt Info(TInt aFunction, TAny* a1);
       
    60 	};
       
    61 
       
    62 
       
    63 class DMediaDriverWB : public DMediaDriver
       
    64 	{
       
    65 public:
       
    66 	DMediaDriverWB(TInt aMediaId);
       
    67 	~DMediaDriverWB();
       
    68 	// ...from DMediaDriver
       
    69 	virtual void Close();
       
    70 	// replacing pure virtual
       
    71 	virtual void Disconnect(DLocalDrive* aLocalDrive, TThreadMessage*);
       
    72 	virtual TInt Request(TLocDrvRequest& aRequest);
       
    73 	virtual TInt PartitionInfo(TPartitionInfo& anInfo);
       
    74 	virtual void NotifyPowerDown();
       
    75 	virtual void NotifyEmergencyPowerDown();
       
    76 	// For creation by DPhysicalDeviceMediaMmcFlash
       
    77 	TInt DoCreate(TInt aMediaId);
       
    78 
       
    79 private:
       
    80 
       
    81 private:
       
    82 	// MMC device specific stuff
       
    83 	TInt DoRead(TLocDrvRequest&);
       
    84 	TInt DoWrite(TLocDrvRequest&);
       
    85 	TInt DoFormat(TLocDrvRequest&);
       
    86 	TInt Caps(TLocDrv& aDrive, TLocalDriveCapsV6& aInfo);
       
    87 
       
    88 
       
    89 	TInt DecodePartitionInfo();
       
    90 	TInt WritePartitionInfo();
       
    91 	TInt GetDefaultPartitionInfo(TMBRPartitionEntry& aPartitionEntry);
       
    92 	TInt CreateDefaultPartition();
       
    93 
       
    94 
       
    95 
       
    96 	static void SetPartitionEntry(TPartitionEntry* aEntry, TUint aFirstSector, TUint aNumSectors);
       
    97 
       
    98 	TInt CheckDevice(int aReqType);
       
    99 	
       
   100 	void Reset();
       
   101 
       
   102 private:
       
   103 	TInt iMediaId;
       
   104 	TPartitionInfo* iPartitionInfo;
       
   105 	TBool iMbrMissing;
       
   106 	TUint iHiddenSectors;						// bootup / password
       
   107 
       
   108 	TUint8* ptrWriteBuf;							// start of current buffer region
       
   109 	TUint8* ptrReadBuf;							// start of current buffer region
       
   110 	TInt	read_size;
       
   111 	TInt	read_pos;
       
   112 
       
   113 	TInt 	iUnitSize;
       
   114 	TInt	iBlockSize;
       
   115 	};
       
   116 	
       
   117 // ======== DPhysicalDeviceMediaMmcFlash ========
       
   118 
       
   119 
       
   120 DPhysicalDeviceMediaWB::DPhysicalDeviceMediaWB()
       
   121 	{
       
   122 	#ifdef _MEDWB_DEBUG_3_
       
   123 	Kern::Printf("=mmd:ctr");
       
   124 	#endif
       
   125 	iUnitsMask = 0x01;
       
   126 	iVersion = TVersion(KMediaDriverInterfaceMajorVersion,KMediaDriverInterfaceMinorVersion,KMediaDriverInterfaceBuildVersion);
       
   127 	}
       
   128 
       
   129 
       
   130 TInt DPhysicalDeviceMediaWB::Install()
       
   131 	{
       
   132 	#ifdef _MEDWB_DEBUG_3_
       
   133 	Kern::Printf("=mmd:ins");
       
   134 	#endif
       
   135 	_LIT(KDrvNm, "Media.WB");
       
   136 	return SetName(&KDrvNm);
       
   137 	}
       
   138 
       
   139 
       
   140 void DPhysicalDeviceMediaWB::GetCaps(TDes8& /* aDes */) const
       
   141 	{
       
   142 	#ifdef _MEDWB_DEBUG_3_
       
   143 	Kern::Printf("=mmd:cap");
       
   144 	#endif
       
   145 	}
       
   146 								 
       
   147 									 
       
   148 TInt DPhysicalDeviceMediaWB::Info(TInt aFunction, TAny* /*a1*/)
       
   149 //
       
   150 // Return the priority of this media driver
       
   151 //
       
   152 	{
       
   153 	#ifdef _MEDWB_DEBUG_3_
       
   154 	Kern::Printf("=mmd:info");
       
   155 	#endif
       
   156 	if (aFunction==EPriority)
       
   157 		return KMediaDriverPriorityNormal;
       
   158 	// Don't close media driver when peripheral bus powers down. This avoids the need for Caps() to power up the stack.
       
   159 	if (aFunction==EMediaDriverPersistent)
       
   160 		return KErrNone;
       
   161 	return KErrNotSupported;
       
   162 	}
       
   163 								 
       
   164 TInt DPhysicalDeviceMediaWB::Validate(TInt aDeviceType, const TDesC8* /*aInfo*/, const TVersion& aVer)
       
   165 	{
       
   166 	#ifdef _MEDWB_DEBUG_3_
       
   167 	Kern::Printf("=mmd:validate aDeviceType %d\n", aDeviceType);
       
   168 	#endif
       
   169 	if (!Kern::QueryVersionSupported(iVersion,aVer))
       
   170 	{	
       
   171 	#ifdef _MEDWB_DEBUG_3_
       
   172 		Kern::Printf("Validate -> KErrNotSupported\n");
       
   173 	#endif
       
   174 		return KErrNotSupported;
       
   175 	}
       
   176 	if (aDeviceType!=MEDIA_DEVICE_MMC)
       
   177 	{
       
   178 	#ifdef _MEDWB_DEBUG_3_
       
   179 		Kern::Printf("Validate -> Wrong DeviceType\n");
       
   180 	#endif
       
   181 		return KErrNotSupported;
       
   182 	}
       
   183 	return KErrNone;
       
   184 	}
       
   185 	
       
   186 								 
       
   187 TInt DPhysicalDeviceMediaWB::Create(DBase*& aChannel, TInt aMediaId, const TDesC8* /*aInfo*/, const TVersion& aVer)
       
   188 //
       
   189 // Create an MMC Card media driver.
       
   190 //
       
   191 	{
       
   192 	#ifdef _MEDWB_DEBUG_3_
       
   193 	Kern::Printf("=mmd:crt");
       
   194 	#endif
       
   195 	if (!Kern::QueryVersionSupported(iVersion,aVer))
       
   196 		return KErrNotSupported;
       
   197 
       
   198 	DMediaDriverWB* pD = new DMediaDriverWB(aMediaId);
       
   199 	aChannel=pD;
       
   200 
       
   201 	TInt r=KErrNoMemory;
       
   202 	if (pD)
       
   203 		r=pD->DoCreate(aMediaId);
       
   204 
       
   205 	#ifdef REGIST_MEDIA_USE_MMC
       
   206 	if (r==KErrNone)
       
   207 		pD->OpenMediaDriverComplete(KErrNone);
       
   208 	#endif
       
   209 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:mdf"));
       
   210 	return r;
       
   211 	}
       
   212 
       
   213 
       
   214 
       
   215 // Helper
       
   216 template <class T>
       
   217 inline T* KernAlloc(const TUint32 n)
       
   218 	{ return static_cast<T*>(Kern::Alloc(n * sizeof(T))); }
       
   219 
       
   220 // ---- ctor, open, close, dtor ----
       
   221 
       
   222 #pragma warning( disable : 4355 )	// this used in initializer list
       
   223 DMediaDriverWB::DMediaDriverWB(TInt aMediaId)
       
   224    :DMediaDriver(aMediaId),
       
   225 	iMediaId(iPrimaryMedia->iNextMediaId)
       
   226 	{
       
   227 	#ifdef _MEDWB_DEBUG_2_
       
   228 	Kern::Printf("=mmd:wb");
       
   229 	// NB aMedia Id = the media ID of the primary media, iMediaId = the media ID of this media
       
   230 	Kern::Printf("DMediaDriverWB(), iMediaId %d, aMediaId %d\n", iMediaId, aMediaId);
       
   231 	#endif
       
   232 	}
       
   233 
       
   234 #pragma warning( default : 4355 )
       
   235 TInt DMediaDriverWB::DoCreate(TInt /*aMediaId*/)
       
   236 	{
       
   237 	#ifdef _MEDWB_DEBUG_3_
       
   238 	Kern::Printf(">mmd:opn");
       
   239 	#endif
       
   240 	iUnitSize = CyAsSymbianStorageDriver::GetUnitSize();
       
   241 	iBlockSize = CyAsSymbianStorageDriver::GetBlockSize();
       
   242 	if( iBlockSize == 0 )
       
   243 		iBlockSize = 1;
       
   244 	
       
   245 	read_pos = 0x7FFFFFFF;
       
   246 	read_size = 0;
       
   247 
       
   248 	// get card characteristics
       
   249 	SetTotalSizeInBytes(CyAsSymbianStorageDriver::GetMediaSize());
       
   250 	
       
   251 	// get buffer memory from EPBUS
       
   252 	ptrReadBuf = ptrWBBuffer;
       
   253 	ptrWriteBuf = &ptrWBBuffer[65536+512];
       
   254 	
       
   255 	#ifdef _MEDWB_DEBUG_3_
       
   256 	Kern::Printf("<mmd:opn");
       
   257 	#endif
       
   258 	return(KErrNone);
       
   259 	}
       
   260 
       
   261 void DMediaDriverWB::Close()
       
   262 //
       
   263 // Close the media driver - also called on media change
       
   264 //
       
   265 	{
       
   266 	#ifdef _MEDWB_DEBUG_3_
       
   267 	Kern::Printf("=mmd:cls");
       
   268 	#endif
       
   269 	EndInCritical();
       
   270 	//CompleteRequest(KErrNotReady);
       
   271 	DMediaDriver::Close();
       
   272 	}
       
   273 
       
   274 
       
   275 DMediaDriverWB::~DMediaDriverWB()
       
   276 	{
       
   277 
       
   278 	}
       
   279 
       
   280 
       
   281 // ---- media access ----
       
   282 
       
   283 TInt DMediaDriverWB::DoRead(TLocDrvRequest& iCurrentReq)
       
   284 //
       
   285 // set up iReqStart, iReqEnd and iReqCur and launch first read.  Subsequent reads
       
   286 // will be launched from the callback DFC.
       
   287 //
       
   288 	{
       
   289 	Int64 pos=iCurrentReq.Pos();
       
   290 	Int64 length=iCurrentReq.Length();
       
   291 	TInt r;
       
   292 	if (length<0 || pos<0 || (pos+length)>KMaxTInt)
       
   293 		return KErrGeneral;
       
   294 	TInt p=(TInt)pos;
       
   295 	TInt l=(TInt)length;
       
   296 	
       
   297 	if (p+l>CyAsSymbianStorageDriver::GetMediaSize())
       
   298 		return KErrGeneral;
       
   299 
       
   300 	TInt pos_block = p / iBlockSize;
       
   301 	TInt pos_offset =  p % iBlockSize;
       
   302 	TInt size_block = l / iBlockSize; 
       
   303 	TInt size_offset = l % iBlockSize; 
       
   304 	
       
   305 	TUint buf_offset = 0;
       
   306 	TInt	local_pos;
       
   307 
       
   308 	#ifdef _MEDWB_DEBUG_2_
       
   309 	Kern::Printf("WB::Read> real (%d@%d)", l, p);
       
   310 	Kern::Printf("WB::Read> pos_block - %d", pos_block) ;
       
   311 	Kern::Printf("WB::Read> pos_offset - %d", pos_offset) ;
       
   312 	Kern::Printf("WB::Read> size_block - %d", size_block) ;
       
   313 	Kern::Printf("WB::Read> size_offset - %d", size_offset) ;
       
   314 	#endif
       
   315 
       
   316 	#ifdef INTERVAL_FOR_WB
       
   317 	NKern::Sleep(INTERVAL_FOR_WB);
       
   318 	#endif
       
   319 	if( pos_block == read_pos )
       
   320 	{
       
   321 		if( read_size < l )
       
   322 		{
       
   323 			buf_offset = read_size;
       
   324 			size_block = (l-read_size) / iBlockSize;
       
   325 			size_offset = (l-read_size) % iBlockSize;
       
   326 
       
   327 			local_pos = pos_block + (read_size /iBlockSize);
       
   328 		}
       
   329 		else
       
   330 		{
       
   331 			TPtrC8 des((ptrReadBuf+pos_offset), l);
       
   332 				
       
   333 			r = iCurrentReq.WriteRemote(&des, 0);
       
   334 			
       
   335 			return r;
       
   336 		}
       
   337 	}
       
   338 	#if 0
       
   339 	else if( (read_pos + (read_size/iBlockSize)) > pos_block )
       
   340 	{
       
   341 		TInt adjust_offset;
       
   342 		TInt adjust_size;
       
   343 		adjust_offset = (read_pos - pos_block) * iBlockSize;
       
   344 		adjust_size = read_size - adjust_offset;
       
   345 		
       
   346 		memcpy(ptrReadBuf, &ptrReadBuf[adjust_offset], adjust_size );
       
   347 
       
   348 		read_pos = pos_block;
       
   349 		read_size = adjust_size;
       
   350 	
       
   351 		if( read_size < l )
       
   352 		{
       
   353 			buf_offset = read_size;
       
   354 			size_block = (l-read_size) / iBlockSize;
       
   355 			size_offset = (l-read_size) % iBlockSize;
       
   356 
       
   357 			local_pos = pos_block + (read_size /iBlockSize);
       
   358 		}
       
   359 		else
       
   360 		{
       
   361 			TPtrC8 des((ptrReadBuf+pos_offset), l);
       
   362 				
       
   363 			r = iCurrentReq.WriteRemote(&des, 0);
       
   364 			
       
   365 			return r;
       
   366 		}
       
   367 	}
       
   368 	#endif
       
   369 	else
       
   370 		local_pos = read_pos = pos_block;
       
   371 		
       
   372 	#ifdef _MEDWB_DEBUG_2_
       
   373 	Kern::Printf("WB::Read> local_pos - %d", local_pos) ;
       
   374 	Kern::Printf("WB::Read> buf_offset - %d", buf_offset) ;
       
   375 	#endif
       
   376 
       
   377 
       
   378 	if( size_block )
       
   379 	{
       
   380 		CyAsSymbianStorageDriver::Read(local_pos, size_block, (void *)(ptrReadBuf+buf_offset));
       
   381 		local_pos += size_block;
       
   382 		buf_offset += (size_block*iBlockSize);
       
   383 	#ifdef INTERVAL_FOR_WB
       
   384 		NKern::Sleep(INTERVAL_FOR_WB);
       
   385 	#endif
       
   386 	}
       
   387 	
       
   388 	if( pos_offset || size_offset )
       
   389 	{
       
   390 		CyAsSymbianStorageDriver::Read(local_pos, 1, (void *)(ptrReadBuf+buf_offset) );
       
   391 		local_pos += size_block;
       
   392 		buf_offset += iBlockSize;
       
   393 	#ifdef INTERVAL_FOR_WB
       
   394 		NKern::Sleep(INTERVAL_FOR_WB);
       
   395 	#endif
       
   396 	}
       
   397 
       
   398 	read_size = buf_offset;
       
   399 		
       
   400 	TPtrC8 des((ptrReadBuf+pos_offset), l);
       
   401 		
       
   402 	r = iCurrentReq.WriteRemote(&des, 0);
       
   403 	
       
   404 	return r;
       
   405 	}
       
   406 
       
   407 
       
   408 TInt DMediaDriverWB::DoWrite(TLocDrvRequest& iCurrentReq)
       
   409 //
       
   410 // set up iReqStart, iReqEnd, and iReqCur, and launch first write.  Any subsequent
       
   411 // writes are launched from the session end DFC.  LaunchWrite() handles pre-reading
       
   412 // any sectors that are only partially modified.
       
   413 //
       
   414 	{
       
   415 	Int64 pos = iCurrentReq.Pos();
       
   416 	Int64 length = iCurrentReq.Length();
       
   417 	TInt r;
       
   418 	if (length<0 || pos<0 || (pos+length)>KMaxTInt)
       
   419 		return KErrGeneral;
       
   420 	TInt p=(TInt)pos;
       
   421 	TInt l=(TInt)length;
       
   422 
       
   423 	if (p+l>CyAsSymbianStorageDriver::GetMediaSize())
       
   424 		return KErrGeneral;
       
   425 
       
   426 	#ifdef _MEDWB_DEBUG_2_
       
   427 	Kern::Printf("WB::Write> (%d@%d)", l, p);
       
   428 	#endif
       
   429 	#ifdef INTERVAL_FOR_WB
       
   430 	NKern::Sleep(INTERVAL_FOR_WB);
       
   431 	#endif
       
   432 	
       
   433 	TInt pos_block = p / iBlockSize;
       
   434 	TInt pos_offset =  p % iBlockSize;
       
   435 	TInt size_block = l / iBlockSize; 
       
   436 	TInt size_offset = l % iBlockSize; 
       
   437 	
       
   438 	TUint buf_offset = 0;
       
   439 	TInt	local_pos;
       
   440 	TInt	w_block_size = 0;
       
   441 
       
   442 	local_pos = pos_block;
       
   443 	
       
   444 	if( size_block )
       
   445 	{
       
   446 		CyAsSymbianStorageDriver::Read(local_pos, size_block, (void *)(ptrWriteBuf+buf_offset));
       
   447 		local_pos += size_block;
       
   448 		buf_offset += (size_block*iBlockSize);
       
   449 		w_block_size += size_block;
       
   450 	#ifdef INTERVAL_FOR_WB
       
   451 		NKern::Sleep(INTERVAL_FOR_WB);
       
   452 	#endif
       
   453 	}
       
   454 	
       
   455 	if( pos_offset || size_offset )
       
   456 	{
       
   457 		CyAsSymbianStorageDriver::Read(local_pos, 1, (void *)(ptrWriteBuf+buf_offset) );
       
   458 		local_pos += size_block;
       
   459 		buf_offset += iBlockSize;
       
   460 		w_block_size ++;
       
   461 	#ifdef INTERVAL_FOR_WB
       
   462 		NKern::Sleep(INTERVAL_FOR_WB);
       
   463 	#endif
       
   464 	}
       
   465 	
       
   466 	#ifdef _MEDWB_DEBUG_2_
       
   467 	Kern::Printf("WB::Write> pos_block - %d", pos_block) ;
       
   468 	Kern::Printf("WB::Write> pos_offset - %d", pos_offset) ;
       
   469 	Kern::Printf("WB::Write> size_block - %d", size_block) ;
       
   470 	Kern::Printf("WB::Write> size_offset - %d", size_offset) ;
       
   471 	#endif
       
   472 
       
   473 	TPtr8 des((ptrWriteBuf+pos_offset), l);
       
   474 		
       
   475 	if ( (r = iCurrentReq.ReadRemote(&des,0)) !=KErrNone)
       
   476 	{
       
   477 		return r;			
       
   478 	}
       
   479 
       
   480 
       
   481 	#ifdef _MEDWB_DEBUG_2_
       
   482 	Kern::Printf("WB::Write> local_pos - %d", local_pos) ;
       
   483 	Kern::Printf("WB::Write> w_block_size - %d", w_block_size) ;
       
   484 	#endif
       
   485 	
       
   486 	CyAsSymbianStorageDriver::Write(local_pos, w_block_size, ptrWriteBuf);
       
   487 	#ifdef INTERVAL_FOR_WB
       
   488 	NKern::Sleep(INTERVAL_FOR_WB);
       
   489 	#endif
       
   490 	return r;
       
   491 	}
       
   492 
       
   493 
       
   494 TInt DMediaDriverWB::DoFormat(TLocDrvRequest& iCurrentReq)
       
   495 	{
       
   496 	Int64 pos = iCurrentReq.Pos();
       
   497 	Int64 length = iCurrentReq.Length();
       
   498 	TInt r;
       
   499 	if (length<0 || pos<0 || (pos+length)>KMaxTInt)
       
   500 		return KErrGeneral;
       
   501 	TInt p=(TInt)pos;
       
   502 	TInt l=(TInt)length;
       
   503 
       
   504 	if (p+l>CyAsSymbianStorageDriver::GetMediaSize())
       
   505 		return KErrGeneral;
       
   506 
       
   507 	#ifdef _MEDWB_DEBUG_1_
       
   508 	Kern::Printf("WB::Format> (%d@%d)", l, p);
       
   509 	#endif
       
   510 	#ifdef INTERVAL_FOR_WB
       
   511 	NKern::Sleep(INTERVAL_FOR_WB);
       
   512 	#endif
       
   513 	TInt pos_block = p / iBlockSize;
       
   514 	TInt pos_offset =  p % iBlockSize;
       
   515 	TInt size_block = l / iBlockSize; 
       
   516 	TInt size_offset = l % iBlockSize; 
       
   517 	
       
   518 	TUint buf_offset = 0;
       
   519 	TInt	local_pos;
       
   520 	TInt	w_block_size = 0;
       
   521 
       
   522 	local_pos = pos_block;
       
   523 	
       
   524 	if( size_block )
       
   525 	{
       
   526 		CyAsSymbianStorageDriver::Read(local_pos, size_block, (void *)(ptrWriteBuf+buf_offset));
       
   527 		local_pos += size_block;
       
   528 		buf_offset += (size_block*iBlockSize);
       
   529 		w_block_size += size_block;
       
   530 	#ifdef INTERVAL_FOR_WB
       
   531 		NKern::Sleep(INTERVAL_FOR_WB);
       
   532 	#endif
       
   533 	}
       
   534 	
       
   535 	if( pos_offset || size_offset )
       
   536 	{
       
   537 		CyAsSymbianStorageDriver::Read(local_pos, 1, (void *)(ptrWriteBuf+buf_offset) );
       
   538 		local_pos += size_block;
       
   539 		buf_offset += iBlockSize;
       
   540 		w_block_size ++;
       
   541 	#ifdef INTERVAL_FOR_WB
       
   542 		NKern::Sleep(INTERVAL_FOR_WB);
       
   543 	#endif
       
   544 	}
       
   545 	
       
   546 	memclr(ptrWriteBuf + pos_offset, l);
       
   547 	
       
   548 	CyAsSymbianStorageDriver::Write(local_pos, w_block_size, ptrWriteBuf);
       
   549 	#ifdef INTERVAL_FOR_WB
       
   550 	NKern::Sleep(INTERVAL_FOR_WB);
       
   551 	#endif
       
   552 	return r;
       
   553 	}
       
   554 
       
   555 
       
   556 TInt DMediaDriverWB::PartitionInfo(TPartitionInfo& anInfo)
       
   557 //
       
   558 // Read the partition information for the media.  If the user supplied a password,
       
   559 // then unlock the card before trying to read the first sector.
       
   560 //
       
   561 	{
       
   562 	#ifdef _MEDWB_DEBUG_3_
       
   563 	Kern::Printf(">mmd:rpi");
       
   564 	#endif
       
   565 	iPartitionInfo = &anInfo;
       
   566 
       
   567 	// Assume MBR will be present or is not required
       
   568 	iMbrMissing = EFalse;
       
   569 
       
   570 	TInt r = DecodePartitionInfo();
       
   571 
       
   572 
       
   573 	if(r == KErrLocked)
       
   574 		{
       
   575 		// If the media is locked, we present a default partition entry to the local
       
   576 		// media subsystem, which will be updated when the media is finally unlocked.
       
   577 		r = CreateDefaultPartition();
       
   578 		if (r != KErrNone)
       
   579 			return r;
       
   580 		return KErrLocked;
       
   581 		}
       
   582 
       
   583 	#ifdef _MEDWB_DEBUG_3_
       
   584 	Kern::Printf("<mmd:rpi:%d", r);
       
   585 	#endif
       
   586 	// KErrNone indicates asynchronous completion
       
   587 	return r;
       
   588 	}
       
   589 
       
   590 
       
   591 
       
   592 
       
   593 
       
   594 TInt DMediaDriverWB::DecodePartitionInfo()
       
   595 //
       
   596 // decode partition info that was read into internal buffer 
       
   597 //
       
   598 	{
       
   599 	TInt partitionCount=iPartitionInfo->iPartitionCount=0;
       
   600 	TInt defaultPartitionNumber=-1;
       
   601 	TMBRPartitionEntry* pe;
       
   602 	const TUint KMBRFirstPartitionOffsetAligned = KMBRFirstPartitionOffset & ~3;
       
   603 	TInt i;
       
   604 
       
   605 	CyAsSymbianStorageDriver::Read(0, 1, (void *)ptrReadBuf);
       
   606 	read_pos = 0;
       
   607 	read_size = 512;
       
   608 	// Read of the first sector successful so check for a Master Boot Record
       
   609 	if (*(TUint16*)(&ptrReadBuf[KMBRSignatureOffset])!=0xAA55)
       
   610 		goto mbr_done;
       
   611 
       
   612 	__ASSERT_COMPILE(KMBRFirstPartitionOffsetAligned + KMBRMaxPrimaryPartitions * sizeof(TMBRPartitionEntry) <= KMBRSignatureOffset);
       
   613 
       
   614 	memmove(&ptrReadBuf[0], &ptrReadBuf[2],
       
   615 		KMBRFirstPartitionOffsetAligned + KMBRMaxPrimaryPartitions * sizeof(TMBRPartitionEntry)); 
       
   616 
       
   617 
       
   618 	for (i=0, pe = (TMBRPartitionEntry*)(&ptrReadBuf[KMBRFirstPartitionOffsetAligned]);
       
   619 		pe->iPartitionType != 0 && i < KMBRMaxPrimaryPartitions;i++,pe++)
       
   620 		{
       
   621 		if (pe->IsDefaultBootPartition())
       
   622 			{
       
   623 			SetPartitionEntry(&iPartitionInfo->iEntry[0],pe->iFirstSector,pe->iNumSectors);
       
   624 			defaultPartitionNumber=i;
       
   625 			partitionCount++;
       
   626 			break;
       
   627 			}
       
   628 		}
       
   629 
       
   630 	// Now add any other partitions
       
   631 	for (i=0, pe = (TMBRPartitionEntry*)(&ptrReadBuf[KMBRFirstPartitionOffsetAligned]);
       
   632 		pe->iPartitionType != 0 && i < KMBRMaxPrimaryPartitions;i++,pe++)
       
   633 		{
       
   634 		TBool validPartition = ETrue;	// assume partition valid
       
   635 
       
   636 		if (defaultPartitionNumber==i)
       
   637 			{
       
   638 			// Already sorted
       
   639 			}
       
   640 
       
   641 		// FAT partition ?
       
   642 		else if (pe->IsValidDosPartition() || pe->IsValidFAT32Partition())
       
   643 			{
       
   644 			SetPartitionEntry(&iPartitionInfo->iEntry[partitionCount],pe->iFirstSector,pe->iNumSectors);
       
   645 			#ifdef _MEDWB_DEBUG_1_
       
   646 			Kern::Printf("WB: FAT partition found at sector #%u", pe->iFirstSector);
       
   647 			#endif
       
   648 			partitionCount++;
       
   649 			}
       
   650 		else
       
   651 			{
       
   652 			validPartition = EFalse;
       
   653 			}
       
   654 		
       
   655 		if (validPartition && partitionCount == 1)
       
   656 			iHiddenSectors = pe->iFirstSector;
       
   657 
       
   658 		}
       
   659 
       
   660 	// Check the validity of the partition address boundaries
       
   661 	// If there is any
       
   662 	if(partitionCount > 0)
       
   663 		{
       
   664 		const TInt64 deviceSize = CyAsSymbianStorageDriver::GetMediaSize();
       
   665 		TPartitionEntry& part = iPartitionInfo->iEntry[partitionCount - 1];
       
   666 		// Check that the card address space boundary is not exceeded by the last partition
       
   667 		// In case of only 1 partition in the media check also it
       
   668 		if(part.iPartitionBaseAddr + part.iPartitionLen > deviceSize)
       
   669 			{
       
   670 			Kern::Printf("WB: MBR partition exceeds card memory space");
       
   671 			// Adjust the partition length to card address boundary
       
   672 			part.iPartitionLen = (deviceSize - part.iPartitionBaseAddr);
       
   673 
       
   674 			// Check that the base address contained valid information
       
   675 			if(part.iPartitionLen <= 0)
       
   676 				{
       
   677 				Kern::Printf("WB: Invalid base address");
       
   678 				// Invalid MBR - assume the boot sector is in the first sector
       
   679 				defaultPartitionNumber =-1; 
       
   680 				partitionCount=0;
       
   681 				}
       
   682 			}
       
   683 		// More than one partition. Go through all of them
       
   684 		if (partitionCount > 0)
       
   685 			{
       
   686 			for(i=partitionCount-1; i>0; i--)
       
   687 				{
       
   688 				const TPartitionEntry& curr = iPartitionInfo->iEntry[i];
       
   689 				TPartitionEntry& prev = iPartitionInfo->iEntry[i-1];
       
   690 				// Check if partitions overlap
       
   691 				if(curr.iPartitionBaseAddr < (prev.iPartitionBaseAddr + prev.iPartitionLen))
       
   692 					{
       
   693 					Kern::Printf("WB: Overlapping partitions");
       
   694 					// Adjust the partition length to not overlap the next partition
       
   695 					prev.iPartitionLen = (curr.iPartitionBaseAddr - prev.iPartitionBaseAddr);
       
   696 
       
   697 					// Check that the base address contained valid information
       
   698 					if(prev.iPartitionLen <= 0)
       
   699 						{
       
   700 						Kern::Printf("WB: Invalid base address");
       
   701 						// Invalid MBR - assume the boot sector is in the first sector
       
   702 						defaultPartitionNumber=(-1); 
       
   703 						partitionCount=0;
       
   704 						}
       
   705 					}
       
   706 				}
       
   707 			}
       
   708 		}
       
   709 
       
   710 mbr_done:
       
   711 	if (defaultPartitionNumber==(-1) && partitionCount==0)
       
   712 		{
       
   713 		Kern::Printf("WB:PartitionInfo no MBR");
       
   714 			{
       
   715 			// Assume it has no MBR, and the Boot Sector is in the 1st sector
       
   716 			SetPartitionEntry(&iPartitionInfo->iEntry[0],0,I64LOW(CyAsSymbianStorageDriver::GetMediaSize()>>KDiskSectorShift));
       
   717 			iHiddenSectors=0;
       
   718 			}
       
   719 		partitionCount=1;
       
   720 		}
       
   721 
       
   722 	iPartitionInfo->iPartitionCount=partitionCount;
       
   723 	iPartitionInfo->iMediaSizeInBytes=TotalSizeInBytes();
       
   724 	#ifdef _MEDWB_DEBUG_1_
       
   725 
       
   726 	Kern::Printf("<Mmc:PartitionInfo (C:%d)",iPartitionInfo->iPartitionCount);
       
   727 	Kern::Printf("     Partition1 (B:%xH L:%xH)",I64LOW(iPartitionInfo->iEntry[0].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[0].iPartitionLen));
       
   728 	Kern::Printf("     Partition2 (B:%xH L:%xH)",I64LOW(iPartitionInfo->iEntry[1].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[1].iPartitionLen));
       
   729 	Kern::Printf("     Partition3 (B:%xH L:%xH)",I64LOW(iPartitionInfo->iEntry[2].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[2].iPartitionLen));
       
   730 	Kern::Printf("     Partition4 (B:%xH L:%xH)",I64LOW(iPartitionInfo->iEntry[3].iPartitionBaseAddr),I64LOW(iPartitionInfo->iEntry[3].iPartitionLen));
       
   731 
       
   732 
       
   733 	Kern::Printf("     iMediaSizeInBytes (%d)",iPartitionInfo->iMediaSizeInBytes);
       
   734 	Kern::Printf("     iHiddenSectors (%d)",iHiddenSectors);
       
   735 	#endif
       
   736 
       
   737 	#ifdef _MEDWB_DEBUG_3_
       
   738 
       
   739 	TMBRPartitionEntry cPe;
       
   740 	if(GetDefaultPartitionInfo(cPe) == KErrNone)
       
   741 		{
       
   742 		pe = (TMBRPartitionEntry*)(&ptrReadBuf[0]);
       
   743 
       
   744 		Kern::Printf("-------------------------------------------");
       
   745 		Kern::Printf("-- Partition Entry Validation/Comparison --");
       
   746 		Kern::Printf("-------------------------------------------");
       
   747 		Kern::Printf("-- iX86BootIndicator [%02x:%02x] %c       -", pe->iX86BootIndicator, cPe.iX86BootIndicator, pe->iX86BootIndicator == cPe.iX86BootIndicator ? ' ' : 'X');
       
   748 		Kern::Printf("--        iStartHead [%02x:%02x] %c       -", pe->iStartHead,        cPe.iStartHead,        pe->iStartHead        == cPe.iStartHead        ? ' ' : 'X');
       
   749 		Kern::Printf("--      iStartSector [%02x:%02x] %c       -", pe->iStartSector,      cPe.iStartSector,      pe->iStartSector      == cPe.iStartSector      ? ' ' : 'X');
       
   750 		Kern::Printf("--    iStartCylinder [%02x:%02x] %c       -", pe->iStartCylinder,    cPe.iStartCylinder,    pe->iStartCylinder    == cPe.iStartCylinder    ? ' ' : 'X');
       
   751 		Kern::Printf("--    iPartitionType [%02x:%02x] %c       -", pe->iPartitionType,    cPe.iPartitionType,    pe->iPartitionType    == cPe.iPartitionType    ? ' ' : 'X');
       
   752 		Kern::Printf("--          iEndHead [%02x:%02x] %c       -", pe->iEndHead,          cPe.iEndHead,          pe->iEndHead          == cPe.iEndHead          ? ' ' : 'X');
       
   753 		Kern::Printf("--        iEndSector [%02x:%02x] %c       -", pe->iEndSector,        cPe.iEndSector,        pe->iEndSector        == cPe.iEndSector        ? ' ' : 'X');
       
   754 		Kern::Printf("--      iEndCylinder [%02x:%02x] %c       -", pe->iEndCylinder,      cPe.iEndCylinder,      pe->iEndCylinder      == cPe.iEndCylinder      ? ' ' : 'X');
       
   755 		Kern::Printf("--      iFirstSector [%08x:%08x] %c       -", pe->iFirstSector,      cPe.iFirstSector,      pe->iFirstSector      == cPe.iFirstSector      ? ' ' : 'X');
       
   756 		Kern::Printf("--       iNumSectors [%08x:%08x] %c       -", pe->iNumSectors,       cPe.iNumSectors,       pe->iNumSectors       == cPe.iNumSectors       ? ' ' : 'X');
       
   757 		Kern::Printf("-------------------------------------------");
       
   758 		}
       
   759 	#endif
       
   760 
       
   761 	return(KErrNone);
       
   762 	}
       
   763 
       
   764 
       
   765 TInt DMediaDriverWB::WritePartitionInfo()
       
   766 /**
       
   767 	Write the default partition table to freshly formatted media
       
   768 	@return Standard Symbian OS Error Code
       
   769  */
       
   770 	{
       
   771 	#ifdef _MEDWB_DEBUG_3_
       
   772 	Kern::Printf(">mmd:wpi");
       
   773 	#endif
       
   774 	TMBRPartitionEntry partitionEntry;
       
   775 	TInt err = GetDefaultPartitionInfo(partitionEntry);
       
   776 	if(err == KErrNone)
       
   777 		{
       
   778 		TUint8	*iPartitionBuf;
       
   779 		
       
   780 		iPartitionBuf = new TUint8[KDiskSectorSize];
       
   781 		
       
   782 		#ifdef _MEDWB_DEBUG_2_
       
   783 		Kern::Printf("mmd:MBR/Partition Table");
       
   784 		Kern::Printf("    Boot ID          : %02xh", partitionEntry.iX86BootIndicator);
       
   785 		Kern::Printf("    Start Head       : %02xh", partitionEntry.iStartHead);
       
   786 		Kern::Printf("    Start Sector     : %02xh", partitionEntry.iStartSector);
       
   787 		Kern::Printf("    Start Cyclinder  : %02xh", partitionEntry.iStartCylinder);
       
   788 		Kern::Printf("    System ID        : %02xh", partitionEntry.iPartitionType);
       
   789 		Kern::Printf("    End Head         : %02xh", partitionEntry.iEndHead);
       
   790 		Kern::Printf("    End Sector       : %02xh", partitionEntry.iEndSector);
       
   791 		Kern::Printf("    End Cyclinder    : %02xh", partitionEntry.iEndCylinder);
       
   792 		Kern::Printf("    Relative Sector  : %08xh", partitionEntry.iFirstSector);
       
   793 		Kern::Printf("    Number of Sectors: %08xh", partitionEntry.iNumSectors);
       
   794 		#endif
       
   795 		//
       
   796 		// Clear all other partition entries and align the partition info into the minor buffer for writing...
       
   797 		//
       
   798 		memclr(iPartitionBuf, KDiskSectorSize);
       
   799 		memcpy(&iPartitionBuf[KMBRFirstPartitionEntry], &partitionEntry, sizeof(TMBRPartitionEntry));
       
   800 
       
   801 		*(TUint16*)(&iPartitionBuf[KMBRSignatureOffset]) = 0xAA55;
       
   802 
       
   803 		err = CyAsSymbianStorageDriver::Write(0, 1, iPartitionBuf);
       
   804 		
       
   805 		//
       
   806 		// Write the partition table and engage the read to validate and complete the mount process
       
   807 		//
       
   808 		delete iPartitionBuf;
       
   809 		iMbrMissing = EFalse;
       
   810 		}
       
   811 
       
   812 	#ifdef _MEDWB_DEBUG_3_
       
   813 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<mmd:wpi:%d", err));
       
   814 	#endif
       
   815 	
       
   816 	return(err);
       
   817 	}
       
   818 
       
   819 
       
   820 TInt DMediaDriverWB::CreateDefaultPartition()
       
   821 	{
       
   822 	TMBRPartitionEntry defPartition;
       
   823 	TInt r = GetDefaultPartitionInfo(defPartition);
       
   824 	if (r == KErrNone)
       
   825 		{
       
   826 		SetPartitionEntry(&iPartitionInfo->iEntry[0], defPartition.iFirstSector, defPartition.iNumSectors);
       
   827 		iHiddenSectors = defPartition.iFirstSector;
       
   828 		iPartitionInfo->iPartitionCount   = 1;
       
   829 		iPartitionInfo->iMediaSizeInBytes = TotalSizeInBytes();
       
   830 		}
       
   831 	return r;
       
   832 	}
       
   833 
       
   834 TInt DMediaDriverWB::GetDefaultPartitionInfo(TMBRPartitionEntry& aPartitionEntry)
       
   835 /**
       
   836 	Calculates the default patition information for an specific card.
       
   837 	@param aPartitionEntry The TMBRPartitionEntry to be filled in with the format parameters
       
   838 	@return Standard Symbian OS Error Code
       
   839  */
       
   840 	{
       
   841 	memclr(&aPartitionEntry, sizeof(TMBRPartitionEntry));
       
   842 
       
   843 	const TUint32 KTotalSectors = I64LOW(CyAsSymbianStorageDriver::GetMediaSize() >> KDiskSectorShift);
       
   844 
       
   845 	aPartitionEntry.iFirstSector = (CyAsSymbianStorageDriver::GetEraseBlockSize()>> KDiskSectorShift);
       
   846 	aPartitionEntry.iNumSectors  = KTotalSectors - aPartitionEntry.iFirstSector;
       
   847 	aPartitionEntry.iX86BootIndicator = 0x00;
       
   848 
       
   849 	if(aPartitionEntry.iNumSectors < 32680)
       
   850 		{
       
   851 		aPartitionEntry.iPartitionType = KPartitionTypeFAT12;
       
   852 		}
       
   853 	else if(aPartitionEntry.iNumSectors < 65536)
       
   854 		{
       
   855 		aPartitionEntry.iPartitionType = KPartitionTypeFAT16small;
       
   856 		}
       
   857 	else if (aPartitionEntry.iNumSectors < 1048576)
       
   858 		{
       
   859 		aPartitionEntry.iPartitionType = KPartitionTypeFAT16;
       
   860  		}
       
   861 	else
       
   862 		{
       
   863 		aPartitionEntry.iPartitionType = KPartitionTypeWin95FAT32;
       
   864 		}
       
   865 	
       
   866 	return(KErrNone);
       
   867 	}
       
   868 
       
   869 void DMediaDriverWB::SetPartitionEntry(TPartitionEntry* aEntry, TUint aFirstSector, TUint aNumSectors)
       
   870 //
       
   871 // auxiliary static function to record partition information in TPartitionEntry object
       
   872 //
       
   873 	{
       
   874 	aEntry->iPartitionBaseAddr=aFirstSector;
       
   875 	aEntry->iPartitionBaseAddr<<=KDiskSectorShift;
       
   876 	aEntry->iPartitionLen=aNumSectors;
       
   877 	aEntry->iPartitionLen<<=KDiskSectorShift;
       
   878 	aEntry->iPartitionType=KPartitionTypeFAT12;	
       
   879 	}
       
   880 
       
   881 // ---- device status, callback DFC ----
       
   882 
       
   883 TInt DMediaDriverWB::CheckDevice(int aReqType)
       
   884 //
       
   885 // Check the device before initiating a command
       
   886 //
       
   887 	{
       
   888 	
       
   889 	__KTRACE_OPT(KPBUSDRV, Kern::Printf(">wb:cd:%d",aReqType));
       
   890 
       
   891 	TInt r=KErrNone;
       
   892 #if 0
       
   893 	if (!iCard->IsReady())
       
   894 		r=KErrNotReady;
       
   895 
       
   896 	// The card must be locked if attempting to unlock during RPI, and
       
   897 	// unlocked at all other times.
       
   898 	else if (aReqType!=EMReqTypeUnlockPswd && iCard->IsLocked())
       
   899 		r=KErrLocked;
       
   900 	// Don't perform Password setting for WriteProtected cards, 
       
   901 	// unable to recover (ForcedErase) if password lost.
       
   902 	else if (aReqType==EMReqTypeChangePswd)
       
   903 		{
       
   904 		if (iCard->MediaType()==EMultiMediaROM)
       
   905 			{
       
   906 			r=KErrAccessDenied;
       
   907 			}
       
   908 		}
       
   909 	else if (iMbrMissing && aReqType==EMReqTypeNormalRd)
       
   910 		r=KErrCorrupt;
       
   911 
       
   912 	// Don't perform write operations when the mechanical write protect switch is set
       
   913 	else if (aReqType==EMReqTypeNormalWr && iCard->IsWriteProtected())
       
   914 		r=KErrAccessDenied;
       
   915 	// Don't perform write/format operations on MMC ROM cards
       
   916 	else if (iMediaType==EMultiMediaROM && aReqType == EMReqTypeNormalWr)
       
   917 		r=KErrAccessDenied;
       
   918 #endif
       
   919 	__KTRACE_OPT(KPBUSDRV, Kern::Printf("<wb:cd:%d", r));
       
   920 	return(r);
       
   921 	}
       
   922 
       
   923 
       
   924 // ---- request management ----
       
   925 
       
   926 TInt DMediaDriverWB::Caps(TLocDrv& aDrive, TLocalDriveCapsV6& aInfo)
       
   927 	{
       
   928 	#ifdef _MEDWB_DEBUG_3_
       
   929 	Kern::Printf(">WB::Caps ");
       
   930 	#endif
       
   931 	// Fill buffer with current media caps.
       
   932 	aInfo.iType = EMediaHardDisk;
       
   933 	aInfo.iBattery = EBatNotSupported;
       
   934 	aInfo.iDriveAtt = KDriveAttLocal;
       
   935 	aInfo.iMediaAtt	= KMediaAttFormattable;
       
   936 
       
   937 	//if(CyAsSymbianStorageDriver::GetIsLocked())
       
   938 	//	aInfo.iMediaAtt |= KMediaAttLockable;
       
   939 	//if (iCard->HasPassword())
       
   940 		//aInfo.iMediaAtt |= KMediaAttHasPassword;
       
   941 		
       
   942 	if ( !CyAsSymbianStorageDriver::GetIsWriteable())
       
   943 		aInfo.iMediaAtt |= KMediaAttWriteProtected;
       
   944 	if (CyAsSymbianStorageDriver::GetIsLocked())
       
   945 		aInfo.iMediaAtt |= KMediaAttLocked;
       
   946 
       
   947 	aInfo.iFileSystemId = KDriveFileSysFAT;
       
   948 
       
   949 	// Format is performed in multiples of the erase sector (or multiple block) size
       
   950 	aInfo.iMaxBytesPerFormat =  CyAsSymbianStorageDriver::GetBlockSize();
       
   951 
       
   952     // Set serial number to CID
       
   953     aInfo.iSerialNumLength = 16;
       
   954     for (TUint i=0; i<16; i++)
       
   955         aInfo.iSerialNum[i] = 0;
       
   956     
       
   957 	// Get block size & erase block size to allow the file system to align first usable cluster correctly
       
   958 	aInfo.iBlockSize = CyAsSymbianStorageDriver::GetBlockSize();
       
   959 	aInfo.iEraseBlockSize = CyAsSymbianStorageDriver::GetEraseBlockSize();
       
   960 
       
   961 	if ( CyAsSymbianStorageDriver::GetIsRemovable())
       
   962 		aInfo.iDriveAtt|= KDriveAttRemovable;
       
   963 
       
   964 	// Must return KErrCompletion to indicate that this 
       
   965 	// is a synchronous version of the function
       
   966 	return KErrNone;
       
   967 	}
       
   968 
       
   969 
       
   970 void DMediaDriverWB::NotifyPowerDown()
       
   971 	{
       
   972 	#ifdef _MEDWB_DEBUG_3_
       
   973 	Kern::Printf(">Mmc:NotifyPowerDown");
       
   974 	#endif
       
   975 
       
   976 	EndInCritical();
       
   977 
       
   978 	//CompleteRequest(KErrNotReady);
       
   979 	}
       
   980 
       
   981 void DMediaDriverWB::NotifyEmergencyPowerDown()
       
   982 	{
       
   983 	#ifdef _MEDWB_DEBUG_3_
       
   984 	Kern::Printf(">Ata:NotifyEmergencyPowerDown");
       
   985 	#endif
       
   986 	//TInt r=KErrNotReady;
       
   987 	//if (iCritical)
       
   988 	//	r=KErrAbort;
       
   989 	EndInCritical();
       
   990 
       
   991 	// need to cancel the session as the stack doesn't take too kindly to having the same session engaged more than once.
       
   992 
       
   993 	//CompleteRequest(r);
       
   994 	}
       
   995 
       
   996 TInt DMediaDriverWB::Request(TLocDrvRequest& aRequest)
       
   997 	{
       
   998 	#ifdef _MEDWB_DEBUG_3_
       
   999 	Kern::Printf("MmcMd:Req %08x id %d",&aRequest,aRequest.Id());
       
  1000 	#endif
       
  1001 	TInt r=KErrNotSupported;
       
  1002 	TInt id=aRequest.Id();
       
  1003 
       
  1004 	NKern::ThreadEnterCS();
       
  1005 
       
  1006 	TUint partitionType = aRequest.Drive()->iPartitionType;
       
  1007 	TBool readOnly = (partitionType == KPartitionTypeRofs || partitionType == KPartitionTypeROM);
       
  1008 
       
  1009 	switch (id)
       
  1010 		{
       
  1011 		case DLocalDrive::ECaps:
       
  1012 			{
       
  1013 				TLocalDriveCapsV6& c = *(TLocalDriveCapsV6*)aRequest.RemoteDes();
       
  1014 				TLocDrv& drive = *aRequest.Drive();
       
  1015 				r = Caps(drive, c);
       
  1016 				c.iSize = drive.iPartitionLen;
       
  1017 				c.iPartitionType = drive.iPartitionType;	
       
  1018 				c.iHiddenSectors = (TUint) (drive.iPartitionBaseAddr >> KDiskSectorShift);
       
  1019 				#ifdef _MEDWB_DEBUG_2_
       
  1020 				Kern::Printf("caps : c.iSize = %d ", c.iSize);
       
  1021 				Kern::Printf("caps : c.iPartitionType = %d ", c.iPartitionType);
       
  1022 				Kern::Printf("caps : c.iHiddenSectors = %d ", c.iHiddenSectors);
       
  1023 				#endif
       
  1024 			}
       
  1025 			break;
       
  1026 		case DLocalDrive::EQueryDevice:
       
  1027 			Kern::Printf(">WB::EQueryDevice ");
       
  1028 			r = KErrNotSupported;
       
  1029 			break;
       
  1030 
       
  1031 		case DLocalDrive::ERead:
       
  1032 			r=DoRead(aRequest);
       
  1033 			break;
       
  1034 		case DLocalDrive::EWrite:
       
  1035 			if (readOnly)
       
  1036 				return KErrNotSupported;
       
  1037 			r=DoWrite(aRequest);
       
  1038 			break;
       
  1039 		case DLocalDrive::EFormat:
       
  1040 			if (readOnly)
       
  1041 				return KErrNotSupported;
       
  1042 			r=DoFormat(aRequest);
       
  1043 			break;
       
  1044 
       
  1045 
       
  1046 		case DLocalDrive::EPasswordUnlock:
       
  1047 		case DLocalDrive::EPasswordLock:
       
  1048 		case DLocalDrive::EPasswordClear:
       
  1049 		case DLocalDrive::EPasswordErase:
       
  1050 		case DLocalDrive::EWritePasswordStore:
       
  1051 				Kern::Printf(">WB::EPassword ");
       
  1052 		break;
       
  1053 		case DLocalDrive::EEnlarge:
       
  1054 		case DLocalDrive::EReduce:
       
  1055 			Kern::Printf(">WB::EReduce ");
       
  1056 		default:
       
  1057 			Kern::Printf(">WB::default ");
       
  1058 			r=KErrNotSupported;
       
  1059 			break;
       
  1060 		}
       
  1061 
       
  1062 	NKern::ThreadLeaveCS();
       
  1063 	#ifdef _MEDWB_DEBUG_3_
       
  1064 	Kern::Printf("MmcMd:Req %08x cmp %d",&aRequest,r);
       
  1065 	#endif
       
  1066 	return r;
       
  1067 	}
       
  1068 
       
  1069 void DMediaDriverWB::Disconnect(DLocalDrive* aLocalDrive, TThreadMessage* aMsg)
       
  1070 	{
       
  1071 	// Complete using the default implementation
       
  1072 	DMediaDriver::Disconnect(aLocalDrive, aMsg);
       
  1073 	}
       
  1074 
       
  1075 
       
  1076 DECLARE_EXTENSION_PDD()
       
  1077 	{
       
  1078 	// NB if the media driver has been defined as a kernel extension in the .OBY/.IBY file 
       
  1079 	// i.e the "extension" keyword has been used rather than "device", then an instance of 
       
  1080 	// DPhysicalDeviceMediaMmcFlash will already have been created by InitExtension(). In this 
       
  1081 	// case the kernel will see that an object of the same name already exists and delete the 
       
  1082 	// new one.
       
  1083 	return new DPhysicalDeviceMediaWB;
       
  1084 	}
       
  1085 #ifdef REGIST_MEDIA_USE_MMC
       
  1086 DECLARE_STANDARD_EXTENSION()
       
  1087 	{	
       
  1088 	Kern::Printf("Creating WestBridge PDD");
       
  1089 
       
  1090 	DPhysicalDeviceMediaWB* device = new DPhysicalDeviceMediaWB;
       
  1091 
       
  1092 	TInt r;
       
  1093 	if (device==NULL)
       
  1094 		r=KErrNoMemory;
       
  1095 	else
       
  1096 		r=Kern::InstallPhysicalDevice(device);
       
  1097 	Kern::Printf("Installing WestBridge PDD in kernel returned %d",r);
       
  1098 
       
  1099 	if( CyAsSymbianStorageDriver::Open() )
       
  1100 		Kern::Printf("**CyAsSymbianStorageDriver::Open() - Success");
       
  1101 	else
       
  1102 		Kern::Printf("**CyAsSymbianStorageDriver::Open() - Fail");
       
  1103 
       
  1104 	Kern::Printf("WestBridge extension entry point drive returns %d",r);
       
  1105 	return r;
       
  1106 	}
       
  1107 
       
  1108 #else
       
  1109 static const TInt WBDriveNumbers[1]={1};	
       
  1110 _LIT(KWBDriveName,"WestBridge");
       
  1111 
       
  1112 DECLARE_STANDARD_EXTENSION()
       
  1113 	{
       
  1114 	__KTRACE_OPT(KBOOT,Kern::Printf("Registering WB drive"));
       
  1115 	#ifdef _MEDWB_DEBUG_2_
       
  1116 	Kern::Printf("Registering WB drive");
       
  1117 	#endif
       
  1118 	TInt r=KErrNoMemory;
       
  1119 	DPrimaryMediaBase* pM=new DPrimaryMediaBase;
       
  1120 	if (pM)
       
  1121 		{
       
  1122 		r=LocDrv::RegisterMediaDevice(MEDIA_DEVICE_MMC,1,&WBDriveNumbers[0],pM,1,KWBDriveName);
       
  1123 		}
       
  1124 
       
  1125 
       
  1126 	if( CyAsSymbianStorageDriver::Open() )
       
  1127 	{
       
  1128 		#ifdef _MEDWB_DEBUG_1_
       
  1129 		Kern::Printf("**CyAsSymbianStorageDriver::Open() - Success");
       
  1130 		#endif
       
  1131 	}
       
  1132 	else
       
  1133 	{
       
  1134 		#ifdef _MEDWB_DEBUG_1_
       
  1135 		Kern::Printf("**CyAsSymbianStorageDriver::Open() - Fail");
       
  1136 		#endif
       
  1137 	}
       
  1138 	
       
  1139 	__KTRACE_OPT(KBOOT,Kern::Printf("Registering WB drive - return %d",r));
       
  1140 	#ifdef _MEDWB_DEBUG_2_
       
  1141 	Kern::Printf("Registering WB drive - return %d",r);
       
  1142 	#endif
       
  1143 	
       
  1144 	return r;
       
  1145 	}
       
  1146 
       
  1147 
       
  1148 #endif
       
  1149