os/kernelhwsrv/kerneltest/f32test/shostmassstorage/testclient/usbtestmsclient/cmassstoragefilesystem.cpp
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".
 
     8 // Initial Contributors:
 
     9 // Nokia Corporation - initial contribution.
 
    14 // CMassStorageFileSystem implementation.
 
    29 #include "usbmsshared.h"
 
    32 #include "mserverprotocol.h"
 
    33 #include "cusbmassstoragecontroller.h"
 
    34 #include "cmassstoragefilesystem.h"
 
    35 #include "cmassstoragemountcb.h"
 
    38 #include "massstorage.h"
 
    40 _LIT(KMsFsyName, "MassStorageFileSystem");
 
    41 _LIT(KMsThreadName, "MassStorageThread");
 
    42 _LIT(KMsDeadThreadName, "MassStorageDeadThread");
 
    43 _LIT(KMsFsysSemName, "MassStorageSemaphore");
 
    44 LOCAL_D const TInt KMsFsyMajorVersionNumber=1;
 
    45 LOCAL_D const TInt KMsFsyMinorVersionNumber=0;
 
    47 CMassStorageFileSystem::CMassStorageFileSystem()
 
    52 CMassStorageFileSystem::~CMassStorageFileSystem()
 
    55 	//Kill the controller thread if it exists
 
    56 	delete iMassStorageController;
 
    57 	iMediaChangedStatusList.Close();
 
    59 	TInt err = thread.Open(KMsThreadName);
 
    62 		thread.Kill(1); //Parameter is irrelevant
 
    68 CMassStorageFileSystem* CMassStorageFileSystem::NewL()
 
    71 	CMassStorageFileSystem*  self = new (ELeave) CMassStorageFileSystem();
 
    72 	CleanupStack::PushL(self);
 
    74 	CleanupStack::Pop(self);
 
    78 void CMassStorageFileSystem::ConstructL()
 
    84 Set the file system version and name
 
    86 @return Any of the standard Symbian error codes.
 
    88 TInt CMassStorageFileSystem::Install()
 
    91 	iVersion=TVersion(KMsFsyMajorVersionNumber, KMsFsyMinorVersionNumber, KF32BuildVersionNumber);
 
    92 	TInt err = SetName(&KMsFsyName);
 
    96 TInt CMassStorageFileSystem::Remove()
 
   102 		// Try connecting to the server to send a shutdown message.
 
   103 		// - If the class controller has a session in use, this will return KErrInUse
 
   104 		RUsbMassStorage usbMs;
 
   105 		err = usbMs.Connect();
 
   108 			err = usbMs.Shutdown();
 
   113 				User::WaitForRequest(iThreadStat);
 
   114 				err = iThreadStat.Int();
 
   118 				__PRINT1(_L("CMassStorageFileSystem::Remove Shutdown Error %d\n"),err);
 
   123 			__PRINT1(_L("CMassStorageFileSystem::Remove Connect Error %d\n"),err);
 
   130 Creates a new Mass Storage mount object.
 
   132 @return A new CMassStorageMountCB
 
   133 @leave KErrNotReady if the Mass Storage controller is not running.
 
   135 CMountCB* CMassStorageFileSystem::NewMountL() const
 
   140 		User::Leave(KErrNotReady);
 
   142 	return CMassStorageMountCB::NewL(iDriveMap);
 
   146 Sets the media attributes and type in the aInfo parameter to those of the specified drive.
 
   148 @param anInfo TDriveInfo object to store the drive information.
 
   149 @param aDriveNumber The number of the drive to get the information from.
 
   151 void CMassStorageFileSystem::DriveInfo(TDriveInfo& aInfo, TInt aDriveNumber) const
 
   154 	TLocalDriveCapsV2Buf caps;
 
   155 	if (!IsValidLocalDriveMapping(aDriveNumber))
 
   159 	GetLocalDrive(aDriveNumber).Caps(caps);
 
   160 	aInfo.iMediaAtt=caps().iMediaAtt;
 
   161     aInfo.iType = ::EMediaNotPresent;  // Media is not available to the file system
 
   162 	aInfo.iDriveAtt=caps().iDriveAtt;
 
   166 Returns a reference to the Mass Storage controller.
 
   168 @return Reference to the Mass Storage controller.
 
   170 CUsbMassStorageController& CMassStorageFileSystem::Controller()
 
   173 	return *iMassStorageController;
 
   177 Fill iMsDrives with a mapping of lun->drive number for supported mass storage drives
 
   180 TInt CMassStorageFileSystem::EnumerateMsDrivesL()
 
   186 	TLocalDriveCapsV2Buf caps;
 
   188 	for (TInt i = EDriveC; i < KMaxDrives; i++)
 
   192 		if (IsValidLocalDriveMapping(i))
 
   194 			TInt err = GetLocalDrive(i).Caps(caps);
 
   195 			TInt locDrvNum = DriveNumberToLocalDriveNumber(i);
 
   196 			__PRINT2(_L("Caps: err=%d, att=%d\n"), err, caps().iDriveAtt);
 
   198 			TBool isRemovable = err==KErrNotReady || (caps().iDriveAtt & KDriveAttRemovable);
 
   199 			__PRINT2(_L("EnumerateMsDrives: Drive %c: is %sremovable\n"),
 
   201 							isRemovable?_S(""):_S("NOT "));
 
   206 				// STF: Connect to the local drive here.  This gives us the media changed flag, and
 
   207 				//		our own TBusLocalDrive object for use by the proxy drive and controller.
 
   210                 TMediaChangedStatus mediaChanged;
 
   211                 iMediaChangedStatusList.AppendL(mediaChanged);
 
   213                 TBool& mediaChangedRef = iMediaChangedStatusList[driveCount].iMediaChanged;
 
   214 				TInt err = iMediaChangedStatusList[driveCount].iLocalDrive.Connect(locDrvNum, mediaChangedRef);
 
   217 					iDriveMap.Append(static_cast <TDriveNumber>(i));
 
   224 	__PRINT1(_L("EnumerateMsDrives Out, %d MS drives found\n"), driveCount);
 
   228 TBool CMassStorageFileSystem::IsExtensionSupported() const
 
   235 Creates a TrapCleanup and ActiveScheduler and initializes the Mass Storage controller.
 
   236 Start the ActiveScheduler.
 
   238 @return Any of the standard Symbian error codes.
 
   240 TInt CMassStorageFileSystem::InitThread()
 
   244 	//Give the thread a name so we can kill it later
 
   245 	User::RenameThread(KMsThreadName);
 
   247 	CTrapCleanup* cleanup = CTrapCleanup::New();
 
   253 	TRAPD(err, InitThreadL());
 
   257 	__PRINT1(_L("CMassStorageFileSystem::InitThread Out, error=%d\n"), err);
 
   261 TInt CMassStorageFileSystem::InitThreadL()
 
   264 	RSemaphore gSemThreadReady;
 
   265 	gSemThreadReady.OpenGlobal(KMsFsysSemName);
 
   267 	// Determine which drives are available for Mass Storage.
 
   268 	// (this also creates a local TBusLocalDrive for use by the drive controller)
 
   269 	EnumerateMsDrivesL();
 
   271 	CActiveScheduler* sched = new (ELeave) CActiveScheduler;
 
   274 		gSemThreadReady.Signal();
 
   275 		User::Leave(KErrNoMemory);
 
   277 	CleanupStack::PushL(sched);
 
   278 	CActiveScheduler::Install(sched);
 
   280 	iMassStorageController = CUsbMassStorageController::NewL();
 
   281 	if (iMassStorageController == NULL)
 
   283 		gSemThreadReady.Signal();
 
   284 		User::Leave(KErrNoMemory);
 
   287 	__PRINT(_L("CMassStorageFileSystem::InitThread: Creating Mass Storage Controller\n"));
 
   288 	TRAPD(err, iMassStorageController->CreateL(iDriveMap));
 
   291 		gSemThreadReady.Signal();
 
   292 		CActiveScheduler::Install(NULL);
 
   296 	CleanupStack::Pop(sched);
 
   299 	gSemThreadReady.Signal();
 
   300 	gSemThreadReady.Close();
 
   301 	CActiveScheduler::Start();
 
   303 //========= stop thread ================
 
   304 	delete iMassStorageController;
 
   305 	iMassStorageController = NULL;
 
   307 	for (TInt i=0; i < iMediaChangedStatusList.Count(); i++)
 
   309         iMediaChangedStatusList[i].iLocalDrive.Disconnect();
 
   311 	iMediaChangedStatusList.Reset();
 
   319 Not supported in Mass Storage file system.
 
   323 CFileCB* CMassStorageFileSystem::NewFileL() const
 
   326 	User::Leave(KErrNotReady);
 
   331 Not supported in Mass Storage file system.
 
   335 CDirCB* CMassStorageFileSystem::NewDirL() const
 
   338 	User::Leave(KErrNotReady);
 
   343 Not supported in Mass Storage file system.
 
   347 CFormatCB* CMassStorageFileSystem::NewFormatL() const
 
   350 	User::Leave(KErrNotReady);
 
   355 Not supported in Mass Storage file system.
 
   357 @return KErrNotSupported
 
   359 TInt CMassStorageFileSystem::DefaultPath(TDes& /*aPath*/) const
 
   362 	return KErrNotSupported;
 
   366 Not supported in Mass Storage file system.
 
   368 @return KErrNotSupported
 
   370 TInt CMassStorageFileSystem::DriveList(TDriveList& /*aList*/) const
 
   373 	return KErrNotSupported;
 
   379 LOCAL_C TInt MsInitThreadFn(TAny* aPtr)
 
   381 	User::SetCritical(User::ESystemCritical);
 
   382 	((CMassStorageFileSystem*)aPtr)->InitThread();
 
   383 	//Rename the thread so we can create a new one with the same original name later
 
   384 	User::RenameThread(KMsDeadThreadName);
 
   389 Standard entry point for file systems.
 
   390 Creates a new file system object and starts a new thread for the Mass Storage controller.
 
   392 extern "C" EXPORT_C CFileSystem* CreateFileSystem()
 
   394 	__PRINT(_L("CMassStorageFileSystem::CreateFileSystem In\n"));
 
   395 	RSemaphore gSemThreadReady;
 
   396 	TInt err = gSemThreadReady.CreateGlobal(KMsFsysSemName, 0);
 
   399 		__PRINT1(_L("CMassStorageFileSystem::CreateFileSystem Out Semaphore Error %d\n"),err);
 
   403 	CFileSystem* msFsys = NULL;
 
   404 	TRAP(err,  msFsys = CMassStorageFileSystem::NewL());
 
   407 		__PRINT1(_L("CMassStorageFileSystem::CreateFileSystem Out MSFS Error %d\n"),err);
 
   408 		gSemThreadReady.Close();
 
   413 	__PRINT(_L("CMassStorageFileSystem::CreateFileSystem: Creating Mass Storage thread\n"));
 
   414 	err = msThread.Create(KMsThreadName, MsInitThreadFn, KDefaultStackSize, NULL, msFsys);
 
   417 		__PRINT1(_L("CMassStorageFileSystem::CreateFileSystem Out Thread Error %d\n"),err);
 
   418 		gSemThreadReady.Close();
 
   421 	((CMassStorageFileSystem*)msFsys)->iInstalled=ETrue;
 
   424 	msThread.Logon(((CMassStorageFileSystem*)msFsys)->iThreadStat);
 
   426 	gSemThreadReady.Wait();
 
   427 	gSemThreadReady.Close();
 
   430 	__PRINT(_L("CMassStorageFileSystem::CreateFileSystem Out Clean\n"));