sl@0: // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // CDriveManager and CMassStorageDrive classes for USB Mass Storage. sl@0: // sl@0: // sl@0: sl@0: sl@0: sl@0: /** sl@0: @file sl@0: @internalTechnology sl@0: */ sl@0: sl@0: #ifndef DRIVEMANAGER_H sl@0: #define DRIVEMANAGER_H sl@0: sl@0: #include "d32locd.h" sl@0: sl@0: // Forward declarations sl@0: class CDriveManager; sl@0: class RDriveMediaErrorPublisher; sl@0: class RDriveStateChangedPublisher; sl@0: class TLocalDriveCapsV4; sl@0: class CProxyDrive; sl@0: sl@0: sl@0: sl@0: class TMediaParams sl@0: { sl@0: public: sl@0: const TUint32 KDefaultBlockSize = 0x200; //default block size for FAT sl@0: sl@0: void Init(TLocalDriveCapsV4& aCaps); sl@0: sl@0: TUint32 NumBlocks() const {return iNumBlocks;} sl@0: TUint64 Size() const {return iSize;}; sl@0: TUint32 BlockSize() const {return KDefaultBlockSize;} sl@0: TBool IsWriteProtected() const {return iMediaAtt & KMediaAttWriteProtected ? ETrue : EFalse;} sl@0: TBool IsLocked() const {return iMediaAtt & KMediaAttLocked ? ETrue : EFalse;} sl@0: sl@0: private: sl@0: TLocalDriveCapsV4 iCaps; sl@0: sl@0: TUint32 iNumBlocks; sl@0: TInt64 iSize; sl@0: TUint iMediaAtt; sl@0: }; sl@0: sl@0: sl@0: /** sl@0: A private structure that, when Connected, holds references to sl@0: the CProxyDrive and the corresponding TBusLocalDrive's Media Changed flag. sl@0: */ sl@0: class TLocalDriveRef sl@0: { sl@0: public: sl@0: /** sl@0: The Drive Media State Machine. sl@0: */ sl@0: enum TDriveState sl@0: { sl@0: /** sl@0: The media is present and ready for access. sl@0: */ sl@0: EIdle, sl@0: /** sl@0: The media is currently being accessed by Mass Storage. sl@0: */ sl@0: EActive, sl@0: /** sl@0: The media is present but is password-protected. sl@0: */ sl@0: ELocked, sl@0: /** sl@0: The media is not physically present. sl@0: */ sl@0: EMediaNotPresent, sl@0: /** sl@0: No drive. sl@0: */ sl@0: EErrDisMounted sl@0: }; sl@0: sl@0: sl@0: TLocalDriveRef(CProxyDrive& aProxyDrive, sl@0: TBool& aMediaChanged, sl@0: RDriveStateChangedPublisher& aDriveStateChangedPublisher); sl@0: sl@0: void SetDriveState(TDriveState aState); sl@0: TInt Read(const TInt64& aPos, TInt aLength, TDes8& aBuf, TBool aWholeMedia); sl@0: TInt Write(const TInt64& aPos, TDesC8& aBuf, TBool aWholeMedia); sl@0: TBool IsMediaChanged(TBool aReset); sl@0: TInt SetCritical(TBool aCritical); sl@0: TDriveState DriveState() const; sl@0: TInt Caps(TLocalDriveCapsV4& aInfo); sl@0: sl@0: private: sl@0: static TBool IsActive(TDriveState aDriveState); sl@0: sl@0: private: sl@0: CProxyDrive& iProxyDrive; sl@0: sl@0: TBool& iMediaChanged; sl@0: /** sl@0: The Drive Media state machine sl@0: */ sl@0: TDriveState iDriveState; sl@0: sl@0: /** sl@0: Reference to publisher for tracking drive state changes. sl@0: */ sl@0: RDriveStateChangedPublisher& iDriveStateChangedPublisher; sl@0: }; sl@0: sl@0: sl@0: inline TLocalDriveRef::TLocalDriveRef(CProxyDrive& aProxyDrive, sl@0: TBool& aMediaChanged, sl@0: RDriveStateChangedPublisher& aDriveStateChangedPublisher) sl@0: : iProxyDrive(aProxyDrive), sl@0: iMediaChanged(aMediaChanged), sl@0: iDriveState(EIdle), sl@0: iDriveStateChangedPublisher(aDriveStateChangedPublisher) sl@0: { sl@0: } sl@0: sl@0: sl@0: inline TBool TLocalDriveRef::IsActive(TLocalDriveRef::TDriveState aDriveState) sl@0: { sl@0: return aDriveState==TLocalDriveRef::EActive; sl@0: } sl@0: sl@0: sl@0: inline TLocalDriveRef::TDriveState TLocalDriveRef::DriveState() const sl@0: { sl@0: return iDriveState; sl@0: } sl@0: sl@0: sl@0: /** sl@0: @internalTechnology sl@0: sl@0: Along with CDriveManager, this provides an interface between the generic SCSI sl@0: protocol component and the target drive unit. The CMassStorageDrive class is sl@0: instantiated by the drive manager, and contains a pointer to the associated sl@0: CProxyDrive that was registered by the Mass Storage File System. sl@0: */ sl@0: class CMassStorageDrive: public CBase sl@0: { sl@0: public: sl@0: /** sl@0: The Drive Mount State Machine. sl@0: */ sl@0: enum TMountState sl@0: { sl@0: /** sl@0: Unmounted sl@0: */ sl@0: EDisconnected, sl@0: /** sl@0: Not mounted, but SCSI started sl@0: */ sl@0: EConnecting, sl@0: /** sl@0: Mounted sl@0: */ sl@0: EConnected, sl@0: /** sl@0: Not unmounted, but SCSI stopped sl@0: */ sl@0: EDisconnecting sl@0: }; sl@0: sl@0: public: sl@0: static CMassStorageDrive* NewL(RCriticalSection& aCritSec, sl@0: RDriveStateChangedPublisher& aDriveStateChangedPublisher); sl@0: ~CMassStorageDrive(); sl@0: sl@0: private: sl@0: void ConstructL(); sl@0: CMassStorageDrive(RCriticalSection& aCritSec, sl@0: RDriveStateChangedPublisher& aDriveStateChangedPublisher); sl@0: sl@0: public: sl@0: sl@0: TInt Read(const TInt64& aPos, TInt aLength, TDes8& aBuf, TBool aWholeMedia = ETrue); sl@0: TInt Write(const TInt64& aPos, TDesC8& aBuf, TBool aWholeMedia = ETrue); sl@0: sl@0: TMountState MountState() const; sl@0: TLocalDriveRef::TDriveState DriveState() const; sl@0: TLocalDriveRef::TDriveState CheckDriveState(); sl@0: void SetMountDisconnected(); sl@0: void SetMountConnecting(); sl@0: void SetMountDisconnecting(); sl@0: void SetMountConnected(); sl@0: void SetMountConnectedL(CProxyDrive& aProxyDrive, TBool& aMediaChanged, RDriveStateChangedPublisher& aDriveStateChangedPublisher); sl@0: TInt SetCritical(TBool aCritical); sl@0: TBool IsMediaChanged(TBool aReset=EFalse); sl@0: sl@0: const TMediaParams& MediaParams() const {return iMediaParams;} sl@0: sl@0: private: sl@0: TInt HandleCriticalError(); sl@0: TInt ClearCriticalError(); sl@0: TInt DoCaps(TLocalDriveCapsV4& aCaps); sl@0: void SetDriveState(TLocalDriveRef::TDriveState aNewState); sl@0: void SetMountState(TMountState aNewState, TBool aCriticalSection = EFalse); sl@0: sl@0: private: sl@0: /** sl@0: A Critical Section, shared by all instances of CMassStorageDrive, used to ensure sl@0: that iMountState and iProxyDrive are changed atomically. sl@0: */ sl@0: RCriticalSection& iCritSec; sl@0: /** sl@0: The Drive Mount state machine sl@0: */ sl@0: TMountState iMountState; sl@0: sl@0: /** sl@0: When Connected, references to CProxyDrive and TBusLocalDrive's Media Changed flag. sl@0: */ sl@0: TLocalDriveRef* iLocalDrive; sl@0: /** sl@0: Publisher for media errors. sl@0: */ sl@0: RDriveMediaErrorPublisher* iDriveMediaErrorPublisher; sl@0: /** sl@0: Reference to publisher for tracking drive state changes. sl@0: */ sl@0: RDriveStateChangedPublisher& iDriveStateChangedPublisher; sl@0: sl@0: TMediaParams iMediaParams; sl@0: }; sl@0: sl@0: sl@0: /** sl@0: @internalTechnology sl@0: sl@0: Along with CMassStorageDrive, this provides an interface between the generic SCSI sl@0: protocol component and the target drive unit. This package is responsible for sl@0: maintaining the list of registered drives. The owner of the controller registers sl@0: each drive it wishes to make available to USB Mass Storage along with an sl@0: associated Logical Drive Unit identifier. The SCSI protocol contains a reference sl@0: to the drive manager in order to route the incoming request to a drive. sl@0: */ sl@0: class CDriveManager : public CBase sl@0: { sl@0: public: sl@0: /** sl@0: The Logical Drive Unit Identifiers (LUN) must be in the range 0..7 due to the sl@0: fact that the status for all drives is encoded into one 32-bit word. sl@0: */ sl@0: enum { KAllLuns = 0xff }; sl@0: sl@0: static CDriveManager* NewL(const TLunToDriveMap& aDriveMap); sl@0: ~CDriveManager(); sl@0: sl@0: void RegisterDriveL(CProxyDrive& aProxyDrive, TBool& aMediaChanged, TLun aLun); sl@0: void DeregisterDrive(TLun aLun); sl@0: CMassStorageDrive* Drive(TLun aLun) const; sl@0: sl@0: void Connect(); sl@0: void Connect(TLun aLun); sl@0: sl@0: void Disconnect(); sl@0: void Disconnect(TLun aLun); sl@0: sl@0: TBool IsMediaChanged(TLun aLun, TBool aReset = EFalse); sl@0: TInt SetCritical(TLun aLun, TBool aCritical); sl@0: sl@0: TLun MaxLun() const; sl@0: sl@0: private: sl@0: // private default constructor to ensure that NewL is used sl@0: CDriveManager(TLun aLun); sl@0: void ConstructL(const TLunToDriveMap& aDriveMap); sl@0: sl@0: private: sl@0: /** sl@0: The array of drives. The index into the array is a LUN. sl@0: */ sl@0: TMsDriveList iDrives; sl@0: sl@0: /** sl@0: Publisher for tracking drive state changes. sl@0: */ sl@0: RDriveStateChangedPublisher* iDriveStateChangedPublisher; sl@0: sl@0: TLun iMaxLun; sl@0: /** sl@0: A resource owned by DriveManager but used by the Drive objects. sl@0: */ sl@0: RCriticalSection iDriveCritSec; sl@0: }; sl@0: sl@0: #include "drivemanager.inl" sl@0: sl@0: #endif // DRIVEMANAGER_H