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