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