os/kernelhwsrv/kerneltest/e32test/multimedia/d_mmcsc.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2005-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 // e32test\multimedia\d_mmcsc.cpp
    15 // The interface to a device driver which creates a shared chunk - for use with the multimedia driver tests.
    16 // This driver uses only generic kernel APIs and so need not be built from each variant.
    17 // 
    18 //
    19 
    20 /**
    21  @file
    22  @internalComponent
    23  @test
    24 */
    25 
    26 #include <kernel/kern_priv.h>
    27 #include "d_mmcsc.h"
    28 
    29 //#define __KTRACE_MMD(s) s;
    30 #define __KTRACE_MMD(s)
    31 
    32 class DMmCreateScFactory : public DLogicalDevice
    33 	{
    34 public:
    35 	DMmCreateScFactory();
    36 	virtual TInt Install();
    37 	virtual void GetCaps(TDes8 &aDes) const;
    38 	virtual TInt Create(DLogicalChannelBase*& aChannel);
    39 	};
    40 
    41 class DMmCreateSc : public DLogicalChannel
    42 	{
    43 public:
    44 	DMmCreateSc();
    45 	~DMmCreateSc();
    46 protected:
    47 	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
    48 	virtual void HandleMsg(TMessageBase* aMsg);
    49 	TInt DoControl(TInt aId, TAny* a1, TAny* a2);
    50 private:
    51 	TInt CreateChunk(TInt aNumBuffers,TInt aBufferSize);
    52 	void CloseChunk();
    53 	TInt MakeChunkHandle(DThread* aThread);
    54 private:
    55 	DThread* iClient;
    56 	TMmSharedChunkBufConfig iBufferConfig;
    57 	DChunk* iChunk;
    58 	};
    59 
    60 DECLARE_STANDARD_LDD()
    61 	{
    62 	return new DMmCreateScFactory;
    63 	}
    64 
    65 DMmCreateScFactory::DMmCreateScFactory()
    66 	{
    67 	// Set version number for this device
    68 	iVersion=TVersion(0,1,1);
    69 
    70 	// Indicate we do support units or a PDD
    71 	iParseMask=0;
    72 	}
    73 
    74 TInt DMmCreateScFactory::Install()
    75 	{
    76 	return(SetName(&KDevMmCScName));
    77 	}
    78 
    79 void DMmCreateScFactory::GetCaps(TDes8& aDes) const
    80 	{
    81 	// Create a capabilities object
    82 	TCapsMmCScV01 caps;
    83 	caps.iVersion=iVersion;
    84 
    85 	// Write it back to user memory
    86 	Kern::InfoCopy(aDes,(TUint8*)&caps,sizeof(caps));
    87 	}
    88 
    89 TInt DMmCreateScFactory::Create(DLogicalChannelBase*& aChannel)
    90 	{
    91 	aChannel=new DMmCreateSc;
    92 	if (!aChannel)
    93 		return(KErrNoMemory);
    94 	return(KErrNone);
    95 	}
    96 
    97 DMmCreateSc::DMmCreateSc()
    98 	{
    99 //	iChunk=NULL;
   100 	iClient=&Kern::CurrentThread();
   101 	((DObject*)iClient)->Open();
   102 	}
   103 
   104 DMmCreateSc::~DMmCreateSc()
   105 	{
   106 	CloseChunk();
   107 	Kern::SafeClose((DObject*&)iClient,NULL);
   108 	}
   109 
   110 TInt DMmCreateSc::DoCreate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& /*aVer*/)
   111 	{
   112 
   113 	__KTRACE_MMD(Kern::Printf("DMmCreateSc:DoCreate"));
   114 
   115 	SetDfcQ(Kern::DfcQue0());
   116 	iMsgQ.Receive();
   117 
   118 	TInt r;
   119 	r=CreateChunk(KMmCScNumBuffers,KMmCScBufSize);	// Contains two 150K buffers
   120 	return(r);
   121 	}
   122 
   123 void DMmCreateSc::HandleMsg(TMessageBase* aMsg)
   124 	{
   125 
   126 	TThreadMessage& m=*(TThreadMessage*)aMsg;
   127 	TInt id=m.iValue;
   128 	if (id==(TInt)ECloseMsg)
   129 		{
   130 		m.Complete(KErrNone, EFalse);
   131 		return;
   132 		}
   133 	else if (id==KMaxTInt)
   134 		{
   135 		m.Complete(KErrNone,ETrue);	// DoCancel
   136 		return;
   137 		}
   138 
   139 	if (id<0)
   140 		{
   141 		TRequestStatus* pS=(TRequestStatus*)m.Ptr0();	// DoRequest
   142 		Kern::RequestComplete(iClient,pS,KErrNotSupported);
   143 		m.Complete(KErrNone,ETrue);
   144 		}
   145 	else
   146 		{
   147 		TInt r=DoControl(id,m.Ptr0(),m.Ptr1());	// DoControl
   148 		m.Complete(r,ETrue);
   149 		}
   150 	}
   151 
   152 TInt DMmCreateSc::DoControl(TInt aId, TAny* a1, TAny* /*a2*/)
   153 	{
   154 
   155 	__KTRACE_MMD(Kern::Printf("DMmCreateSc:DoControl %x",aId));
   156 	TInt r=KErrNone;
   157 
   158 	switch (aId)
   159 		{
   160 		case RMmCreateSc::EGetChunkHandle:
   161 			{
   162 			r=MakeChunkHandle(iClient);
   163 			break;
   164 			}
   165 		case RMmCreateSc::EGetBufInfo:
   166 			{
   167 			TPtrC8 ptr((const TUint8*)&iBufferConfig,sizeof(iBufferConfig));
   168 			r=Kern::ThreadDesWrite(iClient,a1,ptr,0,KTruncateToMaxLength,NULL);
   169 			break;
   170 			}
   171 		default:
   172 			r=KErrNotSupported;
   173 		}
   174 	return(r);
   175 	}
   176 
   177 TInt DMmCreateSc::CreateChunk(TInt aNumBuffers,TInt aBufferSize)
   178 	{
   179 
   180 	TInt bufferSize=Kern::RoundToPageSize(aBufferSize);
   181 	TInt chunkSz=(aNumBuffers*bufferSize);
   182 	__KTRACE_MMD(Kern::Printf("DMmCreateSc:CreateChunk %d",chunkSz));
   183 
   184 	// Thread must be in critical section to create chunks
   185 	NKern::ThreadEnterCS();
   186 
   187 	// Create the shared chunk.
   188 	TChunkCreateInfo info;
   189 	info.iType = TChunkCreateInfo::ESharedKernelMultiple;
   190 	info.iMaxSize = chunkSz;
   191 #if !defined(__WINS__)
   192 	info.iMapAttr=EMapAttrFullyBlocking;	// No caching
   193 #else
   194 	info.iMapAttr=0;
   195 #endif
   196 	info.iOwnsMemory = ETrue; 				// Using RAM pages
   197 	info.iDestroyedDfc = NULL;				// No chunk destroy DFC
   198 
   199 	DChunk* chunk;
   200 	TLinAddr chunkKernelAddr;
   201 	TUint32 mapAttr;
   202 	TInt r = Kern::ChunkCreate(info,chunk,chunkKernelAddr,mapAttr);
   203 	if (r!=KErrNone)
   204 		{
   205 		NKern::ThreadLeaveCS();
   206 		return(r);
   207 		}
   208 
   209 	// Map the three buffers into the chunk - each containing	physically contiguous RAM pages.
   210 	TInt bufOffset=0;
   211 	TUint32 pa;
   212 	r=Kern::ChunkCommitContiguous(chunk,bufOffset,bufferSize,pa);
   213 	if (r==KErrNone)
   214 		{
   215 		bufOffset+=bufferSize;
   216 		r=Kern::ChunkCommitContiguous(chunk,bufOffset,bufferSize,pa);
   217 		}
   218 	if (r==KErrNone)
   219 		{
   220 		bufOffset+=bufferSize;
   221 		r=Kern::ChunkCommitContiguous(chunk,bufOffset,bufferSize,pa);
   222 		}
   223 	if (r!=KErrNone)
   224 		Kern::ChunkClose(chunk); // Commit failed - tidy-up.
   225 	else
   226 		iChunk=chunk;
   227 
   228 	NKern::ThreadLeaveCS();
   229 
   230 	// Update our buffer info. structure to reflect the chunk we have just created.
   231 	iBufferConfig.iNumBuffers=aNumBuffers;
   232 	iBufferConfig.iBufferSizeInBytes=aBufferSize;
   233 	iBufferConfig.iFlags|=KScFlagBufOffsetListInUse;
   234 	iBufferConfig.iSpec[0].iBufferOffset=0;
   235 	iBufferConfig.iSpec[0].iBufferId=0;
   236 	iBufferConfig.iSpec[1].iBufferOffset=bufferSize;
   237 	iBufferConfig.iSpec[1].iBufferId=1;
   238 
   239 	__KTRACE_MMD(Kern::Printf("ChunkCommit %d",r));
   240 	return(r);
   241 	}
   242 
   243 void DMmCreateSc::CloseChunk()
   244 	{
   245 
   246 	__KTRACE_MMD(Kern::Printf("DMmCreateSc:CloseChunk"));
   247 
   248 	// Thread must be in critical section to close a chunk
   249 	NKern::ThreadEnterCS();
   250 
   251 	// Close chunk
   252 	if (iChunk)
   253 		Kern::ChunkClose(iChunk);
   254 
   255 	// Can leave critical section now
   256 	NKern::ThreadLeaveCS();
   257 	}
   258 
   259 TInt DMmCreateSc::MakeChunkHandle(DThread* aThread)
   260 	{
   261 
   262 	TInt r;
   263 	// Thread must be in critical section to make a handle
   264 	NKern::ThreadEnterCS();
   265 	if (iChunk)
   266 		r=Kern::MakeHandleAndOpen(aThread,iChunk);
   267 	else
   268 		r=KErrNotFound;
   269 	NKern::ThreadLeaveCS();
   270 	__KTRACE_MMD(Kern::Printf("DMmCreateSc:MakeChunkHandle %x %d",aThread,r));
   271 	return(r);
   272 	}