os/kernelhwsrv/userlibandfileserver/fileserver/smassstorage/drivemanager.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/userlibandfileserver/fileserver/smassstorage/drivemanager.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,794 @@
     1.4 +// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// Class implementation of CDriveManager and CMassStorageDrive.
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +/**
    1.22 + @file
    1.23 + @internalTechnology
    1.24 +*/
    1.25 +
    1.26 +#include <f32fsys.h>
    1.27 +#include "massstoragedebug.h"
    1.28 +#include "usbmsshared.h"
    1.29 +#include "drivemanager.h"
    1.30 +
    1.31 +///////////////////////////////////////////////////////////////////////////////
    1.32 +
    1.33 +
    1.34 +/**
    1.35 +A private structure that, when Connected, holds references to 
    1.36 +the CProxyDrive and the corresponding TBusLocalDrive's Media Changed flag.
    1.37 +*/
    1.38 +struct CMassStorageDrive::CLocalDriveRef : public CBase
    1.39 +	{
    1.40 +	CLocalDriveRef(CProxyDrive& aProxyDrive, TBool& aMediaChanged)
    1.41 +		: iProxyDrive(aProxyDrive), iMediaChanged(aMediaChanged), iDriveState(EIdle)
    1.42 +		{
    1.43 +		}
    1.44 +	CProxyDrive& iProxyDrive;
    1.45 +	TBool& iMediaChanged;
    1.46 +	/**
    1.47 +	The Drive Media state machine
    1.48 +	*/
    1.49 +	TDriveState iDriveState;
    1.50 +	};
    1.51 +
    1.52 +/**
    1.53 +@param aCritSec A Critical Section object shared by all drives.
    1.54 +@param aDrives Reference to the list of CMassStorageDrive objects. 
    1.55 +@param aDriveMap Reference to array mapping lun to drive number for supported 
    1.56 +	   mass storage drives.
    1.57 +@post Object is fully constructed
    1.58 + */
    1.59 +CMassStorageDrive::CMassStorageDrive(RCriticalSection& aCritSec, 
    1.60 +									 RDriveStateChangedPublisher& aDriveStateChangedPublisher)
    1.61 +	: 
    1.62 +	iCritSec(aCritSec),
    1.63 +	iMountState(EDisconnected),
    1.64 +	iDriveStateChangedPublisher(aDriveStateChangedPublisher)
    1.65 +	{
    1.66 +	__FNLOG("CMassStorageDrive::CMassStorageDrive");
    1.67 +
    1.68 +	}
    1.69 +
    1.70 +CMassStorageDrive::~CMassStorageDrive()
    1.71 +	{
    1.72 +	delete iLocalDrive;
    1.73 +	}
    1.74 +
    1.75 +/**
    1.76 +Read from the target drive unit.
    1.77 +@return KErrNone on success, otherwise system wide error code
    1.78 +*/
    1.79 +TInt CMassStorageDrive::Read(const TInt64& aPos, TInt aLength, TDes8& aBuf, TBool aWholeMedia)
    1.80 +	{
    1.81 +	__FNLOG("CMassStorageDrive::Read");
    1.82 +
    1.83 +	TInt err = KErrUnknown; // never return this
    1.84 +	iCritSec.Wait();
    1.85 +
    1.86 +	if(iMountState != EConnected)
    1.87 +		{
    1.88 +		err = KErrDisconnected;
    1.89 +		}
    1.90 +	else
    1.91 +		{
    1.92 +		if(aWholeMedia)
    1.93 +			{
    1.94 +			err = SafeProxyDrive().Read(aPos, aLength, &aBuf, KLocalMessageHandle, 0, RLocalDrive::ELocDrvWholeMedia);
    1.95 +			}
    1.96 +		else
    1.97 +			{
    1.98 +			err = SafeProxyDrive().Read(aPos, aLength, aBuf);
    1.99 +			}
   1.100 +
   1.101 +		if(err == KErrNone)
   1.102 +			{
   1.103 +#ifndef USB_TRANSFER_PUBLISHER
   1.104 +			iBytesRead += aBuf.Length();
   1.105 +			__PRINT1(_L("iBytesRead=%d\n"),iBytesRead);
   1.106 +#endif
   1.107 +			}
   1.108 +
   1.109 +		else if(err == KErrLocked)
   1.110 +			{
   1.111 +			SetDriveState(ELocked);
   1.112 +			}
   1.113 +		}
   1.114 +
   1.115 +	iCritSec.Signal();
   1.116 +	return err;
   1.117 +	}
   1.118 +
   1.119 +/**
   1.120 +Write to the target drive unit.
   1.121 +@return KErrNone on success, otherwise system wide error code
   1.122 +*/
   1.123 +TInt CMassStorageDrive::Write(const TInt64& aPos, TDesC8& aBuf, TBool aWholeMedia)
   1.124 +	{
   1.125 +	__FNLOG("CMassStorageDrive::Write");
   1.126 +
   1.127 +	TInt err = KErrNone;
   1.128 +	iCritSec.Wait();
   1.129 +
   1.130 +	if (iMountState != EConnected)
   1.131 +		{
   1.132 +		err = KErrDisconnected;
   1.133 +		}
   1.134 +	else
   1.135 +		{
   1.136 +		__ASSERT_DEBUG(iLocalDrive, User::Invariant());
   1.137 +		TDriveState oldState = iLocalDrive->iDriveState;
   1.138 +		if(oldState != EActive)
   1.139 +			{
   1.140 +			// SCSI hasn't called SetCritical
   1.141 +			SetDriveState(EActive);
   1.142 +			}
   1.143 +
   1.144 +		if (aWholeMedia)
   1.145 +			{
   1.146 +			err = SafeProxyDrive().Write(aPos, aBuf.Length(), &aBuf, KLocalMessageHandle, 0, RLocalDrive::ELocDrvWholeMedia);
   1.147 +			}
   1.148 +		else
   1.149 +			{
   1.150 +			err = SafeProxyDrive().Write(aPos,aBuf);
   1.151 +			}
   1.152 +
   1.153 +		if (err == KErrNone)
   1.154 +			{
   1.155 +#ifndef USB_TRANSFER_PUBLISHER
   1.156 +			iBytesWritten += aBuf.Length();
   1.157 +#endif
   1.158 +			}
   1.159 +
   1.160 +		if (err == KErrLocked)
   1.161 +			{
   1.162 +			SetDriveState(ELocked);
   1.163 +			}
   1.164 +		else if (oldState != EActive)
   1.165 +			{
   1.166 +			SetDriveState(oldState);
   1.167 +			}
   1.168 +		}
   1.169 +
   1.170 +	iCritSec.Signal();
   1.171 +	return err;
   1.172 +	}
   1.173 +
   1.174 +/**
   1.175 +Get the capabilities of the target drive unit.
   1.176 +@return KErrNone on success, otherwise system wide error code
   1.177 +*/
   1.178 +TInt CMassStorageDrive::Caps(TLocalDriveCapsV4& aInfo)
   1.179 +	{
   1.180 +	__FNLOG("CMassStorageDrive::Caps");
   1.181 +
   1.182 +	TInt err = KErrNone;
   1.183 +	iCritSec.Wait();
   1.184 +	
   1.185 +	if(iMountState != EConnected)
   1.186 +		{
   1.187 +		err = KErrDisconnected;
   1.188 +		}
   1.189 +	else 
   1.190 +		{
   1.191 +		// Initialise in case Caps() fails
   1.192 +		aInfo.iType = ::EMediaUnknown; 
   1.193 +		err = DoCaps(aInfo);
   1.194 +
   1.195 +		__PRINTERR(_L("CheckDriveState: DoCaps err=%d\n"), err);
   1.196 +
   1.197 +		if(err == KErrNotReady || (err==KErrNone && aInfo.iType == ::EMediaNotPresent))
   1.198 +			{
   1.199 +			__PRINT(_L("CMassStorageDrive::Caps detected MediaNotPresent\n"));
   1.200 +			SetDriveState(CMassStorageDrive::EMediaNotPresent);
   1.201 +			}
   1.202 +		}
   1.203 +
   1.204 +	iCritSec.Signal();
   1.205 +	return err;
   1.206 +	}
   1.207 +
   1.208 +/**
   1.209 +Provides an interface to CProxyDrive::Caps that hides the
   1.210 +package buffer.
   1.211 +@return KErrNone on success, otherwise system wide error code
   1.212 +@param aInfo
   1.213 +*/
   1.214 +TInt CMassStorageDrive::DoCaps(TLocalDriveCapsV4& aInfo)
   1.215 +	{
   1.216 +	__FNLOG("CMassStorageDrive::DoCaps");
   1.217 +
   1.218 +	TLocalDriveCapsV4Buf buf;
   1.219 +	buf.FillZ();
   1.220 +	CProxyDrive& pd = SafeProxyDrive();
   1.221 +
   1.222 +	__PRINT(_L("CMassStorageDrive::DoCaps calling Caps\n"));
   1.223 +	TInt err = pd.Caps(buf);
   1.224 +
   1.225 +	__PRINT1(_L("CMassStorageDrive::DoCaps: Caps returned %d\n"), err);
   1.226 +
   1.227 +	if(err==KErrNone)
   1.228 +		{
   1.229 +		// Invoke function call operator to cast to TLocalDriveCapsV4&
   1.230 +		aInfo = buf();
   1.231 +		}
   1.232 +
   1.233 +	return err;
   1.234 +	}
   1.235 +
   1.236 +/**
   1.237 +Publish media error, user should reinsert the memory card.
   1.238 +Similar to FAT32's TDriver::HandleCriticalError.
   1.239 +Note: User notification is not implemented, instead we abort and dismount.
   1.240 +*/
   1.241 +TInt CMassStorageDrive::HandleCriticalError() 
   1.242 +	{
   1.243 +	__FNLOG("CMassStorageDrive::HandleCriticalError");
   1.244 +	iDriveMediaErrorPublisher.PublishError(ETrue);
   1.245 +	return KErrAbort;
   1.246 +	}
   1.247 +
   1.248 +
   1.249 +TInt CMassStorageDrive::ClearCriticalError()
   1.250 +	{
   1.251 +	iDriveMediaErrorPublisher.PublishError(EFalse);
   1.252 +	return KErrNone;
   1.253 +	}
   1.254 +
   1.255 +
   1.256 +/**
   1.257 +Checks the Media Changed flag, and optionally resets it.
   1.258 +@return The state of the Media Changed flag.
   1.259 +@param aReset If true, the Media Changed flag is reset to EFalse.
   1.260 +*/
   1.261 +TBool CMassStorageDrive::IsMediaChanged(TBool aReset)
   1.262 +	{
   1.263 +	__FNLOG("CMassStorageDrive::IsMediaChanged");
   1.264 +
   1.265 +	iCritSec.Wait();
   1.266 +
   1.267 +	TBool mediaChanged = EFalse;
   1.268 +	if(iLocalDrive)
   1.269 +		{
   1.270 +		mediaChanged = iLocalDrive->iMediaChanged;
   1.271 +		if(aReset) 
   1.272 +			{
   1.273 +			iLocalDrive->iMediaChanged = EFalse;
   1.274 +			}
   1.275 +		}
   1.276 +	else
   1.277 +		{
   1.278 +		__PRINT(_L("CMassStorageDrive::IsMediaChanged: no drive\n"));
   1.279 +		}
   1.280 +
   1.281 +	iCritSec.Signal();
   1.282 +
   1.283 +	__PRINT1(_L("CMassStorageDrive::IsMediaChanged: returning %d\n"), mediaChanged);
   1.284 +	return mediaChanged;
   1.285 +	}
   1.286 +
   1.287 +/**
   1.288 +Set the Drive State to Active or Idle.
   1.289 +@return KErrNone on success, KErrNotReady if media not present, KErrDisMounted if not mounted
   1.290 +@param aCritical ETrue for Active, EFalse for Idle
   1.291 +*/
   1.292 +TInt CMassStorageDrive::SetCritical(TBool aCritical)
   1.293 +	{
   1.294 +	__FNLOG("CMassStorageDrive::SetCritical");
   1.295 +
   1.296 +	TInt err = KErrDisMounted;
   1.297 +
   1.298 +	iCritSec.Wait();
   1.299 +
   1.300 +	if(iLocalDrive)
   1.301 +		{
   1.302 +		if(iLocalDrive->iDriveState == CMassStorageDrive::EMediaNotPresent)
   1.303 +			{
   1.304 +			err = KErrNotReady;
   1.305 +			}
   1.306 +		else
   1.307 +			{
   1.308 +			SetDriveState(
   1.309 +				aCritical 
   1.310 +				? CMassStorageDrive::EActive
   1.311 +				: CMassStorageDrive::EIdle );
   1.312 +				
   1.313 +			err = KErrNone;
   1.314 +			}
   1.315 +		}
   1.316 +
   1.317 +	iCritSec.Signal();
   1.318 +
   1.319 +	return err;
   1.320 +	}
   1.321 +
   1.322 +/**
   1.323 +Set the mount state
   1.324 +*/
   1.325 +TInt CMassStorageDrive::SetMountConnected(CProxyDrive& aProxyDrive, TBool& aMediaChanged)
   1.326 +	{
   1.327 +	__FNLOG("CMassStorageDrive::SetMountConnected");
   1.328 +	CLocalDriveRef* localDrive = NULL;
   1.329 +
   1.330 +	__PRINT(_L("SetMountConnected entering critical section\n"));
   1.331 +	iCritSec.Wait(); // note: signalled in SetMountState
   1.332 +
   1.333 +	if (iLocalDrive == NULL)
   1.334 +		{
   1.335 +		localDrive = new CLocalDriveRef(aProxyDrive, aMediaChanged);
   1.336 +		if (localDrive==NULL) 
   1.337 +			{
   1.338 +			iCritSec.Signal();
   1.339 +			return KErrNoMemory;
   1.340 +			}
   1.341 +		}
   1.342 +
   1.343 +	return SetMountState(EConnected, localDrive);
   1.344 +	}
   1.345 +
   1.346 +/**
   1.347 +@return KErrNone
   1.348 +@param aNewState
   1.349 +@param aLocalDrive Only provide this if aNewState is EConnected.
   1.350 +*/
   1.351 +TInt CMassStorageDrive::SetMountState(TMountState aNewState, CLocalDriveRef* aLocalDrive/*=NULL*/)
   1.352 +	{
   1.353 +	__FNLOG("CMassStorageDrive::SetMountState");
   1.354 +	
   1.355 +	if(iMountState == aNewState)
   1.356 +		{
   1.357 +		__PRINT(_L("SetMountState: No change\n"));
   1.358 +		}
   1.359 +	else
   1.360 +		{
   1.361 +		// If called from SetMountConnected, already in critical section, 
   1.362 +		// otherwise, must enter it here.
   1.363 +		if(EConnected!=aNewState)
   1.364 +			{
   1.365 +			__PRINT(_L("SetMountState entering critical section\n"));
   1.366 +			iCritSec.Wait();
   1.367 +			}
   1.368 +
   1.369 +		switch(aNewState)
   1.370 +			{
   1.371 +			case EConnected:
   1.372 +				if(aLocalDrive)
   1.373 +					{
   1.374 +					__ASSERT_DEBUG(!iLocalDrive, User::Invariant());
   1.375 +					iLocalDrive = aLocalDrive;
   1.376 +					}
   1.377 +				__ASSERT_DEBUG(iLocalDrive, User::Invariant());
   1.378 +				break;
   1.379 +
   1.380 +			case EDisconnected:
   1.381 +				delete iLocalDrive;
   1.382 +				iLocalDrive = NULL;
   1.383 +#ifndef USB_TRANSFER_PUBLISHER
   1.384 +				iBytesWritten = iBytesRead = 0;
   1.385 +#endif
   1.386 +				break;
   1.387 +
   1.388 +			case EDisconnecting:
   1.389 +			case EConnecting:
   1.390 +				// Do not change iLocalDrive for these state changes
   1.391 +				break;
   1.392 +			}
   1.393 +		
   1.394 +		iMountState = aNewState;
   1.395 +		__PRINT1(_L("SetMountState: state=%d\n"), iMountState);
   1.396 +
   1.397 +		iDriveStateChangedPublisher.DriveStateChanged();
   1.398 +		
   1.399 +		iCritSec.Signal();
   1.400 +		__PRINT(_L("SetMountState has left the critical section\n"));
   1.401 +		}
   1.402 +
   1.403 +	return KErrNone;
   1.404 +	}
   1.405 +
   1.406 +/**
   1.407 +@return Current drive media state
   1.408 +*/
   1.409 +CMassStorageDrive::TDriveState CMassStorageDrive::DriveState() const
   1.410 +	{
   1.411 +	return iLocalDrive ? iLocalDrive->iDriveState : EErrDisMounted;
   1.412 +	}
   1.413 +
   1.414 +/**
   1.415 +Check for media not present, and return the drive state.
   1.416 +@return Current drive media state
   1.417 +*/
   1.418 +CMassStorageDrive::TDriveState CMassStorageDrive::CheckDriveState()
   1.419 +	{
   1.420 +	__FNLOG("CMassStorageDrive::CheckDriveState");
   1.421 +
   1.422 +	CMassStorageDrive::TDriveState state = EErrDisMounted;
   1.423 +
   1.424 +	iCritSec.Wait();
   1.425 +
   1.426 +	if (iLocalDrive)
   1.427 +		{
   1.428 +		TInt err = KErrGeneral;
   1.429 +		TLocalDriveCapsV4 caps;
   1.430 +
   1.431 +		FOREVER
   1.432 +			{
   1.433 +			// Initialise in case Caps() fails
   1.434 +			caps.iType = ::EMediaNotPresent; 
   1.435 +
   1.436 +			err = DoCaps(caps);
   1.437 +			__PRINTERR(_L("CheckDriveState: DoCaps err=%d\n"), err);
   1.438 +			if (err == KErrNotReady || (err == KErrNone && caps.iType == ::EMediaNotPresent))
   1.439 +				{
   1.440 +				__PRINT(_L("CheckDriveState: detected MediaNotPresent\n"));
   1.441 +
   1.442 +				SetDriveState(CMassStorageDrive::EMediaNotPresent);
   1.443 +
   1.444 +				if (HandleCriticalError() == KErrAbort)
   1.445 +					break;
   1.446 +				}
   1.447 +			else 
   1.448 +				{
   1.449 +				ClearCriticalError();
   1.450 +				break;
   1.451 +				}
   1.452 +			}
   1.453 +
   1.454 +		if (err == KErrNone && caps.iType != ::EMediaNotPresent)
   1.455 +			{
   1.456 +			if (iLocalDrive->iDriveState == CMassStorageDrive::EMediaNotPresent)
   1.457 +				{
   1.458 +				__PRINT(_L("CheckDriveState: detected media inserted\n"));
   1.459 +				SetDriveState(CMassStorageDrive::EIdle);
   1.460 +				}
   1.461 +			else if (iLocalDrive->iDriveState == CMassStorageDrive::ELocked &&
   1.462 +					 !(caps.iMediaAtt & KMediaAttLocked))
   1.463 +				{
   1.464 +				__PRINT(_L("CheckDriveState: detected media unlocked\n"));
   1.465 +				SetDriveState(CMassStorageDrive::EIdle);
   1.466 +				}
   1.467 +			else if (caps.iMediaAtt & KMediaAttLocked)
   1.468 +				{
   1.469 +				__PRINT(_L("CheckDriveState: detected media locked\n"));
   1.470 +				SetDriveState(CMassStorageDrive::ELocked);
   1.471 +				}
   1.472 +			
   1.473 +			iWholeMediaAccess = !(caps.iDriveAtt & KDriveAttLogicallyRemovable);
   1.474 +			}
   1.475 +
   1.476 +		// Get the current state
   1.477 +		state = iLocalDrive->iDriveState;
   1.478 +		}
   1.479 +
   1.480 +	iCritSec.Signal();
   1.481 +
   1.482 +	return state;
   1.483 +	}
   1.484 +
   1.485 +static TBool IsActive(CMassStorageDrive::TDriveState aDriveState)
   1.486 +	{
   1.487 +	return aDriveState==CMassStorageDrive::EActive;
   1.488 +	}
   1.489 +
   1.490 +/**
   1.491 +@param aNewState
   1.492 +*/
   1.493 +void CMassStorageDrive::SetDriveState(TDriveState aNewState)
   1.494 +	{
   1.495 +	__FNLOG("CMassStorageDrive::SetDriveState");
   1.496 +
   1.497 +	__ASSERT_DEBUG(aNewState == EIdle ||
   1.498 +                   (iMountState == EConnected && NULL != iLocalDrive) ||
   1.499 +                   (iMountState == EDisconnecting && NULL != iLocalDrive),
   1.500 +        User::Invariant());
   1.501 +
   1.502 +	if(!iLocalDrive)
   1.503 +		{
   1.504 +		__PRINT(_L("SetDriveState: Drive not mounted.\n"));
   1.505 +		}
   1.506 +	else
   1.507 +		{
   1.508 +		__PRINT2(_L("SetDriveState: %d->%d\n"), iLocalDrive->iDriveState, aNewState);
   1.509 +
   1.510 +		if(iLocalDrive->iDriveState != aNewState)
   1.511 +			{
   1.512 +			CMountCB* mount = SafeProxyDrive().Mount();
   1.513 +#if !defined(USBMSDRIVE_TEST)
   1.514 +			__ASSERT_DEBUG(mount != NULL, User::Invariant());
   1.515 +#endif
   1.516 +			if(mount)
   1.517 +				{
   1.518 +				if(!IsActive(iLocalDrive->iDriveState) && IsActive(aNewState))
   1.519 +					{
   1.520 +					mount->IncLock();
   1.521 +					}
   1.522 +				else if(IsActive(iLocalDrive->iDriveState) && !IsActive(aNewState))
   1.523 +					{
   1.524 +					mount->DecLock();
   1.525 +					}
   1.526 +				__PRINT1(_L("SetDriveState: LockStatus=%d\n"), mount->LockStatus());
   1.527 +				}
   1.528 +
   1.529 +			iLocalDrive->iDriveState = aNewState;
   1.530 +
   1.531 +			iDriveStateChangedPublisher.DriveStateChanged();
   1.532 +			}
   1.533 +		}
   1.534 +	}
   1.535 +
   1.536 +/**
   1.537 +Accessor for iProxyDrive; asserts if NULL
   1.538 +*/
   1.539 +CProxyDrive& CMassStorageDrive::SafeProxyDrive() const
   1.540 +	{
   1.541 +	__ASSERT_ALWAYS(NULL!=iLocalDrive, User::Invariant());
   1.542 +	return iLocalDrive->iProxyDrive;
   1.543 +	}
   1.544 +
   1.545 +/////////////////////////////////////////////////////////////////
   1.546 +
   1.547 +/**
   1.548 +Construct a CDriveManager object.
   1.549 +@param aDriveMap Reference to array mapping lun to drive number for supported 
   1.550 +	   mass storage drives.
   1.551 +*/
   1.552 +CDriveManager* CDriveManager::NewL(TRefDriveMap aDriveMap)
   1.553 +	{
   1.554 +	__FNLOG("CDriveManager::NewL");
   1.555 +	__PRINT1(_L("CDriveManager::NewL - %d drives\n"), aDriveMap.Count());
   1.556 +
   1.557 +	CDriveManager* self = new (ELeave) CDriveManager(aDriveMap);
   1.558 +	CleanupStack::PushL(self);
   1.559 +	self->ConstructL();
   1.560 +	CleanupStack::Pop();
   1.561 +	return self;
   1.562 +	}
   1.563 +
   1.564 +CDriveManager::CDriveManager(const RArray<TInt>& aDriveMap)
   1.565 +	: iDriveMap(aDriveMap)
   1.566 +	{}
   1.567 +
   1.568 +/**
   1.569 +Construct a CDriveManager object.
   1.570 +*/
   1.571 +void CDriveManager::ConstructL()
   1.572 +	{
   1.573 +	__FNLOG("CDriveManager::ConstructL");
   1.574 +
   1.575 +	User::LeaveIfError(iDriveCritSec.CreateLocal());
   1.576 +
   1.577 +	iDriveStateChangedPublisher = new (ELeave) RDriveStateChangedPublisher(iDrives, iDriveMap);
   1.578 +
   1.579 +	for(TInt i = 0; i < iDriveMap.Count(); i++)
   1.580 +		{
   1.581 +		iDrives[i] = new (ELeave) CMassStorageDrive(iDriveCritSec, *iDriveStateChangedPublisher);
   1.582 +		}
   1.583 +
   1.584 +	// Publish initial drive state
   1.585 +	if (iDriveMap.Count() > 0)
   1.586 +		{
   1.587 +		iDriveStateChangedPublisher->DriveStateChanged();
   1.588 +		}
   1.589 +	}
   1.590 +
   1.591 +/**
   1.592 +Destructor
   1.593 +*/
   1.594 +CDriveManager::~CDriveManager()
   1.595 +	{
   1.596 +	__FNLOG("CDriveManager::~CDriveManager");
   1.597 +
   1.598 +	iDrives.DeleteAll();
   1.599 +	delete iDriveStateChangedPublisher;
   1.600 +	iDriveCritSec.Close();
   1.601 +	}
   1.602 +
   1.603 +/**
   1.604 +Set the mount state to Connected and specify the Proxy Drive.
   1.605 +@return KErrNone on success, otherwise system wide error code
   1.606 +@param aDrive The mounted Proxy Drive
   1.607 +@param aLun The Logical Drive Unit identifier (0..numDrives-1)
   1.608 +@pre If the Mount State is Connected, then aDrive must be the
   1.609 +     same as it was the last time this function was called.
   1.610 +@post The Mount State will be Connected.
   1.611 +*/
   1.612 +TInt CDriveManager::RegisterDrive(CProxyDrive& aProxyDrive, TBool& aMediaChanged, TUint aLun)
   1.613 +	{
   1.614 +	__FNLOG("CDriveManager::RegisterDrive");
   1.615 +	__PRINT1(_L("Lun=%d \n"),aLun);
   1.616 +	TInt err = KErrUnknown; // never return this
   1.617 +	CMassStorageDrive* drive = CDriveManager::Drive(aLun, err);
   1.618 +	if(drive)
   1.619 +		{
   1.620 +		drive->SetMountConnected(aProxyDrive, aMediaChanged);
   1.621 +		}
   1.622 +
   1.623 +	__PRINT1(_L("CDriveManager::RegisterDrive err=%d\n"), err);
   1.624 +	return err;
   1.625 +	}
   1.626 +
   1.627 +/**
   1.628 +Set the mount state to Disconnected.
   1.629 +@return KErrNone on success, otherwise system wide error code
   1.630 +@param aLun The Logical Drive Unit identifier (0..numDrives-1)
   1.631 +@post The Mount State will be Disconnected.
   1.632 +*/
   1.633 +TInt CDriveManager::DeregisterDrive(TUint aLun)
   1.634 +	{
   1.635 +	__FNLOG("CDriveManager::DeregisterDrive");
   1.636 +
   1.637 +	TInt err = KErrUnknown; // never return this
   1.638 +	if(CMassStorageDrive* drive = Drive(aLun, err))
   1.639 +		{
   1.640 +		err = drive->SetMountDisconnected();
   1.641 +		}
   1.642 +
   1.643 +	__PRINT1(_L("CDriveManager::DeregisterDrive err=%d\n"), err);
   1.644 +	return err;
   1.645 +	}
   1.646 +
   1.647 +/**
   1.648 +Return a pointer to the drive specified aLun, or NULL if
   1.649 +aLun is invalid.
   1.650 +
   1.651 +@return Pointer to the specified drive, or NULL.
   1.652 +@param aLun The Logical Drive Unit identifier (0..numDrives-1)
   1.653 +@param aError KErrNone on success, KErrArgument if NULL is returned.
   1.654 +*/
   1.655 +CMassStorageDrive* CDriveManager::Drive(TUint aLun, TInt& aError) const
   1.656 +	{
   1.657 +	aError = KErrNone;
   1.658 +	CMassStorageDrive* drive = NULL;
   1.659 +
   1.660 +	// Check if aLun exceeds the specified number of drives
   1.661 +	// (This will panic if it exceeds KMaxLun).
   1.662 +	if(aLun>=KUsbMsMaxDrives || !iDrives[aLun]) 
   1.663 +		{
   1.664 +		aError = KErrArgument;
   1.665 +		}
   1.666 +	else
   1.667 +		{
   1.668 +		drive = iDrives[aLun];
   1.669 +		}
   1.670 +	return drive;
   1.671 +	}
   1.672 +
   1.673 +/**
   1.674 +Checks the Media Changed flag, and optionally resets it.
   1.675 +@return The state of the Media Changed flag.
   1.676 +@param aLun The Logical Drive Unit identifier (0..numDrives-1)
   1.677 +@param aReset If true, the Media Changed flag is reset to EFalse.
   1.678 +*/
   1.679 +TBool CDriveManager::IsMediaChanged(TUint aLun, TBool aReset)
   1.680 +	{
   1.681 +	__FNLOG("CDriveManager::IsMediaChanged");
   1.682 +
   1.683 +	TInt err; // not used, but is a required parameter
   1.684 +	CMassStorageDrive* drive = Drive(aLun, err);
   1.685 +
   1.686 +	if(!drive)
   1.687 +		{
   1.688 +		__PRINT1(_L("CDriveManager::IsMediaChanged: LUN=%d not found, returning false\n"), aLun);
   1.689 +		return ETrue;
   1.690 +		}
   1.691 +	else
   1.692 +		{
   1.693 +		return drive->IsMediaChanged(aReset);
   1.694 +		}
   1.695 +	}
   1.696 +
   1.697 +/**
   1.698 +Set the Drive State to Active or Idle.
   1.699 +Ref: 3.6.3.2 - PREVENT_MEDIUM_REMOVAL
   1.700 +@return KErrNone on success, otherwise system wide error code
   1.701 +@param aLun The Logical Drive Unit identifier (0..numDrives-1)
   1.702 +@param aCritical ETrue for Active, EFalse for Idle
   1.703 +*/
   1.704 +TInt CDriveManager::SetCritical(TUint aLun, TBool aCritical)
   1.705 +	{
   1.706 +	__FNLOG("CDriveManager::SetCritical");
   1.707 +
   1.708 +	TInt err = KErrUnknown; // never return this
   1.709 +
   1.710 +	TInt i=aLun;
   1.711 +	TInt cnt=aLun+1;
   1.712 +	
   1.713 +	if (aLun == KAllLuns)
   1.714 +		{
   1.715 +		i=0;
   1.716 +		cnt= iDriveMap.Count();
   1.717 +		}
   1.718 +
   1.719 +	for(; i<cnt; i++)
   1.720 +		{
   1.721 +
   1.722 +		CMassStorageDrive* drive = Drive(i, err);
   1.723 +		if(drive)
   1.724 +			{
   1.725 +			err = drive->SetCritical(aCritical);
   1.726 +			}
   1.727 +		}
   1.728 +	return err;
   1.729 +	}
   1.730 +
   1.731 +/**
   1.732 +Inititiate transition to Connected.
   1.733 +@return KErrNone on success, otherwise system wide error code
   1.734 +@param aLun The Logical Drive Unit identifier (0..numDrives-1)
   1.735 +@post The Mount State will be Connected or Connecting.
   1.736 +*/
   1.737 +TInt CDriveManager::Connect(TUint aLun)
   1.738 +	{
   1.739 +	__FNLOG("CDriveManager::Connect");
   1.740 +
   1.741 +	TInt err = KErrUnknown; // never return this
   1.742 +	CMassStorageDrive* drive = Drive(aLun, err);
   1.743 +
   1.744 +	__PRINT3(_L("CDriveManager::Connect lun=%d, err=%d, mountState=%d\n"), aLun, err, drive->MountState());
   1.745 +
   1.746 +	if(drive)
   1.747 +		{
   1.748 +		switch(drive->MountState())
   1.749 +			{
   1.750 +			case CMassStorageDrive::EDisconnected:
   1.751 +				err = drive->SetMountConnecting();
   1.752 +				break;
   1.753 +			case CMassStorageDrive::EDisconnecting:
   1.754 +				err = drive->SetMountConnected();
   1.755 +				break;
   1.756 +			case CMassStorageDrive::EConnected:
   1.757 +			case CMassStorageDrive::EConnecting:
   1.758 +				// do nothing
   1.759 +				break;
   1.760 +			}
   1.761 +		}
   1.762 +	return err;
   1.763 +	}
   1.764 +
   1.765 +/**
   1.766 +Inititiate transition to Disconnected.
   1.767 +@return KErrNone on success, otherwise system wide error code
   1.768 +@param aLun The Logical Drive Unit identifier (0..numDrives-1)
   1.769 +@post The Mount State will be Disconnected or Disconnecting.
   1.770 +*/
   1.771 +TInt CDriveManager::Disconnect(TUint aLun)
   1.772 +	{
   1.773 +	__FNLOG("CDriveManager::Disconnect");
   1.774 +
   1.775 +	TInt err = KErrUnknown; // never return this
   1.776 +	CMassStorageDrive* drive = Drive(aLun, err);
   1.777 +
   1.778 +	if(drive)
   1.779 +		{
   1.780 +		switch(drive->MountState())
   1.781 +			{
   1.782 +			case CMassStorageDrive::EConnected:
   1.783 +				err = drive->SetMountDisconnecting();
   1.784 +				break;
   1.785 +			case CMassStorageDrive::EConnecting:
   1.786 +				err = drive->SetMountDisconnected();
   1.787 +				break;
   1.788 +			case CMassStorageDrive::EDisconnected:
   1.789 +			case CMassStorageDrive::EDisconnecting:
   1.790 +				// do nothing
   1.791 +				break;
   1.792 +			}
   1.793 +		}
   1.794 +	return err;
   1.795 +	}
   1.796 +
   1.797 +