author | hgs |
Mon, 11 Oct 2010 17:54:41 +0100 | |
changeset 286 | 48e57fb1237e |
parent 253 | d37db4dcc88d |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 2000-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 |
// e32\drivers\usbc\usbdma.cpp |
|
15 |
// LDD for USB Device driver stack: |
|
16 |
// Management of DMA-capable data buffers. |
|
17 |
// |
|
18 |
// |
|
19 |
||
20 |
/** |
|
21 |
@file usbdma.cpp |
|
22 |
@internalTechnology |
|
23 |
*/ |
|
24 |
||
25 |
#include <drivers/usbc.h> |
|
253 | 26 |
#include "OstTraceDefinitions.h" |
27 |
#ifdef OST_TRACE_COMPILER_IN_USE |
|
28 |
#include "usbdmaTraces.h" |
|
29 |
#endif |
|
30 |
||
0 | 31 |
|
32 |
||
33 |
#if defined(_DEBUG) |
|
34 |
static const char KUsbPanicLdd[] = "USB LDD"; |
|
35 |
#endif |
|
36 |
||
37 |
||
38 |
TDmaBuf::TDmaBuf(TUsbcEndpointInfo* aEndpointInfo, TInt aBandwidthPriority) |
|
39 |
: iBufBasePtr(NULL), |
|
40 |
iCurrentDrainingBuffer(NULL), |
|
41 |
iCurrentPacket(0), |
|
42 |
iCurrentPacketIndexArray(NULL), |
|
43 |
iCurrentPacketSizeArray(NULL) |
|
44 |
{ |
|
45 |
iMaxPacketSize = aEndpointInfo->iSize; |
|
46 |
iEndpointType = aEndpointInfo->iType; |
|
47 |
||
48 |
switch (aEndpointInfo->iType) |
|
49 |
{ |
|
50 |
case KUsbEpTypeControl: |
|
51 |
iBufSz = KUsbcDmaBufSzControl; |
|
52 |
iNumberofBuffers = KUsbcDmaBufNumControl; |
|
53 |
break; |
|
54 |
case KUsbEpTypeIsochronous: |
|
55 |
iBufSz = KUsbcDmaBufSzIsochronous; |
|
56 |
iNumberofBuffers = KUsbcDmaBufNumIsochronous; |
|
57 |
break; |
|
58 |
case KUsbEpTypeBulk: |
|
59 |
{ |
|
60 |
if (aEndpointInfo->iDir == KUsbEpDirOut) |
|
61 |
{ |
|
62 |
const TInt priorityOUT = aBandwidthPriority & 0x0f; |
|
63 |
iBufSz = KUsbcDmaBufSizesBulkOUT[priorityOUT]; |
|
64 |
} |
|
65 |
else |
|
66 |
{ |
|
67 |
const TInt priorityIN = (aBandwidthPriority >> 4) & 0x0f; |
|
68 |
iBufSz = KUsbcDmaBufSizesBulkIN[priorityIN]; |
|
69 |
} |
|
70 |
iNumberofBuffers = KUsbcDmaBufNumBulk; |
|
71 |
} |
|
72 |
break; |
|
73 |
case KUsbEpTypeInterrupt: |
|
74 |
iBufSz = KUsbcDmaBufSzInterrupt; |
|
75 |
iNumberofBuffers = KUsbcDmaBufNumInterrupt; |
|
76 |
break; |
|
77 |
default: |
|
78 |
iBufSz = 0; |
|
79 |
iNumberofBuffers = 0; |
|
80 |
} |
|
81 |
||
82 |
if (aEndpointInfo->iDir == KUsbEpDirIn) |
|
83 |
{ |
|
84 |
iNumberofBuffers = 1; // IN endpoints only have 1 buffer |
|
85 |
} |
|
86 |
||
87 |
for (TInt i = 0; i < KUsbcDmaBufNumMax; i++) |
|
88 |
{ |
|
89 |
// Buffer logical addresses (pointers) |
|
90 |
iBuffers[i] = NULL; |
|
91 |
// Buffer physical addresses |
|
92 |
iBufferPhys[i] = 0; |
|
93 |
// Packet indexes base array |
|
94 |
iPacketIndex[i] = NULL; |
|
95 |
// Packet sizes base array |
|
96 |
iPacketSize[i] = NULL; |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
||
101 |
TInt TDmaBuf::Construct(TUsbcEndpointInfo* aEndpointInfo) |
|
102 |
{ |
|
103 |
if (aEndpointInfo->iDir != KUsbEpDirIn) |
|
104 |
{ |
|
105 |
// IN endpoints don't need a packet array |
|
106 |
||
107 |
// At most 2 packets (clump of max packet size packets) + possible zlp |
|
108 |
TUsbcPacketArray* bufPtr = iPacketInfoStorage; |
|
109 |
// this divides up the packet indexing & packet size array over the number of buffers |
|
253 | 110 |
OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_CONSTRUCT, |
111 |
"TDmaBuf::Construct() array base=0x%08x", bufPtr ); |
|
0 | 112 |
for (TInt i = 0; i < iNumberofBuffers; i++) |
113 |
{ |
|
114 |
iPacketIndex[i] = bufPtr; |
|
115 |
bufPtr += KUsbcDmaBufMaxPkts; |
|
116 |
iPacketSize[i] = bufPtr; |
|
117 |
bufPtr += KUsbcDmaBufMaxPkts; |
|
253 | 118 |
OstTraceDefExt4( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_CONSTRUCT_DUP1, |
119 |
"TDmaBuf::Construct() packetIndex[%d]=0x%08x packetSize[%d]=0x%08x", |
|
120 |
i, reinterpret_cast<TUint>(iPacketIndex[i]), i, |
|
121 |
reinterpret_cast<TUint>(iPacketSize[i]) ); |
|
122 |
||
0 | 123 |
} |
124 |
} |
|
125 |
else |
|
126 |
{ |
|
253 | 127 |
OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_CONSTRUCT_DUP2, |
128 |
"TDmaBuf::Construct() IN endpoint" ); |
|
0 | 129 |
} |
130 |
Flush(); |
|
131 |
return KErrNone; |
|
132 |
} |
|
133 |
||
134 |
||
135 |
TDmaBuf::~TDmaBuf() |
|
136 |
{ |
|
253 | 137 |
OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_FLOW, TDMABUF_TDMABUF_DES, "TDmaBuf::~TDmaBuf()" ); |
0 | 138 |
} |
139 |
||
140 |
TInt TDmaBuf::BufferTotalSize() const |
|
141 |
{ |
|
142 |
return iBufSz * iNumberofBuffers; |
|
143 |
} |
|
144 |
||
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
145 |
TInt TDmaBuf::BufferSize() const |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
146 |
{ |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
147 |
return iBufSz; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
148 |
} |
0 | 149 |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
150 |
TInt TDmaBuf::SetBufferAddr(TInt aBufInd, TUint8* aBufAddr) |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
151 |
{ |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
152 |
__ASSERT_DEBUG((aBufInd < iNumberofBuffers), |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
153 |
Kern::Fault(KUsbPanicLdd, __LINE__)); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
154 |
iDrainable[aBufInd] = iCanBeFreed[aBufInd] = EFalse; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
iBuffers[aBufInd] = aBufAddr; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
iBufferPhys[aBufInd] = Epoc::LinearToPhysical((TLinAddr)aBufAddr); |
253 | 157 |
OstTraceDefExt2( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_SETBUFFERADDR, |
158 |
"TDmaBuf::SetBufferAddr() iBuffers[%d]=0x%08x", aBufInd, |
|
159 |
reinterpret_cast<TUint>(iBuffers[aBufInd]) ); |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
return KErrNone; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
} |
0 | 162 |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
163 |
TInt TDmaBuf::BufferNumber() const |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
164 |
{ |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
165 |
return iNumberofBuffers; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
} |
0 | 167 |
|
168 |
void TDmaBuf::SetMaxPacketSize(TInt aSize) |
|
169 |
{ |
|
170 |
iMaxPacketSize = aSize; |
|
171 |
} |
|
172 |
||
173 |
||
174 |
void TDmaBuf::Flush() |
|
175 |
{ |
|
253 | 176 |
OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_FLUSH, |
177 |
"TDmaBuf::Flush %x", this ); |
|
0 | 178 |
iRxActive = EFalse; |
179 |
iTxActive = EFalse; |
|
180 |
iExtractOffset = 0; |
|
181 |
iTotalRxBytesAvail = 0; |
|
182 |
iTotalRxPacketsAvail = 0; |
|
183 |
iCurrentDrainingBufferIndex = KUsbcInvalidBufferIndex; |
|
184 |
iCurrentFillingBufferIndex = 0; |
|
185 |
iDrainQueueIndex = KUsbcInvalidDrainQueueIndex; |
|
186 |
for (TInt i = 0; i < KUsbcDmaBufNumMax; i++) |
|
187 |
{ |
|
188 |
iDrainable[i] = EFalse; |
|
189 |
iCanBeFreed[i] = EFalse; |
|
190 |
iNumberofBytesRx[i] = 0; |
|
191 |
iNumberofPacketsRx[i] = 0; |
|
192 |
iError[i] = KErrGeneral; |
|
193 |
iDrainQueue[i] = KUsbcInvalidBufferIndex; |
|
194 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
195 |
iFillingOrderArray[i] = 0; |
|
196 |
iNumberofBytesRxRemain[i] = 0; |
|
197 |
iNumberofPacketsRxRemain[i] = 0; |
|
198 |
#endif |
|
199 |
} |
|
200 |
// Drain queue is 1 oversized |
|
201 |
iDrainQueue[KUsbcDmaBufNumMax] = KUsbcInvalidBufferIndex; |
|
202 |
||
203 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
204 |
iFillingOrder = 0; |
|
205 |
iDrainingOrder = 0; |
|
206 |
#endif |
|
207 |
} |
|
208 |
||
209 |
||
210 |
void TDmaBuf::RxSetActive() |
|
211 |
{ |
|
253 | 212 |
OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_RXSETACTIVE, |
213 |
"TDmaBuf::RxSetActive %x", this ); |
|
0 | 214 |
iRxActive = ETrue; |
215 |
} |
|
216 |
||
217 |
||
218 |
void TDmaBuf::RxSetInActive() |
|
219 |
{ |
|
253 | 220 |
OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_RXSETINACTIVE, |
221 |
"TDmaBuf::RxSetInActive %x", this ); |
|
0 | 222 |
iRxActive = EFalse; |
223 |
} |
|
224 |
||
225 |
||
226 |
TBool TDmaBuf::RxIsActive() |
|
227 |
{ |
|
228 |
return iRxActive; |
|
229 |
} |
|
230 |
||
231 |
||
232 |
void TDmaBuf::TxSetActive() |
|
233 |
{ |
|
234 |
iTxActive = ETrue; |
|
235 |
} |
|
236 |
||
237 |
||
238 |
void TDmaBuf::TxSetInActive() |
|
239 |
{ |
|
240 |
iTxActive = EFalse; |
|
241 |
} |
|
242 |
||
243 |
||
244 |
TBool TDmaBuf::TxIsActive() |
|
245 |
{ |
|
246 |
return iTxActive; |
|
247 |
} |
|
248 |
||
249 |
||
250 |
/**************************** Rx DMA Buffer Access *************************/ |
|
251 |
||
252 |
void TDmaBuf::ModifyTotalRxBytesAvail(TInt aVal) |
|
253 |
{ |
|
254 |
iTotalRxBytesAvail += aVal; |
|
255 |
} |
|
256 |
||
257 |
||
258 |
void TDmaBuf::ModifyTotalRxPacketsAvail(TInt aVal) |
|
259 |
{ |
|
260 |
iTotalRxPacketsAvail += aVal; |
|
261 |
} |
|
262 |
||
263 |
||
264 |
TBool TDmaBuf::AdvancePacket() |
|
265 |
{ |
|
266 |
ModifyTotalRxPacketsAvail(-1); |
|
267 |
TBool r = ETrue; |
|
268 |
__ASSERT_DEBUG((iCurrentDrainingBufferIndex >= 0), |
|
269 |
Kern::Fault(KUsbPanicLdd, __LINE__)); |
|
270 |
if (++iCurrentPacket >= iNumberofPacketsRx[iCurrentDrainingBufferIndex]) |
|
271 |
{ |
|
272 |
r = NextDrainableBuffer(); |
|
273 |
} |
|
274 |
iExtractOffset = 0; |
|
275 |
__ASSERT_DEBUG((iCurrentDrainingBufferIndex == KUsbcInvalidBufferIndex) || |
|
276 |
(iCurrentPacket < KUsbcDmaBufMaxPkts), |
|
277 |
Kern::Fault(KUsbPanicLdd, __LINE__)); |
|
278 |
return r; |
|
279 |
} |
|
280 |
||
281 |
||
282 |
TInt TDmaBuf::PeekNextPacketSize() |
|
283 |
{ |
|
284 |
TUint pkt = iCurrentPacket; |
|
285 |
TInt index = iCurrentDrainingBufferIndex; |
|
286 |
TInt size = -1; |
|
287 |
if (pkt >= iNumberofPacketsRx[index]) |
|
288 |
{ |
|
289 |
index = PeekNextDrainableBuffer(); |
|
290 |
pkt = 0; |
|
291 |
} |
|
292 |
||
293 |
if ((index != KUsbcInvalidBufferIndex) && iNumberofPacketsRx[index]) |
|
294 |
{ |
|
295 |
const TUsbcPacketArray* sizeArray = iPacketSize[index]; |
|
296 |
size = (TInt)sizeArray[pkt]; |
|
297 |
} |
|
298 |
||
299 |
__ASSERT_DEBUG((iCurrentDrainingBufferIndex == KUsbcInvalidBufferIndex) || |
|
300 |
(iCurrentPacket < KUsbcDmaBufMaxPkts), |
|
301 |
Kern::Fault(KUsbPanicLdd, __LINE__)); |
|
302 |
return size; |
|
303 |
} |
|
304 |
||
305 |
||
306 |
inline TInt TDmaBuf::GetCurrentError() |
|
307 |
{ |
|
308 |
// USB bus errors are v.rare. To avoid having an error code attached to every packet since |
|
309 |
// almost every errorcode will be KErrNone, we have a single error code per buffer |
|
310 |
// If the error code is != KErrNone then it refers to the LAST packet in the buffer |
|
311 |
TInt errorCode = KErrNone; |
|
312 |
//Check the index, it's not equal to negative (-1) value defined in |
|
313 |
//KUsbcInvalidBufferIndex. |
|
314 |
__ASSERT_DEBUG((iCurrentDrainingBufferIndex >= 0), |
|
315 |
Kern::Fault(KUsbPanicLdd, __LINE__)); |
|
316 |
||
317 |
if (iError[iCurrentDrainingBufferIndex] != KErrNone) |
|
318 |
{ |
|
319 |
// See if we are at the last packet |
|
320 |
if ((iCurrentPacket + 1) == iNumberofPacketsRx[iCurrentDrainingBufferIndex]) |
|
321 |
{ |
|
322 |
errorCode = iError[iCurrentDrainingBufferIndex]; |
|
323 |
} |
|
324 |
} |
|
325 |
return errorCode; |
|
326 |
} |
|
327 |
||
328 |
||
329 |
// used to decide whether a client read can complete straight away |
|
330 |
TBool TDmaBuf::IsReaderEmpty() |
|
331 |
{ |
|
253 | 332 |
OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_ISREADEREMPTY, |
333 |
"TDmaBuf::IsReaderEmpty iTotalRxPacketsAvail=%d", iTotalRxPacketsAvail); |
|
0 | 334 |
return (iTotalRxPacketsAvail == 0); |
335 |
} |
|
336 |
||
337 |
||
338 |
void TDmaBuf::ReadXferComplete(TInt aNoBytesRecv, TInt aNoPacketsRecv, TInt aErrorCode) |
|
339 |
{ |
|
340 |
// Adjust pending packet |
|
341 |
if ((aNoBytesRecv == 0) && (aErrorCode != KErrNone)) |
|
342 |
{ |
|
343 |
// Make the buffer available for reuse |
|
344 |
iDrainable[iCurrentFillingBufferIndex] = EFalse; |
|
345 |
return; |
|
346 |
} |
|
347 |
||
348 |
ModifyTotalRxBytesAvail(aNoBytesRecv); |
|
349 |
ModifyTotalRxPacketsAvail(aNoPacketsRecv); |
|
350 |
iNumberofBytesRx[iCurrentFillingBufferIndex] = aNoBytesRecv; |
|
351 |
iNumberofPacketsRx[iCurrentFillingBufferIndex] = aNoPacketsRecv; |
|
352 |
||
353 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
354 |
iNumberofBytesRxRemain[iCurrentFillingBufferIndex] = aNoBytesRecv; |
|
355 |
iNumberofPacketsRxRemain[iCurrentFillingBufferIndex] = aNoPacketsRecv; |
|
356 |
#endif |
|
357 |
||
253 | 358 |
OstTraceDefExt2( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_READXFERCOMPLETE, |
359 |
"TDmaBuf::ReadXferComplete 2 # of bytes=%d # of packets=%d", iTotalRxBytesAvail, iTotalRxPacketsAvail ); |
|
0 | 360 |
iDrainable[iCurrentFillingBufferIndex] = ETrue; |
361 |
iError[iCurrentFillingBufferIndex] = aErrorCode; |
|
362 |
AddToDrainQueue(iCurrentFillingBufferIndex); |
|
363 |
if (iCurrentDrainingBufferIndex == KUsbcInvalidBufferIndex) |
|
364 |
{ |
|
365 |
NextDrainableBuffer(); |
|
366 |
} |
|
367 |
} |
|
368 |
||
369 |
||
370 |
TInt TDmaBuf::RxGetNextXfer(TUint8*& aBufferAddr, TUsbcPacketArray*& aIndexArray, |
|
371 |
TUsbcPacketArray*& aSizeArray, TInt& aLength, TPhysAddr& aBufferPhys) |
|
372 |
{ |
|
253 | 373 |
OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_RXGETNEXTXFER, |
374 |
"TDmaBuf::RxGetNextXfer 1" ); |
|
0 | 375 |
if (RxIsActive()) |
376 |
{ |
|
253 | 377 |
OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_RXGETNEXTXFER_DUP1, |
378 |
" ---> RxIsActive, returning" ); |
|
0 | 379 |
return KErrInUse; |
380 |
} |
|
381 |
||
253 | 382 |
OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_RXGETNEXTXFER_DUP2, |
383 |
"TDmaBuf::RxGetNextXfer Current buffer=%d", iCurrentFillingBufferIndex ); |
|
0 | 384 |
if (iDrainable[iCurrentFillingBufferIndex]) |
385 |
{ |
|
386 |
// If the controller refused the last read request, then the current buffer will still be marked |
|
387 |
// as !Drainable, because the controller never completed the read to the ldd. and therefore the buffer |
|
388 |
// can be reused. |
|
389 |
if (!NextFillableBuffer()) |
|
390 |
{ |
|
391 |
return KErrNoMemory; |
|
392 |
} |
|
393 |
} |
|
394 |
||
253 | 395 |
OstTraceDef1( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_RXGETNEXTXFER_DUP3, |
396 |
"TDmaBuf::RxGetNextXfer New buffer=%d", iCurrentFillingBufferIndex ); |
|
0 | 397 |
aBufferAddr = iBuffers[iCurrentFillingBufferIndex]; |
398 |
aBufferPhys = iBufferPhys[iCurrentFillingBufferIndex]; |
|
399 |
aIndexArray = iPacketIndex[iCurrentFillingBufferIndex]; |
|
400 |
aSizeArray = iPacketSize[iCurrentFillingBufferIndex]; |
|
401 |
aLength = iBufSz; |
|
402 |
||
403 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
404 |
iFillingOrderArray[iCurrentFillingBufferIndex] = ++iFillingOrder; |
|
405 |
#endif |
|
406 |
||
407 |
return KErrNone; |
|
408 |
} |
|
409 |
||
410 |
||
411 |
TInt TDmaBuf::RxCopyPacketToClient(DThread* aThread, TClientBuffer *aTcb, TInt aLength) |
|
412 |
{ |
|
253 | 413 |
OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_RXCOPYPACKETTOCLIENT, |
414 |
"TDmaBuf::RxCopyPacketToClient 1" ); |
|
0 | 415 |
|
416 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
417 |
const TInt numPkts = NoRxPackets(); |
|
418 |
const TInt numPktsAlt = NoRxPacketsAlt(); |
|
419 |
const TInt numBytes = RxBytesAvailable(); |
|
420 |
const TInt numBytesAlt = NoRxBytesAlt(); |
|
421 |
||
422 |
if (numPkts != numPktsAlt) |
|
423 |
{ |
|
253 | 424 |
OstTraceExt2( TRACE_NORMAL, TDMABUF_RXCOPYPACKETTOCLIENT_DUP1, |
425 |
"TDmaBuf::RxCopyPacketToClient: Error: #pkts mismatch global=%d actual=%d", |
|
426 |
numPkts, numPktsAlt); |
|
0 | 427 |
} |
428 |
if (numBytes != numBytesAlt) |
|
429 |
{ |
|
253 | 430 |
OstTraceExt2( TRACE_NORMAL, TDMABUF_RXCOPYPACKETTOCLIENT_DUP2, |
431 |
"TDmaBuf::RxCopyPacketToClient: Error: #bytes mismatch global=%d actual=%d", |
|
432 |
numBytes, numBytesAlt); |
|
433 |
||
0 | 434 |
} |
435 |
if ((numPkts == 0) && (numBytes !=0)) |
|
436 |
{ |
|
253 | 437 |
OstTraceExt2( TRACE_NORMAL, TDMABUF_RXCOPYPACKETTOCLIENT_DUP3, |
0 | 438 |
"TDmaBuf::RxCopyPacketToClient: Error: global bytes & pkts mismatch pkts=%d bytes=%d", |
439 |
numPkts, numBytes); |
|
440 |
} |
|
441 |
if ((numPktsAlt == 0) && (numBytesAlt !=0)) |
|
442 |
{ |
|
253 | 443 |
OstTraceExt2( TRACE_NORMAL, TDMABUF_RXCOPYPACKETTOCLIENT_DUP4, |
0 | 444 |
"TDmaBuf::RxCopyPacketToClient: Error: actual bytes & pkts mismatch pkts=%d bytes=%d", |
445 |
numPktsAlt, numBytesAlt); |
|
446 |
} |
|
447 |
#endif |
|
448 |
||
449 |
if (!NoRxPackets()) |
|
450 |
return KErrNotFound; |
|
451 |
||
253 | 452 |
OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_RXCOPYPACKETTOCLIENT_DUP5, |
453 |
"TDmaBuf::RxCopyPacketToClient 2" ); |
|
0 | 454 |
// the next condition should be true because we have some packets available |
455 |
// coverity[var_tested_neg] |
|
456 |
if (iCurrentDrainingBufferIndex == KUsbcInvalidBufferIndex) |
|
457 |
{ |
|
458 |
// Marked as Coverity "Intentional" as the member variable |
|
459 |
// iCurrentDrainingBufferIndex is attentionaly negative, from previous |
|
460 |
// initialization to KUsbcInvalidBufferIndex (which equals -1). |
|
461 |
if (!NextDrainableBuffer()) |
|
462 |
return KErrNotFound; |
|
463 |
} |
|
464 |
||
465 |
__ASSERT_DEBUG((iCurrentDrainingBufferIndex >= 0 ), |
|
466 |
Kern::Fault(KUsbPanicLdd, __LINE__)); |
|
467 |
||
468 |
if (!iDrainable[iCurrentDrainingBufferIndex]) |
|
469 |
return KErrNotFound; |
|
470 |
||
471 |
// Calculate copy-from address & adjust for the fact that |
|
472 |
// some data may have already been read from the packet |
|
473 |
TUint8* logicalSrc = iCurrentDrainingBuffer + iCurrentPacketIndexArray[iCurrentPacket] + iExtractOffset; |
|
474 |
TInt packetSz = iCurrentPacketSizeArray[iCurrentPacket]; |
|
475 |
TInt thisPacketSz = packetSz - iExtractOffset; |
|
476 |
TInt errorCode; |
|
477 |
// try and sort out what a "packet" might mean. |
|
478 |
// in a multi-packet dma environment, we might see super-packets |
|
479 |
// i.e. we might just see one packet, maybe 4K or so long, made of lots of small packets |
|
480 |
// Since we don't know where the packet boundaries will be, we have to assume that |
|
481 |
// any 'packet' larger than the max packet size of the ep is, in fact, a conglomeration |
|
482 |
// of smaller packets. However, for the purposes of the packet count, this is still regarded |
|
483 |
// as a single packet and the packet count only decremented when it is consumed. |
|
484 |
// As before, if the user fails to read an entire packet out then the next packet is moved onto anyway |
|
485 |
// To be safe the user must always supply a buffer of at least max packet size bytes. |
|
486 |
if (thisPacketSz > iMaxPacketSize) |
|
487 |
{ |
|
488 |
// Multiple packets left in buffer |
|
489 |
// calculate number of bytes to end of packet |
|
490 |
if (iEndpointType == KUsbEpTypeBulk) |
|
491 |
{ |
|
492 |
thisPacketSz = iMaxPacketSize - (iExtractOffset & (iMaxPacketSize - 1)); |
|
493 |
} |
|
494 |
else |
|
495 |
{ |
|
496 |
thisPacketSz = iMaxPacketSize - (iExtractOffset % iMaxPacketSize); |
|
497 |
} |
|
498 |
errorCode = KErrNone; |
|
499 |
} |
|
500 |
else |
|
501 |
{ |
|
502 |
errorCode = GetCurrentError(); // single packet left |
|
503 |
} |
|
504 |
||
505 |
iExtractOffset += thisPacketSz; // iExtractOffset is now at the end of the real or notional packet |
|
506 |
||
507 |
ModifyTotalRxBytesAvail(-thisPacketSz); |
|
508 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
509 |
iNumberofBytesRxRemain[iCurrentDrainingBufferIndex] -= thisPacketSz; |
|
510 |
#endif |
|
511 |
// this can only be untrue if the "packet" is a conglomeration of smaller packets: |
|
512 |
if (iExtractOffset == packetSz) |
|
513 |
{ |
|
514 |
// packet consumed, advance to next packet in buffer |
|
515 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
516 |
iNumberofPacketsRxRemain[iCurrentDrainingBufferIndex] -= 1; |
|
517 |
#endif |
|
518 |
AdvancePacket(); |
|
519 |
} |
|
520 |
||
521 |
TPtrC8 des(logicalSrc, thisPacketSz); |
|
522 |
TInt r=Kern::ThreadBufWrite(aThread, aTcb, des, 0, 0, aThread); |
|
523 |
if (r == KErrNone) |
|
524 |
{ |
|
525 |
r = errorCode; |
|
526 |
} |
|
253 | 527 |
OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_RXCOPYPACKETTOCLIENT_DUP6, |
528 |
"TDmaBuf::RxCopyPacketToClient 3" ); |
|
529 |
||
0 | 530 |
|
531 |
FreeDrainedBuffers(); |
|
532 |
||
533 |
// Use this error code to complete client read request: |
|
534 |
return r; |
|
535 |
} |
|
536 |
||
537 |
||
538 |
TInt TDmaBuf::RxCopyDataToClient(DThread* aThread, TClientBuffer *aTcb, TInt aLength, TUint32& aDestOffset, |
|
539 |
TBool aRUS, TBool& aCompleteNow) |
|
540 |
{ |
|
253 | 541 |
OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_RXCOPYDATATOCLIENT, |
542 |
"TDmaBuf::RxCopyDataToClient 1" ); |
|
0 | 543 |
aCompleteNow = ETrue; |
544 |
||
545 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
546 |
const TInt numPkts = NoRxPackets(); |
|
547 |
const TInt numPktsAlt = NoRxPacketsAlt(); |
|
548 |
const TInt numBytes = RxBytesAvailable(); |
|
549 |
const TInt numBytesAlt = NoRxBytesAlt(); |
|
550 |
||
551 |
if (numPkts != numPktsAlt) |
|
552 |
{ |
|
253 | 553 |
OstTraceExt2( TRACE_NORMAL, TDMABUF_RXCOPYDATATOCLIENT_DUP1, |
0 | 554 |
"TDmaBuf::RxCopyDataToClient: Error: #pkts mismatch global=%d actual=%d", |
555 |
numPkts, numPktsAlt); |
|
556 |
} |
|
557 |
if (numBytes != numBytesAlt) |
|
558 |
{ |
|
253 | 559 |
OstTraceExt2( TRACE_NORMAL, TDMABUF_RXCOPYDATATOCLIENT_DUP2, |
0 | 560 |
"TDmaBuf::RxCopyDataToClient: Error: #bytes mismatch global=%d actual=%d", |
561 |
numBytes, numBytesAlt); |
|
562 |
} |
|
563 |
if ((numPkts == 0) && (numBytes != 0)) |
|
564 |
{ |
|
253 | 565 |
OstTraceExt2( TRACE_NORMAL, TDMABUF_RXCOPYDATATOCLIENT_DUP3, |
0 | 566 |
"TDmaBuf::RxCopyDataToClient: Error: global bytes & pkts mismatch pkts=%d bytes=%d", |
567 |
numPkts, numBytes); |
|
568 |
} |
|
569 |
if ((numPktsAlt == 0) && (numBytesAlt != 0)) |
|
570 |
{ |
|
253 | 571 |
OstTraceExt2( TRACE_NORMAL, TDMABUF_RXCOPYDATATOCLIENT_DUP4, |
0 | 572 |
"TDmaBuf::RxCopyDataToClient: Error: actual bytes & pkts mismatch pkts=%d bytes=%d", |
573 |
numPktsAlt, numBytesAlt); |
|
574 |
} |
|
575 |
#endif |
|
576 |
||
577 |
if (!NoRxPackets()) |
|
578 |
{ |
|
579 |
return KErrNotFound; |
|
580 |
} |
|
581 |
||
582 |
// coverity[var_tested_neg] |
|
583 |
if (iCurrentDrainingBufferIndex == KUsbcInvalidBufferIndex) |
|
584 |
{ |
|
585 |
// Marked as Coverity "Inentional" as the member variable |
|
586 |
// iCurrentDrainingBufferIndex is attentionaly negative, from previous |
|
587 |
// initialization to KUsbcInvalidBufferIndex (which equals -1). |
|
588 |
||
589 |
if (!NextDrainableBuffer()) |
|
590 |
{ |
|
591 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
253 | 592 |
OstTraceExt2( TRACE_NORMAL, TDMABUF_RXCOPYDATATOCLIENT_DUP5, |
593 |
"TDmaBuf::RxCopyDataToClient: Error: No buffer draining=%d, packets=%d", |
|
594 |
iCurrentDrainingBufferIndex, iTotalRxPacketsAvail); |
|
0 | 595 |
#endif |
596 |
return KErrNotFound; |
|
597 |
} |
|
598 |
} |
|
599 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
600 |
||
601 |
__ASSERT_DEBUG((iCurrentDrainingBufferIndex >= 0 ), |
|
602 |
Kern::Fault(KUsbPanicLdd, __LINE__)); |
|
603 |
||
604 |
if (iDrainingOrder != iFillingOrderArray[iCurrentDrainingBufferIndex]) |
|
605 |
{ |
|
253 | 606 |
OstTrace1( TRACE_NORMAL, TDMABUF_RXCOPYDATATOCLIENT_DUP6, |
607 |
"!!! Out of Order Draining TDmaBuf::RxCopyDataToClient 10 draining=%d", |
|
608 |
iCurrentDrainingBufferIndex); |
|
0 | 609 |
} |
610 |
#endif |
|
253 | 611 |
OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_RXCOPYDATATOCLIENT_DUP7, |
612 |
"TDmaBuf::RxCopyDataToClient 2" ); |
|
0 | 613 |
|
614 |
TUint8* blockStartAddr = iCurrentDrainingBuffer + iCurrentPacketIndexArray[iCurrentPacket] + iExtractOffset; |
|
615 |
TUint8* lastEndAddr = blockStartAddr; // going to track the contiguity of the memory |
|
616 |
TUint8* thisStartAddr = blockStartAddr; |
|
617 |
TInt toDo = Min(aLength - (TInt)aDestOffset, iTotalRxBytesAvail); |
|
618 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
619 |
TInt bufnum = iCurrentDrainingBufferIndex; |
|
620 |
#endif |
|
621 |
TInt errorCode = KErrNone; |
|
622 |
TBool isShortPacket = EFalse; |
|
623 |
const TInt maxPacketSizeMask = iMaxPacketSize - 1; |
|
624 |
do |
|
625 |
{ |
|
626 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
627 |
if (bufnum != iCurrentDrainingBufferIndex) |
|
628 |
{ |
|
629 |
bufnum = iCurrentDrainingBufferIndex; |
|
630 |
if (iDrainingOrder != iFillingOrderArray[iCurrentDrainingBufferIndex]) |
|
631 |
{ |
|
253 | 632 |
OstTrace1( TRACE_NORMAL, TDMABUF_RXCOPYDATATOCLIENT_DUP8, |
633 |
"!!! Out of Order Draining TDmaBuf::RxCopyDataToClient 20 draining=%d", |
|
634 |
iCurrentDrainingBufferIndex); |
|
0 | 635 |
} |
636 |
} |
|
637 |
#endif |
|
638 |
if (errorCode == KErrNone) |
|
639 |
{ |
|
640 |
errorCode = GetCurrentError(); |
|
641 |
} |
|
642 |
thisStartAddr = iCurrentDrainingBuffer + iCurrentPacketIndexArray[iCurrentPacket] + iExtractOffset; |
|
643 |
const TInt thisPacketSize = iCurrentPacketSizeArray[iCurrentPacket]; |
|
644 |
const TInt size = thisPacketSize - iExtractOffset; |
|
645 |
if (aRUS) |
|
646 |
{ |
|
647 |
if (iEndpointType == KUsbEpTypeBulk) |
|
648 |
{ |
|
189 | 649 |
if(iExtractOffset & maxPacketSizeMask) |
650 |
{ |
|
651 |
isShortPacket = ((size+iExtractOffset) < iMaxPacketSize) || ((size+iExtractOffset) & maxPacketSizeMask); |
|
652 |
} |
|
653 |
else |
|
654 |
{ |
|
655 |
isShortPacket = (size < iMaxPacketSize) || (size & maxPacketSizeMask); |
|
656 |
} |
|
0 | 657 |
} |
658 |
else |
|
659 |
{ |
|
660 |
// this 'if' block is arranged to avoid a division on packet sizes <= iMaxPacketSize |
|
661 |
isShortPacket = (size < iMaxPacketSize) || |
|
662 |
((size > iMaxPacketSize) && (size % iMaxPacketSize)); |
|
663 |
} |
|
664 |
} |
|
665 |
TInt copySize = Min(size, toDo); |
|
666 |
iExtractOffset += copySize; |
|
667 |
toDo -= copySize; |
|
668 |
if (thisStartAddr != lastEndAddr) |
|
669 |
{ |
|
670 |
TInt bytesToCopy = lastEndAddr - blockStartAddr; |
|
671 |
TInt r=CopyToUser(aThread, blockStartAddr, bytesToCopy, aTcb, aDestOffset); |
|
672 |
if(r != KErrNone) |
|
673 |
Kern::ThreadKill(aThread, EExitPanic, r, KUsbLDDKillCat); |
|
674 |
blockStartAddr = thisStartAddr; |
|
675 |
} |
|
676 |
||
677 |
ModifyTotalRxBytesAvail(-copySize); |
|
678 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
679 |
iNumberofBytesRxRemain[iCurrentDrainingBufferIndex] -= copySize; |
|
680 |
#endif |
|
681 |
lastEndAddr = thisStartAddr + copySize; |
|
682 |
if (iExtractOffset == thisPacketSize) |
|
683 |
{ |
|
684 |
// More data to copy, so need to access new packet |
|
685 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
686 |
iNumberofPacketsRxRemain[iCurrentDrainingBufferIndex] -= 1; |
|
687 |
#endif |
|
688 |
if (!AdvancePacket()) |
|
689 |
{ |
|
690 |
break; // no more packets left |
|
691 |
} |
|
692 |
} |
|
693 |
} while (toDo > 0 && !isShortPacket); |
|
694 |
||
695 |
if (thisStartAddr != lastEndAddr) |
|
696 |
{ |
|
697 |
TInt bytesToCopy = lastEndAddr - blockStartAddr; |
|
698 |
TInt r=CopyToUser(aThread, blockStartAddr, bytesToCopy, aTcb, aDestOffset); |
|
699 |
if(r != KErrNone) |
|
700 |
Kern::ThreadKill(aThread, EExitPanic, r, KUsbLDDKillCat); |
|
701 |
} |
|
702 |
||
703 |
// If we have transferred the requested amount of data it is still possible that |
|
704 |
// the next packet is a zlp which needs to be bumped over |
|
705 |
||
706 |
if (aRUS && (toDo == 0) && (iExtractOffset == 0) && (!isShortPacket) && (!IsReaderEmpty()) && |
|
707 |
(PeekNextPacketSize() == 0)) |
|
708 |
{ |
|
709 |
// swallow a zlp |
|
710 |
isShortPacket = ETrue; |
|
711 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
712 |
iNumberofPacketsRxRemain[iCurrentDrainingBufferIndex] -= 1; |
|
713 |
#endif |
|
714 |
AdvancePacket(); |
|
715 |
} |
|
716 |
aCompleteNow = isShortPacket || (((TInt)aDestOffset) == aLength) || (errorCode != KErrNone); |
|
717 |
||
718 |
FreeDrainedBuffers(); |
|
719 |
||
720 |
// Use this error code to complete client read request |
|
721 |
return errorCode; |
|
722 |
} |
|
723 |
||
724 |
||
725 |
inline TInt TDmaBuf::CopyToUser(DThread* aThread, const TUint8* aSourceAddr, |
|
726 |
TInt aLength, TClientBuffer *aTcb, TUint32& aDestOffset) |
|
727 |
{ |
|
728 |
TPtrC8 des(aSourceAddr, aLength); |
|
729 |
TInt errorCode = Kern::ThreadBufWrite(aThread, aTcb, des, aDestOffset, KChunkShiftBy0, aThread); |
|
730 |
if (errorCode == KErrNone) |
|
731 |
{ |
|
732 |
aDestOffset += aLength; |
|
733 |
} |
|
734 |
return errorCode; |
|
735 |
} |
|
736 |
||
737 |
||
738 |
inline TInt TDmaBuf::NoRxPackets() const |
|
739 |
{ |
|
740 |
return iTotalRxPacketsAvail; |
|
741 |
} |
|
742 |
||
743 |
||
744 |
inline void TDmaBuf::IncrementBufferIndex(TInt& aIndex) |
|
745 |
{ |
|
746 |
if (++aIndex == iNumberofBuffers) |
|
747 |
aIndex = 0; |
|
748 |
} |
|
749 |
||
750 |
||
751 |
TBool TDmaBuf::NextDrainableBuffer() |
|
752 |
{ |
|
753 |
TBool r = EFalse; |
|
754 |
if (iCurrentDrainingBufferIndex != KUsbcInvalidBufferIndex) |
|
755 |
{ |
|
756 |
iCanBeFreed[iCurrentDrainingBufferIndex] = ETrue; |
|
757 |
iNumberofPacketsRx[iCurrentDrainingBufferIndex] = 0; // Current buffer is empty |
|
758 |
iNumberofBytesRx[iCurrentDrainingBufferIndex] = 0; // Current buffer is empty |
|
759 |
||
760 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
761 |
TUint& bytesRemain = iNumberofBytesRxRemain[iCurrentDrainingBufferIndex]; |
|
762 |
TUint& pktsRemain = iNumberofPacketsRxRemain[iCurrentDrainingBufferIndex]; |
|
763 |
if ((bytesRemain != 0) || (pktsRemain != 0)) |
|
764 |
{ |
|
253 | 765 |
OstTraceExt3( TRACE_NORMAL, TDMABUF_NEXTDRAINABLEBUFFER, |
766 |
"TDmaBuf::NextDrainableBuffer: Error: data discarded buffer=%d pkts=%d bytes=%d", |
|
767 |
iCurrentDrainingBufferIndex, pktsRemain, bytesRemain); |
|
0 | 768 |
bytesRemain = 0; |
769 |
pktsRemain = 0; |
|
770 |
} |
|
771 |
#endif |
|
772 |
||
773 |
iCurrentDrainingBufferIndex = KUsbcInvalidBufferIndex; |
|
774 |
iCurrentPacket = KUsbcInvalidPacketIndex; |
|
775 |
} |
|
776 |
||
777 |
if (iDrainQueueIndex != KUsbcInvalidDrainQueueIndex) |
|
778 |
{ |
|
779 |
r = ETrue; |
|
780 |
const TInt index = iDrainQueue[0]; |
|
781 |
iDrainQueueIndex--; |
|
782 |
for (TInt i = 0; i < iNumberofBuffers; i++) |
|
783 |
{ |
|
784 |
iDrainQueue[i] = iDrainQueue[i+1]; |
|
785 |
} |
|
786 |
||
787 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
788 |
if (index != KUsbcInvalidBufferIndex) |
|
789 |
iDrainingOrder++; |
|
790 |
#endif |
|
791 |
||
792 |
iCurrentDrainingBufferIndex = index; |
|
793 |
iCurrentDrainingBuffer = iBuffers[index]; |
|
794 |
iCurrentPacketIndexArray = iPacketIndex[index]; |
|
795 |
iCurrentPacketSizeArray = iPacketSize[index]; |
|
796 |
iCurrentPacket = 0; |
|
797 |
} |
|
798 |
return r; |
|
799 |
} |
|
800 |
||
801 |
||
802 |
TInt TDmaBuf::PeekNextDrainableBuffer() |
|
803 |
{ |
|
804 |
TInt r = KUsbcInvalidBufferIndex; |
|
805 |
if (iDrainQueueIndex != KUsbcInvalidDrainQueueIndex) |
|
806 |
{ |
|
807 |
r = iDrainQueue[0]; |
|
808 |
} |
|
809 |
return r; |
|
810 |
} |
|
811 |
||
812 |
||
813 |
TBool TDmaBuf::NextFillableBuffer() |
|
814 |
{ |
|
815 |
TBool r = EFalse; |
|
816 |
TInt index = iCurrentFillingBufferIndex; |
|
817 |
IncrementBufferIndex(index); |
|
818 |
// the sequence will restart at 0 if a buffer can't be found this time |
|
819 |
iCurrentFillingBufferIndex = 0; |
|
820 |
for (TInt i = 0; i < iNumberofBuffers; i++) |
|
821 |
{ |
|
822 |
if (!iDrainable[index]) |
|
823 |
{ |
|
824 |
iCurrentFillingBufferIndex = index; |
|
825 |
r = ETrue; |
|
826 |
break; |
|
827 |
} |
|
828 |
IncrementBufferIndex(index); |
|
829 |
} |
|
830 |
return r; |
|
831 |
} |
|
832 |
||
833 |
||
834 |
void TDmaBuf::FreeDrainedBuffers() |
|
835 |
{ |
|
836 |
for (TInt i = 0; i < iNumberofBuffers; i++) |
|
837 |
{ |
|
838 |
if (iDrainable[i] && iCanBeFreed[i]) |
|
839 |
{ |
|
840 |
iDrainable[i] = iCanBeFreed[i] = EFalse; |
|
841 |
} |
|
842 |
} |
|
843 |
} |
|
844 |
||
845 |
||
846 |
TBool TDmaBuf::ShortPacketExists() |
|
847 |
{ |
|
848 |
// Actually, a short packet or residue data |
|
253 | 849 |
OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_SHORTPACKETEXISTS, |
850 |
"TDmaBuf::ShortPacketExists 1" ); |
|
0 | 851 |
TInt index = iCurrentDrainingBufferIndex; |
852 |
TUsbcPacketArray* pktSizeArray = iCurrentPacketSizeArray; |
|
853 |
||
854 |
if (iMaxPacketSize > 0) |
|
855 |
{ |
|
856 |
// No buffers available for draining |
|
857 |
if ((iCurrentDrainingBufferIndex == KUsbcInvalidBufferIndex) || |
|
858 |
(iCurrentPacket == KUsbcInvalidPacketIndex)) |
|
859 |
return EFalse; |
|
860 |
||
861 |
// Zlp waiting at tail |
|
862 |
if ((iTotalRxBytesAvail == 0) && (NoRxPackets() == 1)) |
|
863 |
return ETrue; |
|
864 |
||
865 |
if (iEndpointType == KUsbEpTypeBulk) |
|
866 |
{ |
|
867 |
const TInt mask = iMaxPacketSize - 1; |
|
868 |
||
869 |
||
870 |
for (TInt i = 0; i < iNumberofBuffers; i++) |
|
871 |
{ |
|
872 |
if (index == KUsbcInvalidBufferIndex) |
|
873 |
break; |
|
874 |
if (iDrainable[index]) |
|
875 |
{ |
|
876 |
const TInt packetCount = iNumberofPacketsRx[index]; |
|
877 |
const TInt lastPacketSize=pktSizeArray[packetCount - 1]; |
|
878 |
if ((lastPacketSize < iMaxPacketSize) || (lastPacketSize & mask)) |
|
879 |
{ |
|
880 |
return ETrue; |
|
881 |
} |
|
882 |
} |
|
883 |
index = iDrainQueue[i]; |
|
884 |
pktSizeArray = iPacketSize[index]; |
|
885 |
} |
|
886 |
} |
|
887 |
else |
|
888 |
{ |
|
889 |
if (iTotalRxBytesAvail % iMaxPacketSize) |
|
890 |
return ETrue; |
|
891 |
||
892 |
// residue==0; this can be because |
|
893 |
// zlps exist, or short packets combine to n * max_packet_size |
|
894 |
// This means spadework |
|
895 |
const TInt s = iCurrentPacketSizeArray[iCurrentPacket] - iExtractOffset; |
|
896 |
if ((s == 0) || (s % iMaxPacketSize)) |
|
897 |
{ |
|
898 |
return ETrue; |
|
899 |
} |
|
900 |
||
901 |
for (TInt i = 0; i < iNumberofBuffers; i++) |
|
902 |
{ |
|
903 |
if (index == KUsbcInvalidBufferIndex) |
|
904 |
break; |
|
905 |
if (iDrainable[index]) |
|
906 |
{ |
|
907 |
const TInt packetCount = iNumberofPacketsRx[index]; |
|
908 |
const TInt lastPacketSize = pktSizeArray[packetCount - 1]; |
|
909 |
if ((lastPacketSize < iMaxPacketSize) || (lastPacketSize % iMaxPacketSize)) |
|
910 |
{ |
|
911 |
return ETrue; |
|
912 |
} |
|
913 |
} |
|
914 |
index = iDrainQueue[i]; |
|
915 |
pktSizeArray = iPacketSize[index]; |
|
916 |
} |
|
917 |
} |
|
918 |
} |
|
919 |
||
920 |
return EFalse; |
|
921 |
} |
|
922 |
||
923 |
||
924 |
void TDmaBuf::AddToDrainQueue(TInt aBufferIndex) |
|
925 |
{ |
|
926 |
if (iDrainQueue[iDrainQueueIndex + 1] != KUsbcInvalidBufferIndex) |
|
927 |
{ |
|
928 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
253 | 929 |
OstTrace0( TRACE_NORMAL, TDMABUF_ADDTODRAINQUEUE, "TDmaBuf::AddToDrainQueue: Error: invalid iDrainQueue[x]" ); |
0 | 930 |
#endif |
931 |
} |
|
932 |
iDrainQueue[++iDrainQueueIndex] = aBufferIndex; |
|
933 |
} |
|
934 |
||
935 |
||
936 |
#if defined(USBC_LDD_BUFFER_TRACE) |
|
937 |
TInt TDmaBuf::NoRxPacketsAlt() const |
|
938 |
{ |
|
939 |
TInt pktCount = 0; |
|
940 |
for(TInt i = 0; i < iNumberofBuffers; i++) |
|
941 |
{ |
|
942 |
if (iDrainable[i]) |
|
943 |
{ |
|
944 |
pktCount += iNumberofPacketsRxRemain[i]; |
|
945 |
} |
|
946 |
} |
|
947 |
return pktCount; |
|
948 |
} |
|
949 |
||
950 |
||
951 |
TInt TDmaBuf::NoRxBytesAlt() const |
|
952 |
{ |
|
953 |
TInt byteCount = 0; |
|
954 |
for(TInt i = 0; i < iNumberofBuffers; i++) |
|
955 |
{ |
|
956 |
if (iDrainable[i]) |
|
957 |
{ |
|
958 |
byteCount += iNumberofBytesRxRemain[i]; |
|
959 |
} |
|
960 |
} |
|
961 |
return byteCount; |
|
962 |
} |
|
963 |
#endif |
|
964 |
||
965 |
||
966 |
// We only store 1 transaction, no other buffering is done |
|
967 |
TInt TDmaBuf::TxStoreData(DThread* aThread, TClientBuffer *aTcb, TInt aTxLength, TUint32 aBufferOffset) |
|
968 |
{ |
|
253 | 969 |
OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_TXSTOREDATA, |
970 |
"TDmaBuf::TxStoreData 1" ); |
|
0 | 971 |
if (!IsReaderEmpty()) |
972 |
return KErrInUse; |
|
973 |
||
253 | 974 |
OstTraceDef0( OST_TRACE_CATEGORY_RND, TRACE_NORMAL, TDMABUF_TXSTOREDATA_DUP1, |
975 |
"TDmaBuf::TxStoreData 2" ); |
|
33
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
976 |
TInt remainTxLength = aTxLength; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
977 |
TUint32 bufferOffset = aBufferOffset; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
978 |
// Store each buffer separately |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
979 |
for( TInt i=0;(i<iNumberofBuffers)&&(remainTxLength>0);i++) |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
980 |
{ |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
981 |
TUint8* logicalDest = iBuffers[i]; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
982 |
TInt xferSz = Min(remainTxLength, iBufSz); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
983 |
TPtr8 des(logicalDest, xferSz, xferSz); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
984 |
TInt r = Kern::ThreadBufRead(aThread, aTcb, des, bufferOffset, KChunkShiftBy0); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
985 |
if(r != KErrNone) |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
986 |
{ |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
987 |
Kern::ThreadKill(aThread, EExitPanic, r, KUsbLDDKillCat); |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
988 |
return r; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
989 |
} |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
990 |
remainTxLength -= iBufSz; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
991 |
bufferOffset += iBufSz; |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
992 |
} |
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
993 |
|
0173bcd7697c
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
994 |
return KErrNone; |
0 | 995 |
} |
996 |
||
997 |
||
998 |
TInt TDmaBuf::TxGetNextXfer(TUint8*& aBufferAddr, TInt& aTxLength, TPhysAddr& aBufferPhys) |
|
999 |
{ |
|
1000 |
if (iTxActive) |
|
1001 |
return KErrInUse; |
|
1002 |
||
1003 |
aBufferAddr = iBuffers[0]; // only 1 tx buffer |
|
1004 |
aBufferPhys = iBufferPhys[0]; |
|
1005 |
aTxLength = BufferTotalSize(); |
|
1006 |
||
1007 |
return KErrNone; |
|
1008 |
} |
|
1009 |