os/kernelhwsrv/kernel/eka/drivers/medint/iram.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1996-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 // e32\drivers\medint\iram.cpp
    15 // 
    16 //
    17 
    18 #include "locmedia.h"
    19 #include "platform.h"
    20 #include "variantmediadef.h"
    21 
    22 _LIT(KPddName, "Media.IRam");
    23 
    24 class DPhysicalDeviceMediaIRam : public DPhysicalDevice
    25 	{
    26 public:
    27 	DPhysicalDeviceMediaIRam();
    28 	virtual TInt Install();
    29 	virtual void GetCaps(TDes8& aDes) const;
    30 	virtual TInt Create(DBase*& aChannel, TInt aMediaId, const TDesC8* anInfo, const TVersion& aVer);
    31 	virtual TInt Validate(TInt aDeviceType, const TDesC8* anInfo, const TVersion& aVer);
    32 	virtual TInt Info(TInt aFunction, TAny* a1);
    33 	};
    34 								
    35 class DMediaDriverIRam : public DMediaDriver
    36 	{
    37 public:
    38 	DMediaDriverIRam(TInt aMediaId);
    39 public:
    40 	// replacing pure virtual
    41 	virtual TInt Request(TLocDrvRequest& aRequest);
    42 	virtual void Disconnect(DLocalDrive* aLocalDrive, TThreadMessage* aMsg);
    43 	virtual TInt PartitionInfo(TPartitionInfo &anInfo);
    44 	virtual void NotifyPowerDown();
    45 	virtual void NotifyEmergencyPowerDown();
    46 public:
    47 	TInt DoCreate(TInt aMediaId);
    48 	TInt Caps(TLocDrvRequest& aRequest);
    49 	TInt Read(TLocDrvRequest& aRequest);
    50 	TInt Write(TLocDrvRequest& aRequest);
    51 	TInt Format(TLocDrvRequest& aRequest);
    52 	TInt Enlarge(TLocDrvRequest& aRequest);
    53 	TInt Reduce(TLocDrvRequest& aRequest);
    54 	};
    55 
    56 DPhysicalDeviceMediaIRam::DPhysicalDeviceMediaIRam()
    57 //
    58 // Constructor
    59 //
    60 	{
    61 	iUnitsMask=0x1;
    62 	iVersion=TVersion(KMediaDriverInterfaceMajorVersion,KMediaDriverInterfaceMinorVersion,KMediaDriverInterfaceBuildVersion);
    63 	}
    64 
    65 TInt DPhysicalDeviceMediaIRam::Install()
    66 //
    67 // Install the Internal Ram Media PDD.
    68 //
    69 	{
    70 
    71 	return SetName(&KPddName);
    72 	}
    73 
    74 void DPhysicalDeviceMediaIRam::GetCaps(TDes8& /*aDes*/) const
    75 //
    76 // Return the media drivers capabilities.
    77 //
    78 	{
    79 	}
    80 
    81 TInt DPhysicalDeviceMediaIRam::Create(DBase*& aChannel, TInt aMediaId, const TDesC8* /* anInfo */,const TVersion &aVer)
    82 //
    83 // Create an Internal Ram media driver.
    84 //
    85 	{
    86 	if (!Kern::QueryVersionSupported(iVersion,aVer))
    87 		return KErrNotSupported;
    88 	TInt r=KErrNoMemory;
    89 	DMediaDriverIRam* pD=new DMediaDriverIRam(aMediaId);
    90 	aChannel=pD;
    91 	if (pD)
    92 		r=pD->DoCreate(aMediaId);
    93 	TInternalRamDrive::Unlock();
    94 	return r;
    95 	}
    96 
    97 TInt DPhysicalDeviceMediaIRam::Validate(TInt aDeviceType, const TDesC8* /*anInfo*/, const TVersion& aVer)
    98 	{
    99 	if (!Kern::QueryVersionSupported(iVersion,aVer))
   100 		return KErrNotSupported;
   101 	if (aDeviceType!=MEDIA_DEVICE_IRAM)
   102 		return KErrNotSupported;
   103 	return KErrNone;
   104 	}
   105 
   106 TInt DPhysicalDeviceMediaIRam::Info(TInt aFunction, TAny*)
   107 //
   108 // Return the priority of this media driver
   109 //
   110 	{
   111 	if (aFunction==EPriority)
   112 		return KMediaDriverPriorityNormal;
   113 	return KErrNotSupported;
   114 	}
   115 
   116 DMediaDriverIRam::DMediaDriverIRam(TInt aMediaId)
   117 //
   118 // Constructor.
   119 //
   120 	:	DMediaDriver(aMediaId)
   121 	{}
   122 
   123 TInt DMediaDriverIRam::DoCreate(TInt /*aMediaId*/)
   124 //
   125 // Create the media driver.
   126 //
   127 	{
   128 	
   129 	TInt size=TInternalRamDrive::Size();
   130 	if (size<0)
   131 		return KErrGeneral;		// no RAM drive!
   132 	SetTotalSizeInBytes(size);
   133 	return(KErrNone);
   134 	}
   135 
   136 TInt DMediaDriverIRam::Request(TLocDrvRequest& m)
   137 	{
   138 	TInt request=m.Id();
   139 	__KTRACE_OPT(KLOCDRV,Kern::Printf(">DMediaDriverIRam::Request %d",request));
   140 	TInt r=KErrNotSupported;
   141 	NKern::ThreadEnterCS();
   142 	TInternalRamDrive::Wait();
   143 	switch (request)
   144 		{
   145 		case DLocalDrive::ECaps:
   146 			r=Caps(m);
   147 			break;
   148 		case DLocalDrive::ERead:
   149 			r=Read(m);
   150 			break;
   151 		case DLocalDrive::EWrite:
   152 			r=Write(m);
   153 			break;
   154 		case DLocalDrive::EFormat:
   155 			r=Format(m);
   156 			break;
   157 		case DLocalDrive::EEnlarge:
   158 			r=Enlarge(m);
   159 			break;
   160 		case DLocalDrive::EReduce:
   161 			r=Reduce(m);
   162 			break;
   163 		default:
   164 			r=KErrNotSupported;
   165 			break;
   166 		}
   167 	TInternalRamDrive::Signal();
   168 	NKern::ThreadLeaveCS();
   169 	__KTRACE_OPT(KLOCDRV,Kern::Printf("<DMediaDriverIRam::Request %d",r));
   170 	return r;
   171 	}
   172 
   173 void DMediaDriverIRam::Disconnect(DLocalDrive* /*aLocalDrive*/, TThreadMessage*)
   174 	{
   175 	// no action required
   176 	}
   177 
   178 void DMediaDriverIRam::NotifyPowerDown()
   179 	{
   180 	// no action required
   181 	}
   182 
   183 void DMediaDriverIRam::NotifyEmergencyPowerDown()
   184 	{
   185 	// no action required
   186 	}
   187 
   188 TInt DMediaDriverIRam::Caps(TLocDrvRequest& m)
   189 	{
   190 	TLocalDriveCapsV6& caps=*(TLocalDriveCapsV6*)m.RemoteDes();
   191 	caps.iType=EMediaRam;
   192 	caps.iConnectionBusType=EConnectionBusInternal;
   193 	caps.iDriveAtt=KDriveAttLocal|KDriveAttInternal;
   194 	caps.iMediaAtt=KMediaAttVariableSize|KMediaAttFormattable;
   195     caps.iBaseAddress=(TUint8*)TInternalRamDrive::Base();
   196 	caps.iFileSystemId=KDriveFileSysFAT;
   197 	caps.iPartitionType=KPartitionTypeFAT16;
   198 	caps.iSize=m.Drive()->iPartitionLen;
   199 	caps.iHiddenSectors=0;
   200 	caps.iEraseBlockSize=TInternalRamDrive::MaxSize();	// overload for RAM drive to avoid
   201 														// F32 depending on memory model
   202 	caps.iBlockSize=1;
   203 	return KErrNone;									
   204 	}
   205 
   206 TInt DMediaDriverIRam::Read(TLocDrvRequest& m)
   207 	{
   208 	Int64 pos=m.Pos();
   209 	Int64 length=m.Length();
   210 	if (length<0 || pos<0 || (pos+length)>KMaxTInt)
   211 		return KErrGeneral;
   212 	TInt p=(TInt)pos;
   213 	TInt l=(TInt)length;
   214 	if (p+l>TInternalRamDrive::Size())
   215 		return KErrGeneral;
   216 	TPtrC8 des((TUint8*)TInternalRamDrive::Base()+p,l);
   217 	TInt r=m.WriteRemote(&des,0);
   218 	return r;
   219 	}
   220 
   221 TInt DMediaDriverIRam::Write(TLocDrvRequest& m)
   222 	{
   223 	Int64 pos=m.Pos();
   224 	Int64 length=m.Length();
   225 	if (length<0 || pos<0 || (pos+length)>KMaxTInt)
   226 		return KErrGeneral;
   227 	TInt p=(TInt)pos;
   228 	TInt l=(TInt)length;
   229 	if (p+l>TInternalRamDrive::Size())
   230 		return KErrGeneral;
   231 	TPtr8 des((TUint8*)TInternalRamDrive::Base()+p,l,l);
   232 	TInt r=m.ReadRemote(&des,0);
   233 	return r;
   234 	}
   235 
   236 TInt DMediaDriverIRam::Format(TLocDrvRequest& m)
   237 	{
   238 	Int64 pos=m.Pos();
   239 	Int64 length=m.Length();
   240 	if (length<0 || pos<0 || (pos+length)>KMaxTInt)
   241 		return KErrGeneral;
   242 	TInt p=(TInt)pos;
   243 	TInt l=(TInt)length;
   244 	if (p+l>TInternalRamDrive::Size())
   245 		return KErrGeneral;
   246 	memclr((TUint8*)TInternalRamDrive::Base()+p, l);
   247 	return KErrNone;
   248 	}
   249 
   250 TInt DMediaDriverIRam::Enlarge(TLocDrvRequest& m)
   251 //
   252 // Enlarge the drive
   253 //
   254 	{
   255 	TInt length=(TInt)m.Length();
   256 	__KTRACE_OPT(KLOCDRV,Kern::Printf(">IRam::Enlarge (%d)",length));
   257  
   258 	Int64 newLen=TotalSizeInBytes();
   259 	newLen+=length;
   260 	if (newLen>KMaxTInt)
   261 		return(KErrGeneral);
   262 
   263 	TInt r=TInternalRamDrive::Adjust((TInt)newLen);
   264 	if (r==KErrNone)
   265 		{
   266 		SetTotalSizeInBytes(newLen,m.Drive());
   267 		__KTRACE_OPT(KLOCDRV,Kern::Printf("Enlarge-Success (S:0x%lx RS:%d)",TotalSizeInBytes(),TInternalRamDrive::Size()));
   268 		}
   269 	else if (r==KErrNoMemory)
   270 		r=KErrDiskFull;
   271 	return r;
   272 	}
   273 
   274 TInt DMediaDriverIRam::Reduce(TLocDrvRequest& m)
   275 //
   276 // Reduce in size the drive
   277 //
   278 	{
   279 	Int64 pos=m.Pos();
   280 	TInt length=(TInt)m.Length();
   281 	if (pos>KMaxTInt)
   282 		return(KErrGeneral);
   283 
   284 	__KTRACE_OPT(KLOCDRV,Kern::Printf(">IRam::ReduceSize (%d@%d)",length,(TInt)pos));
   285 
   286 	TUint8 *Trg=(TUint8*)TInternalRamDrive::Base()+(TInt)pos;
   287 	TInt len=TInternalRamDrive::Size()-((TInt)pos+length);
   288 	memmove((TAny*)Trg,(TAny*)(Trg+length),len);
   289 	len=TInternalRamDrive::Size()-length;
   290 	TInt r=TInternalRamDrive::Adjust(len);
   291 	if (r==KErrNone)
   292 		{
   293 		SetTotalSizeInBytes(len,m.Drive());
   294 		__KTRACE_OPT(KLOCDRV,Kern::Printf("ReduceSize-Success (S:0x%lx RS:%d)",TotalSizeInBytes(),TInternalRamDrive::Size()));
   295 		}
   296 	return r;
   297 	}
   298 
   299 TInt DMediaDriverIRam::PartitionInfo(TPartitionInfo& anInfo)
   300 //
   301 // Return partition information on the media.
   302 //
   303 	{
   304 	
   305 	anInfo.iPartitionCount=1;
   306 	anInfo.iEntry[0].iPartitionBaseAddr=0;
   307 	anInfo.iEntry[0].iPartitionLen=anInfo.iMediaSizeInBytes=TotalSizeInBytes();
   308 	anInfo.iEntry[0].iPartitionType=KPartitionTypeFAT16;
   309 	return KErrNone;
   310 	}
   311 
   312 DECLARE_EXTENSION_PDD()
   313 	{
   314 	return new DPhysicalDeviceMediaIRam;
   315 	}
   316 
   317 static const TInt IRamDriveNumbers[IRAM_DRIVECOUNT]={IRAM_DRIVELIST};	
   318 _LIT(KIRamDriveName,IRAM_DRIVENAME);
   319 
   320 DECLARE_STANDARD_EXTENSION()
   321 	{
   322 	__KTRACE_OPT(KBOOT,Kern::Printf("Registering IRAM drive"));
   323 	TInt r=KErrNoMemory;
   324 	DPrimaryMediaBase* pM=new DPrimaryMediaBase;
   325 	if (pM)
   326 		{
   327 		r=LocDrv::RegisterMediaDevice(MEDIA_DEVICE_IRAM,IRAM_DRIVECOUNT,&IRamDriveNumbers[0],pM,IRAM_NUMMEDIA,KIRamDriveName);
   328 		}
   329 	__KTRACE_OPT(KBOOT,Kern::Printf("Registering IRAM drive - return %d",r));
   330 	return r;
   331 	}
   332 
   333