First public contribution.
1 // Copyright (c) 2008-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\drivers\usbcsc\usbcsc_bil.cpp
19 #include <e32std_private.h>
20 #include <d32usbcsc.h>
23 /** @file usbcsc_bil.cpp
25 Buffer Interface Layer for USB Client Device driver stack, using shared chunks.
30 EXPORT_C TInt RDevUsbcScClient::FinalizeInterface()
32 TInt errorOrhandle = DoControl(EControlRealizeInterface); //returns a error value or chunk handle
33 TInt r = iSharedChunk.SetReturnedHandle(errorOrhandle);
34 iEndpointStatus = 0x00; //all endpoints are closed at the moment
35 iAlternateSetting = 0;
42 EXPORT_C TInt RDevUsbcScClient::FinalizeInterface(RChunk*& aChunk)
44 TInt errorOrhandle = DoControl(EControlRealizeInterface);
45 iSharedChunk.SetReturnedHandle(errorOrhandle);
46 iEndpointStatus = 0x00; //all endpoints are closed at the moment
47 iAlternateSetting = 0;
48 return aChunk->SetReturnedHandle(errorOrhandle);
52 EXPORT_C TInt RDevUsbcScClient::OpenEndpoint(TEndpointBuffer& aEpB, TInt aEpI)
54 TUsbcScHdrEndpointRecord* endpointInf = NULL;
55 TBuf8<KUsbDescSize_Endpoint> descriptor;
56 TUsbcScChunkHeader chunkHeader(iSharedChunk);
57 //Do some validity checks
58 if((aEpB.iInState != TEndpointBuffer::ENotValid) && (aEpB.iOutState != TEndpointBuffer::ENotValid))
61 TInt nEndpoints = chunkHeader.GetNumberOfEndpoints(iAlternateSetting);
62 if ((aEpI < KEp0Number) && (aEpI > nEndpoints)) // Check endpoint number range
65 if(iEndpointStatus & (1 << aEpI)) // Check that endpoint isn't already opene
68 if(aEpI == KEp0Number) //endpoint 0
70 TUsbcScHdrEndpointRecord ep0= TUsbcScHdrEndpointRecord(KUsbcScEndpointZero, KUsbScHdrEpDirectionBiDir | KUsbScHdrEpTypeControl);
71 aEpB.Construct(this,iSharedChunk.Base(), &ep0 ,aEpI,
72 (SUsbcScBufferHeader*) ((TUint)iSharedChunk.Base() + (chunkHeader.iBuffers)->Ep0Out()->Offset()));
74 aEpB.iBufferStartAddr = (TUint8*) ((TUint)iSharedChunk.Base() + (chunkHeader.iBuffers)->Ep0In()->Offset());
75 aEpB.iSize = chunkHeader.iBuffers->Ep0In()->Size();
77 else // If normal endpoint (!ep0)
79 TUsbcScBufferRecord* buf = chunkHeader.GetBuffer(iAlternateSetting,aEpI,endpointInf);
82 // Set up endpoint members
83 aEpB.iBufferStartAddr = (TUint8*) (buf->Offset() + (TUint)iSharedChunk.Base());
84 aEpB.iSize = buf->Size();
85 TInt r = GetEndpointDescriptor(iAlternateSetting, aEpI, descriptor);
86 if(r != KErrNone) // We need this to be able to calculate alignment
91 if (endpointInf->Direction()&KUsbScHdrEpDirectionIn)
92 { //in case of IN endpoints, first endpoint buffer location points to end offset
93 aEpB.Construct(this,iSharedChunk.Base(),endpointInf,aEpI);
94 if (iInAltSetting==KErrEof)
95 aEpB.iInState=TEndpointBuffer::EEOF;
100 SUsbcScBufferHeader *endpointHdr = (SUsbcScBufferHeader *) aEpB.iBufferStartAddr;
101 //In this case,SUsbcScBufferHeader points to full OUT endpoint header
102 aEpB.Construct(this,iSharedChunk.Base(),endpointInf,aEpI, endpointHdr);
105 iEndpointStatus |= (1 << aEpI);
109 RDebug::Printf("iEndpointStatus: %x \n",iEndpointStatus);
115 //Internal, used by RDevUsbcScClient::StartNextOutAlternateSetting(...)
116 //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.
119 TInt RDevUsbcScClient::Drain(TUint aBufferOffset)
121 TUint8* base = iSharedChunk.Base();
122 SUsbcScBufferHeader* endpointHdr = (SUsbcScBufferHeader*) (aBufferOffset+base);
123 TUint localTail = endpointHdr->iBilTail;
124 TUsbcScTransferHeader* currentTransfer;
125 TUint16 next = (iAltSettingSeq+1)&0xFFFF;
130 if (localTail == (TUint) endpointHdr->iHead)
135 currentTransfer = (TUsbcScTransferHeader*) (base + localTail);
137 if (currentTransfer->iAltSettingSeq == next)
139 iNewAltSetting=currentTransfer->iAltSetting; // record new alt setting
144 localTail = currentTransfer->iNext;
147 endpointHdr->iBilTail = localTail;
148 endpointHdr->iTail = localTail;
152 //Internal, used by RDevUsbcScClient::StartNextOutAlternateSetting(...)
153 //This method checks that the OUT buffer is ready for reading an ep.
154 //aBufferOffset - The offset, into the chunk, that the buffer in question, may be found.
156 TInt RDevUsbcScClient::Peek(TUint aBufferOffset)
158 TUint8* base = iSharedChunk.Base();
159 SUsbcScBufferHeader* endpointHdr = (SUsbcScBufferHeader*) (aBufferOffset+base);
160 TUint localTail = endpointHdr->iBilTail;
161 TUsbcScTransferHeader* currentTransfer = (TUsbcScTransferHeader*) (base + localTail);
163 if ((localTail == (TUint)endpointHdr->iHead) || (currentTransfer->iAltSettingSeq != (iAltSettingSeq+1)&0xFFFF))
164 // if alternate setting has not changed
168 iNewAltSetting=currentTransfer->iAltSetting;
173 //Internal, used by RDevUsbcScClient::StartNextOutAlternateSetting(...)
174 //This method is called if an alternate setting change happens from a set of ONLY IN endpoints.
175 //Used to find the least possible alternate setting it can return to the user, stored in iNewAltSetting
176 //Returns the sequence number of the 'latest' alternate setting it can switch to
178 TInt RDevUsbcScClient::FindNextAlternateSetting()
180 TUsbcScChunkHeader chunkHeader(iSharedChunk);
181 TUsbcScHdrEndpointRecord* endpointInf = NULL;
187 RArray <TInt> bufferOffset; // Array to contain all OUT enpoint buffer offsets
189 for (altSet = 0; altSet < chunkHeader.iAltSettings->iNumOfAltSettings ; altSet++)
191 TInt numEndpoints = chunkHeader.GetNumberOfEndpoints(altSet);
192 for (ep = 1; ep <= numEndpoints ; ep ++)
194 bufOff = chunkHeader.GetBuffer(altSet, ep, endpointInf)->Offset();
195 if ((endpointInf->Direction() & KUsbScHdrEpDirectionOut) && (bufferOffset.Find(bufOff) == KErrNotFound))
197 bufferOffset.Append(bufOff);
202 TInt err = KErrNotFound;
203 TUint16 altSetSeqDelta = 0;
204 TUint16 currentaltSetSeqDelta = 0;
205 TBool noNewSettingFound = ETrue;
206 TInt altSetSeq = iAltSettingSeq;
207 TUint8* base = iSharedChunk.Base();
209 for (bufNum = 0; bufNum < bufferOffset.Count(); bufNum++) // Scan all OUT buffers
211 SUsbcScBufferHeader* endpointHdr = (SUsbcScBufferHeader*) (bufferOffset[bufNum] + base);
212 TUint localTail = endpointHdr->iBilTail;
213 TUsbcScTransferHeader* currentTransfer;
214 TUint16 next = (iAltSettingSeq+1)&0xFFFF;
218 if (localTail == (TUint) endpointHdr->iHead)
220 break; // This OUT endpoint buffer has no data, proceed checking with other OUT endpoint buffers
222 currentTransfer = (TUsbcScTransferHeader*) (base + localTail);
224 if (currentTransfer->iAltSettingSeq != iAltSettingSeq)
226 if (currentTransfer->iAltSettingSeq == next)
228 altSetSeq = currentTransfer->iAltSettingSeq;
229 iNewAltSetting = currentTransfer->iAltSetting; // record new alt setting
234 if (noNewSettingFound)
236 altSetSeqDelta = Abs(iAltSettingSeq - currentTransfer->iAltSettingSeq);
237 altSetSeq = currentTransfer->iAltSettingSeq;
238 iNewAltSetting = currentTransfer->iAltSetting; // record new alt setting
239 noNewSettingFound = EFalse;
243 currentaltSetSeqDelta = Abs(iAltSettingSeq - currentTransfer->iAltSettingSeq);
244 if (currentaltSetSeqDelta < altSetSeqDelta)
246 altSetSeqDelta = currentaltSetSeqDelta;
247 altSetSeq = currentTransfer->iAltSettingSeq;
248 iNewAltSetting = currentTransfer->iAltSetting;
254 localTail = currentTransfer->iNext;
257 if (!err) // Found an alt set sequence one after iAltSettingSeq
259 break; // found 'the next' alternate setting, exit for loop
267 EXPORT_C TInt RDevUsbcScClient::StartNextOutAlternateSetting(TBool aFlush)
269 TUsbcScChunkHeader chunkHeader(iSharedChunk);
271 //if endpoints are still open, return KErrInUse
272 if((iEndpointStatus&~1) != 0)
281 TBool inEndpointSet = ETrue;
282 TUsbcScHdrEndpointRecord* endpointInf = NULL;
284 // check if alternate setting contains all IN endpoints
285 noEp = chunkHeader.GetNumberOfEndpoints(iAlternateSetting);
287 // for each used buffer.
288 for (ep=1;ep<=noEp;ep++)
290 bufOff = chunkHeader.GetBuffer(iAlternateSetting,ep,endpointInf)->Offset();
292 if (endpointInf->Direction() & KUsbScHdrEpDirectionOut)
294 inEndpointSet = EFalse;
296 r = Drain(bufOff); // we need to remove anythng in the way, and get it ready for reading.
298 r = Peek(bufOff); // we need to check it is ready for reading!
306 if (inEndpointSet) // If all endpoints in the current alternate setting are IN endpoints
307 { // go through all OUT buffers for alternate setting change
308 altSeq = FindNextAlternateSetting();
311 if((iNewAltSetting == iAlternateSetting) && (!inEndpointSet))
316 // Find/Set IN alternate setting
317 TInt ret = StartNextInAlternateSetting();
318 SUsbcScAlternateSetting* altrec = ((SUsbcScAlternateSetting*) (&ret));
320 if (altrec->iSequence==iAltSettingSeq+1)
322 if (altrec->iSetting!=iNewAltSetting)
324 iInAltSetting=iNewAltSetting;
330 if ((altSeq == iAltSettingSeq) || (iAltSettingSeq == altrec->iSequence))
334 else if (altSeq != altrec->iSequence)
336 iInAltSetting=KErrEof;
339 iInAltSetting=KErrEof;
342 iAlternateSetting = iNewAltSetting;
345 return iAlternateSetting;
349 EXPORT_C TInt RDevUsbcScClient::GetDataTransferChunk(RChunk* & aChunk)
351 aChunk = &iSharedChunk;
357 EXPORT_C TEndpointBuffer::TEndpointBuffer()
358 :iInState(ENotValid),
359 iOutState(ENotValid),
367 // Internal, called by RDevUsbcScClient::OpenEndpoint.
368 void TEndpointBuffer::Construct(RDevUsbcScClient* aClient, TUint8* aBaseAddr, const TUsbcScHdrEndpointRecord* aEpType , TInt aEndpointNumber,SUsbcScBufferHeader* aEndpointHdr)
371 iBaseAddr = (TUint) aBaseAddr;
372 iInState = (((aEpType->Direction())&KUsbScHdrEpDirectionIn) ? EValid : ENotValid);
373 iOutState = (((aEpType->Direction())&KUsbScHdrEpDirectionOut) ? EValid : ENotValid);
374 iBufferNum = (aEpType->iBufferNo==(KUsbcScEndpointZero&0xFF))?KUsbcScEndpointZero:aEpType->iBufferNo;
375 iEndpointNumber = aEndpointNumber;
377 iEndpointHdr = aEndpointHdr;
380 EXPORT_C TInt TEndpointBuffer::GetInBufferRange(TAny*& aStart, TUint& aSize)
386 aStart= iBufferStartAddr;
391 EXPORT_C TInt TEndpointBuffer::GetInBufferRange(TUint& aStart, TUint& aSize)
395 aStart= (TUint) iBufferStartAddr - iBaseAddr;
401 EXPORT_C TInt TEndpointBuffer::GetBuffer(TAny*& aBuffer,TUint& aSize,TBool& aZLP,TRequestStatus& aStatus,TUint aLength)
406 TUsbcScTransferHeader* currentTransfer;
408 do // until we have a transfer with data.
410 iEndpointHdr->iTail = iEndpointHdr->iBilTail;
411 if(iEndpointHdr->iBilTail == iEndpointHdr->iHead) //If no new data, create request
413 r = iClient->ReadDataNotify(iBufferNum,aStatus);
414 if (r!=KErrCompletion) // Data could arrive since we checked.
417 currentTransfer = (TUsbcScTransferHeader*) (iBaseAddr + iEndpointHdr->iBilTail);
419 iEndpointHdr->iBilTail = currentTransfer->iNext;
420 aZLP = (currentTransfer->iFlags & KUsbcScShortPacket)!=EFalse;
422 if(currentTransfer->iAltSettingSeq != (iClient->iAltSettingSeq)) // if alternate setting has changed
424 if (currentTransfer->iAltSettingSeq == (iClient->iAltSettingSeq+1)) //Note- KIS ATM, if multiple alternate setting changes happen
425 iClient->iNewAltSetting = currentTransfer->iAltSetting; //before StartNextOutAlternateSetting is called,
426 //this variable will reflect the latest requested AlternateSetting
429 if (iEndpointNumber != KEp0Number)
434 else if ((currentTransfer->iBytes==0) && (!aZLP))
436 return KErrAlternateSettingChanged;
441 while ((currentTransfer->iBytes==0) && (!aZLP)); // ignore empty transfers
443 aBuffer = currentTransfer->iData.i;
444 aSize = currentTransfer->iBytes;
445 return (currentTransfer->iFlags & KUsbcScStateChange)?KStateChange:KErrCompletion;
448 EXPORT_C TInt TEndpointBuffer::TakeBuffer(TAny*& aBuffer,TUint& aSize,TBool& aZLP,TRequestStatus& aStatus,TUint aLength)
453 TUsbcScTransferHeader* currentTransfer;
455 do // until we have a transfer with data.
457 if(iEndpointHdr->iBilTail == iEndpointHdr->iHead) //If no new data, create request
459 r = iClient->ReadDataNotify(iBufferNum,aStatus);
460 if (r!=KErrCompletion) // Data could arrive since we checked.
466 currentTransfer = (TUsbcScTransferHeader*) (iBaseAddr + iEndpointHdr->iBilTail);
467 iEndpointHdr->iBilTail = currentTransfer->iNext;
468 aZLP = (currentTransfer->iFlags & KUsbcScShortPacket)!=EFalse; // True if short packet else false
470 if(currentTransfer->iAltSettingSeq != (iClient->iAltSettingSeq)) // if alternate setting has changed
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)
481 else if ((currentTransfer->iBytes==0) && (!aZLP))
483 return KErrAlternateSettingChanged;
488 if ((currentTransfer->iBytes==0) && (!aZLP)) // here , if empty transfer with alt setting information, Call expire
490 Expire(currentTransfer->iData.i);
493 while ((currentTransfer->iBytes==0) && (!aZLP)); // ignore empty transfers
495 aBuffer = currentTransfer->iData.i;
496 aSize = currentTransfer->iBytes;
497 return (currentTransfer->iFlags & KUsbcScStateChange)?KStateChange:KErrCompletion;
500 EXPORT_C TInt TEndpointBuffer::Expire()
502 if (!(iOutState != ENotValid))
505 if (iEndpointHdr->iTail != iEndpointHdr->iBilTail)
507 TUsbcScTransferHeader* currentTransfer = (TUsbcScTransferHeader*) (iBaseAddr + iEndpointHdr->iTail);
508 iEndpointHdr->iTail = currentTransfer->iNext;
513 EXPORT_C TInt TEndpointBuffer::Expire(TAny* aAddress)
515 if (!(iOutState != ENotValid))
518 TUint headerSize = sizeof(TUsbcScTransferHeader)-4; // TransferHeader includes 4 bytes of data.
519 TInt transferToExpire = ((TUint) aAddress - headerSize);
520 TInt offsetToExpire = transferToExpire - iBaseAddr;
522 TInt currentTail = iEndpointHdr->iTail;
524 TInt prevTail = NULL;
525 TBool found = EFalse;
526 while (currentTail != iEndpointHdr->iBilTail)
528 TUsbcScTransferHeader* currentTransfer = (TUsbcScTransferHeader*) (iBaseAddr + currentTail);
529 if (currentTail == offsetToExpire) // found which to expire
532 // This offset is to be expired
533 if (prevTail == NULL)
535 // The offset is at the list head
536 iEndpointHdr->iTail = currentTransfer->iNext;
540 // The offset is NOT at the list head
541 // This leaves a GAP in the buffer which will not be filled unless the 'transfers' before 'currentTail' are expired
542 currentTail = currentTransfer->iNext;
543 TUsbcScTransferHeader* prevTransfer = (TUsbcScTransferHeader*) (iBaseAddr + prevTail);
544 prevTransfer->iNext = currentTail;
548 prevTail = currentTail;
549 currentTail = currentTransfer->iNext;
551 return found ? KErrNone : KErrNotFound;
555 EXPORT_C TInt TEndpointBuffer::WriteBuffer(TAny* aBuffer,TUint aSize,TBool aZLP,TRequestStatus& aStatus)
560 iClient->WriteData(iBufferNum, ((TUint)aBuffer - (TUint)iBaseAddr),aSize,aZLP,aStatus);
565 EXPORT_C TInt TEndpointBuffer::WriteBuffer(TUint aOffset,TUint aSize,TBool aZLP,TRequestStatus& aStatus)
570 iClient->WriteData(iBufferNum,aOffset,aSize,aZLP,aStatus);
576 Closes the endpoint buffer
577 @return KErrNone if close is successfull
579 EXPORT_C TInt TEndpointBuffer::Close()
581 if ((iInState == ENotValid) && (iOutState == ENotValid))
583 if (iOutState != ENotValid)
585 TUsbcScTransferHeader* currentTransfer = (TUsbcScTransferHeader*) (iBaseAddr + iEndpointHdr->iTail);
586 //Incase of AlternateSetting changes and using TEndpointBuffer::GetBuffer, iTail is always one 'transfer' behind iBilTail
587 //Incase of AlternateSetting changes and using TEndpointBuffer::TakeBuffer, this shuold force the user to update iTail & only then closes the endpoint buffer
588 if (((TInt) currentTransfer->iNext != iEndpointHdr->iBilTail) && (iEndpointHdr->iTail != iEndpointHdr->iBilTail))
591 iClient->iEndpointStatus &= ~(1 << iEndpointNumber); //reset the bit corresponding to endpoint
592 iInState = ENotValid;
593 iOutState = ENotValid;
599 EXPORT_C TUsbcScChunkHeader::TUsbcScChunkHeader(RChunk aChunk)
602 iBuffers = (TUsbcScChunkBuffersHeader*) (aChunk.Base()+((TUsbcScChunkHdrOffs*)iChunk.Base())->iBuffers);
603 iAltSettings = (TUsbcScChunkAltSettingHeader*) (aChunk.Base()+((TUsbcScChunkHdrOffs*)iChunk.Base())->iAltSettings);
606 EXPORT_C TInt TUsbcScChunkHeader::GetNumberOfEndpoints(TInt aAltSetting)
608 if ((aAltSetting<0) || (aAltSetting>=iAltSettings->iNumOfAltSettings))
610 return *((TInt*) (iAltSettings->iAltTableOffset[aAltSetting] + (TInt) iChunk.Base()));
614 EXPORT_C TUsbcScBufferRecord* TUsbcScChunkHeader::GetBuffer(TInt aAltSetting, TInt aEndpoint, TUsbcScHdrEndpointRecord*& aEndpointInf)
616 if ((aAltSetting<0) || (aAltSetting>=iAltSettings->iNumOfAltSettings))
618 TInt8* iEndpoint = (TInt8*) (iAltSettings->iAltTableOffset[aAltSetting] + (TInt) iChunk.Base());
619 if ((aEndpoint<=0) || (aEndpoint>*iEndpoint))
621 aEndpointInf = (TUsbcScHdrEndpointRecord*) &(iEndpoint[aEndpoint*iAltSettings->iEpRecordSize]);
622 return iBuffers->Buffers(aEndpointInf->iBufferNo);
626 /* Debug functions */
628 EXPORT_C void TEndpointBuffer::Dump()
630 RDebug::Printf("TEndpointBuffer::Dump iBufferStart: 0x%x, iSize: 0x%x, iEndpointNumber: 0x%x, iBufferNum: %d, iInState: 0x%x iOutState: 0x%x\n",
631 iBufferStartAddr,iSize,iEndpointNumber,iBufferNum,iInState,iOutState);