os/kernelhwsrv/kerneltest/f32test/shostmassstorage/testclient/usbtestmsclient/inc/drivemanager.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 // CDriveManager and CMassStorageDrive classes for USB Mass Storage.
    15 // 
    16 //
    17 
    18 
    19 
    20 /**
    21  @file
    22  @internalTechnology
    23 */
    24 
    25 #ifndef DRIVEMANAGER_H
    26 #define DRIVEMANAGER_H
    27 
    28 #include "d32locd.h"
    29 
    30 // Forward declarations
    31 class CDriveManager;
    32 class RDriveMediaErrorPublisher;
    33 class RDriveStateChangedPublisher;
    34 class TLocalDriveCapsV4;
    35 class CProxyDrive;
    36 
    37 
    38 
    39 class TMediaParams
    40     {
    41 public:
    42     const TUint32 KDefaultBlockSize = 0x200;  //default block size for FAT
    43 
    44     void Init(TLocalDriveCapsV4& aCaps);
    45 
    46     TUint32 NumBlocks() const {return iNumBlocks;}
    47     TUint64 Size() const {return iSize;};
    48     TUint32 BlockSize() const {return KDefaultBlockSize;}
    49     TBool IsWriteProtected() const {return iMediaAtt & KMediaAttWriteProtected ? ETrue : EFalse;}
    50     TBool IsLocked() const {return iMediaAtt & KMediaAttLocked ? ETrue : EFalse;}
    51 
    52 private:
    53     TLocalDriveCapsV4 iCaps;
    54 
    55     TUint32 iNumBlocks;
    56     TInt64 iSize;
    57     TUint iMediaAtt;
    58     };
    59 
    60 
    61 /**
    62 A private structure that, when Connected, holds references to
    63 the CProxyDrive and the corresponding TBusLocalDrive's Media Changed flag.
    64 */
    65 class TLocalDriveRef
    66 	{
    67 public:
    68     /**
    69     The Drive Media State Machine.
    70     */
    71     enum TDriveState
    72         {
    73         /**
    74         The media is present and ready for access.
    75         */
    76         EIdle,
    77         /**
    78         The media is currently being accessed by Mass Storage.
    79         */
    80         EActive,
    81         /**
    82         The media is present but is password-protected.
    83         */
    84         ELocked,
    85         /**
    86         The media is not physically present.
    87         */
    88         EMediaNotPresent,
    89         /**
    90         No drive.
    91         */
    92         EErrDisMounted
    93         };
    94 
    95 
    96     TLocalDriveRef(CProxyDrive& aProxyDrive,
    97                    TBool& aMediaChanged,
    98                    RDriveStateChangedPublisher& aDriveStateChangedPublisher);
    99 
   100     void SetDriveState(TDriveState aState);
   101     TInt Read(const TInt64& aPos, TInt aLength, TDes8& aBuf, TBool aWholeMedia);
   102     TInt Write(const TInt64& aPos, TDesC8& aBuf, TBool aWholeMedia);
   103     TBool IsMediaChanged(TBool aReset);
   104     TInt SetCritical(TBool aCritical);
   105     TDriveState DriveState() const;
   106     TInt Caps(TLocalDriveCapsV4& aInfo);
   107 
   108 private:
   109     static TBool IsActive(TDriveState aDriveState);
   110 
   111 private:
   112 	CProxyDrive& iProxyDrive;
   113 
   114 	TBool& iMediaChanged;
   115 	/**
   116 	The Drive Media state machine
   117 	*/
   118 	TDriveState iDriveState;
   119 
   120     /**
   121     Reference to publisher for tracking drive state changes.
   122     */
   123     RDriveStateChangedPublisher& iDriveStateChangedPublisher;
   124 	};
   125 
   126 
   127 inline TLocalDriveRef::TLocalDriveRef(CProxyDrive& aProxyDrive,
   128                                       TBool& aMediaChanged,
   129                                       RDriveStateChangedPublisher& aDriveStateChangedPublisher)
   130 :   iProxyDrive(aProxyDrive),
   131     iMediaChanged(aMediaChanged),
   132     iDriveState(EIdle),
   133     iDriveStateChangedPublisher(aDriveStateChangedPublisher)
   134 	{
   135 	}
   136 
   137 
   138 inline TBool TLocalDriveRef::IsActive(TLocalDriveRef::TDriveState aDriveState)
   139 	{
   140 	return aDriveState==TLocalDriveRef::EActive;
   141 	}
   142 
   143 
   144 inline TLocalDriveRef::TDriveState TLocalDriveRef::DriveState() const
   145     {
   146     return iDriveState;
   147     }
   148 
   149 
   150 /**
   151 @internalTechnology
   152 
   153 Along with CDriveManager, this provides an interface between the generic SCSI
   154 protocol component and the target drive unit.  The CMassStorageDrive class is
   155 instantiated by the drive manager, and contains a pointer to the associated
   156 CProxyDrive that was registered by the Mass Storage File System.
   157 */
   158 class CMassStorageDrive: public CBase
   159 	{
   160 public:
   161 	/**
   162 	The Drive Mount State Machine.
   163 	*/
   164 	enum TMountState
   165 		{
   166 		/**
   167 		Unmounted
   168 		*/
   169 		EDisconnected,
   170 		/**
   171 		Not mounted, but SCSI started
   172 		*/
   173 		EConnecting,
   174 		/**
   175 		Mounted
   176 		*/
   177 		EConnected,
   178 		/**
   179 		Not unmounted, but SCSI stopped
   180 		*/
   181 		EDisconnecting
   182 		};
   183 
   184 public:
   185 	static CMassStorageDrive* NewL(RCriticalSection& aCritSec,
   186                                    RDriveStateChangedPublisher& aDriveStateChangedPublisher);
   187 	~CMassStorageDrive();
   188 
   189 private:
   190     void ConstructL();
   191 	CMassStorageDrive(RCriticalSection& aCritSec,
   192 					  RDriveStateChangedPublisher& aDriveStateChangedPublisher);
   193 
   194 public:
   195 
   196 	TInt Read(const TInt64& aPos, TInt aLength, TDes8& aBuf, TBool aWholeMedia = ETrue);
   197 	TInt Write(const TInt64& aPos, TDesC8& aBuf, TBool aWholeMedia = ETrue);
   198 
   199 	TMountState MountState() const;
   200 	TLocalDriveRef::TDriveState DriveState() const;
   201 	TLocalDriveRef::TDriveState CheckDriveState();
   202 	void SetMountDisconnected();
   203 	void SetMountConnecting();
   204 	void SetMountDisconnecting();
   205 	void SetMountConnected();
   206 	void SetMountConnectedL(CProxyDrive& aProxyDrive, TBool& aMediaChanged, RDriveStateChangedPublisher& aDriveStateChangedPublisher);
   207 	TInt SetCritical(TBool aCritical);
   208 	TBool IsMediaChanged(TBool aReset=EFalse);
   209 
   210     const TMediaParams& MediaParams() const {return iMediaParams;}
   211 
   212  private:
   213 	TInt HandleCriticalError();
   214 	TInt ClearCriticalError();
   215 	TInt DoCaps(TLocalDriveCapsV4& aCaps);
   216 	void SetDriveState(TLocalDriveRef::TDriveState aNewState);
   217 	void SetMountState(TMountState aNewState, TBool aCriticalSection = EFalse);
   218 
   219 private:
   220 	/**
   221 	A Critical Section, shared by all instances of CMassStorageDrive, used to ensure
   222 	that iMountState and iProxyDrive are changed atomically.
   223 	*/
   224 	RCriticalSection& iCritSec;
   225 	/**
   226 	The Drive Mount state machine
   227 	*/
   228 	TMountState iMountState;
   229 
   230 	/**
   231 	When Connected, references to CProxyDrive and TBusLocalDrive's Media Changed flag.
   232 	*/
   233 	TLocalDriveRef* iLocalDrive;
   234 	/**
   235 	Publisher for media errors.
   236 	*/
   237 	RDriveMediaErrorPublisher* iDriveMediaErrorPublisher;
   238 	/**
   239 	Reference to publisher for tracking drive state changes.
   240 	*/
   241 	RDriveStateChangedPublisher& iDriveStateChangedPublisher;
   242 
   243     TMediaParams iMediaParams;
   244 	};
   245 
   246 
   247 /**
   248 @internalTechnology
   249 
   250 Along with CMassStorageDrive, this provides an interface between the generic SCSI
   251 protocol component and the target drive unit.  This package is responsible for
   252 maintaining the list of registered drives.  The owner of the controller registers
   253 each drive it wishes to make available to USB Mass Storage along with an
   254 associated Logical Drive Unit identifier.  The SCSI protocol contains a reference
   255 to the drive manager in order to route the incoming request to a drive.
   256 */
   257 class CDriveManager : public CBase
   258 	{
   259 public:
   260 	/**
   261 	The Logical Drive Unit Identifiers (LUN) must be in the range 0..7 due to the
   262 	fact that the status for all drives is encoded into one 32-bit word.
   263 	*/
   264 	enum { KAllLuns = 0xff };
   265 
   266 	static CDriveManager* NewL(const TLunToDriveMap& aDriveMap);
   267 	~CDriveManager();
   268 
   269 	void RegisterDriveL(CProxyDrive& aProxyDrive, TBool& aMediaChanged, TLun aLun);
   270 	void DeregisterDrive(TLun aLun);
   271 	CMassStorageDrive* Drive(TLun aLun) const;
   272 
   273 	void Connect();
   274 	void Connect(TLun aLun);
   275 
   276     void Disconnect();
   277     void Disconnect(TLun aLun);
   278 
   279 	TBool IsMediaChanged(TLun aLun, TBool aReset = EFalse);
   280 	TInt SetCritical(TLun aLun, TBool aCritical);
   281 
   282     TLun MaxLun() const;
   283 
   284 private:
   285 	// private default constructor to ensure that NewL is used
   286 	CDriveManager(TLun aLun);
   287 	void ConstructL(const TLunToDriveMap& aDriveMap);
   288 
   289 private:
   290 	/**
   291 	The array of drives.  The index into the array is a LUN.
   292 	*/
   293 	TMsDriveList iDrives;
   294 
   295 	/**
   296 	Publisher for tracking drive state changes.
   297 	*/
   298 	RDriveStateChangedPublisher* iDriveStateChangedPublisher;
   299 
   300 	TLun iMaxLun;
   301 	/**
   302 	A resource owned by DriveManager but used by the Drive objects.
   303 	*/
   304 	RCriticalSection iDriveCritSec;
   305 	};
   306 
   307 #include "drivemanager.inl"
   308 
   309 #endif // DRIVEMANAGER_H