author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 02:35:35 +0300 | |
changeset 281 | 13fbfa31d2ba |
parent 0 | a41df078684a |
child 271 | dc268b18d709 |
permissions | -rw-r--r-- |
281
13fbfa31d2ba
Revision: 201039
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1 |
// Copyright (c) 2004-2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
} |
|
203 |
||
204 |
||
205 |
/** |
|
206 |
Called by the protocol after processing the packet to indicate that more data is required. |
|
207 |
||
208 |
@param aData reference to the data buffer. |
|
209 |
*/ |
|
210 |
void CBulkOnlyTransport::SetupReadData(TUint aLength) |
|
211 |
{ |
|
212 |
__FNLOG("CBulkOnlyTransport::SetupReadData"); |
|
213 |
__PRINT1(_L("Length = %d (bytes)\n"), aLength); |
|
214 |
iBufSize = aLength; |
|
215 |
iReadSetUp = ETrue; |
|
216 |
} |
|
217 |
||
218 |
||
219 |
/** |
|
220 |
Called by the protocol after processing the packet to indicate that data should be written to the host. |
|
221 |
||
222 |
@param aData reference to the data buffer. |
|
223 |
*/ |
|
224 |
void CBulkOnlyTransport::SetupWriteData(TPtrC8& aData) |
|
225 |
{ |
|
226 |
__FNLOG("CBulkOnlyTransport::SetupWriteData"); |
|
227 |
__PRINT1(_L("Length = %d (bytes)\n"), aData.Length()); |
|
228 |
iWriteBufPtr.Set(aData); |
|
229 |
iWriteSetUp = ETrue; |
|
230 |
} |
|
231 |
||
232 |
||
233 |
TInt CBulkOnlyTransport::Start() |
|
234 |
{ |
|
235 |
__FNLOG("CBulkOnlyTransport::Start"); |
|
236 |
||
237 |
TInt err = KErrNone; |
|
238 |
||
239 |
if (!iProtocol) |
|
240 |
{ |
|
241 |
return KErrBadHandle; //protocol should be set up before start |
|
242 |
} |
|
243 |
||
244 |
if (IsActive()) |
|
245 |
{ |
|
246 |
__PRINT(_L("CBulkOnlyTransport::Start - active before start!\n")); |
|
247 |
return KErrInUse; |
|
248 |
} |
|
249 |
||
250 |
if ((err = SetupConfigurationDescriptor()) != KErrNone || |
|
251 |
(err = SetupInterfaceDescriptors()) != KErrNone ) |
|
252 |
{ |
|
253 |
__PRINT(_L("CBulkOnlyTransport::Start - Error during descriptors setup!\n")); |
|
254 |
return err; |
|
255 |
} |
|
256 |
||
257 |
AllocateEndpointResources(); |
|
258 |
ActivateDeviceStateNotifier(); // activate notifier wich will wait until USB became configured |
|
259 |
TUsbcDeviceState deviceStatus = EUsbcDeviceStateDefault; |
|
260 |
err = GetDeviceStatus(deviceStatus); |
|
261 |
__PRINT1(_L("CBulkOnlyTransport::Start - Device status = %d\n"), deviceStatus); |
|
262 |
if (err == KErrNone && deviceStatus == EUsbcDeviceStateConfigured) |
|
263 |
{ |
|
264 |
__PRINT(_L("CBulkOnlyTransport::Start - Starting bulk only transport\n")); |
|
265 |
err = HwStart(); |
|
266 |
} |
|
267 |
||
268 |
#ifdef MSDC_MULTITHREADED |
|
269 |
TPtr8 aDes1(NULL,0); |
|
270 |
TPtr8 aDes2(NULL,0); |
|
271 |
GetBufferPointers(aDes1, aDes2); |
|
272 |
iProtocol->InitializeBufferPointers(aDes1, aDes2); // have to pass pointer to memory not offsets to initialise TPtr, and lengths |
|
273 |
#endif |
|
274 |
||
275 |
iInterfaceConfigured = ETrue; |
|
276 |
return err; |
|
277 |
} |
|
278 |
||
279 |
TInt CBulkOnlyTransport::HwStart(TBool aDiscard) |
|
280 |
{ |
|
281 |
__FNLOG("CBulkOnlyTransport::HwStart"); |
|
282 |
||
283 |
TInt lun = MaxLun(); |
|
284 |
do |
|
285 |
{ |
|
286 |
Controller().DriveManager().Connect(lun); |
|
287 |
} |
|
288 |
while(--lun >= 0); |
|
289 |
||
290 |
TInt res = StartControlInterface(); |
|
291 |
||
292 |
iCurrentState = ENone; |
|
293 |
iWriteSetUp=EFalse; |
|
294 |
iReadSetUp=EFalse; |
|
295 |
iStarted = ETrue; |
|
296 |
||
297 |
if (aDiscard) |
|
298 |
{ |
|
299 |
FlushData(); |
|
300 |
} |
|
301 |
||
302 |
ReadCBW(); |
|
303 |
return res; |
|
304 |
} |
|
305 |
||
306 |
||
307 |
TInt CBulkOnlyTransport::HwStop() |
|
308 |
{ |
|
309 |
__FNLOG("CBulkOnlyTransport::HwStop"); |
|
310 |
if (iStarted) |
|
311 |
{ |
|
312 |
StopBulkOnlyEndpoint(); |
|
313 |
CancelControlInterface(); |
|
314 |
iStarted = EFalse; |
|
315 |
} |
|
316 |
return KErrNone; |
|
317 |
} |
|
318 |
||
319 |
||
320 |
void CBulkOnlyTransport::StopBulkOnlyEndpoint() |
|
321 |
{ |
|
322 |
__FNLOG("CBulkOnlyTransport::StopBulkOnlyEndpoint"); |
|
323 |
||
324 |
TInt lun = MaxLun(); |
|
325 |
do |
|
326 |
{ |
|
327 |
Controller().DriveManager().Disconnect(lun); |
|
328 |
} |
|
329 |
while(--lun >= 0); |
|
330 |
Cancel(); |
|
331 |
iProtocol->Cancel(); |
|
332 |
} |
|
333 |
||
334 |
||
335 |
TInt CBulkOnlyTransport::HwSuspend() |
|
336 |
{ |
|
337 |
__FNLOG("CBulkOnlyTransport::HwSuspend"); |
|
338 |
||
339 |
TInt lun = MaxLun(); |
|
340 |
do |
|
341 |
{ |
|
342 |
Controller().DriveManager().Disconnect(lun); |
|
343 |
} |
|
344 |
while(--lun >= 0); |
|
345 |
||
346 |
return KErrNone; |
|
347 |
} |
|
348 |
||
349 |
||
350 |
TInt CBulkOnlyTransport::HwResume() |
|
351 |
{ |
|
352 |
__FNLOG("CBulkOnlyTransport::HwResume"); |
|
353 |
||
354 |
TInt lun = MaxLun(); |
|
355 |
do |
|
356 |
{ |
|
357 |
Controller().DriveManager().Connect(lun); |
|
358 |
} |
|
359 |
while(--lun >= 0); |
|
360 |
||
361 |
return KErrNone; |
|
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
Stops the Bulk Only Transport |
|
366 |
*/ |
|
367 |
TInt CBulkOnlyTransport::Stop() |
|
368 |
{ |
|
369 |
__FNLOG("CBulkOnlyTransport::Stop"); |
|
370 |
CancelControlInterface(); |
|
371 |
CancelDeviceStateNotifier(); |
|
372 |
Cancel(); |
|
373 |
if (iInterfaceConfigured) |
|
374 |
{ |
|
375 |
ReleaseInterface(); |
|
376 |
SetupConfigurationDescriptor(ETrue); |
|
377 |
} |
|
378 |
iCurrentState = ENone; |
|
379 |
iInterfaceConfigured = EFalse; |
|
380 |
||
381 |
return KErrNone; |
|
382 |
} |
|
383 |
||
384 |
||
385 |
||
386 |
void CBulkOnlyTransport::DoCancel() |
|
387 |
{ |
|
388 |
__FNLOG("CBulkOnlyTransport::DoCancel"); |
|
389 |
CancelReadWriteRequests(); |
|
390 |
} |
|
391 |
||
392 |
||
393 |
void CBulkOnlyTransport::Activate(TInt aReason) |
|
394 |
{ |
|
395 |
SetActive(); |
|
396 |
TRequestStatus* r = &iStatus; |
|
397 |
User::RequestComplete(r, aReason); |
|
398 |
} |
|
399 |
||
400 |
||
401 |
void CBulkOnlyTransport::RunL() |
|
402 |
{ |
|
403 |
__FNLOG("CBulkOnlyTransport::RunL"); |
|
404 |
if (iStatus != KErrNone) |
|
405 |
{ |
|
406 |
__PRINT1(_L("Error %d in RunL, halt endpoints \n"), iStatus.Int()); |
|
407 |
SetPermError(); //halt endpoints for reset recovery |
|
408 |
return; |
|
409 |
} |
|
410 |
switch (iCurrentState) |
|
411 |
{ |
|
412 |
case EWaitForCBW: |
|
413 |
__PRINT(_L("EWaitForCBW")); |
|
414 |
ProcessCbwEvent(); |
|
415 |
break; |
|
416 |
||
417 |
case EWritingData: |
|
418 |
__PRINT(_L("EWritingData")); |
|
419 |
iWriteSetUp = EFalse; //the buffer was used |
|
420 |
||
421 |
if (iDataResidue && iStallAllowed) |
|
422 |
{ |
|
423 |
StallEndpointAndWaitForClear(); |
|
424 |
} |
|
425 |
||
426 |
SendCSW(iCbwTag, iDataResidue, iCmdStatus); |
|
427 |
break; |
|
428 |
||
429 |
case EReadingData: |
|
430 |
{ |
|
431 |
__PRINT(_L("EReadingData")); |
|
432 |
||
433 |
ProcessReadingDataEvent(); |
|
434 |
} |
|
435 |
break; |
|
436 |
||
437 |
case ESendingCSW: |
|
438 |
__PRINT(_L("ESendingCSW")); |
|
439 |
ReadCBW(); |
|
440 |
break; |
|
441 |
||
442 |
case EPermErr: |
|
443 |
__PRINT(_L("EPermErr")); |
|
444 |
StallEndpointAndWaitForClear(); |
|
445 |
break; |
|
446 |
||
447 |
default: |
|
448 |
SetPermError(); // unexpected state |
|
449 |
} |
|
450 |
} |
|
451 |
||
452 |
||
453 |
/** |
|
454 |
Decode the CBW received from the host via OutEndpoint |
|
455 |
||
456 |
- If the header is valid, the data content is passed to the parser. |
|
457 |
- Depending on the command, more data may be transmitted/received. |
|
458 |
- ...or the CSW is sent (if not a data command). |
|
459 |
||
460 |
*/ |
|
461 |
void CBulkOnlyTransport::DecodeCBW() |
|
462 |
{ |
|
463 |
__FNLOG("CBulkOnlyTransport::DecodeCBW"); |
|
464 |
||
465 |
SetCbwPtr(); |
|
466 |
||
467 |
if (!CheckCBW()) //check if CBW valid and meaningful |
|
468 |
{ |
|
469 |
// CBW not valid or meaningful |
|
470 |
// Specification says: "If the CBW is not valid, the device shall STALL |
|
471 |
// the Bulk-In pipe. Also, the device shall either STALL the Bulk-Out pipe, |
|
472 |
// or the device shall accept and discard any Bulk-Out data. The device |
|
473 |
// shall maintain this state until a Reset Recovery." |
|
474 |
// Here we keep bulk-in ep stalled and ignore bulk-out ep. |
|
475 |
SetPermError(); |
|
476 |
ExpireData((TAny*) (iCbwBufPtr.Ptr())); |
|
477 |
return; |
|
478 |
} |
|
479 |
||
480 |
TPtrC8 aData; |
|
481 |
aData.Set(&iCbwBufPtr[KCbwCbLengthOffset], KMaxCbwcbLength+1); //prepare data for protocol starting form Length |
|
482 |
TUint8 lun = static_cast<TUint8>(iCbwBufPtr[13] & 0x0f); |
|
483 |
||
484 |
iCbwTag = static_cast<TUint32>(iCbwBufPtr[KCbwTagOffset]) | |
|
485 |
static_cast<TUint32>(iCbwBufPtr[KCbwTagOffset+1]) <<8 | |
|
486 |
static_cast<TUint32>(iCbwBufPtr[KCbwTagOffset+2]) <<16| |
|
487 |
static_cast<TUint32>(iCbwBufPtr[KCbwTagOffset+3]) <<24; |
|
488 |
||
489 |
TInt i = KCbwDataTransferLengthOffset; |
|
490 |
TUint hostDataLength = static_cast<TUint32>(iCbwBufPtr[i ]) | |
|
491 |
static_cast<TUint32>(iCbwBufPtr[i+1]) <<8 | |
|
492 |
static_cast<TUint32>(iCbwBufPtr[i+2]) <<16 | |
|
493 |
static_cast<TUint32>(iCbwBufPtr[i+3]) <<24; |
|
494 |
||
495 |
TBool dataToHost = iCbwBufPtr[KCbwFlagOffset] & 0x80; |
|
496 |
||
497 |
__PRINT4(_L("lun =%d, hostDataLength=%d, CBWtag = 0x%x, dataToHost=%d\n"), lun, hostDataLength, iCbwTag, dataToHost); |
|
498 |
////////////////////////////////////////////// |
|
499 |
TBool ret = iProtocol->DecodePacket(aData, lun); |
|
500 |
////////////////////////////////////////////// |
|
501 |
ExpireData((TAny*) (iCbwBufPtr.Ptr())); |
|
502 |
||
503 |
||
504 |
iStallAllowed = ETrue; |
|
505 |
||
506 |
if (!ret) |
|
507 |
{ |
|
508 |
__PRINT(_L("Command Failed\n")); |
|
509 |
iCmdStatus = ECommandFailed; |
|
510 |
} |
|
511 |
else |
|
512 |
{ |
|
513 |
__PRINT(_L("Command Passed\n")); |
|
514 |
iCmdStatus = ECommandPassed; |
|
515 |
} |
|
516 |
||
517 |
if (hostDataLength) // Host expected data transfer |
|
518 |
{ |
|
519 |
if (dataToHost) // send data to host |
|
520 |
{ |
|
521 |
if (!iWriteSetUp) //write buffer was not set up |
|
522 |
{ |
|
523 |
__PRINT(_L("Write buffer was not setup\n")); |
|
524 |
iDataResidue =hostDataLength; |
|
525 |
__PRINT1(_L("DataResidue (write to host)=%d\n"),iDataResidue); |
|
526 |
||
527 |
//------------------------------------ |
|
528 |
if (hostDataLength <= KBOTMaxBufSize) |
|
529 |
{ |
|
530 |
__PRINT(_L("Case 4 or 8\n")); |
|
531 |
SetPaddingBufPtr(hostDataLength); |
|
532 |
iPaddingBufPtr.FillZ(hostDataLength); |
|
533 |
TPtrC8 ptr(NULL, 0); |
|
534 |
ptr.Set((TUint8*)iPaddingBufPtr.Ptr(), hostDataLength); |
|
535 |
WriteData(iStatus, ptr, hostDataLength, EFalse); |
|
536 |
iStallAllowed = EFalse; |
|
537 |
if (iReadSetUp) //read buffer WAS set up - case (8) |
|
538 |
{ |
|
539 |
__PRINT(_L("It is Case 8\n")); |
|
540 |
iCmdStatus = EPhaseError; |
|
541 |
} |
|
542 |
return; |
|
543 |
} |
|
544 |
else |
|
545 |
//------------------------------------ |
|
546 |
// Use next block instead of StallEndpointAndWaitForClear(InEndpoint); |
|
547 |
{ |
|
548 |
SetPaddingBufPtr(hostDataLength); |
|
549 |
iPaddingBufPtr.FillZ(KBOTMaxBufSize); |
|
550 |
TUint c =0; |
|
551 |
TRequestStatus status; |
|
552 |
while (c<hostDataLength) |
|
553 |
{ |
|
554 |
TInt len; |
|
555 |
if (hostDataLength - c > KBOTMaxBufSize) |
|
556 |
{ |
|
557 |
len = KBOTMaxBufSize; |
|
558 |
} |
|
559 |
else |
|
560 |
{ |
|
561 |
len = hostDataLength - c; |
|
562 |
} |
|
563 |
||
564 |
TPtrC8 ptr(NULL, 0); |
|
565 |
ptr.Set((TUint8*)iPaddingBufPtr.Ptr(), len); |
|
566 |
WriteUsb(status, ptr, len); |
|
567 |
User::WaitForRequest(status); |
|
568 |
c += KBOTMaxBufSize; |
|
569 |
} |
|
570 |
} |
|
571 |
||
572 |
if (iReadSetUp) //read buffer WAS set up - case (8) |
|
573 |
{ |
|
574 |
__PRINT(_L("Case 8\n")); |
|
575 |
SendCSW(iCbwTag, hostDataLength, EPhaseError); |
|
576 |
//don't care to reset any flag - should get reset recovery |
|
577 |
} |
|
578 |
else // case (4) |
|
579 |
{ |
|
580 |
__PRINT(_L("Case 4\n")); |
|
581 |
SendCSW(iCbwTag, hostDataLength, iCmdStatus); |
|
582 |
} |
|
583 |
return; |
|
584 |
} // if (!iWriteSetUp) |
|
585 |
||
586 |
//================== |
|
587 |
TUint deviceDataLength = static_cast<TUint>(iWriteBufPtr.Length()); |
|
588 |
iDataResidue =hostDataLength - deviceDataLength ; |
|
589 |
__PRINT2(_L("Device data length = %d, DataResidue (write to host)=%d\n"), deviceDataLength, iDataResidue); |
|
590 |
||
591 |
if (deviceDataLength < hostDataLength && |
|
592 |
hostDataLength < KBOTMaxBufSize ) |
|
593 |
{ |
|
594 |
__PRINT(_L("Case 5 (padding)\n")); |
|
595 |
SetPaddingBufPtr(hostDataLength); |
|
596 |
iPaddingBufPtr.Zero(); |
|
597 |
iPaddingBufPtr.Append(iWriteBufPtr); |
|
598 |
iStallAllowed = EFalse; |
|
599 |
__PRINT1(_L("iPaddingBufPtr.Length = %d\n"),iPaddingBufPtr.Length()); |
|
600 |
TPtrC8 ptr(NULL, 0); |
|
601 |
ptr.Set((TUint8*)iPaddingBufPtr.Ptr(), hostDataLength); |
|
602 |
WriteData(iStatus, ptr, hostDataLength, EFalse); |
|
603 |
return; |
|
604 |
} |
|
605 |
||
606 |
//=================== |
|
607 |
||
608 |
if (deviceDataLength == hostDataLength) //case (6)[==] |
|
609 |
{ |
|
610 |
__PRINT(_L("Case 6\n")); |
|
611 |
WriteData(iStatus, iWriteBufPtr, deviceDataLength); |
|
612 |
return; |
|
613 |
} |
|
614 |
else if (deviceDataLength < hostDataLength) //case (5)[<] |
|
615 |
{ |
|
616 |
__PRINT(_L("Case 5\n")); |
|
617 |
WriteData(iStatus, iWriteBufPtr, deviceDataLength, ETrue); // Send ZLP |
|
618 |
return; |
|
619 |
} |
|
620 |
else // deviceDataLength > hostDataLength - case (7) |
|
621 |
{ |
|
622 |
__PRINT(_L("Case 7\n")); |
|
623 |
iCmdStatus = EPhaseError; |
|
624 |
iDataResidue = 0; |
|
625 |
WriteData(iStatus, iWriteBufPtr, hostDataLength); |
|
626 |
return; |
|
627 |
} |
|
628 |
} |
|
629 |
else //read data from host |
|
630 |
{ |
|
631 |
if (!iReadSetUp) |
|
632 |
{ |
|
633 |
iDataResidue = hostDataLength; |
|
634 |
__PRINT(_L("Read buffer was not setup\n")); |
|
635 |
// Use next block instead of StallEndpointAndWaitForClear(OutEndpoint); |
|
636 |
DiscardData(hostDataLength); |
|
637 |
||
638 |
if (iWriteSetUp) //case (10) |
|
639 |
{ |
|
640 |
__PRINT(_L("case 10\n")); |
|
641 |
SendCSW(iCbwTag, hostDataLength, EPhaseError); |
|
642 |
} |
|
643 |
else // case (9) |
|
644 |
{ |
|
645 |
__PRINT(_L("Case 9\n")); |
|
646 |
SendCSW(iCbwTag, hostDataLength, iCmdStatus); |
|
647 |
} |
|
648 |
||
649 |
return; |
|
650 |
} |
|
651 |
||
652 |
TUint deviceDataLength = iBufSize; |
|
653 |
iDataResidue = hostDataLength; // calculate residue later |
|
654 |
||
655 |
__PRINT2(_L("deviceDataLength = iBufSize = %d, DataResidue = HDL for now (read from host) =%d\n"),deviceDataLength,iDataResidue); |
|
656 |
||
657 |
if (deviceDataLength <= hostDataLength) // case (11) and (12) |
|
658 |
{ |
|
659 |
__PRINT(_L("Case 11 or 12\n")); |
|
660 |
ReadData(deviceDataLength); |
|
661 |
return; |
|
662 |
} |
|
663 |
if (deviceDataLength > hostDataLength) // case (13) |
|
664 |
{ |
|
665 |
__PRINT(_L("Case 13\n")); |
|
666 |
/** |
|
667 |
* Comment following line in order to pass compliant test. |
|
668 |
* As spec said in case 13:"The device may receive data up to a |
|
669 |
* total of dCBWDataTransferLength." |
|
670 |
* Here we choose to ignore incoming data. |
|
671 |
*/ |
|
672 |
//StallEndpointAndWaitForClear(OutEndpoint); //Stall Out endpoint |
|
673 |
if (iReadSetUp) |
|
674 |
{ |
|
675 |
WriteToClient(hostDataLength); |
|
676 |
iReadSetUp = EFalse; |
|
677 |
} |
|
678 |
SendCSW(iCbwTag, hostDataLength, EPhaseError); |
|
679 |
return; |
|
680 |
} |
|
681 |
} |
|
682 |
} |
|
683 |
else // Host expected no data transfer |
|
684 |
{ |
|
685 |
__PRINT(_L("No data transfer expected\n")); |
|
686 |
iDataResidue = 0; |
|
687 |
if (iWriteSetUp || iReadSetUp) // case (2) and (3) |
|
688 |
{ |
|
689 |
__PRINT(_L("Case 2 or 3\n")); |
|
690 |
SendCSW(iCbwTag, 0, EPhaseError); |
|
691 |
} |
|
692 |
else |
|
693 |
{ |
|
694 |
__PRINT(_L("Case 1\n")); |
|
695 |
SendCSW(iCbwTag, 0, iCmdStatus); //case (1) |
|
696 |
} |
|
697 |
} |
|
698 |
} |
|
699 |
||
700 |
||
701 |
/** |
|
702 |
Check if CBW Valid and Meaningful. |
|
703 |
||
704 |
@return ETrue if CBW is Valid and Meaningful, EFalse otherwise |
|
705 |
*/ |
|
706 |
TBool CBulkOnlyTransport::CheckCBW() |
|
707 |
{ |
|
708 |
__FNLOG("CBulkOnlyTransport::CheckCBW"); |
|
709 |
||
710 |
// |
|
711 |
// Check valid |
|
712 |
// |
|
713 |
||
714 |
// Check length |
|
715 |
if ((TUint) (iCbwBufPtr.Length()) != KCbwLength) |
|
716 |
{ |
|
717 |
__PRINT2(_L("Bad length: %d != KCbwLength"), iCbwBufPtr.Length(), KCbwLength); |
|
718 |
return EFalse; |
|
719 |
} |
|
720 |
||
721 |
// Check signature |
|
722 |
TInt i = KCbwSignatureOffset; |
|
723 |
if (iCbwBufPtr[i ] != 0x55 || // CBW Singature from USB Bulk-Only Transport spec |
|
724 |
iCbwBufPtr[i+1] != 0x53 || |
|
725 |
iCbwBufPtr[i+2] != 0x42 || |
|
726 |
iCbwBufPtr[i+3] != 0x43) |
|
727 |
{ |
|
728 |
__PRINT(_L("Bad signature")); |
|
729 |
__PRINT4(_L(" 0x%x, 0x%x, 0x%x, 0x%x \n"), iCbwBufPtr[i], iCbwBufPtr[i+1], iCbwBufPtr[i+2],iCbwBufPtr[i+3]) |
|
730 |
return EFalse; |
|
731 |
} |
|
732 |
||
733 |
// |
|
734 |
// Check meaningful |
|
735 |
// |
|
736 |
||
737 |
// Check reserved bits ( must be zero ) |
|
738 |
if ((iCbwBufPtr[KCbwLunOffset] & 0xF0) || (iCbwBufPtr[KCbwCbLengthOffset] & 0xE0)) |
|
739 |
{ |
|
740 |
__PRINT(_L("Reserved bits not zero\n")); |
|
741 |
return EFalse; |
|
742 |
} |
|
743 |
||
744 |
// check command block length |
|
745 |
TInt cbwcbLength = iCbwBufPtr[KCbwCbLengthOffset] & 0x1F; |
|
746 |
if (cbwcbLength >KMaxCbwcbLength) |
|
747 |
{ |
|
748 |
__PRINT(_L("Incorrect block length\n")); |
|
749 |
return EFalse; |
|
750 |
} |
|
751 |
||
752 |
//check LUN |
|
753 |
TInt8 lun = static_cast<TUint8>(iCbwBufPtr[KCbwLunOffset] & 0x0f); |
|
754 |
if (iMaxLun < lun) |
|
755 |
{ |
|
756 |
__PRINT1(_L("bad lun: %d"), lun); |
|
757 |
return EFalse; |
|
758 |
} |
|
759 |
||
760 |
return ETrue; |
|
761 |
} |
|
762 |
||
763 |
||
764 |
/** |
|
765 |
Initiate stalling of bulk IN endpoint. |
|
766 |
Used when protocol wants to force host to initiate a reset recovery. |
|
767 |
*/ |
|
768 |
void CBulkOnlyTransport::SetPermError() |
|
769 |
{ |
|
770 |
__FNLOG("CBulkOnlyTransport::SetPermError"); |
|
771 |
iCurrentState = EPermErr; |
|
772 |
Activate(KErrNone); |
|
773 |
} |
|
774 |
||
775 |
||
776 |
/** |
|
777 |
Send data provided by protocol to the host |
|
778 |
||
779 |
@param aLength amount of data (in bytes) to be send to host |
|
780 |
*/ |
|
781 |
void CBulkOnlyTransport::WriteData(TRequestStatus& aStatus, TPtrC8& aDes, TUint aLength, TBool aZlpRequired) |
|
782 |
{ |
|
783 |
__FNLOG("CBulkOnlyTransport::WriteData"); |
|
784 |
||
785 |
if (IsActive()) |
|
786 |
{ |
|
787 |
__PRINT(_L("Still active\n")); |
|
788 |
__ASSERT_DEBUG(EFalse, User::Panic(KUsbMsSvrPncCat, EMsBulkOnlyStillActive)); |
|
789 |
return; |
|
790 |
} |
|
791 |
WriteUsb(aStatus, aDes, aLength, aZlpRequired); |
|
792 |
iCurrentState = EWritingData; |
|
793 |
SetActive(); |
|
794 |
} |
|
795 |
||
796 |
||
797 |
/** |
|
798 |
Send Command Status Wrapper to the host |
|
799 |
||
800 |
@param aTag Echo of Command Block Tag sent by the host. |
|
801 |
@param aDataResidue the difference between the amount of data expected by the |
|
802 |
host, and the actual amount of data processed by the device. |
|
803 |
@param aStatus indicates the success or failure of the command. |
|
804 |
*/ |
|
805 |
void CBulkOnlyTransport::SendCSW(TUint aTag, TUint aDataResidue, TCswStatus aStatus) |
|
806 |
{ |
|
807 |
__FNLOG("CBulkOnlyTransport::SendCSW"); |
|
808 |
__PRINT2(_L("DataResidue = %d, Status = %d \n"), aDataResidue, aStatus); |
|
809 |
||
810 |
if (IsActive()) |
|
811 |
{ |
|
812 |
__PRINT(_L("Still active\n")); |
|
813 |
__ASSERT_DEBUG(EFalse, User::Panic(KUsbMsSvrPncCat, EMsBulkOnlyStillActive)); |
|
814 |
return; |
|
815 |
} |
|
816 |
||
817 |
SetCswBufPtr(KCswLength); |
|
818 |
TInt i = KCswSingnatureOffset; |
|
819 |
iCswBufPtr[i ] = 0x55; // CSW Singature from USB Bulk-Only Transport spec |
|
820 |
iCswBufPtr[i+1] = 0x53; |
|
821 |
iCswBufPtr[i+2] = 0x42; |
|
822 |
iCswBufPtr[i+3] = 0x53; |
|
823 |
||
824 |
i = KCswTagOffset; |
|
825 |
||
826 |
iCswBufPtr[i ] = static_cast<TUint8>((aTag & 0x000000FF)); |
|
827 |
iCswBufPtr[i+1] = static_cast<TUint8>((aTag & 0x0000FF00) >> 8); |
|
828 |
iCswBufPtr[i+2] = static_cast<TUint8>((aTag & 0x00FF0000) >> 16); |
|
829 |
iCswBufPtr[i+3] = static_cast<TUint8>((aTag & 0xFF000000) >> 24); |
|
830 |
||
831 |
i = KCswDataResidueOffset; |
|
832 |
iCswBufPtr[i ] = static_cast<TUint8>((aDataResidue & 0x000000FF)); |
|
833 |
iCswBufPtr[i+1] = static_cast<TUint8>((aDataResidue & 0x0000FF00) >> 8); |
|
834 |
iCswBufPtr[i+2] = static_cast<TUint8>((aDataResidue & 0x00FF0000) >> 16); |
|
835 |
iCswBufPtr[i+3] = static_cast<TUint8>((aDataResidue & 0xFF000000) >> 24); |
|
836 |
||
837 |
iCswBufPtr[KCswStatusOffset] = static_cast<TUint8>(aStatus); |
|
838 |
||
839 |
TPtrC8 ptr(NULL, 0); |
|
840 |
ptr.Set((const TUint8*)iCswBufPtr.Ptr(), KCswLength); |
|
841 |
||
842 |
WriteUsb(iStatus, ptr, KCswLength); |
|
843 |
||
844 |
iCurrentState = ESendingCSW; |
|
845 |
||
846 |
SetActive(); |
|
847 |
} |
|
848 |
||
849 |
||
850 |
/** |
|
851 |
Associates the transport with the protocol. Called during initialization of the controller. |
|
852 |
||
853 |
@param aProtocol reference to the protocol |
|
854 |
*/ |
|
855 |
void CBulkOnlyTransport::RegisterProtocol(MProtocolBase& aProtocol) |
|
856 |
{ |
|
857 |
__FNLOG("CBulkOnlyTransport::RegisterProtocol"); |
|
858 |
iProtocol = &aProtocol; |
|
859 |
} |
|
860 |
||
861 |
||
862 |
/** |
|
863 |
Used by CControlInterface |
|
864 |
||
865 |
@return reference to the controller which instantiate the CBulkOnlyTransport |
|
866 |
*/ |
|
867 |
CUsbMassStorageController& CBulkOnlyTransport::Controller() |
|
868 |
{ |
|
869 |
return iController; |
|
870 |
} |
|
871 |
||
872 |
||
873 |
/** |
|
874 |
@return the number of logical units supported by the device. |
|
875 |
Logical Unit Numbers on the device shall be numbered contiguously starting from LUN |
|
876 |
0 to a maximum LUN of 15 (Fh). |
|
877 |
*/ |
|
878 |
TInt CBulkOnlyTransport::MaxLun() |
|
879 |
{ |
|
880 |
return iMaxLun; |
|
881 |
} |
|
882 |
||
883 |
||
884 |
void CBulkOnlyTransport::GetCommandBufPtr(TPtr8& aDes, TUint aLength) // Set pointer to buffer of specified aLength for command |
|
885 |
{ |
|
886 |
aDes.Set(SetCommandBufPtr(aLength)); |
|
887 |
} |
|
888 |
||
889 |
void CBulkOnlyTransport::GetReadDataBufPtr(TPtr8& aDes) // Set pointer to buffer into which data is to be read from drive (Read10) |
|
890 |
{ |
|
891 |
aDes.Set(SetDataBufPtr()); |
|
892 |
} |
|
893 |
||
894 |
||
895 |
void CBulkOnlyTransport::GetWriteDataBufPtr(TPtrC8& aDes) // Set pointer to buffer from which data is to be written to drive (Write10) |
|
896 |
{ |
|
897 |
aDes.Set(iReadBufPtr); |
|
898 |
} |
|
899 |
||
900 |
#ifdef MSDC_MULTITHREADED |
|
901 |
void CBulkOnlyTransport::ProcessReadData(TAny* aAddress) |
|
902 |
{ |
|
903 |
ExpireData(aAddress); |
|
904 |
} |
|
905 |
#endif |
|
906 |
||
907 |
||
908 |
||
909 |
||
910 |
||
911 |
||
912 |
||
913 |