os/kernelhwsrv/userlibandfileserver/fileserver/smassstorage/cmassstoragefilesystem.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// CMassStorageFileSystem implementation.
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
/**
sl@0
    19
 @file
sl@0
    20
 @internalTechnology
sl@0
    21
*/
sl@0
    22
sl@0
    23
#include <f32file.h>
sl@0
    24
#include "cmassstoragefilesystem.h"
sl@0
    25
#include "cmassstoragemountcb.h"
sl@0
    26
#include "massstoragedebug.h"
sl@0
    27
#include "massstorage.h"
sl@0
    28
sl@0
    29
_LIT(KMsFsyName, "MassStorageFileSystem");
sl@0
    30
_LIT(KMsThreadName, "MassStorageThread");
sl@0
    31
_LIT(KMsDeadThreadName, "MassStorageDeadThread");
sl@0
    32
_LIT(KMsFsysSemName, "MassStorageSemaphore");
sl@0
    33
LOCAL_D const TInt KMsFsyMajorVersionNumber=1;
sl@0
    34
LOCAL_D const TInt KMsFsyMinorVersionNumber=0;
sl@0
    35
sl@0
    36
CMassStorageFileSystem::CMassStorageFileSystem()
sl@0
    37
	{
sl@0
    38
	}
sl@0
    39
sl@0
    40
CMassStorageFileSystem::~CMassStorageFileSystem()
sl@0
    41
	{
sl@0
    42
	//Kill the controller thread if it exists
sl@0
    43
	delete iMassStorageController;
sl@0
    44
	delete iMediaChanged;
sl@0
    45
	RThread thread;
sl@0
    46
	TInt err = thread.Open(KMsThreadName);
sl@0
    47
	if (err == KErrNone)
sl@0
    48
		{
sl@0
    49
		thread.Kill(1); //Parameter is irrelevant
sl@0
    50
		}
sl@0
    51
	thread.Close();
sl@0
    52
	iMsDrives.Close();
sl@0
    53
	}
sl@0
    54
sl@0
    55
CMassStorageFileSystem* CMassStorageFileSystem::NewL()
sl@0
    56
	{
sl@0
    57
	CMassStorageFileSystem*  self = new (ELeave) CMassStorageFileSystem();
sl@0
    58
	CleanupStack::PushL(self);
sl@0
    59
	self->ConstructL();
sl@0
    60
	CleanupStack::Pop(self);
sl@0
    61
	return self;
sl@0
    62
	}
sl@0
    63
	
sl@0
    64
void CMassStorageFileSystem::ConstructL()
sl@0
    65
	{
sl@0
    66
	iMediaChanged = new(ELeave) CArrayFixFlat<TBool>(KMaxDrives);
sl@0
    67
	}
sl@0
    68
sl@0
    69
/**
sl@0
    70
Set the file system version and name
sl@0
    71
sl@0
    72
@return Any of the standard Symbian error codes.
sl@0
    73
*/
sl@0
    74
TInt CMassStorageFileSystem::Install()
sl@0
    75
	{
sl@0
    76
	__PRINT(_L("CMassStorageFileSystem::Install In\n"));
sl@0
    77
	iVersion=TVersion(KMsFsyMajorVersionNumber, KMsFsyMinorVersionNumber, KF32BuildVersionNumber);
sl@0
    78
	TInt err = SetName(&KMsFsyName);
sl@0
    79
	__PRINT(_L("CMassStorageFileSystem::Install Out\n"));
sl@0
    80
	return err;
sl@0
    81
	}
sl@0
    82
sl@0
    83
TInt CMassStorageFileSystem::Remove()
sl@0
    84
	{
sl@0
    85
	__PRINT(_L("CMassStorageFileSystem::Remove In\n"));
sl@0
    86
	TInt err = KErrNone;
sl@0
    87
	if (iInstalled)
sl@0
    88
		{
sl@0
    89
		// Try connecting to the server to send a shutdown message.
sl@0
    90
		// - If the class controller has a session in use, this will return KErrInUse
sl@0
    91
		RUsbMassStorage usbMs;
sl@0
    92
		err = usbMs.Connect();
sl@0
    93
		if(err == KErrNone)
sl@0
    94
			{
sl@0
    95
			err = usbMs.Shutdown();
sl@0
    96
			usbMs.Close();
sl@0
    97
sl@0
    98
			if(err == KErrNone)
sl@0
    99
				{
sl@0
   100
				User::WaitForRequest(iThreadStat);
sl@0
   101
				err = iThreadStat.Int();
sl@0
   102
				}
sl@0
   103
			else
sl@0
   104
				{
sl@0
   105
				__PRINT1(_L("CMassStorageFileSystem::Remove Shutdown Error %d\n"),err);
sl@0
   106
				}
sl@0
   107
			}
sl@0
   108
		else
sl@0
   109
			{
sl@0
   110
			__PRINT1(_L("CMassStorageFileSystem::Remove Connect Error %d\n"),err);
sl@0
   111
			}
sl@0
   112
		}
sl@0
   113
	__PRINT(_L("CMassStorageFileSystem::Remove Out\n"));
sl@0
   114
	return(err);
sl@0
   115
	}
sl@0
   116
sl@0
   117
/**
sl@0
   118
Creates a new Mass Storage mount object.
sl@0
   119
sl@0
   120
@return A new CMassStorageMountCB
sl@0
   121
@leave KErrNotReady if the Mass Storage controller is not running.
sl@0
   122
*/
sl@0
   123
CMountCB* CMassStorageFileSystem::NewMountL() const
sl@0
   124
	{
sl@0
   125
	if (!iRunning)
sl@0
   126
		{
sl@0
   127
		User::Leave(KErrNotReady);
sl@0
   128
		}
sl@0
   129
	return CMassStorageMountCB::NewL(iMsDrives);
sl@0
   130
	}
sl@0
   131
sl@0
   132
/**
sl@0
   133
Sets the media attributes and type in the aInfo parameter to those of the specified drive.
sl@0
   134
sl@0
   135
@param anInfo TDriveInfo object to store the drive information.
sl@0
   136
@param aDriveNumber The number of the drive to get the information from.
sl@0
   137
*/
sl@0
   138
void CMassStorageFileSystem::DriveInfo(TDriveInfo& aInfo, TInt aDriveNumber) const
sl@0
   139
	{
sl@0
   140
	__PRINT(_L("CMassStorageFileSystem::DriveInfo In\n"));
sl@0
   141
	TLocalDriveCapsV2Buf caps;
sl@0
   142
	if (!IsValidLocalDriveMapping(aDriveNumber))
sl@0
   143
		{
sl@0
   144
		return;
sl@0
   145
		}
sl@0
   146
	(void)GetLocalDrive(aDriveNumber).Caps(caps);   
sl@0
   147
    // error ignored as Caps always returns  valid Media and Drive attributes
sl@0
   148
	aInfo.iMediaAtt=caps().iMediaAtt;
sl@0
   149
    aInfo.iType = ::EMediaNotPresent;  // Media is not available to the file system
sl@0
   150
	aInfo.iDriveAtt=caps().iDriveAtt;
sl@0
   151
	__PRINT(_L("CMassStorageFileSystem::DriveInfo Out\n"));
sl@0
   152
	}
sl@0
   153
sl@0
   154
/**
sl@0
   155
Returns a reference to the Mass Storage controller.
sl@0
   156
sl@0
   157
@return Reference to the Mass Storage controller.
sl@0
   158
*/
sl@0
   159
CUsbMassStorageController& CMassStorageFileSystem::Controller()
sl@0
   160
	{
sl@0
   161
	return *iMassStorageController;
sl@0
   162
	}
sl@0
   163
sl@0
   164
/**
sl@0
   165
Fill iMsDrives with a mapping of lun->drive number for supported mass storage drives
sl@0
   166
sl@0
   167
*/
sl@0
   168
TInt CMassStorageFileSystem::EnumerateMsDrivesL()
sl@0
   169
	{
sl@0
   170
	__PRINT(_L("CMassStorageFileSystem::EnumerateMsDrives In\n"));
sl@0
   171
	iMsDrives.Reset();
sl@0
   172
	TInt driveCount = 0;
sl@0
   173
sl@0
   174
	TLocalDriveCapsV2Buf caps;
sl@0
   175
	for (TInt i = EDriveC; i < KMaxDrives; i++)
sl@0
   176
		{
sl@0
   177
		caps.FillZ();
sl@0
   178
		
sl@0
   179
		if (IsValidLocalDriveMapping(i))
sl@0
   180
			{
sl@0
   181
			TInt err = GetLocalDrive(i).Caps(caps);
sl@0
   182
			TInt locDrvNum = DriveNumberToLocalDriveNumber(i);
sl@0
   183
			__PRINT2(_L("Caps: err=%d, att=%d\n"), err, caps().iDriveAtt);
sl@0
   184
			
sl@0
   185
			TBool isRemovable = err==KErrNotReady || (caps().iDriveAtt & KDriveAttRemovable);
sl@0
   186
			__PRINT2(_L("EnumerateMsDrives: Drive %c: is %sremovable\n"),
sl@0
   187
							'A'+i-EDriveA, 
sl@0
   188
							isRemovable?_S(""):_S("NOT "));
sl@0
   189
							
sl@0
   190
			if (isRemovable)
sl@0
   191
				{
sl@0
   192
				//
sl@0
   193
				// STF: Connect to the local drive here.  This gives us the media changed flag, and
sl@0
   194
				//		our own TBusLocalDrive object for use by the proxy drive and controller.
sl@0
   195
				//
sl@0
   196
				TBool& mediaChanged = (*iMediaChanged).ExtendL();
sl@0
   197
				mediaChanged = EFalse;
sl@0
   198
				TBusLocalDrive* localDrive = new(ELeave) TBusLocalDrive;
sl@0
   199
				iLocalDriveForMediaFlag.Append(*localDrive);
sl@0
   200
				
sl@0
   201
				TInt err=iLocalDriveForMediaFlag[driveCount].Connect(locDrvNum, mediaChanged);
sl@0
   202
				if(err == KErrNone)
sl@0
   203
					{
sl@0
   204
					iMsDrives.Append(i);
sl@0
   205
					}
sl@0
   206
				driveCount++;
sl@0
   207
				}
sl@0
   208
			}
sl@0
   209
		}
sl@0
   210
sl@0
   211
	__PRINT1(_L("CMassStorageFileSystem::EnumerateMsDrives Out, %d MS drives found\n"), driveCount);
sl@0
   212
	return driveCount;
sl@0
   213
	}
sl@0
   214
sl@0
   215
TBool CMassStorageFileSystem::IsExtensionSupported() const
sl@0
   216
	{
sl@0
   217
	return ETrue;
sl@0
   218
	}
sl@0
   219
sl@0
   220
/**
sl@0
   221
Creates a TrapCleanup and ActiveScheduler and initializes the Mass Storage controller.
sl@0
   222
Start the ActiveScheduler.
sl@0
   223
sl@0
   224
@return Any of the standard Symbian error codes.
sl@0
   225
*/
sl@0
   226
TInt CMassStorageFileSystem::InitThread()
sl@0
   227
	{
sl@0
   228
	__PRINT(_L("CMassStorageFileSystem::InitThread In\n"));
sl@0
   229
	
sl@0
   230
	//Give the thread a name so we can kill it later
sl@0
   231
	User::RenameThread(KMsThreadName);
sl@0
   232
		
sl@0
   233
	CTrapCleanup* cleanup = CTrapCleanup::New();
sl@0
   234
	if (cleanup == NULL)
sl@0
   235
		{
sl@0
   236
		return KErrNoMemory;
sl@0
   237
		}
sl@0
   238
sl@0
   239
	TRAPD(err, InitThreadL());
sl@0
   240
	
sl@0
   241
	delete cleanup;
sl@0
   242
	
sl@0
   243
	__PRINT1(_L("CMassStorageFileSystem::InitThread Out, error=%d\n"), err);
sl@0
   244
	return err;
sl@0
   245
	}
sl@0
   246
sl@0
   247
TInt CMassStorageFileSystem::InitThreadL()
sl@0
   248
	{
sl@0
   249
	__PRINT(_L("CMassStorageFileSystem::InitThreadL In\n"));
sl@0
   250
	
sl@0
   251
	RSemaphore gSemThreadReady;
sl@0
   252
sl@0
   253
	TInt ret = gSemThreadReady.OpenGlobal(KMsFsysSemName);
sl@0
   254
sl@0
   255
	if (ret != KErrNone && ret != KErrAlreadyExists)
sl@0
   256
		{
sl@0
   257
		User::Leave(ret);
sl@0
   258
		}
sl@0
   259
sl@0
   260
	// Determine which drives are available for Mass Storage.
sl@0
   261
	// (this also creates a local TBusLocalDrive for use by the drive controller)
sl@0
   262
	EnumerateMsDrivesL();
sl@0
   263
sl@0
   264
	CActiveScheduler* sched = new CActiveScheduler;
sl@0
   265
	if (sched == NULL)
sl@0
   266
		{
sl@0
   267
		gSemThreadReady.Signal();
sl@0
   268
		User::Leave(KErrNoMemory);
sl@0
   269
		}
sl@0
   270
	CleanupStack::PushL(sched);
sl@0
   271
	CActiveScheduler::Install(sched);
sl@0
   272
sl@0
   273
	iMassStorageController = new CUsbMassStorageController;
sl@0
   274
	if (iMassStorageController == NULL)
sl@0
   275
		{
sl@0
   276
		gSemThreadReady.Signal();
sl@0
   277
		User::Leave(KErrNoMemory);
sl@0
   278
		}
sl@0
   279
	
sl@0
   280
	__PRINT(_L("CMassStorageFileSystem::InitThread: Creating Mass Storage Controller\n"));
sl@0
   281
	TRAPD(err, iMassStorageController->CreateL(iMsDrives));
sl@0
   282
	if (err != KErrNone)
sl@0
   283
		{
sl@0
   284
		gSemThreadReady.Signal();
sl@0
   285
		CActiveScheduler::Install(NULL);
sl@0
   286
		User::Leave(err);
sl@0
   287
		}
sl@0
   288
	
sl@0
   289
	CleanupStack::Pop(sched);
sl@0
   290
sl@0
   291
	iRunning = ETrue;
sl@0
   292
	gSemThreadReady.Signal();
sl@0
   293
	gSemThreadReady.Close();
sl@0
   294
	CActiveScheduler::Start();
sl@0
   295
sl@0
   296
//========= stop thread ================
sl@0
   297
	delete iMassStorageController;
sl@0
   298
	iMassStorageController = NULL;
sl@0
   299
	TInt i=0;
sl@0
   300
	for (;i<iLocalDriveForMediaFlag.Count();i++)
sl@0
   301
		{
sl@0
   302
		iLocalDriveForMediaFlag[i].Disconnect();
sl@0
   303
		}
sl@0
   304
	iLocalDriveForMediaFlag.Reset();
sl@0
   305
	(*iMediaChanged).Reset();	
sl@0
   306
	delete sched;
sl@0
   307
	iRunning = EFalse;
sl@0
   308
	
sl@0
   309
	__PRINT(_L("CMassStorageFileSystem::InitThread Out\n"));
sl@0
   310
	return KErrNone;
sl@0
   311
	}
sl@0
   312
	
sl@0
   313
/**
sl@0
   314
Not supported in Mass Storage file system.
sl@0
   315
sl@0
   316
@leave KErrNotReady
sl@0
   317
*/
sl@0
   318
CFileCB* CMassStorageFileSystem::NewFileL() const
sl@0
   319
	{
sl@0
   320
	__PRINT(_L("CMassStorageFileSystem::NewFileL In\n"));
sl@0
   321
	User::Leave(KErrNotReady);
sl@0
   322
	return NULL;
sl@0
   323
	}
sl@0
   324
sl@0
   325
/**
sl@0
   326
Not supported in Mass Storage file system.
sl@0
   327
sl@0
   328
@leave KErrNotReady
sl@0
   329
*/
sl@0
   330
CDirCB* CMassStorageFileSystem::NewDirL() const
sl@0
   331
	{
sl@0
   332
	User::Leave(KErrNotReady);
sl@0
   333
	return NULL;
sl@0
   334
	}
sl@0
   335
sl@0
   336
/**
sl@0
   337
Not supported in Mass Storage file system.
sl@0
   338
sl@0
   339
@leave KErrNotReady
sl@0
   340
*/
sl@0
   341
CFormatCB* CMassStorageFileSystem::NewFormatL() const
sl@0
   342
	{
sl@0
   343
	User::Leave(KErrNotReady);
sl@0
   344
	return NULL;
sl@0
   345
	}
sl@0
   346
sl@0
   347
/**
sl@0
   348
Not supported in Mass Storage file system.
sl@0
   349
sl@0
   350
@return KErrNotSupported
sl@0
   351
*/
sl@0
   352
TInt CMassStorageFileSystem::DefaultPath(TDes& /*aPath*/) const
sl@0
   353
	{
sl@0
   354
	return KErrNotSupported;
sl@0
   355
	}
sl@0
   356
sl@0
   357
/**
sl@0
   358
Not supported in Mass Storage file system.
sl@0
   359
sl@0
   360
@return KErrNotSupported
sl@0
   361
*/
sl@0
   362
TInt CMassStorageFileSystem::DriveList(TDriveList& /*aList*/) const
sl@0
   363
	{
sl@0
   364
	return KErrNotSupported;
sl@0
   365
	}
sl@0
   366
sl@0
   367
/**
sl@0
   368
Thread entry point.
sl@0
   369
*/
sl@0
   370
LOCAL_C TInt MsInitThreadFn(TAny* aPtr)
sl@0
   371
	{
sl@0
   372
	User::SetCritical(User::ESystemCritical);
sl@0
   373
	((CMassStorageFileSystem*)aPtr)->InitThread();
sl@0
   374
	//Rename the thread so we can create a new one with the same original name later
sl@0
   375
	User::RenameThread(KMsDeadThreadName);
sl@0
   376
	return KErrNone;
sl@0
   377
	}
sl@0
   378
sl@0
   379
/**
sl@0
   380
Standard entry point for file systems.
sl@0
   381
Creates a new file system object and starts a new thread for the Mass Storage controller.
sl@0
   382
*/
sl@0
   383
extern "C" EXPORT_C CFileSystem* CreateFileSystem()
sl@0
   384
	{
sl@0
   385
	__PRINT(_L("CMassStorageFileSystem::CreateFileSystem In\n"));
sl@0
   386
	RSemaphore gSemThreadReady;
sl@0
   387
	TInt err = gSemThreadReady.CreateGlobal(KMsFsysSemName, 0);
sl@0
   388
	if (err != KErrNone)
sl@0
   389
		{
sl@0
   390
		__PRINT1(_L("CMassStorageFileSystem::CreateFileSystem Out Semaphore Error %d\n"),err);
sl@0
   391
		return NULL;
sl@0
   392
		}
sl@0
   393
sl@0
   394
	CFileSystem* msFsys = NULL;
sl@0
   395
	TRAP(err,  msFsys = CMassStorageFileSystem::NewL());
sl@0
   396
	if (err != KErrNone)
sl@0
   397
		{
sl@0
   398
		__PRINT1(_L("CMassStorageFileSystem::CreateFileSystem Out MSFS Error %d\n"),err);
sl@0
   399
		gSemThreadReady.Close();
sl@0
   400
		return NULL;
sl@0
   401
		}
sl@0
   402
sl@0
   403
	RThread msThread;
sl@0
   404
	__PRINT(_L("CMassStorageFileSystem::CreateFileSystem: Creating Mass Storage thread\n"));
sl@0
   405
	err = msThread.Create(KMsThreadName, MsInitThreadFn, KDefaultStackSize, NULL, msFsys);
sl@0
   406
	if (err != KErrNone)
sl@0
   407
		{
sl@0
   408
		__PRINT1(_L("CMassStorageFileSystem::CreateFileSystem Out Thread Error %d\n"),err);
sl@0
   409
		gSemThreadReady.Close();
sl@0
   410
		return msFsys;
sl@0
   411
		}
sl@0
   412
	((CMassStorageFileSystem*)msFsys)->iInstalled=ETrue;
sl@0
   413
sl@0
   414
sl@0
   415
	msThread.Logon(((CMassStorageFileSystem*)msFsys)->iThreadStat);
sl@0
   416
	msThread.Resume();
sl@0
   417
	gSemThreadReady.Wait();
sl@0
   418
	gSemThreadReady.Close();
sl@0
   419
	msThread.Close();
sl@0
   420
	
sl@0
   421
	__PRINT(_L("CMassStorageFileSystem::CreateFileSystem Out Clean\n"));
sl@0
   422
	
sl@0
   423
	return msFsys;
sl@0
   424
	}
sl@0
   425