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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
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.
26 #include <kernel/kern_priv.h>
29 //#define __KTRACE_MMD(s) s;
30 #define __KTRACE_MMD(s)
32 class DMmCreateScFactory : public DLogicalDevice
36 virtual TInt Install();
37 virtual void GetCaps(TDes8 &aDes) const;
38 virtual TInt Create(DLogicalChannelBase*& aChannel);
41 class DMmCreateSc : public DLogicalChannel
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);
51 TInt CreateChunk(TInt aNumBuffers,TInt aBufferSize);
53 TInt MakeChunkHandle(DThread* aThread);
56 TMmSharedChunkBufConfig iBufferConfig;
60 DECLARE_STANDARD_LDD()
62 return new DMmCreateScFactory;
65 DMmCreateScFactory::DMmCreateScFactory()
67 // Set version number for this device
68 iVersion=TVersion(0,1,1);
70 // Indicate we do support units or a PDD
74 TInt DMmCreateScFactory::Install()
76 return(SetName(&KDevMmCScName));
79 void DMmCreateScFactory::GetCaps(TDes8& aDes) const
81 // Create a capabilities object
83 caps.iVersion=iVersion;
85 // Write it back to user memory
86 Kern::InfoCopy(aDes,(TUint8*)&caps,sizeof(caps));
89 TInt DMmCreateScFactory::Create(DLogicalChannelBase*& aChannel)
91 aChannel=new DMmCreateSc;
97 DMmCreateSc::DMmCreateSc()
100 iClient=&Kern::CurrentThread();
101 ((DObject*)iClient)->Open();
104 DMmCreateSc::~DMmCreateSc()
107 Kern::SafeClose((DObject*&)iClient,NULL);
110 TInt DMmCreateSc::DoCreate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& /*aVer*/)
113 __KTRACE_MMD(Kern::Printf("DMmCreateSc:DoCreate"));
115 SetDfcQ(Kern::DfcQue0());
119 r=CreateChunk(KMmCScNumBuffers,KMmCScBufSize); // Contains two 150K buffers
123 void DMmCreateSc::HandleMsg(TMessageBase* aMsg)
126 TThreadMessage& m=*(TThreadMessage*)aMsg;
128 if (id==(TInt)ECloseMsg)
130 m.Complete(KErrNone, EFalse);
133 else if (id==KMaxTInt)
135 m.Complete(KErrNone,ETrue); // DoCancel
141 TRequestStatus* pS=(TRequestStatus*)m.Ptr0(); // DoRequest
142 Kern::RequestComplete(iClient,pS,KErrNotSupported);
143 m.Complete(KErrNone,ETrue);
147 TInt r=DoControl(id,m.Ptr0(),m.Ptr1()); // DoControl
152 TInt DMmCreateSc::DoControl(TInt aId, TAny* a1, TAny* /*a2*/)
155 __KTRACE_MMD(Kern::Printf("DMmCreateSc:DoControl %x",aId));
160 case RMmCreateSc::EGetChunkHandle:
162 r=MakeChunkHandle(iClient);
165 case RMmCreateSc::EGetBufInfo:
167 TPtrC8 ptr((const TUint8*)&iBufferConfig,sizeof(iBufferConfig));
168 r=Kern::ThreadDesWrite(iClient,a1,ptr,0,KTruncateToMaxLength,NULL);
177 TInt DMmCreateSc::CreateChunk(TInt aNumBuffers,TInt aBufferSize)
180 TInt bufferSize=Kern::RoundToPageSize(aBufferSize);
181 TInt chunkSz=(aNumBuffers*bufferSize);
182 __KTRACE_MMD(Kern::Printf("DMmCreateSc:CreateChunk %d",chunkSz));
184 // Thread must be in critical section to create chunks
185 NKern::ThreadEnterCS();
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
196 info.iOwnsMemory = ETrue; // Using RAM pages
197 info.iDestroyedDfc = NULL; // No chunk destroy DFC
200 TLinAddr chunkKernelAddr;
202 TInt r = Kern::ChunkCreate(info,chunk,chunkKernelAddr,mapAttr);
205 NKern::ThreadLeaveCS();
209 // Map the three buffers into the chunk - each containing physically contiguous RAM pages.
212 r=Kern::ChunkCommitContiguous(chunk,bufOffset,bufferSize,pa);
215 bufOffset+=bufferSize;
216 r=Kern::ChunkCommitContiguous(chunk,bufOffset,bufferSize,pa);
220 bufOffset+=bufferSize;
221 r=Kern::ChunkCommitContiguous(chunk,bufOffset,bufferSize,pa);
224 Kern::ChunkClose(chunk); // Commit failed - tidy-up.
228 NKern::ThreadLeaveCS();
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;
239 __KTRACE_MMD(Kern::Printf("ChunkCommit %d",r));
243 void DMmCreateSc::CloseChunk()
246 __KTRACE_MMD(Kern::Printf("DMmCreateSc:CloseChunk"));
248 // Thread must be in critical section to close a chunk
249 NKern::ThreadEnterCS();
253 Kern::ChunkClose(iChunk);
255 // Can leave critical section now
256 NKern::ThreadLeaveCS();
259 TInt DMmCreateSc::MakeChunkHandle(DThread* aThread)
263 // Thread must be in critical section to make a handle
264 NKern::ThreadEnterCS();
266 r=Kern::MakeHandleAndOpen(aThread,iChunk);
269 NKern::ThreadLeaveCS();
270 __KTRACE_MMD(Kern::Printf("DMmCreateSc:MakeChunkHandle %x %d",aThread,r));