kernel/eka/drivers/usbcsc/usbcsc_bil.cpp
branchRCL_3
changeset 256 c1f20ce4abcf
parent 0 a41df078684a
child 257 3e88ff8f41d5
equal deleted inserted replaced
249:a179b74831c9 256:c1f20ce4abcf
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 2008-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".
    17 
    17 
    18 #include <e32std.h>
    18 #include <e32std.h>
    19 #include <e32std_private.h>
    19 #include <e32std_private.h>
    20 #include <d32usbcsc.h>
    20 #include <d32usbcsc.h>
    21 #include <e32debug.h>
    21 #include <e32debug.h>
    22 
    22 #include "OstTraceDefinitions.h"
       
    23 #ifdef OST_TRACE_COMPILER_IN_USE
       
    24 #include "usbcsc_bilTraces.h"
       
    25 #endif
    23 /** @file usbcsc_bil.cpp
    26 /** @file usbcsc_bil.cpp
    24 
    27 
    25 	Buffer Interface Layer for USB Client Device driver stack, using shared chunks.
    28 	Buffer Interface Layer for USB Client Device driver stack, using shared chunks.
    26 
    29 
    27 	@internalTechnology
    30 	@internalTechnology
    36 	iNewAltSetting = 0;
    39 	iNewAltSetting = 0;
    37 	iAltSettingSeq = 0;
    40 	iAltSettingSeq = 0;
    38 	return r;
    41 	return r;
    39 	}
    42 	}
    40 
    43 
       
    44 // empty a endpoint buffer, this is called when device state enter undefined
       
    45 TInt RDevUsbcScClient::Empty(TUint aBufferOffset)
       
    46 {
       
    47 	TUint8* base = iSharedChunk.Base();
       
    48 	SUsbcScBufferHeader* endpointHdr = (SUsbcScBufferHeader*) (aBufferOffset + base);
       
    49 	TUint localTail = endpointHdr->iBilTail;
       
    50 	TUsbcScTransferHeader* currentTransfer;
       
    51 	TInt err=KErrNone;
       
    52 
       
    53 	while (ETrue)
       
    54 		{
       
    55 		if (localTail == (TUint) endpointHdr->iHead)
       
    56 			{
       
    57 			err = KErrNone;			
       
    58 			break;
       
    59 			}
       
    60 		currentTransfer = (TUsbcScTransferHeader*) (base + localTail);
       
    61 		localTail = currentTransfer->iNext;
       
    62 		} // end while
       
    63 	endpointHdr->iBilTail = localTail;
       
    64 	endpointHdr->iTail = localTail;
       
    65 	return err;
       
    66 }
       
    67 
       
    68 
    41 
    69 
    42 EXPORT_C TInt RDevUsbcScClient::FinalizeInterface(RChunk*& aChunk)
    70 EXPORT_C TInt RDevUsbcScClient::FinalizeInterface(RChunk*& aChunk)
    43 	{
    71 	{
    44 	TInt errorOrhandle = DoControl(EControlRealizeInterface);
    72 	TInt errorOrhandle = DoControl(EControlRealizeInterface);
    45 	iSharedChunk.SetReturnedHandle(errorOrhandle);
    73 	iSharedChunk.SetReturnedHandle(errorOrhandle);
    46 	iEndpointStatus = 0x00; //all endpoints are closed at the moment
    74 	iEndpointStatus = 0x00; //all endpoints are closed at the moment
    47 	iAlternateSetting = 0;
    75 	iAlternateSetting = 0;
    48 	return aChunk->SetReturnedHandle(errorOrhandle);
    76 	return aChunk->SetReturnedHandle(errorOrhandle);
       
    77 	}
       
    78 
       
    79 
       
    80 EXPORT_C void RDevUsbcScClient::ResetAltSetting()
       
    81 	{
       
    82 	if (iAlternateSetting == 0)
       
    83 		return;
       
    84 	TUsbcScChunkHeader chunkHeader(iSharedChunk);
       
    85 	
       
    86 	TInt ep;
       
    87 	TInt noEp;
       
    88 	TUint bufOff;
       
    89 	TUsbcScHdrEndpointRecord* endpointInf = NULL;
       
    90 
       
    91 	// check if alternate setting contains all IN endpoints
       
    92 	noEp = chunkHeader.GetNumberOfEndpoints(iAlternateSetting);
       
    93 
       
    94 	// for each used buffer. 
       
    95 	for (ep=1;ep<=noEp;ep++)
       
    96 		{
       
    97 		bufOff = chunkHeader.GetBuffer(iAlternateSetting,ep,endpointInf)->Offset();	
       
    98 	
       
    99 		if (endpointInf->Direction() & KUsbScHdrEpDirectionOut) 
       
   100 			{
       
   101 			Empty(bufOff); // we need to remove anythng in the way, and get it ready for reading.
       
   102 			}
       
   103 		}
       
   104 	
       
   105 	iAlternateSetting = 0;
    49 	}
   106 	}
    50 
   107 
    51 
   108 
    52 EXPORT_C TInt RDevUsbcScClient::OpenEndpoint(TEndpointBuffer& aEpB, TInt aEpI)
   109 EXPORT_C TInt RDevUsbcScClient::OpenEndpoint(TEndpointBuffer& aEpB, TInt aEpI)
    53 	{
   110 	{
   104 		}
   161 		}
   105 	iEndpointStatus |= (1 << aEpI);	
   162 	iEndpointStatus |= (1 << aEpI);	
   106 
   163 
   107 #ifdef _DEBUG
   164 #ifdef _DEBUG
   108 	aEpB.Dump();
   165 	aEpB.Dump();
   109 	RDebug::Printf("iEndpointStatus: %x \n",iEndpointStatus);
   166 	OstTraceDef1(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, RDEVUSBCSCCLIENT_OPENENDPOINT, "iEndpointStatus: %x \n",iEndpointStatus );
   110 #endif
   167 #endif
   111 	return KErrNone;
   168 	return KErrNone;
   112 	}
   169 	}
   113 
   170 
   114 
   171 
   116 //This drains any old data from an OUT buffer, and gets it ready for reading an ep.
   173 //This drains any old data from an OUT buffer, and gets it ready for reading an ep.
   117 //aBufferOffset - The offset, into the chunk, that the buffer in question, may be found.
   174 //aBufferOffset - The offset, into the chunk, that the buffer in question, may be found.
   118  
   175  
   119 TInt RDevUsbcScClient::Drain(TUint aBufferOffset)
   176 TInt RDevUsbcScClient::Drain(TUint aBufferOffset)
   120 {
   177 {
       
   178 
   121 	TUint8* base = iSharedChunk.Base();
   179 	TUint8* base = iSharedChunk.Base();
   122 	SUsbcScBufferHeader* endpointHdr = (SUsbcScBufferHeader*) (aBufferOffset+base);
   180 	SUsbcScBufferHeader* endpointHdr = (SUsbcScBufferHeader*) (aBufferOffset+base);
   123 	TUint localTail = endpointHdr->iBilTail;
   181 	TUint localTail = endpointHdr->iBilTail;
   124 	TUsbcScTransferHeader* currentTransfer;
   182 	TUsbcScTransferHeader* currentTransfer;
   125 	TUint16 next = (iAltSettingSeq+1)&0xFFFF;
   183 	TUint16 next = (iAltSettingSeq+1)&0xFFFF;
   126 	TInt err=KErrNone;
   184 	TInt err=KErrNone;
       
   185 	TBool aZLP;
   127 
   186 
   128 	while (ETrue)
   187 	while (ETrue)
   129 		{
   188 		{
   130 		if (localTail == (TUint) endpointHdr->iHead)
   189 		if (localTail == (TUint) endpointHdr->iHead)
   131 			{
   190 			{
   133 			break;
   192 			break;
   134 			}
   193 			}
   135 		currentTransfer = (TUsbcScTransferHeader*) (base + localTail);
   194 		currentTransfer = (TUsbcScTransferHeader*) (base + localTail);
   136 
   195 
   137 		if (currentTransfer->iAltSettingSeq == next)
   196 		if (currentTransfer->iAltSettingSeq == next)
   138 			{
   197 			{			
   139 			iNewAltSetting=currentTransfer->iAltSetting; // record new alt setting
   198 			iNewAltSetting=currentTransfer->iAltSetting; // record new alt setting
       
   199 			aZLP = (currentTransfer->iFlags & KUsbcScShortPacket)!=EFalse;
       
   200 			if ((currentTransfer->iBytes==0) && (!aZLP)) // take empty packet which is for alternate setting change
       
   201 				{
       
   202 				localTail = currentTransfer->iNext;
       
   203 				}
   140 			break;
   204 			break;
   141 			}
   205 			}
   142 		else
   206 		else
   143 			{
   207 			{
   144 			localTail = currentTransfer->iNext;
   208 			localTail = currentTransfer->iNext;
   162 
   226 
   163 	if ((localTail == (TUint)endpointHdr->iHead) || (currentTransfer->iAltSettingSeq != (iAltSettingSeq+1)&0xFFFF))
   227 	if ((localTail == (TUint)endpointHdr->iHead) || (currentTransfer->iAltSettingSeq != (iAltSettingSeq+1)&0xFFFF))
   164 		// if alternate setting has not changed
   228 		// if alternate setting has not changed
   165 		return KErrNotReady;
   229 		return KErrNotReady;
   166 	else
   230 	else
   167 		{
   231 		{		
   168 		iNewAltSetting=currentTransfer->iAltSetting;
   232 		iNewAltSetting=currentTransfer->iAltSetting;
   169 		return KErrNone;
   233 		return KErrNone;
   170 		}
   234 		}
   171 }
   235 }
   172 
   236 
   403 	if (iOutState)
   467 	if (iOutState)
   404 		return iOutState;
   468 		return iOutState;
   405 
   469 
   406 	TUsbcScTransferHeader* currentTransfer;
   470 	TUsbcScTransferHeader* currentTransfer;
   407 	TInt r;
   471 	TInt r;
       
   472 	TInt aBilTail;
   408 	do // until we have a transfer with data.
   473 	do // until we have a transfer with data.
   409 		{
   474 		{
   410 		iEndpointHdr->iTail = iEndpointHdr->iBilTail; 
   475 		iEndpointHdr->iTail = iEndpointHdr->iBilTail; 
   411 		if(iEndpointHdr->iBilTail == iEndpointHdr->iHead)  //If no new data, create request
   476 		if(iEndpointHdr->iBilTail == iEndpointHdr->iHead)  //If no new data, create request
   412 			{
   477 			{
   413 			r = iClient->ReadDataNotify(iBufferNum,aStatus);
   478 			r = iClient->ReadDataNotify(iBufferNum,aStatus);
   414 			if (r!=KErrCompletion)  // Data could arrive since we checked.
   479 			if (r!=KErrCompletion)  // Data could arrive since we checked.
   415 				return r;
   480 				return r;
   416 			}
   481 			}
   417 		currentTransfer = (TUsbcScTransferHeader*) (iBaseAddr + iEndpointHdr->iBilTail);
   482 		currentTransfer = (TUsbcScTransferHeader*) (iBaseAddr + iEndpointHdr->iBilTail);
   418 
   483 		aBilTail = iEndpointHdr->iBilTail;
   419 		iEndpointHdr->iBilTail = currentTransfer->iNext;
   484 		iEndpointHdr->iBilTail = currentTransfer->iNext;
   420 		aZLP = (currentTransfer->iFlags & KUsbcScShortPacket)!=EFalse;
   485 		aZLP = (currentTransfer->iFlags & KUsbcScShortPacket)!=EFalse;
       
   486 
       
   487 		if(currentTransfer->iAltSettingSeq != (iClient->iAltSettingSeq))  // if alternate setting has changed
       
   488 			{
       
   489 			if (currentTransfer->iAltSettingSeq == (iClient->iAltSettingSeq+1))	   //Note- KIS ATM, if multiple alternate setting changes happen
       
   490 				{
       
   491 				iClient->iNewAltSetting = currentTransfer->iAltSetting; //before StartNextOutAlternateSetting is called, 		   
       
   492 																	   //this variable will reflect the latest requested AlternateSetting		
       
   493 				}													   
       
   494 
       
   495 
       
   496 			if (iEndpointNumber != KEp0Number)
       
   497 				{
       
   498 				iEndpointHdr->iBilTail = aBilTail;
       
   499 //				iOutState =  EEOF;	
       
   500 				return KErrEof;
       
   501 				}
       
   502 			else if ((currentTransfer->iBytes==0) && (!aZLP)) 
       
   503 				{
       
   504 				return KErrAlternateSettingChanged;
       
   505 				}
       
   506 			}						
       
   507 
       
   508 		}
       
   509 	while ((currentTransfer->iBytes==0) && (!aZLP)); // ignore empty transfers
       
   510 
       
   511 	aBuffer = currentTransfer->iData.i;
       
   512 	aSize = currentTransfer->iBytes;	
       
   513 	return (currentTransfer->iFlags & KUsbcScStateChange)?KStateChange:KErrCompletion;	
       
   514 	}
       
   515 
       
   516 EXPORT_C TInt TEndpointBuffer::TakeBuffer(TAny*& aBuffer,TUint& aSize,TBool& aZLP,TRequestStatus& aStatus,TUint aLength)
       
   517 	{
       
   518 	if (iOutState)
       
   519 		return iOutState;
       
   520 
       
   521 
       
   522 	TUsbcScTransferHeader* currentTransfer;
       
   523 	TInt r;
       
   524 	TInt aBilTail;
       
   525 	do // until we have a transfer with data.
       
   526 		{
       
   527 		if(iEndpointHdr->iBilTail == iEndpointHdr->iHead)  //If no new data, create request
       
   528 			{
       
   529 			r = iClient->ReadDataNotify(iBufferNum,aStatus);
       
   530 			if (r!=KErrCompletion)  // Data could arrive since we checked.
       
   531 				{
       
   532 				return r;
       
   533 				}
       
   534 			}
       
   535 
       
   536 		currentTransfer = (TUsbcScTransferHeader*) (iBaseAddr + iEndpointHdr->iBilTail);
       
   537 		aBilTail = iEndpointHdr->iBilTail;
       
   538 		iEndpointHdr->iBilTail = currentTransfer->iNext;
       
   539 		aZLP = (currentTransfer->iFlags & KUsbcScShortPacket)!=EFalse; // True if short packet else false 
   421 
   540 
   422 		if(currentTransfer->iAltSettingSeq != (iClient->iAltSettingSeq))  // if alternate setting has changed
   541 		if(currentTransfer->iAltSettingSeq != (iClient->iAltSettingSeq))  // if alternate setting has changed
   423 			{
   542 			{
   424 			if (currentTransfer->iAltSettingSeq == (iClient->iAltSettingSeq+1))	   //Note- KIS ATM, if multiple alternate setting changes happen
   543 			if (currentTransfer->iAltSettingSeq == (iClient->iAltSettingSeq+1))	   //Note- KIS ATM, if multiple alternate setting changes happen
   425 				iClient->iNewAltSetting = currentTransfer->iAltSetting; //before StartNextOutAlternateSetting is called, 		   
   544 				iClient->iNewAltSetting = currentTransfer->iAltSetting; //before StartNextOutAlternateSetting is called, 		   
   426 																	   //this variable will reflect the latest requested AlternateSetting
   545 																	   //this variable will reflect the latest requested AlternateSetting
   427 
   546 			
   428 
       
   429 			if (iEndpointNumber != KEp0Number)
   547 			if (iEndpointNumber != KEp0Number)
   430 				{
   548 				{
   431 //				iOutState =  EEOF;	
   549 				iEndpointHdr->iBilTail = aBilTail;
   432 				return KErrEof;
       
   433 				}
       
   434 			else if ((currentTransfer->iBytes==0) && (!aZLP)) 
       
   435 				{
       
   436 				return KErrAlternateSettingChanged;
       
   437 				}
       
   438 			}						
       
   439 
       
   440 		}
       
   441 	while ((currentTransfer->iBytes==0) && (!aZLP)); // ignore empty transfers
       
   442 
       
   443 	aBuffer = currentTransfer->iData.i;
       
   444 	aSize = currentTransfer->iBytes;	
       
   445 	return (currentTransfer->iFlags & KUsbcScStateChange)?KStateChange:KErrCompletion;	
       
   446 	}
       
   447 
       
   448 EXPORT_C TInt TEndpointBuffer::TakeBuffer(TAny*& aBuffer,TUint& aSize,TBool& aZLP,TRequestStatus& aStatus,TUint aLength)
       
   449 	{
       
   450 	if (iOutState)
       
   451 		return iOutState;
       
   452 
       
   453 	TUsbcScTransferHeader* currentTransfer;
       
   454 	TInt r;
       
   455 	do // until we have a transfer with data.
       
   456 		{
       
   457 		if(iEndpointHdr->iBilTail == iEndpointHdr->iHead)  //If no new data, create request
       
   458 			{
       
   459 			r = iClient->ReadDataNotify(iBufferNum,aStatus);
       
   460 			if (r!=KErrCompletion)  // Data could arrive since we checked.
       
   461 				{
       
   462 				return r;
       
   463 				}
       
   464 			}
       
   465 
       
   466 		currentTransfer = (TUsbcScTransferHeader*) (iBaseAddr + iEndpointHdr->iBilTail);
       
   467 		iEndpointHdr->iBilTail = currentTransfer->iNext;
       
   468 		aZLP = (currentTransfer->iFlags & KUsbcScShortPacket)!=EFalse; // True if short packet else false 
       
   469 
       
   470 		if(currentTransfer->iAltSettingSeq != (iClient->iAltSettingSeq))  // if alternate setting has changed
       
   471 			{
       
   472 			if (currentTransfer->iAltSettingSeq == (iClient->iAltSettingSeq+1))	   //Note- KIS ATM, if multiple alternate setting changes happen
       
   473 				iClient->iNewAltSetting = currentTransfer->iAltSetting; //before StartNextOutAlternateSetting is called, 		   
       
   474 																	   //this variable will reflect the latest requested AlternateSetting
       
   475 			Expire(currentTransfer->iData.i);
       
   476 			if (iEndpointNumber != KEp0Number)
       
   477 				{
       
   478 //				iOutState = EEOF;
   550 //				iOutState = EEOF;
   479 				return KErrEof;
   551 				return KErrEof;
   480 				}
   552 				}
   481 			else if ((currentTransfer->iBytes==0) && (!aZLP)) 
   553 			else if ((currentTransfer->iBytes==0) && (!aZLP)) 
   482 				{
   554 				{
       
   555 				Expire(currentTransfer->iData.i);
   483 				return KErrAlternateSettingChanged;
   556 				return KErrAlternateSettingChanged;
   484 				}
   557 				}
       
   558 			Expire(currentTransfer->iData.i);
   485 
   559 
   486 			}	
   560 			}	
   487 
   561 
   488 		if ((currentTransfer->iBytes==0) && (!aZLP)) // here , if empty transfer with alt setting information, Call expire 
   562 		if ((currentTransfer->iBytes==0) && (!aZLP)) // here , if empty transfer with alt setting information, Call expire 
   489 			{
   563 			{
   521 
   595 
   522 	TInt currentTail = iEndpointHdr->iTail;
   596 	TInt currentTail = iEndpointHdr->iTail;
   523 
   597 
   524 	TInt prevTail = NULL;
   598 	TInt prevTail = NULL;
   525 	TBool found = EFalse;
   599 	TBool found = EFalse;
       
   600 	
   526 	while (currentTail != iEndpointHdr->iBilTail)
   601 	while (currentTail != iEndpointHdr->iBilTail)
   527 		{
   602 		{			
   528 		TUsbcScTransferHeader* currentTransfer = (TUsbcScTransferHeader*) (iBaseAddr + currentTail);
   603 		TUsbcScTransferHeader* currentTransfer = (TUsbcScTransferHeader*) (iBaseAddr + currentTail);
   529 		if (currentTail == offsetToExpire)		// found which to expire
   604 		if (currentTail == offsetToExpire)		// found which to expire
   530 			{
   605 			{			
   531 			found = ETrue;
   606 			found = ETrue;
   532 			// This offset is to be expired
   607 			// This offset is to be expired
   533 			if (prevTail == NULL)
   608 			if (prevTail == NULL)
   534 				{
   609 				{
   535 				// The offset is at the list head
   610 				// The offset is at the list head
   545 				}
   620 				}
   546 			break;
   621 			break;
   547 			}
   622 			}
   548 		prevTail = currentTail;
   623 		prevTail = currentTail;
   549 		currentTail = currentTransfer->iNext;
   624 		currentTail = currentTransfer->iNext;
   550 		}
   625 		}	
   551 	return found ? KErrNone : KErrNotFound;
   626 	return found ? KErrNone : KErrNotFound;
   552 	}
   627 	}
   553 
   628 
   554 	
   629 	
   555 EXPORT_C TInt TEndpointBuffer::WriteBuffer(TAny* aBuffer,TUint aSize,TBool aZLP,TRequestStatus& aStatus)
   630 EXPORT_C TInt TEndpointBuffer::WriteBuffer(TAny* aBuffer,TUint aSize,TBool aZLP,TRequestStatus& aStatus)
   625 
   700 
   626 /* Debug functions */
   701 /* Debug functions */
   627 
   702 
   628 EXPORT_C void TEndpointBuffer::Dump()
   703 EXPORT_C void TEndpointBuffer::Dump()
   629 	{
   704 	{
   630 	RDebug::Printf("TEndpointBuffer::Dump iBufferStart: 0x%x, iSize: 0x%x, iEndpointNumber: 0x%x, iBufferNum: %d, iInState: 0x%x iOutState: 0x%x\n",
   705 	OstTraceDefExt5(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TENDPOINTBUFFER_DUMP, "TEndpointBuffer::Dump iBufferStart: 0x%x, iSize: 0x%x, iEndpointNumber: 0x%x, iBufferNum: %d, iInState: 0x%x",
   631 							iBufferStartAddr,iSize,iEndpointNumber,iBufferNum,iInState,iOutState);
   706             (TUint)iBufferStartAddr,iSize,iEndpointNumber,iBufferNum, (TUint)iInState);
   632 	}
   707 
   633 
   708     OstTraceDef1(OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TENDPOINTBUFFER_DUMP_DUP1, " iOutState: 0x%x\n", iOutState);
       
   709 	}
       
   710