kernel/eka/drivers/usbcc/chapter9.cpp
changeset 259 57b9594f5772
parent 139 95f71bcdcdb7
child 257 3e88ff8f41d5
equal deleted inserted replaced
247:d8d70de2bd36 259:57b9594f5772
     1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    21  @file chapter9.cpp
    21  @file chapter9.cpp
    22  @internalTechnology
    22  @internalTechnology
    23 */
    23 */
    24 
    24 
    25 #include <drivers/usbc.h>
    25 #include <drivers/usbc.h>
       
    26 #include "OstTraceDefinitions.h"
       
    27 #ifdef OST_TRACE_COMPILER_IN_USE
       
    28 #include "chapter9Traces.h"
       
    29 #endif
       
    30 
    26 
    31 
    27 
    32 
    28 //#define ENABLE_EXCESSIVE_DEBUG_OUTPUT
    33 //#define ENABLE_EXCESSIVE_DEBUG_OUTPUT
    29 
    34 
    30 //
    35 //
    59 
    64 
    60 	@publishedPartner @released
    65 	@publishedPartner @released
    61 */
    66 */
    62 TUsbcEp0State DUsbClientController::EnquireEp0NextState(const TUint8* aSetupBuf) const
    67 TUsbcEp0State DUsbClientController::EnquireEp0NextState(const TUint8* aSetupBuf) const
    63 	{
    68 	{
    64 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::EnquireEp0NextState()"));
    69     OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_ENQUIREEP0NEXTSTATE,
       
    70             "DUsbClientController::EnquireEp0NextState()" );
    65 
    71 
    66 	// This function may be called by the PSL from within an ISR -- so we have
    72 	// This function may be called by the PSL from within an ISR -- so we have
    67 	// to take care what we do here (and also in all functions that get called
    73 	// to take care what we do here (and also in all functions that get called
    68 	// from here).
    74 	// from here).
    69 
    75 
    70 	if (SWAP_BYTES_16((reinterpret_cast<const TUint16*>(aSetupBuf)[3])) == 0) // iLength
    76 	if (SWAP_BYTES_16((reinterpret_cast<const TUint16*>(aSetupBuf)[3])) == 0) // iLength
    71 		{
    77 		{
    72 		__KTRACE_OPT(KUSB, Kern::Printf("  --> EEp0StateStatusIn"));
    78 	    OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_ENQUIREEP0NEXTSTATE_DUP1,
       
    79 	            "  --> EEp0StateStatusIn" );
       
    80 
    73 		return EEp0StateStatusIn;							// No-data Control => Status_IN
    81 		return EEp0StateStatusIn;							// No-data Control => Status_IN
    74 		}
    82 		}
    75 	else if ((aSetupBuf[0] & KUsbRequestType_DirMask) == KUsbRequestType_DirToDev)
    83 	else if ((aSetupBuf[0] & KUsbRequestType_DirMask) == KUsbRequestType_DirToDev)
    76 		{
    84 		{
    77 		__KTRACE_OPT(KUSB, Kern::Printf("  --> EEp0StateDataOut"));
    85         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_ENQUIREEP0NEXTSTATE_DUP2,
       
    86 		        "  --> EEp0StateDataOut" );
    78 		return EEp0StateDataOut;							// Control Write => Data_OUT
    87 		return EEp0StateDataOut;							// Control Write => Data_OUT
    79 		}
    88 		}
    80 	else
    89 	else
    81 		{
    90 		{
    82 		__KTRACE_OPT(KUSB, Kern::Printf("  --> EEp0StateDataIn"));
    91         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_ENQUIREEP0NEXTSTATE_DUP3,
       
    92 		        "  --> EEp0StateDataIn" );
    83 		return EEp0StateDataIn;								// Control Read => Data_IN
    93 		return EEp0StateDataIn;								// Control Read => Data_IN
    84 		}
    94 		}
    85 	}
    95 	}
    86 
    96 
    87 
    97 
    88 TInt DUsbClientController::ProcessEp0ReceiveDone(TInt aCount)
    98 TInt DUsbClientController::ProcessEp0ReceiveDone(TInt aCount)
    89 	{
    99 	{
    90 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessEp0ReceiveDone()"));
   100 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0RECEIVEDONE,
       
   101 	        "DUsbClientController::ProcessEp0ReceiveDone()" );
    91 	TInt r;
   102 	TInt r;
    92 	if (iEp0DataReceiving == EFalse)
   103 	if (iEp0DataReceiving == EFalse)
    93 		{
   104 		{
    94 		// It's obviously a Setup packet, so...
   105 		// It's obviously a Setup packet, so...
    95 		r = ProcessEp0SetupReceived(aCount);
   106 		r = ProcessEp0SetupReceived(aCount);
   109 	}
   120 	}
   110 
   121 
   111 
   122 
   112 TInt DUsbClientController::ProcessEp0TransmitDone(TInt aCount, TInt aError)
   123 TInt DUsbClientController::ProcessEp0TransmitDone(TInt aCount, TInt aError)
   113 	{
   124 	{
   114 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessEp0TransmitDone()"));
   125 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0TRANSMITDONE,
       
   126 	        "DUsbClientController::ProcessEp0TransmitDone()" );
   115 	// In any case: there's now no longer a write pending
   127 	// In any case: there's now no longer a write pending
   116 	iEp0WritePending = EFalse;
   128 	iEp0WritePending = EFalse;
   117 	// If it was a client who set up this transmission, we report to that client
   129 	// If it was a client who set up this transmission, we report to that client
   118 	if (iEp0ClientDataTransmitting)
   130 	if (iEp0ClientDataTransmitting)
   119 		{
   131 		{
   125 			p->iError = aError;
   137 			p->iError = aError;
   126 			p->iTxBytes = aCount;
   138 			p->iTxBytes = aCount;
   127 			ProcessDataTransferDone(*p);
   139 			ProcessDataTransferDone(*p);
   128 			return KErrNone;
   140 			return KErrNone;
   129 			}
   141 			}
   130 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: DUsbClientController::ProcessEpTransmitDone: Stalling Ep0"));
   142 	    OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0TRANSMITDONE_DUP1,
       
   143 		        "  Error: DUsbClientController::ProcessEpTransmitDone: Stalling Ep0" );
   131 		StallEndpoint(KEp0_In);								// request not found
   144 		StallEndpoint(KEp0_In);								// request not found
   132 		return KErrNotFound;
   145 		return KErrNotFound;
   133 		}
   146 		}
   134 	// If _we_ sent the data, we simply do nothing here...
   147 	// If _we_ sent the data, we simply do nothing here...
   135 	return KErrNone;
   148 	return KErrNone;
   136 	}
   149 	}
   137 
   150 
   138 
       
   139 #define USB_PROCESS_REQUEST(request) \
       
   140 	if (Process ## request(packet) != KErrNone) \
       
   141 		{ \
       
   142 		__KTRACE_OPT(KUSB, \
       
   143 					 Kern::Printf("  ProcessEp0SetupReceived: Stalling Ep0")); \
       
   144 		StallEndpoint(KEp0_In); \
       
   145 		}
       
   146 
       
   147 TInt DUsbClientController::ProcessEp0SetupReceived(TInt aCount)
   151 TInt DUsbClientController::ProcessEp0SetupReceived(TInt aCount)
   148 	{
   152 	{
   149 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessEp0SetupReceived()"));
   153 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED, 
       
   154 	        "DUsbClientController::ProcessEp0SetupReceived()" );
   150 
   155 
   151 	if (aCount > iEp0MaxPacketSize)
   156 	if (aCount > iEp0MaxPacketSize)
   152 		{
   157 		{
   153 		// Fatal error: too much data!
   158 		// Fatal error: too much data!
   154 		aCount = iEp0MaxPacketSize;
   159 		aCount = iEp0MaxPacketSize;
   158 	TUsbcSetup packet;
   163 	TUsbcSetup packet;
   159 	Buffer2Setup(iEp0_RxBuf, packet);
   164 	Buffer2Setup(iEp0_RxBuf, packet);
   160 
   165 
   161 #if defined(_DEBUG) && defined(ENABLE_EXCESSIVE_DEBUG_OUTPUT)
   166 #if defined(_DEBUG) && defined(ENABLE_EXCESSIVE_DEBUG_OUTPUT)
   162 	// let's see what we've got:
   167 	// let's see what we've got:
   163 	__KTRACE_OPT(KUSB, Kern::Printf("  bmRequestType = 0x%02x", packet.iRequestType));
   168 	OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP1, 
       
   169 	        "  bmRequestType = 0x%02x", packet.iRequestType );
   164 	if ((packet.iRequestType & KUsbRequestType_TypeMask) == KUsbRequestType_TypeStd)
   170 	if ((packet.iRequestType & KUsbRequestType_TypeMask) == KUsbRequestType_TypeStd)
   165 		{
   171 		{
   166 		switch (packet.iRequest)
   172 		switch (packet.iRequest)
   167 			{
   173 			{
   168 		case KUsbRequest_GetStatus:
   174 		case KUsbRequest_GetStatus:
   169 			__KTRACE_OPT(KUSB, Kern::Printf("  bRequest      = 0x%02x (GET_STATUS)",
   175 		    OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP2, 
   170 											KUsbRequest_GetStatus));
   176 		            "  bRequest      = 0x%02x (GET_STATUS)", KUsbRequest_GetStatus );
   171 			break;
   177 			break;
   172 		case KUsbRequest_ClearFeature:
   178 		case KUsbRequest_ClearFeature:
   173 			__KTRACE_OPT(KUSB, Kern::Printf("  bRequest      = 0x%02x (CLEAR_FEATURE)",
   179 		    OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP3, 
   174 											KUsbRequest_ClearFeature));
   180 		            "  bRequest      = 0x%02x (CLEAR_FEATURE)", KUsbRequest_ClearFeature );
   175 			break;
   181 			break;
   176 		case KUsbRequest_SetFeature:
   182 		case KUsbRequest_SetFeature:
   177 			__KTRACE_OPT(KUSB, Kern::Printf("  bRequest      = 0x%02x (SET_FEATURE)",
   183 		    OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP4, 
   178 											KUsbRequest_SetFeature));
   184 		            "  bRequest      = 0x%02x (SET_FEATURE)", KUsbRequest_SetFeature );
   179 			break;
   185 			break;
   180 		case KUsbRequest_SetAddress:
   186 		case KUsbRequest_SetAddress:
   181 			__KTRACE_OPT(KUSB, Kern::Printf("  bRequest      = 0x%02x (SET_ADDRESS)",
   187 		    OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP5, 
   182 											KUsbRequest_SetAddress));
   188 		            "  bRequest      = 0x%02x (SET_ADDRESS)", KUsbRequest_SetAddress );
   183 			break;
   189 			break;
   184 		case KUsbRequest_GetDescriptor:
   190 		case KUsbRequest_GetDescriptor:
   185 			__KTRACE_OPT(KUSB, Kern::Printf("  bRequest      = 0x%02x (GET_DESCRIPTOR)",
   191 		    OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP6, 
   186 											KUsbRequest_GetDescriptor));
   192 		            "  bRequest      = 0x%02x (GET_DESCRIPTOR)", KUsbRequest_GetDescriptor );
   187 			break;
   193 			break;
   188 		case KUsbRequest_SetDescriptor:
   194 		case KUsbRequest_SetDescriptor:
   189 			__KTRACE_OPT(KUSB, Kern::Printf("  bRequest      = 0x%02x (SET_DESCRIPTOR)",
   195 		    OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP7, 
   190 											KUsbRequest_SetDescriptor));
   196 		            "  bRequest      = 0x%02x (SET_DESCRIPTOR)", KUsbRequest_SetDescriptor );
   191 			break;
   197 			break;
   192 		case KUsbRequest_GetConfig:
   198 		case KUsbRequest_GetConfig:
   193 			__KTRACE_OPT(KUSB, Kern::Printf("  bRequest      = 0x%02x (GET_CONFIGURATION)",
   199 		    OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP8, 
   194 											KUsbRequest_GetConfig));
   200 		            "  bRequest      = 0x%02x (GET_CONFIGURATION)", KUsbRequest_GetConfig );
   195 			break;
   201 			break;
   196 		case KUsbRequest_SetConfig:
   202 		case KUsbRequest_SetConfig:
   197 			__KTRACE_OPT(KUSB, Kern::Printf("  bRequest      = 0x%02x (SET_CONFIGURATION)",
   203 		    OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP9, 
   198 											KUsbRequest_SetConfig));
   204 		            "  bRequest      = 0x%02x (SET_CONFIGURATION)", KUsbRequest_SetConfig );
   199 			break;
   205 			break;
   200 		case KUsbRequest_GetInterface:
   206 		case KUsbRequest_GetInterface:
   201 			__KTRACE_OPT(KUSB, Kern::Printf("  bRequest      = 0x%02x (GET_INTERFACE)",
   207 		    OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP10, 
   202 											KUsbRequest_GetInterface));
   208 		            "  bRequest      = 0x%02x (GET_INTERFACE)", KUsbRequest_GetInterface );
   203 			break;
   209 			break;
   204 		case KUsbRequest_SetInterface:
   210 		case KUsbRequest_SetInterface:
   205 			__KTRACE_OPT(KUSB, Kern::Printf("  bRequest      = 0x%02x (SET_INTERFACE)",
   211 		    OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP11, 
   206 											KUsbRequest_SetInterface));
   212 		            "  bRequest      = 0x%02x (SET_INTERFACE)", KUsbRequest_SetInterface );
   207 			break;
   213 			break;
   208 		case KUsbRequest_SynchFrame:
   214 		case KUsbRequest_SynchFrame:
   209 			__KTRACE_OPT(KUSB, Kern::Printf("  bRequest      = 0x%02x (SYNCH_FRAME)",
   215 		    OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP12, 
   210 											KUsbRequest_SynchFrame));
   216 		            "  bRequest      = 0x%02x (SYNCH_FRAME)", KUsbRequest_SynchFrame );
   211 			break;
   217 			break;
   212 		default:
   218 		default:
   213 			__KTRACE_OPT(KPANIC, Kern::Printf("  Error: bRequest = 0x%02x (UNKNWON STANDARD REQUEST)",
   219 		    OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP13, 
   214 											  packet.iRequest));
   220 		            "  Error: bRequest = 0x%02x (UNKNWON STANDARD REQUEST)", packet.iRequest );
   215 			break;
   221 			break;
   216 			}
   222 			}
   217 		}
   223 		}
   218 	else
   224 	else
   219 		{
   225 		{
   220 		__KTRACE_OPT(KUSB, Kern::Printf("  bRequest      = 0x%02x (NON-STANDARD REQUEST)",
   226         OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP14, 
   221 										packet.iRequest));
   227                 "  bRequest      = 0x%02x (NON-STANDARD REQUEST)", packet.iRequest );
   222 		}
   228 		}
   223 	__KTRACE_OPT(KUSB, Kern::Printf("  wValue        = 0x%04x", packet.iValue));
   229 	OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP15, 
   224 	__KTRACE_OPT(KUSB, Kern::Printf("  wIndex        = 0x%04x", packet.iIndex));
   230 	        "  wValue        = 0x%04x", packet.iValue );
   225 	__KTRACE_OPT(KUSB, Kern::Printf("  wLength       = 0x%04x", packet.iLength));
   231 	OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP16, 
       
   232 	        "  wIndex        = 0x%04x", packet.iIndex );
       
   233 	OstTraceDef1( OST_TRACE_CATEGORY_DEBUG, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP17, 
       
   234 	        "  wLength       = 0x%04x", packet.iLength );
   226 #endif // defined(_DEBUG) && defined(ENABLE_EXCESSIVE_DEBUG_OUTPUT)
   235 #endif // defined(_DEBUG) && defined(ENABLE_EXCESSIVE_DEBUG_OUTPUT)
   227 
   236 
   228 	// now the actual analysis
   237 	// now the actual analysis
   229 	if ((packet.iRequestType & KUsbRequestType_TypeMask) == KUsbRequestType_TypeStd)
   238 	if ((packet.iRequestType & KUsbRequestType_TypeMask) == KUsbRequestType_TypeStd)
   230 		{
   239 		{
   233 			{
   242 			{
   234 		case KUsbRequest_GetStatus:
   243 		case KUsbRequest_GetStatus:
   235 			switch (packet.iRequestType & KUsbRequestType_DestMask)
   244 			switch (packet.iRequestType & KUsbRequestType_DestMask)
   236 				{ // Recipient
   245 				{ // Recipient
   237 			case KUsbRequestType_DestDevice:
   246 			case KUsbRequestType_DestDevice:
   238 				USB_PROCESS_REQUEST(GetDeviceStatus);
   247 			    if (ProcessGetDeviceStatus(packet) != KErrNone)
       
   248 			        {
       
   249                     OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP18, 
       
   250                             "ProcessEp0SetupReceived: Stalling Ep0" );
       
   251                     StallEndpoint(KEp0_In);
       
   252                     }
   239 				break;
   253 				break;
   240 			case KUsbRequestType_DestIfc:
   254 			case KUsbRequestType_DestIfc:
   241 				USB_PROCESS_REQUEST(GetInterfaceStatus);
   255                 if (ProcessGetInterfaceStatus(packet) != KErrNone)
       
   256                     {
       
   257                     OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP19, 
       
   258                             "ProcessEp0SetupReceived: Stalling Ep0" );
       
   259                     StallEndpoint(KEp0_In);
       
   260                     }
   242 				break;
   261 				break;
   243 			case KUsbRequestType_DestEp:
   262 			case KUsbRequestType_DestEp:
   244 				USB_PROCESS_REQUEST(GetEndpointStatus);
   263                 if (ProcessGetEndpointStatus(packet) != KErrNone)
       
   264                     {
       
   265                     OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP20, 
       
   266                             "ProcessEp0SetupReceived: Stalling Ep0" );
       
   267                     StallEndpoint(KEp0_In);
       
   268                     }
   245 				break;
   269 				break;
   246 			default:
   270 			default:
   247 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: GET STATUS - Other or Unknown recipient"));
   271 			    OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP21, 
   248 				__KTRACE_OPT(KPANIC, Kern::Printf("  -> DUsbClientController::ProcessEp0SetupReceived: "
   272 			            "  Error: GET STATUS - Other or Unknown recipient" );
   249 												  "Stalling Ep0"));
   273 			    OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP22, 
       
   274 			            "  -> DUsbClientController::ProcessEp0SetupReceived: Stalling Ep0" );
   250 				StallEndpoint(KEp0_In);
   275 				StallEndpoint(KEp0_In);
   251 				break;
   276 				break;
   252 				}
   277 				}
   253 			break;
   278 			break;
   254 		case KUsbRequest_ClearFeature:
   279 		case KUsbRequest_ClearFeature:
   255 		case KUsbRequest_SetFeature:
   280 		case KUsbRequest_SetFeature:
   256 			switch (packet.iRequestType & KUsbRequestType_DestMask)
   281 			switch (packet.iRequestType & KUsbRequestType_DestMask)
   257 				{ // Recipient
   282 				{ // Recipient
   258 			case KUsbRequestType_DestDevice:
   283 			case KUsbRequestType_DestDevice:
   259 				USB_PROCESS_REQUEST(SetClearDevFeature);
   284                 if (ProcessSetClearDevFeature(packet) != KErrNone)
       
   285                     {
       
   286                     OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP23, 
       
   287                             "ProcessEp0SetupReceived: Stalling Ep0" );
       
   288                     StallEndpoint(KEp0_In);
       
   289                     }
   260 				break;
   290 				break;
   261 			case KUsbRequestType_DestIfc:
   291 			case KUsbRequestType_DestIfc:
   262 				USB_PROCESS_REQUEST(SetClearIfcFeature);
   292                 if (ProcessSetClearIfcFeature(packet) != KErrNone)
       
   293                     {
       
   294                     OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP24, 
       
   295                             "ProcessEp0SetupReceived: Stalling Ep0" );
       
   296                     StallEndpoint(KEp0_In);
       
   297                     }
   263 				break;
   298 				break;
   264 			case KUsbRequestType_DestEp:
   299 			case KUsbRequestType_DestEp:
   265 				USB_PROCESS_REQUEST(SetClearEpFeature);
   300                 if (ProcessSetClearEpFeature(packet) != KErrNone)
       
   301                     {
       
   302                     OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP25, 
       
   303                             "ProcessEp0SetupReceived: Stalling Ep0" );
       
   304                     StallEndpoint(KEp0_In);
       
   305                     }
   266 				break;
   306 				break;
   267 			default:
   307 			default:
   268 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: SET/CLEAR FEATURE - "
   308 			    OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP26, 
   269 												  "Other or Unknown recipient"));
   309 			            "  Error: SET/CLEAR FEATURE - Other or Unknown recipient" );
   270 				__KTRACE_OPT(KPANIC, Kern::Printf("  -> Stalling Ep0"));
   310 			    OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP27, 
       
   311 			            "  -> Stalling Ep0" );
   271 				StallEndpoint(KEp0_In);
   312 				StallEndpoint(KEp0_In);
   272 				break;
   313 				break;
   273 				}
   314 				}
   274 			break;
   315 			break;
   275 		case KUsbRequest_SetAddress:
   316 		case KUsbRequest_SetAddress:
   276 			USB_PROCESS_REQUEST(SetAddress);
   317             if (ProcessSetAddress(packet) != KErrNone)
       
   318                 {
       
   319                 OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP28, 
       
   320                         "ProcessEp0SetupReceived: Stalling Ep0" );
       
   321                 StallEndpoint(KEp0_In);
       
   322                 }
   277 			break;
   323 			break;
   278 		case KUsbRequest_GetDescriptor:
   324 		case KUsbRequest_GetDescriptor:
   279 			USB_PROCESS_REQUEST(GetDescriptor);
   325             if (ProcessGetDescriptor(packet) != KErrNone)
       
   326                 {
       
   327                 OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP29, 
       
   328                         "ProcessEp0SetupReceived: Stalling Ep0" );
       
   329                 StallEndpoint(KEp0_In);
       
   330                 }
   280 			break;
   331 			break;
   281 		case KUsbRequest_SetDescriptor:
   332 		case KUsbRequest_SetDescriptor:
   282 			USB_PROCESS_REQUEST(SetDescriptor);
   333             if (ProcessSetDescriptor(packet) != KErrNone)
       
   334                 {
       
   335                 OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP30, 
       
   336                         "ProcessEp0SetupReceived: Stalling Ep0" );
       
   337                 StallEndpoint(KEp0_In);
       
   338                 }
   283 			break;
   339 			break;
   284 		case KUsbRequest_GetConfig:
   340 		case KUsbRequest_GetConfig:
   285 			USB_PROCESS_REQUEST(GetConfiguration);
   341             if (ProcessGetConfiguration(packet) != KErrNone)
       
   342                 {
       
   343                 OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP31, 
       
   344                         "ProcessEp0SetupReceived: Stalling Ep0" );
       
   345                 StallEndpoint(KEp0_In);
       
   346                 }
   286 			break;
   347 			break;
   287 		case KUsbRequest_SetConfig:
   348 		case KUsbRequest_SetConfig:
   288 			USB_PROCESS_REQUEST(SetConfiguration);
   349             if (ProcessSetConfiguration(packet) != KErrNone)
       
   350                 {
       
   351                 OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP32, 
       
   352                         "ProcessEp0SetupReceived: Stalling Ep0" );
       
   353                 StallEndpoint(KEp0_In);
       
   354                 }
   289 			break;
   355 			break;
   290 		case KUsbRequest_GetInterface:
   356 		case KUsbRequest_GetInterface:
   291 			USB_PROCESS_REQUEST(GetInterface);
   357             if (ProcessGetInterface(packet) != KErrNone)
       
   358                 {
       
   359                 OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP33, 
       
   360                         "ProcessEp0SetupReceived: Stalling Ep0" );
       
   361                 StallEndpoint(KEp0_In);
       
   362                 }
   292 			break;
   363 			break;
   293 		case KUsbRequest_SetInterface:
   364 		case KUsbRequest_SetInterface:
   294 			USB_PROCESS_REQUEST(SetInterface);
   365             if (ProcessSetInterface(packet) != KErrNone)
       
   366                 {
       
   367                 OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP34, 
       
   368                         "ProcessEp0SetupReceived: Stalling Ep0" );
       
   369                 StallEndpoint(KEp0_In);
       
   370                 }
   295 			break;
   371 			break;
   296 		case KUsbRequest_SynchFrame:
   372 		case KUsbRequest_SynchFrame:
   297 			USB_PROCESS_REQUEST(SynchFrame);
   373             if (ProcessSynchFrame(packet) != KErrNone)
       
   374                 {
       
   375                 OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP35, 
       
   376                         "ProcessEp0SetupReceived: Stalling Ep0" );
       
   377                 StallEndpoint(KEp0_In);
       
   378                 }
   298 			break;
   379 			break;
   299 		default:
   380 		default:
   300 			__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Unknown/unsupported Std Setup Request"));
   381 		    OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP36, 
   301 			__KTRACE_OPT(KPANIC, Kern::Printf("  -> Stalling Ep0"));
   382 		            "  Error: Unknown/unsupported Std Setup Request" );
       
   383 		    OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP37, 
       
   384 		            "  -> Stalling Ep0" );
   302 			StallEndpoint(KEp0_In);
   385 			StallEndpoint(KEp0_In);
   303 			break;
   386 			break;
   304 			}
   387 			}
   305 		}
   388 		}
   306 	else
   389 	else
   319 		        {
   402 		        {
   320 		        NKern::FMWait(&iMutex);
   403 		        NKern::FMWait(&iMutex);
   321 		        }
   404 		        }
   322 			if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
   405 			if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
   323 				{
   406 				{
   324 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
   407                 OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP38, 
       
   408                         "  Error: Invalid device state" );
   325 				}
   409 				}
   326 			else
   410 			else
   327 				{
   411 				{
   328 				const TUsbcInterfaceSet* const ifcset_ptr =
   412 				const TUsbcInterfaceSet* const ifcset_ptr =
   329 					InterfaceNumber2InterfacePointer(packet.iIndex);
   413 					InterfaceNumber2InterfacePointer(packet.iIndex);
   332 				//0 != ifcset_ptr->iInterfaces.Count() here.
   416 				//0 != ifcset_ptr->iInterfaces.Count() here.
   333 				if (ifcset_ptr && 0 != ifcset_ptr->iInterfaces.Count())
   417 				if (ifcset_ptr && 0 != ifcset_ptr->iInterfaces.Count())
   334 					{
   418 					{
   335 					if (ifcset_ptr->CurrentInterface()->iNoEp0Requests)
   419 					if (ifcset_ptr->CurrentInterface()->iNoEp0Requests)
   336 						{
   420 						{
   337 						__KTRACE_OPT(KUSB, Kern::Printf("  Recipient says: NoEp0RequestsPlease"));
   421                         OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP39, 
       
   422                                 "  Recipient says: NoEp0RequestsPlease" );
   338 						}
   423 						}
   339 					else
   424 					else
   340 						{
   425 						{
   341 						client = ifcset_ptr->iClientId;
   426 						client = ifcset_ptr->iClientId;
   342 						}
   427 						}
   343 					}
   428 					}
   344 				else
   429 				else
   345 					{
   430 					{
   346 					__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Interface 0x%02x does not exist",
   431                     OstTraceDef1(OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP40, 
   347 													  packet.iIndex));
   432                             "  Error: Interface 0x%02x does not exist", packet.iIndex );
   348 					}
   433 					}
   349 				}
   434 				}
   350 	        if (NKern::CurrentContext() == EThread)
   435 	        if (NKern::CurrentContext() == EThread)
   351 	            {
   436 	            {
   352                 NKern::FMSignal(&iMutex);
   437                 NKern::FMSignal(&iMutex);
   358 	            {
   443 	            {
   359                 NKern::FMWait(&iMutex);
   444                 NKern::FMWait(&iMutex);
   360 	            }
   445 	            }
   361 			if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
   446 			if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
   362 				{
   447 				{
   363 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
   448                 OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP41, 
       
   449                         "  Error: Invalid device state" );
   364 				}
   450 				}
   365 			else if (EndpointExists(packet.iIndex) == EFalse)
   451 			else if (EndpointExists(packet.iIndex) == EFalse)
   366 				{
   452 				{
   367 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Endpoint 0x%02x does not exist",
   453                 OstTraceDef1(OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP42, 
   368 												  packet.iIndex));
   454                         "  Error: Endpoint 0x%02x does not exist", packet.iIndex );
   369 				}
   455 				}
   370 			else
   456 			else
   371 				{
   457 				{
   372 				const TInt idx = EpAddr2Idx(packet.iIndex);
   458 				const TInt idx = EpAddr2Idx(packet.iIndex);
   373 				const TUsbcInterfaceSet* const ifcset_ptr =
   459 				const TUsbcInterfaceSet* const ifcset_ptr =
   374 					iRealEndpoints[idx].iLEndpoint->iInterface->iInterfaceSet;
   460 					iRealEndpoints[idx].iLEndpoint->iInterface->iInterfaceSet;
   375 				if (ifcset_ptr->CurrentInterface()->iNoEp0Requests)
   461 				if (ifcset_ptr->CurrentInterface()->iNoEp0Requests)
   376 					{
   462 					{
   377 					__KTRACE_OPT(KUSB, Kern::Printf("  Recipient says: NoEp0RequestsPlease"));
   463                     OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP43, 
       
   464                             "  Recipient says: NoEp0RequestsPlease" );
   378 					}
   465 					}
   379 				else
   466 				else
   380 					{
   467 					{
   381 					client = ifcset_ptr->iClientId;
   468 					client = ifcset_ptr->iClientId;
   382 					}
   469 					}
   385                 {
   472                 {
   386                 NKern::FMSignal(&iMutex);
   473                 NKern::FMSignal(&iMutex);
   387                 }
   474                 }
   388 			break;
   475 			break;
   389 		default:
   476 		default:
   390 			__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Other or Unknown recipient"));
   477 		    OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP44, 
       
   478 		            "  Error: Other or Unknown recipient" );
   391 			break;
   479 			break;
   392 			}
   480 			}
   393 		if (client != NULL)
   481 		if (client != NULL)
   394 			{
   482 			{
   395 			// Try to relay packet to the appropriate recipient
   483 			// Try to relay packet to the appropriate recipient
   399 				{
   487 				{
   400 				if (p->Owner() == client)
   488 				if (p->Owner() == client)
   401 					{
   489 					{
   402 					__ASSERT_DEBUG((p->iEndpointNum == 0), Kern::Fault(KUsbPILPanicCat, __LINE__));
   490 					__ASSERT_DEBUG((p->iEndpointNum == 0), Kern::Fault(KUsbPILPanicCat, __LINE__));
   403 					__ASSERT_DEBUG((p->iTransferDir == EControllerRead), Kern::Fault(KUsbPILPanicCat, __LINE__));
   491 					__ASSERT_DEBUG((p->iTransferDir == EControllerRead), Kern::Fault(KUsbPILPanicCat, __LINE__));
   404 					__KTRACE_OPT(KUSB, Kern::Printf("  Found Ep0 read request"));
   492 					OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP45, 
       
   493 					        "  Found Ep0 read request" );
   405 					if (packet.iLength != 0)
   494 					if (packet.iLength != 0)
   406 						{
   495 						{
   407 						if ((packet.iRequestType & KUsbRequestType_DirMask) == KUsbRequestType_DirToDev)
   496 						if ((packet.iRequestType & KUsbRequestType_DirMask) == KUsbRequestType_DirToDev)
   408 							{
   497 							{
   409 							// Data transfer & direction OUT => there'll be a DATA_OUT stage
   498 							// Data transfer & direction OUT => there'll be a DATA_OUT stage
   410 							__KTRACE_OPT(KUSB, Kern::Printf("  Next is DATA_OUT: setting up DataOutVars"));
   499                             OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP46, 
       
   500                                     "  Next is DATA_OUT: setting up DataOutVars" );
   411 							SetEp0DataOutVars(packet, client);
   501 							SetEp0DataOutVars(packet, client);
   412 							}
   502 							}
   413 						else if ((packet.iRequestType & KUsbRequestType_DirMask) == KUsbRequestType_DirToHost)
   503 						else if ((packet.iRequestType & KUsbRequestType_DirMask) == KUsbRequestType_DirToHost)
   414 							{
   504 							{
   415 							// For possible later use (ZLP).
   505 							// For possible later use (ZLP).
   423 					*(p->iPacketIndex) = 0;
   513 					*(p->iPacketIndex) = 0;
   424 					ProcessDataTransferDone(*p);
   514 					ProcessDataTransferDone(*p);
   425 					return KErrNone;
   515 					return KErrNone;
   426 					}
   516 					}
   427 				}
   517 				}
   428 			__KTRACE_OPT(KUSB, Kern::Printf("  Ep0 read request not found: setting RxExtra vars (Setup)"));
   518 			OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP47, 
       
   519 			        "  Ep0 read request not found: setting RxExtra vars (Setup)" );
   429 			iEp0_RxExtraCount = aCount;
   520 			iEp0_RxExtraCount = aCount;
   430 			iEp0_RxExtraData = ETrue;
   521 			iEp0_RxExtraData = ETrue;
   431 			return KErrNotFound;
   522 			return KErrNotFound;
   432 			}
   523 			}
   433 		else // if (client == NULL)
   524 		else // if (client == NULL)
   434 			{
   525 			{
   435 			__KTRACE_OPT(KPANIC, Kern::Printf("  Ep0 request error: Stalling Ep0"));
   526             OstTraceDef0(OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0SETUPRECEIVED_DUP48, 
       
   527                     "  Ep0 request error: Stalling Ep0" );
   436 			StallEndpoint(KEp0_In);
   528 			StallEndpoint(KEp0_In);
   437 			return KErrGeneral;
   529 			return KErrGeneral;
   438 			}
   530 			}
   439 		}
   531 		}
   440 	return KErrNone;
   532 	return KErrNone;
   441 	}
   533 	}
   442 
   534 
   443 #undef USB_PROCESS_REQUEST
       
   444 
       
   445 
       
   446 TInt DUsbClientController::ProcessEp0DataReceived(TInt aCount)
   535 TInt DUsbClientController::ProcessEp0DataReceived(TInt aCount)
   447 	{
   536 	{
   448 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessEp0DataReceived()"));
   537 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSEP0DATARECEIVED, 
   449 
   538 	        "DUsbClientController::ProcessEp0DataReceived()" );
   450 	__KTRACE_OPT(KUSB, Kern::Printf("  : %d bytes", aCount));
   539 	OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0DATARECEIVED_DUP1, 
   451 
   540 	        "  : %d bytes", aCount );
   452 	if (aCount > iEp0MaxPacketSize)
   541 	if (aCount > iEp0MaxPacketSize)
   453 		{
   542 		{
   454 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Too much data"));
   543         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0DATARECEIVED_DUP2, 
       
   544                 "  Error: Too much data" );
   455 		aCount = iEp0MaxPacketSize;
   545 		aCount = iEp0MaxPacketSize;
   456 		}
   546 		}
   457 	iEp0DataReceived += aCount;
   547 	iEp0DataReceived += aCount;
   458 	if (iEp0ClientId == NULL)
   548 	if (iEp0ClientId == NULL)
   459 		{
   549 		{
   465 			memcpy(iEp0_RxCollectionBuf + iEp0DataReceived, iEp0_RxBuf, aCount);
   555 			memcpy(iEp0_RxCollectionBuf + iEp0DataReceived, iEp0_RxBuf, aCount);
   466 			ProceedSetDescriptor();
   556 			ProceedSetDescriptor();
   467 			break;
   557 			break;
   468 #endif
   558 #endif
   469 		default:
   559 		default:
   470 			__KTRACE_OPT(KPANIC, Kern::Printf("  Error: invalid request in iSetup"));
   560             OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0DATARECEIVED_DUP3, 
   471 			__KTRACE_OPT(KPANIC, Kern::Printf("  -> DUsbClientController::ProcessEp0DataReceived: Stalling Ep0"));
   561                     "  Error: invalid request in iSetup" );
       
   562             OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSEP0DATARECEIVED_DUP4, 
       
   563                     "  -> DUsbClientController::ProcessEp0DataReceived: Stalling Ep0" );
   472 			StallEndpoint(KEp0_In);
   564 			StallEndpoint(KEp0_In);
   473 			ResetEp0DataOutVars();
   565 			ResetEp0DataOutVars();
   474 			break;
   566 			break;
   475 			}
   567 			}
   476 		}
   568 		}
   483 			{
   575 			{
   484 			if (p->Owner() == iEp0ClientId)
   576 			if (p->Owner() == iEp0ClientId)
   485 				{
   577 				{
   486 				__ASSERT_DEBUG((p->iEndpointNum == 0), Kern::Fault(KUsbPILPanicCat, __LINE__));
   578 				__ASSERT_DEBUG((p->iEndpointNum == 0), Kern::Fault(KUsbPILPanicCat, __LINE__));
   487 				__ASSERT_DEBUG((p->iTransferDir == EControllerRead), Kern::Fault(KUsbPILPanicCat, __LINE__));
   579 				__ASSERT_DEBUG((p->iTransferDir == EControllerRead), Kern::Fault(KUsbPILPanicCat, __LINE__));
   488 				__KTRACE_OPT(KUSB, Kern::Printf("  Found Ep0 read request"));
   580 	            OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0DATARECEIVED_DUP5,
       
   581 	                    "  Found Ep0 read request" );
   489 				memcpy(p->iBufferStart, iEp0_RxBuf, aCount);
   582 				memcpy(p->iBufferStart, iEp0_RxBuf, aCount);
   490 				p->iError = KErrNone;						// if it wasn't 'KErrNone' we wouldn't be here
   583 				p->iError = KErrNone;						// if it wasn't 'KErrNone' we wouldn't be here
   491 				*(p->iPacketSize) = aCount;
   584 				*(p->iPacketSize) = aCount;
   492 				p->iRxPackets = 1;
   585 				p->iRxPackets = 1;
   493 				*(p->iPacketIndex) = 0;
   586 				*(p->iPacketIndex) = 0;
   494 				ProcessDataTransferDone(*p);
   587 				ProcessDataTransferDone(*p);
   495 				goto found;
   588 				goto found;
   496 				}
   589 				}
   497 			}
   590 			}
   498 		__KTRACE_OPT(KUSB, Kern::Printf("  Ep0 read request not found: setting RxExtra vars (Data)"));
   591         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSEP0DATARECEIVED_DUP6,
       
   592                 "  Ep0 read request not found: setting RxExtra vars (Data)" );
   499 		iEp0_RxExtraCount = aCount;
   593 		iEp0_RxExtraCount = aCount;
   500 		iEp0_RxExtraData = ETrue;
   594 		iEp0_RxExtraData = ETrue;
   501 		iEp0DataReceived -= aCount;
   595 		iEp0DataReceived -= aCount;
   502 		return KErrNotFound;
   596 		return KErrNotFound;
   503 		}
   597 		}
   513 
   607 
   514 // --- The USB Spec Chapter 9 Standard Endpoint Zero Device Requests ---
   608 // --- The USB Spec Chapter 9 Standard Endpoint Zero Device Requests ---
   515 
   609 
   516 TInt DUsbClientController::ProcessGetDeviceStatus(const TUsbcSetup& aPacket)
   610 TInt DUsbClientController::ProcessGetDeviceStatus(const TUsbcSetup& aPacket)
   517 	{
   611 	{
   518 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessGetDeviceStatus()"));
   612 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSGETDEVICESTATUS, 
       
   613 	        "DUsbClientController::ProcessGetDeviceStatus()" );
   519 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateAddress)
   614 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateAddress)
   520 		{
   615 		{
   521 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
   616         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSGETDEVICESTATUS_DUP1, 
       
   617                 "  Error: Invalid device state" );
   522 		return KErrGeneral;
   618 		return KErrGeneral;
   523 		}
   619 		}
   524 	const TUint16 status = ((DeviceSelfPowered() ? KUsbDevStat_SelfPowered : 0) |
   620 	const TUint16 status = ((DeviceSelfPowered() ? KUsbDevStat_SelfPowered : 0) |
   525 					  (iRmWakeupStatus_Enabled ? KUsbDevStat_RemoteWakeup : 0));
   621 					  (iRmWakeupStatus_Enabled ? KUsbDevStat_RemoteWakeup : 0));
   526 	__KTRACE_OPT(KUSB, Kern::Printf("  Reporting device status: 0x%02x", status));
   622 	OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSGETDEVICESTATUS_DUP2, 
       
   623 	        "  Reporting device status: 0x%02x", status );
   527 	*reinterpret_cast<TUint16*>(iEp0_TxBuf) = SWAP_BYTES_16(status);
   624 	*reinterpret_cast<TUint16*>(iEp0_TxBuf) = SWAP_BYTES_16(status);
   528 	if (SetupEndpointZeroWrite(iEp0_TxBuf, sizeof(status)) == KErrNone)
   625 	if (SetupEndpointZeroWrite(iEp0_TxBuf, sizeof(status)) == KErrNone)
   529 		{
   626 		{
   530 		iEp0WritePending = ETrue;
   627 		iEp0WritePending = ETrue;
   531 		}
   628 		}
   533 	}
   630 	}
   534 
   631 
   535 
   632 
   536 TInt DUsbClientController::ProcessGetInterfaceStatus(const TUsbcSetup& aPacket)
   633 TInt DUsbClientController::ProcessGetInterfaceStatus(const TUsbcSetup& aPacket)
   537 	{
   634 	{
   538 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessGetInterfaceStatus()"));
   635 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSGETINTERFACESTATUS, 
       
   636 	        "DUsbClientController::ProcessGetInterfaceStatus()" );
   539 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
   637 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
   540 		{
   638 		{
   541 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
   639         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSGETINTERFACESTATUS_DUP1, 
       
   640                 "  Error: Invalid device state" );
   542 		return KErrGeneral;
   641 		return KErrGeneral;
   543 		}
   642 		}
   544 	if (InterfaceExists(aPacket.iIndex) == EFalse)
   643 	if (InterfaceExists(aPacket.iIndex) == EFalse)
   545 		{
   644 		{
   546 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Interface does not exist"));
   645         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSGETINTERFACESTATUS_DUP2, 
       
   646                 "  Error: Interface does not exist" );
   547 		return KErrGeneral;
   647 		return KErrGeneral;
   548 		}
   648 		}
   549 	const TUint16 status = 0x0000;							// as of USB Spec 2.0
   649 	const TUint16 status = 0x0000;							// as of USB Spec 2.0
   550 	__KTRACE_OPT(KUSB, Kern::Printf("  Reporting interface status: 0x%02x", status));
   650     OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSGETINTERFACESTATUS_DUP3,
       
   651             "  Reporting interface status: 0x%02x", status );
   551 	*reinterpret_cast<TUint16*>(iEp0_TxBuf) = SWAP_BYTES_16(status);
   652 	*reinterpret_cast<TUint16*>(iEp0_TxBuf) = SWAP_BYTES_16(status);
   552 	if (SetupEndpointZeroWrite(iEp0_TxBuf, sizeof(status)) == KErrNone)
   653 	if (SetupEndpointZeroWrite(iEp0_TxBuf, sizeof(status)) == KErrNone)
   553 		{
   654 		{
   554 		iEp0WritePending = ETrue;
   655 		iEp0WritePending = ETrue;
   555 		}
   656 		}
   557 	}
   658 	}
   558 
   659 
   559 
   660 
   560 TInt DUsbClientController::ProcessGetEndpointStatus(const TUsbcSetup& aPacket)
   661 TInt DUsbClientController::ProcessGetEndpointStatus(const TUsbcSetup& aPacket)
   561 	{
   662 	{
   562 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessGetEndpointStatus()"));
   663 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSGETENDPOINTSTATUS, 
       
   664 	        "DUsbClientController::ProcessGetEndpointStatus()" );
   563 	if (iTrackDeviceState &&
   665 	if (iTrackDeviceState &&
   564 		((iDeviceState < EUsbcDeviceStateAddress) ||
   666 		((iDeviceState < EUsbcDeviceStateAddress) ||
   565 		 (iDeviceState == EUsbcDeviceStateAddress && (aPacket.iIndex & KUsbEpAddress_Portmask) != 0)))
   667 		 (iDeviceState == EUsbcDeviceStateAddress && (aPacket.iIndex & KUsbEpAddress_Portmask) != 0)))
   566 		{
   668 		{
   567 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
   669         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSGETENDPOINTSTATUS_DUP1, 
       
   670                 "  Error: Invalid device state" );
   568 		return KErrGeneral;
   671 		return KErrGeneral;
   569 		}
   672 		}
   570 	if (EndpointExists(aPacket.iIndex) == EFalse)
   673 	if (EndpointExists(aPacket.iIndex) == EFalse)
   571 		{
   674 		{
   572 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Endpoint does not exist"));
   675         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSGETENDPOINTSTATUS_DUP2, 
       
   676                 "  Error: Endpoint does not exist" );
   573 		return KErrGeneral;
   677 		return KErrGeneral;
   574 		}
   678 		}
   575 	const TInt ep = EpAddr2Idx(aPacket.iIndex);
   679 	const TInt ep = EpAddr2Idx(aPacket.iIndex);
   576 	const TUint16 status = (iRealEndpoints[ep].iHalt) ?	 KUsbEpStat_Halt : 0;
   680 	const TUint16 status = (iRealEndpoints[ep].iHalt) ?	 KUsbEpStat_Halt : 0;
   577 	__KTRACE_OPT(KUSB, Kern::Printf("  Reporting endpoint status 0x%02x for real endpoint %d",
   681 	OstTraceDefExt2( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSGETENDPOINTSTATUS_DUP3, 
   578 									status, ep));
   682 	        "  Reporting endpoint status 0x%02x for real endpoint %d", status, ep );
   579 	*reinterpret_cast<TUint16*>(iEp0_TxBuf) = SWAP_BYTES_16(status);
   683 	*reinterpret_cast<TUint16*>(iEp0_TxBuf) = SWAP_BYTES_16(status);
   580 	if (SetupEndpointZeroWrite(iEp0_TxBuf, 2) == KErrNone)
   684 	if (SetupEndpointZeroWrite(iEp0_TxBuf, 2) == KErrNone)
   581 		{
   685 		{
   582 		iEp0WritePending = ETrue;
   686 		iEp0WritePending = ETrue;
   583 		}
   687 		}
   585 	}
   689 	}
   586 
   690 
   587 
   691 
   588 TInt DUsbClientController::ProcessSetClearDevFeature(const TUsbcSetup& aPacket)
   692 TInt DUsbClientController::ProcessSetClearDevFeature(const TUsbcSetup& aPacket)
   589 	{
   693 	{
   590 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessSetClearDevFeature()"));
   694 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE,
       
   695 	        "DUsbClientController::ProcessSetClearDevFeature()" );
   591 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateDefault)
   696 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateDefault)
   592 		{
   697 		{
   593 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
   698         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP1,
       
   699                 "  Error: Invalid device state" );
   594 		return KErrGeneral;
   700 		return KErrGeneral;
   595 		}
   701 		}
   596 
   702 
   597 	TUint test_sel = 0;
   703 	TUint test_sel = 0;
   598 
   704 
   601 		switch (aPacket.iValue)
   707 		switch (aPacket.iValue)
   602 			{
   708 			{
   603 		case KUsbFeature_RemoteWakeup:
   709 		case KUsbFeature_RemoteWakeup:
   604 			if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateAddress)
   710 			if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateAddress)
   605 				{
   711 				{
   606 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
   712                 OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP2,
       
   713                         "  Error: Invalid device state" );
   607 				return KErrGeneral;
   714 				return KErrGeneral;
   608 				}
   715 				}
   609 			iRmWakeupStatus_Enabled = ETrue;
   716 			iRmWakeupStatus_Enabled = ETrue;
   610 			break;
   717 			break;
   611 		case KUsbFeature_TestMode:
   718 		case KUsbFeature_TestMode:
   612 			if (!iHighSpeed)
   719 			if (!iHighSpeed)
   613 				{
   720 				{
   614 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Request only supported in High-Speed mode"));
   721                 OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP3,
       
   722                         "  Error: Request only supported in High-Speed mode" );
   615 				return KErrGeneral;
   723 				return KErrGeneral;
   616 				}
   724 				}
   617 			if (LowByte(aPacket.iIndex) != 0)
   725 			if (LowByte(aPacket.iIndex) != 0)
   618 				{
   726 				{
   619 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Lower byte of wIndex must be zero"));
   727                 OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP4,
       
   728                         "  Error: Lower byte of wIndex must be zero" );
   620 				return KErrGeneral;
   729 				return KErrGeneral;
   621 				}
   730 				}
   622 			test_sel = HighByte(aPacket.iIndex);
   731 			test_sel = HighByte(aPacket.iIndex);
   623 			if ((test_sel < KUsbTestSelector_Test_J) || (test_sel > KUsbTestSelector_Test_Force_Enable))
   732 			if ((test_sel < KUsbTestSelector_Test_J) || (test_sel > KUsbTestSelector_Test_Force_Enable))
   624 				{
   733 				{
   625 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid test selector: %d", test_sel));
   734                 OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP5,
       
   735                         "  Error: Invalid test selector: %d", test_sel );
   626 				return KErrGeneral;
   736 				return KErrGeneral;
   627 				}
   737 				}
   628 			break;
   738 			break;
   629 		case KUsbFeature_B_HnpEnable:
   739 		case KUsbFeature_B_HnpEnable:
   630 			if (!iOtgSupport)
   740 			if (!iOtgSupport)
   631 				{
   741 				{
   632 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Request only supported on a OTG device"));
   742                 OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP6,
       
   743                         "  Error: Request only supported on a OTG device" );
   633 				return KErrGeneral;
   744 				return KErrGeneral;
   634 				}
   745 				}
   635 			if (!(iOtgFuncMap & KUsbOtgAttr_HnpSupp))
   746 			if (!(iOtgFuncMap & KUsbOtgAttr_HnpSupp))
   636 				{
   747 				{
   637 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Request only valid if OTG device supports HNP"));
   748                 OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP7,
       
   749                         "  Error: Request only valid if OTG device supports HNP" );
   638 				return KErrGeneral;
   750 				return KErrGeneral;
   639 				}
   751 				}
   640 			iOtgFuncMap |= KUsbOtgAttr_B_HnpEnable;
   752 			iOtgFuncMap |= KUsbOtgAttr_B_HnpEnable;
   641 			OtgFeaturesNotify();
   753 			OtgFeaturesNotify();
   642 			break;
   754 			break;
   643 		case KUsbFeature_A_HnpSupport:
   755 		case KUsbFeature_A_HnpSupport:
   644 			if (!iOtgSupport)
   756 			if (!iOtgSupport)
   645 				{
   757 				{
   646 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Request only supported on a OTG device"));
   758                 OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP8,
       
   759                         "  Error: Request only supported on a OTG device" );
   647 				return KErrGeneral;
   760 				return KErrGeneral;
   648 				}
   761 				}
   649 			if (!(iOtgFuncMap & KUsbOtgAttr_HnpSupp))
   762 			if (!(iOtgFuncMap & KUsbOtgAttr_HnpSupp))
   650 				{
   763 				{
   651 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Request only valid if OTG device supports HNP"));
   764                 OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP9,
       
   765                         "  Error: Request only valid if OTG device supports HNP" );
   652 				return KErrGeneral;
   766 				return KErrGeneral;
   653 				}
   767 				}
   654 			iOtgFuncMap |= KUsbOtgAttr_A_HnpSupport;
   768 			iOtgFuncMap |= KUsbOtgAttr_A_HnpSupport;
   655 			OtgFeaturesNotify();
   769 			OtgFeaturesNotify();
   656 			break;
   770 			break;
   657 		case KUsbFeature_A_AltHnpSupport:
   771 		case KUsbFeature_A_AltHnpSupport:
   658 			if (!iOtgSupport)
   772 			if (!iOtgSupport)
   659 				{
   773 				{
   660 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Request only supported on a OTG device"));
   774                 OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP10,
       
   775                         "  Error: Request only supported on a OTG device" );
   661 				return KErrGeneral;
   776 				return KErrGeneral;
   662 				}
   777 				}
   663 			if (!(iOtgFuncMap & KUsbOtgAttr_HnpSupp))
   778 			if (!(iOtgFuncMap & KUsbOtgAttr_HnpSupp))
   664 				{
   779 				{
   665 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Request only valid if OTG device supports HNP"));
   780                 OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP11,
       
   781                         "  Error: Request only valid if OTG device supports HNP" );
   666 				return KErrGeneral;
   782 				return KErrGeneral;
   667 				}
   783 				}
   668 			iOtgFuncMap |= KUsbOtgAttr_A_AltHnpSupport;
   784 			iOtgFuncMap |= KUsbOtgAttr_A_AltHnpSupport;
   669 			OtgFeaturesNotify();
   785 			OtgFeaturesNotify();
   670 			break;
   786 			break;
   671 		default:
   787 		default:
   672 			__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Unknown feature requested"));
   788 		    OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP12,
       
   789 		            "  Error: Unknown feature requested" );
   673 			return KErrGeneral;
   790 			return KErrGeneral;
   674 			}
   791 			}
   675 		}
   792 		}
   676 	else // KUsbRequest_ClearFeature
   793 	else // KUsbRequest_ClearFeature
   677 		{
   794 		{
   678 		switch (aPacket.iValue)
   795 		switch (aPacket.iValue)
   679 			{
   796 			{
   680 		case KUsbFeature_RemoteWakeup:
   797 		case KUsbFeature_RemoteWakeup:
   681 			if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateAddress)
   798 			if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateAddress)
   682 				{
   799 				{
   683 				__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
   800                 OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP13,
       
   801                         "  Error: Invalid device state" );
   684 				return KErrGeneral;
   802 				return KErrGeneral;
   685 				}
   803 				}
   686 			iRmWakeupStatus_Enabled = EFalse;
   804 			iRmWakeupStatus_Enabled = EFalse;
   687 			break;
   805 			break;
   688 		default:
   806 		default:
   689 			__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Unknown feature requested"));
   807 		    OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP14,
       
   808 		            "  Error: Unknown feature requested" );
   690 			return KErrGeneral;
   809 			return KErrGeneral;
   691 			}
   810 			}
   692 		}
   811 		}
   693 
   812 
   694 	SendEp0ZeroByteStatusPacket();							// success: zero bytes data during status stage
   813 	SendEp0ZeroByteStatusPacket();							// success: zero bytes data during status stage
   695 
   814 
   696 	// 9.4.9: "The transition to test mode of an upstream facing port must not happen until
   815 	// 9.4.9: "The transition to test mode of an upstream facing port must not happen until
   697 	// after the status stage of the request."
   816 	// after the status stage of the request."
   698 	if (test_sel)
   817 	if (test_sel)
   699 		{
   818 		{
   700 		__KTRACE_OPT(KPANIC, Kern::Printf("  Entering HS Test Mode %d", test_sel));
   819         OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARDEVFEATURE_DUP15,
       
   820                 "  Entering HS Test Mode %d", test_sel );
   701 		EnterTestMode(test_sel);
   821 		EnterTestMode(test_sel);
   702 		}
   822 		}
   703 
   823 
   704 	return KErrNone;
   824 	return KErrNone;
   705 	}
   825 	}
   706 
   826 
   707 
   827 
   708 TInt DUsbClientController::ProcessSetClearIfcFeature(const TUsbcSetup& aPacket)
   828 TInt DUsbClientController::ProcessSetClearIfcFeature(const TUsbcSetup& aPacket)
   709 	{
   829 	{
   710 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessSetClearIfcFeature()"));
   830 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSSETCLEARIFCFEATURE,
       
   831 	        "DUsbClientController::ProcessSetClearIfcFeature()" );
   711 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
   832 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
   712 		{
   833 		{
   713 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
   834         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEARIFCFEATURE_DUP1,
       
   835                 "  Error: Invalid device state" );
   714 		return KErrGeneral;
   836 		return KErrGeneral;
   715 		}
   837 		}
   716 	// No interface features defined in USB spec, thus
   838 	// No interface features defined in USB spec, thus
   717 	return KErrGeneral;
   839 	return KErrGeneral;
   718 	}
   840 	}
   719 
   841 
   720 
   842 
   721 TInt DUsbClientController::ProcessSetClearEpFeature(const TUsbcSetup& aPacket)
   843 TInt DUsbClientController::ProcessSetClearEpFeature(const TUsbcSetup& aPacket)
   722 	{
   844 	{
   723 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessSetClearEpFeature()"));
   845 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSSETCLEAREPFEATURE, 
       
   846 	        "DUsbClientController::ProcessSetClearEpFeature()" );
   724 	if (iTrackDeviceState &&
   847 	if (iTrackDeviceState &&
   725 		((iDeviceState < EUsbcDeviceStateAddress) ||
   848 		((iDeviceState < EUsbcDeviceStateAddress) ||
   726 		 (iDeviceState == EUsbcDeviceStateAddress && (aPacket.iIndex & KUsbEpAddress_Portmask) != 0)))
   849 		 (iDeviceState == EUsbcDeviceStateAddress && (aPacket.iIndex & KUsbEpAddress_Portmask) != 0)))
   727 		{
   850 		{
   728 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
   851         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEAREPFEATURE_DUP1, 
       
   852                 "  Error: Invalid device state" );
   729 		return KErrGeneral;
   853 		return KErrGeneral;
   730 		}
   854 		}
   731 	if (aPacket.iValue != KUsbFeature_EndpointHalt)
   855 	if (aPacket.iValue != KUsbFeature_EndpointHalt)
   732 		{
   856 		{
   733 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Unknown feature requested"));
   857         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEAREPFEATURE_DUP2, 
       
   858                 "  Error: Unknown feature requested" );
   734 		return KErrGeneral;
   859 		return KErrGeneral;
   735 		}
   860 		}
   736 	if (EndpointExists(aPacket.iIndex) == EFalse)
   861 	if (EndpointExists(aPacket.iIndex) == EFalse)
   737 		{
   862 		{
   738 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Endpoint does not exist"));
   863         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEAREPFEATURE_DUP3, 
       
   864                 "  Error: Endpoint does not exist" );
   739 		return KErrGeneral;
   865 		return KErrGeneral;
   740 		}
   866 		}
   741 	const TInt ep = EpAddr2Idx(aPacket.iIndex);
   867 	const TInt ep = EpAddr2Idx(aPacket.iIndex);
   742 	if (iRealEndpoints[ep].iLEndpoint->iInfo.iType == KUsbEpTypeControl ||
   868 	if (iRealEndpoints[ep].iLEndpoint->iInfo.iType == KUsbEpTypeControl ||
   743 		iRealEndpoints[ep].iLEndpoint->iInfo.iType == KUsbEpTypeIsochronous)
   869 		iRealEndpoints[ep].iLEndpoint->iInfo.iType == KUsbEpTypeIsochronous)
   744 		{
   870 		{
   745 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Endpoint is Control or Isochronous"));
   871         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCLEAREPFEATURE_DUP4, 
       
   872                 "  Error: Endpoint is Control or Isochronous" );
   746 		return KErrGeneral;
   873 		return KErrGeneral;
   747 		}
   874 		}
   748 	SetClearHaltFeature(ep, aPacket.iRequest);
   875 	SetClearHaltFeature(ep, aPacket.iRequest);
   749 	SendEp0ZeroByteStatusPacket();							// success: zero bytes data during status stage
   876 	SendEp0ZeroByteStatusPacket();							// success: zero bytes data during status stage
   750 	return KErrNone;
   877 	return KErrNone;
   751 	}
   878 	}
   752 
   879 
   753 
   880 
   754 TInt DUsbClientController::ProcessSetAddress(const TUsbcSetup& aPacket)
   881 TInt DUsbClientController::ProcessSetAddress(const TUsbcSetup& aPacket)
   755 	{
   882 	{
   756 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessSetAddress()"));
   883 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSSETADDRESS,
       
   884 	        "DUsbClientController::ProcessSetAddress()" );
   757 	if (iTrackDeviceState && iDeviceState > EUsbcDeviceStateAddress)
   885 	if (iTrackDeviceState && iDeviceState > EUsbcDeviceStateAddress)
   758 		{
   886 		{
   759 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
   887         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETADDRESS_DUP1,
       
   888                 "  Error: Invalid device state" );
   760 		return KErrGeneral;
   889 		return KErrGeneral;
   761 		}
   890 		}
   762 	const TUint16 addr = aPacket.iValue;
   891 	const TUint16 addr = aPacket.iValue;
   763 	if (addr > 127)
   892 	if (addr > 127)
   764 		{
   893 		{
   765 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Bad address value: %d (>127)", addr));
   894         OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETADDRESS_DUP2,
       
   895                 "  Error: Bad address value: %d (>127)", addr );
   766 		return KErrGeneral;
   896 		return KErrGeneral;
   767 		}
   897 		}
   768 	if (addr == 0)
   898 	if (addr == 0)
   769 		{
   899 		{
   770 		// Enter Default state (from Default or Address)
   900 		// Enter Default state (from Default or Address)
   771 		NextDeviceState(EUsbcDeviceStateDefault);
   901 		NextDeviceState(EUsbcDeviceStateDefault);
   772 		}
   902 		}
   773 	__KTRACE_OPT(KUSB, Kern::Printf("  USB address: %d", addr));
   903 	OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSSETADDRESS_DUP3,
       
   904 	        "  USB address: %d", addr );
   774 	// The spec says, under section 9.4.6:
   905 	// The spec says, under section 9.4.6:
   775 	// "Stages after the initial Setup packet assume the same device address as the Setup packet. The USB
   906 	// "Stages after the initial Setup packet assume the same device address as the Setup packet. The USB
   776 	// device does not change its device address until after the Status stage of this request is completed
   907 	// device does not change its device address until after the Status stage of this request is completed
   777 	// successfully. Note that this is a difference between this request and all other requests. For all other
   908 	// successfully. Note that this is a difference between this request and all other requests. For all other
   778 	// requests, the operation indicated must be completed before the Status stage."
   909 	// requests, the operation indicated must be completed before the Status stage."
   783 	}
   914 	}
   784 
   915 
   785 
   916 
   786 TInt DUsbClientController::ProcessGetDescriptor(const TUsbcSetup& aPacket)
   917 TInt DUsbClientController::ProcessGetDescriptor(const TUsbcSetup& aPacket)
   787 	{
   918 	{
   788 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessGetDescriptor()"));
   919 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSGETDESCRIPTOR, 
       
   920 	        "DUsbClientController::ProcessGetDescriptor()" );
   789 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateDefault)
   921 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateDefault)
   790 		{
   922 		{
   791 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
   923         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSGETDESCRIPTOR_DUP1, 
       
   924                 "  Error: Invalid device state" );
   792 		return KErrGeneral;
   925 		return KErrGeneral;
   793 		}
   926 		}
   794 
   927 
   795 	// Make sure we assume the correct speed
   928 	// Make sure we assume the correct speed
   796 	__ASSERT_DEBUG((iHighSpeed == CurrentlyUsingHighSpeed()), Kern::Fault(KUsbPILPanicCat, __LINE__));
   929 	__ASSERT_DEBUG((iHighSpeed == CurrentlyUsingHighSpeed()), Kern::Fault(KUsbPILPanicCat, __LINE__));
   802 													size);
   935 													size);
   803 
   936 
   804 	if ((result != KErrNone) || (size == 0))
   937 	if ((result != KErrNone) || (size == 0))
   805 		{
   938 		{
   806 		// This doesn't have to be an error - protocol-wise it's OK.
   939 		// This doesn't have to be an error - protocol-wise it's OK.
   807 		__KTRACE_OPT(KUSB, Kern::Printf("  Couldn't retrieve descriptor"));
   940         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSGETDESCRIPTOR_DUP2, 
   808 		return KErrGeneral;
   941                 "  Couldn't retrieve descriptor" );
   809 		}
   942 		return KErrGeneral;
   810 
   943 		}
   811 	__KTRACE_OPT(KUSB, Kern::Printf("  Descriptor found, size: %d (requested: %d)",
   944 
   812 									size, aPacket.iLength));
   945     OstTraceDefExt2( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSGETDESCRIPTOR_DUP3,
       
   946             "  Descriptor found, size: %d (requested: %d)", size, aPacket.iLength );
   813 	if (size > KUsbcBufSz_Ep0Tx)
   947 	if (size > KUsbcBufSz_Ep0Tx)
   814 		{
   948 		{
   815 		// This should actually not be possible (i.e. we should never get here).
   949 		// This should actually not be possible (i.e. we should never get here).
   816 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Ep0_Tx buffer too small"));
   950         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSGETDESCRIPTOR_DUP4,
       
   951                 "  Error: Ep0_Tx buffer too small" );
   817 		}
   952 		}
   818 	if (size > aPacket.iLength)
   953 	if (size > aPacket.iLength)
   819 		{
   954 		{
   820 		// Send only as much data as requested by the host
   955 		// Send only as much data as requested by the host
   821 		size = aPacket.iLength;
   956 		size = aPacket.iLength;
   822 		}
   957 		}
   823 
   958 
   824 #ifdef ENABLE_EXCESSIVE_DEBUG_OUTPUT
   959 #ifdef ENABLE_EXCESSIVE_DEBUG_OUTPUT
   825 	__KTRACE_OPT(KUSB,
   960     OstTraceDefExt1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSGETDESCRIPTOR_DUP5,
   826 				 Kern::Printf("  Data: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x ...",
   961             "  Data: %{uint8[]}", TOstArray<TUint8>(iEp0_TxBuf, 8) );
   827 							  iEp0_TxBuf[0], iEp0_TxBuf[1], iEp0_TxBuf[2], iEp0_TxBuf[3],
       
   828 							  iEp0_TxBuf[4], iEp0_TxBuf[5], iEp0_TxBuf[6], iEp0_TxBuf[7]));
       
   829 #endif
   962 #endif
   830 	// If we're about to send less bytes than expected by the host AND our number is a
   963 	// If we're about to send less bytes than expected by the host AND our number is a
   831 	// multiple of the packet size, in order to indicate the end of the control transfer,
   964 	// multiple of the packet size, in order to indicate the end of the control transfer,
   832 	// we must finally send a zero length data packet (ZLP):
   965 	// we must finally send a zero length data packet (ZLP):
   833 	const TBool zlp = ((size < aPacket.iLength) && (size % iEp0MaxPacketSize == 0));
   966 	const TBool zlp = ((size < aPacket.iLength) && (size % iEp0MaxPacketSize == 0));
   840 	}
   973 	}
   841 
   974 
   842 
   975 
   843 TInt DUsbClientController::ProcessSetDescriptor(const TUsbcSetup& aPacket)
   976 TInt DUsbClientController::ProcessSetDescriptor(const TUsbcSetup& aPacket)
   844 	{
   977 	{
   845 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessSetDescriptor()"));
   978 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSSETDESCRIPTOR, 
       
   979 	        "DUsbClientController::ProcessSetDescriptor()" );
   846 #ifndef USB_SUPPORTS_SET_DESCRIPTOR_REQUEST
   980 #ifndef USB_SUPPORTS_SET_DESCRIPTOR_REQUEST
   847 	return KErrGeneral;
   981 	return KErrGeneral;
   848 #else
   982 #else
   849 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateAddress)
   983 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateAddress)
   850 		{
   984 		{
   852 		return KErrGeneral;
   986 		return KErrGeneral;
   853 		}
   987 		}
   854 	if (aPacket.iLength > KUsbcBufSz_Ep0Rx)
   988 	if (aPacket.iLength > KUsbcBufSz_Ep0Rx)
   855 		{
   989 		{
   856 		// Error: Our Rx buffer is too small! (Raise a defect to make it larger)
   990 		// Error: Our Rx buffer is too small! (Raise a defect to make it larger)
   857 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Ep0_Rx buffer too small"));
   991         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETDESCRIPTOR_DUP1,
       
   992 	            "  Error: Ep0_Rx buffer too small" );
   858 		return KErrGeneral;
   993 		return KErrGeneral;
   859 		}
   994 		}
   860 	SetEp0DataOutVars(aPacket);
   995 	SetEp0DataOutVars(aPacket);
   861 	SetupEndpointZeroRead();
   996 	SetupEndpointZeroRead();
   862 	return KErrNone;
   997 	return KErrNone;
   864 	}
   999 	}
   865 
  1000 
   866 
  1001 
   867 TInt DUsbClientController::ProcessGetConfiguration(const TUsbcSetup& aPacket)
  1002 TInt DUsbClientController::ProcessGetConfiguration(const TUsbcSetup& aPacket)
   868 	{
  1003 	{
   869 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessGetConfiguration()"));
  1004 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSGETCONFIGURATION, 
       
  1005 	        "DUsbClientController::ProcessGetConfiguration()" );
   870 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateAddress)
  1006 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateAddress)
   871 		{
  1007 		{
   872 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
  1008         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSGETCONFIGURATION_DUP1, 
       
  1009                 "  Error: Invalid device state" );
   873 		return KErrGeneral;
  1010 		return KErrGeneral;
   874 		}
  1011 		}
   875 	if (iTrackDeviceState && iDeviceState == EUsbcDeviceStateAddress && iCurrentConfig != 0)
  1012 	if (iTrackDeviceState && iDeviceState == EUsbcDeviceStateAddress && iCurrentConfig != 0)
   876 		{
  1013 		{
   877 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: DeviceState Address && Config != 0"));
  1014         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSGETCONFIGURATION_DUP2, 
       
  1015                 "  Error: DeviceState Address && Config != 0" );
   878 		return KErrGeneral;
  1016 		return KErrGeneral;
   879 		}
  1017 		}
   880 	if (iTrackDeviceState && iDeviceState == EUsbcDeviceStateConfigured && iCurrentConfig == 0)
  1018 	if (iTrackDeviceState && iDeviceState == EUsbcDeviceStateConfigured && iCurrentConfig == 0)
   881 		{
  1019 		{
   882 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: DeviceState Configured && Config == 0"));
  1020         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSGETCONFIGURATION_DUP3, 
       
  1021                 "  Error: DeviceState Configured && Config == 0" );
   883 		return KErrGeneral;
  1022 		return KErrGeneral;
   884 		}
  1023 		}
   885 	if (aPacket.iLength != 1)								// "unspecified behavior"
  1024 	if (aPacket.iLength != 1)								// "unspecified behavior"
   886 		{
  1025 		{
   887 		__KTRACE_OPT(KUSB, Kern::Printf("  Warning: wLength != 1 (= %d)", aPacket.iLength));
  1026         OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSGETCONFIGURATION_DUP4, 
   888 		}
  1027                 "  Warning: wLength != 1 (= %d)", aPacket.iLength );
   889 	__KTRACE_OPT(KUSB, Kern::Printf("  Reporting configuration value %d", iCurrentConfig));
  1028 		}
       
  1029 	OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSGETCONFIGURATION_DUP5, 
       
  1030 	        "  Reporting configuration value %d", iCurrentConfig );
   890 	if (SetupEndpointZeroWrite(&iCurrentConfig, sizeof(iCurrentConfig)) == KErrNone)
  1031 	if (SetupEndpointZeroWrite(&iCurrentConfig, sizeof(iCurrentConfig)) == KErrNone)
   891 		{
  1032 		{
   892 		iEp0WritePending = ETrue;
  1033 		iEp0WritePending = ETrue;
   893 		}
  1034 		}
   894 	return KErrNone;
  1035 	return KErrNone;
   904 
  1045 
   905 	@publishedPartner @released
  1046 	@publishedPartner @released
   906 */
  1047 */
   907 TInt DUsbClientController::ProcessSetConfiguration(const TUsbcSetup& aPacket)
  1048 TInt DUsbClientController::ProcessSetConfiguration(const TUsbcSetup& aPacket)
   908 	{
  1049 	{
   909 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessSetConfiguration()"));
  1050 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSSETCONFIGURATION, 
   910 
  1051 	        "DUsbClientController::ProcessSetConfiguration()" );
   911 	// This function may be called by the PSL from within an ISR -- so we have
  1052 	// This function may be called by the PSL from within an ISR -- so we have
   912 	// to take care what we do here (and also in all functions that get called
  1053 	// to take care what we do here (and also in all functions that get called
   913 	// from here).
  1054 	// from here).
   914 
  1055 
   915 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateAddress)
  1056 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateAddress)
   916 		{
  1057 		{
   917 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
  1058         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCONFIGURATION_DUP1, 
       
  1059                 "  Error: Invalid device state" );
   918 		return KErrGeneral;
  1060 		return KErrGeneral;
   919 		}
  1061 		}
   920 	const TUint16 value = aPacket.iValue;
  1062 	const TUint16 value = aPacket.iValue;
   921 	if (value > 1)											// we support only one configuration
  1063 	if (value > 1)											// we support only one configuration
   922 		{
  1064 		{
   923 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Configuration value too large: %d", value));
  1065         OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETCONFIGURATION_DUP2, 
   924 		return KErrGeneral;
  1066                 "  Error: Configuration value too large: %d", value );
   925 		}
  1067 		return KErrGeneral;
   926 
  1068 		}
   927 	__KTRACE_OPT(KUSB, Kern::Printf("  Configuration value: %d", value));
  1069 
       
  1070     OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSSETCONFIGURATION_DUP3,
       
  1071             "  Configuration value: %d", value );
   928 	ChangeConfiguration(value);
  1072 	ChangeConfiguration(value);
   929 
  1073 
   930 	// In 9.4.5 under GET_STATUS we read, that after SET_CONFIGURATION the HALT feature
  1074 	// In 9.4.5 under GET_STATUS we read, that after SET_CONFIGURATION the HALT feature
   931 	// for all endpoints is reset to zero.
  1075 	// for all endpoints is reset to zero.
   932 	TInt num = 0;
  1076 	TInt num = 0;
   933 	(TAny) DoForEveryEndpointInUse(&DUsbClientController::ClearHaltFeature, num);
  1077 	(TAny) DoForEveryEndpointInUse(&DUsbClientController::ClearHaltFeature, num);
   934 	__KTRACE_OPT(KUSB, Kern::Printf("  Called ClearHaltFeature() for %d endpoints", num));
  1078 	OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSSETCONFIGURATION_DUP4,
       
  1079 	        "  Called ClearHaltFeature() for %d endpoints", num );
   935 	SendEp0ZeroByteStatusPacket();							// success: zero bytes data during status stage
  1080 	SendEp0ZeroByteStatusPacket();							// success: zero bytes data during status stage
   936 	return KErrNone;
  1081 	return KErrNone;
   937 	}
  1082 	}
   938 
  1083 
   939 
  1084 
   940 TInt DUsbClientController::ProcessGetInterface(const TUsbcSetup& aPacket)
  1085 TInt DUsbClientController::ProcessGetInterface(const TUsbcSetup& aPacket)
   941 	{
  1086 	{
   942 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessGetInterface()"));
  1087 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSGETINTERFACE, 
       
  1088 	        "DUsbClientController::ProcessGetInterface()" );
   943 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
  1089 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
   944 		{
  1090 		{
   945 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
  1091         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSGETINTERFACE_DUP1, 
       
  1092                 "  Error: Invalid device state" );
   946 		return KErrGeneral;
  1093 		return KErrGeneral;
   947 		}
  1094 		}
   948 	if (iCurrentConfig == 0)
  1095 	if (iCurrentConfig == 0)
   949 		{
  1096 		{
   950 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Device not configured"));
  1097         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSGETINTERFACE_DUP2, 
       
  1098                 "  Error: Device not configured" );
   951 		return KErrGeneral;
  1099 		return KErrGeneral;
   952 		}
  1100 		}
   953 	const TInt number = aPacket.iIndex;
  1101 	const TInt number = aPacket.iIndex;
   954 	if (!InterfaceExists(number))
  1102 	if (!InterfaceExists(number))
   955 		{
  1103 		{
   956 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Bad interface index: %d", number));
  1104         OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSGETINTERFACE_DUP3, 
       
  1105                 "  Error: Bad interface index: %d", number );
   957 		return KErrGeneral;
  1106 		return KErrGeneral;
   958 		}
  1107 		}
   959 	// Send alternate setting code of iCurrentInterface of Interface(set) <number> of the current
  1108 	// Send alternate setting code of iCurrentInterface of Interface(set) <number> of the current
   960 	// config (iCurrentConfig).
  1109 	// config (iCurrentConfig).
   961 	const TUint8 setting = InterfaceNumber2InterfacePointer(number)->iCurrentInterface;
  1110 	const TUint8 setting = InterfaceNumber2InterfacePointer(number)->iCurrentInterface;
   962 	__KTRACE_OPT(KUSB, Kern::Printf("  Reporting interface setting %d", setting));
  1111 	OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSGETINTERFACE_DUP4, 
       
  1112 	        "  Reporting interface setting %d", setting );
   963 	if (SetupEndpointZeroWrite(&setting, 1) == KErrNone)
  1113 	if (SetupEndpointZeroWrite(&setting, 1) == KErrNone)
   964 		{
  1114 		{
   965 		iEp0WritePending = ETrue;
  1115 		iEp0WritePending = ETrue;
   966 		}
  1116 		}
   967 	return KErrNone;
  1117 	return KErrNone;
   968 	}
  1118 	}
   969 
  1119 
   970 
  1120 
   971 TInt DUsbClientController::ProcessSetInterface(const TUsbcSetup& aPacket)
  1121 TInt DUsbClientController::ProcessSetInterface(const TUsbcSetup& aPacket)
   972 	{
  1122 	{
   973 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessSetInterface()"));
  1123 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSSETINTERFACE,
       
  1124 	        "DUsbClientController::ProcessSetInterface()" );
   974 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
  1125 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
   975 		{
  1126 		{
   976 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
  1127         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETINTERFACE_DUP1,
       
  1128                 "  Error: Invalid device state" );
   977 		return KErrGeneral;
  1129 		return KErrGeneral;
   978 		}
  1130 		}
   979 	if (iCurrentConfig == 0)
  1131 	if (iCurrentConfig == 0)
   980 		{
  1132 		{
   981 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Device not configured"));
  1133         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETINTERFACE_DUP2,
       
  1134                 "  Error: Device not configured" );
   982 		return KErrGeneral;
  1135 		return KErrGeneral;
   983 		}
  1136 		}
   984 	const TInt number = aPacket.iIndex;
  1137 	const TInt number = aPacket.iIndex;
   985 	if (!InterfaceExists(number))
  1138 	if (!InterfaceExists(number))
   986 		{
  1139 		{
   987 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Bad interface index: %d", number));
  1140         OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETINTERFACE_DUP3,
       
  1141                 "  Error: Bad interface index: %d", number );
   988 		return KErrGeneral;
  1142 		return KErrGeneral;
   989 		}
  1143 		}
   990 	const TInt setting = aPacket.iValue;
  1144 	const TInt setting = aPacket.iValue;
   991 	TUsbcInterfaceSet* const ifcset_ptr = InterfaceNumber2InterfacePointer(number);
  1145 	TUsbcInterfaceSet* const ifcset_ptr = InterfaceNumber2InterfacePointer(number);
   992 	RPointerArray<TUsbcInterface>& ifcs = ifcset_ptr->iInterfaces;
  1146 	RPointerArray<TUsbcInterface>& ifcs = ifcset_ptr->iInterfaces;
   993 	if (setting >= ifcs.Count())
  1147 	if (setting >= ifcs.Count())
   994 		{
  1148 		{
   995 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Alt Setting >= bNumAltSettings: %d", setting));
  1149         OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSETINTERFACE_DUP4,
   996 		return KErrGeneral;
  1150                 "  Error: Alt Setting >= bNumAltSettings: %d", setting );
   997 		}
  1151 		return KErrGeneral;
   998 	__KTRACE_OPT(KUSB, Kern::Printf("  Interface setting:: %d", setting));
  1152 		}
       
  1153     OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_PROCESSSETINTERFACE_DUP5,
       
  1154             "  Interface setting:: %d", setting );
   999 	// Set iCurrentInterface of Interface(set) <number> of the current config
  1155 	// Set iCurrentInterface of Interface(set) <number> of the current config
  1000 	// (iCurrentConfig) to alternate setting <setting>.
  1156 	// (iCurrentConfig) to alternate setting <setting>.
  1001 	ChangeInterface(ifcs[setting]);
  1157 	ChangeInterface(ifcs[setting]);
  1002 	// In 9.4.5 under GET_STATUS we read, that after SET_INTERFACE the HALT feature
  1158 	// In 9.4.5 under GET_STATUS we read, that after SET_INTERFACE the HALT feature
  1003 	// for all endpoints (of the now current interface setting) is reset to zero.
  1159 	// for all endpoints (of the now current interface setting) is reset to zero.
  1013 	}
  1169 	}
  1014 
  1170 
  1015 
  1171 
  1016 TInt DUsbClientController::ProcessSynchFrame(const TUsbcSetup& aPacket)
  1172 TInt DUsbClientController::ProcessSynchFrame(const TUsbcSetup& aPacket)
  1017 	{
  1173 	{
  1018 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProcessSynchFrame()"));
  1174 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCESSSYNCHFRAME, 
       
  1175 	        "DUsbClientController::ProcessSynchFrame()" );
  1019 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
  1176 	if (iTrackDeviceState && iDeviceState < EUsbcDeviceStateConfigured)
  1020 		{
  1177 		{
  1021 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Invalid device state"));
  1178         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSYNCHFRAME_DUP1, 
       
  1179                 "  Error: Invalid device state" );
  1022 		return KErrGeneral;
  1180 		return KErrGeneral;
  1023 		}
  1181 		}
  1024 	const TInt ep = aPacket.iIndex;
  1182 	const TInt ep = aPacket.iIndex;
  1025 	if (EndpointExists(ep) == EFalse)
  1183 	if (EndpointExists(ep) == EFalse)
  1026 		{
  1184 		{
  1027 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Endpoint does not exist"));
  1185         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSYNCHFRAME_DUP2, 
       
  1186                 "  Error: Endpoint does not exist" );
  1028 		return KErrGeneral;
  1187 		return KErrGeneral;
  1029 		}
  1188 		}
  1030 	if (iRealEndpoints[EpAddr2Idx(ep)].iLEndpoint->iInfo.iType != KUsbEpTypeIsochronous)
  1189 	if (iRealEndpoints[EpAddr2Idx(ep)].iLEndpoint->iInfo.iType != KUsbEpTypeIsochronous)
  1031 		{
  1190 		{
  1032 		__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Endpoint is not isochronous"));
  1191         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_PROCESSSYNCHFRAME_DUP3, 
       
  1192                 "  Error: Endpoint is not isochronous" );
  1033 		return KErrGeneral;
  1193 		return KErrGeneral;
  1034 		}
  1194 		}
  1035 	// We always send 0:
  1195 	// We always send 0:
  1036 	*reinterpret_cast<TUint16*>(iEp0_TxBuf) = 0x00;
  1196 	*reinterpret_cast<TUint16*>(iEp0_TxBuf) = 0x00;
  1037 	if (SetupEndpointZeroWrite(iEp0_TxBuf, 2) == KErrNone)
  1197 	if (SetupEndpointZeroWrite(iEp0_TxBuf, 2) == KErrNone)
  1043 
  1203 
  1044 
  1204 
  1045 #ifdef USB_SUPPORTS_SET_DESCRIPTOR_REQUEST
  1205 #ifdef USB_SUPPORTS_SET_DESCRIPTOR_REQUEST
  1046 void DUsbClientController::ProceedSetDescriptor()
  1206 void DUsbClientController::ProceedSetDescriptor()
  1047 	{
  1207 	{
  1048 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ProceedSetDescriptor()"));
  1208 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_PROCEEDSETDESCRIPTOR,
       
  1209 	        "DUsbClientController::ProceedSetDescriptor()" );
  1049 	// iEp0DataReceived already reflects the current buffer state
  1210 	// iEp0DataReceived already reflects the current buffer state
  1050 	if (iEp0DataReceived < iSetup.iLength)
  1211 	if (iEp0DataReceived < iSetup.iLength)
  1051 		{
  1212 		{
  1052 		// Not yet all data received => proceed
  1213 		// Not yet all data received => proceed
  1053 		return;
  1214 		return;
  1076 
  1237 
  1077 // --- Secondary (Helper) Functions
  1238 // --- Secondary (Helper) Functions
  1078 
  1239 
  1079 void DUsbClientController::SetClearHaltFeature(TInt aRealEndpoint, TUint8 aRequest)
  1240 void DUsbClientController::SetClearHaltFeature(TInt aRealEndpoint, TUint8 aRequest)
  1080 	{
  1241 	{
  1081 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::SetClearHaltFeature()"));
  1242 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_SETCLEARHALTFEATURE, 
       
  1243 	        "DUsbClientController::SetClearHaltFeature()" );
  1082 	if (aRequest == KUsbRequest_SetFeature)
  1244 	if (aRequest == KUsbRequest_SetFeature)
  1083 		{
  1245 		{
  1084 		if (iRealEndpoints[aRealEndpoint].iHalt)
  1246 		if (iRealEndpoints[aRealEndpoint].iHalt)
  1085 			{
  1247 			{
  1086 			// (This condition is not really an error)
  1248 			// (This condition is not really an error)
  1087 			__KTRACE_OPT(KUSB, Kern::Printf("  Warning: HALT feature already set"));
  1249             OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_SETCLEARHALTFEATURE_DUP1, 
       
  1250                     "  Warning: HALT feature already set" );
  1088 			return;
  1251 			return;
  1089 			}
  1252 			}
  1090 		__KTRACE_OPT(KUSB, Kern::Printf("  setting HALT feature for real endpoint %d",
  1253 		OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_SETCLEARHALTFEATURE_DUP2, 
  1091 										aRealEndpoint));
  1254 		        "  setting HALT feature for real endpoint %d", aRealEndpoint );
  1092 		StallEndpoint(aRealEndpoint);
  1255 		StallEndpoint(aRealEndpoint);
  1093 		iRealEndpoints[aRealEndpoint].iHalt = ETrue;
  1256 		iRealEndpoints[aRealEndpoint].iHalt = ETrue;
  1094 		}
  1257 		}
  1095 	else													// KUsbRequest_ClearFeature
  1258 	else													// KUsbRequest_ClearFeature
  1096 		{
  1259 		{
  1097 		if (iRealEndpoints[aRealEndpoint].iHalt == EFalse)
  1260 		if (iRealEndpoints[aRealEndpoint].iHalt == EFalse)
  1098 			{
  1261 			{
  1099 			// In this case, before we return, the data toggles are reset to DATA0.
  1262 			// In this case, before we return, the data toggles are reset to DATA0.
  1100 			__KTRACE_OPT(KUSB, Kern::Printf("  Warning: HALT feature already cleared"));
  1263             OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_SETCLEARHALTFEATURE_DUP3, 
       
  1264                     "  Warning: HALT feature already cleared" );
  1101 			ResetDataToggle(aRealEndpoint);
  1265 			ResetDataToggle(aRealEndpoint);
  1102 			return;
  1266 			return;
  1103 			}
  1267 			}
  1104 		__KTRACE_OPT(KUSB, Kern::Printf("  clearing HALT feature for real endpoint %d",
  1268 		OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_SETCLEARHALTFEATURE_DUP4, 
  1105 										aRealEndpoint));
  1269 		        "  clearing HALT feature for real endpoint %d", aRealEndpoint );
  1106 		ResetDataToggle(aRealEndpoint);
  1270 		ResetDataToggle(aRealEndpoint);
  1107 		ClearStallEndpoint(aRealEndpoint);
  1271 		ClearStallEndpoint(aRealEndpoint);
  1108 		iRealEndpoints[aRealEndpoint].iHalt = EFalse;
  1272 		iRealEndpoints[aRealEndpoint].iHalt = EFalse;
  1109 		}
  1273 		}
  1110 	EpStatusNotify(aRealEndpoint);							// only called if actually something changed
  1274 	EpStatusNotify(aRealEndpoint);							// only called if actually something changed
  1111 	}
  1275 	}
  1112 
  1276 
  1113 
  1277 
  1114 TInt DUsbClientController::ClearHaltFeature(TInt aRealEndpoint)
  1278 TInt DUsbClientController::ClearHaltFeature(TInt aRealEndpoint)
  1115 	{
  1279 	{
  1116 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ClearHaltFeature()"));
  1280 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_CLEARHALTFEATURE, 
       
  1281 	        "DUsbClientController::ClearHaltFeature()" );
  1117 	if (iRealEndpoints[aRealEndpoint].iHalt != EFalse)
  1282 	if (iRealEndpoints[aRealEndpoint].iHalt != EFalse)
  1118 		{
  1283 		{
  1119 		ClearStallEndpoint(aRealEndpoint);
  1284 		ClearStallEndpoint(aRealEndpoint);
  1120 		iRealEndpoints[aRealEndpoint].iHalt = EFalse;
  1285 		iRealEndpoints[aRealEndpoint].iHalt = EFalse;
  1121 		}
  1286 		}
  1123 	}
  1288 	}
  1124 
  1289 
  1125 
  1290 
  1126 void DUsbClientController::ChangeConfiguration(TUint16 aValue)
  1291 void DUsbClientController::ChangeConfiguration(TUint16 aValue)
  1127 	{
  1292 	{
  1128 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ChangeConfiguration()"));
  1293 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_CHANGECONFIGURATION, 
       
  1294 	        "DUsbClientController::ChangeConfiguration()" );
  1129 	// New configuration is the same as the old one: 0
  1295 	// New configuration is the same as the old one: 0
  1130 	if (iCurrentConfig == 0 && aValue == 0)
  1296 	if (iCurrentConfig == 0 && aValue == 0)
  1131 		{
  1297 		{
  1132 		// no-op
  1298 		// no-op
  1133 		__KTRACE_OPT(KUSB, Kern::Printf("  Configuration: New == Old == 0 --> exiting"));
  1299         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_CHANGECONFIGURATION_DUP1, 
       
  1300                 "  Configuration: New == Old == 0 --> exiting" );
  1134 		return;
  1301 		return;
  1135 		}
  1302 		}
  1136 	// New configuration is the same as the old one (but not 0)
  1303 	// New configuration is the same as the old one (but not 0)
  1137 	if (iCurrentConfig == aValue)
  1304 	if (iCurrentConfig == aValue)
  1138 		{
  1305 		{
  1139 		__KTRACE_OPT(KUSB, Kern::Printf("  Configuration: New == Old == %d --> exiting", aValue));
  1306         OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_CHANGECONFIGURATION_DUP2, 
  1140 
  1307                 "  Configuration: New == Old == %d --> exiting", aValue );
  1141 		// From the spec 9.1.1.5, Data toggle is reset to zero here when 
  1308 		// From the spec 9.1.1.5, Data toggle is reset to zero here when 
  1142 		// setconfiguration(x->x)(x!=0) received, although we only support
  1309 		// setconfiguration(x->x)(x!=0) received, although we only support
  1143 		// single configuration currently.
  1310 		// single configuration currently.
  1144 		TInt num = 0;
  1311 		TInt num = 0;
  1145 		TInt ret = DoForEveryEndpointInUse(&DUsbClientController::ResetDataToggle, num);
  1312 		TInt ret = DoForEveryEndpointInUse(&DUsbClientController::ResetDataToggle, num);
  1146 		if(ret != KErrNone)
  1313 		if(ret != KErrNone)
  1147 			{
  1314 			{
  1148 			__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Endpoint data toggle reset failed"));
  1315             OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_CHANGECONFIGURATION_DUP3, 
  1149 			}
  1316                     "  Error: Endpoint data toggle reset failed" );
  1150 		__KTRACE_OPT(KUSB, Kern::Printf("  Called ResetDataToggle()for %d endpoints", num));	
  1317 			}
  1151 	
  1318 		OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_CHANGECONFIGURATION_DUP4, 
       
  1319 		        "  Called ResetDataToggle()for %d endpoints", num );	
  1152 		return;
  1320 		return;
  1153 		}
  1321 		}
  1154 	// Device is already configured
  1322 	// Device is already configured
  1155 	if (iCurrentConfig != 0)
  1323 	if (iCurrentConfig != 0)
  1156 		{
  1324 		{
  1157 		__KTRACE_OPT(KUSB, Kern::Printf("  Device was configured: %d", iCurrentConfig));
  1325         OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_CHANGECONFIGURATION_DUP5, 
       
  1326                 "  Device was configured: %d", iCurrentConfig );
  1158 		// Tear down all interface(set)s of the old configuration
  1327 		// Tear down all interface(set)s of the old configuration
  1159 		RPointerArray<TUsbcInterfaceSet>& ifcsets = CurrentConfig()->iInterfaceSets;
  1328 		RPointerArray<TUsbcInterfaceSet>& ifcsets = CurrentConfig()->iInterfaceSets;
  1160 		for (TInt i = 0; i < ifcsets.Count(); ++i)
  1329 		for (TInt i = 0; i < ifcsets.Count(); ++i)
  1161 			{
  1330 			{
  1162 			__KTRACE_OPT(KUSB, Kern::Printf("  Tearing down InterfaceSet %d", i));
  1331             OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_CHANGECONFIGURATION_DUP6, 
       
  1332                     "  Tearing down InterfaceSet %d", i );
  1163 			InterfaceSetTeardown(ifcsets[i]);
  1333 			InterfaceSetTeardown(ifcsets[i]);
  1164 			}
  1334 			}
  1165 		iCurrentConfig = 0;
  1335 		iCurrentConfig = 0;
  1166 		// Enter Address state (from Configured)
  1336 		// Enter Address state (from Configured)
  1167 		if (iDeviceState == EUsbcDeviceStateConfigured)
  1337 		if (iDeviceState == EUsbcDeviceStateConfigured)
  1168 			NextDeviceState(EUsbcDeviceStateAddress);
  1338 			NextDeviceState(EUsbcDeviceStateAddress);
  1169 		}
  1339 		}
  1170 	// Device gets a new configuration
  1340 	// Device gets a new configuration
  1171 	if (aValue != 0)
  1341 	if (aValue != 0)
  1172 		{
  1342 		{
  1173 		__KTRACE_OPT(KUSB, Kern::Printf("  Device gets new configuration..."));
  1343         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_CHANGECONFIGURATION_DUP7, 
       
  1344                 "  Device gets new configuration..." );
  1174 		// Setup all alternate settings 0 of all interfaces
  1345 		// Setup all alternate settings 0 of all interfaces
  1175 		// (Don't separate the next two lines of code.)
  1346 		// (Don't separate the next two lines of code.)
  1176 		iCurrentConfig = aValue;
  1347 		iCurrentConfig = aValue;
  1177 		RPointerArray<TUsbcInterfaceSet>& ifcsets = CurrentConfig()->iInterfaceSets;
  1348 		RPointerArray<TUsbcInterfaceSet>& ifcsets = CurrentConfig()->iInterfaceSets;
  1178 		const TInt n = ifcsets.Count();
  1349 		const TInt n = ifcsets.Count();
  1179 		for (TInt i = 0; i < n; ++i)
  1350 		for (TInt i = 0; i < n; ++i)
  1180 			{
  1351 			{
  1181 			__KTRACE_OPT(KUSB, Kern::Printf("  Setting up InterfaceSet %d", i));
  1352             OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_CHANGECONFIGURATION_DUP8, 
       
  1353                     "  Setting up InterfaceSet %d", i );
  1182 			InterfaceSetup(ifcsets[i]->iInterfaces[0]);
  1354 			InterfaceSetup(ifcsets[i]->iInterfaces[0]);
  1183 			}
  1355 			}
  1184 		// Enter Configured state (from Address or Configured)
  1356 		// Enter Configured state (from Address or Configured)
  1185 		NextDeviceState(EUsbcDeviceStateConfigured);
  1357 		NextDeviceState(EUsbcDeviceStateConfigured);
  1186 		}
  1358 		}
  1187 	__KTRACE_OPT(KUSB, Kern::Printf("  New configuration: %d", iCurrentConfig));
  1359     OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_CHANGECONFIGURATION_DUP9, 
       
  1360             "  New configuration: %d", iCurrentConfig );
  1188 	return;
  1361 	return;
  1189 	}
  1362 	}
  1190 
  1363 
  1191 
  1364 
  1192 void DUsbClientController::InterfaceSetup(TUsbcInterface* aIfc)
  1365 void DUsbClientController::InterfaceSetup(TUsbcInterface* aIfc)
  1193 	{
  1366 	{
  1194 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::InterfaceSetup()"));
  1367 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_INTERFACESETUP, 
       
  1368 	        "DUsbClientController::InterfaceSetup()" );
  1195 	const TInt num_eps = aIfc->iEndpoints.Count();
  1369 	const TInt num_eps = aIfc->iEndpoints.Count();
  1196 	for (TInt i = 0; i < num_eps; i++)
  1370 	for (TInt i = 0; i < num_eps; i++)
  1197 		{
  1371 		{
  1198 		// Prepare this endpoint for I/O
  1372 		// Prepare this endpoint for I/O
  1199 		TUsbcLogicalEndpoint* const ep = aIfc->iEndpoints[i];
  1373 		TUsbcLogicalEndpoint* const ep = aIfc->iEndpoints[i];
  1200 		// (TUsbcLogicalEndpoint's FS/HS endpoint sizes and interval values got
  1374 		// (TUsbcLogicalEndpoint's FS/HS endpoint sizes and interval values got
  1201 		//  adjusted in its constructor.)
  1375 		//  adjusted in its constructor.)
  1202 		if (iHighSpeed)
  1376 		if (iHighSpeed)
  1203 			{
  1377 			{
  1204 			__KTRACE_OPT(KUSB, Kern::Printf("  Setting Ep info size to %d (HS)", ep->iEpSize_Hs));
  1378             OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_INTERFACESETUP_DUP1, 
       
  1379                     "  Setting Ep info size to %d (HS)", ep->iEpSize_Hs );
  1205 			ep->iInfo.iSize = ep->iEpSize_Hs;
  1380 			ep->iInfo.iSize = ep->iEpSize_Hs;
  1206 			}
  1381 			}
  1207 		else
  1382 		else
  1208 			{
  1383 			{
  1209 			__KTRACE_OPT(KUSB, Kern::Printf("  Setting Ep info size to %d (FS)", ep->iEpSize_Fs));
  1384             OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_INTERFACESETUP_DUP2, 
       
  1385                     "  Setting Ep info size to %d (FS)", ep->iEpSize_Fs );
  1210 			ep->iInfo.iSize = ep->iEpSize_Fs;
  1386 			ep->iInfo.iSize = ep->iEpSize_Fs;
  1211 			}
  1387 			}
  1212 		const TInt idx = EpAddr2Idx(ep->iPEndpoint->iEndpointAddr);
  1388 		const TInt idx = EpAddr2Idx(ep->iPEndpoint->iEndpointAddr);
  1213 		if (ConfigureEndpoint(idx, ep->iInfo) != KErrNone)
  1389 		if (ConfigureEndpoint(idx, ep->iInfo) != KErrNone)
  1214 			{
  1390 			{
  1215 			__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Endpoint %d configuration failed", idx));
  1391             OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_INTERFACESETUP_DUP3, 
       
  1392                     "  Error: Endpoint %d configuration failed", idx );
  1216 			continue;
  1393 			continue;
  1217 			}
  1394 			}
  1218 		// Should there be a problem with it then we could try resetting the ep
  1395 		// Should there be a problem with it then we could try resetting the ep
  1219 		// data toggle at this point (or before the Configure) as well.
  1396 		// data toggle at this point (or before the Configure) as well.
  1220 		__KTRACE_OPT(KUSB, Kern::Printf("  Connecting real ep addr 0x%02x & logical ep #%d",
  1397         OstTraceDefExt2( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_INTERFACESETUP_DUP4, 
  1221 										ep->iPEndpoint->iEndpointAddr, ep->iLEndpointNum));
  1398                 "  Connecting real ep addr 0x%02x & logical ep #%d", ep->iPEndpoint->iEndpointAddr, ep->iLEndpointNum );
  1222 		ep->iPEndpoint->iLEndpoint = ep;
  1399 		ep->iPEndpoint->iLEndpoint = ep;
  1223 		}
  1400 		}
  1224 	aIfc->iInterfaceSet->iCurrentInterface = aIfc->iSettingCode;
  1401 	aIfc->iInterfaceSet->iCurrentInterface = aIfc->iSettingCode;
  1225 	return;
  1402 	return;
  1226 	}
  1403 	}
  1227 
  1404 
  1228 
  1405 
  1229 void DUsbClientController::InterfaceSetTeardown(TUsbcInterfaceSet* aIfcSet)
  1406 void DUsbClientController::InterfaceSetTeardown(TUsbcInterfaceSet* aIfcSet)
  1230 	{
  1407 	{
  1231 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::InterfaceSetTeardown()"));
  1408 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_INTERFACESETTEARDOWN,
       
  1409 	        "DUsbClientController::InterfaceSetTeardown()" );
  1232 	if (aIfcSet->iInterfaces.Count() == 0)
  1410 	if (aIfcSet->iInterfaces.Count() == 0)
  1233 		{
  1411 		{
  1234 		__KTRACE_OPT(KUSB, Kern::Printf("  No interfaces exist - returning"));
  1412         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_INTERFACESETTEARDOWN_DUP1,
       
  1413                 "  No interfaces exist - returning" );
  1235 		return;
  1414 		return;
  1236 		}
  1415 		}
  1237 	RPointerArray<TUsbcLogicalEndpoint>& eps = aIfcSet->CurrentInterface()->iEndpoints;
  1416 	RPointerArray<TUsbcLogicalEndpoint>& eps = aIfcSet->CurrentInterface()->iEndpoints;
  1238 	const TInt num_eps = eps.Count();
  1417 	const TInt num_eps = eps.Count();
  1239 	for (TInt i = 0; i < num_eps; i++)
  1418 	for (TInt i = 0; i < num_eps; i++)
  1243 
  1422 
  1244 		CancelTransferRequests(idx);
  1423 		CancelTransferRequests(idx);
  1245 
  1424 
  1246 		if (!ep->iPEndpoint->iLEndpoint)
  1425 		if (!ep->iPEndpoint->iLEndpoint)
  1247 			{
  1426 			{
  1248 			__KTRACE_OPT(KUSB, Kern::Printf("  real ep %d not configured: skipping", idx));
  1427             OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_INTERFACESETTEARDOWN_DUP2,
       
  1428                     "  real ep %d not configured: skipping", idx );
  1249 			continue;
  1429 			continue;
  1250 			}
  1430 			}
  1251 		if (ResetDataToggle(idx) != KErrNone)
  1431 		if (ResetDataToggle(idx) != KErrNone)
  1252 			{
  1432 			{
  1253 			__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Endpoint %d data toggle reset failed", idx));
  1433             OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_INTERFACESETTEARDOWN_DUP3,
       
  1434                     "  Error: Endpoint %d data toggle reset failed", idx );
  1254 			}
  1435 			}
  1255 		if (DeConfigureEndpoint(idx) != KErrNone)
  1436 		if (DeConfigureEndpoint(idx) != KErrNone)
  1256 			{
  1437 			{
  1257 			__KTRACE_OPT(KPANIC, Kern::Printf("  Error: Endpoint %d de-configuration failed", idx));
  1438             OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_FATAL, DUSBCLIENTCONTROLLER_INTERFACESETTEARDOWN_DUP4,
  1258 			}
  1439                     "  Error: Endpoint %d de-configuration failed", idx );
  1259 
  1440 			}
  1260 		__KTRACE_OPT(KUSB, Kern::Printf("  disconnecting real ep & logical ep"));
  1441 
       
  1442         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_INTERFACESETTEARDOWN_DUP5,
       
  1443                 "  disconnecting real ep & logical ep" );
  1261 		ep->iPEndpoint->iLEndpoint = NULL;
  1444 		ep->iPEndpoint->iLEndpoint = NULL;
  1262 		}
  1445 		}
  1263 	if (aIfcSet->CurrentInterface() != 0)
  1446 	if (aIfcSet->CurrentInterface() != 0)
  1264 		{
  1447 		{
  1265 		__KTRACE_OPT(KUSB, Kern::Printf("  Resetting alternate interface setting to 0"));
  1448         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_INTERFACESETTEARDOWN_DUP6,
       
  1449                 "  Resetting alternate interface setting to 0" );
  1266 		//Add this mutex to protect the interface set data structure
  1450 		//Add this mutex to protect the interface set data structure
  1267 		if (NKern::CurrentContext() == EThread)
  1451 		if (NKern::CurrentContext() == EThread)
  1268 		    {
  1452 		    {
  1269             NKern::FMWait(&iMutex);
  1453             NKern::FMWait(&iMutex);
  1270 		    }
  1454 		    }
  1279 	}
  1463 	}
  1280 
  1464 
  1281 
  1465 
  1282 void DUsbClientController::ChangeInterface(TUsbcInterface* aIfc)
  1466 void DUsbClientController::ChangeInterface(TUsbcInterface* aIfc)
  1283 	{
  1467 	{
  1284 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::ChangeInterface()"));
  1468 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_CHANGEINTERFACE, 
       
  1469 	        "DUsbClientController::ChangeInterface()" );
  1285 	TUsbcInterfaceSet* ifcset = aIfc->iInterfaceSet;
  1470 	TUsbcInterfaceSet* ifcset = aIfc->iInterfaceSet;
  1286 	const TUint8 setting = aIfc->iSettingCode;
  1471 	const TUint8 setting = aIfc->iSettingCode;
  1287 	if (ifcset->iCurrentInterface == setting)
  1472 	if (ifcset->iCurrentInterface == setting)
  1288 		{
  1473 		{
  1289 		__KTRACE_OPT(KUSB, Kern::Printf("  New Ifc == old Ifc: nothing to do"));
  1474         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_CHANGEINTERFACE_DUP1, 
       
  1475                 "  New Ifc == old Ifc: nothing to do" );
  1290 		return;
  1476 		return;
  1291 		}
  1477 		}
  1292 	__KTRACE_OPT(KUSB, Kern::Printf("  Setting new interface setting #%d", setting));
  1478     OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_CHANGEINTERFACE_DUP2,
       
  1479             "  Setting new interface setting #%d", setting );
  1293 	InterfaceSetTeardown(ifcset);
  1480 	InterfaceSetTeardown(ifcset);
  1294 	InterfaceSetup(aIfc);
  1481 	InterfaceSetup(aIfc);
  1295 	StatusNotify(static_cast<TUsbcDeviceState>(KUsbAlternateSetting | setting), ifcset->iClientId);
  1482 	StatusNotify(static_cast<TUsbcDeviceState>(KUsbAlternateSetting | setting), ifcset->iClientId);
  1296 	}
  1483 	}
  1297 
  1484 
  1299 // aFunction gets called, successively, with the endpoint index of every ep in-use as its argument.
  1486 // aFunction gets called, successively, with the endpoint index of every ep in-use as its argument.
  1300 // (BTW: The declaration "type (class::*name)(params)" makes <name> a "pointer to element function".)
  1487 // (BTW: The declaration "type (class::*name)(params)" makes <name> a "pointer to element function".)
  1301 //
  1488 //
  1302 TInt DUsbClientController::DoForEveryEndpointInUse(TInt (DUsbClientController::*aFunction)(TInt), TInt& aCount)
  1489 TInt DUsbClientController::DoForEveryEndpointInUse(TInt (DUsbClientController::*aFunction)(TInt), TInt& aCount)
  1303 	{
  1490 	{
  1304 	__KTRACE_OPT(KUSB, Kern::Printf("DUsbClientController::DoForEveryEndpointInUse()"));
  1491 	OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, DUSBCLIENTCONTROLLER_DOFOREVERYENDPOINTINUSE, 
       
  1492 	        "DUsbClientController::DoForEveryEndpointInUse()" );
  1305 	aCount = 0;
  1493 	aCount = 0;
  1306 	TUsbcConfiguration* const config = CurrentConfig();
  1494 	TUsbcConfiguration* const config = CurrentConfig();
  1307 	if (!config)
  1495 	if (!config)
  1308 		{
  1496 		{
  1309 		__KTRACE_OPT(KUSB, Kern::Printf("  Device is not configured - returning"));
  1497         OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, DUSBCLIENTCONTROLLER_DOFOREVERYENDPOINTINUSE_DUP1, 
       
  1498                 "  Device is not configured - returning" );
  1310 		return KErrNone;
  1499 		return KErrNone;
  1311 		}
  1500 		}
  1312 	RPointerArray<TUsbcInterfaceSet>& ifcsets = config->iInterfaceSets;
  1501 	RPointerArray<TUsbcInterfaceSet>& ifcsets = config->iInterfaceSets;
  1313 	const TInt num_ifcsets = ifcsets.Count();
  1502 	const TInt num_ifcsets = ifcsets.Count();
  1314 	for (TInt i = 0; i < num_ifcsets; i++)
  1503 	for (TInt i = 0; i < num_ifcsets; i++)