os/kernelhwsrv/userlibandfileserver/fileserver/smassstorage/cbulkonlytransport.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 /**
    17  @file
    18  @internalTechnology
    19 */
    20 #include "cbulkonlytransport.h"
    21 #include "cbulkonlytransportusbcldd.h"
    22 #include "cbulkonlytransportusbcscldd.h"
    23 #include "usbmsshared.h"
    24 #include "massstoragedebug.h"
    25 #include "cusbmassstorageserver.h"
    26 
    27 
    28 //CBW offsets
    29 LOCAL_D const TInt KCbwSignatureOffset 			= 0;
    30 LOCAL_D const TInt KCbwTagOffset 				= 4;
    31 LOCAL_D const TInt KCbwDataTransferLengthOffset = 8;
    32 LOCAL_D const TInt KCbwFlagOffset 				= 12;
    33 LOCAL_D const TInt KCbwLunOffset				= 13;
    34 LOCAL_D const TInt KCbwCbLengthOffset  			= 14;
    35 
    36 LOCAL_D const TInt KMaxCbwcbLength 				= 16;
    37 
    38 // CSW offsets
    39 LOCAL_D const TInt KCswSingnatureOffset 		= 0;
    40 LOCAL_D const TInt KCswTagOffset 				= 4;
    41 LOCAL_D const TInt KCswDataResidueOffset 		= 8;
    42 LOCAL_D const TInt KCswStatusOffset 			= 12;
    43 
    44 
    45 
    46 /**
    47  This function unpacks into the TUsbRequestHdr class from a descriptor with
    48  the alignment that would be introduced on the USB bus.
    49 
    50  @param aBuffer Input buffer
    51  @param aTarget Unpacked header.
    52  @return Error.
    53  */
    54 TInt TUsbRequestHdr::Decode(const TDesC8& aBuffer)
    55 
    56 	{
    57 	if (aBuffer.Length() < static_cast<TInt>(KRequestHdrSize))
    58 		{
    59         __PRINT1(_L("TUsbRequestHdr::Decode buffer invalid length %d"), aBuffer.Length());
    60 		return KErrGeneral;
    61 		}
    62 
    63 	iRequestType = aBuffer[0];
    64 	iRequest = static_cast<TEp0Request>(aBuffer[1]);
    65 	iValue	 = static_cast<TUint16>(aBuffer[2] + (aBuffer[3] << 8));
    66 	iIndex	 = static_cast<TUint16>(aBuffer[4] + (aBuffer[5] << 8));
    67 	iLength  = static_cast<TUint16>(aBuffer[6] + (aBuffer[7] << 8));
    68     __PRINT5(_L("type=%d request=%d value=%d index=%d length=%d"), iRequestType,iRequest,iValue,iIndex,iLength);
    69 
    70 	return KErrNone;
    71 	}
    72 
    73 
    74 /**
    75 This function determines whether data is required by the host in response
    76 to a message header.
    77 
    78 @return TBool	Flag indicating whether a data response required.
    79 */
    80 TBool TUsbRequestHdr::IsDataResponseRequired() const
    81 
    82 	{
    83 	return (iRequestType & 0x80) ? (TBool)ETrue : (TBool)EFalse;
    84 	}
    85 
    86 //-------------------------------------
    87 /**
    88 Create an object of a class derived from CBulkOnlyTransport (default to CBulkOnlyTransportUsbcLdd object)
    89 @param aNumDrives - The number of drives available for MS
    90 @param aController - reference to the parent
    91 @return pointer to newly created derived class object
    92 */
    93 CBulkOnlyTransport* CBulkOnlyTransport::NewL(TInt aNumDrives,CUsbMassStorageController& aController)
    94 	{
    95 	__FNLOG("CBulkOnlyTransport::NewL()");
    96 	
    97 	return NewL(aNumDrives,aController, (CUsbMassStorageController::TTransportldd) 1);
    98 	}
    99 
   100 /**
   101 Create an object of a class derived from CBulkOnlyTransport 
   102 @param aNumDrives - The number of drives available for MS
   103 @param aController - reference to the parent
   104 @param aTransportLddFlag - Type of usb client ldd
   105 @return pointer to newly created derived class object
   106 */
   107 CBulkOnlyTransport* CBulkOnlyTransport::NewL(TInt aNumDrives,CUsbMassStorageController& aController, CUsbMassStorageController::TTransportldd aTransportLddFlag)
   108 	{
   109 	__FNLOG("CBulkOnlyTransport::NewL()");
   110 
   111 	if (aNumDrives <=0 || static_cast<TUint>(aNumDrives) > KUsbMsMaxDrives)
   112 		{
   113 		User::Leave(KErrArgument);
   114 		}
   115 
   116 	CBulkOnlyTransportUsbcScLdd* scTransport;
   117 	CBulkOnlyTransportUsbcLdd* nonscTransport;
   118 	switch (aTransportLddFlag)
   119 		{
   120 		case 1: 
   121 				nonscTransport = new(ELeave) CBulkOnlyTransportUsbcLdd(aNumDrives, aController);
   122 				return nonscTransport;
   123 
   124 		case 2: 
   125 				scTransport = new(ELeave) CBulkOnlyTransportUsbcScLdd(aNumDrives, aController);
   126 				return scTransport;
   127 		default:
   128 				return NULL;
   129 
   130 		}
   131 	}
   132 
   133 
   134 TInt CBulkOnlyTransport::InitialiseTransportL(TInt aTransportLddFlag)
   135 	{
   136 	__FNLOG("CBulkOnlyTransportUsbcScLdd::InitialiseTransportL()");
   137 	TInt ret = KErrNone;
   138 	MTransportBase* transport;
   139 	iController.GetTransport(transport);
   140 	switch (aTransportLddFlag)
   141 		{
   142 		case 2: 
   143 				ret = ((CBulkOnlyTransportUsbcScLdd*) transport)->Ldd().Open(0);
   144 				if (ret != KErrNone)
   145 					{
   146 					return ret;
   147 					}
   148 				else
   149 					{
   150 					((CBulkOnlyTransportUsbcScLdd*) transport)->Ldd().Close();		
   151 					CleanupStack::PushL(transport);
   152 					((CBulkOnlyTransportUsbcScLdd*) transport)->ConstructL();
   153 					CleanupStack::Pop(transport);
   154 					return ret;
   155 					}
   156 		case 1:
   157 				ret = ((CBulkOnlyTransportUsbcLdd*) transport)->Ldd().Open(0);
   158 				if (ret != KErrNone)
   159 					{
   160 					return ret;
   161 					}
   162 				else
   163 					{
   164 					((CBulkOnlyTransportUsbcLdd*) transport)->Ldd().Close();
   165 					CleanupStack::PushL(transport);
   166 					((CBulkOnlyTransportUsbcLdd*) transport)->ConstructL();
   167 					CleanupStack::Pop(transport);
   168 					return ret;
   169 					}
   170 		default:
   171 				return KErrNotFound;
   172 		}
   173 	}	
   174 
   175 /**
   176 c'tor
   177 @param aNumDrives - The number of drives available for MS
   178 @param aController - reference to the parent
   179 */
   180 CBulkOnlyTransport::CBulkOnlyTransport(TInt aNumDrives,CUsbMassStorageController& aController):
   181 	CActive(EPriorityStandard),
   182 	iMaxLun(aNumDrives-1),
   183 	iController(aController),
   184 	iStallAllowed(ETrue),
   185 	iInterfaceConfigured(EFalse),
   186 	iCommandBufPtr(NULL,0),
   187 	iDataBufPtr(NULL,0),
   188 	iCswBufPtr(NULL,0),
   189 	iPaddingBufPtr(NULL,0),
   190 	iWriteBufPtr(NULL,0),
   191 	iReadBufPtr(NULL, 0),
   192 	iCbwBufPtr(NULL,0)
   193 	{
   194 	__FNLOG("CBulkOnlyTransport::CBulkOnlyTransport");
   195 	}
   196 
   197 /**
   198 Destructor
   199 */
   200 CBulkOnlyTransport::~CBulkOnlyTransport()
   201 	{
   202 	__FNLOG("CBulkOnlyTransport::~CBulkOnlyTransport");
   203 	if (iInterfaceConfigured)
   204 		{
   205 		Stop();
   206 		}
   207 	}
   208 
   209 
   210 /**
   211 Called by the protocol after processing the packet to indicate that more data is required.
   212 
   213 @param aData reference to the data buffer.
   214 */
   215 void CBulkOnlyTransport::SetupReadData(TUint aLength)
   216 	{
   217 	__FNLOG("CBulkOnlyTransport::SetupReadData");
   218 	__PRINT1(_L("Length = %d  (bytes)\n"), aLength);
   219 	iBufSize = aLength;
   220 	iReadSetUp = ETrue;
   221 	}
   222 
   223 
   224 /**
   225 Called by the protocol after processing the packet to indicate that data should be written to the host.
   226 
   227 @param aData reference to the data buffer.
   228 */
   229 void CBulkOnlyTransport::SetupWriteData(TPtrC8& aData)
   230 	{
   231 	__FNLOG("CBulkOnlyTransport::SetupWriteData");
   232 	__PRINT1(_L("Length = %d  (bytes)\n"), aData.Length());
   233 	iWriteBufPtr.Set(aData);
   234 	iWriteSetUp = ETrue;
   235 	}
   236 
   237 
   238 TInt CBulkOnlyTransport::Start()
   239 	{
   240 	__FNLOG("CBulkOnlyTransport::Start");
   241 
   242 	TInt err = KErrNone;
   243 
   244 	if (!iProtocol)
   245 		{
   246 		return KErrBadHandle;   //protocol should be set up before start
   247 		}
   248 
   249 	if (IsActive())
   250 		{
   251 		__PRINT(_L("CBulkOnlyTransport::Start  - active before start!\n"));
   252 		return KErrInUse;
   253 		}
   254 
   255 	if ((err = SetupConfigurationDescriptor()) 	!= KErrNone ||
   256 		(err = SetupInterfaceDescriptors())		!= KErrNone )
   257 		{
   258 		__PRINT(_L("CBulkOnlyTransport::Start  - Error during descriptors setup!\n"));
   259 		return err;
   260 		}
   261 
   262 	AllocateEndpointResources();
   263 	ActivateDeviceStateNotifier();  // activate notifier wich will wait until USB became configured
   264 	TUsbcDeviceState deviceStatus = EUsbcDeviceStateDefault;
   265 	err = GetDeviceStatus(deviceStatus);
   266 	__PRINT1(_L("CBulkOnlyTransport::Start - Device status = %d\n"), deviceStatus);
   267 	if (err == KErrNone && deviceStatus == EUsbcDeviceStateConfigured)
   268 		{
   269 		__PRINT(_L("CBulkOnlyTransport::Start  - Starting bulk only transport\n"));
   270 		err = HwStart();
   271 		}
   272 
   273 #ifdef MSDC_MULTITHREADED
   274 	TPtr8 aDes1(NULL,0);
   275 	TPtr8 aDes2(NULL,0);
   276 	GetBufferPointers(aDes1, aDes2);
   277 	iProtocol->InitializeBufferPointers(aDes1, aDes2); // have to pass pointer to memory not offsets to initialise TPtr, and lengths
   278 #endif
   279 
   280 	iInterfaceConfigured = ETrue;
   281 	return err;
   282 	}
   283 
   284 TInt CBulkOnlyTransport::HwStart(TBool aDiscard)
   285 	{
   286 	__FNLOG("CBulkOnlyTransport::HwStart");
   287 
   288     TInt lun = MaxLun();
   289     do
   290         {
   291         Controller().DriveManager().Connect(lun);
   292         }
   293     while(--lun >= 0);
   294 
   295 	TInt res = StartControlInterface();
   296 
   297 	iCurrentState = ENone;
   298 	iWriteSetUp=EFalse;
   299 	iReadSetUp=EFalse;
   300 	iStarted = ETrue;
   301 
   302     if (aDiscard)
   303 		{
   304 		FlushData();
   305 		}
   306 
   307 	ReadCBW();
   308 	return res;
   309 	}
   310 
   311 
   312 TInt CBulkOnlyTransport::HwStop()
   313 	{
   314 	__FNLOG("CBulkOnlyTransport::HwStop");
   315 	if (iStarted)
   316 		{
   317         StopBulkOnlyEndpoint();
   318 		CancelControlInterface();
   319 		iStarted = EFalse;
   320 		}
   321 	return KErrNone;
   322 	}
   323 
   324 
   325 void CBulkOnlyTransport::StopBulkOnlyEndpoint()
   326 	{
   327 	__FNLOG("CBulkOnlyTransport::StopBulkOnlyEndpoint");
   328 
   329     TInt lun = MaxLun();
   330     do
   331         {
   332         Controller().DriveManager().Disconnect(lun);
   333         }
   334     while(--lun >= 0);
   335 	Cancel();
   336 	iProtocol->Cancel();
   337 	}
   338 
   339 
   340 TInt CBulkOnlyTransport::HwSuspend()
   341 	{
   342 	__FNLOG("CBulkOnlyTransport::HwSuspend");
   343 
   344 	TInt lun = MaxLun();
   345 	do
   346 		{
   347 		Controller().DriveManager().Disconnect(lun);
   348 		}
   349 	while(--lun >= 0);
   350 
   351 	return KErrNone;
   352 	}
   353 
   354 
   355 TInt CBulkOnlyTransport::HwResume()
   356 	{
   357 	__FNLOG("CBulkOnlyTransport::HwResume");
   358 
   359     TInt lun = MaxLun();
   360     do
   361         {
   362         Controller().DriveManager().Connect(lun);
   363         }
   364     while(--lun >= 0);
   365 
   366 	return KErrNone;
   367 	}
   368 
   369 /**
   370 Stops the Bulk Only Transport
   371 */
   372 TInt CBulkOnlyTransport::Stop()
   373 	{
   374 	__FNLOG("CBulkOnlyTransport::Stop");
   375 	CancelControlInterface();
   376 	CancelDeviceStateNotifier();
   377 	Cancel();
   378 	if  (iInterfaceConfigured)
   379 		{
   380 		ReleaseInterface();
   381 		SetupConfigurationDescriptor(ETrue);
   382 		}
   383 	iCurrentState = ENone;
   384 	iInterfaceConfigured = EFalse;
   385 
   386 	return KErrNone;
   387 	}
   388 
   389 
   390 
   391 void CBulkOnlyTransport::DoCancel()
   392 	{
   393 	__FNLOG("CBulkOnlyTransport::DoCancel");
   394 	CancelReadWriteRequests();
   395 	}
   396 
   397 
   398 void CBulkOnlyTransport::Activate(TInt aReason)
   399     {
   400     SetActive();
   401     TRequestStatus* r = &iStatus;
   402     User::RequestComplete(r, aReason);
   403     }
   404 
   405 
   406 void CBulkOnlyTransport::RunL()
   407 	{
   408 	__FNLOG("CBulkOnlyTransport::RunL");
   409 	if (iStatus != KErrNone)
   410 		{
   411 		__PRINT1(_L("Error %d in RunL, halt endpoints \n"), iStatus.Int());
   412 		SetPermError(); //halt endpoints for reset recovery
   413 		return;
   414 		}
   415 	switch (iCurrentState)
   416 		{
   417 		case EWaitForCBW:
   418 			__PRINT(_L("EWaitForCBW"));
   419 			ProcessCbwEvent();
   420 			break;
   421 
   422 		case EWritingData:
   423             __PRINT(_L("EWritingData"));
   424 			iWriteSetUp = EFalse;  //the buffer was used
   425 
   426 			if (iDataResidue && iStallAllowed)
   427 				{
   428 				StallEndpointAndWaitForClear();
   429 				}
   430 
   431 			SendCSW(iCbwTag, iDataResidue, iCmdStatus);
   432 			break;
   433 
   434 		case EReadingData:
   435 			{
   436 			__PRINT(_L("EReadingData"));
   437 
   438 			ProcessReadingDataEvent();
   439 			}
   440 			break;
   441 
   442 		case ESendingCSW:
   443 			__PRINT(_L("ESendingCSW"));
   444 			ReadCBW();
   445 			break;
   446 
   447         case EPermErr:
   448 			__PRINT(_L("EPermErr"));
   449 			StallEndpointAndWaitForClear();
   450             break;
   451 
   452         default:
   453 			SetPermError();		// unexpected state
   454 		}
   455 	}
   456 
   457 
   458 /**
   459 Decode the CBW received from the host via OutEndpoint
   460 
   461 - If the header is valid, the data content is passed to the parser.
   462 - Depending on the command, more data may be transmitted/received.
   463 - ...or the CSW is sent (if not a data command).
   464 
   465 */
   466 void CBulkOnlyTransport::DecodeCBW()
   467 	{
   468 	__FNLOG("CBulkOnlyTransport::DecodeCBW");
   469 
   470 	SetCbwPtr();
   471 
   472 	if (!CheckCBW())  //check if CBW valid and meaningful
   473 		{
   474         // CBW not valid or meaningful
   475         // Specification says: "If the CBW is not valid, the device shall STALL
   476         // the Bulk-In pipe. Also, the device shall either STALL the Bulk-Out pipe,
   477         // or the device shall accept and discard any Bulk-Out data. The device
   478         // shall maintain this state until a Reset Recovery."
   479         // Here we keep bulk-in ep stalled and ignore bulk-out ep.
   480 		SetPermError();
   481 		ExpireData((TAny*) (iCbwBufPtr.Ptr()));
   482 		return;
   483 		}
   484 
   485 	TPtrC8 aData;
   486 	aData.Set(&iCbwBufPtr[KCbwCbLengthOffset], KMaxCbwcbLength+1);    //prepare data for protocol starting form Length
   487 	TUint8 lun = static_cast<TUint8>(iCbwBufPtr[13] & 0x0f);
   488 
   489 	iCbwTag  =	static_cast<TUint32>(iCbwBufPtr[KCbwTagOffset])		|
   490 				static_cast<TUint32>(iCbwBufPtr[KCbwTagOffset+1])	<<8 |
   491 				static_cast<TUint32>(iCbwBufPtr[KCbwTagOffset+2])	<<16|
   492 				static_cast<TUint32>(iCbwBufPtr[KCbwTagOffset+3])	<<24;
   493 
   494 	TInt i = KCbwDataTransferLengthOffset;
   495 	TUint hostDataLength = 	static_cast<TUint32>(iCbwBufPtr[i  ])		|
   496 							static_cast<TUint32>(iCbwBufPtr[i+1]) <<8 	|
   497 							static_cast<TUint32>(iCbwBufPtr[i+2]) <<16	|
   498 							static_cast<TUint32>(iCbwBufPtr[i+3]) <<24;
   499 
   500 	TBool dataToHost = iCbwBufPtr[KCbwFlagOffset] & 0x80;
   501 
   502 	__PRINT4(_L("lun =%d, hostDataLength=%d, CBWtag = 0x%x, dataToHost=%d\n"), lun, hostDataLength, iCbwTag, dataToHost);
   503 	//////////////////////////////////////////////
   504 	TBool ret = iProtocol->DecodePacket(aData, lun);
   505 	//////////////////////////////////////////////
   506 	ExpireData((TAny*) (iCbwBufPtr.Ptr()));
   507 
   508 
   509 	iStallAllowed = ETrue;
   510 
   511 	if (!ret)
   512 		{
   513 		__PRINT(_L("Command Failed\n"));
   514 		iCmdStatus = ECommandFailed;
   515 		}
   516 	else
   517 		{
   518 		__PRINT(_L("Command Passed\n"));
   519 		iCmdStatus = ECommandPassed;
   520 		}
   521 
   522 	if (hostDataLength)    // Host  expected data transfer
   523 		{
   524 		if (dataToHost)  // send data to host
   525 			{
   526 			if (!iWriteSetUp) //write buffer was not set up
   527 				{
   528 				__PRINT(_L("Write buffer was not setup\n"));
   529 				iDataResidue =hostDataLength;
   530 				__PRINT1(_L("DataResidue (write to host)=%d\n"),iDataResidue);
   531 
   532 //------------------------------------
   533 				if (hostDataLength <= KBOTMaxBufSize)
   534 					{
   535 					__PRINT(_L("Case 4 or 8\n"));
   536 					SetPaddingBufPtr(hostDataLength);
   537 					iPaddingBufPtr.FillZ(hostDataLength);
   538 					TPtrC8 ptr(NULL, 0);
   539 					ptr.Set((TUint8*)iPaddingBufPtr.Ptr(), hostDataLength);
   540 					WriteData(iStatus, ptr, hostDataLength, EFalse);
   541 					iStallAllowed = EFalse;
   542 					if (iReadSetUp)  //read buffer WAS set up - case (8)
   543 						{
   544 						__PRINT(_L("It is Case 8\n"));
   545 						iCmdStatus = EPhaseError;
   546 						}
   547 					return;
   548 					}
   549 				else
   550 //------------------------------------
   551 //					Use next block instead of StallEndpointAndWaitForClear(InEndpoint);
   552 					{
   553 					SetPaddingBufPtr(hostDataLength);
   554 					iPaddingBufPtr.FillZ(KBOTMaxBufSize);
   555 					TUint c =0;
   556 					TRequestStatus status;
   557 					while (c<hostDataLength)
   558 						{
   559 						TInt len;
   560 						if (hostDataLength - c >  KBOTMaxBufSize)
   561 							{
   562 							len = KBOTMaxBufSize;
   563 							}
   564 						else
   565 							{
   566 							len = hostDataLength - c;
   567 							}
   568 
   569 							TPtrC8 ptr(NULL, 0);
   570 							ptr.Set((TUint8*)iPaddingBufPtr.Ptr(), len);
   571 							WriteUsb(status, ptr, len);
   572 							User::WaitForRequest(status);
   573 							c +=  KBOTMaxBufSize;
   574 						}
   575 					}
   576 
   577 				if (iReadSetUp)  //read buffer WAS set up - case (8)
   578 					{
   579 					__PRINT(_L("Case 8\n"));
   580 					SendCSW(iCbwTag, hostDataLength, EPhaseError);
   581 					  //don't care to reset any flag - should get reset recovery
   582 					}
   583 				else   // case (4)
   584 					{
   585 					__PRINT(_L("Case 4\n"));
   586 					SendCSW(iCbwTag, hostDataLength, iCmdStatus);
   587 					}
   588 				return;
   589 				}	// if (!iWriteSetUp)
   590 
   591 //==================
   592 			TUint deviceDataLength = static_cast<TUint>(iWriteBufPtr.Length());
   593 			iDataResidue =hostDataLength - deviceDataLength ;
   594 			__PRINT2(_L("Device data length = %d, DataResidue (write to host)=%d\n"), deviceDataLength, iDataResidue);
   595 
   596 			if (deviceDataLength < hostDataLength  &&
   597 				hostDataLength < KBOTMaxBufSize )
   598 					{
   599 					__PRINT(_L("Case 5 (padding)\n"));
   600 					SetPaddingBufPtr(hostDataLength);
   601 					iPaddingBufPtr.Zero();
   602 					iPaddingBufPtr.Append(iWriteBufPtr);
   603 					iStallAllowed = EFalse;
   604 					__PRINT1(_L("iPaddingBufPtr.Length = %d\n"),iPaddingBufPtr.Length());
   605 					TPtrC8 ptr(NULL, 0);
   606 					ptr.Set((TUint8*)iPaddingBufPtr.Ptr(), hostDataLength);
   607 					WriteData(iStatus, ptr, hostDataLength, EFalse);
   608 					return;
   609 					}
   610 
   611 //===================
   612 
   613 			if (deviceDataLength == hostDataLength)  	//case (6)[==]
   614 				{
   615 				__PRINT(_L("Case 6\n"));
   616 				WriteData(iStatus, iWriteBufPtr, deviceDataLength);
   617 				return;
   618 				}
   619 			else if (deviceDataLength < hostDataLength)	//case (5)[<]
   620 				{
   621 				__PRINT(_L("Case 5\n"));
   622 				WriteData(iStatus, iWriteBufPtr, deviceDataLength, ETrue);		// Send ZLP
   623 				return;
   624 				}
   625 			else 										// deviceDataLength > hostDataLength - case (7)
   626 				{
   627 				__PRINT(_L("Case 7\n"));
   628 				iCmdStatus = EPhaseError;
   629 				iDataResidue = 0;
   630 				WriteData(iStatus, iWriteBufPtr, hostDataLength);
   631 				return;
   632 				}
   633 			}
   634 		else  //read data from host
   635 			{
   636 			if (!iReadSetUp)
   637 				{
   638 				iDataResidue = hostDataLength;
   639 				__PRINT(_L("Read buffer was not setup\n"));
   640 //				Use next block instead of StallEndpointAndWaitForClear(OutEndpoint);
   641 				DiscardData(hostDataLength);
   642 
   643 				if (iWriteSetUp) //case (10)
   644 					{
   645 					__PRINT(_L("case 10\n"));
   646 					SendCSW(iCbwTag, hostDataLength, EPhaseError);
   647 					}
   648 				else // case (9)
   649 					{
   650 					__PRINT(_L("Case 9\n"));
   651 					SendCSW(iCbwTag, hostDataLength, iCmdStatus);
   652 					}
   653 
   654 				return;
   655 				}
   656 
   657 			TUint deviceDataLength = iBufSize;
   658 			iDataResidue = hostDataLength;  // calculate residue later
   659 
   660 			__PRINT2(_L("deviceDataLength = iBufSize = %d, DataResidue = HDL for now (read from host) =%d\n"),deviceDataLength,iDataResidue);
   661 
   662 			if (deviceDataLength <= hostDataLength)  // case (11) and (12)
   663 				{
   664 				__PRINT(_L("Case 11 or 12\n"));
   665 				ReadData(deviceDataLength);
   666 				return;
   667 				}
   668 			if (deviceDataLength > hostDataLength) // case  (13)
   669 				{
   670 				__PRINT(_L("Case 13\n"));
   671                 /**
   672                  * Comment following line in order to pass compliant test.
   673                  * As spec said in case 13:"The device may receive data up to a
   674                  * total of dCBWDataTransferLength."
   675                  * Here we choose to ignore incoming data.
   676                  */
   677 				//StallEndpointAndWaitForClear(OutEndpoint); //Stall Out endpoint
   678                 if (iReadSetUp)
   679                     {
   680 					WriteToClient(hostDataLength);
   681                     iReadSetUp = EFalse;
   682                     }
   683                 SendCSW(iCbwTag, hostDataLength, EPhaseError);
   684 				return;
   685 				}
   686 			}
   687 		}
   688 	else  // Host expected no data transfer
   689 		{
   690 		__PRINT(_L("No data transfer expected\n"));
   691 		iDataResidue = 0;
   692 		if (iWriteSetUp || iReadSetUp)   // case (2) and (3)
   693 			{
   694 			__PRINT(_L("Case 2 or 3\n"));
   695 			SendCSW(iCbwTag, 0, EPhaseError);
   696 			}
   697 		else
   698 			{
   699 			__PRINT(_L("Case 1\n"));
   700 			SendCSW(iCbwTag, 0, iCmdStatus);  //case (1)
   701 			}
   702 		}
   703 	}
   704 
   705 
   706 /**
   707 Check if CBW Valid and Meaningful.
   708 
   709 @return ETrue if CBW is Valid and Meaningful, EFalse otherwise
   710 */
   711 TBool CBulkOnlyTransport::CheckCBW()
   712 	{
   713 	__FNLOG("CBulkOnlyTransport::CheckCBW");
   714 
   715     //
   716     // Check valid
   717     //
   718 
   719     // Check length
   720     if ((TUint) (iCbwBufPtr.Length()) != KCbwLength)
   721         {
   722 		__PRINT2(_L("Bad length: %d != KCbwLength"), iCbwBufPtr.Length(), KCbwLength);
   723 		return EFalse;
   724         }
   725 
   726     // Check signature
   727 	TInt i = KCbwSignatureOffset;
   728 	if (iCbwBufPtr[i  ] != 0x55 ||         // CBW Singature from USB Bulk-Only Transport spec
   729 		iCbwBufPtr[i+1] != 0x53 ||
   730 		iCbwBufPtr[i+2] != 0x42 ||
   731 		iCbwBufPtr[i+3] != 0x43)
   732 		{
   733 		__PRINT(_L("Bad signature"));
   734 		__PRINT4(_L(" 0x%x, 0x%x, 0x%x, 0x%x \n"), iCbwBufPtr[i], iCbwBufPtr[i+1], iCbwBufPtr[i+2],iCbwBufPtr[i+3])
   735 		return EFalse;
   736 		}
   737 
   738     //
   739     // Check meaningful
   740     //
   741 
   742     // Check reserved bits ( must be zero )
   743     if ((iCbwBufPtr[KCbwLunOffset] & 0xF0) || (iCbwBufPtr[KCbwCbLengthOffset] & 0xE0))
   744 		{
   745 		__PRINT(_L("Reserved bits not zero\n"));
   746 		return EFalse;
   747 		}
   748 
   749 	// check command block length
   750 	TInt cbwcbLength = iCbwBufPtr[KCbwCbLengthOffset] & 0x1F;
   751 	if (cbwcbLength >KMaxCbwcbLength)
   752 		{
   753 		__PRINT(_L("Incorrect block length\n"));
   754 		return EFalse;
   755 		}
   756 
   757 	//check LUN
   758 	TInt8 lun = static_cast<TUint8>(iCbwBufPtr[KCbwLunOffset] & 0x0f);
   759 	if (iMaxLun < lun)
   760 		{
   761 		__PRINT1(_L("bad lun: %d"), lun);
   762 		return EFalse;
   763 		}
   764 
   765 	return ETrue;
   766 	}
   767 
   768 
   769 /**
   770 Initiate stalling of bulk IN endpoint.
   771 Used when protocol wants to force host to initiate a reset recovery.
   772 */
   773 void CBulkOnlyTransport::SetPermError()
   774 	{
   775 	__FNLOG("CBulkOnlyTransport::SetPermError");
   776     iCurrentState = EPermErr;
   777     Activate(KErrNone);
   778 	}
   779 
   780 
   781 /**
   782 Send data provided by protocol to the host
   783 
   784 @param aLength amount of data (in bytes) to be send to host
   785 */
   786 void CBulkOnlyTransport::WriteData(TRequestStatus& aStatus, TPtrC8& aDes, TUint aLength, TBool aZlpRequired)
   787 	{
   788 	__FNLOG("CBulkOnlyTransport::WriteData");
   789 
   790 	if (IsActive())
   791 		{
   792 		__PRINT(_L("Still active\n"));
   793 		__ASSERT_DEBUG(EFalse, User::Panic(KUsbMsSvrPncCat, EMsBulkOnlyStillActive));
   794 		return;
   795 		}
   796 	WriteUsb(aStatus, aDes, aLength, aZlpRequired);
   797 	iCurrentState = EWritingData;
   798 	SetActive();
   799 	}
   800 
   801 
   802 /**
   803 Send Command Status Wrapper to the host
   804 
   805 @param aTag Echo of Command Block Tag sent by the host.
   806 @param aDataResidue the difference between the amount of data expected by the
   807        host, and the actual amount of data processed by the device.
   808 @param aStatus indicates the success or failure of the command.
   809 */
   810 void CBulkOnlyTransport::SendCSW(TUint aTag, TUint aDataResidue, TCswStatus aStatus)
   811 	{
   812 	__FNLOG("CBulkOnlyTransport::SendCSW");
   813 	__PRINT2(_L("DataResidue = %d, Status = %d \n"), aDataResidue, aStatus);
   814 
   815 	if (IsActive())
   816 		{
   817 		__PRINT(_L("Still active\n"));
   818 		__ASSERT_DEBUG(EFalse, User::Panic(KUsbMsSvrPncCat, EMsBulkOnlyStillActive));
   819 		return;
   820 		}
   821 
   822 	SetCswBufPtr(KCswLength);
   823 	TInt i = KCswSingnatureOffset;
   824 	iCswBufPtr[i  ] = 0x55;   // CSW Singature from USB Bulk-Only Transport spec
   825 	iCswBufPtr[i+1] = 0x53;
   826 	iCswBufPtr[i+2] = 0x42;
   827 	iCswBufPtr[i+3] = 0x53;
   828 
   829 	i = KCswTagOffset;
   830 
   831 	iCswBufPtr[i  ] = static_cast<TUint8>((aTag & 0x000000FF));
   832 	iCswBufPtr[i+1] = static_cast<TUint8>((aTag & 0x0000FF00) >> 8);
   833 	iCswBufPtr[i+2] = static_cast<TUint8>((aTag & 0x00FF0000) >> 16);
   834 	iCswBufPtr[i+3] = static_cast<TUint8>((aTag & 0xFF000000) >> 24);
   835 
   836 	i = KCswDataResidueOffset;
   837 	iCswBufPtr[i  ] = static_cast<TUint8>((aDataResidue & 0x000000FF));
   838 	iCswBufPtr[i+1] = static_cast<TUint8>((aDataResidue & 0x0000FF00) >> 8);
   839 	iCswBufPtr[i+2] = static_cast<TUint8>((aDataResidue & 0x00FF0000) >> 16);
   840 	iCswBufPtr[i+3] = static_cast<TUint8>((aDataResidue & 0xFF000000) >> 24);
   841 
   842 	iCswBufPtr[KCswStatusOffset] = static_cast<TUint8>(aStatus);
   843 
   844 	TPtrC8 ptr(NULL, 0);
   845 	ptr.Set((const TUint8*)iCswBufPtr.Ptr(), KCswLength);
   846 
   847 	WriteUsb(iStatus, ptr, KCswLength);
   848 
   849 	iCurrentState = ESendingCSW;
   850 
   851 	SetActive();
   852 	}
   853 
   854 
   855 /**
   856 Associates the transport with the protocol.  Called during initialization of the controller.
   857 
   858 @param aProtocol reference to the protocol
   859 */
   860 void CBulkOnlyTransport::RegisterProtocol(MProtocolBase& aProtocol)
   861 	{
   862 	__FNLOG("CBulkOnlyTransport::RegisterProtocol");
   863 	iProtocol = &aProtocol;
   864 	}
   865 
   866 
   867 /**
   868 Used by CControlInterface
   869 
   870 @return reference to the controller which instantiate the CBulkOnlyTransport
   871 */
   872 CUsbMassStorageController& CBulkOnlyTransport::Controller()
   873 	{
   874 	return iController;
   875 	}
   876 
   877 
   878 /**
   879 @return the number of logical units supported by the device.
   880 Logical Unit Numbers on the device shall be numbered contiguously starting from LUN
   881 0 to a maximum LUN of 15 (Fh).
   882 */
   883 TInt CBulkOnlyTransport::MaxLun()
   884 	{
   885 	return iMaxLun;
   886 	}
   887 
   888 
   889 void CBulkOnlyTransport::GetCommandBufPtr(TPtr8& aDes, TUint aLength) // Set pointer to buffer of specified aLength for command
   890 	{
   891 	aDes.Set(SetCommandBufPtr(aLength));
   892 	}
   893 
   894 void CBulkOnlyTransport::GetReadDataBufPtr(TPtr8& aDes) // Set pointer to buffer into which data is to be read from drive (Read10)
   895 	{
   896 	aDes.Set(SetDataBufPtr());
   897 	}
   898 
   899 
   900 void CBulkOnlyTransport::GetWriteDataBufPtr(TPtrC8& aDes) // Set pointer to buffer from which data is to be written to drive (Write10)
   901 	{
   902 	aDes.Set(iReadBufPtr);
   903 	}
   904 
   905 #ifdef MSDC_MULTITHREADED
   906 void CBulkOnlyTransport::ProcessReadData(TAny* aAddress)
   907 	{
   908 	ExpireData(aAddress);
   909 	}
   910 #endif
   911 
   912 
   913 
   914 
   915 
   916 
   917 
   918