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