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