os/kernelhwsrv/bsptemplate/asspandvariant/template_assp/pa_usbc.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // template\template_assp\pa_usbc.cpp
    15 // Platform-dependent USB client controller layer (USB PSL).
    16 // 
    17 //
    18 
    19 
    20 #include <template_assp.h>									// /assp/template_assp/
    21 #include <template_assp_priv.h>								// /assp/template_assp/
    22 
    23 #include <drivers/usbc.h>
    24 
    25 #include "pa_usbc.h"										// .
    26 
    27 // Debug support
    28 #ifdef _DEBUG
    29 static const char KUsbPanicCat[] = "USB PSL";
    30 #endif
    31 
    32 
    33 // Define USB_SUPPORTS_PREMATURE_STATUS_IN to enable proper handling of a premature STATUS_IN stage, i.e. a
    34 // situation where the host sends less data than first announced and instead of more data (OUT) will send an
    35 // IN token to start the status stage. What we do in order to implement this here is to prime the TX fifo with
    36 // a ZLP immediately when we find out that we're dealing with a DATA_OUT request. This way, as soon as the
    37 // premature IN token is received, we complete the transaction by sending off the ZLP. If we don't prime the
    38 // TX fifo then there is no way for us to recognise a premature status because the IN token itself doesn't
    39 // raise an interrupt. We would simply wait forever for more data, or rather we would time out and the host
    40 // would move on and send the next Setup packet.
    41 // The reason why we would not want to implement the proper behaviour is this: After having primed the TX fifo
    42 // with a ZLP, it is impossible for a user to reject such a (class/vendor specific) Setup request, basically
    43 // because the successful status stage happens automatically. At the time the user has received and decoded
    44 // the Setup request there's for her no way to stall Ep0 in order to show to the host that this Setup packet
    45 // is invalid or inappropriate or whatever, because she cannot prevent the status stage from happening.
    46 // (All this is strictly true only if the amount of data in the data stage is less than or equal to Ep0's max
    47 //	packet size. However this is almost always the case.)
    48 //#define USB_SUPPORTS_PREMATURE_STATUS_IN
    49 
    50 
    51 static const TUsbcEndpointCaps DeviceEndpoints[KUsbTotalEndpoints] =
    52 	{
    53 	//                                                      Hardware #    iEndpoints index
    54 	{KEp0MaxPktSzMask,	(KUsbEpTypeControl	   | KUsbEpDirOut)}, //	 0 -  0
    55 	{KEp0MaxPktSzMask,	(KUsbEpTypeControl	   | KUsbEpDirIn )}, //	 0 -  1
    56 	{KUsbEpNotAvailable, KUsbEpNotAvailable},				// --- Not present
    57 	{KBlkMaxPktSzMask,	(KUsbEpTypeBulk		   | KUsbEpDirIn )}, //	 1 -  3
    58 	{KBlkMaxPktSzMask,	(KUsbEpTypeBulk		   | KUsbEpDirOut)}, //	 2 -  4
    59 	{KUsbEpNotAvailable, KUsbEpNotAvailable},				// --- Not present
    60 	{KUsbEpNotAvailable, KUsbEpNotAvailable},				// --- Not present
    61 	{KIsoMaxPktSzMask,	(KUsbEpTypeIsochronous | KUsbEpDirIn )}, //	 3 -  7
    62 	{KIsoMaxPktSzMask,	(KUsbEpTypeIsochronous | KUsbEpDirOut)}, //	 4 -  8
    63 	{KUsbEpNotAvailable, KUsbEpNotAvailable},				// --- Not present
    64 	{KUsbEpNotAvailable, KUsbEpNotAvailable},				// --- Not present
    65 	{KIntMaxPktSzMask,	(KUsbEpTypeInterrupt   | KUsbEpDirIn )}, //	 5 - 11
    66 	};
    67 
    68 
    69 // --- TEndpoint --------------------------------------------------------------
    70 
    71 TEndpoint::TEndpoint()
    72 //
    73 // Constructor
    74 //
    75 	: iRxBuf(NULL), iReceived(0), iLength(0), iZlpReqd(EFalse), iNoBuffer(EFalse), iDisabled(EFalse),
    76 	  iPackets(0), iLastError(KErrNone), iRequest(NULL), iRxTimer(RxTimerCallback, this),
    77 	  iRxTimerSet(EFalse), iRxMoreDataRcvd(EFalse), iPacketIndex(NULL), iPacketSize(NULL)
    78 	{
    79 	__KTRACE_OPT(KUSB, Kern::Printf("TEndpoint::TEndpoint"));
    80 	}
    81 
    82 
    83 void TEndpoint::RxTimerCallback(TAny* aPtr)
    84 //
    85 // (This function is static.)
    86 //
    87 	{
    88 	__KTRACE_OPT(KUSB, Kern::Printf("TEndpoint::RxTimerCallback"));
    89 
    90 	TEndpoint* const ep = static_cast<TEndpoint*>(aPtr);
    91 	if (!ep)
    92 		{
    93 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: !ep"));
    94 		}
    95 	else if (!ep->iRxTimerSet)
    96 		{
    97 		// Timer 'stop' substitute (instead of stopping it,
    98 		// we just let it expire after clearing iRxTimerSet)
    99 		__KTRACE_OPT(KUSB, Kern::Printf("!ep->iRxTimerSet - returning"));
   100 		}
   101 	else if (!ep->iRxBuf)
   102 		{
   103 		// Request already completed
   104 		__KTRACE_OPT(KUSB, Kern::Printf("!ep->iRxBuf - returning"));
   105 		}
   106 	else if (ep->iRxMoreDataRcvd)
   107 		{
   108 		__KTRACE_OPT(KUSB, Kern::Printf(" > rx timer cb: not yet completing..."));
   109 		ep->iRxMoreDataRcvd = EFalse;
   110 		ep->iRxTimer.Again(KRxTimerTimeout);
   111 		}
   112 	else
   113 		{
   114 		__KTRACE_OPT(KUSB, Kern::Printf(" > rx timer cb: completing now..."));
   115 		*ep->iPacketSize = ep->iReceived;
   116 		ep->iController->RxComplete(ep);
   117 		}
   118 	}
   119 
   120 
   121 // --- TTemplateAsspUsbcc public ---------------------------------------------------
   122 
   123 TTemplateAsspUsbcc::TTemplateAsspUsbcc()
   124 //
   125 // Constructor.
   126 //
   127 	: iCableConnected(ETrue), iBusIsPowered(EFalse),
   128 	  iInitialized(EFalse), iUsbClientConnectorCallback(UsbClientConnectorCallback),
   129 	  iEp0Configured(EFalse)
   130 	{
   131 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::TTemplateAsspUsbcc"));
   132 
   133 	iAssp = static_cast<TemplateAssp*>(Arch::TheAsic());
   134 
   135 	iSoftwareConnectable = iAssp->UsbSoftwareConnectable();
   136 
   137 	iCableDetectable = iAssp->UsbClientConnectorDetectable();
   138 
   139 	if (iCableDetectable)
   140 		{
   141 		// Register our callback for detecting USB cable insertion/removal.
   142 		// We ignore the error code: if the registration fails, we just won't get any events.
   143 		// (Which of course is bad enough...)
   144 		(void) iAssp->RegisterUsbClientConnectorCallback(iUsbClientConnectorCallback, this);
   145 		// Call the callback straight away so we get the proper PIL state from the beginning.
   146 		(void) UsbClientConnectorCallback(this);
   147 		}
   148 
   149 	for (TInt i = 0; i < KUsbTotalEndpoints; i++)
   150 		{
   151 		iEndpoints[i].iController = this;
   152 		}
   153 	}
   154 
   155 
   156 TInt TTemplateAsspUsbcc::Construct()
   157 //
   158 // Construct.
   159 //
   160 	{
   161 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Construct"));
   162 
   163 	TUsbcDeviceDescriptor* DeviceDesc = TUsbcDeviceDescriptor::New(
   164 		0x00,												// aDeviceClass
   165 		0x00,												// aDeviceSubClass
   166 		0x00,												// aDeviceProtocol
   167 		KEp0MaxPktSz,										// aMaxPacketSize0
   168 		KUsbVendorId,										// aVendorId
   169 		KUsbProductId,										// aProductId
   170 		KUsbDevRelease,										// aDeviceRelease
   171 		1);													// aNumConfigurations
   172 	if (!DeviceDesc)
   173 		{
   174 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Memory allocation for dev desc failed."));
   175 		return KErrGeneral;
   176 		}
   177 
   178 	TUsbcConfigDescriptor* ConfigDesc = TUsbcConfigDescriptor::New(
   179 		1,													// aConfigurationValue
   180 		ETrue,												// aSelfPowered (see 12.4.2 "Bus-Powered Devices")
   181 		ETrue,												// aRemoteWakeup
   182 		0);													// aMaxPower (mA)
   183 	if (!ConfigDesc)
   184 		{
   185 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Memory allocation for config desc failed."));
   186 		return KErrGeneral;
   187 		}
   188 
   189 	TUsbcLangIdDescriptor* StringDescLang = TUsbcLangIdDescriptor::New(KUsbLangId);
   190 	if (!StringDescLang)
   191 		{
   192 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Memory allocation for lang id $ desc failed."));
   193 		return KErrGeneral;
   194 		}
   195 
   196 	// ('sizeof(x) - 2' because 'wchar_t KStringXyz' created a wide string that ends in '\0\0'.)
   197 
   198 	TUsbcStringDescriptor* StringDescManu =
   199 		TUsbcStringDescriptor::New(TPtr8(
   200 									   const_cast<TUint8*>(reinterpret_cast<const TUint8*>(KStringManufacturer)),
   201 									   sizeof(KStringManufacturer) - 2, sizeof(KStringManufacturer) - 2));
   202 	if (!StringDescManu)
   203 		{
   204 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Memory allocation for manufacturer $ desc failed."));
   205 		return KErrGeneral;
   206 		}
   207 
   208 	TUsbcStringDescriptor* StringDescProd =
   209 		TUsbcStringDescriptor::New(TPtr8(
   210 									   const_cast<TUint8*>(reinterpret_cast<const TUint8*>(KStringProduct)),
   211 									   sizeof(KStringProduct) - 2, sizeof(KStringProduct) - 2));
   212 	if (!StringDescProd)
   213 		{
   214 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Memory allocation for product $ desc failed."));
   215 		return KErrGeneral;
   216 		}
   217 
   218 	TUsbcStringDescriptor* StringDescSer =
   219 		TUsbcStringDescriptor::New(TPtr8(
   220 									   const_cast<TUint8*>(reinterpret_cast<const TUint8*>(KStringSerialNo)),
   221 									   sizeof(KStringSerialNo) - 2, sizeof(KStringSerialNo) - 2));
   222 	if (!StringDescSer)
   223 		{
   224 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Memory allocation for serial no $ desc failed."));
   225 		return KErrGeneral;
   226 		}
   227 
   228 	TUsbcStringDescriptor* StringDescConf =
   229 		TUsbcStringDescriptor::New(TPtr8(
   230 									   const_cast<TUint8*>(reinterpret_cast<const TUint8*>(KStringConfig)),
   231 									   sizeof(KStringConfig) - 2, sizeof(KStringConfig) - 2));
   232 	if (!StringDescConf)
   233 		{
   234 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Memory allocation for config $ desc failed."));
   235 		return KErrGeneral;
   236 		}
   237 
   238 	const TBool r =	InitialiseBaseClass(DeviceDesc,
   239 										ConfigDesc,
   240 										StringDescLang,
   241 										StringDescManu,
   242 										StringDescProd,
   243 										StringDescSer,
   244 										StringDescConf);
   245 	if (!r)
   246 		{
   247 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: UsbClientController::InitialiseBaseClass failed."));
   248 		return KErrGeneral;
   249 		}
   250 
   251 	return KErrNone;
   252 	}
   253 
   254 
   255 TTemplateAsspUsbcc::~TTemplateAsspUsbcc()
   256 //
   257 // Destructor.
   258 //
   259 	{
   260 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::~TTemplateAsspUsbcc"));
   261 
   262 	// Unregister our callback for detecting USB cable insertion/removal
   263 	if (iCableDetectable)
   264 		{
   265 		iAssp->UnregisterUsbClientConnectorCallback();
   266 		}
   267 	if (iInitialized)
   268 		{
   269 		// (The explicit scope operator is used against Lint warning #1506.)
   270 		TTemplateAsspUsbcc::StopUdc();
   271 		}
   272 	}
   273 
   274 
   275 TBool TTemplateAsspUsbcc::DeviceStateChangeCaps() const
   276 //
   277 // Returns capability of hardware to accurately track the device state (Chapter 9 state).
   278 //
   279 	{
   280 	// TO DO: Return EFalse or ETrue here, depending on whether the UDC supports exact device state tracking
   281 	// (most don't).
   282 	return EFalse;
   283 	}
   284 
   285 
   286 TInt TTemplateAsspUsbcc::SignalRemoteWakeup()
   287 //
   288 // Forces the UDC into a non-idle state to perform a remote wakeup operation.
   289 //
   290 	{
   291 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::SignalRemoteWakeup"));
   292 
   293 	// TO DO: Do here whatever is necessary for the UDC to signal remote wakeup.
   294 	
   295 	return KErrNone;
   296 	}
   297 
   298 
   299 void TTemplateAsspUsbcc::DumpRegisters()
   300 //
   301 // Dumps the contents of a number of UDC registers to the screen (using Kern::Printf()).
   302 // Rarely used, but might prove helpful when needed.
   303 //
   304 	{
   305 	Kern::Printf("TCotullaUsbcc::DumpRegisters:");
   306 
   307 	// TO DO: Print the contents of some (or all) UDC registers here.
   308 	}
   309 
   310 
   311 TDfcQue* TTemplateAsspUsbcc::DfcQ(TInt /* aUnit */)
   312 //
   313 // Returns a pointer to the kernel DFC queue to be used buy the USB LDD.
   314 //
   315 	{
   316 	return Kern::DfcQue0();
   317 	}
   318 
   319 
   320 // --- TTemplateAsspUsbcc private virtual ------------------------------------------
   321 
   322 TInt TTemplateAsspUsbcc::SetDeviceAddress(TInt aAddress)
   323 //
   324 // Sets the PIL-provided device address manually (if possible - otherwise do nothing).
   325 //
   326 	{
   327 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::SetDeviceAddress: %d", aAddress));
   328 
   329 	// TO DO (optional): Set device address here.
   330 
   331 	if (aAddress)
   332 		{
   333 		// Address can be zero.
   334 		MoveToAddressState();
   335 		}
   336 
   337 	return KErrNone;
   338 	}
   339 
   340 
   341 TInt TTemplateAsspUsbcc::ConfigureEndpoint(TInt aRealEndpoint, const TUsbcEndpointInfo& aEndpointInfo)
   342 //
   343 // Prepares (enables) an endpoint (incl. Ep0) for data transmission or reception.
   344 //
   345 	{
   346 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::ConfigureEndpoint(%d)", aRealEndpoint));
   347 
   348 	const TInt n = ArrayIdx2TemplateEp(aRealEndpoint);
   349 	if (n < 0)
   350 		return KErrArgument;
   351 
   352 	TEndpoint* const ep = &iEndpoints[aRealEndpoint];
   353 	if (ep->iDisabled == EFalse)
   354 		{
   355 		EnableEndpointInterrupt(n);
   356 		}
   357 	ep->iNoBuffer = EFalse;
   358 	if (n == 0)
   359 		iEp0Configured = ETrue;
   360 
   361 	return KErrNone;
   362 	}
   363 
   364 
   365 TInt TTemplateAsspUsbcc::DeConfigureEndpoint(TInt aRealEndpoint)
   366 //
   367 // Disables an endpoint (incl. Ep0).
   368 //
   369 	{
   370 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::DeConfigureEndpoint(%d)", aRealEndpoint));
   371 
   372 	const TInt n = ArrayIdx2TemplateEp(aRealEndpoint);
   373 	if (n < 0)
   374 		return KErrArgument;
   375 
   376 	DisableEndpointInterrupt(n);
   377 	if (n == 0)
   378 		iEp0Configured = EFalse;
   379 
   380 	return KErrNone;
   381 	}
   382 
   383 
   384 TInt TTemplateAsspUsbcc::AllocateEndpointResource(TInt aRealEndpoint, TUsbcEndpointResource aResource)
   385 //
   386 // Puts the requested endpoint resource to use, if possible.
   387 //
   388 	{
   389 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::AllocateEndpointResource(%d): %d",
   390 									aRealEndpoint, aResource));
   391 
   392 	// TO DO: Allocate endpoint resource here.
   393 
   394 	return KErrNone;
   395 	}
   396 
   397 
   398 TInt TTemplateAsspUsbcc::DeAllocateEndpointResource(TInt aRealEndpoint, TUsbcEndpointResource aResource)
   399 //
   400 // Stops the use of the indicated endpoint resource, if beneficial.
   401 //
   402 	{
   403 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::DeAllocateEndpointResource(%d): %d",
   404 									aRealEndpoint, aResource));
   405 
   406 	// TO DO: Deallocate endpoint resource here.
   407 
   408 	return KErrNone;
   409 	}
   410 
   411 
   412 TBool TTemplateAsspUsbcc::QueryEndpointResource(TInt aRealEndpoint, TUsbcEndpointResource aResource) const
   413 //
   414 // Returns the status of the indicated resource and endpoint.
   415 //
   416 	{
   417 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::QueryEndpointResource(%d): %d",
   418 									aRealEndpoint, aResource));
   419 
   420 	// TO DO: Query endpoint resource here. The return value should reflect the actual state.
   421 	return ETrue;
   422 	}
   423 
   424 
   425 TInt TTemplateAsspUsbcc::OpenDmaChannel(TInt aRealEndpoint)
   426 //
   427 // Opens a DMA channel for this endpoint. This function is always called during the creation of an endpoint
   428 // in the PIL. If DMA channels are a scarce resource, it's possible to do nothing here and wait for an
   429 // AllocateEndpointResource call instead.
   430 //
   431 	{
   432 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::OpenDmaChannel(%d)", aRealEndpoint));
   433 
   434 	// TO DO (optional): Open DMA channel here.
   435 
   436 	// An error should only  be returned in case of an actual DMA problem.
   437 	return KErrNone;
   438 	}
   439 
   440 
   441 void TTemplateAsspUsbcc::CloseDmaChannel(TInt aRealEndpoint)
   442 //
   443 // Closes a DMA channel for this endpoint. This function is always called during the destruction of an
   444 // endpoint in the PIL.
   445 //
   446 	{
   447 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::CloseDmaChannel(%d)", aRealEndpoint));
   448 
   449 	// TO DO (optional): Close DMA channel here (only if it was opened via OpenDmaChannel).
   450 	}
   451 
   452 
   453 TInt TTemplateAsspUsbcc::SetupEndpointRead(TInt aRealEndpoint, TUsbcRequestCallback& aCallback)
   454 //
   455 // Sets up a read request for an endpoint on behalf of the LDD.
   456 //
   457 	{
   458 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::SetupEndpointRead(%d)", aRealEndpoint));
   459 
   460 	if (!IS_OUT_ENDPOINT(aRealEndpoint))
   461 		{
   462 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: !IS_OUT_ENDPOINT(%d)", aRealEndpoint));
   463 		return KErrArgument;
   464 		}
   465 	TEndpoint* const ep = &iEndpoints[aRealEndpoint];
   466 	if (ep->iRxBuf != NULL)
   467 		{
   468 		__KTRACE_OPT(KUSB, Kern::Printf(" > WARNING: iEndpoints[%d].iRxBuf != NULL", aRealEndpoint));
   469 		return KErrGeneral;
   470 		}
   471 	ep->iRxBuf = aCallback.iBufferStart;
   472 	ep->iReceived = 0;
   473 	ep->iLength = aCallback.iLength;
   474 	// For Bulk reads we start out with the assumption of 1 packet (see BulkReceive for why):
   475 	ep->iPackets = IS_BULK_OUT_ENDPOINT(aRealEndpoint) ? 1 : 0;
   476 	ep->iRequest = &aCallback;
   477 	ep->iPacketIndex = aCallback.iPacketIndex;
   478 	if (IS_BULK_OUT_ENDPOINT(aRealEndpoint))
   479 		*ep->iPacketIndex = 0;								// a one-off optimization
   480 	ep->iPacketSize = aCallback.iPacketSize;
   481 
   482 	const TInt n = ArrayIdx2TemplateEp(aRealEndpoint);
   483 	if (ep->iDisabled)
   484 		{
   485 		ep->iDisabled = EFalse;
   486 		EnableEndpointInterrupt(n);
   487 		}
   488 	else if (ep->iNoBuffer)
   489 		{
   490 		__KTRACE_OPT(KUSB, Kern::Printf(" > There had been no Rx buffer available: reading Rx FIFO now"));
   491 		ep->iNoBuffer = EFalse;
   492 		if (IS_BULK_OUT_ENDPOINT(aRealEndpoint))
   493 			{
   494 			BulkReadRxFifo(n);
   495 			}
   496 		else if (IS_ISO_OUT_ENDPOINT(aRealEndpoint))
   497 			{
   498 			IsoReadRxFifo(n);
   499 			}
   500 		else
   501 			{
   502 			__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Endpoint not found"));
   503 			}
   504 		}
   505 
   506 	return KErrNone;
   507 	}
   508 
   509 
   510 TInt TTemplateAsspUsbcc::SetupEndpointWrite(TInt aRealEndpoint, TUsbcRequestCallback& aCallback)
   511 //
   512 // Sets up a write request for an endpoint on behalf of the LDD.
   513 //
   514 	{
   515 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::SetupEndpointWrite(%d)", aRealEndpoint));
   516 
   517 	if (!IS_IN_ENDPOINT(aRealEndpoint))
   518 		{
   519 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: !IS_IN_ENDPOINT(%d)", aRealEndpoint));
   520 		return KErrArgument;
   521 		}
   522 	TEndpoint* const ep = &iEndpoints[aRealEndpoint];
   523 	if (ep->iTxBuf != NULL)
   524 		{
   525 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: iEndpoints[%d].iTxBuf != NULL", aRealEndpoint));
   526 		return KErrGeneral;
   527 		}
   528 	ep->iTxBuf = aCallback.iBufferStart;
   529 	ep->iTransmitted = 0;
   530 	ep->iLength = aCallback.iLength;
   531 	ep->iPackets = 0;
   532 	ep->iZlpReqd = aCallback.iZlpReqd;
   533 	ep->iRequest = &aCallback;
   534 
   535 	const TInt n = ArrayIdx2TemplateEp(aRealEndpoint);
   536 	if (IS_BULK_IN_ENDPOINT(aRealEndpoint))
   537 		{
   538 		if (ep->iDisabled)
   539 			{
   540 			ep->iDisabled = EFalse;
   541 			EnableEndpointInterrupt(n);
   542 			}
   543 		BulkTransmit(n);
   544 		}
   545 	else if (IS_ISO_IN_ENDPOINT(aRealEndpoint))
   546 		{
   547 		IsoTransmit(n);
   548 		}
   549 	else if (IS_INT_IN_ENDPOINT(aRealEndpoint))
   550 		{
   551 		IntTransmit(n);
   552 		}
   553 	else
   554 		{
   555 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Endpoint not found"));
   556 		}
   557 
   558 	return KErrNone;
   559 	}
   560 
   561 
   562 TInt TTemplateAsspUsbcc::CancelEndpointRead(TInt aRealEndpoint)
   563 //
   564 // Cancels a read request for an endpoint on behalf of the LDD.
   565 // No completion to the PIL occurs.
   566 //
   567 	{
   568 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::CancelEndpointRead(%d)", aRealEndpoint));
   569 
   570 	if (!IS_OUT_ENDPOINT(aRealEndpoint))
   571 		{
   572 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: !IS_OUT_ENDPOINT(%d)", aRealEndpoint));
   573 		return KErrArgument;
   574 		}
   575 	TEndpoint* const ep = &iEndpoints[aRealEndpoint];
   576 	if (ep->iRxBuf == NULL)
   577 		{
   578 		__KTRACE_OPT(KUSB, Kern::Printf(" > WARNING: iEndpoints[%d].iRxBuf == NULL", aRealEndpoint));
   579 		return KErrNone;
   580 		}
   581 	ep->iRxBuf = NULL;
   582 	ep->iReceived = 0;
   583 	ep->iNoBuffer = EFalse;
   584 
   585 	return KErrNone;
   586 	}
   587 
   588 
   589 TInt TTemplateAsspUsbcc::CancelEndpointWrite(TInt aRealEndpoint)
   590 //
   591 // Cancels a write request for an endpoint on behalf of the LDD.
   592 // No completion to the PIL occurs.
   593 //
   594 	{
   595 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::CancelEndpointWrite(%d)", aRealEndpoint));
   596 
   597 	if (!IS_IN_ENDPOINT(aRealEndpoint))
   598 		{
   599 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: !IS_IN_ENDPOINT(%d)", aRealEndpoint));
   600 		return KErrArgument;
   601 		}
   602 	TEndpoint* const ep = &iEndpoints[aRealEndpoint];
   603 	if (ep->iTxBuf == NULL)
   604 		{
   605 		__KTRACE_OPT(KUSB, Kern::Printf(" > WARNING: iEndpoints[%d].iTxBuf == NULL", aRealEndpoint));
   606 		return KErrNone;
   607 		}
   608 
   609 	// TO DO (optional): Flush the Ep's Tx FIFO here, if possible.
   610 
   611 	ep->iTxBuf = NULL;
   612 	ep->iTransmitted = 0;
   613 	ep->iNoBuffer = EFalse;
   614 
   615 	return KErrNone;
   616 	}
   617 
   618 
   619 TInt TTemplateAsspUsbcc::SetupEndpointZeroRead()
   620 //
   621 // Sets up an Ep0 read request (own function due to Ep0's special status).
   622 //
   623 	{
   624 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::SetupEndpointZeroRead"));
   625 
   626 	TEndpoint* const ep = &iEndpoints[KEp0_Out];
   627 	if (ep->iRxBuf != NULL)
   628 		{
   629 		__KTRACE_OPT(KUSB, Kern::Printf(" > WARNING: iEndpoints[%d].iRxBuf != NULL", KEp0_Out));
   630 		return KErrGeneral;
   631 		}
   632 	ep->iRxBuf = iEp0_RxBuf;
   633 	ep->iReceived = 0;
   634 
   635 	return KErrNone;
   636 	}
   637 
   638 
   639 TInt TTemplateAsspUsbcc::SetupEndpointZeroWrite(const TUint8* aBuffer, TInt aLength, TBool aZlpReqd)
   640 //
   641 // Sets up an Ep0 write request (own function due to Ep0's special status).
   642 //
   643 	{
   644 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::SetupEndpointZeroWrite"));
   645 
   646 	TEndpoint* const ep = &iEndpoints[KEp0_In];
   647 	if (ep->iTxBuf != NULL)
   648 		{
   649 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: iEndpoints[%d].iTxBuf != NULL", KEp0_In));
   650 		return KErrGeneral;
   651 		}
   652 	ep->iTxBuf = aBuffer;
   653 	ep->iTransmitted = 0;
   654 	ep->iLength = aLength;
   655 	ep->iZlpReqd = aZlpReqd;
   656 	ep->iRequest = NULL;
   657 	Ep0Transmit();
   658 
   659 	return KErrNone;
   660 	}
   661 
   662 
   663 TInt TTemplateAsspUsbcc::SendEp0ZeroByteStatusPacket()
   664 //
   665 // Sets up an Ep0 write request for zero bytes.
   666 // This is a separate function because no data transfer is involved here.
   667 //
   668 	{
   669 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::SendEp0ZeroByteStatusPacket"));
   670 
   671 	// This is possibly a bit tricky. When this function is called it just means that the higher layer wants a
   672 	// ZLP to be sent. Whether we actually send one manually here depends on a number of factors, as the
   673 	// current Ep0 state (i.e. the stage of the Ep0 Control transfer), and, in case the hardware handles some
   674 	// ZLPs itself, whether it might already handle this one.
   675 
   676 	// Here is an example of what the checking of the conditions might look like:
   677 
   678 #ifndef USB_SUPPORTS_SET_DESCRIPTOR_REQUEST
   679 	if ((!iEp0ReceivedNonStdRequest && iEp0State == EP0_IN_DATA_PHASE) ||
   680 #else
   681 	if ((!iEp0ReceivedNonStdRequest && iEp0State != EP0_IDLE) ||
   682 #endif
   683 #ifdef USB_SUPPORTS_PREMATURE_STATUS_IN
   684 		(iEp0ReceivedNonStdRequest && iEp0State != EP0_OUT_DATA_PHASE))
   685 #else
   686 		(iEp0ReceivedNonStdRequest))
   687 #endif
   688 		{
   689 		// TO DO: Arrange for the sending of a ZLP here.
   690 		}
   691 
   692 	return KErrNone;
   693 	}
   694 
   695 
   696 TInt TTemplateAsspUsbcc::StallEndpoint(TInt aRealEndpoint)
   697 //
   698 // Stalls an endpoint.
   699 //
   700 	{
   701 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::StallEndpoint(%d)", aRealEndpoint));
   702 
   703 	if (IS_ISO_ENDPOINT(aRealEndpoint))
   704 		{
   705 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Iso endpoint cannot be stalled"));
   706 		return KErrArgument;
   707 		}
   708 
   709 	// TO DO: Stall the endpoint here.
   710 
   711 	return KErrNone;
   712 	}
   713 
   714 
   715 TInt TTemplateAsspUsbcc::ClearStallEndpoint(TInt aRealEndpoint)
   716 //
   717 // Clears the stall condition of an endpoint.
   718 //
   719 	{
   720 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::ClearStallEndpoint(%d)", aRealEndpoint));
   721 
   722 	if (IS_ISO_ENDPOINT(aRealEndpoint))
   723 		{
   724 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Iso endpoint cannot be unstalled"));
   725 		return KErrArgument;
   726 		}
   727 
   728 	// TO DO: De-stall the endpoint here.
   729 
   730 	return KErrNone;
   731 	}
   732 
   733 
   734 TInt TTemplateAsspUsbcc::EndpointStallStatus(TInt aRealEndpoint) const
   735 //
   736 // Reports the stall status of an endpoint.
   737 //
   738 	{
   739 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::EndpointStallStatus(%d)", aRealEndpoint));
   740 
   741 	if (IS_ISO_ENDPOINT(aRealEndpoint))
   742 		{
   743 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Iso endpoint has no stall status"));
   744 		return KErrArgument;
   745 		}
   746 
   747 	// TO DO: Query endpoint stall status here. The return value should reflect the actual state.
   748 	return ETrue;
   749 	}
   750 
   751 
   752 TInt TTemplateAsspUsbcc::EndpointErrorStatus(TInt aRealEndpoint) const
   753 //
   754 // Reports the error status of an endpoint.
   755 //
   756 	{
   757 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::EndpointErrorStatus(%d)", aRealEndpoint));
   758 
   759 	if (!IS_VALID_ENDPOINT(aRealEndpoint))
   760 		{
   761 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: !IS_VALID_ENDPOINT(%d)", aRealEndpoint));
   762 		return KErrArgument;
   763 		}
   764 
   765 	// TO DO: Query endpoint error status here. The return value should reflect the actual state.
   766 	// With some UDCs there is no way of inquiring the endpoint error status; say 'ETrue' in that case.
   767 	return KErrNone;
   768 	}
   769 
   770 
   771 TInt TTemplateAsspUsbcc::ResetDataToggle(TInt aRealEndpoint)
   772 //
   773 // Resets to zero the data toggle bit of an endpoint.
   774 //
   775 	{
   776 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::ResetDataToggle(%d)", aRealEndpoint));
   777 
   778 	// TO DO: Reset the endpoint's data toggle bit here.
   779 	// With some UDCs there is no way to individually reset the endpoint's toggle bits; just return KErrNone
   780 	// in that case.
   781 
   782 	return KErrNone;
   783 	}
   784 
   785 
   786 TInt TTemplateAsspUsbcc::SynchFrameNumber() const
   787 //
   788 // For use with isochronous endpoints only. Causes the SOF frame number to be returned.
   789 //
   790 	{
   791 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::SynchFrameNumber"));
   792 
   793 	// TO DO: Query and return the SOF frame number here.
   794 	return 0;
   795 	}
   796 
   797 
   798 void TTemplateAsspUsbcc::SetSynchFrameNumber(TInt aFrameNumber)
   799 //
   800 // For use with isochronous endpoints only. Causes the SOF frame number to be stored.
   801 //
   802 	{
   803 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::SetSynchFrameNumber(%d)", aFrameNumber));
   804 
   805 	// We should actually store this number somewhere. But the PIL always sends '0x00'
   806 	// in response to a SYNCH_FRAME request...
   807 	// TO DO: Store the frame number. Alternatively (until SYNCH_FRAME request specification changes): Do
   808 	// nothing.
   809 	}
   810 
   811 
   812 TInt TTemplateAsspUsbcc::StartUdc()
   813 //
   814 // Called to initialize the device controller hardware before any operation can be performed.
   815 //
   816 	{
   817 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::StartUdc"));
   818 
   819 	if (iInitialized)
   820 		{
   821 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: UDC already initialised"));
   822 		return KErrNone;
   823 		}
   824 
   825 	// Disable UDC (might also reset the entire design):
   826 	UdcDisable();
   827 
   828 	// Enable UDC's clock:
   829 	// TO DO: Enable UDC's clock here.
   830 
   831 	// Even if only one USB feature has been enabled, we later need to undo it:
   832 	iInitialized = ETrue;
   833 
   834 	// Bind & enable the UDC interrupt
   835 	if (SetupUdcInterrupt() != KErrNone)
   836 		{
   837 		return KErrGeneral;
   838 		}
   839 
   840 	// Write meaningful values to some registers:
   841 	InitialiseUdcRegisters();
   842 
   843 	// Finally, turn on the UDC:
   844 	UdcEnable();
   845 
   846 	return KErrNone;
   847 	}
   848 
   849 
   850 TInt TTemplateAsspUsbcc::StopUdc()
   851 //
   852 // Basically, makes undone what happened in StartUdc.
   853 //
   854 	{
   855 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::StopUdc"));
   856 
   857 	if (!iInitialized)
   858 		{
   859 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: UDC not initialized"));
   860 		return KErrNone;
   861 		}
   862 
   863 	// Disable UDC:
   864 	UdcDisable();
   865 
   866 	// Mask (disable) Reset interrupt:
   867 	// TO DO: Mask (disable) the USB Reset interrupt here.
   868 
   869 	// Disable & unbind the UDC interrupt:
   870 	ReleaseUdcInterrupt();
   871 
   872 	// Finally turn off UDC's clock:
   873 	// TO DO: Disable UDC's clock here.
   874 
   875 	// Only when all USB features have been disabled we'll call it a day:
   876 	iInitialized = EFalse;
   877 
   878 	return KErrNone;
   879 	}
   880 
   881 
   882 TInt TTemplateAsspUsbcc::UdcConnect()
   883 //
   884 // Connects the UDC to the bus under software control. How this is achieved depends on the UDC; the
   885 // functionality might also be part of the Variant component (instead of the ASSP).
   886 //
   887 	{
   888 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::UdcConnect"));
   889 
   890 	// Here: A call into the Variant-provided function.
   891 	return iAssp->UsbConnect();
   892 	}
   893 
   894 
   895 TInt TTemplateAsspUsbcc::UdcDisconnect()
   896 //
   897 // Disconnects the UDC from the bus under software control. How this is achieved depends on the UDC; the
   898 // functionality might also be part of the Variant component (instead of the ASSP).
   899 //
   900 	{
   901 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::UdcDisconnect"));
   902 
   903 	// Here: A call into the Variant-provided function.
   904 	return iAssp->UsbDisconnect();
   905 	}
   906 
   907 
   908 TBool TTemplateAsspUsbcc::UsbConnectionStatus() const
   909 //
   910 // Returns a value showing the USB cable connection status of the device.
   911 //
   912 	{
   913 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::UsbConnectionStatus"));
   914 
   915 	return iCableConnected;
   916 	}
   917 
   918 
   919 TBool TTemplateAsspUsbcc::UsbPowerStatus() const
   920 //
   921 // Returns a truth value showing whether VBUS is currently powered or not.
   922 //
   923 	{
   924 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::UsbPowerStatus"));
   925 
   926 	return iBusIsPowered;
   927 	}
   928 
   929 
   930 TBool TTemplateAsspUsbcc::DeviceSelfPowered() const
   931 //
   932 // Returns a truth value showing whether the device is currently self-powered or not.
   933 //
   934 	{
   935 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::DeviceSelfPowered"));
   936 
   937 	// TO DO: Query and return self powered status here. The return value should reflect the actual state.
   938 	// (This can be always 'ETrue' if the UDC does not support bus-powered devices.)
   939 	return ETrue;
   940 	}
   941 
   942 
   943 const TUsbcEndpointCaps* TTemplateAsspUsbcc::DeviceEndpointCaps() const
   944 //
   945 // Returns a pointer to an array of elements, each of which describes the capabilities of one endpoint.
   946 //
   947 	{
   948 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::DeviceEndpointCaps"));
   949 	__KTRACE_OPT(KUSB, Kern::Printf(" > Ep: Sizes Mask, Types Mask"));
   950 	__KTRACE_OPT(KUSB, Kern::Printf(" > --------------------------"));
   951 	for (TInt i = 0; i < KUsbTotalEndpoints; ++i)
   952 		{
   953 		__KTRACE_OPT(KUSB, Kern::Printf(" > %02d: 0x%08x, 0x%08x",
   954 										i, DeviceEndpoints[i].iSizes, DeviceEndpoints[i].iTypesAndDir));
   955 		}
   956 	return DeviceEndpoints;
   957 	}
   958 
   959 
   960 TInt TTemplateAsspUsbcc::DeviceTotalEndpoints() const
   961 //
   962 // Returns the element number of the endpoints array a pointer to which is returned by DeviceEndpointCaps.
   963 //
   964 	{
   965 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::DeviceTotalEndpoints"));
   966 
   967 	return KUsbTotalEndpoints;
   968 	}
   969 
   970 
   971 TBool TTemplateAsspUsbcc::SoftConnectCaps() const
   972 //
   973 // Returns a truth value showing whether or not there is the capability to disconnect and re-connect the D+
   974 // line under software control.
   975 //
   976 	{
   977 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::SoftConnectCaps"));
   978 
   979 	return iSoftwareConnectable;
   980 	}
   981 
   982 
   983 void TTemplateAsspUsbcc::Suspend()
   984 //
   985 // Called by the PIL after a Suspend event has been reported (by us).
   986 //
   987 	{
   988 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Suspend"));
   989 
   990 	// TO DO (optional): Implement here anything the device might require after bus SUSPEND signalling.
   991 	}
   992 
   993 
   994 void TTemplateAsspUsbcc::Resume()
   995 //
   996 // Called by the PIL after a Resume event has been reported (by us).
   997 //
   998 	{
   999 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Resume"));
  1000 
  1001 	// TO DO (optional): Implement here anything the device might require after bus RESUME signalling.
  1002 	}
  1003 
  1004 
  1005 void TTemplateAsspUsbcc::Reset()
  1006 //
  1007 // Called by the PIL after a Reset event has been reported (by us).
  1008 //
  1009 	{
  1010 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Reset"));
  1011 
  1012 	// This does not really belong here, but has to do with the way the PIL sets
  1013 	// up Ep0 reads and writes.
  1014 	TEndpoint* ep = &iEndpoints[0];
  1015 	ep->iRxBuf = NULL;
  1016 	++ep;
  1017 	ep->iTxBuf = NULL;
  1018 	// Idle
  1019 	Ep0NextState(EP0_IDLE);
  1020 
  1021 	// TO DO (optional): Implement here anything the device might require after bus RESET signalling.
  1022 
  1023 	// Write meaningful values to some registers
  1024 	InitialiseUdcRegisters();
  1025 	UdcEnable();
  1026 	if (iEp0Configured)
  1027 		EnableEndpointInterrupt(0);
  1028 	}
  1029 
  1030 
  1031 // --- TTemplateAsspUsbcc private --------------------------------------------------
  1032 
  1033 void TTemplateAsspUsbcc::InitialiseUdcRegisters()
  1034 //
  1035 // Called after every USB Reset etc.
  1036 //
  1037 	{
  1038 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::InitialiseUdcRegisters"));
  1039 
  1040 	// Unmask Suspend interrupt
  1041 	// TO DO: Unmask Suspend interrupt here.
  1042 
  1043 	// Unmask Resume interrupt
  1044 	// TO DO: Unmask Resume interrupt here.
  1045 
  1046 	// Unmask Start-of-Frame (SOF) interrupt
  1047 	// TO DO (optional): Unmask SOF interrupt here.
  1048 
  1049 	// Disable interrupt requests for all endpoints
  1050 	// TO DO: Disable interrupt requests for all endpoints here.
  1051 	}
  1052 
  1053 
  1054 void TTemplateAsspUsbcc::UdcEnable()
  1055 //
  1056 // Enables the UDC for USB transmission or reception.
  1057 //
  1058 	{
  1059 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::UdcEnable"));
  1060 
  1061 	// TO DO: Do whatever is necessary to enable the UDC here. This might include enabling (unmasking)
  1062 	// the USB Reset interrupt, setting a UDC enable bit, etc.
  1063 	}
  1064 
  1065 
  1066 void TTemplateAsspUsbcc::UdcDisable()
  1067 //
  1068 // Disables the UDC.
  1069 //
  1070 	{
  1071 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::UdcDisable"));
  1072 
  1073 	// TO DO: Do whatever is necessary to disable the UDC here. This might include disabling (masking)
  1074 	// the USB Reset interrupt, clearing a UDC enable bit, etc.
  1075 	}
  1076 
  1077 
  1078 void TTemplateAsspUsbcc::EnableEndpointInterrupt(TInt aEndpoint)
  1079 //
  1080 // Enables interrupt requests for an endpoint.
  1081 //
  1082 	{
  1083 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::EnableEndpointInterrupt(%d)", aEndpoint));
  1084 
  1085 	// Enable (unmask) interrupt requests for this endpoint:
  1086 	// TO DO: Enable interrupt requests for aEndpoint here.
  1087 	}
  1088 
  1089 
  1090 void TTemplateAsspUsbcc::DisableEndpointInterrupt(TInt aEndpoint)
  1091 //
  1092 // Disables interrupt requests for an endpoint.
  1093 //
  1094 	{
  1095 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::DisableEndpointInterrupt(%d)", aEndpoint));
  1096 
  1097 	// Disable (mask) interrupt requests for this endpoint:
  1098 	// TO DO: Disable interrupt requests for aEndpoint here.
  1099 	}
  1100 
  1101 
  1102 void TTemplateAsspUsbcc::ClearEndpointInterrupt(TInt aEndpoint)
  1103 //
  1104 // Clears a pending interrupt request for an endpoint.
  1105 //
  1106 	{
  1107 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::ClearEndpointInterrupt(%d)", aEndpoint));
  1108 
  1109 	// Clear (reset) pending interrupt request for this endpoint:
  1110 	// TO DO: Clear interrupt request for aEndpoint here.
  1111 	}
  1112 
  1113 
  1114 void TTemplateAsspUsbcc::Ep0IntService()
  1115 //
  1116 // ISR for endpoint zero interrupt.
  1117 //
  1118 	{
  1119 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Ep0IntService"));
  1120 
  1121 	// TO DO: Enquire about Ep0 status & the interrupt cause here. Depending on the event and the Ep0 state,
  1122 	// one or more of the following functions might then be called:
  1123 	Ep0Cancel();
  1124 	Ep0ReadSetupPkt();
  1125 	Ep0EndXfer();
  1126 	Ep0PrematureStatusOut();
  1127 	Ep0Transmit();
  1128 	Ep0StatusIn();
  1129 	Ep0Receive();
  1130 	ClearStallEndpoint(0);
  1131 
  1132 	ClearEndpointInterrupt(0);
  1133 	return;
  1134 	}
  1135 
  1136 
  1137 void TTemplateAsspUsbcc::Ep0ReadSetupPkt()
  1138 //
  1139 // Called from the Ep0 ISR when a new Setup packet has been received.
  1140 //
  1141 	{
  1142 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Ep0ReadSetupPkt"));
  1143 
  1144 	TEndpoint* const ep = &iEndpoints[KEp0_Out];
  1145 	TUint8* buf = ep->iRxBuf;
  1146 	if (!buf)
  1147 		{
  1148 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: No Ep0 Rx buffer available (1)"));
  1149 		StallEndpoint(KEp0_Out);
  1150 		return;
  1151 		}
  1152 
  1153 	// TO DO: Read Setup packet data from Rx FIFO into 'buf' here.
  1154 	// (In this function we don't need to use "ep->iReceived" since Setup packets
  1155 	// are always 8 bytes long.)
  1156 
  1157 	// Upcall into PIL to determine next Ep0 state:
  1158 	TUsbcEp0State state = EnquireEp0NextState(ep->iRxBuf);
  1159 
  1160 	if (state == EEp0StateStatusIn)
  1161 		{
  1162 		Ep0NextState(EP0_IDLE);								// Ep0 No Data
  1163 		}
  1164 	else if (state == EEp0StateDataIn)
  1165 		{
  1166 		Ep0NextState(EP0_IN_DATA_PHASE);					// Ep0 Control Read
  1167 		}
  1168 	else
  1169 		{
  1170 		Ep0NextState(EP0_OUT_DATA_PHASE);					// Ep0 Control Write
  1171 		}
  1172 
  1173 	ep->iRxBuf = NULL;
  1174 	const TInt r = Ep0RequestComplete(KEp0_Out, 8, KErrNone);
  1175 
  1176 	// Don't finish (proceed) if request completion returned 'KErrNotFound'!
  1177 	if (!(r == KErrNone || r == KErrGeneral))
  1178 		{
  1179 		DisableEndpointInterrupt(0);
  1180 		}
  1181 
  1182 	// TO DO (optional): Clear Ep0 Setup condition flags here.
  1183 
  1184 #ifdef USB_SUPPORTS_PREMATURE_STATUS_IN
  1185 	if (iEp0State == EP0_OUT_DATA_PHASE)
  1186 		{
  1187 		// Allow for a premature STATUS IN
  1188 		// TO DO: Arrange for the sending of a ZLP here.
  1189 		}
  1190 #endif
  1191 	}
  1192 
  1193 
  1194 void TTemplateAsspUsbcc::Ep0ReadSetupPktProceed()
  1195 //
  1196 // Called by the PIL to signal that it has finished processing a received Setup packet and that the PSL can
  1197 // now prepare itself for the next Ep0 reception (for instance by re-enabling the Ep0 interrupt).
  1198 //
  1199 	{
  1200 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Ep0ReadSetupPktProceed"));
  1201 
  1202 	EnableEndpointInterrupt(0);
  1203 	}
  1204 
  1205 
  1206 void TTemplateAsspUsbcc::Ep0Receive()
  1207 //
  1208 // Called from the Ep0 ISR when a data packet has been received.
  1209 //
  1210 	{
  1211 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Ep0Receive"));
  1212 
  1213 	TEndpoint* const ep = &iEndpoints[KEp0_Out];
  1214 	TUint8* buf = ep->iRxBuf;
  1215 	if (!buf)
  1216 		{
  1217 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: No Ep0 Rx buffer available (2)"));
  1218 		StallEndpoint(KEp0_Out);
  1219 		return;
  1220 		}
  1221 
  1222 	TInt n = 0;
  1223 	// TO DO: Read packet data from Rx FIFO into 'buf' and update 'n' (# of received bytes) here.
  1224 
  1225 	ep->iReceived = n;
  1226 	ep->iRxBuf = NULL;
  1227 	const TInt r = Ep0RequestComplete(KEp0_Out, n, KErrNone);
  1228 
  1229 	// Don't finish (proceed) if request was 'KErrNotFound'!
  1230 	if (!(r == KErrNone || r == KErrGeneral))
  1231 		{
  1232 		DisableEndpointInterrupt(0);
  1233 		}
  1234 
  1235 	// TO DO (optional): Clear Ep0 Rx condition flags here.
  1236 
  1237 #ifdef USB_SUPPORTS_PREMATURE_STATUS_IN
  1238 	// Allow for a premature STATUS IN
  1239 	// TO DO: Arrange for the sending of a ZLP here.
  1240 #endif
  1241 	}
  1242 
  1243 
  1244 void TTemplateAsspUsbcc::Ep0ReceiveProceed()
  1245 //
  1246 // Called by the PIL to signal that it has finished processing a received Ep0 data packet and that the PSL can
  1247 // now prepare itself for the next Ep0 reception (for instance by re-enabling the Ep0 interrupt).
  1248 //
  1249 	{
  1250 	Ep0ReadSetupPktProceed();
  1251 	}
  1252 
  1253 
  1254 void TTemplateAsspUsbcc::Ep0Transmit()
  1255 //
  1256 // Called from either the Ep0 ISR or the PIL when a data packet has been or is to be transmitted.
  1257 //
  1258 	{
  1259 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Ep0Transmit"));
  1260 
  1261 	if (iEp0State != EP0_IN_DATA_PHASE)
  1262 		{
  1263 		__KTRACE_OPT(KUSB, Kern::Printf(" > WARNING: Invalid Ep0 state when trying to handle EP0 IN"));
  1264 		// TO DO (optional): Do something about this warning.
  1265 		}
  1266 
  1267 	TEndpoint* const ep = &iEndpoints[KEp0_In];
  1268 	const TUint8* buf = ep->iTxBuf;
  1269 	if (!buf)
  1270 		{
  1271 		__KTRACE_OPT(KUSB, Kern::Printf(" > No Tx buffer available: returning"));
  1272 		return;
  1273 		}
  1274 	const TInt t = ep->iTransmitted;						// already transmitted
  1275 	buf += t;
  1276 	TInt n = 0;												// now transmitted
  1277 
  1278 	// TO DO: Write packet data (if any) into Tx FIFO from 'buf' and update 'n' (# of tx'ed bytes) here.
  1279 
  1280 	ep->iTransmitted += n;
  1281 	
  1282 	// coverity[dead_error_condition]
  1283 	// The next line should be reachable when this template file is edited for use
  1284 	if (n == KEp0MaxPktSz)
  1285 		{
  1286 		if (ep->iTransmitted == ep->iLength && !(ep->iZlpReqd))
  1287 			Ep0NextState(EP0_END_XFER);
  1288 		}
  1289 	else if (n && n != KEp0MaxPktSz)
  1290 		{
  1291 		// Send off the data
  1292 		__ASSERT_DEBUG((ep->iTransmitted == ep->iLength),
  1293 					   Kern::Printf(" > ERROR: Short packet in mid-transfer"));
  1294 		Ep0NextState(EP0_END_XFER);
  1295 		// TO DO: Send off the data here.
  1296 		}
  1297 	else // if (n == 0)
  1298 		{
  1299 		__ASSERT_DEBUG((ep->iTransmitted == ep->iLength),
  1300 					   Kern::Printf(" > ERROR: Nothing transmitted but still not finished"));
  1301 		if (ep->iZlpReqd)
  1302 			{
  1303 			// Send a zero length packet
  1304 			ep->iZlpReqd = EFalse;
  1305 			Ep0NextState(EP0_END_XFER);
  1306 			// TO DO: Arrange for the sending of a ZLP here.
  1307 			}
  1308 		else
  1309 			{
  1310 			__KTRACE_OPT(KPANIC, Kern::Printf("  Error: nothing transmitted & no ZLP req'd"));
  1311 			}
  1312 		}
  1313 	}
  1314 
  1315 
  1316 void TTemplateAsspUsbcc::Ep0EndXfer()
  1317 //
  1318 // Called at the end of a Ep0 Control transfer.
  1319 //
  1320 	{
  1321 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Ep0EndXfer"));
  1322 
  1323 	// TO DO (optional): Clear Ep0 Rx condition flags here.
  1324 
  1325 	Ep0NextState(EP0_IDLE);
  1326 	TEndpoint* const ep = &iEndpoints[KEp0_In];
  1327 	ep->iTxBuf = NULL;
  1328 	(void) Ep0RequestComplete(KEp0_In, ep->iTransmitted, KErrNone);
  1329 	}
  1330 
  1331 
  1332 void TTemplateAsspUsbcc::Ep0Cancel()
  1333 //
  1334 // Called when an ongoing Ep0 Control transfer has to be aborted prematurely (for instance when receiving a
  1335 // new Setup packet before the processing of the old one has completed).
  1336 //
  1337 	{
  1338 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Ep0Cancel"));
  1339 
  1340 	Ep0NextState(EP0_IDLE);
  1341 	TEndpoint* const ep = &iEndpoints[KEp0_In];
  1342 	if (ep->iTxBuf)
  1343 		{
  1344 		ep->iTxBuf = NULL;
  1345 		const TInt err = (ep->iTransmitted == ep->iLength) ? KErrNone : KErrCancel;
  1346 		(void) Ep0RequestComplete(KEp0_In, ep->iTransmitted, err);
  1347 		}
  1348 	}
  1349 
  1350 
  1351 void TTemplateAsspUsbcc::Ep0PrematureStatusOut()
  1352 //
  1353 // Called when an ongoing Ep0 Control transfer encounters a premature Status OUT condition.
  1354 //
  1355 	{
  1356 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Ep0PrematureStatusOut"));
  1357 
  1358 	// TO DO (optional): Clear Ep0 Rx condition flags here.
  1359 
  1360 	Ep0NextState(EP0_IDLE);
  1361 
  1362 	// TO DO (optional): Flush the Ep0 Tx FIFO here, if possible.
  1363 
  1364 	TEndpoint* const ep = &iEndpoints[KEp0_In];
  1365 	if (ep->iTxBuf)
  1366 		{
  1367 		ep->iTxBuf = NULL;
  1368 		(void) Ep0RequestComplete(KEp0_In, ep->iTransmitted, KErrPrematureEnd);
  1369 		}
  1370 	}
  1371 
  1372 
  1373 void TTemplateAsspUsbcc::Ep0StatusIn()
  1374 //
  1375 // Called when an ongoing Ep0 Control transfer moves to a Status IN stage.
  1376 //
  1377 	{
  1378 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Ep0StatusIn"));
  1379 
  1380 	Ep0NextState(EP0_IDLE);
  1381 	}
  1382 
  1383 
  1384 void TTemplateAsspUsbcc::BulkTransmit(TInt aEndpoint)
  1385 //
  1386 // Endpoint 1 (BULK IN).
  1387 // Called from either the Ep ISR or the PIL when a data packet has been or is to be transmitted.
  1388 //
  1389 	{
  1390 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::BulkTransmit(%d)", aEndpoint));
  1391 
  1392 	// TO DO: Enquire about Ep status here.
  1393 
  1394 	const TInt idx = 3;										// only in our special case of course!
  1395 	TEndpoint* const ep = &iEndpoints[idx];
  1396 	const TUint8* buf = ep->iTxBuf;
  1397 	if (!buf)
  1398 		{
  1399 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: No Tx buffer has been set up"));
  1400 		DisableEndpointInterrupt(aEndpoint);
  1401 		ep->iDisabled = ETrue;
  1402 		ClearEndpointInterrupt(aEndpoint);
  1403 		return;
  1404 		}
  1405 	const TInt t = ep->iTransmitted;						// already transmitted
  1406 	const TInt len = ep->iLength;							// to be sent in total
  1407 	// (len || ep->iPackets): Don't complete for a zero bytes request straight away.
  1408 	if (t >= len && (len || ep->iPackets))
  1409 		{
  1410 		if (ep->iZlpReqd)
  1411 			{
  1412 			__KTRACE_OPT(KUSB, Kern::Printf(" > 'Transmit Short Packet' explicitly"));
  1413 			// TO DO: Arrange for the sending of a ZLP here.
  1414 			ep->iZlpReqd = EFalse;
  1415 			}
  1416 		else
  1417 			{
  1418 			__KTRACE_OPT(KUSB, Kern::Printf(" > All data sent: %d --> completing", len));
  1419 			ep->iTxBuf = NULL;
  1420 			ep->iRequest->iTxBytes = ep->iTransmitted;
  1421 			ep->iRequest->iError = KErrNone;
  1422 			EndpointRequestComplete(ep->iRequest);
  1423 			ep->iRequest = NULL;
  1424 			}
  1425 		}
  1426 	else
  1427 		{
  1428 		buf += t;
  1429 		TInt left = len - t;								// left in total
  1430 		TInt n = (left >= KBlkMaxPktSz) ? KBlkMaxPktSz : left; // now to be transmitted
  1431 		__KTRACE_OPT(KUSB, Kern::Printf(" > About to send %d bytes (%d bytes left in total)", n, left));
  1432 
  1433 		// TO DO: Write data into Tx FIFO from 'buf' here.
  1434 
  1435 		ep->iTransmitted += n;
  1436 		ep->iPackets++;										// only used for (len == 0) case
  1437 		left -= n;											// (still) left in total
  1438 		if (n < KBlkMaxPktSz)
  1439 			{
  1440 			__KTRACE_OPT(KUSB, Kern::Printf(" > 'Transmit Short Packet' implicitly"));
  1441 			// TO DO: Arrange for the sending of a ZLP here.
  1442 			ep->iZlpReqd = EFalse;
  1443 			}
  1444 		// If double-buffering is available, it might be possible to stick a second packet
  1445 		// into the FIFO here.
  1446 
  1447 		// TO DO (optional): Send another packet if possible (& available) here.
  1448 		}
  1449 
  1450 	ClearEndpointInterrupt(aEndpoint);
  1451 	}
  1452 
  1453 
  1454 
  1455 void TTemplateAsspUsbcc::BulkReceive(TInt aEndpoint)
  1456 //
  1457 // Endpoint 2 (BULK OUT) (This one is called in an ISR.)
  1458 //
  1459 	{
  1460 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::BulkReceive(%d)", aEndpoint));
  1461 
  1462 	// TO DO: Enquire about Ep status here.
  1463 	const TUint32 status = *(TUint32*)0xdefaced;			// bogus
  1464 
  1465 	const TInt idx = 4;										// only in our special case of course!
  1466 	TEndpoint* const ep = &iEndpoints[idx];
  1467 	TUint8* buf = ep->iRxBuf;
  1468 	if (!buf)
  1469 		{
  1470 		__KTRACE_OPT(KUSB, Kern::Printf(" > No Rx buffer available: setting iNoBuffer"));
  1471 		ep->iNoBuffer = ETrue;
  1472 		DisableEndpointInterrupt(aEndpoint);
  1473 		ep->iDisabled = ETrue;
  1474 		ClearEndpointInterrupt(aEndpoint);
  1475 		return;
  1476 		}
  1477 	TInt bytes = 0;
  1478 	const TInt r = ep->iReceived;							// already received
  1479 	// TO DO: Check whether a ZLP was received here:
  1480 	if (status & 1)											// some condition
  1481 		{
  1482 		__KTRACE_OPT(KUSB, Kern::Printf(" > received zero-length packet"));
  1483 		}
  1484 	else if (status & 2)									// some other condition
  1485 		{
  1486 		// TO DO: Get number of bytes received here.
  1487 		bytes = *(TUint32*)0xdadadada;						// bogus
  1488 		__KTRACE_OPT(KUSB, Kern::Printf(" > Bulk received: %d bytes", bytes));
  1489 		if (r + bytes > ep->iLength)
  1490 			{
  1491 			__KTRACE_OPT(KUSB, Kern::Printf(" > not enough space in rx buffer: setting iNoBuffer"));
  1492 			ep->iNoBuffer = ETrue;
  1493 			StopRxTimer(ep);
  1494 			*ep->iPacketSize = ep->iReceived;
  1495 			RxComplete(ep);
  1496 
  1497 			// TO DO (optional): Clear Ep Rx condition flags here.
  1498 
  1499 			ClearEndpointInterrupt(aEndpoint);
  1500 			return;
  1501 			}
  1502 		buf += r;											// set buffer pointer
  1503 
  1504 		// TO DO: Read 'bytes' bytes from Rx FIFO into 'buf' here.
  1505 
  1506 		ep->iReceived += bytes;
  1507 		}
  1508 	else
  1509 		{
  1510 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Inconsistent Ep%d state", aEndpoint));
  1511 
  1512 		// TO DO (optional): Clear Ep Rx condition flags here.
  1513 
  1514 		ClearEndpointInterrupt(aEndpoint);
  1515 		return;
  1516 		}
  1517 
  1518 	if (bytes == 0)
  1519 		{
  1520 		// ZLPs must be recorded separately
  1521 		const TInt i = ep->iReceived ? 1 : 0;
  1522 		ep->iPacketIndex[i] = r;
  1523 		ep->iPacketSize[i] = 0;
  1524 		// If there were data packets before: total packets reported 1 -> 2
  1525 		ep->iPackets += i;
  1526 		}
  1527 
  1528 	if ((bytes < KBlkMaxPktSz) ||
  1529 		(ep->iReceived == ep->iLength))
  1530 		{
  1531 		StopRxTimer(ep);
  1532 		*ep->iPacketSize = ep->iReceived;
  1533 		RxComplete(ep);
  1534 		// since we have no buffer any longer we disable interrupts:
  1535 		DisableEndpointInterrupt(aEndpoint);
  1536 		ep->iDisabled = ETrue;
  1537 		}
  1538 	else
  1539 		{
  1540 		if (!ep->iRxTimerSet)
  1541 			{
  1542 			__KTRACE_OPT(KUSB, Kern::Printf(" > setting rx timer"));
  1543 			ep->iRxTimerSet = ETrue;
  1544 			ep->iRxTimer.OneShot(KRxTimerTimeout);
  1545 			}
  1546 		else
  1547 			{
  1548 			ep->iRxMoreDataRcvd = ETrue;
  1549 			}
  1550 		}
  1551 
  1552 	// TO DO (optional): Clear Ep Rx condition flags here.
  1553 
  1554 	ClearEndpointInterrupt(aEndpoint);
  1555 	}
  1556 
  1557 
  1558 void TTemplateAsspUsbcc::BulkReadRxFifo(TInt aEndpoint)
  1559 //
  1560 // Endpoint 2 (BULK OUT) (This one is called w/o interrupt to be served.)
  1561 //
  1562 	{
  1563 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::BulkReadRxFifo(%d)", aEndpoint));
  1564 
  1565 	// TO DO: Enquire about Ep status here.
  1566 	const TUint32 status = *(TUint32*)0xdefaced;			// bogus
  1567 
  1568 	const TInt idx = 4;										// only in our special case of course!
  1569 	TEndpoint* const ep = &iEndpoints[idx];
  1570 	TUint8* buf = ep->iRxBuf;
  1571 	if (!buf)
  1572 		{
  1573 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: No Rx buffer has been set up"));
  1574 		return;
  1575 		}
  1576 	TInt bytes = 0;
  1577 	const TInt r = ep->iReceived;							// already received
  1578 	// TO DO: Check whether a ZLP was received here:
  1579 	if (status & 1)											// some condition
  1580 		{
  1581 		__KTRACE_OPT(KUSB, Kern::Printf(" > received zero-length packet"));
  1582 		}
  1583 	else if (status & 2)									// some other condition
  1584 		{
  1585 		// TO DO: Get number of bytes received here.
  1586 		bytes = *(TUint32*)0xdadadada;						// bogus
  1587 		__KTRACE_OPT(KUSB, Kern::Printf(" > Bulk received: %d bytes", bytes));
  1588 		if (r + bytes > ep->iLength)
  1589 			{
  1590 			__KTRACE_OPT(KUSB, Kern::Printf(" > not enough space in rx buffer: setting iNoBuffer"));
  1591 			ep->iNoBuffer = ETrue;
  1592 			*ep->iPacketSize = ep->iReceived;
  1593 			RxComplete(ep);
  1594 			return;
  1595 			}
  1596 		buf += r;											// set buffer pointer
  1597 
  1598 		// TO DO: Read 'bytes' bytes from Rx FIFO into 'buf' here.
  1599 
  1600 		ep->iReceived += bytes;
  1601 		}
  1602 	else
  1603 		{
  1604 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Inconsistent Ep%d state", aEndpoint));
  1605 		return;
  1606 		}
  1607 
  1608 	if (bytes == 0)
  1609 		{
  1610 		// ZLPs must be recorded separately
  1611 		const TInt i = ep->iReceived ? 1 : 0;
  1612 		ep->iPacketIndex[i] = r;
  1613 		ep->iPacketSize[i] = 0;
  1614 		// If there were data packets before: total packets reported 1 -> 2
  1615 		ep->iPackets += i;
  1616 		}
  1617 
  1618 	if ((bytes < KBlkMaxPktSz) ||
  1619 		(ep->iReceived == ep->iLength))
  1620 		{
  1621 		*ep->iPacketSize = ep->iReceived;
  1622 		RxComplete(ep);
  1623 		}
  1624 	else
  1625 		{
  1626 		if (!ep->iRxTimerSet)
  1627 			{
  1628 			__KTRACE_OPT(KUSB, Kern::Printf(" > setting rx timer"));
  1629 			ep->iRxTimerSet = ETrue;
  1630 			ep->iRxTimer.OneShot(KRxTimerTimeout);
  1631 			}
  1632 		else
  1633 			{
  1634 			ep->iRxMoreDataRcvd = ETrue;
  1635 			}
  1636 		}
  1637 
  1638 	// TO DO (optional): Clear Ep Rx condition flags here.
  1639 
  1640 	}
  1641 
  1642 
  1643 void TTemplateAsspUsbcc::IsoTransmit(TInt aEndpoint)
  1644 //
  1645 // Endpoint 3 (ISOCHRONOUS IN).
  1646 //
  1647 	{
  1648 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::IsoTransmit(%d)", aEndpoint));
  1649 
  1650 	// TO DO: Write data to endpoint FIFO. Might be similar to BulkTransmit.
  1651 
  1652 	}
  1653 
  1654 
  1655 void TTemplateAsspUsbcc::IsoReceive(TInt aEndpoint)
  1656 //
  1657 // Endpoint 4 (ISOCHRONOUS OUT) (This one is called in an ISR.)
  1658 //
  1659 	{
  1660 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::IsoReceive(%d)", aEndpoint));
  1661 
  1662 	// TO DO: Read data from endpoint FIFO. Might be similar to BulkReceive.
  1663 	}
  1664 
  1665 
  1666 void TTemplateAsspUsbcc::IsoReadRxFifo(TInt aEndpoint)
  1667 //
  1668 // Endpoint 4 (ISOCHRONOUS OUT) (This one is called w/o interrupt to be served.)
  1669 //
  1670 	{
  1671 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::IsoReadRxFifo(%d)", aEndpoint));
  1672 
  1673 	// TO DO: Read data from endpoint FIFO. Might be similar to BulkReadRxFifo.
  1674 	}
  1675 
  1676 
  1677 void TTemplateAsspUsbcc::IntTransmit(TInt aEndpoint)
  1678 //
  1679 // Endpoint 5 (INTERRUPT IN).
  1680 //
  1681 	{
  1682 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::IntTransmit(%d)", aEndpoint));
  1683 
  1684 	// TO DO: Write data to endpoint FIFO. Might be similar to BulkTransmit.
  1685 	}
  1686 
  1687 
  1688 void TTemplateAsspUsbcc::RxComplete(TEndpoint* aEndpoint)
  1689 //
  1690 // Called at the end of an Rx (OUT) transfer to complete to the PIL.
  1691 //
  1692 	{
  1693 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::RxComplete"));
  1694 
  1695 	TUsbcRequestCallback* const req = aEndpoint->iRequest;
  1696 
  1697 	__ASSERT_DEBUG((req != NULL), Kern::Fault(KUsbPanicCat, __LINE__));
  1698 
  1699 	aEndpoint->iRxBuf = NULL;
  1700 	aEndpoint->iRxTimerSet = EFalse;
  1701 	aEndpoint->iRxMoreDataRcvd = EFalse;
  1702 	req->iRxPackets = aEndpoint->iPackets;
  1703 	req->iError = aEndpoint->iLastError;
  1704 	EndpointRequestComplete(req);
  1705 	aEndpoint->iRequest = NULL;
  1706 	}
  1707 
  1708 
  1709 void TTemplateAsspUsbcc::StopRxTimer(TEndpoint* aEndpoint)
  1710 //
  1711 // Stops (cancels) the Rx timer for an endpoint.
  1712 //
  1713 	{
  1714 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::StopRxTimer"));
  1715 
  1716 	if (aEndpoint->iRxTimerSet)
  1717 		{
  1718 		__KTRACE_OPT(KUSB, Kern::Printf(" > stopping rx timer"));
  1719 		aEndpoint->iRxTimer.Cancel();
  1720 		aEndpoint->iRxTimerSet = EFalse;
  1721 		}
  1722 	}
  1723 
  1724 
  1725 void TTemplateAsspUsbcc::EndpointIntService(TInt aEndpoint)
  1726 //
  1727 // ISR for endpoint interrupts.
  1728 // Note: the aEndpoint here is a "hardware endpoint", not a aRealEndpoint.
  1729 //
  1730 	{
  1731 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::EndpointIntService(%d)", aEndpoint));
  1732 
  1733 	switch (aEndpoint)
  1734 		{
  1735 	case 0:
  1736 		Ep0IntService();
  1737 		break;
  1738 	case 1:
  1739 		BulkTransmit(aEndpoint);
  1740 		break;
  1741 	case 2:
  1742 		BulkReceive(aEndpoint);
  1743 		break;
  1744 	case 3:
  1745 		IsoTransmit(aEndpoint);
  1746 		break;
  1747 	case 4:
  1748 		IsoReceive(aEndpoint);
  1749 		break;
  1750 	case 5:
  1751 		IntTransmit(aEndpoint);
  1752 		break;
  1753 	default:
  1754 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Endpoint not found"));
  1755 		break;
  1756 		}
  1757 	}
  1758 
  1759 
  1760 TInt TTemplateAsspUsbcc::ResetIntService()
  1761 //
  1762 // ISR for a USB Reset event interrupt.
  1763 // This function returns a value which can be used on the calling end to decide how to proceed.
  1764 //
  1765 	{
  1766 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::ResetIntService"));
  1767 
  1768 	// Clear an interrupt:
  1769 	// TO DO: Clear reset interrupt flag here.
  1770 
  1771 	// TO DO (optional): Enquire about special conditions and possibly return here.
  1772 
  1773 	DeviceEventNotification(EUsbEventReset);
  1774 
  1775 	return KErrNone;
  1776 	}
  1777 
  1778 
  1779 void TTemplateAsspUsbcc::SuspendIntService()
  1780 //
  1781 // ISR for a USB Suspend event interrupt.
  1782 //
  1783 	{
  1784 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::SuspendIntService"));
  1785 
  1786 	// Clear an interrupt:
  1787 	// TO DO: Clear suspend interrupt flag here.
  1788 
  1789 	DeviceEventNotification(EUsbEventSuspend);
  1790 	}
  1791 
  1792 
  1793 void TTemplateAsspUsbcc::ResumeIntService()
  1794 //
  1795 // ISR for a USB Resume event interrupt.
  1796 //
  1797 	{
  1798 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::ResumeIntService"));
  1799 
  1800 	// Clear an interrupt:
  1801 	// TO DO: Clear resume interrupt flag here.
  1802 
  1803 	DeviceEventNotification(EUsbEventResume);
  1804 	}
  1805 
  1806 
  1807 void TTemplateAsspUsbcc::SofIntService()
  1808 //
  1809 // ISR for a USB Start-of-Frame event interrupt.
  1810 //
  1811 	{
  1812 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::SofIntService"));
  1813 
  1814 	// Clear an interrupt:
  1815 	// TO DO: Clear SOF interrupt flag here.
  1816 
  1817 	// TO DO (optional): Do something about the SOF condition.
  1818 	}
  1819 
  1820 
  1821 void TTemplateAsspUsbcc::UdcInterruptService()
  1822 //
  1823 // Main UDC ISR - determines the cause of the interrupt, clears the condition, dispatches further for service.
  1824 //
  1825 	{
  1826 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::InterruptService"));
  1827 
  1828 	// TO DO: Find the cause of the interrupt (possibly querying a number of status registers) here.
  1829 
  1830 	// Determine the type of UDC interrupt & then serve it:
  1831 	// (The following operations are of course EXAMPLES only.)
  1832 	volatile const TUint32* const status_reg = (TUint32*) 0xdefaced;
  1833 	const TUint32 status = *status_reg;
  1834 	enum {reset_interrupt, suspend_interrupt, resume_interrupt, sof_interrupt, ep_interrupt};
  1835 
  1836 	// Reset interrupt
  1837 	if (status & reset_interrupt)
  1838 		{
  1839 		ResetIntService();
  1840 		}
  1841 
  1842 	// Suspend interrupt
  1843 	if (status & suspend_interrupt)
  1844 		{
  1845 		SuspendIntService();
  1846 		}
  1847 
  1848 	// Resume interrupt
  1849 	if (status & resume_interrupt)
  1850 		{
  1851 		ResumeIntService();
  1852 		}
  1853 
  1854 	// Start-of-Frame interrupt
  1855 	if (status & sof_interrupt)
  1856 		{
  1857 		SofIntService();
  1858 		}
  1859 
  1860 	// Endpoint interrupt
  1861 	if (status & ep_interrupt)
  1862 		{
  1863 		const TInt ep = status & 0xffff0000;
  1864 			{
  1865 			EndpointIntService(ep);
  1866 			}
  1867 		}
  1868 	}
  1869 
  1870 
  1871 void TTemplateAsspUsbcc::Ep0NextState(TEp0State aNextState)
  1872 //
  1873 // Moves the Ep0 state to aNextState.
  1874 //
  1875 	{
  1876 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::Ep0NextState"));
  1877 
  1878 	iEp0State = aNextState;
  1879 	}
  1880 
  1881 
  1882 void TTemplateAsspUsbcc::UdcIsr(TAny* aPtr)
  1883 //
  1884 // This is the static ASSP first-level UDC interrupt service routine. It dispatches the call to the
  1885 // actual controller's ISR.
  1886 //
  1887 	{
  1888 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::UdcIsr"));
  1889 
  1890 	static_cast<TTemplateAsspUsbcc*>(aPtr)->UdcInterruptService();
  1891 	}
  1892 
  1893 
  1894 TInt TTemplateAsspUsbcc::UsbClientConnectorCallback(TAny* aPtr)
  1895 //
  1896 // This function is called in ISR context by the Variant's UsbClientConnectorInterruptService.
  1897 // (This function is static.)
  1898 //
  1899 	{
  1900 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::UsbClientConnectorCallback"));
  1901 
  1902 	TTemplateAsspUsbcc* const ptr = static_cast<TTemplateAsspUsbcc*>(aPtr);
  1903 	ptr->iCableConnected = ptr->iAssp->UsbClientConnectorInserted();
  1904 #ifdef _DEBUG
  1905 	_LIT(KIns, "inserted");
  1906 	_LIT(KRem, "removed");
  1907 	__KTRACE_OPT(KUSB, Kern::Printf(" > USB cable now %lS", ptr->iCableConnected ? &KIns : &KRem));
  1908 #endif
  1909 	if (ptr->iCableConnected)
  1910 		{
  1911 		ptr->DeviceEventNotification(EUsbEventCableInserted);
  1912 		}
  1913 	else
  1914 		{
  1915 		ptr->DeviceEventNotification(EUsbEventCableRemoved);
  1916 		}
  1917 
  1918 	return KErrNone;
  1919 	}
  1920 
  1921 
  1922 TInt TTemplateAsspUsbcc::SetupUdcInterrupt()
  1923 //
  1924 // Registers and enables the UDC interrupt (ASSP first level interrupt).
  1925 //
  1926 	{
  1927 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::SetupUdcInterrupt"));
  1928 
  1929 	// Register UDC interrupt:
  1930 	const TInt error = Interrupt::Bind(EAsspIntIdUsb, UdcIsr, this);
  1931 	if (error != KErrNone)
  1932 		{
  1933 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Binding UDC interrupt failed"));
  1934 		return error;
  1935 		}
  1936 
  1937 	// Enable UDC interrupt:
  1938 	Interrupt::Enable(EAsspIntIdUsb);
  1939 
  1940 	return KErrNone;
  1941 	}
  1942 
  1943 
  1944 void TTemplateAsspUsbcc::ReleaseUdcInterrupt()
  1945 //
  1946 // Disables and unbinds the UDC interrupt.
  1947 //
  1948 	{
  1949 	__KTRACE_OPT(KUSB, Kern::Printf("TTemplateAsspUsbcc::ReleaseUdcInterrupt"));
  1950 
  1951 	// Disable UDC interrupt:
  1952 	Interrupt::Disable(EAsspIntIdUsb);
  1953 
  1954 	// Unregister UDC interrupt:
  1955 	Interrupt::Unbind(EAsspIntIdUsb);
  1956 	}
  1957 
  1958 
  1959 //
  1960 // --- DLL Exported Function --------------------------------------------------
  1961 //
  1962 
  1963 DECLARE_STANDARD_EXTENSION()
  1964 //
  1965 // Creates and initializes a new USB client controller object on the kernel heap.
  1966 //
  1967 	{
  1968 	__KTRACE_OPT(KUSB, Kern::Printf(" > Initializing USB client support (Udcc)..."));
  1969 
  1970 	TTemplateAsspUsbcc* const usbcc = new TTemplateAsspUsbcc();
  1971 	if (!usbcc)
  1972 		{
  1973 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Memory allocation for TTemplateAsspUsbcc failed"));
  1974 		return KErrNoMemory;
  1975 		}
  1976 
  1977 	TInt r;
  1978 	if ((r = usbcc->Construct()) != KErrNone)
  1979 		{
  1980 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Construction of TTemplateAsspUsbcc failed (%d)", r));
  1981 		delete usbcc;
  1982 		return r;
  1983 		}
  1984 
  1985 	if (usbcc->RegisterUdc(0) == NULL)
  1986 		{
  1987 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: PIL registration of PSL failed"));
  1988 		delete usbcc;
  1989 		return KErrGeneral;
  1990 		}
  1991 
  1992 	__KTRACE_OPT(KUSB, Kern::Printf(" > Initializing USB client support: Done"));
  1993 
  1994 	return KErrNone;
  1995 	}
  1996 
  1997 
  1998 // --- EOF --------------------------------------------------------------------