userlibandfileserver/fileserver/smassstorage/drivemanager.cpp
changeset 0 a41df078684a
child 286 48e57fb1237e
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Class implementation of CDriveManager and CMassStorageDrive.
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalTechnology
       
    21 */
       
    22 
       
    23 #include <f32fsys.h>
       
    24 #include "massstoragedebug.h"
       
    25 #include "usbmsshared.h"
       
    26 #include "drivemanager.h"
       
    27 
       
    28 ///////////////////////////////////////////////////////////////////////////////
       
    29 
       
    30 
       
    31 /**
       
    32 A private structure that, when Connected, holds references to 
       
    33 the CProxyDrive and the corresponding TBusLocalDrive's Media Changed flag.
       
    34 */
       
    35 struct CMassStorageDrive::CLocalDriveRef : public CBase
       
    36 	{
       
    37 	CLocalDriveRef(CProxyDrive& aProxyDrive, TBool& aMediaChanged)
       
    38 		: iProxyDrive(aProxyDrive), iMediaChanged(aMediaChanged), iDriveState(EIdle)
       
    39 		{
       
    40 		}
       
    41 	CProxyDrive& iProxyDrive;
       
    42 	TBool& iMediaChanged;
       
    43 	/**
       
    44 	The Drive Media state machine
       
    45 	*/
       
    46 	TDriveState iDriveState;
       
    47 	};
       
    48 
       
    49 /**
       
    50 @param aCritSec A Critical Section object shared by all drives.
       
    51 @param aDrives Reference to the list of CMassStorageDrive objects. 
       
    52 @param aDriveMap Reference to array mapping lun to drive number for supported 
       
    53 	   mass storage drives.
       
    54 @post Object is fully constructed
       
    55  */
       
    56 CMassStorageDrive::CMassStorageDrive(RCriticalSection& aCritSec, 
       
    57 									 RDriveStateChangedPublisher& aDriveStateChangedPublisher)
       
    58 	: 
       
    59 	iCritSec(aCritSec),
       
    60 	iMountState(EDisconnected),
       
    61 	iDriveStateChangedPublisher(aDriveStateChangedPublisher)
       
    62 	{
       
    63 	__FNLOG("CMassStorageDrive::CMassStorageDrive");
       
    64 
       
    65 	}
       
    66 
       
    67 CMassStorageDrive::~CMassStorageDrive()
       
    68 	{
       
    69 	delete iLocalDrive;
       
    70 	}
       
    71 
       
    72 /**
       
    73 Read from the target drive unit.
       
    74 @return KErrNone on success, otherwise system wide error code
       
    75 */
       
    76 TInt CMassStorageDrive::Read(const TInt64& aPos, TInt aLength, TDes8& aBuf, TBool aWholeMedia)
       
    77 	{
       
    78 	__FNLOG("CMassStorageDrive::Read");
       
    79 
       
    80 	TInt err = KErrUnknown; // never return this
       
    81 	iCritSec.Wait();
       
    82 
       
    83 	if(iMountState != EConnected)
       
    84 		{
       
    85 		err = KErrDisconnected;
       
    86 		}
       
    87 	else
       
    88 		{
       
    89 		if(aWholeMedia)
       
    90 			{
       
    91 			err = SafeProxyDrive().Read(aPos, aLength, &aBuf, KLocalMessageHandle, 0, RLocalDrive::ELocDrvWholeMedia);
       
    92 			}
       
    93 		else
       
    94 			{
       
    95 			err = SafeProxyDrive().Read(aPos, aLength, aBuf);
       
    96 			}
       
    97 
       
    98 		if(err == KErrNone)
       
    99 			{
       
   100 #ifndef USB_TRANSFER_PUBLISHER
       
   101 			iBytesRead += aBuf.Length();
       
   102 			__PRINT1(_L("iBytesRead=%d\n"),iBytesRead);
       
   103 #endif
       
   104 			}
       
   105 
       
   106 		else if(err == KErrLocked)
       
   107 			{
       
   108 			SetDriveState(ELocked);
       
   109 			}
       
   110 		}
       
   111 
       
   112 	iCritSec.Signal();
       
   113 	return err;
       
   114 	}
       
   115 
       
   116 /**
       
   117 Write to the target drive unit.
       
   118 @return KErrNone on success, otherwise system wide error code
       
   119 */
       
   120 TInt CMassStorageDrive::Write(const TInt64& aPos, TDesC8& aBuf, TBool aWholeMedia)
       
   121 	{
       
   122 	__FNLOG("CMassStorageDrive::Write");
       
   123 
       
   124 	TInt err = KErrNone;
       
   125 	iCritSec.Wait();
       
   126 
       
   127 	if (iMountState != EConnected)
       
   128 		{
       
   129 		err = KErrDisconnected;
       
   130 		}
       
   131 	else
       
   132 		{
       
   133 		__ASSERT_DEBUG(iLocalDrive, User::Invariant());
       
   134 		TDriveState oldState = iLocalDrive->iDriveState;
       
   135 		if(oldState != EActive)
       
   136 			{
       
   137 			// SCSI hasn't called SetCritical
       
   138 			SetDriveState(EActive);
       
   139 			}
       
   140 
       
   141 		if (aWholeMedia)
       
   142 			{
       
   143 			err = SafeProxyDrive().Write(aPos, aBuf.Length(), &aBuf, KLocalMessageHandle, 0, RLocalDrive::ELocDrvWholeMedia);
       
   144 			}
       
   145 		else
       
   146 			{
       
   147 			err = SafeProxyDrive().Write(aPos,aBuf);
       
   148 			}
       
   149 
       
   150 		if (err == KErrNone)
       
   151 			{
       
   152 #ifndef USB_TRANSFER_PUBLISHER
       
   153 			iBytesWritten += aBuf.Length();
       
   154 #endif
       
   155 			}
       
   156 
       
   157 		if (err == KErrLocked)
       
   158 			{
       
   159 			SetDriveState(ELocked);
       
   160 			}
       
   161 		else if (oldState != EActive)
       
   162 			{
       
   163 			SetDriveState(oldState);
       
   164 			}
       
   165 		}
       
   166 
       
   167 	iCritSec.Signal();
       
   168 	return err;
       
   169 	}
       
   170 
       
   171 /**
       
   172 Get the capabilities of the target drive unit.
       
   173 @return KErrNone on success, otherwise system wide error code
       
   174 */
       
   175 TInt CMassStorageDrive::Caps(TLocalDriveCapsV4& aInfo)
       
   176 	{
       
   177 	__FNLOG("CMassStorageDrive::Caps");
       
   178 
       
   179 	TInt err = KErrNone;
       
   180 	iCritSec.Wait();
       
   181 	
       
   182 	if(iMountState != EConnected)
       
   183 		{
       
   184 		err = KErrDisconnected;
       
   185 		}
       
   186 	else 
       
   187 		{
       
   188 		// Initialise in case Caps() fails
       
   189 		aInfo.iType = ::EMediaUnknown; 
       
   190 		err = DoCaps(aInfo);
       
   191 
       
   192 		__PRINTERR(_L("CheckDriveState: DoCaps err=%d\n"), err);
       
   193 
       
   194 		if(err == KErrNotReady || (err==KErrNone && aInfo.iType == ::EMediaNotPresent))
       
   195 			{
       
   196 			__PRINT(_L("CMassStorageDrive::Caps detected MediaNotPresent\n"));
       
   197 			SetDriveState(CMassStorageDrive::EMediaNotPresent);
       
   198 			}
       
   199 		}
       
   200 
       
   201 	iCritSec.Signal();
       
   202 	return err;
       
   203 	}
       
   204 
       
   205 /**
       
   206 Provides an interface to CProxyDrive::Caps that hides the
       
   207 package buffer.
       
   208 @return KErrNone on success, otherwise system wide error code
       
   209 @param aInfo
       
   210 */
       
   211 TInt CMassStorageDrive::DoCaps(TLocalDriveCapsV4& aInfo)
       
   212 	{
       
   213 	__FNLOG("CMassStorageDrive::DoCaps");
       
   214 
       
   215 	TLocalDriveCapsV4Buf buf;
       
   216 	buf.FillZ();
       
   217 	CProxyDrive& pd = SafeProxyDrive();
       
   218 
       
   219 	__PRINT(_L("CMassStorageDrive::DoCaps calling Caps\n"));
       
   220 	TInt err = pd.Caps(buf);
       
   221 
       
   222 	__PRINT1(_L("CMassStorageDrive::DoCaps: Caps returned %d\n"), err);
       
   223 
       
   224 	if(err==KErrNone)
       
   225 		{
       
   226 		// Invoke function call operator to cast to TLocalDriveCapsV4&
       
   227 		aInfo = buf();
       
   228 		}
       
   229 
       
   230 	return err;
       
   231 	}
       
   232 
       
   233 /**
       
   234 Publish media error, user should reinsert the memory card.
       
   235 Similar to FAT32's TDriver::HandleCriticalError.
       
   236 Note: User notification is not implemented, instead we abort and dismount.
       
   237 */
       
   238 TInt CMassStorageDrive::HandleCriticalError() 
       
   239 	{
       
   240 	__FNLOG("CMassStorageDrive::HandleCriticalError");
       
   241 	iDriveMediaErrorPublisher.PublishError(ETrue);
       
   242 	return KErrAbort;
       
   243 	}
       
   244 
       
   245 
       
   246 TInt CMassStorageDrive::ClearCriticalError()
       
   247 	{
       
   248 	iDriveMediaErrorPublisher.PublishError(EFalse);
       
   249 	return KErrNone;
       
   250 	}
       
   251 
       
   252 
       
   253 /**
       
   254 Checks the Media Changed flag, and optionally resets it.
       
   255 @return The state of the Media Changed flag.
       
   256 @param aReset If true, the Media Changed flag is reset to EFalse.
       
   257 */
       
   258 TBool CMassStorageDrive::IsMediaChanged(TBool aReset)
       
   259 	{
       
   260 	__FNLOG("CMassStorageDrive::IsMediaChanged");
       
   261 
       
   262 	iCritSec.Wait();
       
   263 
       
   264 	TBool mediaChanged = EFalse;
       
   265 	if(iLocalDrive)
       
   266 		{
       
   267 		mediaChanged = iLocalDrive->iMediaChanged;
       
   268 		if(aReset) 
       
   269 			{
       
   270 			iLocalDrive->iMediaChanged = EFalse;
       
   271 			}
       
   272 		}
       
   273 	else
       
   274 		{
       
   275 		__PRINT(_L("CMassStorageDrive::IsMediaChanged: no drive\n"));
       
   276 		}
       
   277 
       
   278 	iCritSec.Signal();
       
   279 
       
   280 	__PRINT1(_L("CMassStorageDrive::IsMediaChanged: returning %d\n"), mediaChanged);
       
   281 	return mediaChanged;
       
   282 	}
       
   283 
       
   284 /**
       
   285 Set the Drive State to Active or Idle.
       
   286 @return KErrNone on success, KErrNotReady if media not present, KErrDisMounted if not mounted
       
   287 @param aCritical ETrue for Active, EFalse for Idle
       
   288 */
       
   289 TInt CMassStorageDrive::SetCritical(TBool aCritical)
       
   290 	{
       
   291 	__FNLOG("CMassStorageDrive::SetCritical");
       
   292 
       
   293 	TInt err = KErrDisMounted;
       
   294 
       
   295 	iCritSec.Wait();
       
   296 
       
   297 	if(iLocalDrive)
       
   298 		{
       
   299 		if(iLocalDrive->iDriveState == CMassStorageDrive::EMediaNotPresent)
       
   300 			{
       
   301 			err = KErrNotReady;
       
   302 			}
       
   303 		else
       
   304 			{
       
   305 			SetDriveState(
       
   306 				aCritical 
       
   307 				? CMassStorageDrive::EActive
       
   308 				: CMassStorageDrive::EIdle );
       
   309 				
       
   310 			err = KErrNone;
       
   311 			}
       
   312 		}
       
   313 
       
   314 	iCritSec.Signal();
       
   315 
       
   316 	return err;
       
   317 	}
       
   318 
       
   319 /**
       
   320 Set the mount state
       
   321 */
       
   322 TInt CMassStorageDrive::SetMountConnected(CProxyDrive& aProxyDrive, TBool& aMediaChanged)
       
   323 	{
       
   324 	__FNLOG("CMassStorageDrive::SetMountConnected");
       
   325 	CLocalDriveRef* localDrive = NULL;
       
   326 
       
   327 	__PRINT(_L("SetMountConnected entering critical section\n"));
       
   328 	iCritSec.Wait(); // note: signalled in SetMountState
       
   329 
       
   330 	if (iLocalDrive == NULL)
       
   331 		{
       
   332 		localDrive = new CLocalDriveRef(aProxyDrive, aMediaChanged);
       
   333 		if (localDrive==NULL) 
       
   334 			{
       
   335 			iCritSec.Signal();
       
   336 			return KErrNoMemory;
       
   337 			}
       
   338 		}
       
   339 
       
   340 	return SetMountState(EConnected, localDrive);
       
   341 	}
       
   342 
       
   343 /**
       
   344 @return KErrNone
       
   345 @param aNewState
       
   346 @param aLocalDrive Only provide this if aNewState is EConnected.
       
   347 */
       
   348 TInt CMassStorageDrive::SetMountState(TMountState aNewState, CLocalDriveRef* aLocalDrive/*=NULL*/)
       
   349 	{
       
   350 	__FNLOG("CMassStorageDrive::SetMountState");
       
   351 	
       
   352 	if(iMountState == aNewState)
       
   353 		{
       
   354 		__PRINT(_L("SetMountState: No change\n"));
       
   355 		}
       
   356 	else
       
   357 		{
       
   358 		// If called from SetMountConnected, already in critical section, 
       
   359 		// otherwise, must enter it here.
       
   360 		if(EConnected!=aNewState)
       
   361 			{
       
   362 			__PRINT(_L("SetMountState entering critical section\n"));
       
   363 			iCritSec.Wait();
       
   364 			}
       
   365 
       
   366 		switch(aNewState)
       
   367 			{
       
   368 			case EConnected:
       
   369 				if(aLocalDrive)
       
   370 					{
       
   371 					__ASSERT_DEBUG(!iLocalDrive, User::Invariant());
       
   372 					iLocalDrive = aLocalDrive;
       
   373 					}
       
   374 				__ASSERT_DEBUG(iLocalDrive, User::Invariant());
       
   375 				break;
       
   376 
       
   377 			case EDisconnected:
       
   378 				delete iLocalDrive;
       
   379 				iLocalDrive = NULL;
       
   380 #ifndef USB_TRANSFER_PUBLISHER
       
   381 				iBytesWritten = iBytesRead = 0;
       
   382 #endif
       
   383 				break;
       
   384 
       
   385 			case EDisconnecting:
       
   386 			case EConnecting:
       
   387 				// Do not change iLocalDrive for these state changes
       
   388 				break;
       
   389 			}
       
   390 		
       
   391 		iMountState = aNewState;
       
   392 		__PRINT1(_L("SetMountState: state=%d\n"), iMountState);
       
   393 
       
   394 		iDriveStateChangedPublisher.DriveStateChanged();
       
   395 		
       
   396 		iCritSec.Signal();
       
   397 		__PRINT(_L("SetMountState has left the critical section\n"));
       
   398 		}
       
   399 
       
   400 	return KErrNone;
       
   401 	}
       
   402 
       
   403 /**
       
   404 @return Current drive media state
       
   405 */
       
   406 CMassStorageDrive::TDriveState CMassStorageDrive::DriveState() const
       
   407 	{
       
   408 	return iLocalDrive ? iLocalDrive->iDriveState : EErrDisMounted;
       
   409 	}
       
   410 
       
   411 /**
       
   412 Check for media not present, and return the drive state.
       
   413 @return Current drive media state
       
   414 */
       
   415 CMassStorageDrive::TDriveState CMassStorageDrive::CheckDriveState()
       
   416 	{
       
   417 	__FNLOG("CMassStorageDrive::CheckDriveState");
       
   418 
       
   419 	CMassStorageDrive::TDriveState state = EErrDisMounted;
       
   420 
       
   421 	iCritSec.Wait();
       
   422 
       
   423 	if (iLocalDrive)
       
   424 		{
       
   425 		TInt err = KErrGeneral;
       
   426 		TLocalDriveCapsV4 caps;
       
   427 
       
   428 		FOREVER
       
   429 			{
       
   430 			// Initialise in case Caps() fails
       
   431 			caps.iType = ::EMediaNotPresent; 
       
   432 
       
   433 			err = DoCaps(caps);
       
   434 			__PRINTERR(_L("CheckDriveState: DoCaps err=%d\n"), err);
       
   435 			if (err == KErrNotReady || (err == KErrNone && caps.iType == ::EMediaNotPresent))
       
   436 				{
       
   437 				__PRINT(_L("CheckDriveState: detected MediaNotPresent\n"));
       
   438 
       
   439 				SetDriveState(CMassStorageDrive::EMediaNotPresent);
       
   440 
       
   441 				if (HandleCriticalError() == KErrAbort)
       
   442 					break;
       
   443 				}
       
   444 			else 
       
   445 				{
       
   446 				ClearCriticalError();
       
   447 				break;
       
   448 				}
       
   449 			}
       
   450 
       
   451 		if (err == KErrNone && caps.iType != ::EMediaNotPresent)
       
   452 			{
       
   453 			if (iLocalDrive->iDriveState == CMassStorageDrive::EMediaNotPresent)
       
   454 				{
       
   455 				__PRINT(_L("CheckDriveState: detected media inserted\n"));
       
   456 				SetDriveState(CMassStorageDrive::EIdle);
       
   457 				}
       
   458 			else if (iLocalDrive->iDriveState == CMassStorageDrive::ELocked &&
       
   459 					 !(caps.iMediaAtt & KMediaAttLocked))
       
   460 				{
       
   461 				__PRINT(_L("CheckDriveState: detected media unlocked\n"));
       
   462 				SetDriveState(CMassStorageDrive::EIdle);
       
   463 				}
       
   464 			else if (caps.iMediaAtt & KMediaAttLocked)
       
   465 				{
       
   466 				__PRINT(_L("CheckDriveState: detected media locked\n"));
       
   467 				SetDriveState(CMassStorageDrive::ELocked);
       
   468 				}
       
   469 			
       
   470 			iWholeMediaAccess = !(caps.iDriveAtt & KDriveAttLogicallyRemovable);
       
   471 			}
       
   472 
       
   473 		// Get the current state
       
   474 		state = iLocalDrive->iDriveState;
       
   475 		}
       
   476 
       
   477 	iCritSec.Signal();
       
   478 
       
   479 	return state;
       
   480 	}
       
   481 
       
   482 static TBool IsActive(CMassStorageDrive::TDriveState aDriveState)
       
   483 	{
       
   484 	return aDriveState==CMassStorageDrive::EActive;
       
   485 	}
       
   486 
       
   487 /**
       
   488 @param aNewState
       
   489 */
       
   490 void CMassStorageDrive::SetDriveState(TDriveState aNewState)
       
   491 	{
       
   492 	__FNLOG("CMassStorageDrive::SetDriveState");
       
   493 
       
   494 	__ASSERT_DEBUG(aNewState == EIdle ||
       
   495                    (iMountState == EConnected && NULL != iLocalDrive) ||
       
   496                    (iMountState == EDisconnecting && NULL != iLocalDrive),
       
   497         User::Invariant());
       
   498 
       
   499 	if(!iLocalDrive)
       
   500 		{
       
   501 		__PRINT(_L("SetDriveState: Drive not mounted.\n"));
       
   502 		}
       
   503 	else
       
   504 		{
       
   505 		__PRINT2(_L("SetDriveState: %d->%d\n"), iLocalDrive->iDriveState, aNewState);
       
   506 
       
   507 		if(iLocalDrive->iDriveState != aNewState)
       
   508 			{
       
   509 			CMountCB* mount = SafeProxyDrive().Mount();
       
   510 #if !defined(USBMSDRIVE_TEST)
       
   511 			__ASSERT_DEBUG(mount != NULL, User::Invariant());
       
   512 #endif
       
   513 			if(mount)
       
   514 				{
       
   515 				if(!IsActive(iLocalDrive->iDriveState) && IsActive(aNewState))
       
   516 					{
       
   517 					mount->IncLock();
       
   518 					}
       
   519 				else if(IsActive(iLocalDrive->iDriveState) && !IsActive(aNewState))
       
   520 					{
       
   521 					mount->DecLock();
       
   522 					}
       
   523 				__PRINT1(_L("SetDriveState: LockStatus=%d\n"), mount->LockStatus());
       
   524 				}
       
   525 
       
   526 			iLocalDrive->iDriveState = aNewState;
       
   527 
       
   528 			iDriveStateChangedPublisher.DriveStateChanged();
       
   529 			}
       
   530 		}
       
   531 	}
       
   532 
       
   533 /**
       
   534 Accessor for iProxyDrive; asserts if NULL
       
   535 */
       
   536 CProxyDrive& CMassStorageDrive::SafeProxyDrive() const
       
   537 	{
       
   538 	__ASSERT_ALWAYS(NULL!=iLocalDrive, User::Invariant());
       
   539 	return iLocalDrive->iProxyDrive;
       
   540 	}
       
   541 
       
   542 /////////////////////////////////////////////////////////////////
       
   543 
       
   544 /**
       
   545 Construct a CDriveManager object.
       
   546 @param aDriveMap Reference to array mapping lun to drive number for supported 
       
   547 	   mass storage drives.
       
   548 */
       
   549 CDriveManager* CDriveManager::NewL(TRefDriveMap aDriveMap)
       
   550 	{
       
   551 	__FNLOG("CDriveManager::NewL");
       
   552 	__PRINT1(_L("CDriveManager::NewL - %d drives\n"), aDriveMap.Count());
       
   553 
       
   554 	CDriveManager* self = new (ELeave) CDriveManager(aDriveMap);
       
   555 	CleanupStack::PushL(self);
       
   556 	self->ConstructL();
       
   557 	CleanupStack::Pop();
       
   558 	return self;
       
   559 	}
       
   560 
       
   561 CDriveManager::CDriveManager(const RArray<TInt>& aDriveMap)
       
   562 	: iDriveMap(aDriveMap)
       
   563 	{}
       
   564 
       
   565 /**
       
   566 Construct a CDriveManager object.
       
   567 */
       
   568 void CDriveManager::ConstructL()
       
   569 	{
       
   570 	__FNLOG("CDriveManager::ConstructL");
       
   571 
       
   572 	User::LeaveIfError(iDriveCritSec.CreateLocal());
       
   573 
       
   574 	iDriveStateChangedPublisher = new (ELeave) RDriveStateChangedPublisher(iDrives, iDriveMap);
       
   575 
       
   576 	for(TInt i = 0; i < iDriveMap.Count(); i++)
       
   577 		{
       
   578 		iDrives[i] = new (ELeave) CMassStorageDrive(iDriveCritSec, *iDriveStateChangedPublisher);
       
   579 		}
       
   580 
       
   581 	// Publish initial drive state
       
   582 	if (iDriveMap.Count() > 0)
       
   583 		{
       
   584 		iDriveStateChangedPublisher->DriveStateChanged();
       
   585 		}
       
   586 	}
       
   587 
       
   588 /**
       
   589 Destructor
       
   590 */
       
   591 CDriveManager::~CDriveManager()
       
   592 	{
       
   593 	__FNLOG("CDriveManager::~CDriveManager");
       
   594 
       
   595 	iDrives.DeleteAll();
       
   596 	delete iDriveStateChangedPublisher;
       
   597 	iDriveCritSec.Close();
       
   598 	}
       
   599 
       
   600 /**
       
   601 Set the mount state to Connected and specify the Proxy Drive.
       
   602 @return KErrNone on success, otherwise system wide error code
       
   603 @param aDrive The mounted Proxy Drive
       
   604 @param aLun The Logical Drive Unit identifier (0..numDrives-1)
       
   605 @pre If the Mount State is Connected, then aDrive must be the
       
   606      same as it was the last time this function was called.
       
   607 @post The Mount State will be Connected.
       
   608 */
       
   609 TInt CDriveManager::RegisterDrive(CProxyDrive& aProxyDrive, TBool& aMediaChanged, TUint aLun)
       
   610 	{
       
   611 	__FNLOG("CDriveManager::RegisterDrive");
       
   612 	__PRINT1(_L("Lun=%d \n"),aLun);
       
   613 	TInt err = KErrUnknown; // never return this
       
   614 	CMassStorageDrive* drive = CDriveManager::Drive(aLun, err);
       
   615 	if(drive)
       
   616 		{
       
   617 		drive->SetMountConnected(aProxyDrive, aMediaChanged);
       
   618 		}
       
   619 
       
   620 	__PRINT1(_L("CDriveManager::RegisterDrive err=%d\n"), err);
       
   621 	return err;
       
   622 	}
       
   623 
       
   624 /**
       
   625 Set the mount state to Disconnected.
       
   626 @return KErrNone on success, otherwise system wide error code
       
   627 @param aLun The Logical Drive Unit identifier (0..numDrives-1)
       
   628 @post The Mount State will be Disconnected.
       
   629 */
       
   630 TInt CDriveManager::DeregisterDrive(TUint aLun)
       
   631 	{
       
   632 	__FNLOG("CDriveManager::DeregisterDrive");
       
   633 
       
   634 	TInt err = KErrUnknown; // never return this
       
   635 	if(CMassStorageDrive* drive = Drive(aLun, err))
       
   636 		{
       
   637 		err = drive->SetMountDisconnected();
       
   638 		}
       
   639 
       
   640 	__PRINT1(_L("CDriveManager::DeregisterDrive err=%d\n"), err);
       
   641 	return err;
       
   642 	}
       
   643 
       
   644 /**
       
   645 Return a pointer to the drive specified aLun, or NULL if
       
   646 aLun is invalid.
       
   647 
       
   648 @return Pointer to the specified drive, or NULL.
       
   649 @param aLun The Logical Drive Unit identifier (0..numDrives-1)
       
   650 @param aError KErrNone on success, KErrArgument if NULL is returned.
       
   651 */
       
   652 CMassStorageDrive* CDriveManager::Drive(TUint aLun, TInt& aError) const
       
   653 	{
       
   654 	aError = KErrNone;
       
   655 	CMassStorageDrive* drive = NULL;
       
   656 
       
   657 	// Check if aLun exceeds the specified number of drives
       
   658 	// (This will panic if it exceeds KMaxLun).
       
   659 	if(aLun>=KUsbMsMaxDrives || !iDrives[aLun]) 
       
   660 		{
       
   661 		aError = KErrArgument;
       
   662 		}
       
   663 	else
       
   664 		{
       
   665 		drive = iDrives[aLun];
       
   666 		}
       
   667 	return drive;
       
   668 	}
       
   669 
       
   670 /**
       
   671 Checks the Media Changed flag, and optionally resets it.
       
   672 @return The state of the Media Changed flag.
       
   673 @param aLun The Logical Drive Unit identifier (0..numDrives-1)
       
   674 @param aReset If true, the Media Changed flag is reset to EFalse.
       
   675 */
       
   676 TBool CDriveManager::IsMediaChanged(TUint aLun, TBool aReset)
       
   677 	{
       
   678 	__FNLOG("CDriveManager::IsMediaChanged");
       
   679 
       
   680 	TInt err; // not used, but is a required parameter
       
   681 	CMassStorageDrive* drive = Drive(aLun, err);
       
   682 
       
   683 	if(!drive)
       
   684 		{
       
   685 		__PRINT1(_L("CDriveManager::IsMediaChanged: LUN=%d not found, returning false\n"), aLun);
       
   686 		return ETrue;
       
   687 		}
       
   688 	else
       
   689 		{
       
   690 		return drive->IsMediaChanged(aReset);
       
   691 		}
       
   692 	}
       
   693 
       
   694 /**
       
   695 Set the Drive State to Active or Idle.
       
   696 Ref: 3.6.3.2 - PREVENT_MEDIUM_REMOVAL
       
   697 @return KErrNone on success, otherwise system wide error code
       
   698 @param aLun The Logical Drive Unit identifier (0..numDrives-1)
       
   699 @param aCritical ETrue for Active, EFalse for Idle
       
   700 */
       
   701 TInt CDriveManager::SetCritical(TUint aLun, TBool aCritical)
       
   702 	{
       
   703 	__FNLOG("CDriveManager::SetCritical");
       
   704 
       
   705 	TInt err = KErrUnknown; // never return this
       
   706 
       
   707 	TInt i=aLun;
       
   708 	TInt cnt=aLun+1;
       
   709 	
       
   710 	if (aLun == KAllLuns)
       
   711 		{
       
   712 		i=0;
       
   713 		cnt= iDriveMap.Count();
       
   714 		}
       
   715 
       
   716 	for(; i<cnt; i++)
       
   717 		{
       
   718 
       
   719 		CMassStorageDrive* drive = Drive(i, err);
       
   720 		if(drive)
       
   721 			{
       
   722 			err = drive->SetCritical(aCritical);
       
   723 			}
       
   724 		}
       
   725 	return err;
       
   726 	}
       
   727 
       
   728 /**
       
   729 Inititiate transition to Connected.
       
   730 @return KErrNone on success, otherwise system wide error code
       
   731 @param aLun The Logical Drive Unit identifier (0..numDrives-1)
       
   732 @post The Mount State will be Connected or Connecting.
       
   733 */
       
   734 TInt CDriveManager::Connect(TUint aLun)
       
   735 	{
       
   736 	__FNLOG("CDriveManager::Connect");
       
   737 
       
   738 	TInt err = KErrUnknown; // never return this
       
   739 	CMassStorageDrive* drive = Drive(aLun, err);
       
   740 
       
   741 	__PRINT3(_L("CDriveManager::Connect lun=%d, err=%d, mountState=%d\n"), aLun, err, drive->MountState());
       
   742 
       
   743 	if(drive)
       
   744 		{
       
   745 		switch(drive->MountState())
       
   746 			{
       
   747 			case CMassStorageDrive::EDisconnected:
       
   748 				err = drive->SetMountConnecting();
       
   749 				break;
       
   750 			case CMassStorageDrive::EDisconnecting:
       
   751 				err = drive->SetMountConnected();
       
   752 				break;
       
   753 			case CMassStorageDrive::EConnected:
       
   754 			case CMassStorageDrive::EConnecting:
       
   755 				// do nothing
       
   756 				break;
       
   757 			}
       
   758 		}
       
   759 	return err;
       
   760 	}
       
   761 
       
   762 /**
       
   763 Inititiate transition to Disconnected.
       
   764 @return KErrNone on success, otherwise system wide error code
       
   765 @param aLun The Logical Drive Unit identifier (0..numDrives-1)
       
   766 @post The Mount State will be Disconnected or Disconnecting.
       
   767 */
       
   768 TInt CDriveManager::Disconnect(TUint aLun)
       
   769 	{
       
   770 	__FNLOG("CDriveManager::Disconnect");
       
   771 
       
   772 	TInt err = KErrUnknown; // never return this
       
   773 	CMassStorageDrive* drive = Drive(aLun, err);
       
   774 
       
   775 	if(drive)
       
   776 		{
       
   777 		switch(drive->MountState())
       
   778 			{
       
   779 			case CMassStorageDrive::EConnected:
       
   780 				err = drive->SetMountDisconnecting();
       
   781 				break;
       
   782 			case CMassStorageDrive::EConnecting:
       
   783 				err = drive->SetMountDisconnected();
       
   784 				break;
       
   785 			case CMassStorageDrive::EDisconnected:
       
   786 			case CMassStorageDrive::EDisconnecting:
       
   787 				// do nothing
       
   788 				break;
       
   789 			}
       
   790 		}
       
   791 	return err;
       
   792 	}
       
   793 
       
   794