os/kernelhwsrv/kerneltest/f32test/shostmassstorage/testclient/usbtestmsclient/inc/drivemanager.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/f32test/shostmassstorage/testclient/usbtestmsclient/inc/drivemanager.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,309 @@
     1.4 +// Copyright (c) 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 +// CDriveManager and CMassStorageDrive classes for USB Mass Storage.
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + @file
    1.25 + @internalTechnology
    1.26 +*/
    1.27 +
    1.28 +#ifndef DRIVEMANAGER_H
    1.29 +#define DRIVEMANAGER_H
    1.30 +
    1.31 +#include "d32locd.h"
    1.32 +
    1.33 +// Forward declarations
    1.34 +class CDriveManager;
    1.35 +class RDriveMediaErrorPublisher;
    1.36 +class RDriveStateChangedPublisher;
    1.37 +class TLocalDriveCapsV4;
    1.38 +class CProxyDrive;
    1.39 +
    1.40 +
    1.41 +
    1.42 +class TMediaParams
    1.43 +    {
    1.44 +public:
    1.45 +    const TUint32 KDefaultBlockSize = 0x200;  //default block size for FAT
    1.46 +
    1.47 +    void Init(TLocalDriveCapsV4& aCaps);
    1.48 +
    1.49 +    TUint32 NumBlocks() const {return iNumBlocks;}
    1.50 +    TUint64 Size() const {return iSize;};
    1.51 +    TUint32 BlockSize() const {return KDefaultBlockSize;}
    1.52 +    TBool IsWriteProtected() const {return iMediaAtt & KMediaAttWriteProtected ? ETrue : EFalse;}
    1.53 +    TBool IsLocked() const {return iMediaAtt & KMediaAttLocked ? ETrue : EFalse;}
    1.54 +
    1.55 +private:
    1.56 +    TLocalDriveCapsV4 iCaps;
    1.57 +
    1.58 +    TUint32 iNumBlocks;
    1.59 +    TInt64 iSize;
    1.60 +    TUint iMediaAtt;
    1.61 +    };
    1.62 +
    1.63 +
    1.64 +/**
    1.65 +A private structure that, when Connected, holds references to
    1.66 +the CProxyDrive and the corresponding TBusLocalDrive's Media Changed flag.
    1.67 +*/
    1.68 +class TLocalDriveRef
    1.69 +	{
    1.70 +public:
    1.71 +    /**
    1.72 +    The Drive Media State Machine.
    1.73 +    */
    1.74 +    enum TDriveState
    1.75 +        {
    1.76 +        /**
    1.77 +        The media is present and ready for access.
    1.78 +        */
    1.79 +        EIdle,
    1.80 +        /**
    1.81 +        The media is currently being accessed by Mass Storage.
    1.82 +        */
    1.83 +        EActive,
    1.84 +        /**
    1.85 +        The media is present but is password-protected.
    1.86 +        */
    1.87 +        ELocked,
    1.88 +        /**
    1.89 +        The media is not physically present.
    1.90 +        */
    1.91 +        EMediaNotPresent,
    1.92 +        /**
    1.93 +        No drive.
    1.94 +        */
    1.95 +        EErrDisMounted
    1.96 +        };
    1.97 +
    1.98 +
    1.99 +    TLocalDriveRef(CProxyDrive& aProxyDrive,
   1.100 +                   TBool& aMediaChanged,
   1.101 +                   RDriveStateChangedPublisher& aDriveStateChangedPublisher);
   1.102 +
   1.103 +    void SetDriveState(TDriveState aState);
   1.104 +    TInt Read(const TInt64& aPos, TInt aLength, TDes8& aBuf, TBool aWholeMedia);
   1.105 +    TInt Write(const TInt64& aPos, TDesC8& aBuf, TBool aWholeMedia);
   1.106 +    TBool IsMediaChanged(TBool aReset);
   1.107 +    TInt SetCritical(TBool aCritical);
   1.108 +    TDriveState DriveState() const;
   1.109 +    TInt Caps(TLocalDriveCapsV4& aInfo);
   1.110 +
   1.111 +private:
   1.112 +    static TBool IsActive(TDriveState aDriveState);
   1.113 +
   1.114 +private:
   1.115 +	CProxyDrive& iProxyDrive;
   1.116 +
   1.117 +	TBool& iMediaChanged;
   1.118 +	/**
   1.119 +	The Drive Media state machine
   1.120 +	*/
   1.121 +	TDriveState iDriveState;
   1.122 +
   1.123 +    /**
   1.124 +    Reference to publisher for tracking drive state changes.
   1.125 +    */
   1.126 +    RDriveStateChangedPublisher& iDriveStateChangedPublisher;
   1.127 +	};
   1.128 +
   1.129 +
   1.130 +inline TLocalDriveRef::TLocalDriveRef(CProxyDrive& aProxyDrive,
   1.131 +                                      TBool& aMediaChanged,
   1.132 +                                      RDriveStateChangedPublisher& aDriveStateChangedPublisher)
   1.133 +:   iProxyDrive(aProxyDrive),
   1.134 +    iMediaChanged(aMediaChanged),
   1.135 +    iDriveState(EIdle),
   1.136 +    iDriveStateChangedPublisher(aDriveStateChangedPublisher)
   1.137 +	{
   1.138 +	}
   1.139 +
   1.140 +
   1.141 +inline TBool TLocalDriveRef::IsActive(TLocalDriveRef::TDriveState aDriveState)
   1.142 +	{
   1.143 +	return aDriveState==TLocalDriveRef::EActive;
   1.144 +	}
   1.145 +
   1.146 +
   1.147 +inline TLocalDriveRef::TDriveState TLocalDriveRef::DriveState() const
   1.148 +    {
   1.149 +    return iDriveState;
   1.150 +    }
   1.151 +
   1.152 +
   1.153 +/**
   1.154 +@internalTechnology
   1.155 +
   1.156 +Along with CDriveManager, this provides an interface between the generic SCSI
   1.157 +protocol component and the target drive unit.  The CMassStorageDrive class is
   1.158 +instantiated by the drive manager, and contains a pointer to the associated
   1.159 +CProxyDrive that was registered by the Mass Storage File System.
   1.160 +*/
   1.161 +class CMassStorageDrive: public CBase
   1.162 +	{
   1.163 +public:
   1.164 +	/**
   1.165 +	The Drive Mount State Machine.
   1.166 +	*/
   1.167 +	enum TMountState
   1.168 +		{
   1.169 +		/**
   1.170 +		Unmounted
   1.171 +		*/
   1.172 +		EDisconnected,
   1.173 +		/**
   1.174 +		Not mounted, but SCSI started
   1.175 +		*/
   1.176 +		EConnecting,
   1.177 +		/**
   1.178 +		Mounted
   1.179 +		*/
   1.180 +		EConnected,
   1.181 +		/**
   1.182 +		Not unmounted, but SCSI stopped
   1.183 +		*/
   1.184 +		EDisconnecting
   1.185 +		};
   1.186 +
   1.187 +public:
   1.188 +	static CMassStorageDrive* NewL(RCriticalSection& aCritSec,
   1.189 +                                   RDriveStateChangedPublisher& aDriveStateChangedPublisher);
   1.190 +	~CMassStorageDrive();
   1.191 +
   1.192 +private:
   1.193 +    void ConstructL();
   1.194 +	CMassStorageDrive(RCriticalSection& aCritSec,
   1.195 +					  RDriveStateChangedPublisher& aDriveStateChangedPublisher);
   1.196 +
   1.197 +public:
   1.198 +
   1.199 +	TInt Read(const TInt64& aPos, TInt aLength, TDes8& aBuf, TBool aWholeMedia = ETrue);
   1.200 +	TInt Write(const TInt64& aPos, TDesC8& aBuf, TBool aWholeMedia = ETrue);
   1.201 +
   1.202 +	TMountState MountState() const;
   1.203 +	TLocalDriveRef::TDriveState DriveState() const;
   1.204 +	TLocalDriveRef::TDriveState CheckDriveState();
   1.205 +	void SetMountDisconnected();
   1.206 +	void SetMountConnecting();
   1.207 +	void SetMountDisconnecting();
   1.208 +	void SetMountConnected();
   1.209 +	void SetMountConnectedL(CProxyDrive& aProxyDrive, TBool& aMediaChanged, RDriveStateChangedPublisher& aDriveStateChangedPublisher);
   1.210 +	TInt SetCritical(TBool aCritical);
   1.211 +	TBool IsMediaChanged(TBool aReset=EFalse);
   1.212 +
   1.213 +    const TMediaParams& MediaParams() const {return iMediaParams;}
   1.214 +
   1.215 + private:
   1.216 +	TInt HandleCriticalError();
   1.217 +	TInt ClearCriticalError();
   1.218 +	TInt DoCaps(TLocalDriveCapsV4& aCaps);
   1.219 +	void SetDriveState(TLocalDriveRef::TDriveState aNewState);
   1.220 +	void SetMountState(TMountState aNewState, TBool aCriticalSection = EFalse);
   1.221 +
   1.222 +private:
   1.223 +	/**
   1.224 +	A Critical Section, shared by all instances of CMassStorageDrive, used to ensure
   1.225 +	that iMountState and iProxyDrive are changed atomically.
   1.226 +	*/
   1.227 +	RCriticalSection& iCritSec;
   1.228 +	/**
   1.229 +	The Drive Mount state machine
   1.230 +	*/
   1.231 +	TMountState iMountState;
   1.232 +
   1.233 +	/**
   1.234 +	When Connected, references to CProxyDrive and TBusLocalDrive's Media Changed flag.
   1.235 +	*/
   1.236 +	TLocalDriveRef* iLocalDrive;
   1.237 +	/**
   1.238 +	Publisher for media errors.
   1.239 +	*/
   1.240 +	RDriveMediaErrorPublisher* iDriveMediaErrorPublisher;
   1.241 +	/**
   1.242 +	Reference to publisher for tracking drive state changes.
   1.243 +	*/
   1.244 +	RDriveStateChangedPublisher& iDriveStateChangedPublisher;
   1.245 +
   1.246 +    TMediaParams iMediaParams;
   1.247 +	};
   1.248 +
   1.249 +
   1.250 +/**
   1.251 +@internalTechnology
   1.252 +
   1.253 +Along with CMassStorageDrive, this provides an interface between the generic SCSI
   1.254 +protocol component and the target drive unit.  This package is responsible for
   1.255 +maintaining the list of registered drives.  The owner of the controller registers
   1.256 +each drive it wishes to make available to USB Mass Storage along with an
   1.257 +associated Logical Drive Unit identifier.  The SCSI protocol contains a reference
   1.258 +to the drive manager in order to route the incoming request to a drive.
   1.259 +*/
   1.260 +class CDriveManager : public CBase
   1.261 +	{
   1.262 +public:
   1.263 +	/**
   1.264 +	The Logical Drive Unit Identifiers (LUN) must be in the range 0..7 due to the
   1.265 +	fact that the status for all drives is encoded into one 32-bit word.
   1.266 +	*/
   1.267 +	enum { KAllLuns = 0xff };
   1.268 +
   1.269 +	static CDriveManager* NewL(const TLunToDriveMap& aDriveMap);
   1.270 +	~CDriveManager();
   1.271 +
   1.272 +	void RegisterDriveL(CProxyDrive& aProxyDrive, TBool& aMediaChanged, TLun aLun);
   1.273 +	void DeregisterDrive(TLun aLun);
   1.274 +	CMassStorageDrive* Drive(TLun aLun) const;
   1.275 +
   1.276 +	void Connect();
   1.277 +	void Connect(TLun aLun);
   1.278 +
   1.279 +    void Disconnect();
   1.280 +    void Disconnect(TLun aLun);
   1.281 +
   1.282 +	TBool IsMediaChanged(TLun aLun, TBool aReset = EFalse);
   1.283 +	TInt SetCritical(TLun aLun, TBool aCritical);
   1.284 +
   1.285 +    TLun MaxLun() const;
   1.286 +
   1.287 +private:
   1.288 +	// private default constructor to ensure that NewL is used
   1.289 +	CDriveManager(TLun aLun);
   1.290 +	void ConstructL(const TLunToDriveMap& aDriveMap);
   1.291 +
   1.292 +private:
   1.293 +	/**
   1.294 +	The array of drives.  The index into the array is a LUN.
   1.295 +	*/
   1.296 +	TMsDriveList iDrives;
   1.297 +
   1.298 +	/**
   1.299 +	Publisher for tracking drive state changes.
   1.300 +	*/
   1.301 +	RDriveStateChangedPublisher* iDriveStateChangedPublisher;
   1.302 +
   1.303 +	TLun iMaxLun;
   1.304 +	/**
   1.305 +	A resource owned by DriveManager but used by the Drive objects.
   1.306 +	*/
   1.307 +	RCriticalSection iDriveCritSec;
   1.308 +	};
   1.309 +
   1.310 +#include "drivemanager.inl"
   1.311 +
   1.312 +#endif // DRIVEMANAGER_H