os/kernelhwsrv/kernel/eka/drivers/usbcsc/d_usbcsc.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kernel/eka/drivers/usbcsc/d_usbcsc.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,3676 @@
     1.4 +// Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32\drivers\usbcsc\d_usbcsc.cpp
    1.18 +// LDD for USB Device driver stack, using shared chunks:
    1.19 +// The channel object.
    1.20 +// 
    1.21 +//
    1.22 +
    1.23 +/**
    1.24 + @file d_usbcsc.cpp
    1.25 + @internalTechnology
    1.26 +*/
    1.27 +
    1.28 +#include <drivers/usbcsc.h>
    1.29 +#include "platform.h"
    1.30 +
    1.31 +/*****************************************************************************\
    1.32 +*   DUsbcScLogDevice                                                          *
    1.33 +*                                                                             *
    1.34 +*   Inherits from DLogicalDevice, the USB Shared Chunk LDD factory class      *
    1.35 +*                                                                             *
    1.36 +\*****************************************************************************/
    1.37 +
    1.38 +_LIT(KUsbScLddName, "Usbcsc");
    1.39 +
    1.40 +static const TInt KUsbRequestCallbackPriority = 2;
    1.41 +
    1.42 +/** Real entry point from the Kernel: return a new driver.
    1.43 + */
    1.44 +DECLARE_STANDARD_LDD()
    1.45 +	{
    1.46 +	return new DUsbcScLogDevice;
    1.47 +	}
    1.48 +
    1.49 +/** Create a channel on the device.
    1.50 +
    1.51 +	@internalComponent
    1.52 +*/
    1.53 +TInt DUsbcScLogDevice::Create(DLogicalChannelBase*& aChannel)
    1.54 +	{
    1.55 +	aChannel = new DLddUsbcScChannel;
    1.56 +	return aChannel ? KErrNone : KErrNoMemory;
    1.57 +	}
    1.58 +
    1.59 +
    1.60 +DUsbcScLogDevice::DUsbcScLogDevice()
    1.61 +      {
    1.62 +	  iParseMask = KDeviceAllowUnit;
    1.63 +	  iUnitsMask = 0xffffffff;								// Leave units decision to the Controller
    1.64 +      iVersion = TVersion(KUsbcScMajorVersion, KUsbcScMinorVersion, KUsbcScBuildVersion);
    1.65 +      }
    1.66 +
    1.67 +
    1.68 +TInt DUsbcScLogDevice::Install()
    1.69 +	{
    1.70 +	// Only proceed if we have the Controller underneath us
    1.71 +	if (!DUsbClientController::UsbcControllerPointer())
    1.72 +		{
    1.73 +		__KTRACE_OPT(KPANIC, Kern::Printf("LDD Install: USB Controller Not Present"));
    1.74 +		return KErrGeneral;
    1.75 +		}
    1.76 +	return SetName(&KUsbScLddName);
    1.77 +	}
    1.78 +
    1.79 +
    1.80 +//
    1.81 +// Return the USB controller capabilities.
    1.82 +//
    1.83 +void DUsbcScLogDevice::GetCaps(TDes8& aDes) const
    1.84 +	{
    1.85 +	TPckgBuf<TCapsDevUsbc> b;
    1.86 +	b().version = iVersion;
    1.87 +	Kern::InfoCopy(aDes, b);
    1.88 +	}
    1.89 +
    1.90 +// End DUsbcScLogDevice
    1.91 +
    1.92 +/*****************************************************************************\
    1.93 +*   TUsbcScChunkInfo                                                          *
    1.94 +*                                                                             *
    1.95 +*   Where Chunk information is stored for the channel, and preseved for the   *
    1.96 +*   life of the chunk.                                                        *
    1.97 +*                                                                             *
    1.98 +\*****************************************************************************/
    1.99 +
   1.100 +void DfcChunkCleanup(TAny*);
   1.101 +
   1.102 +TUsbcScChunkInfo::TUsbcScChunkInfo(DLogicalDevice* aLdd)
   1.103 +	: 	iChunk(NULL),
   1.104 +		iCleanup((TDfcFn)&DfcChunkCleanup,this,Kern::SvMsgQue(),0),
   1.105 +		iChunkMem(NULL),
   1.106 +		iLdd(aLdd)
   1.107 +	{
   1.108 +	iPageNtz = (TInt8)__e32_find_ls1_32(Kern::RoundToPageSize(1));
   1.109 +	}
   1.110 +
   1.111 +TInt TUsbcScChunkInfo::CreateChunk(TInt aTotalSize)
   1.112 +	{
   1.113 +	// First, reserve an TUint of memory for each of pages needed to hold aTotalSize of memory.
   1.114 +	// This will form the chunk map, so that we can look up the memory geometry.
   1.115 +	iAllocatedSize = (aTotalSize>>iPageNtz)*sizeof(TUint);
   1.116 +	iPhysicalMap = (TUint*) Kern::AllocZ(iAllocatedSize);
   1.117 +	TInt r;
   1.118 +	if (iPhysicalMap==NULL)
   1.119 +		r = KErrNoMemory;
   1.120 +	else
   1.121 +		{
   1.122 +		TChunkCreateInfo chunkInfo;
   1.123 +		chunkInfo.iType = TChunkCreateInfo::ESharedKernelMultiple;
   1.124 +		chunkInfo.iMaxSize = aTotalSize;
   1.125 +		chunkInfo.iMapAttr = EMapAttrCachedMax;
   1.126 +		chunkInfo.iOwnsMemory = EFalse;
   1.127 +		chunkInfo.iDestroyedDfc = &iCleanup;
   1.128 +
   1.129 +		TLinAddr chunkMem;
   1.130 +		r = Kern::ChunkCreate(chunkInfo, iChunk, chunkMem, iChunkMapAttr);
   1.131 +		iChunkMem = (TInt8*) chunkMem;
   1.132 +		if (r==KErrNone)
   1.133 +			iLdd->Open();
   1.134 +		}
   1.135 +
   1.136 +	return r;
   1.137 +}
   1.138 +
   1.139 +
   1.140 +// This method requests closing the chunk.
   1.141 +// Note that nothing may happen immediately, as something else may have the chunk open.
   1.142 +void TUsbcScChunkInfo::Close()
   1.143 +{
   1.144 +	Kern::ChunkClose(iChunk);	
   1.145 +}
   1.146 +
   1.147 +
   1.148 +TInt TUsbcScChunkInfo::ChunkAlloc(TInt aOffset, TInt aSize)
   1.149 +	{
   1.150 +	TUint pageMask = (~0)<<iPageNtz;
   1.151 +	TUint rleMask = ~pageMask;
   1.152 +	TUint pageSize = rleMask+1;
   1.153 +	TInt r;
   1.154 +	TLinAddr physAddr;
   1.155 +
   1.156 +	__KTRACE_OPT(KUSB, Kern::Printf("::chunkalloc  AllocPhysicalRam aSize %d", aSize));
   1.157 +
   1.158 +	r = Epoc::AllocPhysicalRam(aSize, physAddr);
   1.159 +	__KTRACE_OPT(KUSB, if (r!=KErrNone) Kern::Printf("::chunkalloc AllocPhysicalRam r=%d  (Error!)", r));
   1.160 +	if (r==KErrNone)
   1.161 +		{	
   1.162 +		__KTRACE_OPT(KUSB, Kern::Printf("::chunkalloc ChunkCommitPhysical iChunk 0x%x size(%d), aOffset 0x%x, aSize 0x%x phsAddr 0x%x",
   1.163 +																	 				iChunk, sizeof(DChunk), aOffset, aSize,physAddr ));
   1.164 +
   1.165 +		r = Kern::ChunkCommitPhysical(iChunk, aOffset, aSize, physAddr);
   1.166 +		__KTRACE_OPT(KUSB, if (r!=KErrNone) Kern::Printf("::chunkalloc ChunkCommitPhysical r=%d  (Error!)", r));
   1.167 +
   1.168 +		if (r!=KErrNone)
   1.169 +				Epoc::FreePhysicalRam(physAddr, aSize);
   1.170 +		else 
   1.171 +			{ // record physical address and length in physical map
   1.172 +			TInt rle;
   1.173 +			TInt i=0;
   1.174 +			for (rle=(aSize>>iPageNtz); rle>0; rle--, i++,physAddr+=pageSize) 
   1.175 +				{
   1.176 +				__KTRACE_OPT(KUSB, Kern::Printf("::phys offset 0x%x = 0x%x",
   1.177 +												(aOffset>>iPageNtz)+i,  (physAddr & pageMask) | ((rle>(TInt)rleMask)?(TInt)rleMask:rle)));
   1.178 +				iPhysicalMap[(aOffset>>iPageNtz)+i] = (physAddr & pageMask) | ((rle>(TInt)rleMask)?(TInt)rleMask:rle);
   1.179 +				}
   1.180 +			}
   1.181 +		}
   1.182 +	else if (r==KErrNoMemory)
   1.183 +		r = -KErrNoMemory;  // Semi-expected error.
   1.184 +	return r;
   1.185 +	}
   1.186 +
   1.187 +/**
   1.188 +This method retrieves the physical address of a given offset into the Chunk, and returns
   1.189 +the length of contiguous physical memory from this point.
   1.190 +
   1.191 +@param aOffset		the offset from the start of the chunk, to be queried.
   1.192 +@param aPhysical	a pointer to a TPhysAddr, to be filled with the physical
   1.193 +					address of the memory at the given offset.
   1.194 +
   1.195 +@returns the length of contiguous physical memory from the given offset.
   1.196 +*/
   1.197 +
   1.198 +TInt TUsbcScChunkInfo::GetPhysical(TInt aOffset, TPhysAddr* aPhysical)
   1.199 +	{
   1.200 +	// Use masks, to retrieve the two components from the physical map, we created of the memory.
   1.201 +	TUint pageMask = (~0)<<iPageNtz;
   1.202 +	TUint val =  iPhysicalMap[aOffset>>iPageNtz];
   1.203 +	*aPhysical=(val & pageMask)+(aOffset & ~pageMask);
   1.204 +	return ((val & ~pageMask)<<iPageNtz) -  (aOffset & ~pageMask);
   1.205 +	}
   1.206 +
   1.207 +
   1.208 +// DFC calls this fuction, which invokes the cleanup method.
   1.209 +
   1.210 +void DfcChunkCleanup(TAny* aChunkInfo)
   1.211 +	{
   1.212 +	((TUsbcScChunkInfo*) aChunkInfo)->ChunkCleanup();
   1.213 +	}
   1.214 +
   1.215 +
   1.216 +void TUsbcScChunkInfo::ChunkCleanup()
   1.217 +{
   1.218 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScChunkInfo::ChunkCleanup()"));
   1.219 +	TUint physAddr;
   1.220 +	TInt length;
   1.221 +	TInt offset = 0;
   1.222 +	
   1.223 +	// The part of the field used for the physical page address.
   1.224 +	TUint pageMask = (~0)<<iPageNtz;
   1.225 +
   1.226 +	// The part of the field used for the run length encoding, of the contiguous pages.
   1.227 +	TUint rleMask = ~pageMask;
   1.228 +	TInt records=(iAllocatedSize>>2);
   1.229 +
   1.230 +	while (offset < records) 
   1.231 +		{
   1.232 +		physAddr = 	iPhysicalMap[offset] & pageMask;
   1.233 +		length = iPhysicalMap[offset] & rleMask;
   1.234 +
   1.235 +		if (physAddr>0)	
   1.236 +			Epoc::FreePhysicalRam(physAddr, length);
   1.237 +
   1.238 +		offset += (length>0)?length:1;
   1.239 +		}
   1.240 +	Kern::Free(iPhysicalMap);
   1.241 +
   1.242 +	DLogicalDevice* ldd = iLdd;
   1.243 +	delete this;
   1.244 +	ldd->Close(NULL);
   1.245 +}
   1.246 +
   1.247 +TInt TUsbcScChunkInfo::New(TUsbcScChunkInfo*& aChunk, TInt aSize, DLogicalDevice* aLdd)
   1.248 +{
   1.249 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScChunkInfo::New totalSize %d", aSize));
   1.250 +
   1.251 +	aChunk = new TUsbcScChunkInfo(aLdd);
   1.252 +	if (aChunk==NULL)
   1.253 +		{
   1.254 +		return KErrNoMemory;
   1.255 +		}
   1.256 +					
   1.257 +	TInt r = aChunk->CreateChunk(aSize);
   1.258 +	if (r!=KErrNone)
   1.259 +		{
   1.260 +		delete aChunk;
   1.261 +		aChunk=NULL;
   1.262 +		return r;
   1.263 +		}
   1.264 +
   1.265 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScChunkInfo::New Created at 0x%x",  aChunk->iChunkMem  ));
   1.266 +	return KErrNone;
   1.267 +}
   1.268 +
   1.269 +// End TUsbcScChunkInfo
   1.270 +
   1.271 +/*****************************************************************************\
   1.272 +*    TUsbcScBuffer                                                            *
   1.273 +*                                                                             *
   1.274 +*    Represents a buffer, within a chunk.  Each buffers can be used by        *
   1.275 +*    differt endpoint on differnt alt settings                                *
   1.276 +*                                                                             *
   1.277 +\*****************************************************************************/
   1.278 +
   1.279 +
   1.280 +TInt TUsbcScBuffer::Construct(TInt aDirection, DLddUsbcScChannel* aLdd, TInt aBufferOffset, TInt aBufferEndOffset, TInt aMinReadSize, TInt aMaxPacketSize, TInt aMaxReadSize)
   1.281 +	{
   1.282 +	TInt r;
   1.283 +#ifdef _DEBUG
   1.284 +	iSequence = aBufferOffset; // Initialized at this, so that each buffer starts with a diffrent sequence number
   1.285 +#endif
   1.286 +	iMinReadSize = aMinReadSize;
   1.287 +	TInt size = (aBufferEndOffset - aBufferOffset);
   1.288 +	TInt pageSize = Kern::RoundToPageSize(1);
   1.289 +	if (aMaxReadSize > 0)
   1.290 +		iMaxReadSize = aMaxReadSize;
   1.291 +	else
   1.292 +		iMaxReadSize = pageSize + ((size/3) & ~(pageSize -1));
   1.293 +	iLdd = aLdd;
   1.294 +	iDirection = aDirection;
   1.295 +	iMode=0;
   1.296 +	iChunkInfo = aLdd->iChunkInfo;
   1.297 +	iChunkAddr = (TLinAddr) (aLdd->iChunkInfo->iChunkMem);  //aChunkAddr;
   1.298 +
   1.299 +	TInt headerSize =  sizeof(TUsbcScTransferHeader)-4; // TransferHeader includes 4 bytes of data.
   1.300 +
   1.301 +
   1.302 +	TUint maxAlignment; // Note:  This is a mask for max Alignment, 
   1.303 +
   1.304 +	if (aMaxPacketSize)
   1.305 +		{ // EP0 packets are not DMAed, and so dont need ialignment.
   1.306 +		iAlignMask = ~3;
   1.307 +		maxAlignment = 3;
   1.308 +		}
   1.309 +	else
   1.310 +		 maxAlignment = 1023; // We don't know what the alignment requirement will be until enumeration, so assume worse case.
   1.311 +
   1.312 +	iFirstPacket = aBufferOffset + sizeof(SUsbcScBufferHeader) + headerSize;
   1.313 +	iFirstPacket = (iFirstPacket + maxAlignment) & ~maxAlignment;
   1.314 +	
   1.315 +	iBufferStart = (SUsbcScBufferHeader *) (iChunkAddr+aBufferOffset);
   1.316 +	iBufferEnd = aBufferEndOffset;
   1.317 +
   1.318 +	if ((iDirection&1)==KUsbcScOut)
   1.319 +		iHead = iFirstPacket-headerSize;//aBufferOffset + sizeof(SUsbcScBufferHeader);
   1.320 +	else
   1.321 +		iSent = 0;
   1.322 +
   1.323 +	iStalled=0;
   1.324 +	iMaxPacketSize=0;
   1.325 +	
   1.326 +	r =  iStatusList.Construct((aDirection==KUsbcScIn)?KUsbcScInRequests:KUsbcScOutRequests, iLdd->iClient);
   1.327 +	if (!r)
   1.328 +		{
   1.329 +		iMaxPacketSize = aMaxPacketSize; // Indicates configured if ep0, otherwise not.
   1.330 +		}
   1.331 +	return r;
   1.332 +	}
   1.333 +
   1.334 +
   1.335 +void TUsbcScBuffer::CreateChunkBufferHeader()
   1.336 +{
   1.337 +	if ((iDirection&1)==KUsbcScOut)
   1.338 +		{
   1.339 +		iBufferStart->iHead= iHead;
   1.340 +		iBufferStart->iTail= iHead; // Initially no data!
   1.341 +		iBufferStart->iBilTail=iHead;
   1.342 +		__KTRACE_OPT(KUSB, Kern::Printf("Realize:  iHead 0x%x  bufferHeader 0x%x", iHead,iBufferStart ));
   1.343 +
   1.344 +		// Dont need to round here, as we will round it up on endpoint change. (configuration)
   1.345 +		}
   1.346 +}
   1.347 +
   1.348 +/*
   1.349 +TUsbcScBuffer::StartEndpoint
   1.350 +
   1.351 +This method sets the nessesary paramenters to the buffer, for use for a particular endpoint.
   1.352 +
   1.353 +*/
   1.354 +void TUsbcScBuffer::StartEndpoint(TUsbcRequestCallback* aRequestInfo, TUint aFlags)
   1.355 +	{
   1.356 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::StartEndpoint (0x%x) : ep %d(%d)",this,aRequestInfo->iEndpointNum, aRequestInfo->iRealEpNum));
   1.357 +	
   1.358 +	iCallback=aRequestInfo;
   1.359 +	iMaxPacketSize =  iLdd->iController->EndpointPacketSize(iLdd, aRequestInfo->iRealEpNum);
   1.360 +	iAlignMask = ~(((iMaxPacketSize+1) & 0xFFFFFFF8)-1);
   1.361 +	iMode = aFlags;
   1.362 +    __KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::StartEndpoint : max Packets %d, mask 0x%x flags 0x%x", iMaxPacketSize, iAlignMask, iMode));
   1.363 +	if ((iDirection&1)==KUsbcScOut)
   1.364 +		{
   1.365 +		__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::UsbcScOut\n"));
   1.366 +		// Add dummy packet (doesnt have to be aligned, which avoids what if it changes issue)
   1.367 +		// And Start next read.
   1.368 +		iNeedsPacket=KEpIsStarting;
   1.369 +		}
   1.370 +	}
   1.371 +
   1.372 +
   1.373 +
   1.374 +void TUsbcScBuffer::Destroy()
   1.375 +{
   1.376 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::Destroy()"));
   1.377 +	Cancel(KErrCancel);
   1.378 +	if (iLdd->iController && ((iDirection&1)==KUsbcScOut))  
   1.379 +		{  // Me must cancel reads to LDD to, an there will be no list for the callbacks to look into.
   1.380 +		iLdd->iController->CancelReadBuffer(iLdd, iCallback->iRealEpNum);
   1.381 +		}
   1.382 +	iStatusList.Destroy();
   1.383 +}
   1.384 +
   1.385 +
   1.386 +
   1.387 +TInt TUsbcScBuffer::StartDataRead()
   1.388 +{
   1.389 +	if (!iMaxPacketSize)
   1.390 +	{
   1.391 +		__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::StartDataRead() - Not Configured"));
   1.392 +		return KErrNone;
   1.393 +	}
   1.394 +	if (iStatusList.iState!=ENotRunning) 
   1.395 +		{
   1.396 +		__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::StartDataRead() - Already Stated! (%d)",iStatusList.iState));
   1.397 +		return KErrNone;
   1.398 +		}
   1.399 +
   1.400 +	TInt maxLength;
   1.401 +	TInt freeSpace;
   1.402 +	TPhysAddr physAddr;
   1.403 +
   1.404 +	// get next request
   1.405 +	TUsbcScStatusElement* nextJob = iStatusList.Next();
   1.406 +	if (nextJob == NULL)
   1.407 +		{
   1.408 +		__KTRACE_OPT(KUSB, Kern::Printf("No more jobs"));
   1.409 +		if (iMode && KUsbScCoupledRead)
   1.410 +			return KErrEof;
   1.411 +		iStatusList.iState=EReadingAhead;
   1.412 +		}
   1.413 +	else
   1.414 +		iStatusList.iState=EInProgress;
   1.415 +
   1.416 +	TInt tail = iBufferStart->iTail;
   1.417 +	TInt headerSize =  sizeof(TUsbcScTransferHeader)-4; // TransferHeader includes 4 bytes of data.
   1.418 +	maxLength = iChunkInfo->GetPhysical(iHead + headerSize, &physAddr); //returns all the bytes available after iHead + headerSize)
   1.419 +
   1.420 +	__ASSERT_DEBUG(maxLength>0,Kern::Fault("TUsbcScBuffer::StartDataRead(", __LINE__)); 
   1.421 +
   1.422 +
   1.423 +	if (tail>iHead)  //  # # # H _ _ _ T # # # #
   1.424 +		{
   1.425 +		__KTRACE_OPT(KUSB,Kern::Printf("TUsbcScBuffer::StartDataRead() - tail 0x%x>head 0x%x, maxlength 0x%x", tail, iHead, maxLength));
   1.426 +
   1.427 +		freeSpace = (tail & iAlignMask) - (iHead +headerSize + (~iAlignMask+1) );  // Cant read right up to last buffer, or head/tail will cross.
   1.428 +
   1.429 +		if (freeSpace<iMinReadSize)
   1.430 +			{
   1.431 +			iStatusList.iState=ENotRunning;
   1.432 +			__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::StartDataRead() - Stall!!"));
   1.433 +			return KErrOverflow; 				// Read STALL !! !! !!
   1.434 +			}
   1.435 +
   1.436 +		if (freeSpace<maxLength)
   1.437 +			maxLength = freeSpace;
   1.438 +		}
   1.439 +	if (maxLength> iMaxReadSize) 
   1.440 +		maxLength =  iMaxReadSize;
   1.441 +	// else  tail<iHead (or empty)      _ _ _ T # # # H _ _ _ _
   1.442 +	// We would not have set iHead here if too small. So must be ok.
   1.443 +		
   1.444 +	__ASSERT_DEBUG(maxLength>=iMinReadSize,Kern::Fault("TUsbcScBuffer::StartDataRead(", __LINE__)); 
   1.445 +
   1.446 +	TUint8* data = ((TUsbcScTransferHeader *) (iHead + iChunkAddr))->iData.b;
   1.447 +	// set up callback stucture
   1.448 +
   1.449 +	iCallback->SetRxBufferInfo(data, physAddr, iIndexArray, iSizeArray,maxLength);
   1.450 +	TInt r;
   1.451 +	// Go!!
   1.452 +	r = iLdd->iController->SetupReadBuffer(*iCallback);
   1.453 +	if (r!=KErrNone)
   1.454 +		{
   1.455 +		__KTRACE_OPT(KUSB,Kern::Printf("SetupReadBuffer Error: %d, RT %d",r, iStatusList.iState));
   1.456 +		iStatusList.Complete(r);
   1.457 +		}
   1.458 +	// After this, TUsbcScEndpoint::RequestCallback is called in a DFC.
   1.459 +	// This in turn calls either TUsbcScBuffer::CompleteRead.
   1.460 +	return KErrNone;
   1.461 +}
   1.462 +
   1.463 +
   1.464 +void TUsbcScBuffer::CompleteRead(TBool aStartNextRead)
   1.465 +{
   1.466 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::CompleteRead buff=%x",this));
   1.467 +
   1.468 +    // The first packet always contains the total #of bytes
   1.469 +	const TInt byteCount = iCallback->iPacketSize[0];
   1.470 +	const TInt packetCount = iCallback->iRxPackets;
   1.471 + 	iCallback->iRxPackets=0;
   1.472 +	TUint flags = 0;
   1.473 +
   1.474 +	if (iCallback->iPacketSize[packetCount - 1] < (TUint) iMaxPacketSize)
   1.475 +		flags = KUsbcScShortPacket;
   1.476 +
   1.477 +	UpdateBufferList(byteCount, flags, aStartNextRead);
   1.478 +}
   1.479 +
   1.480 +
   1.481 +// This method "submits" the current transfer, and starts off the next read.
   1.482 +
   1.483 +void TUsbcScBuffer::UpdateBufferList(TInt aByteCount,TUint aFlags, TBool aStartNextRead)
   1.484 +	{
   1.485 +
   1.486 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::UpdateBUfferLIst aByteCount %d, flags 0x%x iHead 0x%x", aByteCount, aFlags, iHead));
   1.487 +
   1.488 +	TInt headerSize =  sizeof(TUsbcScTransferHeader)-4; // TransferHeader includes 4 bytes of data.
   1.489 +	TLinAddr dummy;
   1.490 +	__KTRACE_OPT(KUSB, Kern::Printf("iHead 0x%x headerSize 0x%x",iHead, headerSize));
   1.491 +
   1.492 +	// Find iNext
   1.493 +
   1.494 +	TInt next =  iHead +  headerSize + aByteCount; // next unused byte in buffer.
   1.495 +	TInt maxLength; 
   1.496 +
   1.497 +	// This may take a few loops before we settle on a value.
   1.498 +	do 
   1.499 +		{
   1.500 +		// round up.
   1.501 +		next = (next + headerSize + ~iAlignMask) & iAlignMask;
   1.502 +		maxLength = iChunkInfo->GetPhysical(next, &dummy);
   1.503 +
   1.504 +		__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::UpdateBUfferLIst  next %x  buffer end %x min-read: %x  maxRun %x", next, iBufferEnd, iMinReadSize, maxLength));
   1.505 +		// At the end of the buffer - wrap it if needbe.
   1.506 +		if ((TUint)(next + iMinReadSize) > iBufferEnd)
   1.507 +			{
   1.508 +			next = iFirstPacket;
   1.509 +			continue;
   1.510 +			}
   1.511 +		// Not enough space, move onto next block.
   1.512 +		if (maxLength<iMinReadSize) 
   1.513 +			{
   1.514 +			next+=maxLength;
   1.515 +			__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::UpdateBUfferLIst Skip exhausted block. next %x max %d", next, maxLength));
   1.516 +			continue;
   1.517 +			}
   1.518 +		}
   1.519 +	while (EFalse);
   1.520 +
   1.521 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::UpdateBUfferLIst next (pre deduct): %x, Fill in header at head: 0x%x,  BuffStart: 0x%x.", next, iHead, iBufferStart));
   1.522 +	
   1.523 +	next -=  headerSize;  // Move next back from the data start position, to the header start.
   1.524 +
   1.525 +	TUsbcScTransferHeader* header = (TUsbcScTransferHeader*) (iHead + iChunkAddr);
   1.526 +	
   1.527 +// Create Header
   1.528 +#ifdef _DEBUG
   1.529 +	header->iHashId=59*(iLdd->iAlternateSetting+1)+iCallback->iRealEpNum; // Alt setting realated....
   1.530 +	header->iSequence=iSequence;
   1.531 +	iSequence++;
   1.532 +#endif
   1.533 +	header->iBytes=aByteCount;
   1.534 +	header->iNext=next;
   1.535 +	header->iAltSettingSeq=iLdd->iAsSeq;
   1.536 +	header->iAltSetting=iLdd->iAlternateSetting;
   1.537 +	header->iFlags=aFlags;
   1.538 +	__KTRACE_OPT(KUSB, Kern::Printf("We set next to 0x%x", next));
   1.539 +
   1.540 +	iStatusList.iState=ENotRunning;
   1.541 +	if (next==iBufferStart->iTail) //or (othwise is as good as full)
   1.542 +		{
   1.543 +			iStalled=next;
   1.544 +		}
   1.545 +	else
   1.546 +		{
   1.547 +
   1.548 +		__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::UpdateBUfferLIst StartRead?? "));
   1.549 +		TInt oldHead=iHead;
   1.550 +		iHead = next;
   1.551 +
   1.552 +		if ((aStartNextRead) && (StartDataRead() == KErrOverflow))
   1.553 +			{ // Oh crumbs, set state as slalled.
   1.554 +			if (oldHead != iBufferStart->iBilTail) 
   1.555 +				// If user has not read everything in the buffer
   1.556 +				// then set up a stall, so that ldd get to be woken early
   1.557 +				{
   1.558 +				iStalled=next;
   1.559 +				iHead=oldHead;
   1.560 +				}
   1.561 +			else // otherwise if everything is read
   1.562 +				// no choice but to return what we have
   1.563 +				{
   1.564 +				iBufferStart->iHead = iHead;
   1.565 +				}
   1.566 +			}
   1.567 +		else
   1.568 +			{
   1.569 +			iBufferStart->iHead = next;
   1.570 +			__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::UpdateBUfferLIst Compleating\n"));
   1.571 +			}
   1.572 +		// Complete userside
   1.573 +		iStatusList.Complete();
   1.574 +		}  
   1.575 +	}
   1.576 +
   1.577 +void TUsbcScBuffer::PopStall()
   1.578 +	{
   1.579 +	if (iStalled==iBufferStart->iTail)
   1.580 +		return;  // Still stalled.
   1.581 +
   1.582 +	if (iStalled!=-1) // If not Alt packet only stall
   1.583 +	{
   1.584 +		// pop off packet	
   1.585 +		iHead = iStalled;
   1.586 + 	}
   1.587 +	iStalled=0;
   1.588 +	// If Alt setting of the popped packet is different to now
   1.589 +	// Add alt setting change packet.
   1.590 +
   1.591 +
   1.592 +	if (StartDataRead() == KErrOverflow)
   1.593 +	{
   1.594 +		__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::PopStall Warning: Transfer was freed, but still no space!\n"));
   1.595 +	}
   1.596 +
   1.597 +	iBufferStart->iHead = iHead;
   1.598 +	}
   1.599 +
   1.600 +
   1.601 +
   1.602 +void TUsbcScBuffer::StartDataWrite()
   1.603 +	{
   1.604 +	
   1.605 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::StartDataWrite()"));
   1.606 +	TUsbcScStatusElement* nextJob = iStatusList.Next();
   1.607 +	TBool zlpReqd;
   1.608 +	TInt length;
   1.609 +	TUint start;
   1.610 +	TUint8* startAddr;
   1.611 +	TInt maxLength;
   1.612 +	TPhysAddr physAddr;
   1.613 +	TInt r;
   1.614 +	if (!iMaxPacketSize)
   1.615 +	{
   1.616 +		__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::StartDataWrite() - Not Configured"));
   1.617 +		return;
   1.618 +	}
   1.619 +
   1.620 +	if (nextJob == NULL)
   1.621 +		{
   1.622 +		__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::StartDataWrite() - No more jobs d=%d", iDirection));
   1.623 +		if (iDirection==KUsbcScBiIn) // assume this is EP0, if this is true.
   1.624 +			{
   1.625 +			__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::StartDataWrite() Queue Read on EP0."));	
   1.626 +			// Start other read again.
   1.627 +			iLdd->iBuffers[iLdd->iEP0OutBuff].StartDataRead();
   1.628 +			}
   1.629 +		}
   1.630 +	else
   1.631 +		{
   1.632 +		if (iStatusList.iState==ENotRunning)
   1.633 +			iSent=0;
   1.634 +		iStatusList.iState=EInProgress;
   1.635 +
   1.636 +		start = nextJob->iStart;
   1.637 +		startAddr = (TUint8*) (start + ((TUint) (iChunkInfo->iChunkMem)));
   1.638 +
   1.639 +		length = nextJob->iLength;
   1.640 +		zlpReqd = (nextJob->iFlags & KUsbcScWriteFlagsZlp) !=0;
   1.641 +		// get max read length
   1.642 +		maxLength = iChunkInfo->GetPhysical( start, &physAddr); 
   1.643 +
   1.644 +		if (maxLength < length)
   1.645 +			{
   1.646 +				// modify request.
   1.647 +				nextJob->iStart += maxLength;
   1.648 +				nextJob->iLength -= maxLength;
   1.649 +				// start this request.
   1.650 +				iStatusList.iState=EFramgementInProgress;
   1.651 +				zlpReqd=EFalse;
   1.652 +				length =  maxLength;
   1.653 +			}
   1.654 +
   1.655 +		if (iDirection==KUsbcScBiIn) // this is for EP0
   1.656 +			{
   1.657 +			iLdd->iController->CancelReadBuffer(iLdd, iCallback->iRealEpNum);
   1.658 +			iLdd->iBuffers[iLdd->iEP0OutBuff].iStatusList.iState=ENotRunning;
   1.659 +			}
   1.660 +		
   1.661 +		iCallback->SetTxBufferInfo(startAddr, physAddr, length);
   1.662 +		iCallback->iZlpReqd = zlpReqd;
   1.663 +		r = iLdd->iController->SetupWriteBuffer(*iCallback);
   1.664 +		if (r!=KErrNone)
   1.665 +			{
   1.666 +			__KTRACE_OPT(KUSB, Kern::Printf("SetupWriteBUffer Error: %d",r));
   1.667 +			iStatusList.Complete(r);
   1.668 +			}
   1.669 +		}
   1.670 +
   1.671 +	}
   1.672 +
   1.673 +void TUsbcScBuffer::CompleteWrite()
   1.674 +	{
   1.675 +	TInt error = iCallback->iError;
   1.676 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::CompleteWrite buff=%x, err=%d",this, error));
   1.677 +	iSent+= iCallback->iTxBytes;
   1.678 +
   1.679 +	// More to send?
   1.680 +	if (error || iStatusList.iState!=EFramgementInProgress)
   1.681 +		{
   1.682 +		// complete request with error (if one).
   1.683 +		// Some data could have been transmitted, even with an error. 
   1.684 +		iStatusList.Complete(error);
   1.685 +		}
   1.686 +
   1.687 +	// Start next request, or next part of this one.
   1.688 +	StartDataWrite();
   1.689 +	
   1.690 +	}
   1.691 +
   1.692 +// Cancels the current request's callback.
   1.693 +// This is not to say it will cancel the actual operation,
   1.694 +// However it will cancel any further sections of the user perceived operation
   1.695 +// that are not yet started.
   1.696 +void TUsbcScBuffer::Cancel(TInt aErrorCode)
   1.697 +	{
   1.698 +	iStatusList.CancelQueued();
   1.699 +	if (iLdd->iController && ((iDirection&1)==KUsbcScIn))
   1.700 +		{
   1.701 +		iLdd->iController->CancelWriteBuffer(iLdd, iCallback->iRealEpNum);
   1.702 +		}
   1.703 +
   1.704 +	iStatusList.Complete(aErrorCode);
   1.705 +	}
   1.706 +
   1.707 +void TUsbcScBuffer::Ep0CancelLddRead()
   1.708 +	{
   1.709 +	// Stopping a read isn't as easy as one might think.
   1.710 +	// We cancel the callback, but then check if any data was received (but not returned to us).
   1.711 +	// If so, we must de-queue the request, and call the completion code.
   1.712 +	
   1.713 +	iLdd->iController->CancelReadBuffer(iLdd, iCallback->iRealEpNum);
   1.714 +	if (iCallback->iRxPackets) // received data?
   1.715 +		{
   1.716 +		// remove DFC (if infact sent)
   1.717 +		iCallback->iDfc.Cancel();
   1.718 +
   1.719 +		// process the callback now, but dont start another
   1.720 +		CompleteRead(EFalse);
   1.721 +		}
   1.722 +	}
   1.723 +
   1.724 +void TUsbcScBuffer::SendEp0StatusPacket(TInt aState)
   1.725 +{
   1.726 +	__KTRACE_OPT(KUSB, Kern::Printf(" TUsbcScBuffer::SendEp0StatusPacket(%d)", aState));
   1.727 +
   1.728 +	// We need to add a packet to the buffer, so we must stop the pending read, and start
   1.729 +	// another after we have added out packet.  
   1.730 +	Ep0CancelLddRead();
   1.731 +
   1.732 +	TUint* state = ((TUsbcScTransferHeader *) (iHead + iChunkAddr))->iData.i;
   1.733 +	*state = aState;
   1.734 +	UpdateBufferList(4,KUsbcScStateChange);
   1.735 +}
   1.736 +
   1.737 +// End TUsbcScBuffer
   1.738 +
   1.739 +/*****************************************************************************\
   1.740 +*    TUsbcScStatusList                                                        *
   1.741 +*                                                                             *
   1.742 +*    This is a list of read or write requests, containing user status         *
   1.743 +*    requests, that should later be completed.                                *
   1.744 +*                                                                             *
   1.745 +\*****************************************************************************/
   1.746 +
   1.747 +/**
   1.748 +Constructor for TUsbcScStatusList.
   1.749 +
   1.750 +@param aSize	is the number of requests to allow at any one time.  This value
   1.751 +				must be a power of two, for correct operation.
   1.752 +
   1.753 +@returns KErrNoMemory if memory allocation failure, otherwise KErrNone.
   1.754 +*/
   1.755 +
   1.756 +TInt TUsbcScStatusList::Construct(TInt aSize, DThread* aClient)
   1.757 +	{
   1.758 +	iSize=aSize;
   1.759 +	iHead = 0;
   1.760 +	iLength = 0;
   1.761 +	iClient = aClient;
   1.762 +	iElements=(TUsbcScStatusElement *) Kern::AllocZ(sizeof(TUsbcScStatusElement)*aSize);
   1.763 +	return (iElements==NULL)?KErrNoMemory:KErrNone;	
   1.764 +	};
   1.765 +
   1.766 +
   1.767 +// StatusList must be inactive before destroying.
   1.768 +void TUsbcScStatusList::Destroy()
   1.769 +	{
   1.770 +	if (iState!=ENotRunning)
   1.771 +		Kern::Fault("TUsbcScStatusList::Destroy", __LINE__);
   1.772 +	if (iElements)
   1.773 +		{
   1.774 +		Kern::Free(iElements);	
   1.775 +		iElements=NULL;
   1.776 +		}
   1.777 +	iClient=NULL;
   1.778 +}
   1.779 +
   1.780 +void TUsbcScStatusList::Pop()
   1.781 +	{
   1.782 +	if (iLength>0)
   1.783 +		{
   1.784 +		iLength--;
   1.785 +		iHead = ((iHead+1) & (iSize-1));
   1.786 +		}
   1.787 +	}
   1.788 +
   1.789 +TUsbcScStatusElement* TUsbcScStatusList::Next()
   1.790 +	{
   1.791 +	return (iLength==0)?NULL:&(iElements[iHead]);
   1.792 +	}
   1.793 +
   1.794 +TInt TUsbcScStatusList ::Add(TRequestStatus* aStatus, TInt aLength, TUint aStart, TUint aFlags)
   1.795 +	{
   1.796 +	__KTRACE_OPT(KUSB,Kern::Printf("Adding request.  iLength %d  iSize %d", iLength, iSize));
   1.797 +	if (iLength<iSize)
   1.798 +		{
   1.799 +		TUsbcScStatusElement& e = iElements[((iHead+iLength) & (iSize-1))];
   1.800 +		e.iStatus = aStatus;
   1.801 +		e.iLength = aLength;
   1.802 +		e.iStart = aStart;
   1.803 +		e.iFlags = aFlags;
   1.804 +		iLength++;
   1.805 +		__KTRACE_OPT(KUSB,Kern::Printf("Adding request.  new iLength %d", iLength));
   1.806 +
   1.807 +		return KErrNone;
   1.808 +		}
   1.809 +	else
   1.810 +		return KErrInUse;
   1.811 +	}
   1.812 +
   1.813 +
   1.814 +
   1.815 +// This method cancels any requests that have yet to be started.
   1.816 +
   1.817 +void TUsbcScStatusList::CancelQueued(TInt aError)
   1.818 +{
   1.819 +	if ((iLength==0) || ((iState!=ENotRunning) && (iLength==1)))  // Nothing to do.
   1.820 +		return;  
   1.821 +	TInt elements2Complete = iLength - (iState?1:0);
   1.822 +	TInt head = iHead;
   1.823 +	iLength = 0;
   1.824 +	if (iState)	// If (iState != ENotRunning), complete all elements excepting the one at head
   1.825 +		{
   1.826 +		head = ((head+1) & (iSize-1)); // To iterate through the queue
   1.827 +		iLength = 1;
   1.828 +		}
   1.829 +	// complete them all.
   1.830 +	for (; elements2Complete>0; elements2Complete--)
   1.831 +  		{
   1.832 +		Kern::RequestComplete(iClient, iElements[head].iStatus, aError);
   1.833 +		head = ((head+1) & (iSize-1)); 
   1.834 +  		}
   1.835 +	
   1.836 +}
   1.837 +
   1.838 +
   1.839 +/* This method Completes the head status request, and pops it from its list.
   1.840 +This version of Complete is to be used in cases where the next request is not
   1.841 +chained - usually because of an error.
   1.842 +
   1.843 +@Param aError - the code to complete with.
   1.844 +
   1.845 +returns KErrNotFound if there was no request to complete
   1.846 +*/
   1.847 +
   1.848 +
   1.849 +TInt TUsbcScStatusList::Complete(TInt aError)
   1.850 +	{
   1.851 +	if (iState==ENotRunning)
   1.852 +		{
   1.853 +		__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScStatusList::Complete() - iState == ENotRunning!"));
   1.854 +		}
   1.855 + 	else
   1.856 +		{
   1.857 +		iState=ENotRunning;
   1.858 +		if (iLength==0)
   1.859 +			return KErrNotFound;
   1.860 +
   1.861 +		Kern::RequestComplete(iClient, iElements[iHead].iStatus, aError);
   1.862 +
   1.863 +		iLength--;
   1.864 +		iHead = ((iHead+1) & (iSize-1));
   1.865 +		}
   1.866 +	return KErrNone;
   1.867 +	}
   1.868 +
   1.869 +
   1.870 +/* This method Completes the head status request, and pops it from its list. (If found.)
   1.871 +This version of Complete is to be used in cases where the request is successful, and
   1.872 + next request after this has (if present) been chained.
   1.873 +*/
   1.874 +
   1.875 +void TUsbcScStatusList::Complete()
   1.876 +	{
   1.877 +	if (iLength==0)
   1.878 +		return;
   1.879 +	__KTRACE_OPT(KUSB, Kern::Printf("Completing request.  iLength %d", iLength));
   1.880 +
   1.881 +	Kern::RequestComplete(iClient, iElements[iHead].iStatus, KErrNone);
   1.882 +
   1.883 +	iLength--;
   1.884 +	iHead = ((iHead+1) & (iSize-1));
   1.885 +	}
   1.886 +
   1.887 +// End TUsbcScStatusList
   1.888 +
   1.889 +/*****************************************************************************\
   1.890 +*   TRealizeInfo                                                              *
   1.891 +*                                                                             *
   1.892 +*   Used by DLddUsbcScChannel::RealizeInterface to set up the chunk           *
   1.893 +*                                                                             *
   1.894 +\*****************************************************************************/
   1.895 +
   1.896 +// Init
   1.897 +//
   1.898 +// This method works out the number potential maximum number of endpoints
   1.899 +// and the number of alt settings.  With this information it allocs
   1.900 +// the necessary space for the given stucture to store information about
   1.901 +// the endpoints.  
   1.902 +// This is intended to be called by RealizeInterface.  This stucture is
   1.903 +// intended to be only temporary, and the space will be freed with Free()
   1.904 +// before RealizeInteface has finished.
   1.905 +
   1.906 +void TRealizeInfo::Init(TUsbcScAlternateSettingList* aAlternateSettingList)
   1.907 +{
   1.908 +	iAlternateSettingList = aAlternateSettingList;
   1.909 +	iMaxEndpoints=0;
   1.910 +	iTotalSize   =0;
   1.911 +	iTotalBuffers=0;
   1.912 +	iAltSettings =0;
   1.913 +	__KTRACE_OPT(KUSB, Kern::Printf("Realize: work out max endpoint"));
   1.914 +	// Work out max endpoints and number of alternate settings.
   1.915 +
   1.916 +	if (iAlternateSettingList)
   1.917 +		{
   1.918 +		TUsbcScAlternateSetting* alt = iAlternateSettingList->iHead;
   1.919 +		while (alt != NULL) 
   1.920 +			{
   1.921 +			iAltSettings++;
   1.922 +			if (alt->iNumberOfEndpoints>iMaxEndpoints)
   1.923 +				iMaxEndpoints = alt->iNumberOfEndpoints;
   1.924 +			// could work out in/out specifics, but unnecessary.
   1.925 +			alt = alt->iNext;
   1.926 +			};
   1.927 +		}
   1.928 +	
   1.929 +	// Alloc some temporary working space for temp endpoint metadata 
   1.930 +	__KTRACE_OPT(KUSB, Kern::Printf("Realize: Alloc temp.  Maxendpoints %d", iMaxEndpoints));
   1.931 +	TInt inout;
   1.932 +	for (inout=KUsbcScIn; inout<KUsbcScDirections; inout++)
   1.933 +		{
   1.934 +		iBufs[inout].iEp = (TUsbcScEndpoint **) Kern::AllocZ(iAltSettings*iMaxEndpoints*sizeof(TUsbcScEndpoint *));
   1.935 +		iBufs[inout].iSizes = (TInt *) Kern::AllocZ(iMaxEndpoints*sizeof(TInt));
   1.936 +		}
   1.937 +}
   1.938 +
   1.939 +// CopyAndSortEndpoints
   1.940 +//
   1.941 +// This method copies pointers to the endpoint records into TRealizeInfo
   1.942 +// such that they are sorted in order of size per alt setting.
   1.943 +// In and Out endpoints are separated, and kept separate.
   1.944 +// The provided data structure is assumed to have been initialised with
   1.945 +// Realize_InitRealizeInfo. 
   1.946 +//
   1.947 +// Return KErrArgument if the direction field is neither In or Out.
   1.948 +//
   1.949 +
   1.950 +TInt TRealizeInfo::CopyAndSortEndpoints()
   1.951 +	{
   1.952 +	__KTRACE_OPT(KUSB, Kern::Printf("Realize: copy And sort"));
   1.953 +
   1.954 +	TInt altSetting = 0;
   1.955 +	TInt endpointOffs;
   1.956 +	TInt endpoint;
   1.957 +	TInt altEp;
   1.958 +	TInt inout;
   1.959 +	TBool placed;
   1.960 +	TUsbcScAlternateSetting* alt;
   1.961 +	TEndpointSortBufs* bufsd;
   1.962 +
   1.963 +	if (iAlternateSettingList)
   1.964 +		{
   1.965 +		for (alt = iAlternateSettingList->iHead;alt!=NULL;alt = alt->iNext )
   1.966 +			{		
   1.967 +			__KTRACE_OPT(KUSB, Kern::Printf("Realize:   AlternateSetting %x", alt));
   1.968 +
   1.969 +			iBufs[KUsbcScIn].iEps =0;
   1.970 +			iBufs[KUsbcScOut].iEps =0;
   1.971 +
   1.972 +			// For alt setting, iterate eps
   1.973 +			for (altEp=1; altEp <= alt->iNumberOfEndpoints; altEp++)
   1.974 +				{
   1.975 +				__KTRACE_OPT(KUSB, Kern::Printf("Realize:     Endpoint to add: %d",altEp));
   1.976 +
   1.977 +				TUsbcScEndpoint* nextEp = alt->iEndpoint[altEp];
   1.978 +
   1.979 +				__KTRACE_OPT(KUSB, Kern::Printf("Realize:      ep Buffer Size: %d",nextEp->EndpointInfo()->iBufferSize));
   1.980 +				
   1.981 +				inout = (nextEp->EndpointInfo()->iDir==KUsbEpDirIn)?KUsbcScIn:
   1.982 +						(nextEp->EndpointInfo()->iDir==KUsbEpDirOut)?KUsbcScOut:KUsbcScUnknown;
   1.983 +				if (inout==KUsbcScUnknown)
   1.984 +					{
   1.985 +					__KTRACE_OPT(KUSB, Kern::Printf("Realize:     KUsbcScUnknown %x",nextEp->EndpointInfo()->iDir));
   1.986 +					return KErrArgument;
   1.987 +					}
   1.988 +
   1.989 +				bufsd = &(iBufs[inout]);
   1.990 +				__KTRACE_OPT(KUSB, Kern::Printf("Realize:      ep direction: %x # endpoints %d", inout, bufsd->iEps));
   1.991 +
   1.992 +
   1.993 +				// find and position ep, and insert.
   1.994 +
   1.995 +				if (bufsd->iEps==0) // First entry.
   1.996 +					{
   1.997 +					__KTRACE_OPT(KUSB, Kern::Printf("Realize:       Add first endpoint"));
   1.998 +					endpointOffs = altSetting*iMaxEndpoints;
   1.999 +					bufsd->iEp[endpointOffs] = nextEp;
  1.1000 +					}
  1.1001 +				else
  1.1002 +					{
  1.1003 +					placed = EFalse;
  1.1004 +					// Move down the list, until we find the right place.
  1.1005 +					for (endpoint=bufsd->iEps-1; endpoint>-1; endpoint--)
  1.1006 +						{
  1.1007 +						endpointOffs = altSetting*iMaxEndpoints + endpoint;
  1.1008 +						if (bufsd->iEp[endpointOffs]->EndpointInfo()->iBufferSize < nextEp->EndpointInfo()->iBufferSize)
  1.1009 +							{
  1.1010 +							__KTRACE_OPT(KUSB, Kern::Printf("Realize:       Shift Endpoint %d", endpoint));
  1.1011 +		
  1.1012 +							bufsd->iEp[endpointOffs+1] = bufsd->iEp[endpointOffs];
  1.1013 +							}
  1.1014 +						else
  1.1015 +							{
  1.1016 +							__KTRACE_OPT(KUSB, Kern::Printf("Realize:       Insert After Endpoint %d", endpoint));
  1.1017 +
  1.1018 +							bufsd->iEp[endpointOffs+1] = nextEp;
  1.1019 +							placed = ETrue;
  1.1020 +							break;
  1.1021 +							}
  1.1022 +						} // end for endpoint
  1.1023 +						if (!placed) // if we didn't place it, it must be the biggest so far, so goes at the top.
  1.1024 +							bufsd->iEp[0] = nextEp;
  1.1025 +					} // endif
  1.1026 +				bufsd->iEps++;			
  1.1027 +				} // for altEp
  1.1028 +				altSetting++;
  1.1029 +			} // for alt
  1.1030 +		}// if iAltsettingList
  1.1031 +	return KErrNone;
  1.1032 +	}
  1.1033 +
  1.1034 +// CalcBuffSizes
  1.1035 +//
  1.1036 +// This works out the sizes of all the buffers, and stores the result in aBufInfo
  1.1037 +// based on the buffer information provided in the same structure.
  1.1038 +// Realize_CopyAndSortEndpoints is used to fill the structure with the informaition
  1.1039 +// required.
  1.1040 +
  1.1041 +void TRealizeInfo::CalcBuffSizes()
  1.1042 +	{
  1.1043 +	__KTRACE_OPT(KUSB, Kern::Printf("Realize: Calculate Buffers"));
  1.1044 +
  1.1045 +	TInt endpoint;
  1.1046 +	TInt inout;
  1.1047 +	TInt altSetting;
  1.1048 +	TUsbcScEndpoint* nextEp;
  1.1049 +	TInt bufferSize;
  1.1050 +	TEndpointSortBufs* bufsd;
  1.1051 +
  1.1052 +	for (inout=KUsbcScIn; inout<KUsbcScDirections; inout++)
  1.1053 +		{
  1.1054 +		__KTRACE_OPT(KUSB, Kern::Printf("Realize:   Direction: %d", inout));
  1.1055 +
  1.1056 +
  1.1057 +		bufsd = &(iBufs[inout]);
  1.1058 +		// for each row, ie, buffer, find largest buffer need.
  1.1059 +		for (endpoint=0; endpoint<iMaxEndpoints; endpoint++)
  1.1060 +			{
  1.1061 +			__KTRACE_OPT(KUSB, Kern::Printf("Realize:     endpoint %d", endpoint));
  1.1062 +			TInt bufMaxSize=0;
  1.1063 +			for (altSetting=0; altSetting< iAltSettings; altSetting++)
  1.1064 +				{
  1.1065 +				__KTRACE_OPT(KUSB, Kern::Printf("Realize:       altSetting %d", altSetting));
  1.1066 +				nextEp= bufsd->iEp[altSetting* iMaxEndpoints + endpoint];
  1.1067 +				if (nextEp!=NULL)
  1.1068 +					{
  1.1069 +					bufferSize = nextEp->EndpointInfo()->iBufferSize;
  1.1070 +					__KTRACE_OPT(KUSB, Kern::Printf("Realize:       comparing size %d", bufferSize));
  1.1071 +					if (bufferSize> bufMaxSize)
  1.1072 +						 bufMaxSize = bufferSize;
  1.1073 +					}
  1.1074 +				} // for altsetting
  1.1075 +			__KTRACE_OPT(KUSB, Kern::Printf("Realize:     bufMaxSize %d", bufMaxSize));
  1.1076 +			bufsd->iSizes[endpoint] = bufMaxSize;
  1.1077 +			if (bufMaxSize>0) 
  1.1078 +				{
  1.1079 +				iTotalSize += bufsd->iSizes[endpoint];
  1.1080 +				iTotalBuffers++;
  1.1081 +				}
  1.1082 +			} // for endpoint
  1.1083 +		} // for in/out	
  1.1084 +}
  1.1085 +
  1.1086 +// Free
  1.1087 +//
  1.1088 +// Cleans up after Init()
  1.1089 +
  1.1090 +void TRealizeInfo::Free()
  1.1091 +	{
  1.1092 +	TInt inout;
  1.1093 +	for (inout=KUsbcScIn; inout<KUsbcScDirections; inout++)
  1.1094 +		{
  1.1095 +		Kern::Free(iBufs[inout].iEp);
  1.1096 +		Kern::Free(iBufs[inout].iSizes);
  1.1097 +		}
  1.1098 +	}
  1.1099 +
  1.1100 +// End TRealizeInfo
  1.1101 +
  1.1102 +
  1.1103 +// LayoutChunkHeader
  1.1104 +//
  1.1105 +// Sets up some geometry for the chunk;
  1.1106 +
  1.1107 +void TRealizeInfo::LayoutChunkHeader(TUsbcScChunkInfo* aChunkInfo)
  1.1108 +{ 
  1.1109 +	// First set up the indexes to the header structures.
  1.1110 +	TUsbcScChunkHdrOffs* chkHdr = (TUsbcScChunkHdrOffs*) aChunkInfo->iChunkMem;
  1.1111 +
  1.1112 +	chkHdr->iBuffers = sizeof(TUsbcScChunkHdrOffs); // First struct just after this one.
  1.1113 +	iChunkStuct = (TUsbcScChunkBuffersHeader*) ( (TInt) aChunkInfo->iChunkMem + chkHdr->iBuffers);
  1.1114 +
  1.1115 +	// Store number of buffers in chunk
  1.1116 +	iChunkStuct->iRecordSize = sizeof(TUsbcScBufferRecord);
  1.1117 +	iChunkStuct->iNumOfBufs=iTotalBuffers;
  1.1118 +
  1.1119 +	iAltSettingsTbl = (TUsbcScChunkAltSettingHeader*) &(iChunkStuct->iBufferOffset[(iTotalBuffers+2)*sizeof(TUsbcScBufferRecord)]); // 2 extra for EP0 in and out.
  1.1120 +
  1.1121 +	chkHdr->iAltSettings = (TUint) iAltSettingsTbl - (TUint) aChunkInfo->iChunkMem;
  1.1122 +
  1.1123 +	iAltSettingsTbl->iEpRecordSize = sizeof(TUint);
  1.1124 +	iAltSettingsTbl->iNumOfAltSettings = iAltSettings;
  1.1125 +
  1.1126 +
  1.1127 +	TInt tableOffset  = (TUint) iAltSettingsTbl->iAltTableOffset - (TUint) aChunkInfo->iChunkMem + iAltSettings*sizeof(TInt);
  1.1128 +	__KTRACE_OPT(KUSB, Kern::Printf("Realize: table offset: 0x%x, altTble %x iChnkMem %x altSettings %x",tableOffset, iAltSettingsTbl, aChunkInfo->iChunkMem, iAltSettings ));
  1.1129 +
  1.1130 +	__KTRACE_OPT(KUSB, Kern::Printf("Realize: populate chunk - create alt settings table"));
  1.1131 +
  1.1132 +	// Create alt settings table.  Set each element of altsettings table, to each induivatual alt setting table.
  1.1133 +	// then fill in the number of endpoints for that alt setting, in the table.
  1.1134 +
  1.1135 +	TInt* noEpForAlt;
  1.1136 +	TInt altSetting;
  1.1137 +	TUsbcScAlternateSetting* alt;
  1.1138 +	if (iAlternateSettingList)
  1.1139 +		{
  1.1140 +		alt = iAlternateSettingList->iHead;
  1.1141 +		for (altSetting=0; altSetting<iAltSettings; altSetting++) 
  1.1142 +			{
  1.1143 +				__KTRACE_OPT(KUSB, Kern::Printf("Realize:   altSetting %d, tableOffset %d", altSetting, tableOffset));
  1.1144 +
  1.1145 +				iAltSettingsTbl->iAltTableOffset[altSetting] = tableOffset;
  1.1146 +				noEpForAlt = (TInt*) &aChunkInfo->iChunkMem[tableOffset];
  1.1147 +			 
  1.1148 +				*noEpForAlt = alt->iNumberOfEndpoints;  // Set NumberofEndpoints field in Altsetting table
  1.1149 +				tableOffset+= sizeof(TInt)+ alt->iNumberOfEndpoints*sizeof(TUsbcScHdrEndpointRecord);
  1.1150 +				alt = alt->iNext;
  1.1151 +			}
  1.1152 +		}		
  1.1153 +
  1.1154 +} // end LayoutChunkHeader
  1.1155 +
  1.1156 +
  1.1157 +
  1.1158 +/*****************************************************************************\
  1.1159 +*   DLddUsbcScChannel                                                         *
  1.1160 +*                                                                             *
  1.1161 +*   Inherits from DLogicalDevice, the USB Shared Chunk LDD factory class      *
  1.1162 +*                                                                             *
  1.1163 +\*****************************************************************************/
  1.1164 +
  1.1165 +//
  1.1166 +// Constructor
  1.1167 +//
  1.1168 +DLddUsbcScChannel::DLddUsbcScChannel()
  1.1169 +	: iValidInterface(EFalse),
  1.1170 +	  iAlternateSettingList(NULL),
  1.1171 +	  iEndpoint(NULL),
  1.1172 +	  iCompleteAllCallbackInfo(this, DLddUsbcScChannel::EmergencyCompleteDfc, KUsbRequestCallbackPriority),
  1.1173 +	  iStatusChangePtr(NULL),
  1.1174 +	  iStatusCallbackInfo(this, DLddUsbcScChannel::StatusChangeCallback, KUsbRequestCallbackPriority),
  1.1175 +	  iEndpointStatusChangePtr(NULL),
  1.1176 +	  iEndpointStatusCallbackInfo(this, DLddUsbcScChannel::EndpointStatusChangeCallback,
  1.1177 +								  KUsbRequestCallbackPriority),
  1.1178 +      iOtgFeatureChangePtr(NULL),
  1.1179 +      iOtgFeatureCallbackInfo(this, DLddUsbcScChannel::OtgFeatureChangeCallback, KUsbRequestCallbackPriority),
  1.1180 +	  iNumberOfEndpoints(0),
  1.1181 +	  iDeviceState(EUsbcDeviceStateUndefined),
  1.1182 +	  iOwnsDeviceControl(EFalse),
  1.1183 +	  iAlternateSetting(0),
  1.1184 +	  iAsSeq(0),
  1.1185 +	  iStatusFifo(NULL),
  1.1186 +	  iUserKnowsAltSetting(ETrue),
  1.1187 +	  iDeviceStatusNeeded(EFalse),
  1.1188 +	  iChannelClosing(EFalse),
  1.1189 +	  iRealizeCalled(EFalse),
  1.1190 +	  iChunkInfo(NULL),
  1.1191 +	  iNumBuffers(-1),
  1.1192 +	  iBuffers(NULL),
  1.1193 +	  iEp0Endpoint(NULL)
  1.1194 +	{
  1.1195 +	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::DLddUsbcScChannel()"));
  1.1196 +	iClient = &Kern::CurrentThread();
  1.1197 +	iClient->Open();
  1.1198 +	for (TInt i = 1; i < KUsbcMaxRequests; i++)
  1.1199 +		{
  1.1200 +		iRequestStatus[i] = NULL;
  1.1201 +		}
  1.1202 +	}
  1.1203 +
  1.1204 +
  1.1205 +//
  1.1206 +// Destructor
  1.1207 +//
  1.1208 +
  1.1209 +DLddUsbcScChannel::~DLddUsbcScChannel()
  1.1210 +	{
  1.1211 +	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::~DLddUsbcScChannel()"));
  1.1212 +	if (iController)
  1.1213 +		{
  1.1214 +		iStatusCallbackInfo.Cancel();
  1.1215 +		iEndpointStatusCallbackInfo.Cancel();
  1.1216 +	    iOtgFeatureCallbackInfo.Cancel();
  1.1217 +        iCompleteAllCallbackInfo.Cancel();
  1.1218 +		DestroyAllInterfaces();
  1.1219 +		if (iOwnsDeviceControl)
  1.1220 +			{
  1.1221 +			iController->ReleaseDeviceControl(this);
  1.1222 +			iOwnsDeviceControl = EFalse;
  1.1223 +			}
  1.1224 +		iController->DeRegisterClient(this);
  1.1225 +		iController=NULL;
  1.1226 +		DestroyEp0();
  1.1227 +		if (iStatusFifo!=NULL)
  1.1228 +			{
  1.1229 +			delete iStatusFifo;
  1.1230 +			}
  1.1231 +		}
  1.1232 +	__KTRACE_OPT(KUSB, Kern::Printf("Closing buffers"));
  1.1233 +	if (iBuffers)
  1.1234 +		{
  1.1235 +		TInt i;
  1.1236 +		for (i=0; i<(iNumBuffers+2); i++) 
  1.1237 +			{
  1.1238 +			iBuffers[i].Destroy();
  1.1239 +			}
  1.1240 +		Kern::Free(iBuffers);
  1.1241 +		}
  1.1242 +
  1.1243 +	if (iRealizeCalled)
  1.1244 +		{
  1.1245 +		// Close Chunk
  1.1246 +		iChunkInfo->Close();
  1.1247 +		// ChunkInfo will delete itself with DFC, but the pointer here is no longer needed.		
  1.1248 +		iChunkInfo=NULL;
  1.1249 +		}
  1.1250 +	__KTRACE_OPT(KUSB, Kern::Printf("about to SafeClose"));
  1.1251 +	Kern::SafeClose((DObject*&)iClient, NULL);
  1.1252 +	}
  1.1253 +
  1.1254 +
  1.1255 +//
  1.1256 +// DoCreate - Create channel
  1.1257 +//
  1.1258 +
  1.1259 +TInt DLddUsbcScChannel::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& aVer)
  1.1260 +	{
  1.1261 +	__KTRACE_OPT(KUSB, Kern::Printf("LDD DoCreateL 1 Ver = %02d %02d %02d",
  1.1262 +									aVer.iMajor, aVer.iMinor, aVer.iBuild));
  1.1263 +	if (!Kern::CurrentThreadHasCapability(ECapabilityCommDD,
  1.1264 +										  __PLATSEC_DIAGNOSTIC_STRING("Checked by USBCSC.LDD (USB Driver)")))
  1.1265 +		{
  1.1266 +		return KErrPermissionDenied;
  1.1267 +		}
  1.1268 +
  1.1269 +	iController = DUsbClientController::UsbcControllerPointer();
  1.1270 +
  1.1271 +	if (!iController)
  1.1272 +		{
  1.1273 +		return KErrGeneral;
  1.1274 +		}
  1.1275 +
  1.1276 +	iStatusFifo = new TUsbcDeviceStatusQueue;
  1.1277 +	if (iStatusFifo == NULL)
  1.1278 +		{
  1.1279 +		return KErrNoMemory;
  1.1280 +		}
  1.1281 +
  1.1282 +  	if (!Kern::QueryVersionSupported(TVersion(KUsbcScMajorVersion, KUsbcScMinorVersion, KUsbcScBuildVersion), aVer))
  1.1283 +		{
  1.1284 +		return KErrNotSupported;
  1.1285 +		}
  1.1286 +
  1.1287 +	// set up the correct DFC queue
  1.1288 +	SetDfcQ(iController->DfcQ(0));							// sets the channel's dfc queue
  1.1289 +    iCompleteAllCallbackInfo.SetDfcQ(iDfcQ);
  1.1290 +	iStatusCallbackInfo.SetDfcQ(iDfcQ);						// use the channel's dfcq for this dfc
  1.1291 +	iEndpointStatusCallbackInfo.SetDfcQ(iDfcQ);				// use the channel's dfcq for this dfc
  1.1292 +	iOtgFeatureCallbackInfo.SetDfcQ(iDfcQ);
  1.1293 +	iMsgQ.Receive();										//start up the message q
  1.1294 +	TInt r = iController->RegisterClientCallback(iCompleteAllCallbackInfo);
  1.1295 +	if (r != KErrNone)
  1.1296 +		return r;
  1.1297 +	r = iController->RegisterForStatusChange(iStatusCallbackInfo);
  1.1298 +	if (r != KErrNone)
  1.1299 +		return r;
  1.1300 +	r = iController->RegisterForEndpointStatusChange(iEndpointStatusCallbackInfo);
  1.1301 +	if (r != KErrNone)
  1.1302 +		return r;
  1.1303 +	r = iController->RegisterForOtgFeatureChange(iOtgFeatureCallbackInfo);
  1.1304 +	if (r != KErrNone)
  1.1305 +		return r;
  1.1306 +
  1.1307 +	return r;
  1.1308 +	}
  1.1309 +// end DoCreate.
  1.1310 +
  1.1311 +
  1.1312 +//
  1.1313 +// HandleMsg
  1.1314 +//
  1.1315 +// Events from userside arrive here, and delegated to either DoRequest, DoControl or DoCancel.
  1.1316 +//
  1.1317 +
  1.1318 +void DLddUsbcScChannel::HandleMsg(TMessageBase* aMsg)
  1.1319 +	{
  1.1320 +	TThreadMessage& m = *(TThreadMessage*)aMsg;
  1.1321 +	TInt id = m.iValue;
  1.1322 +	__KTRACE_OPT(KUSB, Kern::Printf("HandleMsg 0x%x", id));
  1.1323 +
  1.1324 +	if (id == (TInt) ECloseMsg)
  1.1325 +		{
  1.1326 +		iChannelClosing = ETrue;
  1.1327 +		m.Complete(KErrNone, EFalse);
  1.1328 +		return;
  1.1329 +		}
  1.1330 +
  1.1331 +	TInt r;
  1.1332 +	if (id < 0)
  1.1333 +		{
  1.1334 +		// DoRequest
  1.1335 +		TRequestStatus* pS = (TRequestStatus*) m.Ptr0();
  1.1336 +		r = DoRequest(~id, pS, m.Ptr1(), m.Ptr2());
  1.1337 +		m.Complete(r, ETrue);
  1.1338 +		}
  1.1339 +	else if (id & RDevUsbcScClient::ERequestCancel)
  1.1340 +		{
  1.1341 +		// DoCancel
  1.1342 +		r = DoCancel(id, (TUint) m.Ptr0(), (TUint) m.Ptr1());
  1.1343 +		m.Complete(r, ETrue);
  1.1344 +	}
  1.1345 +	else
  1.1346 +		{
  1.1347 +		// DoControl
  1.1348 +		r = DoControl(id, m.Ptr0(), m.Ptr1());
  1.1349 +		m.Complete(r, ETrue);
  1.1350 +		}
  1.1351 +	}
  1.1352 +// end HandleMsg.
  1.1353 +
  1.1354 +
  1.1355 +#define BREAK_IF_NULL_ARG(a,r) if (a==NULL) { r = KErrArgument; __KTRACE_OPT(KUSB,Kern::Printf("NULL Argument")); break; }
  1.1356 +
  1.1357 +//
  1.1358 +// DoRequest - Asynchronous requests
  1.1359 +//
  1.1360 +// Overrides pure virtual, called by HandleMsg. (Above)
  1.1361 +//
  1.1362 +TInt DLddUsbcScChannel::DoRequest(TInt aReqNo, TRequestStatus* aStatus, TAny* a1, TAny* a2)
  1.1363 +	{
  1.1364 +	TInt reqNo = aReqNo & RDevUsbcScClient::KFieldIdMask;
  1.1365 +	TInt r = KErrNone;  // return via request notify
  1.1366 +	TBool needsCompletion =EFalse;
  1.1367 +
  1.1368 +	__KTRACE_OPT(KUSB, Kern::Printf("DoRequest 0x%08x", aReqNo));
  1.1369 +
  1.1370 +	if ((reqNo>RDevUsbcScClient::ERequestReadDataNotify) &&
  1.1371 +		(reqNo<RDevUsbcScClient::ERequestMaxRequests))
  1.1372 +		{
  1.1373 +		if (iRequestStatus[reqNo])
  1.1374 +			{
  1.1375 +			PanicClientThread(ERequestAlreadyPending);
  1.1376 +			return 0;
  1.1377 +			}
  1.1378 +		iRequestStatus[reqNo] = aStatus;
  1.1379 +		}
  1.1380 +
  1.1381 +	switch (reqNo)
  1.1382 +		{
  1.1383 +	case RDevUsbcScClient::ERequestWriteData:
  1.1384 +		{
  1.1385 +		TInt buffer =  (aReqNo>>RDevUsbcScClient::KFieldBuffPos)&RDevUsbcScClient::KFieldBuffMask;
  1.1386 +		__KTRACE_OPT(KUSB, Kern::Printf("ERequestWriteData"));
  1.1387 +		BREAK_IF_NULL_ARG(a2,r);
  1.1388 +
  1.1389 +		r = DoWriteData( aStatus, buffer, (TInt) a1 /*Start*/, (TInt) a2 /* Length */,
  1.1390 +						 aReqNo>>RDevUsbcScClient::KFieldFlagsPos ); // Flags
  1.1391 +		break;
  1.1392 +		}
  1.1393 +	case RDevUsbcScClient::ERequestReadDataNotify:
  1.1394 +		{
  1.1395 +		__KTRACE_OPT(KUSB, Kern::Printf("ERequestReadDataNotify"));
  1.1396 +		return DoReadDataNotify(aStatus, (TInt) a1, (TInt) a2); // a1 = aBufferNumber, a2 - aLength;
  1.1397 +		} 
  1.1398 +
  1.1399 +	case RDevUsbcScClient::ERequestAlternateDeviceStatusNotify:
  1.1400 +		{
  1.1401 +		__KTRACE_OPT(KUSB, Kern::Printf("ERequestAlternateDeviceStatusNotify"));
  1.1402 +		BREAK_IF_NULL_ARG(a1,r);
  1.1403 +		iDeviceStatusNeeded = ETrue;
  1.1404 +		iStatusChangePtr = a1;
  1.1405 +		needsCompletion = AlternateDeviceStateTestComplete();
  1.1406 +		break;
  1.1407 +		}
  1.1408 +	case RDevUsbcScClient::ERequestReEnumerate:
  1.1409 +		{
  1.1410 +		__KTRACE_OPT(KUSB, Kern::Printf("ERequestReEnumerate"));
  1.1411 +		// If successful, this will complete via the status notification.
  1.1412 +		r = iController->ReEnumerate();
  1.1413 +		break;
  1.1414 +		}
  1.1415 +	case RDevUsbcScClient::ERequestEndpointStatusNotify:
  1.1416 +		{
  1.1417 +		__KTRACE_OPT(KUSB, Kern::Printf("ERequestEndpointStatusNotify"));
  1.1418 +		BREAK_IF_NULL_ARG(a1,r);
  1.1419 +		
  1.1420 +		iEndpointStatusChangePtr = a1;
  1.1421 +		break;
  1.1422 +		}
  1.1423 +	case RDevUsbcScClient::ERequestOtgFeaturesNotify:
  1.1424 +		{
  1.1425 +		__KTRACE_OPT(KUSB, Kern::Printf("ERequestOtgFeaturesNotify"));
  1.1426 +		BREAK_IF_NULL_ARG(a1,r);
  1.1427 +			
  1.1428 +		iOtgFeatureChangePtr = a1;
  1.1429 +		break;
  1.1430 +		}
  1.1431 +    default:
  1.1432 +		r = KErrNotSupported;
  1.1433 +		}
  1.1434 +
  1.1435 +	if ((needsCompletion) || (r != KErrNone))
  1.1436 +		{
  1.1437 +		iRequestStatus[reqNo] = aStatus;
  1.1438 +		Kern::RequestComplete(iClient, iRequestStatus[reqNo], r);
  1.1439 +		}
  1.1440 +	return KErrNone;
  1.1441 +	}
  1.1442 +// end DoRequest.
  1.1443 +
  1.1444 +
  1.1445 +//
  1.1446 +// DoReadDataNotify
  1.1447 +//
  1.1448 +// This method sets up the request to facilitate the userside being notifed when new data has been read.
  1.1449 +//
  1.1450 +TInt DLddUsbcScChannel::DoReadDataNotify(TRequestStatus* aStatus, TInt aBufferNum, TInt aLength)
  1.1451 +	{
  1.1452 +	__KTRACE_OPT(KUSB, Kern::Printf(" DLddUsbcScChannel::DoReadDataNotify(x, %d, 0x%x)", aBufferNum, aLength));
  1.1453 +	TInt r = KErrNone;
  1.1454 +	// check range
  1.1455 +	if ((aBufferNum<0) ||  (aBufferNum>=iNumBuffers))  // Indirectly checks that we are set up.
  1.1456 +		{
  1.1457 +		if (aBufferNum!=KUsbcScEndpointZero)
  1.1458 +			{
  1.1459 +	        __KTRACE_OPT(KUSB, Kern::Printf(" DLddUsbcScChannel::DoReadDataNotify : Bad Buffer Number!"));
  1.1460 +			return KErrArgument;
  1.1461 +			}
  1.1462 +		else
  1.1463 +			{
  1.1464 +			aBufferNum = iEP0OutBuff;
  1.1465 +			}
  1.1466 +		}
  1.1467 +	else
  1.1468 +		{
  1.1469 +		// check direction
  1.1470 +		if (iBuffers[aBufferNum].iDirection!=KUsbcScOut)
  1.1471 +			{
  1.1472 +   		     __KTRACE_OPT(KUSB, Kern::Printf(" DLddUsbcScChannel::DoReadDataNotify : Bad Buffer Direction!"));
  1.1473 +			return KErrNotSupported;
  1.1474 +			}
  1.1475 +		if (!Configured())
  1.1476 +			return KErrUsbInterfaceNotReady;
  1.1477 +		}
  1.1478 +	SUsbcScBufferHeader* scBuffer = (SUsbcScBufferHeader*) iBuffers[aBufferNum].iBufferStart;
  1.1479 +
  1.1480 +	__KTRACE_OPT(KUSB, Kern::Printf(" DLddUsbcScChannel::DoReadDataNotify  head %x tail %x", iBuffers[aBufferNum].iHead , scBuffer->iTail ));
  1.1481 +
  1.1482 +	if (iBuffers[aBufferNum].iHead != scBuffer->iBilTail)
  1.1483 +		r = KErrCompletion;
  1.1484 +	else
  1.1485 +		if (iBuffers[aBufferNum].iStalled)
  1.1486 +			{
  1.1487 +			iBuffers[aBufferNum].PopStall();
  1.1488 +			return KErrCompletion;
  1.1489 +			}
  1.1490 +		else
  1.1491 +			r = iBuffers[aBufferNum].iStatusList.Add(aStatus, aLength, 0,0);
  1.1492 +
  1.1493 +	if (iBuffers[aBufferNum].iStatusList.iState==ENotRunning)
  1.1494 +		{
  1.1495 +		iBuffers[aBufferNum].StartDataRead();
  1.1496 +		}
  1.1497 +	else
  1.1498 +		{
  1.1499 +		__KTRACE_OPT(KUSB, Kern::Printf("Job in Progress!"));
  1.1500 +		}
  1.1501 +	return r;
  1.1502 +	}
  1.1503 +// end DoReadDataNotify.
  1.1504 +
  1.1505 +
  1.1506 +
  1.1507 +//
  1.1508 +// DoWriteData
  1.1509 +//
  1.1510 +// This method sets up the request to write data to USB from userside.
  1.1511 +//
  1.1512 +TInt DLddUsbcScChannel::DoWriteData(TRequestStatus* aStatus,TInt aBufferNum, TUint aStart, TUint aLength, TUint aFlags)
  1.1513 +	{
  1.1514 +	__KTRACE_OPT(KUSB, Kern::Printf(" DLddUsbcScChannel::DoWriteData(%d, 0x%x, 0x%x, 0x%x)",  aBufferNum, aStart, aLength, aFlags));
  1.1515 +	if (!iUserKnowsAltSetting)
  1.1516 +		return KErrEof;
  1.1517 +	// Check Buffer Number
  1.1518 +	if ((aBufferNum<0) ||  (aBufferNum>=iNumBuffers))
  1.1519 +		{
  1.1520 +		if ((TUint)aBufferNum!=RDevUsbcScClient::KFieldBuffMask)  // KUsbcScEndpointZero & KFieldBuffMas = KFieldBuffMas;
  1.1521 +			{
  1.1522 +	        __KTRACE_OPT(KUSB, Kern::Printf(" DLddUsbcScChannel::DoWriteData : Bad Buffer Number!"));
  1.1523 +			return KErrArgument;
  1.1524 +			}
  1.1525 +		else
  1.1526 +			{
  1.1527 +			aBufferNum = iEP0InBuff;
  1.1528 +			}
  1.1529 +		}
  1.1530 +	else
  1.1531 +		{
  1.1532 +		// check direction
  1.1533 +		if (iBuffers[aBufferNum].iDirection!=KUsbcScIn)
  1.1534 +			{
  1.1535 +	    	    __KTRACE_OPT(KUSB, Kern::Printf(" DLddUsbcScChannel::DoWriteData Bad endpoint Direction"));
  1.1536 +				return KErrArgument;
  1.1537 +			}
  1.1538 +		}
  1.1539 +
  1.1540 +	TUsbcScBuffer& buf=iBuffers[aBufferNum];
  1.1541 +
  1.1542 +	if ((aStart< (((TLinAddr) buf.iBufferStart)-buf.iChunkAddr)) || ((aStart+aLength)>iBuffers[aBufferNum].iBufferEnd))
  1.1543 +		{
  1.1544 +		__KTRACE_OPT(KUSB, Kern::Printf(" DLddUsbcScChannel::DoWriteData Bad Range aStart or aLength 0x%x > 0x%x + 0x%x < 0x%x", (((TLinAddr) buf.iBufferStart)-buf.iChunkAddr),aStart, aLength, iBuffers[aBufferNum].iBufferEnd ));
  1.1545 +		return KErrArgument;
  1.1546 +		}
  1.1547 +
  1.1548 +	if ( (aBufferNum != iEP0InBuff) && !Configured())
  1.1549 +		return KErrUsbInterfaceNotReady;
  1.1550 +
  1.1551 +	if (aStart & ~buf.iAlignMask)
  1.1552 +		{
  1.1553 +		__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScBuffer::DoDataWrite: address 0x%x unaligned.",aStart));
  1.1554 +		return KErrArgument;
  1.1555 +		}
  1.1556 +			
  1.1557 +	TInt r = iBuffers[aBufferNum].iStatusList.Add(aStatus, aLength, aStart, aFlags); //update
  1.1558 +
  1.1559 +	if (iBuffers[aBufferNum].iStatusList.iState==ENotRunning)
  1.1560 +		{
  1.1561 +			iBuffers[aBufferNum].StartDataWrite();
  1.1562 +		}
  1.1563 +	else
  1.1564 +		{	
  1.1565 +		__KTRACE_OPT(KUSB, Kern::Printf("Job in Progress!"));
  1.1566 +		}
  1.1567 +
  1.1568 +
  1.1569 +	return r;
  1.1570 +	}
  1.1571 +// end DoWriteData.
  1.1572 +
  1.1573 +
  1.1574 +//
  1.1575 +// Cancel an outstanding request						// Cancel need reworking.
  1.1576 +//
  1.1577 +TInt DLddUsbcScChannel::DoCancel(TInt aReqNo, TUint aBuff, TUint aSpair)
  1.1578 +	{
  1.1579 +	TInt r = KErrNone;
  1.1580 +	TInt direction=KUsbcScOut;
  1.1581 +
  1.1582 +	__KTRACE_OPT(KUSB, Kern::Printf("DoCancel: 0x%x aBuff 0x%x", aReqNo, aBuff));
  1.1583 +	switch (aReqNo)
  1.1584 +		{
  1.1585 +	case RDevUsbcScClient::ERequestCancel:
  1.1586 +		TInt buffer;
  1.1587 +		TInt mask;
  1.1588 +
  1.1589 +		for (buffer=1, mask=1; buffer<iNumBuffers; buffer++,mask<<=1)
  1.1590 +			if (aBuff&mask)
  1.1591 +				iBuffers[buffer].Cancel(KErrCancel);
  1.1592 +
  1.1593 +		return KErrNone;
  1.1594 +
  1.1595 +	// coverity[missing_break]
  1.1596 +	case RDevUsbcScClient::ERequestWriteDataCancel:
  1.1597 +		direction = KUsbcScIn;
  1.1598 +	case RDevUsbcScClient::ERequestReadDataNotifyCancel:
  1.1599 +		__KTRACE_OPT(KUSB, Kern::Printf("DoCancel Direction %d endpoints: 0x%x",direction, aReqNo));
  1.1600 +
  1.1601 +		if (((TInt)aBuff)==KUsbcScEndpointZero) // EP0 is bi-directional, so pick correct buffer for call type
  1.1602 +			{
  1.1603 +			__KTRACE_OPT(KUSB, Kern::Printf("DoCancel Cancel Endpoint 0/%d",direction));
  1.1604 +			iEp0Endpoint->AbortTransfer();
  1.1605 +			if (direction==KUsbcScIn)
  1.1606 +				aBuff=iEP0InBuff;
  1.1607 +			else
  1.1608 +				aBuff=iEP0OutBuff;
  1.1609 +			} 
  1.1610 +		else if ((TInt)aBuff >= iNumBuffers) // check buff no range.
  1.1611 +			{
  1.1612 +			__KTRACE_OPT(KUSB, Kern::Printf("DoCancel Error: Bad buffer number"));
  1.1613 +			return KErrArgument;
  1.1614 +			}
  1.1615 +
  1.1616 +		if ((iBuffers[aBuff].iDirection&1)!=direction) // Does direction match call type?
  1.1617 +			{
  1.1618 +			__KTRACE_OPT(KUSB, Kern::Printf("DoCancel Error: Bad buffer direction"));
  1.1619 +			return KErrArgument;
  1.1620 +			}	
  1.1621 +		iBuffers[aBuff].iStatusList.CancelQueued();
  1.1622 +		iBuffers[aBuff].Cancel(KErrCancel);
  1.1623 +		
  1.1624 +		return KErrNone;
  1.1625 +
  1.1626 +	case RDevUsbcScClient::ERequestAlternateDeviceStatusNotifyCancel:
  1.1627 +		__KTRACE_OPT(KUSB, Kern::Printf("DoCancel: ERequestAlternateDeviceStatusNotify 0x%x", aReqNo));
  1.1628 +		iDeviceStatusNeeded = EFalse;
  1.1629 +		iStatusFifo->FlushQueue();
  1.1630 +		if (iStatusChangePtr)
  1.1631 +			{
  1.1632 +			TInt deviceState = iController->GetDeviceStatus();
  1.1633 +			r = Kern::ThreadRawWrite(iClient, iStatusChangePtr, &deviceState, sizeof(deviceState), iClient);
  1.1634 +			if (r != KErrNone)
  1.1635 +				PanicClientThread(r);
  1.1636 +			iStatusChangePtr = NULL; 
  1.1637 +			}
  1.1638 +	break;
  1.1639 +
  1.1640 +	case RDevUsbcScClient::ERequestReEnumerateCancel:
  1.1641 +		__KTRACE_OPT(KUSB, Kern::Printf("DoCancel ERequestReEnumerate: 0x%x", aReqNo));
  1.1642 +	break;
  1.1643 +
  1.1644 +	case RDevUsbcScClient::ERequestEndpointStatusNotifyCancel:
  1.1645 +		__KTRACE_OPT(KUSB, Kern::Printf("DoCancel ERequestEndpointStatusNotify: 0x%x", aReqNo));
  1.1646 +		CancelNotifyEndpointStatus();
  1.1647 +	break;
  1.1648 +
  1.1649 + 	case RDevUsbcScClient::ERequestOtgFeaturesNotifyCancel:
  1.1650 +		__KTRACE_OPT(KUSB, Kern::Printf("DoCancel ERequestOtgFeaturesNotify: 0x%x", aReqNo));
  1.1651 +		CancelNotifyOtgFeatures();
  1.1652 +	break;
  1.1653 +
  1.1654 +	default:
  1.1655 +		__KTRACE_OPT(KUSB, Kern::Printf("DoCancel Unknown! 0x%x", aReqNo));
  1.1656 +		return KErrArgument;
  1.1657 +		}
  1.1658 +
  1.1659 +	Kern::RequestComplete(iClient,iRequestStatus[aReqNo & ~RDevUsbcScClient::ERequestCancel], KErrCancel);
  1.1660 +	return r;
  1.1661 +	}
  1.1662 +
  1.1663 +
  1.1664 +void DLddUsbcScChannel::CancelNotifyEndpointStatus()
  1.1665 +	{
  1.1666 +	if (iEndpointStatusChangePtr)
  1.1667 +		{
  1.1668 +		TUint epBitmap = 0;
  1.1669 +		for (TInt i = 1; i <= iNumberOfEndpoints; i++)
  1.1670 +			{
  1.1671 +			TInt v = iController->GetEndpointStatus(this, iEndpoint[i]->RealEpNumber());
  1.1672 +			TUint b;
  1.1673 +			(v == EEndpointStateStalled) ? b = 1 : b = 0;
  1.1674 +			epBitmap |= b << i;
  1.1675 +			}
  1.1676 +		TInt r=Kern::ThreadRawWrite(iClient, iEndpointStatusChangePtr, (TUint8*) &epBitmap, sizeof(epBitmap), iClient);
  1.1677 +		if (r != KErrNone)
  1.1678 +			PanicClientThread(r);
  1.1679 +		iEndpointStatusChangePtr = NULL;
  1.1680 +		}
  1.1681 +	}
  1.1682 +
  1.1683 +void DLddUsbcScChannel::CancelNotifyOtgFeatures()
  1.1684 +	{
  1.1685 +    if (iOtgFeatureChangePtr)
  1.1686 +        {
  1.1687 +        TUint8 features;
  1.1688 +        iController->GetCurrentOtgFeatures(features);
  1.1689 +		TInt r=Kern::ThreadRawWrite(iClient, iOtgFeatureChangePtr, (TUint8*)&features, sizeof(features), iClient);
  1.1690 +		if (r != KErrNone)
  1.1691 +			PanicClientThread(r);
  1.1692 +        iOtgFeatureChangePtr = NULL;
  1.1693 +        }
  1.1694 +    }
  1.1695 +
  1.1696 +
  1.1697 +
  1.1698 +//
  1.1699 +// DoControl - Synchronous requests
  1.1700 +//
  1.1701 +// Called from HandleMsg.
  1.1702 +
  1.1703 +TInt DLddUsbcScChannel::DoControl(TInt aFunction, TAny* a1, TAny* a2)
  1.1704 +	{
  1.1705 +	__KTRACE_OPT(KUSB, Kern::Printf("DoControl: %d", aFunction));
  1.1706 +
  1.1707 +	TInt r = KErrNone;
  1.1708 +	TInt ep, param;
  1.1709 +	TUsbcScEndpoint* pEndpoint;
  1.1710 +	TPtrC8 pZeroDesc(NULL, 0);
  1.1711 +	TEndpointDescriptorInfo epInfo;
  1.1712 +	TUsbcScIfcInfo ifcInfo;
  1.1713 +	TCSDescriptorInfo desInfo;
  1.1714 +	TUsbcEndpointResource epRes;
  1.1715 +
  1.1716 +	switch (aFunction)
  1.1717 +		{
  1.1718 +	case RDevUsbcScClient::EControlEndpointZeroRequestError:
  1.1719 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlEndpointZeroRequestError"));
  1.1720 +		r = KErrNone;
  1.1721 +		if (iOwnsDeviceControl || (iValidInterface && iDeviceState == EUsbcDeviceStateConfigured))
  1.1722 +			{
  1.1723 +			iController->Ep0Stall(this);
  1.1724 +			}
  1.1725 +		else
  1.1726 +			{
  1.1727 +			if (iDeviceState != EUsbcDeviceStateConfigured)
  1.1728 +				r = KErrUsbDeviceNotConfigured;
  1.1729 +			else
  1.1730 +				r = KErrUsbInterfaceNotReady;
  1.1731 +			}
  1.1732 +		break;
  1.1733 +
  1.1734 +	case RDevUsbcScClient::EControlGetAlternateSetting:
  1.1735 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetAlternateSetting"));
  1.1736 +		if (iValidInterface && iDeviceState == EUsbcDeviceStateConfigured)
  1.1737 +			{
  1.1738 +			r = iController->GetInterfaceNumber(this, param);
  1.1739 +			if (r == KErrNone)
  1.1740 +				{
  1.1741 +				r = Kern::ThreadRawWrite(iClient, a1, &param, sizeof(param), iClient);
  1.1742 +				if (r != KErrNone)
  1.1743 +					PanicClientThread(r);
  1.1744 +				}
  1.1745 +			}
  1.1746 +		else
  1.1747 +			{
  1.1748 +			if (iDeviceState != EUsbcDeviceStateConfigured)
  1.1749 +				r = KErrUsbDeviceNotConfigured;
  1.1750 +			else
  1.1751 +				r = KErrUsbInterfaceNotReady;
  1.1752 +			}
  1.1753 +		break;
  1.1754 +
  1.1755 +	case RDevUsbcScClient::EControlDeviceStatus:
  1.1756 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlDeviceStatus"));
  1.1757 +		param = iController->GetDeviceStatus();
  1.1758 +		r = Kern::ThreadRawWrite(iClient, a1, &param, sizeof(param), iClient);
  1.1759 +		if (r != KErrNone)
  1.1760 +			PanicClientThread(r);
  1.1761 +		break;
  1.1762 +
  1.1763 +	case RDevUsbcScClient::EControlEndpointStatus:
  1.1764 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlEndpointStatus"));
  1.1765 +		if (iValidInterface && ValidEndpoint((TInt) a1))
  1.1766 +			{
  1.1767 +			pEndpoint = iEndpoint[(TInt)a1];
  1.1768 +			if (pEndpoint == NULL)
  1.1769 +				r = KErrNotSupported;
  1.1770 +			else
  1.1771 +				{
  1.1772 +				param = iController->GetEndpointStatus(this, iEndpoint[(TInt)a1]->RealEpNumber());
  1.1773 +				r = Kern::ThreadRawWrite(iClient, a2, &param, sizeof(param), iClient);
  1.1774 +				if (r != KErrNone)
  1.1775 +					PanicClientThread(r);
  1.1776 +				}
  1.1777 +			}
  1.1778 +		else
  1.1779 +			{
  1.1780 +			if (iDeviceState != EUsbcDeviceStateConfigured)
  1.1781 +				r = KErrUsbDeviceNotConfigured;
  1.1782 +			else
  1.1783 +				r = KErrUsbInterfaceNotReady;
  1.1784 +			}
  1.1785 +		break;
  1.1786 +
  1.1787 +	case RDevUsbcScClient::EControlEndpointCaps:
  1.1788 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlEndpointCaps"));
  1.1789 +		r = Kern::ThreadDesWrite(iClient, a1, pZeroDesc, 0, 0, iClient);
  1.1790 +		if (r != KErrNone)
  1.1791 +			PanicClientThread(r);
  1.1792 +		iController->EndpointCaps(this, *((TDes8*) a1));
  1.1793 +		break;
  1.1794 +
  1.1795 +	case RDevUsbcScClient::EControlDeviceCaps:
  1.1796 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlDeviceCaps"));
  1.1797 +		r = Kern::ThreadDesWrite(iClient, a1, pZeroDesc, 0, 0, iClient);
  1.1798 +		if (r != KErrNone)
  1.1799 +			PanicClientThread(r);
  1.1800 +		iController->DeviceCaps(this, *((TDes8*) a1));
  1.1801 +		break;
  1.1802 +
  1.1803 +	case RDevUsbcScClient::EControlSendEp0StatusPacket:
  1.1804 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSendEp0StatusPacket"));
  1.1805 +		iController->SendEp0StatusPacket(this);
  1.1806 +		break;
  1.1807 +
  1.1808 +	case RDevUsbcScClient::EControlHaltEndpoint:
  1.1809 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlHaltEndpoint"));
  1.1810 +		if (iValidInterface && ValidEndpoint((TInt) a1))
  1.1811 +			{
  1.1812 +			r = iController->HaltEndpoint(this, iEndpoint[(TInt)a1]->RealEpNumber());
  1.1813 +			}
  1.1814 +		else
  1.1815 +			{
  1.1816 +			if (iDeviceState != EUsbcDeviceStateConfigured)
  1.1817 +				r = KErrUsbDeviceNotConfigured;
  1.1818 +			else
  1.1819 +				r = KErrUsbInterfaceNotReady;
  1.1820 +			}
  1.1821 +		break;
  1.1822 +
  1.1823 +	case RDevUsbcScClient::EControlClearHaltEndpoint:
  1.1824 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlClearHaltEndpoint"));
  1.1825 +		if (iValidInterface && ValidEndpoint((TInt) a1))
  1.1826 +			{
  1.1827 +			r = iController->ClearHaltEndpoint(this, iEndpoint[(TInt)a1]->RealEpNumber());
  1.1828 +			}
  1.1829 +		else
  1.1830 +			{
  1.1831 +			if (iDeviceState != EUsbcDeviceStateConfigured)
  1.1832 +				r = KErrUsbDeviceNotConfigured;
  1.1833 +			else
  1.1834 +				r = KErrUsbInterfaceNotReady;
  1.1835 +			}
  1.1836 +		break;
  1.1837 +
  1.1838 +	case RDevUsbcScClient::EControlDumpRegisters:
  1.1839 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlDumpRegisters"));
  1.1840 +		iController->DumpRegisters();
  1.1841 +		break;
  1.1842 +
  1.1843 +	case RDevUsbcScClient::EControlReleaseDeviceControl:
  1.1844 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlReleaseDeviceControl"));
  1.1845 +		iController->ReleaseDeviceControl(this);
  1.1846 +		iOwnsDeviceControl = EFalse;
  1.1847 +		break;
  1.1848 +
  1.1849 +	case RDevUsbcScClient::EControlEndpointZeroMaxPacketSizes:
  1.1850 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlEndpointZeroMaxPacketSizes"));
  1.1851 +		r = iController->EndpointZeroMaxPacketSizes();
  1.1852 +		break;
  1.1853 +
  1.1854 +	case RDevUsbcScClient::EControlSetEndpointZeroMaxPacketSize:
  1.1855 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetEndpointZeroMaxPacketSize"));
  1.1856 +		r = iController->SetEndpointZeroMaxPacketSize(reinterpret_cast<TInt>(a1));
  1.1857 +		break;
  1.1858 +
  1.1859 +	case RDevUsbcScClient::EControlGetEndpointZeroMaxPacketSize:
  1.1860 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetEndpointZeroMaxPacketSize"));
  1.1861 +		r = iController->Ep0PacketSize();
  1.1862 +		break;
  1.1863 +
  1.1864 +	case RDevUsbcScClient::EControlGetDeviceDescriptor:
  1.1865 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetDeviceDescriptor"));
  1.1866 +		r = Kern::ThreadDesWrite(iClient, a1, pZeroDesc, 0, 0, iClient);
  1.1867 +		if (r != KErrNone)
  1.1868 +			PanicClientThread(r);
  1.1869 +		r = iController->GetDeviceDescriptor(iClient, *((TDes8*) a1));
  1.1870 +		break;
  1.1871 +
  1.1872 +	case RDevUsbcScClient::EControlSetDeviceDescriptor:
  1.1873 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetDeviceDescriptor"));
  1.1874 +		BREAK_IF_NULL_ARG(a1,r);
  1.1875 +		r = iController->SetDeviceDescriptor(iClient, *((TDes8*) a1));
  1.1876 +		break;
  1.1877 +
  1.1878 +	case RDevUsbcScClient::EControlGetDeviceDescriptorSize:
  1.1879 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetDeviceDescriptorSize"));
  1.1880 +		BREAK_IF_NULL_ARG(a1,r);
  1.1881 +		r = iController->GetDeviceDescriptorSize(iClient, *((TDes8*) a1));
  1.1882 +		break;
  1.1883 +
  1.1884 +	case RDevUsbcScClient::EControlGetConfigurationDescriptor:
  1.1885 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetConfigurationDescriptor"));
  1.1886 +		r = Kern::ThreadDesWrite(iClient, a1, pZeroDesc, 0 , 0, iClient);
  1.1887 +		if (r != KErrNone)
  1.1888 +			PanicClientThread(r);
  1.1889 +		r = iController->GetConfigurationDescriptor(iClient, *((TDes8*) a1));
  1.1890 +		break;
  1.1891 +
  1.1892 +	case RDevUsbcScClient::EControlGetConfigurationDescriptorSize:
  1.1893 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetConfigurationDescriptorSize"));
  1.1894 +		if (a1 != NULL)
  1.1895 +			{
  1.1896 +			r = iController->GetConfigurationDescriptorSize(iClient, *((TDes8*) a1));
  1.1897 +			}
  1.1898 +		else
  1.1899 +			r = KErrArgument;
  1.1900 +		break;
  1.1901 +
  1.1902 +	case RDevUsbcScClient::EControlSetConfigurationDescriptor:
  1.1903 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetConfigurationDescriptor"));
  1.1904 +		r = iController->SetConfigurationDescriptor(iClient, *((TDes8*) a1));
  1.1905 +		break;
  1.1906 +
  1.1907 +	case RDevUsbcScClient::EControlGetInterfaceDescriptor:
  1.1908 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetInterfaceDescriptor"));
  1.1909 +		r = iController->GetInterfaceDescriptor(iClient, this, (TInt) a1, *((TDes8*) a2));
  1.1910 +		break;
  1.1911 +
  1.1912 +	case RDevUsbcScClient::EControlGetInterfaceDescriptorSize:
  1.1913 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetInterfaceDescriptorSize"));
  1.1914 +		r = iController->GetInterfaceDescriptorSize(iClient, this, (TInt) a1, *(TDes8*) a2);
  1.1915 +		break;
  1.1916 +
  1.1917 +	case RDevUsbcScClient::EControlSetInterfaceDescriptor:
  1.1918 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetInterfaceDescriptor"));
  1.1919 +		r = iController->SetInterfaceDescriptor(iClient, this, (TInt) a1, *((TDes8*) a2));
  1.1920 +		break;
  1.1921 +
  1.1922 +	case RDevUsbcScClient::EControlGetEndpointDescriptor:
  1.1923 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetEndpointDescriptor"));
  1.1924 +		r = Kern::ThreadRawRead(iClient, a1, &epInfo, sizeof(epInfo));
  1.1925 +		if (r != KErrNone)
  1.1926 +			PanicClientThread(r);
  1.1927 +		ep = EpFromAlternateSetting(epInfo.iSetting, epInfo.iEndpoint);
  1.1928 +		r = (ep<0)?ep:iController->GetEndpointDescriptor(iClient, this, epInfo.iSetting,
  1.1929 +											   ep, *(TDes8*) epInfo.iArg);
  1.1930 +		break;
  1.1931 +
  1.1932 +	case RDevUsbcScClient::EControlGetEndpointDescriptorSize:
  1.1933 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetEndpointDescriptorSize"));
  1.1934 +		r = Kern::ThreadRawRead(iClient, a1, &epInfo, sizeof(epInfo));
  1.1935 +		if (r != KErrNone)
  1.1936 +			PanicClientThread(r);
  1.1937 +		ep = EpFromAlternateSetting(epInfo.iSetting, epInfo.iEndpoint);
  1.1938 +		r = iController->GetEndpointDescriptorSize(iClient, this, epInfo.iSetting,
  1.1939 +												   ep, *(TDes8*) epInfo.iArg);
  1.1940 +		break;
  1.1941 +
  1.1942 +	case RDevUsbcScClient::EControlSetEndpointDescriptor:
  1.1943 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetEndpointDescriptor"));
  1.1944 +		r = Kern::ThreadRawRead(iClient, a1, &epInfo, sizeof(epInfo));
  1.1945 +		if (r != KErrNone)
  1.1946 +			PanicClientThread(r);
  1.1947 +		ep = EpFromAlternateSetting(epInfo.iSetting, epInfo.iEndpoint);
  1.1948 +		r = iController->SetEndpointDescriptor(iClient, this, epInfo.iSetting,
  1.1949 +											   ep, *(TDes8*)epInfo.iArg);
  1.1950 +		break;
  1.1951 +
  1.1952 +	case RDevUsbcScClient::EControlGetDeviceQualifierDescriptor:
  1.1953 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetDeviceQualifierDescriptor"));
  1.1954 +		r = Kern::ThreadDesWrite(iClient, a1, pZeroDesc, 0, 0, iClient);
  1.1955 +		if (r != KErrNone)
  1.1956 +			PanicClientThread(r);
  1.1957 +		r = iController->GetDeviceQualifierDescriptor(iClient, *((TDes8*) a1));
  1.1958 +		break;
  1.1959 +
  1.1960 +	case RDevUsbcScClient::EControlSetDeviceQualifierDescriptor:
  1.1961 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetDeviceQualifierDescriptor"));
  1.1962 +		BREAK_IF_NULL_ARG(a1,r);
  1.1963 +		r = iController->SetDeviceQualifierDescriptor(iClient, *((TDes8*) a1));
  1.1964 +		break;
  1.1965 +
  1.1966 +	case RDevUsbcScClient::EControlGetOtherSpeedConfigurationDescriptor:
  1.1967 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetOtherSpeedConfigurationDescriptor"));
  1.1968 +		r = Kern::ThreadDesWrite(iClient, a1, pZeroDesc, 0 , 0, iClient);
  1.1969 +		if (r != KErrNone)
  1.1970 +			PanicClientThread(r);
  1.1971 +		r = iController->GetOtherSpeedConfigurationDescriptor(iClient, *((TDes8*) a1));
  1.1972 +		break;
  1.1973 +
  1.1974 +	case RDevUsbcScClient::EControlSetOtherSpeedConfigurationDescriptor:
  1.1975 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetOtherSpeedConfigurationDescriptor"));
  1.1976 +		r = iController->SetOtherSpeedConfigurationDescriptor(iClient, *((TDes8*) a1));
  1.1977 +		break;
  1.1978 +
  1.1979 +
  1.1980 +	case RDevUsbcScClient::EControlGetCSInterfaceDescriptor:
  1.1981 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetCSInterfaceDescriptor"));
  1.1982 +		r = iController->GetCSInterfaceDescriptorBlock(iClient, this, (TInt) a1, *((TDes8*) a2));
  1.1983 +		break;
  1.1984 +
  1.1985 +	case RDevUsbcScClient::EControlGetCSInterfaceDescriptorSize:
  1.1986 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetCSInterfaceDescriptorSize"));
  1.1987 +		r = iController->GetCSInterfaceDescriptorBlockSize(iClient, this, (TInt) a1, *(TDes8*) a2);
  1.1988 +		break;
  1.1989 +
  1.1990 +	case RDevUsbcScClient::EControlGetCSEndpointDescriptor:
  1.1991 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetCSEndpointDescriptor"));
  1.1992 +		r = Kern::ThreadRawRead(iClient, a1, &epInfo, sizeof(epInfo));
  1.1993 +		if (r != KErrNone)
  1.1994 +			PanicClientThread(r);
  1.1995 +		ep = EpFromAlternateSetting(epInfo.iSetting, epInfo.iEndpoint);
  1.1996 +		r = iController->GetCSEndpointDescriptorBlock(iClient, this, epInfo.iSetting,
  1.1997 +													  ep, *(TDes8*) epInfo.iArg);
  1.1998 +		break;
  1.1999 +
  1.2000 +	case RDevUsbcScClient::EControlGetCSEndpointDescriptorSize:
  1.2001 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetCSEndpointDescriptorSize"));
  1.2002 +		r = Kern::ThreadRawRead(iClient, a1, &epInfo, sizeof(epInfo));
  1.2003 +		if (r != KErrNone)
  1.2004 +			PanicClientThread(r);
  1.2005 +		ep = EpFromAlternateSetting(epInfo.iSetting, epInfo.iEndpoint);
  1.2006 +		r = iController->GetCSEndpointDescriptorBlockSize(iClient, this, epInfo.iSetting,
  1.2007 +														  ep, *(TDes8*) epInfo.iArg);
  1.2008 +		break;
  1.2009 +
  1.2010 +	case RDevUsbcScClient::EControlSignalRemoteWakeup:
  1.2011 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSignalRemoteWakeup"));
  1.2012 +		r = iController->SignalRemoteWakeup();
  1.2013 +		break;
  1.2014 +
  1.2015 +	case RDevUsbcScClient::EControlDeviceDisconnectFromHost:
  1.2016 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlDeviceDisconnectFromHost"));
  1.2017 +		r = iController->UsbDisconnect();
  1.2018 +		break;
  1.2019 +
  1.2020 +	case RDevUsbcScClient::EControlDeviceConnectToHost:
  1.2021 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlDeviceConnectToHost"));
  1.2022 +		r = iController->UsbConnect();
  1.2023 +		break;
  1.2024 +
  1.2025 +	case RDevUsbcScClient::EControlDevicePowerUpUdc:
  1.2026 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlDevicePowerUpUdc"));
  1.2027 +		r = iController->PowerUpUdc();
  1.2028 +		break;
  1.2029 +
  1.2030 +	case RDevUsbcScClient::EControlSetDeviceControl:
  1.2031 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetDeviceControl"));
  1.2032 +		r = iController->SetDeviceControl(this);
  1.2033 +		if (r == KErrNone)
  1.2034 +			{
  1.2035 +			iOwnsDeviceControl = ETrue;
  1.2036 +			if (iEp0Endpoint == NULL)
  1.2037 +				{
  1.2038 +				__KTRACE_OPT(KUSB, Kern::Printf("EControlSetDeviceControl"));
  1.2039 +				r = SetupEp0();
  1.2040 +				if (r != KErrNone)
  1.2041 +					{
  1.2042 +					__KTRACE_OPT(KPANIC, Kern::Printf("  Error: SetupEp0() failed"));
  1.2043 +					iController->ReleaseDeviceControl(this);
  1.2044 +					iOwnsDeviceControl=EFalse;
  1.2045 +					DestroyEp0();
  1.2046 +					}
  1.2047 +				}
  1.2048 +			}
  1.2049 +		else
  1.2050 +			r = KErrInUse;
  1.2051 +		break;
  1.2052 +
  1.2053 +	case RDevUsbcScClient::EControlCurrentlyUsingHighSpeed:
  1.2054 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlCurrentlyUsingHighSpeed"));
  1.2055 +		r = iController->CurrentlyUsingHighSpeed();
  1.2056 +		break;
  1.2057 +
  1.2058 +	case RDevUsbcScClient::EControlSetInterface:
  1.2059 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetInterface"));
  1.2060 +		r = Kern::ThreadRawRead(iClient, a2, &ifcInfo, sizeof(ifcInfo));
  1.2061 +		if (r != KErrNone)
  1.2062 +			PanicClientThread(r);
  1.2063 +		r = SetInterface((TInt) a1, &ifcInfo);
  1.2064 +		break;
  1.2065 +
  1.2066 +	case RDevUsbcScClient::EControlReleaseInterface: 
  1.2067 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlReleaseInterface"));
  1.2068 +		if (!iRealizeCalled)
  1.2069 +			{
  1.2070 +			r = iController->ReleaseInterface(this, (TInt) a1);
  1.2071 +			if (r == KErrNone)
  1.2072 +				{
  1.2073 +				DestroyInterface((TUint) a1);
  1.2074 +				}
  1.2075 +			else
  1.2076 +				{
  1.2077 +				__KTRACE_OPT(KPANIC, Kern::Printf("  Error in PIL: LDD interface won't be released."));
  1.2078 +				}
  1.2079 +			}
  1.2080 +		else
  1.2081 +			r = KErrUsbAlreadyRealized;
  1.2082 +		break;
  1.2083 +
  1.2084 +	case RDevUsbcScClient::EControlSetCSInterfaceDescriptor:
  1.2085 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetCSInterfaceDescriptor"));
  1.2086 +		r = Kern::ThreadRawRead(iClient, a1, &desInfo, sizeof(desInfo));
  1.2087 +		if (r != KErrNone)
  1.2088 +			PanicClientThread(r);
  1.2089 +		r = iController->SetCSInterfaceDescriptorBlock(iClient, this, desInfo.iSetting,
  1.2090 +													   *reinterpret_cast<const TDes8*>(desInfo.iArg),
  1.2091 +													   desInfo.iSize);
  1.2092 +		break;
  1.2093 +
  1.2094 +	case RDevUsbcScClient::EControlSetCSEndpointDescriptor:
  1.2095 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetCSEndpointDescriptor"));
  1.2096 +		r = Kern::ThreadRawRead(iClient, a1, &desInfo, sizeof(desInfo));
  1.2097 +		if (r != KErrNone)
  1.2098 +			PanicClientThread(r);
  1.2099 +		ep = EpFromAlternateSetting(desInfo.iSetting, desInfo.iEndpoint);
  1.2100 +		r = iController->SetCSEndpointDescriptorBlock(iClient, this, desInfo.iSetting, ep,
  1.2101 +													  *reinterpret_cast<const TDes8*>(desInfo.iArg),
  1.2102 +													  desInfo.iSize);
  1.2103 +		break;
  1.2104 +
  1.2105 +	case RDevUsbcScClient::EControlGetStringDescriptorLangId:
  1.2106 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetStringDescriptorLangId"));
  1.2107 +		r = iController->GetStringDescriptorLangId(iClient, *((TDes8*) a1));
  1.2108 +		break;
  1.2109 +
  1.2110 +	case RDevUsbcScClient::EControlSetStringDescriptorLangId:
  1.2111 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetStringDescriptorLangId"));
  1.2112 +		r = iController->SetStringDescriptorLangId(reinterpret_cast<TUint>(a1));
  1.2113 +		break;
  1.2114 +
  1.2115 +	case RDevUsbcScClient::EControlGetManufacturerStringDescriptor:
  1.2116 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetManufacturerStringDescriptor"));
  1.2117 +		r = iController->GetManufacturerStringDescriptor(iClient, *((TPtr8*) a1));
  1.2118 +		break;
  1.2119 +
  1.2120 +	case RDevUsbcScClient::EControlSetManufacturerStringDescriptor:
  1.2121 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetManufacturerStringDescriptor"));
  1.2122 +		r = iController->SetManufacturerStringDescriptor(iClient, *((TPtr8*) a1));
  1.2123 +		break;
  1.2124 +
  1.2125 +	case RDevUsbcScClient::EControlRemoveManufacturerStringDescriptor:
  1.2126 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlRemoveManufacturerStringDescriptor"));
  1.2127 +		r = iController->RemoveManufacturerStringDescriptor();
  1.2128 +		break;
  1.2129 +
  1.2130 +	case RDevUsbcScClient::EControlGetProductStringDescriptor:
  1.2131 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetProductStringDescriptor"));
  1.2132 +		r = iController->GetProductStringDescriptor(iClient, *((TPtr8*) a1));
  1.2133 +		break;
  1.2134 +
  1.2135 +	case RDevUsbcScClient::EControlSetProductStringDescriptor:
  1.2136 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetProductStringDescriptor"));
  1.2137 +		r = iController->SetProductStringDescriptor(iClient, *((TPtr8*) a1));
  1.2138 +		break;
  1.2139 +
  1.2140 +	case RDevUsbcScClient::EControlRemoveProductStringDescriptor:
  1.2141 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlRemoveProductStringDescriptor"));
  1.2142 +		r = iController->RemoveProductStringDescriptor();
  1.2143 +		break;
  1.2144 +
  1.2145 +	case RDevUsbcScClient::EControlGetSerialNumberStringDescriptor:
  1.2146 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetSerialNumberStringDescriptor"));
  1.2147 +		r = iController->GetSerialNumberStringDescriptor(iClient, *((TPtr8*) a1));
  1.2148 +		break;
  1.2149 +
  1.2150 +	case RDevUsbcScClient::EControlSetSerialNumberStringDescriptor:
  1.2151 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetSerialNumberStringDescriptor"));
  1.2152 +		r = iController->SetSerialNumberStringDescriptor(iClient, *((TPtr8*) a1));
  1.2153 +		break;
  1.2154 +
  1.2155 +	case RDevUsbcScClient::EControlRemoveSerialNumberStringDescriptor:
  1.2156 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlRemoveSerialNumberStringDescriptor"));
  1.2157 +		r = iController->RemoveSerialNumberStringDescriptor();
  1.2158 +		break;
  1.2159 +
  1.2160 +	case RDevUsbcScClient::EControlGetConfigurationStringDescriptor:
  1.2161 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetConfigurationStringDescriptor"));
  1.2162 +		r = iController->GetConfigurationStringDescriptor(iClient, *((TPtr8*) a1));
  1.2163 +		break;
  1.2164 +
  1.2165 +	case RDevUsbcScClient::EControlSetConfigurationStringDescriptor:
  1.2166 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetConfigurationStringDescriptor"));
  1.2167 +		r = iController->SetConfigurationStringDescriptor(iClient, *((TPtr8*) a1));
  1.2168 +		break;
  1.2169 +
  1.2170 +	case RDevUsbcScClient::EControlRemoveConfigurationStringDescriptor:
  1.2171 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlRemoveConfigurationStringDescriptor"));
  1.2172 +		r = iController->RemoveConfigurationStringDescriptor();
  1.2173 +		break;
  1.2174 +
  1.2175 +	case RDevUsbcScClient::EControlGetStringDescriptor:
  1.2176 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetStringDescriptor"));
  1.2177 +		r = iController->GetStringDescriptor(iClient, (TUint8) (TInt) a1, *((TPtr8*) a2));
  1.2178 +		break;
  1.2179 +
  1.2180 +	case RDevUsbcScClient::EControlSetStringDescriptor:
  1.2181 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetStringDescriptor"));
  1.2182 +		r = iController->SetStringDescriptor(iClient, (TUint8) (TInt) a1, *((TPtr8*) a2));
  1.2183 +		break;
  1.2184 +
  1.2185 +	case RDevUsbcScClient::EControlRemoveStringDescriptor:
  1.2186 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlRemoveStringDescriptor"));
  1.2187 +		r = iController->RemoveStringDescriptor((TUint8) (TInt) a1);
  1.2188 +		break;
  1.2189 +
  1.2190 +	case RDevUsbcScClient::EControlAllocateEndpointResource:
  1.2191 +		{
  1.2192 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlAllocateEndpointResource"));
  1.2193 +		epRes = (TUsbcEndpointResource)((TInt) a2);
  1.2194 +		TInt realEp=-1;
  1.2195 +		r = GetRealEpForEpResource((TInt)a1, realEp);
  1.2196 +		if (r==KErrNone)
  1.2197 +			r = iController->AllocateEndpointResource(this, realEp, epRes);
  1.2198 +		break;
  1.2199 +		}
  1.2200 +	case RDevUsbcScClient::EControlDeAllocateEndpointResource:
  1.2201 +		{
  1.2202 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlDeAllocateEndpointResource"));
  1.2203 +		epRes = (TUsbcEndpointResource)((TInt) a2);
  1.2204 +		TInt realEp=-1;
  1.2205 +		r = GetRealEpForEpResource((TInt)a1, realEp);
  1.2206 +		if (r==KErrNone)
  1.2207 +			r = iController->DeAllocateEndpointResource(this, realEp, epRes);
  1.2208 +		break;
  1.2209 +		}
  1.2210 +	case RDevUsbcScClient::EControlQueryEndpointResourceUse:
  1.2211 +		{
  1.2212 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlQueryEndpointResourceUse"));
  1.2213 +		epRes = (TUsbcEndpointResource)((TInt) a2);
  1.2214 +		TInt realEp=-1;
  1.2215 +		r = GetRealEpForEpResource((TInt)a1, realEp);
  1.2216 +		if (r==KErrNone)
  1.2217 +			r = iController->QueryEndpointResource(this, realEp, epRes);
  1.2218 +		break;
  1.2219 +		}
  1.2220 +	case RDevUsbcScClient::EControlSetOtgDescriptor:
  1.2221 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlSetOtgDescriptor"));
  1.2222 +		r = iController->SetOtgDescriptor(iClient, *((const TDesC8*)a1));
  1.2223 +		break;
  1.2224 +
  1.2225 +	case RDevUsbcScClient::EControlGetOtgDescriptor:
  1.2226 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetOtgDescriptor"));
  1.2227 +		r = iController->GetOtgDescriptor(iClient, *((TDes8*)a1));
  1.2228 +		break;
  1.2229 +
  1.2230 +	case RDevUsbcScClient::EControlGetOtgFeatures:
  1.2231 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlGetOtgFeatures"));
  1.2232 +		r = iController->GetOtgFeatures(iClient, *((TDes8*)a1));
  1.2233 +		break;
  1.2234 +
  1.2235 +	case RDevUsbcScClient::EControlRealizeInterface:
  1.2236 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlRealizeInterface"));
  1.2237 +		r = RealizeInterface();
  1.2238 +		break;
  1.2239 +	case RDevUsbcScClient::EControlStartNextInAlternateSetting:
  1.2240 +		__KTRACE_OPT(KUSB, Kern::Printf("EControlStartNextInAlternateSetting"));
  1.2241 +		r = StartNextInAlternateSetting();
  1.2242 +		break;
  1.2243 +
  1.2244 +    default:
  1.2245 +		__KTRACE_OPT(KUSB, Kern::Printf("Function code not supported"));
  1.2246 +		r = KErrNotSupported;
  1.2247 +		}
  1.2248 +
  1.2249 +	return r;
  1.2250 +	}
  1.2251 +// end DoControl.
  1.2252 +
  1.2253 +
  1.2254 +
  1.2255 +//
  1.2256 +// Overriding DObject virtual
  1.2257 +//
  1.2258 +TInt DLddUsbcScChannel::RequestUserHandle(DThread* aThread, TOwnerType /*aType*/)
  1.2259 +	{
  1.2260 +	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::RequestUserHandle"));
  1.2261 +	// The USB client LDD is not designed for a channel to be shared between
  1.2262 +	// threads. It saves a pointer to the current thread when it is opened, and
  1.2263 +	// uses this to complete any asynchronous requests.
  1.2264 +	// It is therefore not acceptable for the handle to be duplicated and used
  1.2265 +	// by another thread:
  1.2266 +	if (aThread == iClient)
  1.2267 +		{
  1.2268 +		return KErrNone;
  1.2269 +		}
  1.2270 +	else
  1.2271 +		{
  1.2272 +		return KErrAccessDenied;
  1.2273 +		}
  1.2274 +	}
  1.2275 +
  1.2276 +inline TInt DLddUsbcScChannel::GetRealEpForEpResource(TInt aEndpoint, TInt& aRealEp)
  1.2277 +	{
  1.2278 +	if (iEndpoint) // if we've enumerated at least once, proceed as normal.
  1.2279 +		{
  1.2280 +		if  (aEndpoint <= iNumberOfEndpoints && aEndpoint >= 0)
  1.2281 +			{
  1.2282 +			aRealEp=iEndpoint[aEndpoint]->RealEpNumber();
  1.2283 +			return KErrNone;
  1.2284 +			}
  1.2285 +		}
  1.2286 +	else // Assume alternate setting 0.
  1.2287 +		{
  1.2288 +		if (iAlternateSettingList)   // Check it has been set up.
  1.2289 +			{
  1.2290 +			TUsbcScAlternateSetting* alt = iAlternateSettingList->iHead;
  1.2291 +			if (alt &&  (aEndpoint <= alt->iNumberOfEndpoints && aEndpoint >= 0))
  1.2292 +				{
  1.2293 +				aRealEp= alt->iEndpoint[aEndpoint]->RealEpNumber();
  1.2294 +				return KErrNone;
  1.2295 +				}
  1.2296 +			}
  1.2297 +		}
  1.2298 +	return KErrUsbDeviceNotConfigured;
  1.2299 +	}
  1.2300 +
  1.2301 +
  1.2302 +TUsbcEndpointInfoArray::TUsbcEndpointInfoArray(const TUsbcScEndpointInfo* aData, TInt aDataSize)
  1.2303 +	{
  1.2304 +	iType = EUsbcScEndpointInfo;
  1.2305 +	iData = (TUint8*) aData;	
  1.2306 +	if (aDataSize>0)
  1.2307 +		iDataSize = aDataSize;
  1.2308 +	else
  1.2309 +		iDataSize = sizeof(TUsbcScEndpointInfo);
  1.2310 +	}
  1.2311 +
  1.2312 +
  1.2313 +//
  1.2314 +// SetInterface
  1.2315 +//
  1.2316 +// Called from DoControl.  Sets the configuration of a given Interface.					// Needs changing
  1.2317 +// All interfaces must be configured before one can be used.  
  1.2318 +//
  1.2319 +
  1.2320 +TInt DLddUsbcScChannel::SetInterface(TInt aInterfaceNumber, TUsbcScIfcInfo* aInfoBuf)
  1.2321 +	{
  1.2322 +	// Copy interface description.
  1.2323 +
  1.2324 +	if (iRealizeCalled)
  1.2325 +		return KErrUsbAlreadyRealized;
  1.2326 +
  1.2327 +	if (!iAlternateSettingList)
  1.2328 +		{
  1.2329 +		iAlternateSettingList = new TUsbcScAlternateSettingList;
  1.2330 +		if (iAlternateSettingList==NULL)
  1.2331 +			{
  1.2332 +			return KErrNoMemory;
  1.2333 +			}
  1.2334 +		}
  1.2335 +
  1.2336 +	// Read descriptor in
  1.2337 +	TUsbcScInterfaceInfoBuf ifc_info_buf;
  1.2338 +	TUsbcScInterfaceInfoBuf* const ifc_info_buf_ptr = aInfoBuf->iInterfaceData;
  1.2339 +	const TInt srcLen = Kern::ThreadGetDesLength(iClient, ifc_info_buf_ptr);
  1.2340 +
  1.2341 +	__KTRACE_OPT(KUSB, Kern::Printf("SetInterface srcLen = %d len = %d", srcLen, ifc_info_buf.Length() ));
  1.2342 +
  1.2343 +	if (srcLen < ifc_info_buf.Length())
  1.2344 +		{
  1.2345 +		__KTRACE_OPT(KUSB, Kern::Printf("SetInterface can't copy"));
  1.2346 +		PanicClientThread(EDesOverflow);
  1.2347 +		}
  1.2348 +
  1.2349 +	TInt r = Kern::ThreadDesRead(iClient, ifc_info_buf_ptr, ifc_info_buf, 0, KChunkShiftBy0);
  1.2350 +	if (r != KErrNone)
  1.2351 +		{
  1.2352 +		__KTRACE_OPT(KUSB, Kern::Printf("SetInterface Copy failed reason=%d", r));
  1.2353 +		PanicClientThread(r);
  1.2354 +		}
  1.2355 +
  1.2356 +	// The list of endpoints is within the interface info.
  1.2357 +	TUsbcScEndpointInfo* pEndpointData = ifc_info_buf().iEndpointData;
  1.2358 +
  1.2359 +	const TInt num_endpoints = ifc_info_buf().iTotalEndpointsUsed;
  1.2360 +	__KTRACE_OPT(KUSB, Kern::Printf("SetInterface num_endpoints=%d", num_endpoints));
  1.2361 +	if (num_endpoints>KMaxEndpointsPerClient)
  1.2362 +		return KErrOverflow;
  1.2363 +
  1.2364 +
  1.2365 +	// Initialize real ep numbers list.
  1.2366 +	TInt i;
  1.2367 +	TInt real_ep_numbers[KMaxEndpointsPerClient+1]; // range 1->KMaxEndpointsPerClient (0 not used)
  1.2368 +	for (i=0; i<=KMaxEndpointsPerClient; i++)
  1.2369 +		real_ep_numbers[i] = -1;
  1.2370 +
  1.2371 +
  1.2372 +	// See if PIL will accept this interface
  1.2373 +	__KTRACE_OPT(KUSB, Kern::Printf("SetInterface Calling controller"));
  1.2374 +	TUsbcEndpointInfoArray endpointData = TUsbcEndpointInfoArray(ifc_info_buf().iEndpointData);
  1.2375 +
  1.2376 +	r = iController->SetInterface(this,
  1.2377 +								  iClient,
  1.2378 +								  aInterfaceNumber,
  1.2379 +								  ifc_info_buf().iClass,
  1.2380 +								  aInfoBuf->iString,
  1.2381 +								  (TInt) ifc_info_buf().iTotalEndpointsUsed,
  1.2382 +								  endpointData,
  1.2383 +								  &real_ep_numbers[0],
  1.2384 +								  ifc_info_buf().iFeatureWord);
  1.2385 +
  1.2386 +	__KTRACE_OPT(KUSB, Kern::Printf("SetInterface controller returned %d", r));
  1.2387 +	if (r != KErrNone)
  1.2388 +		{
  1.2389 +		__KTRACE_OPT(KPANIC, Kern::Printf("SetInterface failed reason=%d", r));
  1.2390 +		return r;
  1.2391 +		}
  1.2392 +
  1.2393 +	// create alternate setting record
  1.2394 +    TUsbcScAlternateSetting* alternateSettingListRec = new TUsbcScAlternateSetting;
  1.2395 +	if (!alternateSettingListRec)
  1.2396 +		{
  1.2397 +		r = KErrNoMemory;
  1.2398 +		goto ReleaseInterface;
  1.2399 +		}
  1.2400 +	
  1.2401 +	// other endpoints
  1.2402 +	for (TInt i = 1; i <= num_endpoints; i++, pEndpointData++)
  1.2403 +		{
  1.2404 +		__KTRACE_OPT(KUSB, Kern::Printf("SetInterface for ep=%d", i));
  1.2405 +
  1.2406 +		if ((pEndpointData->iType==KUsbEpTypeControl)
  1.2407 +			|| (pEndpointData->iDir != KUsbEpDirIn && pEndpointData->iDir != KUsbEpDirOut)
  1.2408 +			|| (pEndpointData->iSize > 1024) || (pEndpointData->iSize<=0))
  1.2409 +			{
  1.2410 +			r = KErrUsbBadEndpoint;
  1.2411 +			goto CleanUp;
  1.2412 +			}
  1.2413 +		// Check data
  1.2414 +
  1.2415 +		TUint* bufferSize = &(pEndpointData->iBufferSize);
  1.2416 +		if (*bufferSize==0)
  1.2417 +			*bufferSize= KUsbcScDefaultBufferSize;
  1.2418 +
  1.2419 +		TInt pageSize = Kern::RoundToPageSize(1);
  1.2420 +		// Round buffersize up to nearest pagesize.
  1.2421 +		*bufferSize = (*bufferSize+pageSize-1) & ~(pageSize-1);
  1.2422 +
  1.2423 +		TUsbcScEndpoint* ep = new TUsbcScEndpoint(this, iController, pEndpointData, i);
  1.2424 +		alternateSettingListRec->iEndpoint[i] = ep;
  1.2425 +		if (!ep)
  1.2426 +			{
  1.2427 +			r = KErrNoMemory;
  1.2428 +			goto CleanUp;
  1.2429 +			}
  1.2430 +		if (ep->Construct() != KErrNone)
  1.2431 +			{
  1.2432 +			r = KErrNoMemory;
  1.2433 +			goto CleanUp;
  1.2434 +			}
  1.2435 +
  1.2436 +	
  1.2437 +		__KTRACE_OPT(KUSB, Kern::Printf("SetInterface for ep=%d rec=0x%08x ep==0x%08x",
  1.2438 +										i, alternateSettingListRec, ep));
  1.2439 +		}
  1.2440 +
  1.2441 +	if (iAlternateSettingList->iHead)
  1.2442 +		{
  1.2443 +		iAlternateSettingList->iTail->iNext = alternateSettingListRec;
  1.2444 +		alternateSettingListRec->iPrevious = iAlternateSettingList->iTail;
  1.2445 +		iAlternateSettingList->iTail = alternateSettingListRec;	
  1.2446 +		}
  1.2447 +	else
  1.2448 +		{
  1.2449 +		iAlternateSettingList->iHead = alternateSettingListRec;	
  1.2450 +		iAlternateSettingList->iTail = alternateSettingListRec;	
  1.2451 +		}	
  1.2452 +	
  1.2453 +	alternateSettingListRec->iNext = NULL;
  1.2454 +	alternateSettingListRec->iSetting = aInterfaceNumber;
  1.2455 +	alternateSettingListRec->iNumberOfEndpoints = num_endpoints;
  1.2456 +
  1.2457 +	// Record the 'real' endpoint number used by the PDD in both the Ep and
  1.2458 +	// the Req callback:
  1.2459 +	for (TInt i = 1; i <= num_endpoints; i++)
  1.2460 +		{
  1.2461 +		alternateSettingListRec->iEndpoint[i]->SetRealEpNumber(real_ep_numbers[i]);
  1.2462 +		}
  1.2463 +
  1.2464 +	return KErrNone;
  1.2465 +
  1.2466 + CleanUp:
  1.2467 +	delete alternateSettingListRec;
  1.2468 +	//Fall Through
  1.2469 +
  1.2470 + ReleaseInterface:
  1.2471 +#if _DEBUG
  1.2472 +	TInt r1 = iController->ReleaseInterface(this, aInterfaceNumber);
  1.2473 +	__KTRACE_OPT(KUSB, Kern::Printf("Release Interface controller returned %d", r1));
  1.2474 +#else
  1.2475 +	(void)	iController->ReleaseInterface(this, aInterfaceNumber);
  1.2476 +#endif
  1.2477 +	return r;
  1.2478 +	}
  1.2479 +// end SetInterface
  1.2480 +
  1.2481 +
  1.2482 +
  1.2483 +#ifdef _DEBUG
  1.2484 +void RealizeInterface_Dump(TUint* aMem)
  1.2485 +	{
  1.2486 +	TUint *mem= NULL;
  1.2487 +	__KTRACE_OPT(KUSB, mem = aMem);
  1.2488 +	if (mem!=NULL)
  1.2489 +		{
  1.2490 +		TInt j;
  1.2491 + 		Kern::Printf("Final chunk header State:");
  1.2492 +		for (j=0; j<30; j+=8)
  1.2493 + 			Kern::Printf("%2x: %8x %8x %8x %8x %8x %8x %8x %8x", j, mem[j], mem[j+1], mem[j+2], mem[j+3], mem[j+4], mem[j+5], mem[j+6], mem[j+7] );
  1.2494 +		};
  1.2495 +	};
  1.2496 +#endif
  1.2497 +
  1.2498 +
  1.2499 +/*
  1.2500 +Chunk Created, filled with structure, and passed back to userside.
  1.2501 +*/
  1.2502 +TInt DLddUsbcScChannel::RealizeInterface(void)
  1.2503 +{
  1.2504 +	if (iRealizeCalled) 
  1.2505 +		return KErrUsbAlreadyRealized;
  1.2506 +
  1.2507 +	TRealizeInfo bufInfo;
  1.2508 +	
  1.2509 +	TInt errorOrChunk = KErrNone;
  1.2510 +	TBool openedCS = EFalse;
  1.2511 +	TInt offset =0;
  1.2512 +		
  1.2513 +	// Start by creating a temporary scratchpad for endpoint calculations.
  1.2514 +	bufInfo.Init(iAlternateSettingList);
  1.2515 +
  1.2516 +	// Fill in our scratchpad with all the required endpoints, sorting them
  1.2517 +	// in order of size required.
  1.2518 +	errorOrChunk = bufInfo.CopyAndSortEndpoints();
  1.2519 +	if (errorOrChunk!=KErrNone)
  1.2520 +		{
  1.2521 +		goto realize_end;
  1.2522 +		}
  1.2523 +
  1.2524 +	// We now have endpoints sorted in order of size for each altsetting.
  1.2525 +	// The very largest for each endpoint will share the first buffer, and all of
  1.2526 +	// the second largest ends points will share the second buffer, and so on.
  1.2527 +	// Find the highest buffer size for each row, to determine the buffer size,
  1.2528 +	// and keep a total of total space needed. 
  1.2529 +	bufInfo.CalcBuffSizes();
  1.2530 +
  1.2531 +	// We now have the max sizes wanted for each endpoint buffer.
  1.2532 +	// we also have to total size for all endpoints.
  1.2533 +	// and finally we have the total number of buffers.
  1.2534 +
  1.2535 +	// Add on size for header, then add on size for guard pages.
  1.2536 +	bufInfo.iTotalSize+= KHeaderSize + bufInfo.iTotalBuffers * KGuardSize;
  1.2537 +
  1.2538 +	// Create shared Chunk .  .  .  .  .  .  .  .  .  . 
  1.2539 +	if (iChunkInfo==NULL)
  1.2540 +		{
  1.2541 +			NKern::ThreadEnterCS();
  1.2542 +			openedCS = ETrue;
  1.2543 +			errorOrChunk = TUsbcScChunkInfo::New(iChunkInfo, bufInfo.iTotalSize, (DLogicalDevice*) iDevice);
  1.2544 +			if (errorOrChunk!=KErrNone)
  1.2545 +				{
  1.2546 +				goto realize_end;
  1.2547 +				}
  1.2548 +		}
  1.2549 +	else
  1.2550 +		{
  1.2551 +		// As of writing, the was no way for iChunk to be anything other then NULL.  
  1.2552 +		// You cannot 'unrealise' and iChunk cannot be set any other way.
  1.2553 +		Kern::Fault("DLddUsbcScChannel::RealizeInterface", __LINE__);
  1.2554 +		}
  1.2555 +
  1.2556 +	// Populate the shared chunk . .  . . . . . 
  1.2557 +
  1.2558 +
  1.2559 +	// First create chunk header.
  1.2560 +	errorOrChunk = iChunkInfo->ChunkAlloc(offset, KHeaderSize);
  1.2561 +	if (errorOrChunk!=KErrNone)
  1.2562 +		{
  1.2563 +		if (errorOrChunk==-KErrNoMemory)
  1.2564 +			errorOrChunk=KErrNoMemory;
  1.2565 +		goto realize_end;
  1.2566 +		} 
  1.2567 +
  1.2568 +
  1.2569 +	offset+=KHeaderSize + KGuardSize; // Also any more for EP0?
  1.2570 +
  1.2571 +	// Next, lay out the geometry of the chunk header.
  1.2572 +
  1.2573 +	bufInfo.LayoutChunkHeader(iChunkInfo);		
  1.2574 +
  1.2575 +
  1.2576 +	{ // Scope ep0Size
  1.2577 +	TInt ep0Size=0;
  1.2578 +	
  1.2579 +	// Create K-side buffer table
  1.2580 +	if (!iBuffers)
  1.2581 +		iBuffers = (TUsbcScBuffer *) Kern::AllocZ(sizeof(TUsbcScBuffer) * (bufInfo.iTotalBuffers+2)); // +2 is for ep0.
  1.2582 +	if (!iBuffers)
  1.2583 +		{
  1.2584 +		__KTRACE_OPT(KUSB, Kern::Printf("Realize: Error: Alloc iBufers failed!"));
  1.2585 +		errorOrChunk = KErrNoMemory;
  1.2586 +		goto realize_end;
  1.2587 +		}
  1.2588 +
  1.2589 +
  1.2590 +	errorOrChunk = SetupEp0();
  1.2591 +	if (errorOrChunk)
  1.2592 +		{
  1.2593 +		__KTRACE_OPT(KUSB, Kern::Printf("Realize: SetupEp0 . ERROR %d",errorOrChunk));
  1.2594 +		goto realize_end;
  1.2595 +		}
  1.2596 +
  1.2597 +	ep0Size = iEp0Endpoint->EndpointInfo()->iSize;
  1.2598 +	__KTRACE_OPT(KUSB, Kern::Printf("Realize: Setup EP0. max packet size %d", ep0Size));
  1.2599 +
  1.2600 +	// Create EP0 buffers
  1.2601 +	iEP0OutBuff=bufInfo.iTotalBuffers;
  1.2602 +	errorOrChunk = iBuffers[iEP0OutBuff].Construct(KUsbcScBiOut,  this,   KUsbScEP0OutBufPos, KUsbScEP0OutBufEnd, ep0Size, ep0Size, ep0Size);
  1.2603 +	if (errorOrChunk)
  1.2604 +		{
  1.2605 +		__KTRACE_OPT(KUSB, Kern::Printf("Realize: Setup EP0 Out. ERROR %d",errorOrChunk));
  1.2606 +		goto realize_end;
  1.2607 +		}
  1.2608 +
  1.2609 +	iBuffers[iEP0OutBuff].CreateChunkBufferHeader();
  1.2610 +	iBuffers[iEP0OutBuff].iCallback =  iEp0Endpoint->iRequestCallbackInfo;
  1.2611 +	((TUsbcScBufferRecord*) &(
  1.2612 +							bufInfo.iChunkStuct->iBufferOffset[KUsbcScEp0OutBuff*sizeof(TUsbcScBufferRecord)]
  1.2613 +							)) ->Set(KUsbScEP0OutBufPos, KUsbScEP0OutBufEnd);
  1.2614 +
  1.2615 +
  1.2616 +	iEP0InBuff=bufInfo.iTotalBuffers+1;
  1.2617 +	errorOrChunk = iBuffers[iEP0InBuff].Construct( KUsbcScBiIn ,  this,   KUsbScEP0InBufPos , KUsbScEP0InBufEnd , ep0Size, ep0Size, ep0Size);	
  1.2618 +	if (errorOrChunk)
  1.2619 +		{
  1.2620 +		__KTRACE_OPT(KUSB, Kern::Printf("Realize: Setup EP0 In. ERROR %d",errorOrChunk));
  1.2621 +		goto realize_end;
  1.2622 +		}
  1.2623 +	
  1.2624 +	iBuffers[iEP0InBuff].iCallback =  iEp0Endpoint->iRequestCallbackInfo;
  1.2625 +
  1.2626 +	  ((TUsbcScBufferRecord*) &(
  1.2627 +	  							bufInfo.iChunkStuct->iBufferOffset[KUsbcScEp0InBuff*sizeof(TUsbcScBufferRecord)]
  1.2628 +								))->Set(KUsbScEP0InBufPos, KUsbScEP0InBufEnd);
  1.2629 +
  1.2630 +
  1.2631 +	} // end ep0Size scope
  1.2632 +
  1.2633 +	// Create resources and tables.  .   .   .   .   .
  1.2634 +	__KTRACE_OPT(KUSB, Kern::Printf("Realize: Create resources tables"));
  1.2635 +
  1.2636 +	{ // scope of bufNum
  1.2637 +	// For each EP buffer
  1.2638 +	TInt buffNum=0;
  1.2639 +	TInt buffMinSize;
  1.2640 +	TInt endpointNumber;
  1.2641 +	TUsbcScEndpoint* endpointRecord;
  1.2642 +	TInt endpoint;
  1.2643 +	TInt inout;
  1.2644 +	TEndpointSortBufs* bufsd;
  1.2645 +	TUsbcScHdrEndpointRecord* epRecord;
  1.2646 +	for (endpoint=0; endpoint<bufInfo.iMaxEndpoints; endpoint++)  // endpoint = buf row.
  1.2647 +		{
  1.2648 +		for (inout=KUsbcScIn; inout<KUsbcScDirections; inout++)
  1.2649 +			{
  1.2650 +			buffMinSize = KUsbSc_BigBuff_MinimumRamRun;
  1.2651 +
  1.2652 +			TInt needed =  bufInfo.iBufs[inout].iSizes[endpoint];
  1.2653 +			if (needed) 
  1.2654 +				{
  1.2655 +				TInt bufStart = offset;
  1.2656 +
  1.2657 +				__KTRACE_OPT(KUSB, Kern::Printf("Realize:    buf row:%d inout %d, iBufferOffset[%d+2]=%x",endpoint, inout, buffNum, bufStart));
  1.2658 +
  1.2659 +				bufsd =  &(bufInfo.iBufs[inout]);
  1.2660 +				// and then point all endpoints that use it, towards it.
  1.2661 +				TInt altSetting;	
  1.2662 +				TUint maxReadSize = ~0;
  1.2663 +				for (altSetting=0; altSetting < bufInfo.iAltSettings; altSetting++)
  1.2664 +					{
  1.2665 +					endpointRecord =bufsd->iEp[altSetting*bufInfo.iMaxEndpoints + endpoint];
  1.2666 +					if (endpointRecord)
  1.2667 +						{
  1.2668 +						endpointNumber = endpointRecord->EpNumber();
  1.2669 +						endpointRecord->SetBuffer(&iBuffers[buffNum]);
  1.2670 +				
  1.2671 +						epRecord = (TUsbcScHdrEndpointRecord*) &iChunkInfo->iChunkMem[
  1.2672 +																(bufInfo.iAltSettingsTbl->iAltTableOffset[altSetting]) 	// i.e. Just after altSettingsTbl
  1.2673 +																+sizeof(TInt)									// after number of endpoints field
  1.2674 +																+(endpointNumber-1)*sizeof(TUsbcScHdrEndpointRecord)
  1.2675 +																];
  1.2676 +						epRecord->iBufferNo = (TUint8) buffNum;
  1.2677 +
  1.2678 +					TInt epType=(endpointRecord->EndpointInfo()->iType);
  1.2679 +					epType= (epType& KUsbEpTypeControl)?KUsbScHdrEpTypeControl:
  1.2680 +							(epType& KUsbEpTypeIsochronous)?KUsbScHdrEpTypeIsochronous:
  1.2681 +							(epType& KUsbEpTypeBulk)?KUsbScHdrEpTypeBulk:
  1.2682 +							(epType& KUsbEpTypeInterrupt)?KUsbScHdrEpTypeInterrupt:KUsbScHdrEpTypeUnknown;
  1.2683 +
  1.2684 +					epRecord->iType = (inout+1) | (epType<<2);
  1.2685 +
  1.2686 +					if (endpointRecord->EndpointInfo()->iReadSize)
  1.2687 +						maxReadSize = (maxReadSize <= endpointRecord->EndpointInfo()->iReadSize) ? maxReadSize : endpointRecord->EndpointInfo()->iReadSize;
  1.2688 +					
  1.2689 +					__KTRACE_OPT(KUSB, Kern::Printf("Realize:      endpointNum %d in altSetting %d, alt table @ %d",
  1.2690 +													 endpointNumber, altSetting,bufInfo.iAltSettingsTbl->iAltTableOffset[altSetting]));
  1.2691 +						}
  1.2692 +					else
  1.2693 +						{
  1.2694 +						__KTRACE_OPT(KUSB, Kern::Printf("Realize:      endpointNum NA in altSetting %d", altSetting));
  1.2695 +						}
  1.2696 +
  1.2697 +					} // end for
  1.2698 +
  1.2699 +
  1.2700 +				// Alloc memory for buffer.
  1.2701 +				TInt grabSize = needed;
  1.2702 +				// Generally, a buffer fragmented into smaller memory regions will reduce the efficiency 
  1.2703 +				// of reading or writing data, and so avoiding the allocation of very small sections
  1.2704 +				// is advantageous.
  1.2705 +				// However, if only a small amount is being allocated to start with, it is likely
  1.2706 +				// smaller amounts of data are to be sent (reducing this advantage), and 1 memory page 
  1.2707 +				// is a much bigger proportion of the buffer, and so more worth allocating individually.
  1.2708 +
  1.2709 +				TInt minimumGrab;
  1.2710 +				if (needed<KUsbScBigBuffIs)
  1.2711 +					{
  1.2712 +					minimumGrab=Kern::RoundToPageSize(1);
  1.2713 +					buffMinSize = KUsbSc_SmallBuff_MinimumRamRun; // 1k
  1.2714 +					}
  1.2715 +				else
  1.2716 +					{
  1.2717 +					minimumGrab = buffMinSize+Kern::RoundToPageSize(1);
  1.2718 +					}
  1.2719 +
  1.2720 +				// Grab required memory, in bits as big as possible, down to the minimum size. 
  1.2721 +				while (needed >= minimumGrab)
  1.2722 +					{
  1.2723 +					TInt r;
  1.2724 +					r = iChunkInfo->ChunkAlloc(offset, grabSize);
  1.2725 +					if (r==KErrNone)
  1.2726 +						{
  1.2727 +						offset+=grabSize;	
  1.2728 +						needed-=grabSize;
  1.2729 +						}
  1.2730 +					else
  1.2731 +						{
  1.2732 +						if (r==-KErrNoMemory)
  1.2733 +							{
  1.2734 +							grabSize>>=1;
  1.2735 +							}
  1.2736 +						if ((grabSize<minimumGrab) || (r!=-KErrNoMemory))
  1.2737 +							{
  1.2738 +							errorOrChunk = r;
  1.2739 +							goto realize_end;
  1.2740 +							}
  1.2741 +						}
  1.2742 +					} // end while needed
  1.2743 +				
  1.2744 +				// Initialize buffer
  1.2745 +				iBuffers[buffNum].Construct(inout,  this,   bufStart, offset, buffMinSize, 0, maxReadSize);
  1.2746 +				iBuffers[buffNum].CreateChunkBufferHeader();
  1.2747 +				((TUsbcScBufferRecord*) &(
  1.2748 +										bufInfo.iChunkStuct->iBufferOffset[(buffNum+2)*sizeof(TUsbcScBufferRecord)]
  1.2749 +										))->Set(bufStart, offset);
  1.2750 +
  1.2751 +
  1.2752 +				// inc pointers for next buffer
  1.2753 +				buffNum++;
  1.2754 +				offset+=KGuardSize;
  1.2755 +				} // end if needed
  1.2756 +
  1.2757 +			} // end for inout
  1.2758 +		} // end for each buffer
  1.2759 +	} // scope of bufNum 
  1.2760 +
  1.2761 +#ifdef _DEBUG
  1.2762 + RealizeInterface_Dump((TUint*) iChunkInfo->iChunkMem); // Debug only tracing
  1.2763 +#endif
  1.2764 +
  1.2765 +realize_end:
  1.2766 +	__KTRACE_OPT(KUSB, Kern::Printf("Realize: cleanup.  Err=%d", errorOrChunk));
  1.2767 +	// Here we clean up after either success, or after bailing out early.
  1.2768 +
  1.2769 +	bufInfo.Free();
  1.2770 +	
  1.2771 +	if (iChunkInfo)
  1.2772 +		{
  1.2773 +		if (errorOrChunk==KErrNone)
  1.2774 +			{ 
  1.2775 +			// Everything is looking good - create RChunk for Userside.
  1.2776 +			errorOrChunk = Kern::MakeHandleAndOpen(iClient, iChunkInfo->iChunk);
  1.2777 +			iRealizeCalled = (errorOrChunk>=0);
  1.2778 +			} // endif errorOrChunk
  1.2779 +
  1.2780 +		if (errorOrChunk<0)  // If error, destroy the chunk.
  1.2781 +			{
  1.2782 +			iChunkInfo->Close();
  1.2783 +			// ChunkInfo will delete itself with DFC, but the pointer here is no longer needed.
  1.2784 +			iChunkInfo=NULL;
  1.2785 +
  1.2786 +			// Destroy iBuffers
  1.2787 +			if (iBuffers)
  1.2788 +				{
  1.2789 +				TInt i;
  1.2790 +				for (i=0; i<(iNumBuffers+2); i++) 
  1.2791 +					{
  1.2792 +					iBuffers[i].iStatusList.Destroy();
  1.2793 +					}
  1.2794 +				Kern::Free(iBuffers);
  1.2795 +				iBuffers=NULL;
  1.2796 +				}
  1.2797 +
  1.2798 +			}
  1.2799 +		else
  1.2800 +			{
  1.2801 +			iNumBuffers = bufInfo.iTotalBuffers;
  1.2802 +			iValidInterface = ETrue;  // Let the games commence!
  1.2803 +			}
  1.2804 +
  1.2805 +		} // endif iChunkInfo
  1.2806 +	if (openedCS)
  1.2807 +		NKern::ThreadLeaveCS();
  1.2808 +
  1.2809 +	__KTRACE_OPT(KUSB, Kern::Printf("Realize: returning %x (%d)", errorOrChunk, errorOrChunk));
  1.2810 +	return errorOrChunk;
  1.2811 +} // End RealizeInterface
  1.2812 +
  1.2813 +
  1.2814 +//
  1.2815 +// DestroyAllInterfaces
  1.2816 +//
  1.2817 +
  1.2818 +void DLddUsbcScChannel::DestroyAllInterfaces()
  1.2819 +	{
  1.2820 +	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::DestroyAllInterfaces"));
  1.2821 +	// Removes all interfaces
  1.2822 +	if (iAlternateSettingList)
  1.2823 +		{
  1.2824 +		if (iAlternateSettingList->iHead != NULL)
  1.2825 +			{
  1.2826 +			TUsbcScAlternateSetting* alternateSettingListRec = iAlternateSettingList->iTail;
  1.2827 +			while (alternateSettingListRec)
  1.2828 +				{
  1.2829 +				iAlternateSettingList->iTail = alternateSettingListRec->iPrevious; 
  1.2830 +				// If this contains NULL now that is only possible if the record to be deleted was at the head
  1.2831 +				__KTRACE_OPT(KUSB, Kern::Printf("Release interface %d \n", alternateSettingListRec->iSetting));
  1.2832 +				iController->ReleaseInterface(this, alternateSettingListRec->iSetting);
  1.2833 +				delete alternateSettingListRec;
  1.2834 +				if (iAlternateSettingList->iTail == NULL) //No more interfaces left 
  1.2835 +					break;
  1.2836 +				else
  1.2837 +					{
  1.2838 +					iAlternateSettingList->iTail->iNext = NULL;
  1.2839 +					alternateSettingListRec = iAlternateSettingList->iTail;
  1.2840 +					}
  1.2841 +				}
  1.2842 +			}
  1.2843 +		delete iAlternateSettingList;	
  1.2844 +		}
  1.2845 +
  1.2846 +	iNumberOfEndpoints = 0;
  1.2847 +	iAlternateSettingList = NULL;
  1.2848 +	iValidInterface = EFalse;
  1.2849 +
  1.2850 +	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::DestroyAllInterfaces done"));
  1.2851 +	}
  1.2852 +
  1.2853 +
  1.2854 +		
  1.2855 +
  1.2856 +
  1.2857 +//
  1.2858 +// DestroyInterface
  1.2859 +//
  1.2860 +
  1.2861 +void DLddUsbcScChannel::DestroyInterface(TUint aInterfaceNumber)
  1.2862 +	{
  1.2863 +	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::DestroyInterface \n"));
  1.2864 +	
  1.2865 +	if (iAlternateSetting == aInterfaceNumber)
  1.2866 +		{
  1.2867 +		ResetInterface(KErrUsbInterfaceNotReady);
  1.2868 +		iValidInterface = EFalse;
  1.2869 +		iNumberOfEndpoints = 0;
  1.2870 +		}
  1.2871 +	if (iAlternateSettingList)
  1.2872 +		{
  1.2873 +		TUsbcScAlternateSetting* alternateSettingListRec = iAlternateSettingList->iTail;
  1.2874 +		TUsbcScAlternateSetting* alternateSettingListRecFound = NULL;
  1.2875 +		while (alternateSettingListRec)
  1.2876 +			{
  1.2877 +			if (alternateSettingListRec->iSetting == aInterfaceNumber)
  1.2878 +				{
  1.2879 +				alternateSettingListRecFound = alternateSettingListRec;
  1.2880 +				if (alternateSettingListRec->iPrevious == NULL)	//Interface is at HEAD OF List, Should only be if Interface is also at Tail of list
  1.2881 +					{
  1.2882 +					iAlternateSettingList->iHead = alternateSettingListRec->iNext;	// Should be NULL
  1.2883 +					if (alternateSettingListRec->iNext)
  1.2884 +						iAlternateSettingList->iHead->iPrevious = NULL;
  1.2885 +					}
  1.2886 +				else if (alternateSettingListRec->iNext == NULL) //Interface is at TAIL OF List
  1.2887 +					{
  1.2888 +					iAlternateSettingList->iTail = alternateSettingListRecFound->iPrevious;
  1.2889 +					iAlternateSettingList->iTail->iNext = NULL;
  1.2890 +					}
  1.2891 +				else	//Somewhere in the middle (would not expect this in normal operation, but here for completeness)
  1.2892 +					{
  1.2893 +					__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::DestroyInterface Middle interface!\n"));
  1.2894 +					alternateSettingListRec->iPrevious->iNext = alternateSettingListRec->iNext;
  1.2895 +					alternateSettingListRec->iNext->iPrevious = alternateSettingListRec->iPrevious;
  1.2896 +					}	
  1.2897 +
  1.2898 +				delete alternateSettingListRecFound;
  1.2899 +				break;
  1.2900 +				}
  1.2901 + 			alternateSettingListRec = alternateSettingListRec->iPrevious;
  1.2902 +			}
  1.2903 +		}
  1.2904 +	}
  1.2905 +
  1.2906 +//
  1.2907 +// SetupEp0
  1.2908 +//
  1.2909 +
  1.2910 +TInt DLddUsbcScChannel::SetupEp0()
  1.2911 +	{
  1.2912 +	__ASSERT_ALWAYS(iEp0Endpoint==NULL, Kern::Fault("DLddUsbcScChannel::SetupEp0", __LINE__));
  1.2913 +
  1.2914 +	TUsbcScEndpointInfo ep0Info = TUsbcScEndpointInfo(KUsbEpTypeControl, KUsbEpDirBidirect);
  1.2915 +	ep0Info.iSize =  iController->Ep0PacketSize();
  1.2916 +
  1.2917 +	TUsbcScEndpoint* ep0 = new TUsbcScEndpoint(this, iController, &ep0Info, 0);
  1.2918 +	if (ep0 == NULL)
  1.2919 +		{
  1.2920 +		return KErrNoMemory;
  1.2921 +		}
  1.2922 +
  1.2923 +	TInt r = ep0->Construct();
  1.2924 +	if (r != KErrNone)
  1.2925 +		{
  1.2926 +		delete ep0;
  1.2927 +		return KErrNoMemory;
  1.2928 +		}
  1.2929 +
  1.2930 +	ep0->SetRealEpNumber(0);
  1.2931 +	ep0->SetBuffer(NULL); // Cannot find it this way.
  1.2932 +
  1.2933 +	iEp0Endpoint = ep0;
  1.2934 +	return KErrNone;
  1.2935 +	}
  1.2936 +
  1.2937 +//
  1.2938 +// DestroyEp0
  1.2939 +//
  1.2940 +
  1.2941 +void DLddUsbcScChannel::DestroyEp0()
  1.2942 +	{
  1.2943 +	__KTRACE_OPT(KUSB, Kern::Printf(" DLddUsbcScChannel::DestroyEp0"));
  1.2944 +	delete iEp0Endpoint;
  1.2945 +	iEp0Endpoint = NULL;
  1.2946 +	}
  1.2947 +
  1.2948 +
  1.2949 +void DLddUsbcScChannel::RequestCallbackEp0(TAny* aDLddUsbcScChannel)
  1.2950 +    {
  1.2951 +	DLddUsbcScChannel* channel = (DLddUsbcScChannel*) aDLddUsbcScChannel;
  1.2952 +
  1.2953 +	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::RequestCallbackEp0"));
  1.2954 +
  1.2955 +	if (channel->ChannelClosing())
  1.2956 +		{
  1.2957 +		__KTRACE_OPT(KUSB, Kern::Printf("Channel Closing: Completion not accepted!"));
  1.2958 +		return;
  1.2959 +		}
  1.2960 +
  1.2961 +	switch (channel->iEp0Endpoint->iRequestCallbackInfo->iTransferDir)
  1.2962 +		{
  1.2963 +	case EControllerWrite:
  1.2964 +		channel->iBuffers[channel->iEP0InBuff].CompleteWrite();
  1.2965 +		return;
  1.2966 +	case EControllerRead:
  1.2967 +		channel->iBuffers[channel->iEP0OutBuff].CompleteRead();
  1.2968 +		return;
  1.2969 +	default:
  1.2970 +		Kern::Printf("DLddUsbcScChannel::RequestCallbackEp0 - Unexpected completion direction %d",channel->iEp0Endpoint->iRequestCallbackInfo->iTransferDir);
  1.2971 +		Kern::Fault("DLddUsbcScChannel::RequestCallbackEp0", __LINE__);
  1.2972 +		} 
  1.2973 +	}
  1.2974 +
  1.2975 +
  1.2976 +
  1.2977 +
  1.2978 +
  1.2979 +
  1.2980 +//
  1.2981 +// EndpointStatusChangeCallback
  1.2982 +//
  1.2983 +
  1.2984 +void DLddUsbcScChannel::EndpointStatusChangeCallback(TAny* aDLddUsbcScChannel)
  1.2985 +    {
  1.2986 +	__KTRACE_OPT(KUSB, Kern::Printf("EndpointStatusChangeCallback"));
  1.2987 +    DLddUsbcScChannel* dUsbc = (DLddUsbcScChannel*) aDLddUsbcScChannel;
  1.2988 +	if (dUsbc->iChannelClosing)
  1.2989 +		return;
  1.2990 +	TUint endpointState = dUsbc->iEndpointStatusCallbackInfo.State();
  1.2991 +	const TInt reqNo = (TInt) RDevUsbcScClient::ERequestEndpointStatusNotify;
  1.2992 +	if (dUsbc->iRequestStatus[reqNo])
  1.2993 +		{
  1.2994 +		__KTRACE_OPT(KUSB, Kern::Printf("EndpointStatusChangeCallback Notify status"));
  1.2995 +		DThread* client = dUsbc->iClient;
  1.2996 +		// set client descriptor length to zero
  1.2997 +		TInt r = Kern::ThreadRawWrite(client, dUsbc->iEndpointStatusChangePtr, &endpointState,
  1.2998 +									  sizeof(TUint), client);
  1.2999 +		if (r != KErrNone)
  1.3000 +			dUsbc->PanicClientThread(r);
  1.3001 +		Kern::RequestComplete(dUsbc->iClient, dUsbc->iRequestStatus[reqNo], r);
  1.3002 +		dUsbc->iEndpointStatusChangePtr = NULL;
  1.3003 +		}
  1.3004 +	}
  1.3005 +
  1.3006 +
  1.3007 +//
  1.3008 +// StatusChangeCallback
  1.3009 +//
  1.3010 +
  1.3011 +void DLddUsbcScChannel::StatusChangeCallback(TAny* aDLddUsbcScChannel)
  1.3012 +	{
  1.3013 +    DLddUsbcScChannel* dUsbc = (DLddUsbcScChannel*) aDLddUsbcScChannel;
  1.3014 +	if (dUsbc->iChannelClosing)
  1.3015 +		return;
  1.3016 +
  1.3017 +    TUsbcDeviceState deviceState;
  1.3018 +    TInt i;
  1.3019 + 	for (i = 0;
  1.3020 + 		 (i < KUsbcDeviceStateRequests) && ((deviceState = dUsbc->iStatusCallbackInfo.State(i)) != EUsbcNoState);
  1.3021 + 		 ++i)
  1.3022 +		{
  1.3023 + 		__KTRACE_OPT(KUSB, Kern::Printf("StatusChangeCallBack status=%d", deviceState));
  1.3024 +		if (deviceState & KUsbAlternateSetting)
  1.3025 +			{
  1.3026 +			dUsbc->ProcessAlternateSetting(deviceState);
  1.3027 +			}
  1.3028 +		else
  1.3029 +			{
  1.3030 +			dUsbc->ProcessDeviceState(deviceState);
  1.3031 +			// Send Status to EP0 buffer.		
  1.3032 +			dUsbc->iBuffers[dUsbc->iEP0OutBuff].SendEp0StatusPacket(deviceState);
  1.3033 +			}
  1.3034 +
  1.3035 +		// Only queue if userside is interested
  1.3036 +		if (dUsbc->iDeviceStatusNeeded)
  1.3037 +			{
  1.3038 +			dUsbc->iStatusFifo->AddStatusToQueue(deviceState);
  1.3039 +			const TInt reqNo = (TInt) RDevUsbcScClient::ERequestAlternateDeviceStatusNotify;
  1.3040 +			if (dUsbc->AlternateDeviceStateTestComplete())
  1.3041 +				Kern::RequestComplete(dUsbc->iClient, dUsbc->iRequestStatus[reqNo], KErrNone);
  1.3042 +			}
  1.3043 +		}
  1.3044 + 	// We don't want to be interrupted in the middle of this:
  1.3045 +	const TInt irqs = NKern::DisableInterrupts(2);
  1.3046 + 	dUsbc->iStatusCallbackInfo.ResetState();
  1.3047 +	NKern::RestoreInterrupts(irqs);
  1.3048 +	}
  1.3049 +
  1.3050 +
  1.3051 +void DLddUsbcScChannel::OtgFeatureChangeCallback(TAny* aDLddUsbcScChannel)
  1.3052 +    {
  1.3053 +	__KTRACE_OPT(KUSB, Kern::Printf("OtgFeatureChangeCallback"));
  1.3054 +    DLddUsbcScChannel* dUsbc = (DLddUsbcScChannel*) aDLddUsbcScChannel;
  1.3055 +	if (dUsbc->iChannelClosing)
  1.3056 +		return;
  1.3057 +
  1.3058 +    TUint8 features;
  1.3059 +    // No return value check. Assume OTG always supported here
  1.3060 +    dUsbc->iController->GetCurrentOtgFeatures(features);
  1.3061 +
  1.3062 +    const TInt reqNo = (TInt) RDevUsbcScClient::ERequestOtgFeaturesNotify;
  1.3063 +	if (dUsbc->iRequestStatus[reqNo])
  1.3064 +		{
  1.3065 +		__KTRACE_OPT(KUSB, Kern::Printf("OtgFeatureChangeCallback Notify status"));
  1.3066 +		TInt r = Kern::ThreadRawWrite(dUsbc->iClient, dUsbc->iOtgFeatureChangePtr,
  1.3067 +                                      &features, sizeof(TUint8), dUsbc->iClient);
  1.3068 +		if (r != KErrNone)
  1.3069 +			dUsbc->PanicClientThread(r);
  1.3070 +		Kern::RequestComplete(dUsbc->iClient, dUsbc->iRequestStatus[reqNo], r);
  1.3071 +		dUsbc->iOtgFeatureChangePtr = NULL;
  1.3072 +		}
  1.3073 +    }
  1.3074 +
  1.3075 +
  1.3076 +//
  1.3077 +// SelectAlternateSetting
  1.3078 +//
  1.3079 +
  1.3080 +TInt DLddUsbcScChannel::SelectAlternateSetting(TUint aAlternateSetting)
  1.3081 +	{
  1.3082 +	TUsbcScEndpoint* ep;
  1.3083 +
  1.3084 +	// First, find the alt setting record, which corresponds to the alt setting number.
  1.3085 +	TUsbcScAlternateSetting* alternateSettingListRec;
  1.3086 +	if(iAlternateSettingList)
  1.3087 +		{
  1.3088 +		for (alternateSettingListRec = iAlternateSettingList->iHead; alternateSettingListRec; alternateSettingListRec = alternateSettingListRec->iNext)
  1.3089 +			if (alternateSettingListRec->iSetting == aAlternateSetting)
  1.3090 +				{
  1.3091 +				// Record has been located.
  1.3092 +
  1.3093 +				// Update current ep setting vars 
  1.3094 +				iEndpoint = alternateSettingListRec->iEndpoint;
  1.3095 +				iNumberOfEndpoints = alternateSettingListRec->iNumberOfEndpoints;
  1.3096 +
  1.3097 +
  1.3098 +
  1.3099 +				// Reset buffers for new ep set
  1.3100 +				for (TInt i = 1; i <= KMaxEndpointsPerClient; i++)
  1.3101 +					{
  1.3102 +					ep = alternateSettingListRec->iEndpoint[i];
  1.3103 +					if (ep!=NULL)
  1.3104 +						ep->StartBuffer(); // Buffer::StartEndpoint(...)   sets the necessary parameters to the buffer, for use for a perticular endpoint.
  1.3105 +					}
  1.3106 +
  1.3107 +				return KErrNone;
  1.3108 +				}
  1.3109 +		}
  1.3110 +	return KErrGeneral;
  1.3111 +	}
  1.3112 +
  1.3113 +/* The user calls this to move into the next alternate setting.  After this call, it is assumed the user wants to
  1.3114 +Transmit using endpoints belonging to this alternate Setting.  Writes to the IN endpoints will be allowed until
  1.3115 +the host changed the alternate setting again
  1.3116 +Returns a 32 int with the top 16 bits represents the sequance, and the botten, the alternatre setting no.
  1.3117 +*/
  1.3118 +TInt32 DLddUsbcScChannel::StartNextInAlternateSetting()
  1.3119 +	{
  1.3120 +	iUserKnowsAltSetting = ETrue;
  1.3121 +	return iAsSeq<<16 | iAlternateSetting;
  1.3122 +	} 
  1.3123 +
  1.3124 +
  1.3125 +//
  1.3126 +// EpFromAlternateSetting
  1.3127 +//
  1.3128 +
  1.3129 +TInt DLddUsbcScChannel::EpFromAlternateSetting(TUint aAlternateSetting, TInt aEndpoint)
  1.3130 +	{
  1.3131 +	TUsbcScAlternateSetting* alternateSettingListRec = iAlternateSettingList->iHead;
  1.3132 +	while (alternateSettingListRec)
  1.3133 +		{
  1.3134 +		if (alternateSettingListRec->iSetting == aAlternateSetting)
  1.3135 +			{
  1.3136 +			if ((aEndpoint <= alternateSettingListRec->iNumberOfEndpoints) &&
  1.3137 +				(aEndpoint > 0))
  1.3138 +				{
  1.3139 +				return alternateSettingListRec->iEndpoint[aEndpoint]->RealEpNumber();
  1.3140 +				}
  1.3141 +			else
  1.3142 +				{
  1.3143 +				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: aEndpoint %d wrong for aAlternateSetting %d",
  1.3144 +												  aEndpoint, aAlternateSetting));
  1.3145 +				return KErrNotFound;
  1.3146 +				}
  1.3147 +			}
  1.3148 +		alternateSettingListRec = alternateSettingListRec->iNext;
  1.3149 +		}
  1.3150 +	__KTRACE_OPT(KPANIC, Kern::Printf("  Error: no aAlternateSetting %d found", aAlternateSetting));
  1.3151 +	return KErrNotFound;
  1.3152 +	}
  1.3153 +
  1.3154 +//
  1.3155 +// ProcessAlternateSetting
  1.3156 +//
  1.3157 +
  1.3158 +TInt DLddUsbcScChannel::ProcessAlternateSetting(TUint aAlternateSetting)
  1.3159 +	{
  1.3160 +
  1.3161 +	TUint newSetting = aAlternateSetting&(~KUsbAlternateSetting);
  1.3162 +	__KTRACE_OPT(KUSB, Kern::Printf("ProcessAlternateSetting 0x%08x selecting alternate setting 0x%08x", aAlternateSetting, newSetting));
  1.3163 +	iUserKnowsAltSetting=EFalse;
  1.3164 +	iAlternateSetting = newSetting;
  1.3165 +	iAsSeq++; 
  1.3166 +	
  1.3167 +	ResetInterface(KErrUsbInterfaceChange);					// kill any outstanding IN transfers
  1.3168 +
  1.3169 +	TInt r = SelectAlternateSetting(newSetting);
  1.3170 +	if (r != KErrNone)
  1.3171 +		return r;
  1.3172 +
  1.3173 +
  1.3174 +	StartEpReads();
  1.3175 +    return KErrNone;
  1.3176 +	}
  1.3177 +
  1.3178 +
  1.3179 +//
  1.3180 +//  ProcessDeviceState
  1.3181 +//
  1.3182 +// Called from StatusChangeCallback.
  1.3183 +
  1.3184 +TInt DLddUsbcScChannel::ProcessDeviceState(TUsbcDeviceState aDeviceState)
  1.3185 +	{
  1.3186 +	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::ProcessDeviceState(%d -> %d)", iDeviceState, aDeviceState));
  1.3187 +	if (iDeviceState == aDeviceState)
  1.3188 +		{
  1.3189 +		__KTRACE_OPT(KUSB, Kern::Printf("  No state change => nothing to be done."));
  1.3190 +		return KErrNone;
  1.3191 +		}
  1.3192 +	if (iDeviceState == EUsbcDeviceStateSuspended)
  1.3193 +		{
  1.3194 +		__KTRACE_OPT(KUSB, Kern::Printf("  Coming out of Suspend: old state = %d", iOldDeviceState));
  1.3195 +		iDeviceState = iOldDeviceState;
  1.3196 +		if (iDeviceState == aDeviceState)
  1.3197 +			{
  1.3198 +			__KTRACE_OPT(KUSB, Kern::Printf("  New state same as before Suspend => nothing to be done."));
  1.3199 +			return KErrNone;
  1.3200 +			}
  1.3201 +		}
  1.3202 +	TBool renumerateState = (aDeviceState == EUsbcDeviceStateConfigured);
  1.3203 +	TBool deconfigured = EFalse;
  1.3204 +	TInt cancellationCode = KErrNone;
  1.3205 +	if (aDeviceState == EUsbcDeviceStateSuspended)
  1.3206 +		{
  1.3207 +		__KTRACE_OPT(KUSB, Kern::Printf("  Suspending..."));
  1.3208 +		iOldDeviceState = iDeviceState;
  1.3209 +		// Put PSL into low power mode here
  1.3210 +		}
  1.3211 +	else
  1.3212 +		{
  1.3213 +		deconfigured = (iDeviceState == EUsbcDeviceStateConfigured &&
  1.3214 +						aDeviceState != EUsbcDeviceStateConfigured);
  1.3215 +		if (iDeviceState == EUsbcDeviceStateConfigured)
  1.3216 +			{
  1.3217 +			if (aDeviceState == EUsbcDeviceStateUndefined)
  1.3218 +				cancellationCode = KErrUsbCableDetached;
  1.3219 +			else if (aDeviceState == EUsbcDeviceStateAddress)
  1.3220 +				cancellationCode = KErrUsbDeviceNotConfigured;
  1.3221 +			else if (aDeviceState == EUsbcDeviceStateDefault)
  1.3222 +				cancellationCode = KErrUsbDeviceBusReset;
  1.3223 +			else
  1.3224 +				cancellationCode = KErrUsbDeviceNotConfigured;
  1.3225 +			}
  1.3226 +		}
  1.3227 +	iDeviceState = aDeviceState;
  1.3228 +	if (iValidInterface || iOwnsDeviceControl)
  1.3229 +		{
  1.3230 +
  1.3231 +		// This LDD may not own an interface. It could be some manager reenumerating
  1.3232 +		// after its subordinate LDDs have setup their interfaces.
  1.3233 +		if (deconfigured)
  1.3234 +			{
  1.3235 +		    DeConfigure(cancellationCode);
  1.3236 +			}
  1.3237 +		else if (renumerateState)
  1.3238 +			{
  1.3239 + 			__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScChannel:: Reumerated!"));
  1.3240 +			// Select main interface & latch in new endpoint set
  1.3241 +			SelectAlternateSetting(0);
  1.3242 +			__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScChannel:: StartReads!"));
  1.3243 +			StartEpReads();
  1.3244 +			}
  1.3245 +		}
  1.3246 +
  1.3247 +	const TInt reqNo = (TInt) RDevUsbcScClient::ERequestReEnumerate;
  1.3248 +	if (renumerateState && iRequestStatus[reqNo])
  1.3249 +		{
  1.3250 +		// This lot must be done if we are reenumerated
  1.3251 +		Kern::RequestComplete(iClient, iRequestStatus[reqNo], KErrNone);
  1.3252 +		}
  1.3253 +
  1.3254 +    return KErrNone;
  1.3255 +    }
  1.3256 +
  1.3257 +
  1.3258 +TBool DLddUsbcScChannel::AlternateDeviceStateTestComplete()
  1.3259 +	{
  1.3260 +	TBool completeNow = EFalse;
  1.3261 +	const TInt reqNo = (TInt) RDevUsbcScClient::ERequestAlternateDeviceStatusNotify;
  1.3262 +	if (iRequestStatus[reqNo])
  1.3263 +		{
  1.3264 +		// User req is outstanding
  1.3265 +		TUint32 deviceState;
  1.3266 +		if (iStatusFifo->GetDeviceQueuedStatus(deviceState) == KErrNone)
  1.3267 +			{
  1.3268 +			// Device state waiting to be sent userside
  1.3269 +			completeNow = ETrue;
  1.3270 +			__KTRACE_OPT(KUSB, Kern::Printf("StatusChangeCallback Notify status"));
  1.3271 +			// set client descriptor length to zero
  1.3272 +			TInt r = Kern::ThreadRawWrite(iClient, iStatusChangePtr, &deviceState,
  1.3273 +										  sizeof(TUint32), iClient);
  1.3274 +			if (r != KErrNone)
  1.3275 +				PanicClientThread(r);
  1.3276 +			iStatusChangePtr = NULL;
  1.3277 +			}
  1.3278 +		}
  1.3279 +	return completeNow;
  1.3280 +	}
  1.3281 +
  1.3282 +
  1.3283 +void DLddUsbcScChannel::DeConfigure(TInt aErrorCode)
  1.3284 +	{
  1.3285 +	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::DeConfigure()"));
  1.3286 +	// Called after deconfiguration. Cancels transfers on all endpoints.
  1.3287 +	ResetInterface(aErrorCode);
  1.3288 +	// Cancel the endpoint status notify request if it is outstanding.
  1.3289 +	const TInt KEpNotReq = RDevUsbcScClient::ERequestEndpointStatusNotify;
  1.3290 +	if (iRequestStatus[KEpNotReq])
  1.3291 +		{
  1.3292 +		CancelNotifyEndpointStatus();
  1.3293 +		Kern::RequestComplete(iClient, iRequestStatus[KEpNotReq], aErrorCode);
  1.3294 +		}
  1.3295 +	// We have to reset the alternate setting number when the config goes away.
  1.3296 + 	SelectAlternateSetting(0);
  1.3297 +	iAlternateSetting = 0;
  1.3298 +	}
  1.3299 +
  1.3300 +
  1.3301 +void DLddUsbcScChannel::StartEpReads()
  1.3302 +	{
  1.3303 +	// Queued after enumeration. Starts reads on all endpoints.
  1.3304 +	// The endpoint itself decides if it can do a read
  1.3305 +	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::StartEpReads - 1"));
  1.3306 +	
  1.3307 +	TInt i;
  1.3308 +	TInt8 needsPacket;
  1.3309 +
  1.3310 +	for (i=0; i<iNumBuffers; i++)
  1.3311 +		{
  1.3312 +		__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::StartEpReads - 2 %d",i));
  1.3313 +
  1.3314 +		needsPacket = iBuffers[i].iNeedsPacket;
  1.3315 +		if (needsPacket)
  1.3316 +			{
  1.3317 +			__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::StartEpReads - 3"));
  1.3318 +			iBuffers[i].UpdateBufferList(0,0,(needsPacket==TUsbcScBuffer::KEpIsStarting));
  1.3319 +			}
  1.3320 +		}
  1.3321 +
  1.3322 +	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::StartEpReads - 4"));
  1.3323 +
  1.3324 +		// now update ep0
  1.3325 +		iBuffers[iEP0OutBuff].Ep0CancelLddRead();
  1.3326 +		iBuffers[iEP0OutBuff].UpdateBufferList(0,0);
  1.3327 +	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::StartEpReads - 5"));
  1.3328 +
  1.3329 +	}
  1.3330 +
  1.3331 +
  1.3332 +void DLddUsbcScChannel::ResetInterface(TInt aErrorCode)
  1.3333 +	{
  1.3334 +	if (!iValidInterface && !iOwnsDeviceControl)
  1.3335 +			return;
  1.3336 +		
  1.3337 +	TInt i;
  1.3338 +	for (i=0; i<iNumBuffers; i++)
  1.3339 +		{
  1.3340 +		iBuffers[i].iNeedsPacket=TUsbcScBuffer::KNoEpAssigned;
  1.3341 +		}
  1.3342 +
  1.3343 +	TUsbcScBuffer* buffer;
  1.3344 +
  1.3345 +	for (i = 1; i <= iNumberOfEndpoints; i++)
  1.3346 +		{
  1.3347 +		// Firstly, cancel ('garbge collect') any stale reads/writes into PIL.
  1.3348 +
  1.3349 +		__KTRACE_OPT(KUSB, Kern::Printf("Cancelling transfer ep=%d", i));
  1.3350 +		iEndpoint[i]->AbortTransfer();
  1.3351 +
  1.3352 +		// All OUT endpoints need a packet sent, to indicate the termination of the current ep 'pipe'.
  1.3353 +		// This will complete any current read, or will be read later.
  1.3354 +		// All IN endpoints must be simply cancelled, including anything queued.
  1.3355 +		// Ep0 operates outside alt settings, and so we don't cancel anything.
  1.3356 +
  1.3357 +		buffer=iEndpoint[i]->GetBuffer();
  1.3358 +		if (buffer->iDirection==KUsbcScIn)
  1.3359 +			{
  1.3360 +			buffer->iStatusList.Complete(KErrCancel);	//aErrorCode 
  1.3361 +			buffer->iStatusList.CancelQueued();			//aErrorCode
  1.3362 +			}
  1.3363 +		else
  1.3364 +			buffer->iNeedsPacket=TUsbcScBuffer::KEpIsEnding;	// We will send a packet on re-start, which doubles as a 'cancel'
  1.3365 +															 	// for the old alt setting.
  1.3366 +		}
  1.3367 +	}
  1.3368 +
  1.3369 +
  1.3370 +
  1.3371 +void DLddUsbcScChannel::EmergencyCompleteDfc(TAny* aDLddUsbcScChannel)
  1.3372 +	{
  1.3373 +	((DLddUsbcScChannel*) aDLddUsbcScChannel)->DoEmergencyComplete();
  1.3374 +	}
  1.3375 +
  1.3376 +TInt DLddUsbcScChannel::DoEmergencyComplete()
  1.3377 +	{
  1.3378 +	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcScChannel::DoEmergencyComplete"));
  1.3379 +	// cancel any pending DFCs
  1.3380 +	// complete all client requests
  1.3381 +
  1.3382 +	TUsbcScBuffer* buffer;
  1.3383 +	TInt i;	
  1.3384 +	// Complete EP0 request
  1.3385 +
  1.3386 +	TInt direction=iEp0Endpoint->iRequestCallbackInfo->iTransferDir;
  1.3387 +	if (direction==EControllerWrite)
  1.3388 +		{
  1.3389 +		iBuffers[iEP0InBuff].iStatusList.CancelQueued();
  1.3390 +		iBuffers[iEP0InBuff].iStatusList.Complete(KErrDisconnected);
  1.3391 +		}
  1.3392 +	else if (direction==EControllerRead)
  1.3393 +		{
  1.3394 +		iBuffers[iEP0OutBuff].iStatusList.CancelQueued();
  1.3395 +		iBuffers[iEP0OutBuff].iStatusList.Complete(KErrDisconnected);
  1.3396 +		}
  1.3397 +		
  1.3398 +	// Complete other Eps request
  1.3399 +	for (i = 1; i <= iNumberOfEndpoints; i++)
  1.3400 +		{
  1.3401 +		__KTRACE_OPT(KUSB, Kern::Printf("Cancelling transfer ep=%d", i));
  1.3402 +		buffer=iEndpoint[i]->GetBuffer();
  1.3403 +		buffer->iStatusList.CancelQueued();
  1.3404 +		buffer->iStatusList.Complete(KErrDisconnected);
  1.3405 +		}
  1.3406 +
  1.3407 +	// Complete remaining requests
  1.3408 +
  1.3409 +    for (TInt i = 0; i < KUsbcMaxRequests; i++)
  1.3410 +        {
  1.3411 +        if (iRequestStatus[i])
  1.3412 +            {
  1.3413 +            __KTRACE_OPT(KUSB, Kern::Printf("Complete request 0x%x", iRequestStatus[i]));
  1.3414 +            Kern::RequestComplete(iClient, iRequestStatus[i], KErrDisconnected);
  1.3415 +            }
  1.3416 +        }
  1.3417 +    iStatusCallbackInfo.Cancel();
  1.3418 +    iEndpointStatusCallbackInfo.Cancel();
  1.3419 +    iOtgFeatureCallbackInfo.Cancel();
  1.3420 +
  1.3421 +	return KErrNone;
  1.3422 +	}
  1.3423 +
  1.3424 +
  1.3425 +void DLddUsbcScChannel::PanicClientThread(TInt aReason)
  1.3426 +	{
  1.3427 +	Kern::ThreadKill(iClient, EExitPanic, aReason, KUsbLDDKillCat);
  1.3428 +	}
  1.3429 +
  1.3430 +// End DLddUsbcScChannel
  1.3431 +
  1.3432 +/*****************************************************************************\
  1.3433 +*    TUsbcScEndpoint                                                          *
  1.3434 +*                                                                             *
  1.3435 +*                                                                             *
  1.3436 +*                                                                             *
  1.3437 +\*****************************************************************************/
  1.3438 +
  1.3439 +
  1.3440 +// Constructor
  1.3441 +TUsbcScEndpoint::TUsbcScEndpoint(DLddUsbcScChannel* aLDD, DUsbClientController* aController,
  1.3442 +							 const TUsbcScEndpointInfo* aEndpointInfo, TInt aEndpointNum
  1.3443 +							 )
  1.3444 +	: iRequestCallbackInfo(NULL),
  1.3445 +	  iController(aController),
  1.3446 +	  iEndpointInfo(*aEndpointInfo),
  1.3447 +	  iClientReadPending(EFalse),
  1.3448 +	  iClientWritePending(EFalse),
  1.3449 +	  iEndpointNumber(aEndpointNum),
  1.3450 +	  iRealEpNumber(-1),
  1.3451 +	  iLdd(aLDD),
  1.3452 +	  iError(KErrNone),
  1.3453 +	  iBytesTransferred(0),
  1.3454 +	  iBuffer(NULL)
  1.3455 +	{
  1.3456 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScEndpoint::TUsbcScEndpoint"));
  1.3457 +	}
  1.3458 +
  1.3459 +
  1.3460 +TInt TUsbcScEndpoint::Construct()
  1.3461 +	{
  1.3462 +	__KTRACE_OPT(KUSB,Kern::Printf("TUsbcScEndpoint::TUsbcScEndpoint iEndpointNumber %d\n",iEndpointNumber));
  1.3463 +
  1.3464 +	iRequestCallbackInfo = new TUsbcRequestCallback(iLdd,
  1.3465 +													iEndpointNumber,
  1.3466 +													(iEndpointNumber==0)?DLddUsbcScChannel::RequestCallbackEp0:TUsbcScEndpoint::RequestCallback,
  1.3467 +													(iEndpointNumber==0)?  (TAny*) iLdd:  (TAny*) this,
  1.3468 +													iLdd->iDfcQ,
  1.3469 +													KUsbRequestCallbackPriority);
  1.3470 +
  1.3471 +	return (iRequestCallbackInfo == NULL)?KErrNoMemory:KErrNone;
  1.3472 +	}
  1.3473 +
  1.3474 +
  1.3475 +TUsbcScEndpoint::~TUsbcScEndpoint()
  1.3476 +	{
  1.3477 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScEndpoint::~TUsbcScEndpoint(%d)", iEndpointNumber));
  1.3478 +	AbortTransfer();
  1.3479 +	delete iRequestCallbackInfo;
  1.3480 +	}
  1.3481 +
  1.3482 +// This is called by the PIL, on return from a read or write.
  1.3483 +// Inturn it calls either the read or write function for that buffer.
  1.3484 +
  1.3485 +void TUsbcScEndpoint::RequestCallback(TAny* aTUsbcScEndpoint)
  1.3486 +	{
  1.3487 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScEndpoint::RequestCallback"));
  1.3488 +
  1.3489 +	if (((TUsbcScEndpoint*)aTUsbcScEndpoint)->iLdd->ChannelClosing())
  1.3490 +		{
  1.3491 +		__KTRACE_OPT(KUSB, Kern::Printf("Channel Closing: Completion not accepted!"));
  1.3492 +		return;
  1.3493 +		}
  1.3494 +
  1.3495 +	switch (((TUsbcScEndpoint*) aTUsbcScEndpoint)->iRequestCallbackInfo->iTransferDir)
  1.3496 +	{
  1.3497 +	case EControllerWrite:
  1.3498 +		((TUsbcScEndpoint*) aTUsbcScEndpoint)->iBuffer->CompleteWrite();
  1.3499 +		return;
  1.3500 +	case EControllerRead:
  1.3501 +		((TUsbcScEndpoint*) aTUsbcScEndpoint)->iBuffer->CompleteRead();
  1.3502 +		return;
  1.3503 +	default:
  1.3504 +		Kern::Printf("TUsbcScEndpoint::RequestCallback - Unexpected compleation direction %d",((TUsbcScEndpoint*) aTUsbcScEndpoint)->iRequestCallbackInfo->iTransferDir);
  1.3505 +		Kern::Fault("TUsbcScEndpoint::RequestCallback", __LINE__);
  1.3506 +	} 
  1.3507 +	}
  1.3508 +
  1.3509 +
  1.3510 +/*
  1.3511 +
  1.3512 +This is used to tidy up cancel calls into the PIL, regardless of them being reads or writes
  1.3513 +
  1.3514 +*/
  1.3515 +
  1.3516 +void TUsbcScEndpoint::AbortTransfer()
  1.3517 +	{
  1.3518 +	if (!iLdd->iRealizeCalled)
  1.3519 +		{
  1.3520 +		__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScEndpoint::AbortTransfer Ep# %d Real Ep # %d - N.R.",iEndpointNumber, iRealEpNumber));
  1.3521 +		return;
  1.3522 +		} 
  1.3523 +	else
  1.3524 +		{
  1.3525 +		__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScEndpoint::AbortTransfer Ep# %d Real Ep # %d",iEndpointNumber, iRealEpNumber));
  1.3526 +		}
  1.3527 +
  1.3528 +	
  1.3529 +	if (iBuffer && (iBuffer->iStatusList.iState) || (!iRealEpNumber))
  1.3530 +		{
  1.3531 +		if (iRequestCallbackInfo->iTransferDir==EControllerWrite)
  1.3532 +			iController->CancelWriteBuffer(iLdd, iRealEpNumber);
  1.3533 +		else if (iRequestCallbackInfo->iTransferDir==EControllerRead)
  1.3534 +			iController->CancelReadBuffer(iLdd, iRealEpNumber);
  1.3535 +		else
  1.3536 +			{
  1.3537 +			if (iEndpointNumber!=0) // endpoint zero starts off not sent in any direction, then keeps changing.
  1.3538 +				{
  1.3539 +				__KTRACE_OPT(KUSB,Kern::Printf("\nTUsbcScEndpoint::AbortTransfer WARNING: Invalid Direction %d on (%d,%d)!\n",iRequestCallbackInfo->iTransferDir,iEndpointNumber, iRealEpNumber));
  1.3540 +				}
  1.3541 +			else
  1.3542 +				{
  1.3543 +				__KTRACE_OPT(KUSB, Kern::Printf("\nTUsbcScEndpoint::AbortTransfer Can't stop direction %d on (%d,%d)!\n",iRequestCallbackInfo->iTransferDir,iEndpointNumber, iRealEpNumber));
  1.3544 +				}
  1.3545 +			}
  1.3546 +		}
  1.3547 +	else if (!iBuffer)
  1.3548 +		{
  1.3549 +		__KTRACE_OPT(KUSB,Kern::Printf("\nTUsbcScEndpoint::AbortTransfer WARNING: iBuffer is NULL on (%d,%d)\n",iEndpointNumber, iRealEpNumber));
  1.3550 +		return;
  1.3551 +		}
  1.3552 +	
  1.3553 +	if (iRequestCallbackInfo)
  1.3554 +		iRequestCallbackInfo->iDfc.Cancel();
  1.3555 +	else
  1.3556 +		{
  1.3557 +		__KTRACE_OPT(KUSB,Kern::Printf("\nTUsbcScEndpoint::AbortTransfer WARNING: iRequestCallbackInfo is NULL\n"));
  1.3558 +		}
  1.3559 +		
  1.3560 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScEndpoint Done."));
  1.3561 +	}
  1.3562 +
  1.3563 +// End TUsbcScEndpoint
  1.3564 +
  1.3565 +
  1.3566 +/*****************************************************************************\
  1.3567 +*    TUsbcScAlternateSettingList                                              *
  1.3568 +*                                                                             *
  1.3569 +*                                                                             *
  1.3570 +*                                                                             *
  1.3571 +\*****************************************************************************/
  1.3572 +
  1.3573 +
  1.3574 +TUsbcScAlternateSetting::TUsbcScAlternateSetting()
  1.3575 +	: iNext(NULL),
  1.3576 +	  iPrevious(NULL),
  1.3577 +	  iNumberOfEndpoints(0),
  1.3578 +	  iSetting(0)
  1.3579 +	{
  1.3580 +	for (TInt i = 0; i <= KMaxEndpointsPerClient; i++)
  1.3581 +		{
  1.3582 +		iEndpoint[i] = NULL;
  1.3583 +		}
  1.3584 +	}
  1.3585 +
  1.3586 +
  1.3587 +TUsbcScAlternateSetting::~TUsbcScAlternateSetting()
  1.3588 +	{
  1.3589 +	__KTRACE_OPT(KUSB, Kern::Printf("TUsbcScAlternateSetting::~TUsbcScAlternateSetting()"));
  1.3590 +	for (TInt i = 0; i <= KMaxEndpointsPerClient; i++)
  1.3591 +		{
  1.3592 +		delete iEndpoint[i];
  1.3593 +		}
  1.3594 +	}
  1.3595 +
  1.3596 +// End TUsbcScAlternateSettingList
  1.3597 +
  1.3598 +
  1.3599 +
  1.3600 +TUsbcScAlternateSettingList::TUsbcScAlternateSettingList()
  1.3601 +	: iHead(NULL),
  1.3602 +	  iTail(NULL)
  1.3603 +	{
  1.3604 +	}
  1.3605 +
  1.3606 +TUsbcScAlternateSettingList::~TUsbcScAlternateSettingList()
  1.3607 +	{
  1.3608 +	}
  1.3609 +
  1.3610 +
  1.3611 +
  1.3612 +/*****************************************************************************\
  1.3613 +*   TUsbcDeviceStatusQueue                                                    *
  1.3614 +*                                                                             *
  1.3615 +*                                                                             *
  1.3616 +*                                                                             *
  1.3617 +\*****************************************************************************/
  1.3618 +
  1.3619 +
  1.3620 +TUsbcDeviceStatusQueue::TUsbcDeviceStatusQueue()
  1.3621 +	{
  1.3622 +	FlushQueue();
  1.3623 +	}
  1.3624 +
  1.3625 +
  1.3626 +void TUsbcDeviceStatusQueue::FlushQueue()
  1.3627 +	{
  1.3628 +	for (TInt i = 0; i < KUsbDeviceStatusQueueDepth; i++)
  1.3629 +		{
  1.3630 +		iDeviceStatusQueue[i] = KUsbDeviceStatusNull;
  1.3631 +		}
  1.3632 +	iStatusQueueHead = 0;
  1.3633 +	}
  1.3634 +
  1.3635 +
  1.3636 +void TUsbcDeviceStatusQueue::AddStatusToQueue(TUint32 aDeviceStatus)
  1.3637 +	{
  1.3638 +	// Only add a new status if it is not a duplicate of the one at the head of the queue
  1.3639 +	if (!(iStatusQueueHead != 0 &&
  1.3640 +		  iDeviceStatusQueue[iStatusQueueHead - 1] == aDeviceStatus))
  1.3641 +		{
  1.3642 +		if (iStatusQueueHead == KUsbDeviceStatusQueueDepth)
  1.3643 +			{
  1.3644 +			// Discard item at tail of queue
  1.3645 +			TUint32 status;
  1.3646 +			GetDeviceQueuedStatus(status);
  1.3647 +			}
  1.3648 +		iDeviceStatusQueue[iStatusQueueHead] = aDeviceStatus;
  1.3649 +		iStatusQueueHead++;
  1.3650 +		}
  1.3651 +	}
  1.3652 +
  1.3653 +
  1.3654 +TInt TUsbcDeviceStatusQueue::GetDeviceQueuedStatus(TUint32& aDeviceStatus)
  1.3655 +	{
  1.3656 +	TInt r = KErrNone;
  1.3657 +	if (iStatusQueueHead <= 0)
  1.3658 +		{
  1.3659 +		r = KErrGeneral;
  1.3660 +		aDeviceStatus = KUsbDeviceStatusNull;
  1.3661 +		}
  1.3662 +	else
  1.3663 +		{
  1.3664 +		aDeviceStatus = iDeviceStatusQueue[0];
  1.3665 +		for(TInt i = 1; i < KUsbDeviceStatusQueueDepth; i++)
  1.3666 +			{
  1.3667 +			TUint32 s = iDeviceStatusQueue[i];
  1.3668 +			iDeviceStatusQueue[i - 1] = s;
  1.3669 +			}
  1.3670 +		iStatusQueueHead--;
  1.3671 +		iDeviceStatusQueue[KUsbDeviceStatusQueueDepth - 1] = KUsbDeviceStatusNull;
  1.3672 +		}
  1.3673 +	return r;
  1.3674 +	}
  1.3675 +
  1.3676 +// End TUsbcDeviceStatusQueue
  1.3677 +
  1.3678 +//---
  1.3679 +