traceservices/tracefw/integ_test/ulogger/TEF/device-driver/src/d_usbc.cpp
changeset 0 08ec8eefde2f
child 23 26645d81f48d
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32\drivers\usbc\d_usbc.cpp
       
    15 // LDD for USB Device driver stack:
       
    16 // The channel object.
       
    17 // 
       
    18 //
       
    19 
       
    20 /**
       
    21  @file d_usbc.cpp
       
    22  @internalTechnology
       
    23 */
       
    24 
       
    25 #include <usbc.h>
       
    26 #include <e32utrace.h>
       
    27 #include "te_setfilterparameters.h"
       
    28 #include "te_utracecmds.h"
       
    29 
       
    30 
       
    31 _LIT(KUsbLddName, "Usbc");
       
    32 
       
    33 static const TInt KUsbRequestCallbackPriority = 2;
       
    34 
       
    35 
       
    36 // Quick sanity check on endpoint properties
       
    37 static TBool ValidateEndpoint(const TUsbcEndpointInfo* aEndpointInfo)
       
    38 	{
       
    39 	const TUint dir = aEndpointInfo->iDir;
       
    40 	const TInt size = aEndpointInfo->iSize;
       
    41 	if (size <= 0)
       
    42 		return EFalse;
       
    43 
       
    44 	switch (aEndpointInfo->iType)
       
    45 		{
       
    46 	case KUsbEpTypeControl:
       
    47 		if (dir != KUsbEpDirBidirect || size > 64)
       
    48 			return EFalse;
       
    49 		break;
       
    50 	case KUsbEpTypeIsochronous:
       
    51 		if ((dir != KUsbEpDirIn && dir != KUsbEpDirOut) || size > 1024)
       
    52 			return EFalse;
       
    53 		break;
       
    54 	case KUsbEpTypeBulk:
       
    55 		if ((dir != KUsbEpDirIn && dir != KUsbEpDirOut) || size > 512)
       
    56 			return EFalse;
       
    57 		break;
       
    58 	case KUsbEpTypeInterrupt:
       
    59 		if ((dir != KUsbEpDirIn && dir != KUsbEpDirOut) || size > 1024)
       
    60 			return EFalse;
       
    61 		break;
       
    62 	default:
       
    63 		return EFalse;
       
    64 		}
       
    65 	return ETrue;
       
    66 	}
       
    67 
       
    68 
       
    69 /** Real entry point from the Kernel: return a new driver.
       
    70  */
       
    71 DECLARE_STANDARD_LDD()
       
    72 	{
       
    73 	return new DUsbcLogDevice;
       
    74 	}
       
    75 
       
    76 
       
    77 /** Create a channel on the device.
       
    78 
       
    79 	@internalComponent
       
    80 */
       
    81 TInt DUsbcLogDevice::Create(DLogicalChannelBase*& aChannel)
       
    82 	{
       
    83 
       
    84 	aChannel = new DLddUsbcChannel;
       
    85 	return aChannel ? KErrNone : KErrNoMemory;
       
    86 	}
       
    87 
       
    88 
       
    89 DUsbcLogDevice::DUsbcLogDevice()
       
    90       {
       
    91      
       
    92 	  iParseMask = KDeviceAllowUnit;
       
    93 	  iUnitsMask = 0xffffffff;								// Leave units decision to the Controller
       
    94       iVersion = TVersion(KUsbcMajorVersion, KUsbcMinorVersion, KUsbcBuildVersion);
       
    95       }
       
    96 
       
    97 
       
    98 TInt DUsbcLogDevice::Install()
       
    99 	{
       
   100 
       
   101 	// Only proceed if we have the Controller underneath us
       
   102 	if (!DUsbClientController::UsbcControllerPointer())
       
   103 		{
       
   104 		//__KTRACE_OPT(KPANIC, Kern::Printf("LDD Install: USB Controller Not Present"));
       
   105 		return KErrGeneral;
       
   106 		}
       
   107 	return SetName(&KUsbLddName);
       
   108 	}
       
   109 
       
   110 
       
   111 //
       
   112 // Return the USB controller capabilities.
       
   113 //
       
   114 void DUsbcLogDevice::GetCaps(TDes8& aDes) const
       
   115 	{
       
   116 	TPckgBuf<TCapsDevUsbc> b;
       
   117 	b().version = iVersion;
       
   118 	Kern::InfoCopy(aDes, b);
       
   119 	}
       
   120 
       
   121 
       
   122 //
       
   123 // Constructor
       
   124 //
       
   125 DLddUsbcChannel::DLddUsbcChannel()
       
   126 	: iValidInterface(EFalse),
       
   127 	  iAlternateSettingList(NULL),
       
   128 	  iCompleteAllCallbackInfo(this, DLddUsbcChannel::EmergencyCompleteDfc, KUsbRequestCallbackPriority),
       
   129 	  iStatusChangePtr(NULL),
       
   130 	  iStatusCallbackInfo(this, DLddUsbcChannel::StatusChangeCallback, KUsbRequestCallbackPriority),
       
   131 	  iEndpointStatusChangePtr(NULL),
       
   132 	  iEndpointStatusCallbackInfo(this, DLddUsbcChannel::EndpointStatusChangeCallback,
       
   133 								  KUsbRequestCallbackPriority),
       
   134       iOtgFeatureChangePtr(NULL),
       
   135       iOtgFeatureCallbackInfo(this, DLddUsbcChannel::OtgFeatureChangeCallback, KUsbRequestCallbackPriority),
       
   136 	  iBufferBaseEp0(NULL),
       
   137 	  iBufferSizeEp0(0),
       
   138 	  iNumberOfEndpoints(0),
       
   139 	  iHwChunkIN(NULL),
       
   140 	  iHwChunkOUT(NULL),
       
   141 	  iHwChunkEp0(NULL),
       
   142 	  iDeviceState(EUsbcDeviceStateUndefined),
       
   143 	  iOwnsDeviceControl(EFalse),
       
   144 	  iAlternateSetting(0),
       
   145 	  iDeviceStatusNeeded(EFalse),
       
   146 	  iChannelClosing(EFalse)
       
   147 	{
       
   148 	//__KTRACE_OPT(KUSB, Kern::Printf("*** DLddUsbcChannel::DLddUsbcChannel CTOR"));
       
   149 	iClient = &Kern::CurrentThread();
       
   150 	iClient->Open();
       
   151 	for (TInt i = 1; i <= KMaxEndpointsPerClient; i++)
       
   152 		{
       
   153 		iEndpoint[i] = NULL;
       
   154 		}
       
   155 	for (TInt i = 1; i < KUsbcMaxRequests; i++)
       
   156 		{
       
   157 		iRequestStatus[i] = NULL;
       
   158 		}
       
   159 	}
       
   160 
       
   161 
       
   162 DLddUsbcChannel::~DLddUsbcChannel()
       
   163 	{
       
   164 	//__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcChannel::~DLddUsbcChannel()"));
       
   165 	if (iController)
       
   166 		{
       
   167 		iStatusCallbackInfo.Cancel();
       
   168 		iEndpointStatusCallbackInfo.Cancel();
       
   169         iOtgFeatureCallbackInfo.Cancel();
       
   170         iCompleteAllCallbackInfo.Cancel();
       
   171 		AbortInterface();
       
   172 		DestroyAllInterfaces();
       
   173 		if (iOwnsDeviceControl)
       
   174 			{
       
   175 			iController->ReleaseDeviceControl(this);
       
   176 			iOwnsDeviceControl = EFalse;
       
   177 			}
       
   178 		iController->DeRegisterClient(this);
       
   179 		DestroyEp0();
       
   180 		delete iStatusFifo;
       
   181 		}
       
   182 	Kern::SafeClose((DObject*&)iClient, NULL);
       
   183 	}
       
   184 
       
   185 
       
   186 inline TBool DLddUsbcChannel::ValidEndpoint(TInt aEndpoint)
       
   187 	{
       
   188 	return (aEndpoint <= iNumberOfEndpoints && aEndpoint >= 0);
       
   189 	}
       
   190 
       
   191 
       
   192 //
       
   193 // Create channel
       
   194 //
       
   195 TInt DLddUsbcChannel::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& aVer)
       
   196 	{
       
   197 	//__KTRACE_OPT(KUSB, Kern::Printf("LDD DoCreateL 1 Ver = %02d %02d %02d",
       
   198 								//	aVer.iMajor, aVer.iMinor, aVer.iBuild));
       
   199 	if (!Kern::CurrentThreadHasCapability(ECapabilityCommDD,
       
   200 										  __PLATSEC_DIAGNOSTIC_STRING("Checked by USBC.LDD (USB Driver)")))
       
   201 		{
       
   202 		return KErrPermissionDenied;
       
   203 		}
       
   204 
       
   205 	iController = DUsbClientController::UsbcControllerPointer();
       
   206 
       
   207 	if (!iController)
       
   208 		{
       
   209 		return KErrGeneral;
       
   210 		}
       
   211 
       
   212     if (!iController->IsActive())
       
   213         return KErrDisconnected;
       
   214 
       
   215 	iStatusFifo = new TUsbcDeviceStatusQueue;
       
   216 	if (iStatusFifo == NULL)
       
   217 		{
       
   218 		return KErrNoMemory;
       
   219 		}
       
   220 
       
   221   	if (!Kern::QueryVersionSupported(TVersion(KUsbcMajorVersion, KUsbcMinorVersion, KUsbcBuildVersion), aVer))
       
   222 		{
       
   223 		return KErrNotSupported;
       
   224 		}
       
   225 
       
   226 	// set up the correct DFC queue
       
   227 	SetDfcQ(iController->DfcQ(0));							// sets the channel's dfc queue
       
   228     iCompleteAllCallbackInfo.SetDfcQ(iDfcQ);
       
   229     iController->RegisterClientCallback(iCompleteAllCallbackInfo);
       
   230 	iStatusCallbackInfo.SetDfcQ(iDfcQ);						// use the channel's dfcq for this dfc
       
   231 	iEndpointStatusCallbackInfo.SetDfcQ(iDfcQ);				// use the channel's dfcq for this dfc
       
   232     iOtgFeatureCallbackInfo.SetDfcQ(iDfcQ);
       
   233 	iMsgQ.Receive();										//start up the message q
       
   234 	iController->RegisterForStatusChange(iStatusCallbackInfo);	// let the ldd start seeing device state changes
       
   235 	return KErrNone;
       
   236 	}
       
   237 
       
   238 
       
   239 void DLddUsbcChannel::HandleMsg(TMessageBase* aMsg)
       
   240 	{
       
   241 	TThreadMessage& m = *(TThreadMessage*)aMsg;
       
   242 	TInt id = m.iValue;
       
   243 	if (id == (TInt) ECloseMsg)
       
   244 		{
       
   245 		iChannelClosing = ETrue;
       
   246 		m.Complete(KErrNone, EFalse);
       
   247 		return;
       
   248 		}
       
   249 	else if (id == KMaxTInt)
       
   250 		{
       
   251 		// Cancel request
       
   252 		TInt mask = m.Int0();
       
   253 		TInt b = 1;
       
   254 		for(TInt reqNo = 0; reqNo <= KUsbcMaxRequests; reqNo++)
       
   255 			{
       
   256 			TRequestStatus* pS = iRequestStatus[reqNo];
       
   257 			if ((mask & b) && (pS != NULL))
       
   258 				{
       
   259 				TInt r = DoCancel(reqNo);
       
   260 				if (r == KErrNone)
       
   261 					r = KErrCancel;
       
   262 				Kern::RequestComplete(iClient,iRequestStatus[reqNo], r);
       
   263 				}
       
   264 			b <<= 1;
       
   265 			}
       
   266 		m.Complete(KErrNone, ETrue);
       
   267 		return;
       
   268 		}
       
   269 
       
   270 	if (!iController->IsActive())
       
   271 		{
       
   272         if (id != RDevUsbcClient::EControlEnableUsbDriver)
       
   273 		    {
       
   274             //__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcChannel::HandleMsg - Controller is not active"));
       
   275             m.Complete(KErrDisconnected, ETrue);
       
   276 		    return;
       
   277 		    }
       
   278 		}
       
   279 	if (id < 0)
       
   280 		{
       
   281 		// DoRequest
       
   282 		TRequestStatus* pS = (TRequestStatus*) m.Ptr0();
       
   283 		DoRequest(~id, pS, m.Ptr1(), m.Ptr2());
       
   284 		m.Complete(KErrNone, ETrue);
       
   285 		}
       
   286 	else
       
   287 		{
       
   288 		// DoControl
       
   289 		TInt r = DoControl(id, m.Ptr0(), m.Ptr1());
       
   290 		m.Complete(r, ETrue);
       
   291 		}
       
   292 	}
       
   293 
       
   294 
       
   295 //
       
   296 // Overriding DObject virtual
       
   297 //
       
   298 TInt DLddUsbcChannel::RequestUserHandle(DThread* aThread, TOwnerType /*aType*/)
       
   299 	{
       
   300 	__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcChannel::RequestUserHandle"));
       
   301 	// The USB client LDD is not designed for a channel to be shared between
       
   302 	// threads. It saves a pointer to the current thread when it is opened, and
       
   303 	// uses this to complete any asynchronous requests.
       
   304 	// It is therefore not acceptable for the handle to be duplicated and used
       
   305 	// by another thread:
       
   306 	if (aThread == iClient)
       
   307 		{
       
   308 		return KErrNone;
       
   309 		}
       
   310 	else
       
   311 		{
       
   312 		return KErrAccessDenied;
       
   313 		}
       
   314 	}
       
   315 // Asynchronous requests - overriding pure virtual
       
   316 //
       
   317 void DLddUsbcChannel::DoRequest(TInt aReqNo, TRequestStatus* aStatus, TAny* a1, TAny* a2)
       
   318 	{
       
   319 	// Check on request status
       
   320 	//__KTRACE_OPT(KUSB, Kern::Printf("DoRequest 0x%08x", aReqNo));
       
   321 	if (aReqNo <= KUsbcMaxRequests)
       
   322 		{
       
   323 		TInt r = KErrNone;
       
   324 		if (iRequestStatus[aReqNo] != NULL)
       
   325 			{
       
   326 			DestroyAllInterfaces();
       
   327 			PanicClientThread(ERequestAlreadyPending);
       
   328 			}
       
   329 		else
       
   330 			{
       
   331 			TBool needsCompletion;
       
   332 			iRequestStatus[aReqNo] = aStatus;
       
   333 
       
   334 			if (aReqNo > KUsbcMaxEpNumber)
       
   335 				{
       
   336 				r = DoOtherAsyncReq(aReqNo, a1, a2, needsCompletion);
       
   337 				}
       
   338 			else
       
   339 				{
       
   340 				r = DoTransferAsyncReq(aReqNo, a1, a2, needsCompletion);
       
   341 				}
       
   342 
       
   343 			if (needsCompletion)
       
   344 				{
       
   345 				Kern::RequestComplete(iClient, iRequestStatus[aReqNo], r);
       
   346 				}
       
   347 			}
       
   348 		}
       
   349 	else
       
   350 		{
       
   351 		Kern::RequestComplete(iClient, aStatus, KErrNotSupported);
       
   352 		}
       
   353 	}
       
   354 
       
   355 
       
   356 TInt DLddUsbcChannel::DoOtherAsyncReq(TInt aReqNo, TAny* a1, TAny* a2, TBool& aNeedsCompletion)
       
   357 	{
       
   358 	// The general assumption is that none of these will complete now.
       
   359 	// However, those that make this function return something other than
       
   360 	// KErrNone will get completed by the calling function.
       
   361 	// So, 1) If you are returning KErrNone but really need to complete because
       
   362 	//        completion criteria can be met (for example, sufficient data is
       
   363 	//        available in the buffer) and then set aNeedsCompletion = ETrue.
       
   364 	//     2) Do NOT complete here AT ALL.
       
   365 	//
       
   366 	aNeedsCompletion = EFalse;
       
   367 	TInt r = KErrNone;
       
   368 
       
   369 	switch (aReqNo)
       
   370 		{
       
   371 	case RDevUsbcClient::ERequestAlternateDeviceStatusNotify:
       
   372 		{
       
   373 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlReqDeviceStatusNotify"));
       
   374 		if (a1 != NULL)
       
   375 			{
       
   376 			iDeviceStatusNeeded = ETrue;
       
   377 			iStatusChangePtr = a1;
       
   378 			aNeedsCompletion = AlternateDeviceStateTestComplete();
       
   379 			}
       
   380 		else
       
   381 			r = KErrArgument;
       
   382 		break;
       
   383 		}
       
   384 	case RDevUsbcClient::ERequestReEnumerate:
       
   385 		{
       
   386 		//__KTRACE_OPT(KUSB, Kern::Printf("ERequestReEnumerate"));
       
   387 		// If successful, this will complete via the status notification.
       
   388 		r = iController->ReEnumerate();
       
   389 		break;
       
   390 		}
       
   391 	case RDevUsbcClient::ERequestEndpointStatusNotify:
       
   392 		{
       
   393 		//__KTRACE_OPT(KUSB, Kern::Printf("ERequestEndpointStatusNotify"));
       
   394 		if (a1 != NULL)
       
   395 			{
       
   396 			iEndpointStatusChangePtr = a1;
       
   397             iController->RegisterForEndpointStatusChange(iEndpointStatusCallbackInfo);
       
   398 			}
       
   399 		else
       
   400 			r = KErrArgument;
       
   401 		break;
       
   402 			}
       
   403 	case RDevUsbcClient::ERequestOtgFeaturesNotify:
       
   404 		{
       
   405 		//__KTRACE_OPT(KUSB, Kern::Printf("ERequestOtgFeaturesNotify"));
       
   406 		if (a1 != NULL)
       
   407 			{
       
   408             iOtgFeatureChangePtr = a1;
       
   409             iController->RegisterForOtgFeatureChange(iOtgFeatureCallbackInfo);
       
   410             }
       
   411 		else
       
   412 			r = KErrArgument;
       
   413         break;
       
   414         }
       
   415     default:
       
   416 		r = KErrNotSupported;
       
   417 		}
       
   418 
       
   419 	aNeedsCompletion = aNeedsCompletion || (r != KErrNone);
       
   420 
       
   421 	return r;
       
   422 	}
       
   423 
       
   424 
       
   425 TInt DLddUsbcChannel::DoTransferAsyncReq(TInt aEndpointNum, TAny* a1, TAny* a2, TBool& aNeedsCompletion)
       
   426 	{
       
   427 	// The general assumption is that none of these will complete now.
       
   428 	// however, those that are returning something other than KErrNone will get completed
       
   429 	// by the calling function.
       
   430 	// So,	1) if you are returning KErrNone but really need to complete because completion criteria can be met
       
   431 	//			(for example, sufficient data is available in the buffer) and then set aNeedsCompletion=ETrue..
       
   432 	//		2) Do NOT complete here AT ALL.
       
   433 	//
       
   434 	aNeedsCompletion = EFalse;
       
   435 	TInt r = KErrNone;
       
   436 	TUsbcEndpoint* pEndpoint = NULL;
       
   437 	TUsbcEndpointInfo* pEndpointInfo = NULL;
       
   438 	TEndpointTransferInfo* pTfr = NULL;
       
   439 
       
   440 	if (aEndpointNum == 0)
       
   441 		{
       
   442 		// ep0 requests
       
   443 		if (!(iValidInterface || iOwnsDeviceControl))
       
   444 			{
       
   445 			//__KTRACE_OPT(KUSB, Kern::Printf("DoRequest rejected: not configured (Ep0)"));
       
   446 			r = KErrUsbInterfaceNotReady;
       
   447 			goto exit;
       
   448 			}
       
   449 		}
       
   450 	else
       
   451 		{
       
   452 		// other eps
       
   453 		if (!(iValidInterface && (iDeviceState == EUsbcDeviceStateConfigured ||
       
   454 		                          iDeviceState == EUsbcDeviceStateSuspended))
       
   455 		   )
       
   456 			{
       
   457 			//__KTRACE_OPT(KUSB, Kern::Printf("DoRequest rejected not configured (Ep %d)", aEndpointNum));
       
   458 			r = KErrUsbInterfaceNotReady;
       
   459 			goto exit;
       
   460 			}
       
   461 		}
       
   462 
       
   463 	if (!ValidEndpoint(aEndpointNum))
       
   464 		{
       
   465 		//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: DoRequest Read: in error complete"));
       
   466 		r = KErrUsbEpNotInInterface;
       
   467 		goto exit;
       
   468  		}
       
   469 
       
   470 	if (a1 == NULL)
       
   471 		{
       
   472 		r = KErrArgument;
       
   473 		goto exit;
       
   474 		}
       
   475 
       
   476 	TEndpointTransferInfo transferInfo;
       
   477 	pTfr = &transferInfo;
       
   478 	r = Kern::ThreadRawRead(iClient, a1, pTfr, sizeof(TEndpointTransferInfo));
       
   479 	if (r != KErrNone)
       
   480 		PanicClientThread(r);
       
   481 	pEndpoint = iEndpoint[aEndpointNum];
       
   482 	if (!pEndpoint)
       
   483 		{
       
   484 		//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: DoRequest Read: in error complete"));
       
   485 		r = KErrUsbEpNotInInterface;
       
   486 		goto exit;
       
   487 		}
       
   488 
       
   489 	pEndpointInfo = pEndpoint->EndpointInfo();
       
   490 	//__KTRACE_OPT(KUSB, Kern::Printf("DoRequest %d", aEndpointNum));
       
   491 
       
   492 	switch (pTfr->iTransferType)
       
   493 		{
       
   494 
       
   495 	case ETransferTypeReadData:
       
   496 	case ETransferTypeReadPacket:
       
   497 	case ETransferTypeReadUntilShort:
       
   498 	case ETransferTypeReadOneOrMore:
       
   499 		{
       
   500 		//__KTRACE_OPT(KUSB, Kern::Printf("DoRequest Read"));
       
   501 		if (pEndpoint->iDmaBuffers->RxIsActive())
       
   502 			{
       
   503 			//__KTRACE_OPT(KUSB, Kern::Printf("**** ReadReq ep%d RxActive", aEndpointNum));
       
   504 			}
       
   505 		else
       
   506 			{
       
   507 			//__KTRACE_OPT(KUSB, Kern::Printf("**** ReadReq ep%d RxInActive", aEndpointNum));
       
   508 			}
       
   509 
       
   510 		if (pEndpointInfo->iDir != KUsbEpDirOut &&
       
   511 			pEndpointInfo->iDir != KUsbEpDirBidirect)
       
   512 			{
       
   513 			// Trying to do the wrong thing
       
   514 			//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: DoRequest Read: in error complete"));
       
   515 			r = KErrUsbEpBadDirection;
       
   516 			break;
       
   517 			}
       
   518 		// Set the length of data to zero now to catch all cases
       
   519 		TPtrC8 pZeroDesc(NULL, 0);
       
   520 		// Set client descriptor length to zero
       
   521 		r = Kern::ThreadDesWrite(iClient, pTfr->iDes, pZeroDesc, 0, 0, iClient);
       
   522 		if (r != KErrNone)
       
   523 			PanicClientThread(r);
       
   524 		pEndpoint->SetTransferInfo(pTfr);
       
   525 		if (pEndpoint->iDmaBuffers->IsReaderEmpty())
       
   526 			{
       
   527 			pEndpoint->SetClientReadPending(ETrue);
       
   528 			}
       
   529 		else
       
   530 			{
       
   531 			if (pTfr->iTransferType == ETransferTypeReadPacket)
       
   532 				{
       
   533 				//__KTRACE_OPT(KUSB, Kern::Printf("DoRequest Read packet: data available complete"));
       
   534 				r = pEndpoint->CopyToClient(iClient);
       
   535 				aNeedsCompletion = ETrue;
       
   536 				break;
       
   537 				}
       
   538 			else if (pTfr->iTransferType == ETransferTypeReadData)
       
   539 				{
       
   540 				if (pTfr->iTransferSize <= pEndpoint->RxBytesAvailable())
       
   541 					{
       
   542 					//__KTRACE_OPT(KUSB, Kern::Printf("DoRequest Read data: data available complete"));
       
   543 					r = pEndpoint->CopyToClient(iClient);
       
   544 					aNeedsCompletion = ETrue;
       
   545 					break;
       
   546 					}
       
   547 				else
       
   548 					{
       
   549 					pEndpoint->SetClientReadPending(ETrue);
       
   550 					}
       
   551 				}
       
   552 			else if (pTfr->iTransferType == ETransferTypeReadOneOrMore)
       
   553 				{
       
   554 				if (pEndpoint->RxBytesAvailable() > 0)
       
   555 					{
       
   556 					//__KTRACE_OPT(KUSB, Kern::Printf("DoRequest Read data: data available complete"));
       
   557 					r = pEndpoint->CopyToClient(iClient);
       
   558 					aNeedsCompletion = ETrue;
       
   559 					break;
       
   560 					}
       
   561 				else
       
   562 					{
       
   563 					pEndpoint->SetClientReadPending(ETrue);
       
   564 					}
       
   565 				}
       
   566 			else if (pTfr->iTransferType == ETransferTypeReadUntilShort)
       
   567 				{
       
   568 				TInt nRx = pEndpoint->RxBytesAvailable();
       
   569 				TInt maxPacketSize = pEndpoint->EndpointInfo()->iSize;
       
   570 				if( (pTfr->iTransferSize <= nRx) ||
       
   571 					(nRx < maxPacketSize) ||
       
   572 					pEndpoint->iDmaBuffers->ShortPacketExists())
       
   573 					{
       
   574 					//__KTRACE_OPT(KUSB, Kern::Printf("DoRequest Read data: data available complete"));
       
   575 					r = pEndpoint->CopyToClient(iClient);
       
   576 					aNeedsCompletion = ETrue;
       
   577 					}
       
   578 				else
       
   579 					{
       
   580 					pEndpoint->SetClientReadPending(ETrue);
       
   581 					}
       
   582 				}
       
   583 			}
       
   584 		r = pEndpoint->TryToStartRead(EFalse);
       
   585 		if (r != KErrNone)
       
   586 			{
       
   587 			//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: DoRequest Read: couldn't start read"));
       
   588 			r = KErrNone;									// Reader full isn't a userside error;
       
   589 			}
       
   590 		break;
       
   591 		}
       
   592 
       
   593 	case ETransferTypeWrite:
       
   594 		{
       
   595 		//__KTRACE_OPT(KUSB, Kern::Printf("DoRequest Write 1"));
       
   596 		if (pEndpointInfo->iDir != KUsbEpDirIn &&
       
   597 			pEndpointInfo->iDir != KUsbEpDirBidirect)
       
   598 			{
       
   599 			//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: DoRequest Write: wrong direction complete"));
       
   600 			r = KErrUsbEpBadDirection;
       
   601 			break;
       
   602 			}
       
   603 		//__KTRACE_OPT(KUSB, Kern::Printf("DoRequest Write 2"));
       
   604 
       
   605 		TAny* logicalSrc = pTfr->iDes;
       
   606 		TInt desLength = Kern::ThreadGetDesLength(iClient, logicalSrc);
       
   607 		if (desLength < pTfr->iTransferSize)
       
   608 			{
       
   609 			//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: DoRequest Write: user buffer too short"));
       
   610 			r = KErrUsbTransferSize;
       
   611 			break;
       
   612 			}
       
   613 
       
   614 		//__KTRACE_OPT(KUSB, Kern::Printf("DoRequest Write 3 length=%d maxlength=%d",
       
   615 									//	pTfr->iTransferSize, desLength));
       
   616 		// Zero length writes are acceptable
       
   617 		pEndpoint->SetClientWritePending(ETrue);
       
   618 		r = pEndpoint->TryToStartWrite(pTfr);
       
   619 		if (r != KErrNone)
       
   620 			{
       
   621 			//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: DoRequest Write: couldn't start write"));
       
   622 			pEndpoint->SetClientWritePending(EFalse);
       
   623 			}
       
   624 		break;
       
   625 		}
       
   626 
       
   627 	default:
       
   628 		//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: DoTransferAsyncReq: pTfr->iTransferType = %d not supported",
       
   629 									//	  pTfr->iTransferType));
       
   630 		r = KErrNotSupported;
       
   631 		break;
       
   632 		}
       
   633  exit:
       
   634 	aNeedsCompletion = aNeedsCompletion || (r != KErrNone);
       
   635 	return r;
       
   636 	}
       
   637 
       
   638 
       
   639 //
       
   640 // Cancel an outstanding request - overriding pure virtual
       
   641 //
       
   642 TInt DLddUsbcChannel::DoCancel(TInt aReqNo)
       
   643 	{
       
   644 	TInt r = KErrNone;
       
   645 	//__KTRACE_OPT(KUSB, Kern::Printf("DoCancel: 0x%x", aReqNo));
       
   646 	if (aReqNo <= iNumberOfEndpoints)
       
   647 		{
       
   648 		//__KTRACE_OPT(KUSB, Kern::Printf("DoCancel endpoint: 0x%x", aReqNo));
       
   649 		iEndpoint[aReqNo]->CancelTransfer(iClient);
       
   650 		}
       
   651 	else if (aReqNo == RDevUsbcClient::ERequestAlternateDeviceStatusNotify)
       
   652 		{
       
   653 		//__KTRACE_OPT(KUSB, Kern::Printf("DoCancel: ERequestAlternateDeviceStatusNotify 0x%x", aReqNo));
       
   654 		iDeviceStatusNeeded = EFalse;
       
   655 		iStatusFifo->FlushQueue();
       
   656 		if (iStatusChangePtr)
       
   657 			{
       
   658 			TInt deviceState = iController->GetDeviceStatus();
       
   659 			r = Kern::ThreadRawWrite(iClient, iStatusChangePtr, &deviceState, sizeof(deviceState), iClient);
       
   660 			if (r != KErrNone)
       
   661 				PanicClientThread(r);
       
   662 			iStatusChangePtr = NULL;
       
   663 			}
       
   664 		}
       
   665 	else if (aReqNo == RDevUsbcClient::ERequestReEnumerate)
       
   666 		{
       
   667 		//__KTRACE_OPT(KUSB, Kern::Printf("DoCancel ERequestReEnumerate: 0x%x", aReqNo));
       
   668 		}
       
   669 	else if (aReqNo == RDevUsbcClient::ERequestEndpointStatusNotify)
       
   670 		{
       
   671 		//__KTRACE_OPT(KUSB, Kern::Printf("DoCancel ERequestEndpointStatusNotify: 0x%x", aReqNo));
       
   672 		CancelNotifyEndpointStatus();
       
   673 		}
       
   674 	else if (aReqNo == RDevUsbcClient::ERequestOtgFeaturesNotify)
       
   675 		{
       
   676 		//__KTRACE_OPT(KUSB, Kern::Printf("DoCancel ERequestOtgFeaturesNotify: 0x%x", aReqNo));
       
   677 		CancelNotifyOtgFeatures();
       
   678 		}
       
   679 	else
       
   680 		{
       
   681 		//__KTRACE_OPT(KUSB, Kern::Printf("DoCancel Unknown! 0x%x", aReqNo));
       
   682 		}
       
   683 
       
   684 	return r;
       
   685 	}
       
   686 
       
   687 
       
   688 void DLddUsbcChannel::CancelNotifyEndpointStatus()
       
   689 	{
       
   690 	if (iEndpointStatusChangePtr)
       
   691 		{
       
   692 		TUint epBitmap = 0;
       
   693 		for (TInt i = 0; i <= iNumberOfEndpoints; i++)
       
   694 			{
       
   695 			TInt v = iController->GetEndpointStatus(this, iEndpoint[i]->RealEpNumber());
       
   696 			TUint b;
       
   697 			(v == EEndpointStateStalled) ? b = 1 : b = 0;
       
   698 			epBitmap |= b << i;
       
   699 			}
       
   700 		Kern::ThreadRawWrite(iClient, iEndpointStatusChangePtr, (TUint8*) &epBitmap, sizeof(epBitmap), iClient);
       
   701 		iEndpointStatusChangePtr = NULL;
       
   702 		}
       
   703 	iController->DeRegisterForEndpointStatusChange(this);
       
   704 	}
       
   705 
       
   706 
       
   707 void DLddUsbcChannel::CancelNotifyOtgFeatures()
       
   708 	{
       
   709     if (iOtgFeatureChangePtr)
       
   710         {
       
   711         TUint8 features;
       
   712         iController->GetCurrentOtgFeatures(features);
       
   713 		Kern::ThreadRawWrite(iClient, iOtgFeatureChangePtr, (TUint8*)&features, sizeof(features), iClient);
       
   714         iOtgFeatureChangePtr = NULL;
       
   715         }
       
   716 	iController->DeRegisterForOtgFeatureChange(this);
       
   717     }
       
   718 
       
   719 
       
   720 TInt DLddUsbcChannel::DoControl(TInt aFunction, TAny* a1, TAny* a2)
       
   721 	{
       
   722 	//__KTRACE_OPT(KUSB, Kern::Printf("DoControl: %d", aFunction));
       
   723 
       
   724 	TInt r = KErrNone;
       
   725 	TInt ep, param;
       
   726 	TUsbcEndpoint* pEndpoint;
       
   727 	TPtrC8 pZeroDesc(NULL, 0);
       
   728 	TEndpointDescriptorInfo epInfo;
       
   729 	TUsbcIfcInfo ifcInfo;
       
   730 	TCSDescriptorInfo desInfo;
       
   731 	TUsbcEndpointResource epRes;
       
   732 	TInt bandwidthPriority;
       
   733 	
       
   734 	/****declare trace objects and variables*******/
       
   735 	CUTraces Testtracer;
       
   736 	int ret=1;
       
   737 	TUTrace TestTraceObject(UTracePrimary, UTraceSecondary, schema, context, pc);
       
   738 	TUTrace NegTraceObject1((TPrimaryFilter)(UTracePrimary+1), (TSecondaryFilter)(UTraceSecondary+1), schema, context, pc);
       
   739 	TUTrace NegTraceObject2((TPrimaryFilter)(UTracePrimary+2), (TSecondaryFilter)(UTraceSecondary+2), schema, context, pc);
       
   740 	TUTrace MultTraceObject1((TPrimaryFilter)(UTracePrimary), (TSecondaryFilter)(UTraceSecondary+2),schema, context, pc );
       
   741 	TUTrace MultTraceObject2((TPrimaryFilter)(UTracePrimary+2), (TSecondaryFilter)(UTraceSecondary),schema, context, pc );
       
   742 	TUTrace MultTraceObject3((TPrimaryFilter)(UTracePrimary+2), (TSecondaryFilter)(UTraceSecondary+1),schema, context, pc );
       
   743 	TUTrace MultTraceObject4((TPrimaryFilter)(UTracePrimary+1), (TSecondaryFilter)(UTraceSecondary),schema, context, pc );
       
   744 	TUTrace MultTraceObject5((TPrimaryFilter)(UTracePrimary+2),schema, context, pc );
       
   745 
       
   746 
       
   747 	switch (aFunction)
       
   748 		{
       
   749 	case RDevUsbcClient::EControlEndpointZeroRequestError:
       
   750 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlEndpointZeroRequestError"));
       
   751 		
       
   752 		/****************UTRACE METHOD - ALL BASIC LOGGING*********************************/
       
   753 		for(int tracetag=0; tracetag!=NumberOfUTraceMacros&&ret;tracetag++)
       
   754 			{
       
   755 				ret = 0;
       
   756 				ret = Testtracer.DoTheTrace(tracetag);
       
   757 			}
       
   758 		for(int tracetag=0; tracetag!=NumberOfUTraceMacros&&ret;tracetag++)
       
   759 			{
       
   760 				ret = 0;
       
   761 				ret = Testtracer.DoTheSetTrace(tracetag, TestTraceObject);
       
   762 			}
       
   763 		for(int tracetag=0; tracetag!=NumberOfUTraceMacros&&ret;tracetag++)
       
   764 			{
       
   765 				ret = 0;
       
   766 				ret = Testtracer.DoTheStaticTrace(tracetag);
       
   767 			}
       
   768 	
       
   769 			//put in extra logs for negative trace test
       
   770 	
       
   771 		NegTraceObject1.Trace();
       
   772 		NegTraceObject2.Trace();
       
   773 		
       
   774 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+1), (TSecondaryFilter)(UTraceSecondary+1), schema, context, pc);
       
   775 		TestTraceObject.Trace();
       
   776 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+2), (TSecondaryFilter)(UTraceSecondary+2), schema, context, pc);
       
   777 		TestTraceObject.Trace();
       
   778 		
       
   779 		TUTrace::Trace((TPrimaryFilter)(UTracePrimary+1), (TSecondaryFilter)(UTraceSecondary+1), schema, context, pc);
       
   780 		TUTrace::Trace((TPrimaryFilter)(UTracePrimary+2), (TSecondaryFilter)(UTraceSecondary+2), schema, context, pc);
       
   781 		
       
   782 
       
   783 		
       
   784 		
       
   785 	//	for(int tracetag=0; tracetag!=NumberOfPrintfMacros&&ret;tracetag++)
       
   786 	//		{
       
   787 	//			ret = 0;
       
   788 	//			ret = Testtracer.DoThePrintf(tracetag);
       
   789 	//		}
       
   790 	//	for(int tracetag=0; tracetag!=NumberOfPrintfMacros&&ret;tracetag++)
       
   791 	//		{
       
   792 	//			ret = 0;
       
   793 	//			ret = Testtracer.DoTheStaticPrintf(tracetag);
       
   794 	//		}
       
   795 			
       
   796 	
       
   797 			
       
   798 		/****************End of Tracing**************************************/
       
   799 		r = KErrNone;
       
   800 		if (iOwnsDeviceControl || (iValidInterface && iDeviceState == EUsbcDeviceStateConfigured))
       
   801 			{
       
   802 			iController->Ep0Stall(this);
       
   803 			}
       
   804 		else
       
   805 			{
       
   806 			if (iDeviceState != EUsbcDeviceStateConfigured)
       
   807 				r = KErrUsbDeviceNotConfigured;
       
   808 			else
       
   809 				r = KErrUsbInterfaceNotReady;
       
   810 			}
       
   811 		break;
       
   812 
       
   813 	case RDevUsbcClient::EControlGetAlternateSetting:
       
   814 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetAlternateSetting"));
       
   815 		/****************UTRACE METHOD NONstatic 0*********************************/
       
   816 		Testtracer.DoTheSetTrace(0, TestTraceObject);
       
   817 	
       
   818 		if (iValidInterface && iDeviceState == EUsbcDeviceStateConfigured)
       
   819 			{
       
   820 			r = iController->GetInterfaceNumber(this, param);
       
   821 			if (r == KErrNone)
       
   822 				{
       
   823 				r = Kern::ThreadRawWrite(iClient, a1, &param, sizeof(param), iClient);
       
   824 				if (r != KErrNone)
       
   825 					PanicClientThread(r);
       
   826 				}
       
   827 			}
       
   828 		else
       
   829 			{
       
   830 			if (iDeviceState != EUsbcDeviceStateConfigured)
       
   831 				r = KErrUsbDeviceNotConfigured;
       
   832 			else
       
   833 				r = KErrUsbInterfaceNotReady;
       
   834 			}
       
   835 		break;
       
   836 
       
   837 	case RDevUsbcClient::EControlDeviceStatus:
       
   838 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlDeviceStatus"));			
       
   839 		param = iController->GetDeviceStatus();
       
   840 		r = Kern::ThreadRawWrite(iClient, a1, &param, sizeof(param), iClient);
       
   841 		if (r != KErrNone)
       
   842 			PanicClientThread(r);
       
   843 		break;
       
   844 
       
   845 	case RDevUsbcClient::EControlEndpointStatus:
       
   846 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlEndpointStatus"));
       
   847 		if (iValidInterface && ValidEndpoint((TInt) a1))
       
   848 			{
       
   849 			pEndpoint = iEndpoint[(TInt)a1];
       
   850 			if (pEndpoint == NULL)
       
   851 				r = KErrNotSupported;
       
   852 			else
       
   853 				{
       
   854 				param = iController->GetEndpointStatus(this, iEndpoint[(TInt)a1]->RealEpNumber());
       
   855 				r = Kern::ThreadRawWrite(iClient, a2, &param, sizeof(param), iClient);
       
   856 				if (r != KErrNone)
       
   857 					PanicClientThread(r);
       
   858 				}
       
   859 			}
       
   860 		else
       
   861 			{
       
   862 			if (iDeviceState != EUsbcDeviceStateConfigured)
       
   863 				r = KErrUsbDeviceNotConfigured;
       
   864 			else
       
   865 				r = KErrUsbInterfaceNotReady;
       
   866 			}
       
   867 		break;
       
   868 
       
   869 	case RDevUsbcClient::EControlQueryReceiveBuffer:
       
   870 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlQueryReceiveBuffer"));
       
   871 		if (iValidInterface && ValidEndpoint((TInt) a1))
       
   872 			{
       
   873 			pEndpoint=iEndpoint[(TInt) a1];
       
   874 			if (pEndpoint == NULL)
       
   875 				r = KErrNotSupported;
       
   876 			else if (pEndpoint->EndpointInfo()->iDir != KUsbEpDirIn)
       
   877 				{
       
   878 				//__KTRACE_OPT(KUSB, Kern::Printf("  bytes = %d", pEndpoint->RxBytesAvailable()));
       
   879 				param = pEndpoint->RxBytesAvailable();
       
   880 				r = Kern::ThreadRawWrite(iClient, a2, &param, sizeof(param), iClient);
       
   881 				if (r != KErrNone)
       
   882 					PanicClientThread(r);
       
   883 				}
       
   884 			}
       
   885 		else
       
   886 			{
       
   887 			if (iDeviceState != EUsbcDeviceStateConfigured)
       
   888 				r = KErrUsbDeviceNotConfigured;
       
   889 			else
       
   890 				r = KErrUsbInterfaceNotReady;
       
   891 			}
       
   892 		break;
       
   893 
       
   894 	case RDevUsbcClient::EControlEndpointCaps:
       
   895 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlEndpointCaps"));
       
   896 		r = Kern::ThreadDesWrite(iClient, a1, pZeroDesc, 0, 0, iClient);
       
   897 		if (r != KErrNone)
       
   898 			PanicClientThread(r);
       
   899 		iController->EndpointCaps(this, *((TDes8*) a1));
       
   900 		break;
       
   901 
       
   902 	case RDevUsbcClient::EControlDeviceCaps:
       
   903 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlDeviceCaps"));
       
   904 		r = Kern::ThreadDesWrite(iClient, a1, pZeroDesc, 0, 0, iClient);
       
   905 		if (r != KErrNone)
       
   906 			PanicClientThread(r);
       
   907 		iController->DeviceCaps(this, *((TDes8*) a1));
       
   908 		break;
       
   909 
       
   910 	case RDevUsbcClient::EControlSendEp0StatusPacket:
       
   911 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSendEp0StatusPacket"));
       
   912 		//**********************UTRACE METHOD NONstatic 2***********************/
       
   913 		Testtracer.DoTheSetTrace(2, TestTraceObject);
       
   914 				
       
   915 		iController->SendEp0StatusPacket(this);
       
   916 		break;
       
   917 
       
   918 	case RDevUsbcClient::EControlHaltEndpoint:
       
   919 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlHaltEndpoint"));
       
   920 		if (iValidInterface && ValidEndpoint((TInt) a1))
       
   921 			{
       
   922 			r = iController->HaltEndpoint(this, iEndpoint[(TInt)a1]->RealEpNumber());
       
   923 			}
       
   924 		else
       
   925 			{
       
   926 			if (iDeviceState != EUsbcDeviceStateConfigured)
       
   927 				r = KErrUsbDeviceNotConfigured;
       
   928 			else
       
   929 				r = KErrUsbInterfaceNotReady;
       
   930 			}
       
   931 		break;
       
   932 
       
   933 	case RDevUsbcClient::EControlClearHaltEndpoint:
       
   934 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlClearHaltEndpoint"));
       
   935 		if (iValidInterface && ValidEndpoint((TInt) a1))
       
   936 			{
       
   937 			r = iController->ClearHaltEndpoint(this, iEndpoint[(TInt)a1]->RealEpNumber());
       
   938 			}
       
   939 		else
       
   940 			{
       
   941 			if (iDeviceState != EUsbcDeviceStateConfigured)
       
   942 				r = KErrUsbDeviceNotConfigured;
       
   943 			else
       
   944 				r = KErrUsbInterfaceNotReady;
       
   945 			}
       
   946 		break;
       
   947 
       
   948 	case RDevUsbcClient::EControlDumpRegisters:
       
   949 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlDumpRegisters"));
       
   950 		iController->DumpRegisters();
       
   951 		break;
       
   952 
       
   953 	case RDevUsbcClient::EControlReleaseDeviceControl:
       
   954 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlReleaseDeviceControl"));
       
   955 		//**********************UTRACE METHOD NONstatic 3***********************/
       
   956 		Testtracer.DoTheSetTrace(3, TestTraceObject);
       
   957 
       
   958 		iController->ReleaseDeviceControl(this);
       
   959 		iOwnsDeviceControl = EFalse;
       
   960 		break;
       
   961 
       
   962 	case RDevUsbcClient::EControlEndpointZeroMaxPacketSizes:
       
   963 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlEndpointZeroMaxPacketSizes"));
       
   964 		//**********************UTRACE METHOD NONstatic 4***********************/
       
   965 		Testtracer.DoTheSetTrace(4, TestTraceObject);
       
   966 	
       
   967 
       
   968 		r = iController->EndpointZeroMaxPacketSizes();
       
   969 		break;
       
   970 
       
   971 	case RDevUsbcClient::EControlSetEndpointZeroMaxPacketSize:
       
   972 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetEndpointZeroMaxPacketSize"));
       
   973 		//**********************UTRACE METHOD NONstatic 5***********************/
       
   974 		Testtracer.DoTheSetTrace(5, TestTraceObject);
       
   975 			
       
   976 		r = iController->SetEndpointZeroMaxPacketSize(reinterpret_cast<TInt>(a1));
       
   977 		break;
       
   978 
       
   979 	case RDevUsbcClient::EControlGetEndpointZeroMaxPacketSize:
       
   980 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetEndpointZeroMaxPacketSize"));
       
   981 		//**********************UTRACE METHOD NONstatic 6***********************/
       
   982 		Testtracer.DoTheSetTrace(6, TestTraceObject);
       
   983 			
       
   984 		r = iController->Ep0PacketSize();
       
   985 		break;
       
   986 
       
   987 	case RDevUsbcClient::EControlGetDeviceDescriptor:
       
   988 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetDeviceDescriptor"));
       
   989 	//**********************UTRACE METHOD NONstatic 7***********************/
       
   990 		Testtracer.DoTheSetTrace(7, TestTraceObject);
       
   991 			
       
   992 		r = Kern::ThreadDesWrite(iClient, a1, pZeroDesc, 0, 0, iClient);
       
   993 		if (r != KErrNone)
       
   994 			PanicClientThread(r);
       
   995 		r = iController->GetDeviceDescriptor(iClient, *((TDes8*) a1));
       
   996 		break;
       
   997 
       
   998 	case RDevUsbcClient::EControlSetDeviceDescriptor:
       
   999 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetDeviceDescriptor"));
       
  1000 	//**********************UTRACE METHOD NONstatic 8***********************/
       
  1001 		Testtracer.DoTheSetTrace(8, TestTraceObject);
       
  1002 			
       
  1003 		if (a1 != NULL)
       
  1004 			r = iController->SetDeviceDescriptor(iClient, *((TDes8*) a1));
       
  1005 		else
       
  1006 			r = KErrArgument;
       
  1007 		break;
       
  1008 
       
  1009 	case RDevUsbcClient::EControlGetDeviceDescriptorSize:
       
  1010 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetDeviceDescriptorSize"));
       
  1011 	//**********************UTRACE METHOD NONstatic 9***********************/
       
  1012 		Testtracer.DoTheSetTrace(9, TestTraceObject);
       
  1013 			
       
  1014 		if (a1 != NULL)
       
  1015 			r = iController->GetDeviceDescriptorSize(iClient, *((TDes8*) a1));
       
  1016 		else
       
  1017 			r = KErrArgument;
       
  1018 		break;
       
  1019 
       
  1020 	case RDevUsbcClient::EControlGetConfigurationDescriptor:
       
  1021 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetConfigurationDescriptor"));
       
  1022 	//**********************UTRACE METHOD NONstatic 10***********************/
       
  1023 		Testtracer.DoTheSetTrace(10, TestTraceObject);
       
  1024 			
       
  1025 		r = Kern::ThreadDesWrite(iClient, a1, pZeroDesc, 0 , 0, iClient);
       
  1026 		if (r != KErrNone)
       
  1027 			PanicClientThread(r);
       
  1028 		r = iController->GetConfigurationDescriptor(iClient, *((TDes8*) a1));
       
  1029 		break;
       
  1030 
       
  1031 	case RDevUsbcClient::EControlGetConfigurationDescriptorSize:
       
  1032 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetConfigurationDescriptorSize"));
       
  1033 	//**********************UTRACE METHOD NONstatic 11***********************/
       
  1034 		Testtracer.DoTheSetTrace(11, TestTraceObject);
       
  1035 			
       
  1036 		if (a1 != NULL)
       
  1037 			{
       
  1038 			r = iController->GetConfigurationDescriptorSize(iClient, *((TDes8*) a1));
       
  1039 			}
       
  1040 		else
       
  1041 			r = KErrArgument;
       
  1042 		break;
       
  1043 
       
  1044 	case RDevUsbcClient::EControlSetConfigurationDescriptor:
       
  1045 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetConfigurationDescriptor"));
       
  1046 	//**********************UTRACE METHOD NONstatic 12***********************/
       
  1047 		Testtracer.DoTheSetTrace(12, TestTraceObject);
       
  1048 
       
  1049 		r = iController->SetConfigurationDescriptor(iClient, *((TDes8*) a1));
       
  1050 		break;
       
  1051 
       
  1052 	case RDevUsbcClient::EControlGetInterfaceDescriptor:
       
  1053 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetInterfaceDescriptor"));
       
  1054 	//**********************UTRACE METHOD NONstatic 13***********************/
       
  1055 		Testtracer.DoTheSetTrace(13, TestTraceObject);
       
  1056 
       
  1057 		r = iController->GetInterfaceDescriptor(iClient, this, (TInt) a1, *((TDes8*) a2));
       
  1058 		break;
       
  1059 
       
  1060 	case RDevUsbcClient::EControlGetInterfaceDescriptorSize:
       
  1061 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetInterfaceDescriptorSize"));
       
  1062 	//**********************UTRACE METHOD NONstatic 14***********************/
       
  1063 		Testtracer.DoTheSetTrace(14, TestTraceObject);
       
  1064 
       
  1065 		r = iController->GetInterfaceDescriptorSize(iClient, this, (TInt) a1, *(TDes8*) a2);
       
  1066 		break;
       
  1067 
       
  1068 	case RDevUsbcClient::EControlSetInterfaceDescriptor:
       
  1069 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetInterfaceDescriptor"));
       
  1070 	//**********************UTRACE METHOD NONstatic 15***********************/
       
  1071 		Testtracer.DoTheSetTrace(15, TestTraceObject);
       
  1072 
       
  1073 		r = iController->SetInterfaceDescriptor(iClient, this, (TInt) a1, *((TDes8*) a2));
       
  1074 		break;
       
  1075 
       
  1076 	case RDevUsbcClient::EControlGetEndpointDescriptor:
       
  1077 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetEndpointDescriptor"));
       
  1078 	//**********************UTRACE METHOD NONstatic 16***********************/
       
  1079 		Testtracer.DoTheSetTrace(16, TestTraceObject);
       
  1080 
       
  1081 		r = Kern::ThreadRawRead(iClient, a1, &epInfo, sizeof(epInfo));
       
  1082 		if (r != KErrNone)
       
  1083 			PanicClientThread(r);
       
  1084 		ep = EpFromAlternateSetting(epInfo.iSetting, epInfo.iEndpoint);
       
  1085 		r = iController->GetEndpointDescriptor(iClient, this, epInfo.iSetting,
       
  1086 											   ep, *(TDes8*) epInfo.iArg);
       
  1087 		break;
       
  1088 
       
  1089 	case RDevUsbcClient::EControlGetEndpointDescriptorSize:
       
  1090 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetEndpointDescriptorSize"));
       
  1091 	//**********************UTRACE METHOD NONstatic 17***********************/
       
  1092 		Testtracer.DoTheSetTrace(17, TestTraceObject);
       
  1093 
       
  1094 		r = Kern::ThreadRawRead(iClient, a1, &epInfo, sizeof(epInfo));
       
  1095 		if (r != KErrNone)
       
  1096 			PanicClientThread(r);
       
  1097 		ep = EpFromAlternateSetting(epInfo.iSetting, epInfo.iEndpoint);
       
  1098 		r = iController->GetEndpointDescriptorSize(iClient, this, epInfo.iSetting,
       
  1099 												   ep, *(TDes8*) epInfo.iArg);
       
  1100 		break;
       
  1101 
       
  1102 	case RDevUsbcClient::EControlSetEndpointDescriptor:
       
  1103 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetEndpointDescriptor"));
       
  1104 		//**********************UTRACE METHOD NONstatic 18***********************/
       
  1105 	//	for(int i=0;i!=UTrace_Count;i++)
       
  1106 	//		Testtracer.DoTheSetTrace(18, TestTraceObject);
       
  1107 
       
  1108 		r = Kern::ThreadRawRead(iClient, a1, &epInfo, sizeof(epInfo));
       
  1109 		if (r != KErrNone)
       
  1110 			PanicClientThread(r);
       
  1111 		ep = EpFromAlternateSetting(epInfo.iSetting, epInfo.iEndpoint);
       
  1112 		r = iController->SetEndpointDescriptor(iClient, this, epInfo.iSetting,
       
  1113 											   ep, *(TDes8*)epInfo.iArg);
       
  1114 		break;
       
  1115 
       
  1116 	case RDevUsbcClient::EControlGetDeviceQualifierDescriptor:
       
  1117 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetDeviceQualifierDescriptor"));
       
  1118 	//**********************UTRACE METHOD NONstatic 19***********************/
       
  1119 	//	for(int i=0;i!=UTrace_Count;i++)
       
  1120 	//		Testtracer.DoTheSetTrace(19, TestTraceObject);
       
  1121 
       
  1122 		r = Kern::ThreadDesWrite(iClient, a1, pZeroDesc, 0, 0, iClient);
       
  1123 		if (r != KErrNone)
       
  1124 			PanicClientThread(r);
       
  1125 		r = iController->GetDeviceQualifierDescriptor(iClient, *((TDes8*) a1));
       
  1126 		break;
       
  1127 
       
  1128 	case RDevUsbcClient::EControlSetDeviceQualifierDescriptor:
       
  1129 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetDeviceQualifierDescriptor"));
       
  1130 	//**********************UTRACE METHOD NONstatic 20***********************/
       
  1131 	//	for(int i=0;i!=UTrace_Count;i++)
       
  1132 	//		Testtracer.DoTheSetTrace(20, TestTraceObject);
       
  1133 
       
  1134 		if (a1 != NULL)
       
  1135 			r = iController->SetDeviceQualifierDescriptor(iClient, *((TDes8*) a1));
       
  1136 		else
       
  1137 			r = KErrArgument;
       
  1138 		break;
       
  1139 
       
  1140 	case RDevUsbcClient::EControlGetOtherSpeedConfigurationDescriptor:
       
  1141 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetOtherSpeedConfigurationDescriptor"));
       
  1142 	//**********************UTRACE METHOD NONstatic 21***********************/
       
  1143 	//	for(int i=0;i!=UTrace_Count;i++)
       
  1144 	//		Testtracer.DoTheSetTrace(21, TestTraceObject);
       
  1145 
       
  1146 		r = Kern::ThreadDesWrite(iClient, a1, pZeroDesc, 0 , 0, iClient);
       
  1147 		if (r != KErrNone)
       
  1148 			PanicClientThread(r);
       
  1149 		r = iController->GetOtherSpeedConfigurationDescriptor(iClient, *((TDes8*) a1));
       
  1150 		break;
       
  1151 
       
  1152 	case RDevUsbcClient::EControlSetOtherSpeedConfigurationDescriptor:
       
  1153 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetOtherSpeedConfigurationDescriptor"));
       
  1154 	//**********************UTRACE METHOD NONstatic 22***********************/
       
  1155 	//	for(int i=0;i!=UTrace_Count;i++)
       
  1156 	//		Testtracer.DoTheSetTrace(22, TestTraceObject);
       
  1157 
       
  1158 		r = iController->SetOtherSpeedConfigurationDescriptor(iClient, *((TDes8*) a1));
       
  1159 		break;
       
  1160 
       
  1161 
       
  1162 	case RDevUsbcClient::EControlGetCSInterfaceDescriptor:
       
  1163 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetCSInterfaceDescriptor"));
       
  1164 	//**********************UTRACE METHOD NONstatic 23***********************/
       
  1165 	//	for(int i=0;i!=UTrace_Count;i++)
       
  1166 	//		Testtracer.DoTheSetTrace(23, TestTraceObject);
       
  1167 
       
  1168 		r = iController->GetCSInterfaceDescriptorBlock(iClient, this, (TInt) a1, *((TDes8*) a2));
       
  1169 		break;
       
  1170 
       
  1171 	case RDevUsbcClient::EControlGetCSInterfaceDescriptorSize:
       
  1172 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetCSInterfaceDescriptorSize"));
       
  1173 	//**********************UTRACE METHOD STATIC 0***********************/
       
  1174 		Testtracer.DoTheStaticTrace(0);
       
  1175 
       
  1176 		r = iController->GetCSInterfaceDescriptorBlockSize(iClient, this, (TInt) a1, *(TDes8*) a2);
       
  1177 		break;
       
  1178 
       
  1179 	case RDevUsbcClient::EControlGetCSEndpointDescriptor:
       
  1180 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetCSEndpointDescriptor"));
       
  1181 	//**********************UTRACE METHOD STATIC 1***********************/
       
  1182 		Testtracer.DoTheStaticTrace(1);
       
  1183 
       
  1184 		r = Kern::ThreadRawRead(iClient, a1, &epInfo, sizeof(epInfo));
       
  1185 		if (r != KErrNone)
       
  1186 			PanicClientThread(r);
       
  1187 		ep = EpFromAlternateSetting(epInfo.iSetting, epInfo.iEndpoint);
       
  1188 		r = iController->GetCSEndpointDescriptorBlock(iClient, this, epInfo.iSetting,
       
  1189 													  ep, *(TDes8*) epInfo.iArg);
       
  1190 		break;
       
  1191 
       
  1192 	case RDevUsbcClient::EControlGetCSEndpointDescriptorSize:
       
  1193 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetCSEndpointDescriptorSize"));
       
  1194 		//**********************UTRACE METHOD STATIC 2***********************/
       
  1195 		Testtracer.DoTheStaticTrace(2);
       
  1196 			
       
  1197 		r = Kern::ThreadRawRead(iClient, a1, &epInfo, sizeof(epInfo));
       
  1198 		if (r != KErrNone)
       
  1199 			PanicClientThread(r);
       
  1200 		ep = EpFromAlternateSetting(epInfo.iSetting, epInfo.iEndpoint);
       
  1201 		r = iController->GetCSEndpointDescriptorBlockSize(iClient, this, epInfo.iSetting,
       
  1202 														  ep, *(TDes8*) epInfo.iArg);
       
  1203 		break;
       
  1204 
       
  1205 	case RDevUsbcClient::EControlSignalRemoteWakeup:
       
  1206 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSignalRemoteWakeup"));
       
  1207 		//**********************UTRACE METHOD STATIC 3***********************/
       
  1208 		Testtracer.DoTheStaticTrace(3);
       
  1209 			
       
  1210 	
       
  1211 		r = iController->SignalRemoteWakeup();
       
  1212 		break;
       
  1213 
       
  1214 	case RDevUsbcClient::EControlDeviceDisconnectFromHost:
       
  1215 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlDeviceDisconnectFromHost"));
       
  1216 		r = iController->UsbDisconnect();
       
  1217 		break;
       
  1218 
       
  1219 	case RDevUsbcClient::EControlDeviceConnectToHost:
       
  1220 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlDeviceConnectToHost"));
       
  1221 		r = iController->UsbConnect();
       
  1222 		break;
       
  1223 
       
  1224 	case RDevUsbcClient::EControlDevicePowerUpUdc:
       
  1225 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlDevicePowerUpUdc"));
       
  1226 	//**********************UTRACE METHOD STATIC 4***********************/
       
  1227 		Testtracer.DoTheStaticTrace(4);
       
  1228 			
       
  1229 		r = iController->PowerUpUdc();
       
  1230 		break;
       
  1231 
       
  1232 	case RDevUsbcClient::EControlSetDeviceControl:
       
  1233 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetDeviceControl"));
       
  1234 	//**********************UTRACE METHOD STATIC 5***********************/
       
  1235 		Testtracer.DoTheStaticTrace(5);
       
  1236 
       
  1237 		r = iController->SetDeviceControl(this);
       
  1238 		if (r == KErrNone)
       
  1239 			{
       
  1240 			iOwnsDeviceControl = ETrue;
       
  1241 			if (iEndpoint[0] == NULL)
       
  1242 				{
       
  1243 				//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetDeviceControl 11"));
       
  1244 				r = SetupEp0();
       
  1245 				if (r != KErrNone)
       
  1246 					{
       
  1247 					//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: SetupEp0() failed"));
       
  1248 					iController->ReleaseDeviceControl(this);
       
  1249 					DestroyEp0();
       
  1250 					iOwnsDeviceControl = EFalse;
       
  1251 					}
       
  1252 				iEndpoint[0]->TryToStartRead(EFalse);
       
  1253 				}
       
  1254 			}
       
  1255 		else
       
  1256 			r = KErrInUse;
       
  1257 		break;
       
  1258 
       
  1259 	case RDevUsbcClient::EControlCurrentlyUsingHighSpeed:
       
  1260 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlCurrentlyUsingHighSpeed"));
       
  1261 	//**********************UTRACE METHOD STATIC 6***********************/
       
  1262 		Testtracer.DoTheStaticTrace(6);
       
  1263 	
       
  1264 		r = iController->CurrentlyUsingHighSpeed();
       
  1265 		break;
       
  1266 
       
  1267 	case RDevUsbcClient::EControlSetInterface:
       
  1268 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetInterface"));
       
  1269 		r = Kern::ThreadRawRead(iClient, a2, &ifcInfo, sizeof(ifcInfo));
       
  1270 		if (r != KErrNone)
       
  1271 			PanicClientThread(r);
       
  1272 		bandwidthPriority = ifcInfo.iBandwidthPriority;
       
  1273 		if ((bandwidthPriority & 0xffffff00) ||
       
  1274 			((bandwidthPriority & 0x0f) >= KUsbcDmaBufMaxPriorities) ||
       
  1275 			(((bandwidthPriority >> 4) & 0x0f) >= KUsbcDmaBufMaxPriorities))
       
  1276 			{
       
  1277 			r = KErrArgument;
       
  1278 			}
       
  1279 		else
       
  1280 			{
       
  1281 			r = SetInterface((TInt) a1, &ifcInfo);
       
  1282 			}
       
  1283 		break;
       
  1284 
       
  1285 	case RDevUsbcClient::EControlReleaseInterface:
       
  1286 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlReleaseInterface"));
       
  1287 	//**********************UTRACE METHOD STATIC 7***********************/
       
  1288 		Testtracer.DoTheStaticTrace(7);
       
  1289 			
       
  1290 		r = iController->ReleaseInterface(this, (TInt) a1);
       
  1291 		if (r == KErrNone)
       
  1292 			{
       
  1293 			DestroyInterface((TUint) a1);
       
  1294 			}
       
  1295 		else
       
  1296 			{
       
  1297 			//__KTRACE_OPT(KPANIC, Kern::Printf("  Error in PIL: LDD interface won't be released."));
       
  1298 			}
       
  1299 		break;
       
  1300 
       
  1301 	case RDevUsbcClient::EControlSetCSInterfaceDescriptor:
       
  1302 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetCSInterfaceDescriptor"));
       
  1303 	//**********************UTRACE METHOD STATIC 8***********************/
       
  1304 		Testtracer.DoTheStaticTrace(8);
       
  1305 
       
  1306 		r = Kern::ThreadRawRead(iClient, a1, &desInfo, sizeof(desInfo));
       
  1307 		if (r != KErrNone)
       
  1308 			PanicClientThread(r);
       
  1309 		r = iController->SetCSInterfaceDescriptorBlock(iClient, this, desInfo.iSetting,
       
  1310 													   *reinterpret_cast<const TDes8*>(desInfo.iArg),
       
  1311 													   desInfo.iSize);
       
  1312 		break;
       
  1313 
       
  1314 	case RDevUsbcClient::EControlSetCSEndpointDescriptor:
       
  1315 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetCSEndpointDescriptor"));
       
  1316 	//**********************UTRACE METHOD STATIC 9***********************/
       
  1317 		Testtracer.DoTheStaticTrace(9);
       
  1318 
       
  1319 		r = Kern::ThreadRawRead(iClient, a1, &desInfo, sizeof(desInfo));
       
  1320 		if (r != KErrNone)
       
  1321 			PanicClientThread(r);
       
  1322 		ep = EpFromAlternateSetting(desInfo.iSetting, desInfo.iEndpoint);
       
  1323 		r = iController->SetCSEndpointDescriptorBlock(iClient, this, desInfo.iSetting, ep,
       
  1324 													  *reinterpret_cast<const TDes8*>(desInfo.iArg),
       
  1325 													  desInfo.iSize);
       
  1326 		break;
       
  1327 
       
  1328 	case RDevUsbcClient::EControlGetStringDescriptorLangId:
       
  1329 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetStringDescriptorLangId"));
       
  1330 		//**********************Trace Multiple logging test***********************/
       
  1331 		Testtracer.DoTheTrace(3);
       
  1332 		NegTraceObject1.Trace(aData, aSize);
       
  1333 		MultTraceObject1.Trace(aData, aSize);
       
  1334 		MultTraceObject2.Trace(aData, aSize);
       
  1335 		NegTraceObject2.Trace(aData, aSize);
       
  1336 		MultTraceObject3.Trace(aData, aSize);
       
  1337 		MultTraceObject4.Trace(aData, aSize);
       
  1338 		MultTraceObject5.Trace(aData, aSize);
       
  1339 		
       
  1340 		
       
  1341 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary), (TSecondaryFilter)(UTraceSecondary), schema, context, pc);
       
  1342 		TestTraceObject.Trace(aData, aSize);	
       
  1343 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+1), (TSecondaryFilter)(UTraceSecondary+1), schema, context, pc);
       
  1344 		TestTraceObject.Trace(aData, aSize);
       
  1345 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary), (TSecondaryFilter)(UTraceSecondary+2), schema, context, pc);
       
  1346 		TestTraceObject.Trace(aData, aSize);
       
  1347 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+2), (TSecondaryFilter)(UTraceSecondary), schema, context, pc);
       
  1348 		TestTraceObject.Trace(aData, aSize);
       
  1349 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+2), (TSecondaryFilter)(UTraceSecondary+2), schema, context, pc);
       
  1350 		TestTraceObject.Trace(aData, aSize);
       
  1351 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+2), (TSecondaryFilter)(UTraceSecondary+1), schema, context, pc);
       
  1352 		TestTraceObject.Trace(aData, aSize);
       
  1353 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+1), (TSecondaryFilter)(UTraceSecondary), schema, context, pc);
       
  1354 		TestTraceObject.Trace(aData, aSize);
       
  1355 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+2), schema, context, pc);
       
  1356 		TestTraceObject.Trace(aData, aSize);
       
  1357 		
       
  1358 		TUTrace::Trace((TPrimaryFilter)(UTracePrimary), (TSecondaryFilter)(UTraceSecondary), schema, context, pc, aData, aSize);
       
  1359 		TUTrace::Trace((TPrimaryFilter)(UTracePrimary+1), (TSecondaryFilter)(UTraceSecondary+1), schema, context, pc, aData, aSize);
       
  1360 		TUTrace::Trace((TPrimaryFilter)(UTracePrimary), (TSecondaryFilter)(UTraceSecondary+2), schema, context, pc, aData, aSize);
       
  1361 		TUTrace::Trace((TPrimaryFilter)(UTracePrimary+2), (TSecondaryFilter)(UTraceSecondary), schema, context, pc, aData, aSize);
       
  1362 		TUTrace::Trace((TPrimaryFilter)(UTracePrimary+2), (TSecondaryFilter)(UTraceSecondary+2), schema, context, pc, aData, aSize);
       
  1363 		TUTrace::Trace((TPrimaryFilter)(UTracePrimary+2), (TSecondaryFilter)(UTraceSecondary+1), schema, context, pc, aData, aSize);
       
  1364 		TUTrace::Trace((TPrimaryFilter)(UTracePrimary+1), (TSecondaryFilter)(UTraceSecondary), schema, context, pc, aData, aSize);
       
  1365 		TUTrace::TracePrimary((TPrimaryFilter)(UTracePrimary+2), schema, context, pc, aData, aSize);
       
  1366 
       
  1367 		
       
  1368 		r = iController->GetStringDescriptorLangId(iClient, *((TDes8*) a1));
       
  1369 		break;
       
  1370 
       
  1371 	case RDevUsbcClient::EControlSetStringDescriptorLangId:
       
  1372 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetStringDescriptorLangId"));
       
  1373 	//**********************UTRACE METHOD STATIC 10***********************/
       
  1374 		Testtracer.DoTheStaticTrace(10);
       
  1375 
       
  1376 		r = iController->SetStringDescriptorLangId(reinterpret_cast<TUint>(a1));
       
  1377 		break;
       
  1378 
       
  1379 	case RDevUsbcClient::EControlGetManufacturerStringDescriptor:
       
  1380 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetManufacturerStringDescriptor"));
       
  1381 	//**********************UTRACE METHOD STATIC 11***********************/
       
  1382 		Testtracer.DoTheStaticTrace(11);
       
  1383 
       
  1384 		r = iController->GetManufacturerStringDescriptor(iClient, *((TPtr8*) a1));
       
  1385 		break;
       
  1386 
       
  1387 	case RDevUsbcClient::EControlSetManufacturerStringDescriptor:
       
  1388 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetManufacturerStringDescriptor"));
       
  1389 	//**********************UTRACE METHOD STATIC 12***********************/
       
  1390 		Testtracer.DoTheStaticTrace(12);
       
  1391 
       
  1392 		r = iController->SetManufacturerStringDescriptor(iClient, *((TPtr8*) a1));
       
  1393 		break;
       
  1394 
       
  1395 	case RDevUsbcClient::EControlRemoveManufacturerStringDescriptor:
       
  1396 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlRemoveManufacturerStringDescriptor"));
       
  1397 	//**********************UTRACE METHOD STATIC 13***********************/
       
  1398 		Testtracer.DoTheStaticTrace(13);
       
  1399 
       
  1400 		r = iController->RemoveManufacturerStringDescriptor();
       
  1401 		break;
       
  1402 
       
  1403 	case RDevUsbcClient::EControlGetProductStringDescriptor:
       
  1404 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetProductStringDescriptor"));
       
  1405 	//**********************UTRACE METHOD STATIC 14***********************/
       
  1406 		Testtracer.DoTheStaticTrace(14);
       
  1407 	
       
  1408 		r = iController->GetProductStringDescriptor(iClient, *((TPtr8*) a1));
       
  1409 		break;
       
  1410 
       
  1411 	case RDevUsbcClient::EControlSetProductStringDescriptor:
       
  1412 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetProductStringDescriptor"));
       
  1413 	//**********************UTRACE METHOD STATIC 15***********************/
       
  1414 		Testtracer.DoTheStaticTrace(15);
       
  1415 		
       
  1416 		r = iController->SetProductStringDescriptor(iClient, *((TPtr8*) a1));
       
  1417 		break;
       
  1418 
       
  1419 	case RDevUsbcClient::EControlRemoveProductStringDescriptor:
       
  1420 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlRemoveProductStringDescriptor"));
       
  1421 	//**********************UTRACE METHOD STATIC 16***********************/
       
  1422 		Testtracer.DoTheStaticTrace(16);
       
  1423 
       
  1424 		r = iController->RemoveProductStringDescriptor();
       
  1425 		break;
       
  1426 
       
  1427 	case RDevUsbcClient::EControlGetSerialNumberStringDescriptor:
       
  1428 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetSerialNumberStringDescriptor"));
       
  1429 	//**********************UTRACE METHOD STATIC 17***********************/
       
  1430 		Testtracer.DoTheStaticTrace(17);
       
  1431 
       
  1432 		r = iController->GetSerialNumberStringDescriptor(iClient, *((TPtr8*) a1));
       
  1433 		break;
       
  1434 
       
  1435 	case RDevUsbcClient::EControlSetSerialNumberStringDescriptor:
       
  1436 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetSerialNumberStringDescriptor"));
       
  1437 	//**********************UTRACE METHOD STATIC 18***********************/
       
  1438 	//	for(int i=0;i!=UTrace_Count;i++)
       
  1439 	//		Testtracer.DoTheStaticTrace(18);
       
  1440 
       
  1441 		r = iController->SetSerialNumberStringDescriptor(iClient, *((TPtr8*) a1));
       
  1442 		break;
       
  1443 
       
  1444 	case RDevUsbcClient::EControlRemoveSerialNumberStringDescriptor:
       
  1445 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlRemoveSerialNumberStringDescriptor"));
       
  1446 		//**********************UTRACE METHOD STATIC 19***********************/
       
  1447 	//	for(int i=0;i!=UTrace_Count;i++)
       
  1448 	//		Testtracer.DoTheStaticTrace(19);
       
  1449 
       
  1450 	
       
  1451 		r = iController->RemoveSerialNumberStringDescriptor();
       
  1452 		break;
       
  1453 
       
  1454 	case RDevUsbcClient::EControlGetConfigurationStringDescriptor:
       
  1455 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetConfigurationStringDescriptor"));
       
  1456 		//**********************UTRACE METHOD NONstatic 1***********************/
       
  1457 		Testtracer.DoTheSetTrace(1, TestTraceObject);
       
  1458 		r = iController->GetConfigurationStringDescriptor(iClient, *((TPtr8*) a1));
       
  1459 		break;
       
  1460 
       
  1461 	case RDevUsbcClient::EControlSetConfigurationStringDescriptor:
       
  1462 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetConfigurationStringDescriptor"));
       
  1463 	//**********************UTRACE METHOD STATIC 21***********************/
       
  1464 	//	for(int i=0;i!=UTrace_Count;i++)
       
  1465 	//		Testtracer.DoTheStaticTrace(21);
       
  1466 	
       
  1467 		r = iController->SetConfigurationStringDescriptor(iClient, *((TPtr8*) a1));
       
  1468 		break;
       
  1469 
       
  1470 	case RDevUsbcClient::EControlRemoveConfigurationStringDescriptor:
       
  1471 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlRemoveConfigurationStringDescriptor"));
       
  1472 	//**********************UTRACE METHOD STATIC 22***********************/
       
  1473 	//	for(int i=0;i!=UTrace_Count;i++)
       
  1474 	//		Testtracer.DoTheStaticTrace(22);
       
  1475 
       
  1476 		
       
  1477 		r = iController->RemoveConfigurationStringDescriptor();
       
  1478 		break;
       
  1479 
       
  1480 	case RDevUsbcClient::EControlGetStringDescriptor:
       
  1481 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlGetStringDescriptor"));
       
  1482 	//**********************UTRACE METHOD STATIC 23***********************/
       
  1483 	//	for(int i=0;i!=UTrace_Count;i++)
       
  1484 	//		Testtracer.DoTheStaticTrace(23);
       
  1485 
       
  1486 		r = iController->GetStringDescriptor(iClient, (TUint8) (TInt) a1, *((TPtr8*) a2));
       
  1487 		break;
       
  1488 
       
  1489 	case RDevUsbcClient::EControlSetStringDescriptor:
       
  1490 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlSetStringDescriptor"));
       
  1491 		//**********************UTrace PRINTF METHODS+NEGATIVE TEST***********************/
       
  1492 			Testtracer.DoThePrintf(8);
       
  1493 			Testtracer.DoThePrintf(12);
       
  1494 			Testtracer.DoTheStaticPrintf(8);
       
  1495 			Testtracer.DoTheStaticPrintf(12);
       
  1496 		
       
  1497 		//put in extra logs for negative trace printf test
       
  1498 	
       
  1499 		NegTraceObject1.Printf("c-style \"string\" with number %i!", 9999);
       
  1500 		NegTraceObject2.Printf("c-style \"string\" with number %i!", 9999);
       
  1501 		
       
  1502 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+1), schema, context, pc);
       
  1503 		TestTraceObject.Printf("c-style \"string\" with number %i!", 9999);
       
  1504 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+2), schema, context, pc);
       
  1505 		TestTraceObject.Printf("c-style \"string\" with number %i!", 9999);
       
  1506 		
       
  1507 		TUTrace::PrintfPrimary((TPrimaryFilter)(UTracePrimary+1), context, pc, "static string no %i", 9999);
       
  1508 		TUTrace::PrintfPrimary((TPrimaryFilter)(UTracePrimary+2), context, pc, "static string no %i", 9999);
       
  1509 		
       
  1510 
       
  1511 		
       
  1512 
       
  1513 	
       
  1514 			
       
  1515 		/****************End of Printf Tracing**************************************/
       
  1516 
       
  1517 
       
  1518 		r = iController->SetStringDescriptor(iClient, (TUint8) (TInt) a1, *((TPtr8*) a2));
       
  1519 		break;
       
  1520 
       
  1521 	case RDevUsbcClient::EControlRemoveStringDescriptor:
       
  1522 		//__KTRACE_OPT(KUSB, Kern::Printf("EControlRemoveStringDescriptor"));
       
  1523 	//**********************Trace Multiple logging test***********************/
       
  1524 		Testtracer.DoThePrintf(8);
       
  1525 		NegTraceObject1.Printf("c-style \"string\" with number %i!", 9999);
       
  1526 		MultTraceObject1.Printf("c-style \"string\" with number %i!", 9999);
       
  1527 		MultTraceObject2.Printf("c-style \"string\" with number %i!", 9999);
       
  1528 		NegTraceObject2.Printf("c-style \"string\" with number %i!", 9999);
       
  1529 		MultTraceObject3.Printf("c-style \"string\" with number %i!", 9999);
       
  1530 		MultTraceObject4.Printf("c-style \"string\" with number %i!", 9999);
       
  1531 		MultTraceObject5.Printf("c-style \"string\" with number %i!", 9999);
       
  1532 		
       
  1533 		
       
  1534 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary), schema, context, pc);
       
  1535 		TestTraceObject.Printf("c-style \"string\" with number %i!", 9999);;	
       
  1536 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+1), schema, context, pc);
       
  1537 		TestTraceObject.Printf("c-style \"string\" with number %i!", 9999);
       
  1538 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary), schema, context, pc);
       
  1539 		TestTraceObject.Printf("c-style \"string\" with number %i!", 9999);
       
  1540 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+2), schema, context, pc);
       
  1541 		TestTraceObject.Printf("c-style \"string\" with number %i!", 9999);
       
  1542 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+2), schema, context, pc);
       
  1543 		TestTraceObject.Printf("c-style \"string\" with number %i!", 9999);
       
  1544 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+2), schema, context, pc);
       
  1545 		TestTraceObject.Printf("c-style \"string\" with number %i!", 9999);
       
  1546 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+1), schema, context, pc);
       
  1547 		TestTraceObject.Printf("c-style \"string\" with number %i!", 9999);
       
  1548 		TestTraceObject.Set((TPrimaryFilter)(UTracePrimary+2), schema, context, pc);
       
  1549 		TestTraceObject.Printf("c-style \"string\" with number %i!", 9999);
       
  1550 		
       
  1551 		TUTrace::PrintfPrimary((TPrimaryFilter)(UTracePrimary),  context, pc, "c-style \"string\" with number %i!", 9999);
       
  1552 		TUTrace::PrintfPrimary((TPrimaryFilter)(UTracePrimary+1), context, pc, "c-style \"string\" with number %i!", 9999);
       
  1553 		TUTrace::PrintfPrimary((TPrimaryFilter)(UTracePrimary),  context, pc, "c-style \"string\" with number %i!", 9999);
       
  1554 		TUTrace::PrintfPrimary((TPrimaryFilter)(UTracePrimary+2),  context, pc, "c-style \"string\" with number %i!", 9999);
       
  1555 		TUTrace::PrintfPrimary((TPrimaryFilter)(UTracePrimary+2),  context, pc, "c-style \"string\" with number %i!", 9999);
       
  1556 		TUTrace::PrintfPrimary((TPrimaryFilter)(UTracePrimary+2),  context, pc, "c-style \"string\" with number %i!", 9999);
       
  1557 		TUTrace::PrintfPrimary((TPrimaryFilter)(UTracePrimary+1),  context, pc, "c-style \"string\" with number %i!", 9999);
       
  1558 		TUTrace::PrintfPrimary((TPrimaryFilter)(UTracePrimary+2), context, pc, "c-style \"string\" with number %i!", 9999);
       
  1559 
       
  1560 		
       
  1561 		r = iController->RemoveStringDescriptor((TUint8) (TInt) a1);
       
  1562 		break;
       
  1563 
       
  1564 	case RDevUsbcClient::EControlAllocateEndpointResource:
       
  1565 	
       
  1566 	
       
  1567 		epRes = (TUsbcEndpointResource)((TInt) a2);
       
  1568 		if (!ValidEndpoint((TInt)a1))
       
  1569 			{
       
  1570 			r = KErrUsbEpNotInInterface;
       
  1571 			}
       
  1572 		else
       
  1573 			{
       
  1574 			r = iController->AllocateEndpointResource(this, iEndpoint[(TInt)a1]->RealEpNumber(), epRes);
       
  1575 			}
       
  1576 		break;
       
  1577 
       
  1578 	case RDevUsbcClient::EControlDeAllocateEndpointResource:
       
  1579 		epRes = (TUsbcEndpointResource)((TInt) a2);
       
  1580 		if (!ValidEndpoint((TInt)a1))
       
  1581 			{
       
  1582 			r = KErrUsbEpNotInInterface;
       
  1583 			}
       
  1584 		else
       
  1585 			{
       
  1586 			r = iController->DeAllocateEndpointResource(this, iEndpoint[(TInt)a1]->RealEpNumber(), epRes);
       
  1587 			}
       
  1588 		break;
       
  1589 
       
  1590 	case RDevUsbcClient::EControlQueryEndpointResourceUse:
       
  1591 		epRes = (TUsbcEndpointResource)((TInt) a2);
       
  1592 		if (!ValidEndpoint((TInt)a1))
       
  1593 			{
       
  1594 			r = KErrUsbEpNotInInterface;
       
  1595 			}
       
  1596 		else
       
  1597 			{
       
  1598 			r = iController->QueryEndpointResource(this, iEndpoint[(TInt)a1]->RealEpNumber(), epRes);
       
  1599 			}
       
  1600 		break;
       
  1601 
       
  1602 	case RDevUsbcClient::EControlSetOtgDescriptor:
       
  1603 		{
       
  1604 		r = iController->SetOtgDescriptor(iClient, *((const TDesC8*)a1));
       
  1605 		}
       
  1606 		break;
       
  1607 
       
  1608 	case RDevUsbcClient::EControlGetOtgDescriptor:
       
  1609 		{
       
  1610 		r = iController->GetOtgDescriptor(iClient, *((TDes8*)a1));
       
  1611 		}
       
  1612 		break;
       
  1613 
       
  1614 	case RDevUsbcClient::EControlGetOtgFeatures:
       
  1615 		{
       
  1616 		r = iController->GetOtgFeatures(iClient, *((TDes8*)a1));
       
  1617 		}
       
  1618 		break;
       
  1619 
       
  1620 	case RDevUsbcClient::EControlEnableUsbDriver:
       
  1621 		{		
       
  1622 		iController->EnableClientStack();
       
  1623 		}
       
  1624 		break;
       
  1625 
       
  1626     case RDevUsbcClient::EControlDisableUsbDriver:
       
  1627 		{
       
  1628 		iController->DisableClientStack();
       
  1629 		}
       
  1630 		break;
       
  1631 
       
  1632     default:
       
  1633 		//__KTRACE_OPT(KUSB, Kern::Printf("Function code not supported"));
       
  1634 		r = KErrNotSupported;
       
  1635 		}
       
  1636 
       
  1637 	return r;
       
  1638 	}
       
  1639 
       
  1640 
       
  1641 TInt DLddUsbcChannel::SetInterface(TInt aInterfaceNumber, TUsbcIfcInfo* aInfoBuf)
       
  1642 	{
       
  1643 	TUsbcInterfaceInfoBuf ifc_info_buf;
       
  1644 	TUsbcInterfaceInfoBuf* const ifc_info_buf_ptr = aInfoBuf->iInterfaceData;
       
  1645 	const TInt srcLen = Kern::ThreadGetDesLength(iClient, ifc_info_buf_ptr);
       
  1646 	if (srcLen < ifc_info_buf.Length())
       
  1647 		{
       
  1648 		//__KTRACE_OPT(KUSB, Kern::Printf("SetInterface can't copy"));
       
  1649 		PanicClientThread(EDesOverflow);
       
  1650 		}
       
  1651 
       
  1652 	TInt r = Kern::ThreadDesRead(iClient, ifc_info_buf_ptr, ifc_info_buf, 0, KChunkShiftBy0);
       
  1653 	if (r != KErrNone)
       
  1654 		{
       
  1655 		//__KTRACE_OPT(KUSB, Kern::Printf("SetInterface Copy failed reason=%d", r));
       
  1656 		PanicClientThread(r);
       
  1657 		}
       
  1658 
       
  1659 	TUsbcEndpointInfo* pEndpointData = ifc_info_buf().iEndpointData;
       
  1660 
       
  1661 	// If an alternate interface is being asked for then do nothing,
       
  1662 	// just pass it down to the Controller.
       
  1663 	const TInt num_endpoints = ifc_info_buf().iTotalEndpointsUsed;
       
  1664 	//__KTRACE_OPT(KUSB, Kern::Printf("SetInterface num_endpoints=%d", num_endpoints));
       
  1665 
       
  1666 	// [The next 4 variables have to be initialized here because of the goto's that follow.]
       
  1667 	// Default attribute for non-dma enabled buffering:
       
  1668 	TUint32 cacheAttribs = EMapAttrSupRw | EMapAttrCachedMax;
       
  1669 	const TUint32 bandwidthPriority = aInfoBuf->iBandwidthPriority;
       
  1670 	// (IN & OUT eps need to have different cache attribs:
       
  1671 	//  IN will be uncached, OUT will be cached.)
       
  1672 	TInt totalINBufferSize = 0;
       
  1673 	TInt totalOUTBufferSize = 0;
       
  1674 
       
  1675 	TInt real_ep_numbers[6] = {-1, -1, -1, -1, -1, -1};
       
  1676 
       
  1677     TUsbcAlternateSettingList* alternateSettingListRec = new TUsbcAlternateSettingList;
       
  1678 	if (!alternateSettingListRec)
       
  1679 		{
       
  1680 		r = KErrNoMemory;
       
  1681 		goto KillAll;
       
  1682 		}
       
  1683 
       
  1684 	// chain in this alternate setting
       
  1685 	alternateSettingListRec->iNext = iAlternateSettingList;
       
  1686 	iAlternateSettingList = alternateSettingListRec;
       
  1687 	alternateSettingListRec->iSetting = aInterfaceNumber;
       
  1688 	alternateSettingListRec->iNumberOfEndpoints = num_endpoints;
       
  1689 
       
  1690 	// ep0
       
  1691 	if (iEndpoint[0] == NULL)
       
  1692 		{
       
  1693 		//__KTRACE_OPT(KUSB, Kern::Printf("SetInterface 11"));
       
  1694 		r = SetupEp0();
       
  1695 		if (r != KErrNone)
       
  1696 			{
       
  1697 			//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: SetupEp0() failed"));
       
  1698 			goto KillAll;
       
  1699 			}
       
  1700 		}
       
  1701 
       
  1702 	// other endpoints
       
  1703 	for (TInt i = 1; i <= num_endpoints; i++, pEndpointData++)
       
  1704 		{
       
  1705 		//__KTRACE_OPT(KUSB, Kern::Printf("SetInterface for ep=%d", i));
       
  1706 		if (!ValidateEndpoint(pEndpointData))
       
  1707 			{
       
  1708 			r = KErrUsbBadEndpoint;
       
  1709 			goto KillAll;
       
  1710 			}
       
  1711 		TUsbcEndpoint* ep = new TUsbcEndpoint(this, iController, pEndpointData, i, bandwidthPriority);
       
  1712 		alternateSettingListRec->iEndpoint[i] = ep;
       
  1713 		if (!ep)
       
  1714 			{
       
  1715 			r = KErrNoMemory;
       
  1716 			goto KillAll;
       
  1717 			}
       
  1718 		if (ep->Construct() != KErrNone)
       
  1719 			{
       
  1720 			r = KErrNoMemory;
       
  1721 			goto KillAll;
       
  1722 			}
       
  1723 		if (pEndpointData->iDir == KUsbEpDirIn)
       
  1724 			{
       
  1725 			totalINBufferSize += ep->BufferTotalSize();
       
  1726 			//__KTRACE_OPT(KUSB, Kern::Printf("IN buffering now %d", totalINBufferSize));
       
  1727 			}
       
  1728 		else if (pEndpointData->iDir == KUsbEpDirOut)
       
  1729 			{
       
  1730 			totalOUTBufferSize += ep->BufferTotalSize();
       
  1731 			//__KTRACE_OPT(KUSB, Kern::Printf("OUT buffering now %d", totalOUTBufferSize));
       
  1732 			}
       
  1733 		//__KTRACE_OPT(KUSB, Kern::Printf("SetInterface for ep=%d rec=0x%08x ep==0x%08x",
       
  1734 									//	i, alternateSettingListRec, ep));
       
  1735 		}
       
  1736 
       
  1737 	// See if PIL will accept this interface
       
  1738 	//__KTRACE_OPT(KUSB, Kern::Printf("SetInterface Calling controller"));
       
  1739 	r = iController->SetInterface(this,
       
  1740 								  iClient,
       
  1741 								  aInterfaceNumber,
       
  1742 								  ifc_info_buf().iClass,
       
  1743 								  aInfoBuf->iString,
       
  1744 								  (TInt) ifc_info_buf().iTotalEndpointsUsed,
       
  1745 								  (TUsbcEndpointInfo*) ifc_info_buf().iEndpointData,
       
  1746 								  &real_ep_numbers);
       
  1747 
       
  1748 	//__KTRACE_OPT(KUSB, Kern::Printf("SetInterface controller returned %d", r));
       
  1749 	if (r != KErrNone)
       
  1750 		{
       
  1751 		//__KTRACE_OPT(KPANIC, Kern::Printf("SetInterface failed reason=%d", r));
       
  1752 		goto KillAll;
       
  1753 		}
       
  1754 
       
  1755 	// Record the 'real' endpoint number used by the PDD in both the Ep and
       
  1756 	// the Req callback:
       
  1757 	for (TInt i = 1; i <= num_endpoints; i++)
       
  1758 		{
       
  1759 		alternateSettingListRec->iEndpoint[i]->SetRealEpNumber(real_ep_numbers[i]);
       
  1760 		}
       
  1761 
       
  1762 	if (totalOUTBufferSize != 0)
       
  1763 		{
       
  1764 		// maximally cached always
       
  1765 		//__KTRACE_OPT(KUSB, Kern::Printf("SetInterface setting up OUT buffering size=%d", totalOUTBufferSize));
       
  1766 		iHwChunkOUT = SetupInterfaceMemory(totalOUTBufferSize, iHwChunkOUT, KUsbEpDirOut, cacheAttribs);
       
  1767 		if (iHwChunkOUT == NULL)
       
  1768 			{
       
  1769 			//__KTRACE_OPT(KPANIC, Kern::Printf("SetInterface can't get chunk for OUT buffering size=%d reason=%d",
       
  1770 										//	  totalOUTBufferSize, r));
       
  1771 			r = KErrNoMemory;
       
  1772 			goto KillAll;
       
  1773 			}
       
  1774 		}
       
  1775 	if (totalINBufferSize != 0)
       
  1776 		{
       
  1777 		cacheAttribs = EMapAttrSupRw;
       
  1778 		//__KTRACE_OPT(KUSB, Kern::Printf("SetInterface setting up IN buffering size=%d", totalINBufferSize));
       
  1779 		iHwChunkIN = SetupInterfaceMemory(totalINBufferSize, iHwChunkIN, KUsbEpDirIn, cacheAttribs);
       
  1780 		if (iHwChunkIN == NULL)
       
  1781 			{
       
  1782 			//__KTRACE_OPT(KPANIC, Kern::Printf("SetInterface can't get chunk for IN buffering size=%d reason=%d",
       
  1783 											//  totalOUTBufferSize, r));
       
  1784 			r = KErrNoMemory;
       
  1785 			goto KillAll;
       
  1786 			}
       
  1787 		}
       
  1788 	//__KTRACE_OPT(KUSB, Kern::Printf("SetInterface ready to exit"));
       
  1789 
       
  1790 	if (aInterfaceNumber == 0)
       
  1791 		{
       
  1792 		// make sure we're ready to go with the main interface
       
  1793 		iValidInterface = ETrue;
       
  1794 		//__KTRACE_OPT(KUSB, Kern::Printf("SetInterface SelectAlternateSetting"));
       
  1795 		SelectAlternateSetting(0);
       
  1796 		}
       
  1797 
       
  1798 	return KErrNone;
       
  1799 
       
  1800  KillAll:
       
  1801 	DestroyAllInterfaces();
       
  1802 	DestroyEp0();
       
  1803 	return r;
       
  1804 	}
       
  1805 
       
  1806 
       
  1807 DPlatChunkHw* DLddUsbcChannel::SetupInterfaceMemory(TInt aBufferSize, DPlatChunkHw* aHwChunk,
       
  1808 													TUint aDirection, TUint32 aCacheAttribs)
       
  1809 	{
       
  1810 	TUint8* oldBase = NULL;
       
  1811 	if (aHwChunk != NULL)
       
  1812 		oldBase = reinterpret_cast<TUint8*>(aHwChunk->LinearAddress());
       
  1813 
       
  1814 	DPlatChunkHw* chunk = ReAllocate(aBufferSize, aHwChunk, aCacheAttribs);
       
  1815 	if (chunk == NULL)
       
  1816 		{
       
  1817 		// lost all interfaces:
       
  1818 		// Tell Controller to release Interface and h/w resources associated with this
       
  1819 		iController->DeRegisterClient(this);
       
  1820 		}
       
  1821 	else
       
  1822 		{
       
  1823 		// Parcel out the memory between endpoints
       
  1824 		TUint8* newBase = reinterpret_cast<TUint8*>(chunk->LinearAddress());
       
  1825 		TBool needsRebase = (newBase != oldBase);
       
  1826 		TUint8* pBuf = newBase;
       
  1827 		TUint8* pBufIf = pBuf;							   // this is where an interface's ep buffering starts
       
  1828 		TUsbcAlternateSettingList* asRec = iAlternateSettingList;
       
  1829 		// the current interface
       
  1830 		//__KTRACE_OPT(KUSB, Kern::Printf("SetupInterfaceMemory rebasing setting=%d", asRec->iSetting));
       
  1831 		RebaseInterfaceMemory(asRec, pBuf, aDirection);
       
  1832 		// now the others if a rebase has occured
       
  1833 		if (needsRebase)
       
  1834 			{
       
  1835 			//__KTRACE_OPT(KUSB, Kern::Printf("SetupInterfaceMemory rebasing "));
       
  1836 			asRec = asRec->iNext;
       
  1837 			while (asRec)
       
  1838 				{
       
  1839 				// Interfaces are not concurrent so they can all start at the same logical address
       
  1840 				//__KTRACE_OPT(KUSB, Kern::Printf("SetupInterfaceMemory rebasing setting=%d", asRec->iSetting));
       
  1841 				pBuf = pBufIf;
       
  1842 				RebaseInterfaceMemory(asRec, pBuf, aDirection);
       
  1843 				asRec = asRec->iNext;
       
  1844 				}
       
  1845 			}
       
  1846 		//__KTRACE_OPT(KUSB, Kern::Printf("SetInterface numberOfEndpoints"));
       
  1847 		}
       
  1848 	return chunk;
       
  1849 	}
       
  1850 
       
  1851 
       
  1852 TInt DLddUsbcChannel::SetupEp0()
       
  1853 	{
       
  1854 	//__KTRACE_OPT(KUSB, Kern::Printf("SetupEp0 entry %x", this));
       
  1855 	TInt ep0Size = iController->Ep0PacketSize();
       
  1856 	TUsbcEndpointInfo ep0Info = TUsbcEndpointInfo(KUsbEpTypeControl, KUsbEpDirBidirect, ep0Size);
       
  1857 	TUsbcEndpoint* ep0 = new TUsbcEndpoint(this, iController, &ep0Info, 0, 0);
       
  1858 	if (ep0 == NULL)
       
  1859 		{
       
  1860 		return KErrNoMemory;
       
  1861 		}
       
  1862 	// In case we have to return early:
       
  1863 	iEndpoint[0] = ep0;
       
  1864 	TInt r = ep0->Construct();
       
  1865 	if (r != KErrNone)
       
  1866 		{
       
  1867 		return KErrNoMemory;
       
  1868 		}
       
  1869 	TInt bufferSize = ep0->BufferTotalSize();
       
  1870 	TUint32 cacheAttribs = EMapAttrSupRw | EMapAttrCachedMax;
       
  1871 	iHwChunkEp0 = Allocate(bufferSize, cacheAttribs);
       
  1872 	if (iHwChunkEp0 == NULL)
       
  1873 		{
       
  1874 		return KErrNoMemory;
       
  1875 		}
       
  1876 	iBufferSizeEp0 = bufferSize;
       
  1877 	iBufferBaseEp0 = (TUint8*) iHwChunkEp0->LinearAddress();
       
  1878 	ep0->SetBufferBase(iBufferBaseEp0);
       
  1879 	ep0->SetRealEpNumber(0);
       
  1880 	//__KTRACE_OPT(KUSB, Kern::Printf("SetupEp0 60 buffersize=%d", iBufferSizeEp0));
       
  1881 	//__KTRACE_OPT(KUSB, Kern::Printf("SetupEp0 exit bufferbase=0x%08x", iBufferBaseEp0));
       
  1882 	return KErrNone;
       
  1883 	}
       
  1884 
       
  1885 
       
  1886 void DLddUsbcChannel::RebaseInterfaceMemory(TUsbcAlternateSettingList* aAlternateSettingListRec,
       
  1887 											TUint8* aBase, TUint aDirection)
       
  1888 	{
       
  1889 	TUint8* pBuf = aBase;
       
  1890 	//__KTRACE_OPT(KUSB, Kern::Printf("RebaseInterfaceMemory buffer base rec= 0x%08x", aAlternateSettingListRec));
       
  1891 	for (TInt i = 1; i <= aAlternateSettingListRec->iNumberOfEndpoints; i++)
       
  1892 		{
       
  1893 		TUsbcEndpoint* ep = aAlternateSettingListRec->iEndpoint[i];
       
  1894 		if (ep != NULL && (ep->EndpointInfo()->iDir == aDirection))
       
  1895 			{
       
  1896 			//__KTRACE_OPT(KUSB, Kern::Printf("RebaseInterfaceMemory buffer base for ep%d 0x%08x 0x%08x",
       
  1897 										//	i, pBuf, ep));
       
  1898 			pBuf = ep->SetBufferBase(pBuf);
       
  1899 			}
       
  1900 		else
       
  1901 			{
       
  1902 			//__KTRACE_OPT(KUSB, Kern::Printf("RebaseInterfaceMemory ep%d wrong direction", i));
       
  1903 			}
       
  1904 		}
       
  1905 	}
       
  1906 
       
  1907 
       
  1908 void DLddUsbcChannel::DestroyAllInterfaces()
       
  1909 	{
       
  1910 	// Removes all interfaces
       
  1911 	TUsbcAlternateSettingList* alternateSettingListRec = iAlternateSettingList;
       
  1912 	while (alternateSettingListRec)
       
  1913 		{
       
  1914 		DestroyEndpoints(alternateSettingListRec);
       
  1915 		iController->ReleaseInterface(this, alternateSettingListRec->iSetting);
       
  1916 		TUsbcAlternateSettingList* alternateSettingListRecNext = alternateSettingListRec->iNext;
       
  1917 		delete alternateSettingListRec;
       
  1918 		alternateSettingListRec = alternateSettingListRecNext;
       
  1919 		}
       
  1920 	iNumberOfEndpoints = 0;
       
  1921 	iAlternateSettingList = NULL;
       
  1922 
       
  1923 	ClosePhysicalChunk(iHwChunkIN);
       
  1924 	ClosePhysicalChunk(iHwChunkOUT);
       
  1925 
       
  1926 	iValidInterface = EFalse;
       
  1927 	}
       
  1928 
       
  1929 
       
  1930 void DLddUsbcChannel::DestroyInterface(TUint aInterfaceNumber)
       
  1931 	{
       
  1932 	if (iAlternateSetting == aInterfaceNumber)
       
  1933 		{
       
  1934 		ResetInterface(KErrUsbInterfaceNotReady);
       
  1935 		iValidInterface = EFalse;
       
  1936 		iNumberOfEndpoints = 0;
       
  1937 		for (TInt i = 1; i <= KMaxEndpointsPerClient; i++)
       
  1938 			{
       
  1939 			iEndpoint[i] = NULL;
       
  1940 			}
       
  1941 		}
       
  1942 	TUsbcAlternateSettingList* alternateSettingListRec = iAlternateSettingList;
       
  1943 	TUsbcAlternateSettingList* alternateSettingListRecOld = NULL;
       
  1944 	while (alternateSettingListRec)
       
  1945 		{
       
  1946 		TUsbcAlternateSettingList* alternateSettingListRecNext = alternateSettingListRec->iNext;
       
  1947 		if (alternateSettingListRec->iSetting == aInterfaceNumber)
       
  1948 			{
       
  1949 			// This record is to be deleted
       
  1950 			if (alternateSettingListRecOld == NULL)
       
  1951 				{
       
  1952 				// The record to be deleted is at the list head
       
  1953 				iAlternateSettingList = alternateSettingListRecNext;
       
  1954 				}
       
  1955 			else
       
  1956 				{
       
  1957 				// The record to be deleted is NOT at the list head
       
  1958 				alternateSettingListRecOld->iNext = alternateSettingListRecNext;
       
  1959 				}
       
  1960 			DestroyEndpoints(alternateSettingListRec);
       
  1961 			delete alternateSettingListRec;
       
  1962 			break;
       
  1963 			}
       
  1964 		alternateSettingListRecOld = alternateSettingListRec;
       
  1965 		alternateSettingListRec = alternateSettingListRecNext;
       
  1966 		}
       
  1967 
       
  1968 	if (iAlternateSettingList == NULL)
       
  1969 		{
       
  1970 		// if no interfaces left destroy non-ep0 buffering
       
  1971 		ClosePhysicalChunk(iHwChunkIN);
       
  1972 		ClosePhysicalChunk(iHwChunkOUT);
       
  1973 		}
       
  1974 	}
       
  1975 
       
  1976 
       
  1977 void DLddUsbcChannel::DestroyEp0()
       
  1978 	{
       
  1979 	delete iEndpoint[0];
       
  1980 	iEndpoint[0] = NULL;
       
  1981 	ClosePhysicalChunk(iHwChunkEp0);
       
  1982 	}
       
  1983 
       
  1984 
       
  1985 void DLddUsbcChannel::DestroyEndpoints(TUsbcAlternateSettingList* aListRec)
       
  1986 	{
       
  1987 	for (TInt i = 1; i < KMaxEndpointsPerClient; i++)
       
  1988 		{
       
  1989 		delete aListRec->iEndpoint[i];
       
  1990 		aListRec->iEndpoint[i] = NULL;
       
  1991 		}
       
  1992 	}
       
  1993 
       
  1994 
       
  1995 void DLddUsbcChannel::EndpointStatusChangeCallback(TAny* aDLddUsbcChannel)
       
  1996     {
       
  1997 	//__KTRACE_OPT(KUSB, Kern::Printf("EndpointStatusChangeCallback"));
       
  1998     DLddUsbcChannel* dUsbc = (DLddUsbcChannel*) aDLddUsbcChannel;
       
  1999 	if (dUsbc->iChannelClosing)
       
  2000 		return;
       
  2001 	TUint endpointState = dUsbc->iEndpointStatusCallbackInfo.State();
       
  2002 	const TInt reqNo = (TInt) RDevUsbcClient::ERequestEndpointStatusNotify;
       
  2003 	if (dUsbc->iRequestStatus[reqNo])
       
  2004 		{
       
  2005 		//__KTRACE_OPT(KUSB, Kern::Printf("EndpointStatusChangeCallback Notify status"));
       
  2006 		DThread* client = dUsbc->iClient;
       
  2007 		// set client descriptor length to zero
       
  2008 		TInt r = Kern::ThreadRawWrite(client, dUsbc->iEndpointStatusChangePtr, &endpointState,
       
  2009 									  sizeof(TUint), client);
       
  2010 		if (r != KErrNone)
       
  2011 			dUsbc->PanicClientThread(r);
       
  2012 		Kern::RequestComplete(dUsbc->iClient, dUsbc->iRequestStatus[reqNo], r);
       
  2013 		dUsbc->iEndpointStatusChangePtr = NULL;
       
  2014 		}
       
  2015 	}
       
  2016 
       
  2017 
       
  2018 void DLddUsbcChannel::StatusChangeCallback(TAny* aDLddUsbcChannel)
       
  2019 	{
       
  2020     DLddUsbcChannel* dUsbc = (DLddUsbcChannel*) aDLddUsbcChannel;
       
  2021 	if (dUsbc->iChannelClosing)
       
  2022 		return;
       
  2023 
       
  2024     TUsbcDeviceState deviceState;
       
  2025     TInt i;
       
  2026  	for (i = 0;
       
  2027  		 (i < KUsbcDeviceStateRequests) && ((deviceState = dUsbc->iStatusCallbackInfo.State(i)) != EUsbcNoState);
       
  2028  		 ++i)
       
  2029 		{
       
  2030  		//__KTRACE_OPT(KUSB, Kern::Printf("StatusChangeCallBack status=%d", deviceState));
       
  2031 		if (deviceState & KUsbAlternateSetting)
       
  2032 			{
       
  2033 			dUsbc->ProcessAlternateSetting(deviceState);
       
  2034 			}
       
  2035 		else
       
  2036 			{
       
  2037 			dUsbc->ProcessDeviceState(deviceState);
       
  2038 			}
       
  2039 		// Only queue if userside is interested
       
  2040 		if (dUsbc->iDeviceStatusNeeded)
       
  2041 			{
       
  2042 			dUsbc->iStatusFifo->AddStatusToQueue(deviceState);
       
  2043 			const TInt reqNo = (TInt) RDevUsbcClient::ERequestAlternateDeviceStatusNotify;
       
  2044 			if (dUsbc->AlternateDeviceStateTestComplete())
       
  2045 				Kern::RequestComplete(dUsbc->iClient, dUsbc->iRequestStatus[reqNo], KErrNone);
       
  2046 			}
       
  2047 		}
       
  2048  	// We don't want to be interrupted in the middle of this:
       
  2049 	const TInt irqs = NKern::DisableInterrupts(2);
       
  2050  	dUsbc->iStatusCallbackInfo.ResetState();
       
  2051 	NKern::RestoreInterrupts(irqs);
       
  2052 	}
       
  2053 
       
  2054 
       
  2055 void DLddUsbcChannel::OtgFeatureChangeCallback(TAny* aDLddUsbcChannel)
       
  2056     {
       
  2057 	//__KTRACE_OPT(KUSB, Kern::Printf("OtgFeatureChangeCallback"));
       
  2058     DLddUsbcChannel* dUsbc = (DLddUsbcChannel*) aDLddUsbcChannel;
       
  2059 	if (dUsbc->iChannelClosing)
       
  2060 		return;
       
  2061 
       
  2062     TUint8 features;
       
  2063     // No return value check. Assume OTG always supported here
       
  2064     dUsbc->iController->GetCurrentOtgFeatures(features);
       
  2065 
       
  2066     const TInt reqNo = (TInt) RDevUsbcClient::ERequestOtgFeaturesNotify;
       
  2067 	if (dUsbc->iRequestStatus[reqNo])
       
  2068 		{
       
  2069 		//__KTRACE_OPT(KUSB, Kern::Printf("OtgFeatureChangeCallback Notify status"));
       
  2070 		TInt r = Kern::ThreadRawWrite(dUsbc->iClient, dUsbc->iOtgFeatureChangePtr,
       
  2071                                       &features, sizeof(TUint8), dUsbc->iClient);
       
  2072 		if (r != KErrNone)
       
  2073 			dUsbc->PanicClientThread(r);
       
  2074 		Kern::RequestComplete(dUsbc->iClient, dUsbc->iRequestStatus[reqNo], r);
       
  2075 		dUsbc->iOtgFeatureChangePtr = NULL;
       
  2076 		}
       
  2077     }
       
  2078 
       
  2079 
       
  2080 TInt DLddUsbcChannel::SelectAlternateSetting(TUint aAlternateSetting)
       
  2081 	{
       
  2082 	TInt r = KErrGeneral;									// error code doesn't go userside
       
  2083 	TUsbcAlternateSettingList* alternateSettingListRec = iAlternateSettingList;
       
  2084 	while (alternateSettingListRec)
       
  2085 		{
       
  2086 		if (alternateSettingListRec->iSetting == aAlternateSetting)
       
  2087 			{
       
  2088 			// found the correct interface, now latch in new endpoint set
       
  2089 			for (TInt i = 1; i <= KMaxEndpointsPerClient; i++)
       
  2090 				{
       
  2091 				iEndpoint[i] = NULL;
       
  2092 				}
       
  2093 			iNumberOfEndpoints = alternateSettingListRec->iNumberOfEndpoints;
       
  2094 			r = KErrNone;
       
  2095 			for (TInt i = 1; i <= KMaxEndpointsPerClient; i++)
       
  2096 				{
       
  2097 				iEndpoint[i] = alternateSettingListRec->iEndpoint[i];
       
  2098 				}
       
  2099 			}
       
  2100 		alternateSettingListRec = alternateSettingListRec->iNext;
       
  2101 		}
       
  2102 	return r;
       
  2103 	}
       
  2104 
       
  2105 
       
  2106 TInt DLddUsbcChannel::EpFromAlternateSetting(TUint aAlternateSetting, TInt aEndpoint)
       
  2107 	{
       
  2108 	TUsbcAlternateSettingList* alternateSettingListRec = iAlternateSettingList;
       
  2109 	while (alternateSettingListRec)
       
  2110 		{
       
  2111 		if (alternateSettingListRec->iSetting == aAlternateSetting)
       
  2112 			{
       
  2113 			if ((aEndpoint <= alternateSettingListRec->iNumberOfEndpoints) &&
       
  2114 				(aEndpoint >= 0))
       
  2115 				{
       
  2116 				return alternateSettingListRec->iEndpoint[aEndpoint]->RealEpNumber();
       
  2117 				}
       
  2118 			else
       
  2119 				{
       
  2120 				//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: aEndpoint %d wrong for aAlternateSetting %d",
       
  2121 											//	  aEndpoint, aAlternateSetting));
       
  2122 				return -1;
       
  2123 				}
       
  2124 			}
       
  2125 		alternateSettingListRec = alternateSettingListRec->iNext;
       
  2126 		}
       
  2127 	//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: no aAlternateSetting %d found", aAlternateSetting));
       
  2128 	return -1;
       
  2129 	}
       
  2130 
       
  2131 
       
  2132 TInt DLddUsbcChannel::ProcessAlternateSetting(TUint aAlternateSetting)
       
  2133 	{
       
  2134 	ResetInterface(KErrUsbInterfaceChange);					// kill any outstanding transfers
       
  2135 	//__KTRACE_OPT(KUSB, Kern::Printf("ProcessAlternateSetting 0x%08x", aAlternateSetting));
       
  2136 	TUint newSetting = aAlternateSetting&(~KUsbAlternateSetting);
       
  2137 	//__KTRACE_OPT(KUSB, Kern::Printf("ProcessAlternateSetting selecting alternate setting 0x%08x", newSetting));
       
  2138 	TInt r = SelectAlternateSetting(newSetting);
       
  2139 	if (r != KErrNone)
       
  2140 		return r;
       
  2141 	StartEpReads();
       
  2142 	iAlternateSetting = newSetting;
       
  2143     return KErrNone;
       
  2144 	}
       
  2145 
       
  2146 
       
  2147 TInt DLddUsbcChannel::ProcessDeviceState(TUsbcDeviceState aDeviceState)
       
  2148 	{
       
  2149 	//__KTRACE_OPT(KUSB, Kern::Printf("ProcessDeviceState(%d -> %d)", iDeviceState, aDeviceState));
       
  2150 	if (iDeviceState == aDeviceState)
       
  2151 		{
       
  2152 		//__KTRACE_OPT(KUSB, Kern::Printf("  No state change => nothing to be done."));
       
  2153 		return KErrNone;
       
  2154 		}
       
  2155 	if (iDeviceState == EUsbcDeviceStateSuspended)
       
  2156 		{
       
  2157 		//__KTRACE_OPT(KUSB, Kern::Printf("  Coming out of Suspend: old state = %d", iOldDeviceState));
       
  2158 		iDeviceState = iOldDeviceState;
       
  2159 		if (iDeviceState == aDeviceState)
       
  2160 			{
       
  2161 			//__KTRACE_OPT(KUSB, Kern::Printf("  New state same as before Suspend => nothing to be done."));
       
  2162 			return KErrNone;
       
  2163 			}
       
  2164 		}
       
  2165 	TBool renumerateState = (aDeviceState == EUsbcDeviceStateConfigured);
       
  2166 	TBool deconfigured = EFalse;
       
  2167 	TInt cancellationCode = KErrNone;
       
  2168 	if (aDeviceState == EUsbcDeviceStateSuspended)
       
  2169 		{
       
  2170 		//__KTRACE_OPT(KUSB, Kern::Printf("  Suspending..."));
       
  2171 		iOldDeviceState = iDeviceState;
       
  2172 		// Put PSL into low power mode here
       
  2173 		}
       
  2174 	else
       
  2175 		{
       
  2176 		deconfigured = (iDeviceState == EUsbcDeviceStateConfigured &&
       
  2177 						aDeviceState != EUsbcDeviceStateConfigured);
       
  2178 		if (iDeviceState == EUsbcDeviceStateConfigured)
       
  2179 			{
       
  2180 			if (aDeviceState == EUsbcDeviceStateUndefined)
       
  2181 				cancellationCode = KErrUsbCableDetached;
       
  2182 			else if (aDeviceState == EUsbcDeviceStateAddress)
       
  2183 				cancellationCode = KErrUsbDeviceNotConfigured;
       
  2184 			else if (aDeviceState == EUsbcDeviceStateDefault)
       
  2185 				cancellationCode = KErrUsbDeviceBusReset;
       
  2186 			else
       
  2187 				cancellationCode = KErrUsbDeviceNotConfigured;
       
  2188 			}
       
  2189 		}
       
  2190 	//__KTRACE_OPT(KUSB, Kern::Printf("  %d --> %d", iDeviceState, aDeviceState));
       
  2191 	iDeviceState = aDeviceState;
       
  2192 	if (iValidInterface || iOwnsDeviceControl)
       
  2193 		{
       
  2194 		// This LDD may not own an interface. It could be some manager reenumerating
       
  2195 		// after its subordinate LDDs have setup their interfaces.
       
  2196 		if (deconfigured)
       
  2197 			{
       
  2198 		    DeConfigure(cancellationCode);
       
  2199 			}
       
  2200 		else if (renumerateState)
       
  2201 			{
       
  2202 			// We are enumerated so let's start a read on every endpoint
       
  2203 			ResetInterface(KErrUsbInterfaceChange);
       
  2204 			// Select main interface & latch in new endpoint set
       
  2205 			SelectAlternateSetting(0);
       
  2206 			// Only after correct ifc setting has been chosen!
       
  2207 			UpdateEndpointSizes();
       
  2208 			// Here we go
       
  2209 			StartEpReads();
       
  2210 			}
       
  2211 		}
       
  2212 
       
  2213 	const TInt reqNo = (TInt) RDevUsbcClient::ERequestReEnumerate;
       
  2214 	if (renumerateState && iRequestStatus[reqNo])
       
  2215 		{
       
  2216 		// This lot must be done if we are reenumerated
       
  2217 		Kern::RequestComplete(iClient, iRequestStatus[reqNo], KErrNone);
       
  2218 		}
       
  2219 
       
  2220     return KErrNone;
       
  2221     }
       
  2222 
       
  2223 
       
  2224 void DLddUsbcChannel::UpdateEndpointSizes()
       
  2225 	{
       
  2226 	// First Ep0.
       
  2227 	iEndpoint[0]->SetMaxPacketSize(iController->Ep0PacketSize());
       
  2228 	// Then the regular ones.
       
  2229 	TInt i = 0;
       
  2230 	while ((++i <= KMaxEndpointsPerClient) && iEndpoint[i])
       
  2231 		{
       
  2232 		const TInt size = iController->EndpointPacketSize(this, iEndpoint[i]->RealEpNumber());
       
  2233 		if (size < 0)
       
  2234 			{
       
  2235 			//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Packet size < 0 for ep %d", i));
       
  2236 			continue;
       
  2237 			}
       
  2238 		iEndpoint[i]->SetMaxPacketSize(size);
       
  2239 		}
       
  2240 	__ASSERT_DEBUG(i == iNumberOfEndpoints + 1,
       
  2241 				   Kern::Printf("  Error: iNumberOfEndpoints wrong (%d)", iNumberOfEndpoints));
       
  2242 	}
       
  2243 
       
  2244 
       
  2245 DPlatChunkHw* DLddUsbcChannel::ReAllocate(TInt aBuffersize, DPlatChunkHw* aHwChunk, TUint32 aCacheAttribs)
       
  2246 	{
       
  2247 	DPlatChunkHw* chunk = aHwChunk;
       
  2248 	if ((!chunk) || (chunk->iSize < aBuffersize))
       
  2249 		{
       
  2250 		if (chunk)
       
  2251 			{
       
  2252 			ClosePhysicalChunk(chunk);
       
  2253 			}
       
  2254 		//__KTRACE_OPT(KUSB, Kern::Printf("ReAllocate need to get new chunk"));
       
  2255 		chunk = Allocate(aBuffersize, aCacheAttribs);
       
  2256 		}
       
  2257 	return chunk;
       
  2258 	}
       
  2259 
       
  2260 
       
  2261 DPlatChunkHw* DLddUsbcChannel::Allocate(TInt aBuffersize, TUint32 aCacheAttribs)
       
  2262 	{
       
  2263 	TUint32 physAddr = 0;
       
  2264 	TUint32 size = Kern::RoundToPageSize(aBuffersize);
       
  2265 
       
  2266 	if (Epoc::AllocPhysicalRam(size, physAddr) != KErrNone)
       
  2267 		return NULL;
       
  2268 
       
  2269 	DPlatChunkHw* HwChunk;
       
  2270 	if (DPlatChunkHw::New(HwChunk, physAddr, aBuffersize, aCacheAttribs) != KErrNone)
       
  2271 		{
       
  2272 		Epoc::FreePhysicalRam(physAddr, size);
       
  2273 		return NULL;
       
  2274 		}
       
  2275 
       
  2276 	return HwChunk;
       
  2277 	}
       
  2278 
       
  2279 
       
  2280 TInt DLddUsbcChannel::DoRxComplete(TUsbcEndpoint* aTUsbcEndpoint, TInt aEndpoint, TBool aReEntrant)
       
  2281 	{
       
  2282 	TBool completeNow;
       
  2283 	TInt err = aTUsbcEndpoint->CopyToClient(iClient, completeNow);
       
  2284 	if (completeNow)
       
  2285 		{
       
  2286 		aTUsbcEndpoint->SetClientReadPending(EFalse);
       
  2287 		Kern::RequestComplete(iClient,iRequestStatus[aEndpoint], err);
       
  2288 		}
       
  2289 	aTUsbcEndpoint->TryToStartRead(aReEntrant);
       
  2290 	return err;
       
  2291 	}
       
  2292 
       
  2293 
       
  2294 void DLddUsbcChannel::DoRxCompleteNow(TUsbcEndpoint* aTUsbcEndpoint, TInt aEndpoint)
       
  2295 	{
       
  2296 	aTUsbcEndpoint->SetClientReadPending(EFalse);
       
  2297 	Kern::RequestComplete(iClient,iRequestStatus[aEndpoint], KErrCancel);
       
  2298 	}
       
  2299 
       
  2300 
       
  2301 void DLddUsbcChannel::DoTxComplete(TUsbcEndpoint* aTUsbcEndpoint, TInt aEndpoint, TInt aError)
       
  2302 	{
       
  2303 	aTUsbcEndpoint->SetClientWritePending(EFalse);
       
  2304 	Kern::RequestComplete(iClient,iRequestStatus[aEndpoint], aError);
       
  2305 	}
       
  2306 
       
  2307 
       
  2308 TBool DLddUsbcChannel::AlternateDeviceStateTestComplete()
       
  2309 	{
       
  2310 	TBool completeNow = EFalse;
       
  2311 	const TInt reqNo = (TInt) RDevUsbcClient::ERequestAlternateDeviceStatusNotify;
       
  2312 	if (iRequestStatus[reqNo])
       
  2313 		{
       
  2314 		// User req is outstanding
       
  2315 		TUint32 deviceState;
       
  2316 		if (iStatusFifo->GetDeviceQueuedStatus(deviceState) == KErrNone)
       
  2317 			{
       
  2318 			// Device state waiting to be sent userside
       
  2319 			completeNow = ETrue;
       
  2320 			//__KTRACE_OPT(KUSB, Kern::Printf("StatusChangeCallback Notify status"));
       
  2321 			// set client descriptor length to zero
       
  2322 			TInt r = Kern::ThreadRawWrite(iClient, iStatusChangePtr, &deviceState,
       
  2323 										  sizeof(TUint32), iClient);
       
  2324 			if (r != KErrNone)
       
  2325 				PanicClientThread(r);
       
  2326 			iStatusChangePtr = NULL;
       
  2327 			}
       
  2328 		}
       
  2329 	return completeNow;
       
  2330 	}
       
  2331 
       
  2332 
       
  2333 void DLddUsbcChannel::EmergencyCompleteDfc(TAny* aDLddUsbcChannel)
       
  2334 	{
       
  2335 	((DLddUsbcChannel*) aDLddUsbcChannel)->DoEmergencyComplete();
       
  2336 	}
       
  2337 
       
  2338 
       
  2339 void DLddUsbcChannel::DeConfigure(TInt aErrorCode)
       
  2340 	{
       
  2341 	//__KTRACE_OPT(KUSB, Kern::Printf("DLddUsbcChannel::DeConfigure()"));
       
  2342 	// Called after deconfiguration. Cancels transfers on all endpoints.
       
  2343 	ResetInterface(aErrorCode);
       
  2344 	// Cancel the endpoint status notify request if it is outstanding.
       
  2345 	const TInt KEpNotReq = RDevUsbcClient::ERequestEndpointStatusNotify;
       
  2346 	if (iRequestStatus[KEpNotReq])
       
  2347 		{
       
  2348 		CancelNotifyEndpointStatus();
       
  2349 		Kern::RequestComplete(iClient, iRequestStatus[KEpNotReq], aErrorCode);
       
  2350 		}
       
  2351 	// We have to reset the alternate setting number when the config goes away.
       
  2352  	SelectAlternateSetting(0);
       
  2353 	iAlternateSetting = 0;
       
  2354 	}
       
  2355 
       
  2356 
       
  2357 void DLddUsbcChannel::StartEpReads()
       
  2358 	{
       
  2359 	// Queued after enumeration. Starts reads on all endpoints.
       
  2360 	// The endpoint itself decides if it can do a read
       
  2361 	TInt i;
       
  2362 	for (i = 0; i <= iNumberOfEndpoints; i++)
       
  2363 		{
       
  2364 		// The endpoint itself will decide if it can read
       
  2365 		iEndpoint[i]->TryToStartRead(EFalse);
       
  2366 		}
       
  2367 	}
       
  2368 
       
  2369 
       
  2370 void DLddUsbcChannel::ResetInterface(TInt aErrorCode)
       
  2371 	{
       
  2372 	// Called after change in alternate setting.  Cancels transfers on all endpoints
       
  2373 	if (iValidInterface || iOwnsDeviceControl)
       
  2374 		{
       
  2375 		for (TInt i = 0; i <= iNumberOfEndpoints; i++)
       
  2376 			{
       
  2377 			//__KTRACE_OPT(KUSB, Kern::Printf("Cancelling transfer ep=%d", i));
       
  2378 			iEndpoint[i]->CancelTransfer(iClient);			// Copies data userside
       
  2379 			iEndpoint[i]->AbortTransfer();					// kills any ldd->pil outstanding transfers
       
  2380 			iEndpoint[i]->iDmaBuffers->Flush();
       
  2381 			if (iRequestStatus[i] != NULL)
       
  2382 				Kern::RequestComplete(iClient, iRequestStatus[i], aErrorCode);
       
  2383 			iEndpoint[i]->SetClientWritePending(EFalse);
       
  2384 			iEndpoint[i]->SetClientReadPending(EFalse);
       
  2385 			}
       
  2386 		}
       
  2387 	}
       
  2388 
       
  2389 
       
  2390 void DLddUsbcChannel::AbortInterface()
       
  2391 	{
       
  2392 	// Called after when channel is closing
       
  2393 	if (iValidInterface || iOwnsDeviceControl)
       
  2394 		{
       
  2395 		for (TInt i = 0; i <= iNumberOfEndpoints; i++)
       
  2396 			{
       
  2397 			iEndpoint[i]->AbortTransfer();					// kills any ldd->pil outstanding transfers
       
  2398 			}
       
  2399 		}
       
  2400 	}
       
  2401 
       
  2402 
       
  2403 void DLddUsbcChannel::ClosePhysicalChunk(DPlatChunkHw*& aHwChunk)
       
  2404 	{
       
  2405 	if (aHwChunk)
       
  2406 		{
       
  2407  		const TPhysAddr addr = aHwChunk->PhysicalAddress();
       
  2408  		const TInt size = aHwChunk->iSize;
       
  2409 		aHwChunk->Close(NULL);
       
  2410  		Epoc::FreePhysicalRam(addr, size);
       
  2411 		}
       
  2412 	aHwChunk = NULL;
       
  2413 	}
       
  2414 
       
  2415 
       
  2416 TInt DLddUsbcChannel::DoEmergencyComplete()
       
  2417 	{
       
  2418 	//__KTRACE_OPT(KUSB, Kern::Printf("TUsbcEndpoint::DoEmergencyComplete"));
       
  2419 	// cancel any pending DFCs
       
  2420 	// complete all client requests
       
  2421     for (TInt i = 0; i < KUsbcMaxRequests; i++)
       
  2422         {
       
  2423         if (iRequestStatus[i])
       
  2424             {
       
  2425             //__KTRACE_OPT(KUSB, Kern::Printf("Complete request 0x%x", iRequestStatus[i]));
       
  2426             Kern::RequestComplete(iClient, iRequestStatus[i], KErrDisconnected);
       
  2427             }
       
  2428         }
       
  2429     iStatusCallbackInfo.Cancel();
       
  2430     iEndpointStatusCallbackInfo.Cancel();
       
  2431     iOtgFeatureCallbackInfo.Cancel();
       
  2432 	return KErrNone;
       
  2433 	}
       
  2434 
       
  2435 
       
  2436 void DLddUsbcChannel::PanicClientThread(TInt aReason)
       
  2437 	{
       
  2438 	Kern::ThreadKill(iClient, EExitPanic, aReason, KUsbLDDKillCat);
       
  2439 	}
       
  2440 
       
  2441 
       
  2442 // ===============Endpoint====================
       
  2443 
       
  2444 // Constructor
       
  2445 TUsbcEndpoint::TUsbcEndpoint(DLddUsbcChannel* aLDD, DUsbClientController* aController,
       
  2446 							 const TUsbcEndpointInfo* aEndpointInfo, TInt aEndpointNum,
       
  2447 							 TInt aBandwidthPriority)
       
  2448 	: iController(aController),
       
  2449 	  iEndpointInfo(aEndpointInfo->iType, aEndpointInfo->iDir, aEndpointInfo->iSize),
       
  2450 	  iClientReadPending(EFalse),
       
  2451 	  iClientWritePending(EFalse),
       
  2452 	  iEndpointNumber(aEndpointNum),
       
  2453 	  iRealEpNumber(-1),
       
  2454 	  iLdd(aLDD),
       
  2455 	  iError(KErrNone),
       
  2456 	  iRequestCallbackInfo(NULL),
       
  2457 	  iBytesTransferred(0),
       
  2458 	  iBandwidthPriority(aBandwidthPriority)
       
  2459 	{
       
  2460  	ResetTransferInfo();
       
  2461 	//__KTRACE_OPT(KUSB, Kern::Printf("TUsbcEndpoint::TUsbcEndpoint 2"));
       
  2462 	}
       
  2463 
       
  2464 
       
  2465 TInt TUsbcEndpoint::Construct()
       
  2466 	{
       
  2467 	iDmaBuffers = new TDmaBuf(&iEndpointInfo, iBandwidthPriority);
       
  2468 	if (iDmaBuffers == NULL)
       
  2469 		{
       
  2470 		return KErrNoMemory;
       
  2471 		}
       
  2472 	const TInt r = iDmaBuffers->Construct(&iEndpointInfo);
       
  2473 	if (r != KErrNone)
       
  2474 		{
       
  2475 		return r;
       
  2476 		}
       
  2477 	iRequestCallbackInfo = new TUsbcRequestCallback(iLdd,
       
  2478 													iEndpointNumber,
       
  2479 													TUsbcEndpoint::RequestCallback,
       
  2480 													this,
       
  2481 													iLdd->iDfcQ,
       
  2482 													KUsbRequestCallbackPriority);
       
  2483 	if (iRequestCallbackInfo == NULL)
       
  2484 		{
       
  2485 		return KErrNoMemory;
       
  2486 		}
       
  2487 	return KErrNone;
       
  2488 	}
       
  2489 
       
  2490 
       
  2491 TUsbcEndpoint::~TUsbcEndpoint()
       
  2492 	{
       
  2493 	//__KTRACE_OPT(KUSB, Kern::Printf("TUsbcEndpoint::~TUsbcEndpoint(%d)", iEndpointNumber));
       
  2494 	AbortTransfer();
       
  2495 	delete iRequestCallbackInfo;
       
  2496 	delete iDmaBuffers;
       
  2497 	}
       
  2498 
       
  2499 
       
  2500 void TUsbcEndpoint::RequestCallback(TAny* aTUsbcEndpoint)
       
  2501 	{
       
  2502 	//__KTRACE_OPT(KUSB, Kern::Printf("TUsbcEndpoint::RequestCallback"));
       
  2503 	((TUsbcEndpoint*) aTUsbcEndpoint)->EndpointComplete();
       
  2504 	}
       
  2505 
       
  2506 
       
  2507 void TUsbcEndpoint::SetMaxPacketSize(TInt aSize)
       
  2508 	{
       
  2509 	iEndpointInfo.iSize = aSize;
       
  2510 	iDmaBuffers->SetMaxPacketSize(aSize);
       
  2511 	}
       
  2512 
       
  2513 
       
  2514 TInt TUsbcEndpoint::EndpointComplete()
       
  2515 	{
       
  2516 	//__KTRACE_OPT(KUSB, Kern::Printf("TUsbcEndpoint::EndpointComplete ep=%d %d",
       
  2517 								//	iEndpointNumber, iRequestCallbackInfo->iEndpointNum));
       
  2518 
       
  2519 	if (iLdd->ChannelClosing())
       
  2520 		{
       
  2521 		//__KTRACE_OPT(KUSB, Kern::Printf("We're going home -> completions no longer accepted"));
       
  2522 		return KErrNone;
       
  2523 		}
       
  2524 
       
  2525 	TTransferDirection transferDir = iRequestCallbackInfo->iTransferDir;
       
  2526 	TInt error = iRequestCallbackInfo->iError;
       
  2527 
       
  2528 	switch (transferDir)
       
  2529 		{
       
  2530 
       
  2531 	case EControllerWrite:
       
  2532 		{
       
  2533 		//__KTRACE_OPT(KUSB, Kern::Printf("TUsbcEndpoint::EndpointComplete Write 2"));
       
  2534 		if (!iDmaBuffers->TxIsActive())
       
  2535 			{
       
  2536 			//__KTRACE_OPT(KUSB, Kern::Printf("  TX completion but !iDmaBuffers->TxIsActive()"));
       
  2537 			break;
       
  2538 			}
       
  2539 
       
  2540 		iDmaBuffers->TxSetInActive();
       
  2541 		TBool completeNow = EFalse;
       
  2542 		iBytesTransferred += iRequestCallbackInfo->iTxBytes;
       
  2543 		if (iClientWritePending)
       
  2544 			{
       
  2545 			//Complete Outstanding Write if necessary
       
  2546 			iError = error;
       
  2547 			if (iError != KErrNone)
       
  2548 				{
       
  2549 				completeNow = ETrue;
       
  2550 				if (iError == KErrPrematureEnd)				// Previous write could not be completed
       
  2551 					iError = KErrNone;
       
  2552 				}
       
  2553 			else
       
  2554 				{
       
  2555 				if (iBytesTransferred == (TUint32) iTransferInfo.iTransferSize)
       
  2556 					{
       
  2557 					completeNow = ETrue;
       
  2558 					}
       
  2559 				else
       
  2560 					{
       
  2561 					iError = ContinueWrite();
       
  2562 					if (iError != KErrNone)
       
  2563 						completeNow = ETrue;
       
  2564 					}
       
  2565 				}
       
  2566 			if (completeNow)
       
  2567 				{
       
  2568 				TxComplete();
       
  2569 				ResetTransferInfo();
       
  2570 				if (iEndpointNumber == 0)
       
  2571 					{
       
  2572 					iDmaBuffers->Flush();
       
  2573 					TryToStartRead(EFalse);
       
  2574 					}
       
  2575 				}
       
  2576 			}
       
  2577 		break;
       
  2578 		}
       
  2579 
       
  2580 	case EControllerRead:
       
  2581 		{
       
  2582 		// The first packet always contains the total #of bytes
       
  2583 		const TInt byteCount = iRequestCallbackInfo->iPacketSize[0];
       
  2584 		const TInt packetCount = iRequestCallbackInfo->iRxPackets;
       
  2585 		iDmaBuffers->ReadXferComplete(byteCount, packetCount, error);
       
  2586 
       
  2587 		// We queue the dfc if we can complete the read, i.e. if we are reading a packet,
       
  2588 		// or if we have enough data to satisfy a read data request.
       
  2589 		if (iClientReadPending)
       
  2590 			{
       
  2591 			//Complete outstanding read
       
  2592 			//__KTRACE_OPT(KUSB, Kern::Printf("TUsbcEndpoint::EndpointComplete Read 3 (bytes "
       
  2593 										//	"available=%d)", iDmaBuffers->RxBytesAvailable()));
       
  2594 			TInt bytesReqd = iTransferInfo.iTransferSize - iBytesTransferred;
       
  2595 			TBool completeNow = EFalse;
       
  2596 
       
  2597 			if (iTransferInfo.iTransferType == ETransferTypeReadPacket ||
       
  2598 				iTransferInfo.iTransferType == ETransferTypeReadOneOrMore)
       
  2599 				{
       
  2600 				// Always complete on a packet read
       
  2601 				completeNow = ETrue;
       
  2602 				}
       
  2603 			else if (iTransferInfo.iTransferType == ETransferTypeReadData)
       
  2604 				{
       
  2605 				// Complete only if enough data is present
       
  2606 				if (iDmaBuffers->RxBytesAvailable() >= bytesReqd)
       
  2607 					completeNow = ETrue;
       
  2608 				}
       
  2609 			else if (iTransferInfo.iTransferType == ETransferTypeReadUntilShort)
       
  2610 				{
       
  2611 				// Complete if enough data is present or if a short packet has been delivered
       
  2612 				const TInt maxPacketSize = iEndpointInfo.iSize;
       
  2613 				const TInt lastPacketSize = iRequestCallbackInfo->iPacketSize[packetCount - 1];
       
  2614 				if (lastPacketSize < maxPacketSize)
       
  2615 					completeNow = ETrue;
       
  2616 				else if (iDmaBuffers->RxBytesAvailable() >= bytesReqd)
       
  2617 					completeNow = ETrue;
       
  2618 				else
       
  2619 					{
       
  2620 					const TUint type = iEndpointInfo.iType;
       
  2621 					if ((type == KUsbEpTypeBulk) && (lastPacketSize & (maxPacketSize - 1)))
       
  2622 						{
       
  2623 						completeNow = ETrue;
       
  2624 						}
       
  2625 					else if ((type != KUsbEpTypeBulk) &&
       
  2626 							 (lastPacketSize > maxPacketSize) &&
       
  2627 							 (lastPacketSize % maxPacketSize))
       
  2628 						{
       
  2629 						completeNow = ETrue;
       
  2630 						}
       
  2631 					}
       
  2632 				}
       
  2633 			if (completeNow)
       
  2634 				{
       
  2635 				iError = error;
       
  2636 				RxComplete(EFalse);
       
  2637 				iClientReadPending = EFalse;
       
  2638 				}
       
  2639 			}
       
  2640 		iDmaBuffers->RxSetInActive();
       
  2641 		if (error != KErrNone)
       
  2642 			{
       
  2643 			return error;
       
  2644 			}
       
  2645 		if (TryToStartRead(EFalse) != KErrNone)
       
  2646 			{
       
  2647 //			if (iEndpointNumber != 0)
       
  2648 //				Kern::Printf("EndpointComplete couldn't start read on ep=%d", iEndpointNumber);
       
  2649 			}
       
  2650 		break;
       
  2651 		}
       
  2652 
       
  2653 	default:
       
  2654 		// shouldn't get here
       
  2655 		break;
       
  2656 		}
       
  2657 
       
  2658 	return KErrNone;
       
  2659 	}
       
  2660 
       
  2661 
       
  2662 void TUsbcEndpoint::TxComplete()
       
  2663 	{
       
  2664 	iLdd->DoTxComplete(this, iEndpointNumber, iError);
       
  2665 	}
       
  2666 
       
  2667 
       
  2668 TInt TUsbcEndpoint::RxComplete(TBool aReEntrant)
       
  2669 	{
       
  2670 	return iLdd->DoRxComplete(this, iEndpointNumber, aReEntrant);
       
  2671 	}
       
  2672 
       
  2673 
       
  2674 void TUsbcEndpoint::RxCompleteNow()
       
  2675 	{
       
  2676 	iLdd->DoRxCompleteNow(this, iEndpointNumber);
       
  2677 	}
       
  2678 
       
  2679 
       
  2680 TInt TUsbcEndpoint::CopyToClient(DThread* aClient)
       
  2681 	{
       
  2682 	TBool completeNow;
       
  2683 	return CopyToClient(aClient, completeNow);
       
  2684 	}
       
  2685 
       
  2686 
       
  2687 TInt TUsbcEndpoint::CopyToClient(DThread* aClient, TBool& aCompleteNow)
       
  2688 	{
       
  2689 	TInt err;
       
  2690 	TAny* des = iTransferInfo.iDes;
       
  2691 	const TInt length = iTransferInfo.iTransferSize;
       
  2692 	const TBool KReadData = EFalse;
       
  2693 	const TBool KReadUntilShort = ETrue;
       
  2694 
       
  2695 	if (iTransferInfo.iTransferType == ETransferTypeReadPacket)
       
  2696 		{
       
  2697 		err = iDmaBuffers->RxCopyPacketToClient(aClient, des, length);
       
  2698 		aCompleteNow = ETrue;
       
  2699 		}
       
  2700 	else if (iTransferInfo.iTransferType == ETransferTypeReadOneOrMore)
       
  2701 		{
       
  2702 		err = iDmaBuffers->RxCopyDataToClient(aClient, des, length, iBytesTransferred,
       
  2703 											  KReadData, aCompleteNow);
       
  2704 		aCompleteNow = ETrue;
       
  2705 		}
       
  2706 	else if (iTransferInfo.iTransferType == ETransferTypeReadUntilShort)
       
  2707 		{
       
  2708 		err = iDmaBuffers->RxCopyDataToClient(aClient, des, length, iBytesTransferred,
       
  2709 											  KReadUntilShort, aCompleteNow);
       
  2710 		}
       
  2711 	else
       
  2712 		{
       
  2713 		err = iDmaBuffers->RxCopyDataToClient(aClient, des, length, iBytesTransferred,
       
  2714 											  KReadData, aCompleteNow);
       
  2715 		}
       
  2716 
       
  2717 	if (aCompleteNow)
       
  2718 		{
       
  2719 		ResetTransferInfo();
       
  2720 		SetClientReadPending(EFalse);
       
  2721 		}
       
  2722 
       
  2723 	return err;
       
  2724 	}
       
  2725 
       
  2726 
       
  2727 TInt TUsbcEndpoint::TryToStartRead(TBool aReEntrant)
       
  2728 	{
       
  2729 	//__KTRACE_OPT(KUSB, Kern::Printf("TryToStartRead 1 ep=%d", iEndpointNumber));
       
  2730 	TInt r = KErrNone;
       
  2731 	if (iEndpointInfo.iDir != KUsbEpDirOut &&
       
  2732 		iEndpointInfo.iDir != KUsbEpDirBidirect)
       
  2733 		{
       
  2734 		// Verify ep direction
       
  2735 		//__KTRACE_OPT(KUSB, Kern::Printf("TryToStartRead wrong direction ep=%d", iEndpointNumber));
       
  2736 		return KErrUsbEpBadDirection;
       
  2737 		}
       
  2738 
       
  2739 	if (iEndpointNumber == 0)
       
  2740 		{
       
  2741 		// Can't issue an Ep0 read if reader or writer is active
       
  2742 		if (iDmaBuffers->TxIsActive())
       
  2743 			{
       
  2744 			//__KTRACE_OPT(KUSB, Kern::Printf("TryToStartRead ep0 Tx already active FATAL"));
       
  2745 			return KErrUsbEpNotReady;
       
  2746 			}
       
  2747 		if (iDmaBuffers->RxIsActive())
       
  2748 			{
       
  2749 			//__KTRACE_OPT(KUSB, Kern::Printf("TryToStartRead ep0 Rx already active non-FATAL"));
       
  2750 			}
       
  2751 		}
       
  2752 
       
  2753 	if (!(iDmaBuffers->RxIsActive()))
       
  2754 		{
       
  2755 		TUint8* bufferAddr;
       
  2756 		TPhysAddr physAddr;
       
  2757 		TUsbcPacketArray* indexArray;
       
  2758 		TUsbcPacketArray* sizeArray;
       
  2759 		TInt length;
       
  2760 		r = iDmaBuffers->RxGetNextXfer(bufferAddr, indexArray, sizeArray, length, physAddr);
       
  2761 		if (r == KErrNone)
       
  2762 			{
       
  2763 			iDmaBuffers->RxSetActive();
       
  2764 			iRequestCallbackInfo->SetRxBufferInfo(bufferAddr, physAddr, indexArray, sizeArray, length);
       
  2765 
       
  2766 			//__KTRACE_OPT(KUSB, Kern::Printf("TryToStartRead 2 bufferAddr=0x%08x", bufferAddr));
       
  2767 
       
  2768 			r = iController->SetupReadBuffer(*iRequestCallbackInfo);
       
  2769 			if (r != KErrNone)
       
  2770 				{
       
  2771 				iDmaBuffers->RxSetInActive();
       
  2772 				//__KTRACE_OPT(KPANIC, Kern::Printf("  Error: TryToStartRead controller rejects read"));
       
  2773 				}
       
  2774 			}
       
  2775 		else
       
  2776 			{
       
  2777 			if (iClientReadPending)
       
  2778 				{
       
  2779 				// Deadlock, try to resolve it by draining buffer into descriptor
       
  2780 				if (!aReEntrant)
       
  2781 					{
       
  2782 					RxComplete(ETrue);
       
  2783 					}
       
  2784 				else
       
  2785 					{
       
  2786 					// we are stuck, better complete userside otherwise the userside request will hang
       
  2787 					RxCompleteNow();
       
  2788 					}
       
  2789 				}
       
  2790 			}
       
  2791 		}
       
  2792 	return r;
       
  2793 	}
       
  2794 
       
  2795 
       
  2796 TInt TUsbcEndpoint::TryToStartWrite(TEndpointTransferInfo* pTfr)
       
  2797 	{
       
  2798 	//__KTRACE_OPT(KUSB, Kern::Printf("TryToStartWrite 1 ep=%d", iEndpointNumber));
       
  2799 	if (iEndpointInfo.iDir != KUsbEpDirIn &&
       
  2800 		iEndpointInfo.iDir != KUsbEpDirBidirect)
       
  2801 		{
       
  2802 		// Verify ep direction
       
  2803 		return KErrUsbEpBadDirection;
       
  2804 		}
       
  2805 	if (iEndpointNumber == 0)
       
  2806 		{
       
  2807 		// Can't issue an Ep0 write if unread data is available or writer is active
       
  2808 		if (iDmaBuffers->TxIsActive() || !iDmaBuffers->IsReaderEmpty())
       
  2809 			{
       
  2810 			return KErrUsbEpNotReady;
       
  2811 			}
       
  2812 		if (iDmaBuffers->RxIsActive())
       
  2813 			{
       
  2814 			// if a reader is active then cancel the read
       
  2815 			iDmaBuffers->RxSetInActive();
       
  2816 			iController->CancelReadBuffer(iLdd, iRealEpNumber);
       
  2817 			}
       
  2818 		}
       
  2819 	SetTransferInfo(pTfr);
       
  2820 	ContinueWrite();
       
  2821 	return KErrNone;
       
  2822 	}
       
  2823 
       
  2824 
       
  2825 TInt TUsbcEndpoint::ContinueWrite()
       
  2826 	{
       
  2827 	//__KTRACE_OPT(KUSB, Kern::Printf("ContinueWrite 2"));
       
  2828 	TUint8* bufferAddr;
       
  2829 	TPhysAddr physAddr;
       
  2830 	TInt bufferLength;
       
  2831 	TInt r = iDmaBuffers->TxGetNextXfer(bufferAddr, bufferLength, physAddr);
       
  2832 	if (r != KErrNone)											// probably already active
       
  2833 		return r;
       
  2834 	//__KTRACE_OPT(KUSB, Kern::Printf("ContinueWrite 3"));
       
  2835 	iDmaBuffers->TxSetActive();
       
  2836 	TBool zlpReqd = EFalse;
       
  2837 	TUint32 transferSize = iTransferInfo.iTransferSize;
       
  2838 	TAny* logicalSrc = iTransferInfo.iDes;
       
  2839 	TInt length = Min(transferSize - iBytesTransferred, (TUint32) bufferLength);
       
  2840 	if (iBytesTransferred+length>=transferSize)
       
  2841 		{
       
  2842 		// only send a zlp if this is the last buffer of the transfer
       
  2843 		zlpReqd = iTransferInfo.iZlpReqd;
       
  2844 		}
       
  2845 	r = iDmaBuffers->TxStoreData(iLdd->Client(), logicalSrc, length, iBytesTransferred);
       
  2846 	iDmaBuffers->TxSetActive();
       
  2847 	iRequestCallbackInfo->SetTxBufferInfo(bufferAddr, physAddr, length);
       
  2848 	iRequestCallbackInfo->iZlpReqd = zlpReqd;
       
  2849 #if 0
       
  2850 	for (TInt i = 0; i < iRequestCallbackInfo->iLength; i++)
       
  2851 		{
       
  2852 		//__KTRACE_OPT(KUSB, Kern::Printf("Buffer[%d] = 0x%02x", i, iRequestCallbackInfo->iBufferStart[i]));
       
  2853 		}
       
  2854 #endif
       
  2855 	r = iController->SetupWriteBuffer(*iRequestCallbackInfo);
       
  2856 	return r;
       
  2857 	}
       
  2858 
       
  2859 
       
  2860 void TUsbcEndpoint::CancelTransfer(DThread* aThread)
       
  2861 	{
       
  2862 	//__KTRACE_OPT(KUSB, Kern::Printf("CancelTransfer"));
       
  2863 	if (iDmaBuffers != NULL)
       
  2864 		{
       
  2865 		if (iClientWritePending)
       
  2866 			{
       
  2867 			//__KTRACE_OPT(KUSB, Kern::Printf("  (iClientWritePending)"));
       
  2868 			iClientWritePending = EFalse;
       
  2869 			iController->CancelWriteBuffer(iLdd, iRealEpNumber);
       
  2870 			iDmaBuffers->TxSetInActive();
       
  2871 			}
       
  2872 		if (iClientReadPending)
       
  2873 			{
       
  2874 			//__KTRACE_OPT(KUSB, Kern::Printf("  (iClientReadPending)"));
       
  2875 			iClientReadPending = EFalse;
       
  2876 			CopyToClient(aThread);
       
  2877 			}
       
  2878 		}
       
  2879 	}
       
  2880 
       
  2881 
       
  2882 void TUsbcEndpoint::AbortTransfer()
       
  2883 	{
       
  2884 	//__KTRACE_OPT(KUSB, Kern::Printf("Abort Transfer"));
       
  2885 	if (iDmaBuffers != NULL)
       
  2886 		{
       
  2887 		if (iDmaBuffers->TxIsActive())
       
  2888 			{
       
  2889 			//__KTRACE_OPT(KUSB, Kern::Printf("  (iClientWritePending)"));
       
  2890 			iController->CancelWriteBuffer(iLdd, iRealEpNumber);
       
  2891 			iDmaBuffers->TxSetInActive();
       
  2892 			}
       
  2893 		if (iDmaBuffers->RxIsActive())
       
  2894 			{
       
  2895 			//__KTRACE_OPT(KUSB, Kern::Printf("  (iClientReadPending)"));
       
  2896 			iController->CancelReadBuffer(iLdd, iRealEpNumber);
       
  2897 			iDmaBuffers->RxSetInActive();
       
  2898 			}
       
  2899 		iRequestCallbackInfo->iDfc.Cancel();
       
  2900 		}
       
  2901 	}
       
  2902 
       
  2903 
       
  2904 TUsbcClientCallback::TUsbcClientCallback(DBase* aOwner, TDfcFn aCallback, TInt aPriority)
       
  2905 	: iOwner(aOwner), iDfc(aCallback, aOwner, aPriority)
       
  2906 	{
       
  2907 	}
       
  2908 
       
  2909 
       
  2910 TUsbcRequestCallback::TUsbcRequestCallback(const DBase* aOwner, TInt aEndpointNum, TDfcFn aDfcFunc,
       
  2911 										   TUsbcEndpoint* aEndpoint, TDfcQue* aDfcQ, TInt aPriority)
       
  2912 	: iEndpointNum(aEndpointNum),
       
  2913 	  iRealEpNum(-1),
       
  2914 	  iOwner(aOwner),
       
  2915 	  iDfc(aDfcFunc, aEndpoint, aDfcQ, aPriority),
       
  2916 	  iTransferDir(EControllerNone),
       
  2917 	  iBufferStart(NULL),
       
  2918 	  iPacketIndex(NULL),									// actually TUint16 (*)[]
       
  2919 	  iPacketSize(NULL),									// actually TUint16 (*)[]
       
  2920 	  iLength(0),
       
  2921 	  iZlpReqd(EFalse),
       
  2922 	  iTxBytes(0),
       
  2923 	  iRxPackets(0),
       
  2924 	  iError(KErrNone)
       
  2925 	{
       
  2926 	}
       
  2927 
       
  2928 
       
  2929 TUsbcRequestCallback::~TUsbcRequestCallback()
       
  2930 	{
       
  2931 	//__KTRACE_OPT(KUSB, Kern::Printf("TUsbcRequestCallback::~TUsbcRequestCallback()"));
       
  2932 	iDfc.Cancel();
       
  2933 	}
       
  2934 
       
  2935 
       
  2936 TUsbcStatusCallback::TUsbcStatusCallback(DBase* aOwner, TDfcFn aCallback, TInt aPriority)
       
  2937 	: iOwner(aOwner), iDfc(aCallback, aOwner, aPriority)
       
  2938 	{
       
  2939  	ResetState();
       
  2940 	}
       
  2941 
       
  2942 
       
  2943 TUsbcEndpointStatusCallback::TUsbcEndpointStatusCallback(DBase* aOwner, TDfcFn aCallback, TInt aPriority)
       
  2944 	: iOwner(aOwner),
       
  2945 	  iDfc(aCallback, aOwner, aPriority),
       
  2946 	  iState(0)
       
  2947 	{
       
  2948 	}
       
  2949 
       
  2950 
       
  2951 TUsbcOtgFeatureCallback::TUsbcOtgFeatureCallback(DBase* aOwner, TDfcFn aCallback, TInt aPriority)
       
  2952 	: iOwner(aOwner),
       
  2953 	  iDfc(aCallback, aOwner, aPriority),
       
  2954 	  iValue(0)
       
  2955 	{
       
  2956 	}
       
  2957 
       
  2958 
       
  2959 TUsbcAlternateSettingList::TUsbcAlternateSettingList()
       
  2960 	: iNext(NULL),
       
  2961 	  iNumberOfEndpoints(0),
       
  2962 	  iSetting(0)
       
  2963 	{
       
  2964 	for (TInt i = 0; i <= KMaxEndpointsPerClient; i++)
       
  2965 		{
       
  2966 		iEndpoint[i] = NULL;
       
  2967 		}
       
  2968 	}
       
  2969 
       
  2970 
       
  2971 TUsbcAlternateSettingList::~TUsbcAlternateSettingList()
       
  2972 	{
       
  2973 	//__KTRACE_OPT(KUSB, Kern::Printf("TUsbcAlternateSettingList::~TUsbcAlternateSettingList()"));
       
  2974 	for (TInt i = 0; i <= KMaxEndpointsPerClient; i++)
       
  2975 		{
       
  2976 		delete iEndpoint[i];
       
  2977 		}
       
  2978 	}
       
  2979 
       
  2980 
       
  2981 TUsbcDeviceStatusQueue::TUsbcDeviceStatusQueue()
       
  2982 	{
       
  2983 	FlushQueue();
       
  2984 	}
       
  2985 
       
  2986 
       
  2987 void TUsbcDeviceStatusQueue::FlushQueue()
       
  2988 	{
       
  2989 	for (TInt i = 0; i < KUsbDeviceStatusQueueDepth; i++)
       
  2990 		{
       
  2991 		iDeviceStatusQueue[i] = KUsbDeviceStatusNull;
       
  2992 		}
       
  2993 	iStatusQueueHead = 0;
       
  2994 	}
       
  2995 
       
  2996 
       
  2997 void TUsbcDeviceStatusQueue::AddStatusToQueue(TUint32 aDeviceStatus)
       
  2998 	{
       
  2999 	// Only add a new status if it is not a duplicate of the one at the head of the queue
       
  3000 	if (!(iStatusQueueHead != 0 &&
       
  3001 		  iDeviceStatusQueue[iStatusQueueHead - 1] == aDeviceStatus))
       
  3002 		{
       
  3003 		if (iStatusQueueHead == KUsbDeviceStatusQueueDepth)
       
  3004 			{
       
  3005 			// Discard item at tail of queue
       
  3006 			TUint32 status;
       
  3007 			GetDeviceQueuedStatus(status);
       
  3008 			}
       
  3009 		iDeviceStatusQueue[iStatusQueueHead] = aDeviceStatus;
       
  3010 		iStatusQueueHead++;
       
  3011 		}
       
  3012 	}
       
  3013 
       
  3014 
       
  3015 TInt TUsbcDeviceStatusQueue::GetDeviceQueuedStatus(TUint32& aDeviceStatus)
       
  3016 	{
       
  3017 	TInt r = KErrNone;
       
  3018 	if (iStatusQueueHead <= 0)
       
  3019 		{
       
  3020 		r = KErrGeneral;
       
  3021 		aDeviceStatus = KUsbDeviceStatusNull;
       
  3022 		}
       
  3023 	else
       
  3024 		{
       
  3025 		aDeviceStatus = iDeviceStatusQueue[0];
       
  3026 		for(TInt i = 1; i < KUsbDeviceStatusQueueDepth; i++)
       
  3027 			{
       
  3028 			TUint32 s = iDeviceStatusQueue[i];
       
  3029 			iDeviceStatusQueue[i - 1] = s;
       
  3030 			}
       
  3031 		iStatusQueueHead--;
       
  3032 		iDeviceStatusQueue[KUsbDeviceStatusQueueDepth - 1] = KUsbDeviceStatusNull;
       
  3033 		}
       
  3034 	return r;
       
  3035 	}
       
  3036 
       
  3037 
       
  3038 //---