author | hgs |
Mon, 11 Oct 2010 17:54:41 +0100 | |
changeset 286 | 48e57fb1237e |
parent 269 | d57b86b1867a |
permissions | -rw-r--r-- |
0 | 1 |
/* |
269 | 2 |
* Copyright (c) 2004-2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 3 |
* All rights reserved. |
4 |
* This component and the accompanying materials are made available |
|
5 |
* under the terms of the License "Eclipse Public License v1.0" |
|
6 |
* which accompanies this distribution, and is available |
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
* |
|
9 |
* Initial Contributors: |
|
10 |
* Nokia Corporation - initial contribution. |
|
11 |
* |
|
12 |
* Contributors: |
|
13 |
* |
|
14 |
* Description: |
|
15 |
* |
|
16 |
*/ |
|
17 |
||
18 |
||
19 |
/** |
|
20 |
@file |
|
21 |
@internalTechnology |
|
22 |
*/ |
|
23 |
||
286 | 24 |
#include <e32std.h> |
25 |
#include "mtransport.h" |
|
26 |
#include "mprotocol.h" |
|
27 |
#include "mldddevicestatenotification.h" |
|
28 |
||
29 |
#include "drivemanager.h" |
|
30 |
#include "cusbmassstoragecontroller.h" |
|
31 |
||
0 | 32 |
#include "cbulkonlytransport.h" |
33 |
#include "cbulkonlytransportusbcscldd.h" |
|
286 | 34 |
#include "smassstorage.h" |
35 |
||
36 |
#include "OstTraceDefinitions.h" |
|
37 |
#ifdef OST_TRACE_COMPILER_IN_USE |
|
38 |
#include "cbulkonlytransportusbcsclddTraces.h" |
|
39 |
#endif |
|
40 |
||
0 | 41 |
|
42 |
||
43 |
//This value defined in USB Mass Storage Bulk Only Transrt spec and not supposed to be changed |
|
286 | 44 |
static const TInt KRequiredNumberOfEndpoints = 2; // in addition to endpoint 0. |
0 | 45 |
|
286 | 46 |
static const TInt KUsbNumInterfacesOffset = 4; |
0 | 47 |
|
48 |
#ifdef MSDC_MULTITHREADED |
|
286 | 49 |
//Only used in DB. The first 2K of KMaxScBufferSize for sending CSW |
0 | 50 |
static const TUint32 KCswBufferSize = 2 * 1024; |
51 |
#endif |
|
52 |
||
53 |
//////////////////////////////////// |
|
54 |
/** |
|
55 |
Called by CBulkOnlyTransportUsbcScLdd to create an instance of CControlInterfaceUsbcScLdd |
|
56 |
||
57 |
@param aParent reference to the CBulkOnlyTransportUsbcScLdd |
|
58 |
*/ |
|
59 |
||
60 |
||
61 |
CControlInterfaceUsbcScLdd* CControlInterfaceUsbcScLdd::NewL(CBulkOnlyTransportUsbcScLdd& aParent) |
|
286 | 62 |
{ |
63 |
CControlInterfaceUsbcScLdd* self = new(ELeave) CControlInterfaceUsbcScLdd(aParent); |
|
64 |
CleanupStack::PushL(self); |
|
65 |
self->ConstructL(); |
|
66 |
CActiveScheduler::Add(self); |
|
67 |
CleanupStack::Pop(); |
|
68 |
return self; |
|
69 |
} |
|
0 | 70 |
|
71 |
||
72 |
void CControlInterfaceUsbcScLdd::ConstructL() |
|
286 | 73 |
{ |
74 |
} |
|
0 | 75 |
|
76 |
||
77 |
/** |
|
78 |
c'tor |
|
79 |
||
80 |
@param aParent reference to the CBulkOnlyTransportUsbcScLdd |
|
81 |
*/ |
|
82 |
CControlInterfaceUsbcScLdd::CControlInterfaceUsbcScLdd(CBulkOnlyTransportUsbcScLdd& aParent) |
|
286 | 83 |
:CActive(EPriorityStandard), |
84 |
iParent(aParent), |
|
85 |
iCurrentState(ENone) |
|
86 |
{ |
|
87 |
} |
|
0 | 88 |
|
89 |
||
90 |
/** |
|
91 |
d'tor |
|
92 |
*/ |
|
93 |
CControlInterfaceUsbcScLdd::~CControlInterfaceUsbcScLdd() |
|
286 | 94 |
{ |
95 |
Cancel(); |
|
96 |
iEp0Buf.Close(); |
|
97 |
} |
|
0 | 98 |
|
99 |
TInt CControlInterfaceUsbcScLdd::OpenEp0() |
|
286 | 100 |
{ |
101 |
TInt res = iParent.Ldd().OpenEndpoint(iEp0Buf,0); |
|
102 |
return res; |
|
103 |
} |
|
0 | 104 |
|
105 |
/** |
|
106 |
Called by CBulkOnlyTransport HwStart to start control interface |
|
107 |
*/ |
|
108 |
TInt CControlInterfaceUsbcScLdd::Start() |
|
286 | 109 |
{ |
110 |
TInt res = ReadEp0Data(); |
|
111 |
return (res); |
|
112 |
} |
|
0 | 113 |
|
114 |
||
115 |
/** |
|
116 |
Called by desctructor of CBulkOnlyTransportUsbcScLdd to stop control interface |
|
117 |
*/ |
|
118 |
void CControlInterfaceUsbcScLdd::Stop() |
|
286 | 119 |
{ |
120 |
if (!IsActive()) |
|
121 |
{ |
|
122 |
return; |
|
123 |
} |
|
124 |
iCurrentState = ENone; |
|
125 |
Cancel(); |
|
126 |
} |
|
0 | 127 |
|
128 |
||
129 |
/** |
|
130 |
Cancel outstanding request (if any) |
|
131 |
*/ |
|
132 |
void CControlInterfaceUsbcScLdd::DoCancel() |
|
286 | 133 |
{ |
134 |
switch(iCurrentState) |
|
135 |
{ |
|
136 |
case EReadEp0Data: |
|
137 |
iParent.Ldd().ReadCancel(KUsbcScEndpointZero); |
|
138 |
break; |
|
139 |
case ESendMaxLun: |
|
140 |
iParent.Ldd().WriteCancel(KUsbcScEndpointZero); |
|
141 |
break; |
|
142 |
default: |
|
143 |
__ASSERT_DEBUG(EFalse, User::Panic(KUsbMsSvrPncCat, EMsControlInterfaceBadState)); |
|
144 |
} |
|
145 |
} |
|
0 | 146 |
|
147 |
||
148 |
/** |
|
149 |
Implement CControlInterfaceUsbcScLdd state machine |
|
150 |
*/ |
|
151 |
void CControlInterfaceUsbcScLdd::RunL() |
|
286 | 152 |
{ |
153 |
if (iStatus != KErrNone) |
|
154 |
{ |
|
155 |
OstTrace1(TRACE_SMASSSTORAGE_BOT, CCONTROLINTERFACEUSBCSCLDD, |
|
156 |
"ERROR %d in RunL", iStatus.Int()); |
|
157 |
//read EP0 again |
|
158 |
ReadEp0Data(); |
|
159 |
return; |
|
160 |
} |
|
0 | 161 |
|
286 | 162 |
ReadEp0Data(); |
163 |
} |
|
0 | 164 |
|
165 |
/** |
|
166 |
Actual Read to RDevUsbcScClient BIL |
|
167 |
*/ |
|
168 |
TInt CControlInterfaceUsbcScLdd::ReadUsbEp0() |
|
286 | 169 |
{ |
170 |
iCurrentState = EReadEp0Data; |
|
171 |
iStatus = KRequestPending; |
|
172 |
return iEp0Buf.GetBuffer (iEp0Packet,iEp0Size,iEp0Zlp,iStatus); |
|
173 |
} |
|
0 | 174 |
|
175 |
||
176 |
/** |
|
177 |
Post a read request to EEndpoint0 to read request header |
|
178 |
*/ |
|
179 |
TInt CControlInterfaceUsbcScLdd::ReadEp0Data() |
|
286 | 180 |
{ |
181 |
if (IsActive()) |
|
182 |
{ |
|
183 |
OstTrace0(TRACE_SMASSSTORAGE_BOT, READEP0DATA_SC, "Still active"); |
|
184 |
return KErrServerBusy; |
|
185 |
} |
|
0 | 186 |
|
286 | 187 |
TInt r = KErrNone; |
188 |
do |
|
189 |
{ |
|
190 |
r = ReadUsbEp0(); |
|
191 |
if (r == KErrCompletion) |
|
192 |
{ |
|
193 |
iStatus = KErrNone; |
|
194 |
DecodeEp0Data(); |
|
195 |
} |
|
196 |
else if (r == KErrNone) |
|
197 |
{ |
|
198 |
SetActive(); |
|
199 |
} |
|
200 |
} while (((r == KErrCompletion) || (r == TEndpointBuffer::KStateChange)) && (!IsActive())); |
|
201 |
return KErrNone; |
|
202 |
} |
|
0 | 203 |
|
204 |
||
205 |
/** |
|
206 |
Decode request header and do appropriate action - get max LUN info or post a reset request |
|
207 |
*/ |
|
208 |
void CControlInterfaceUsbcScLdd::DecodeEp0Data() |
|
286 | 209 |
{ |
210 |
if (IsActive()) |
|
211 |
{ |
|
212 |
__ASSERT_DEBUG(EFalse, User::Panic(KUsbMsSvrPncCat, EMsControlInterfaceStillActive)); |
|
213 |
return; |
|
214 |
} |
|
0 | 215 |
|
286 | 216 |
TPtrC8 ep0ReadDataPtr((TUint8*)iEp0Packet, iEp0Size); |
217 |
TInt err = iRequestHeader.Decode(ep0ReadDataPtr); |
|
0 | 218 |
|
286 | 219 |
if(err != KErrNone) |
220 |
return; |
|
0 | 221 |
|
222 |
switch(iRequestHeader.iRequest) |
|
286 | 223 |
{ |
224 |
// |
|
225 |
// GET MAX LUN (0xFE) |
|
226 |
// |
|
227 |
case TUsbRequestHdr::EReqGetMaxLun: |
|
228 |
{ |
|
229 |
OstTrace1(TRACE_SMASSSTORAGE_BOT, DECODEEP0DATA_SC, ">>> EP0 GetMaxLun = %d", iParent.MaxLun()); |
|
0 | 230 |
|
231 |
if ( iRequestHeader.iRequestType != 0xA1 //value from USB MS BOT spec |
|
232 |
|| iRequestHeader.iIndex > 15 |
|
233 |
|| iRequestHeader.iValue != 0 |
|
234 |
|| iRequestHeader.iLength != 1) |
|
235 |
{ |
|
286 | 236 |
OstTrace0(TRACE_SMASSSTORAGE_BOT, DECODEEP0DATA1_SC, "ERROR: GetMaxLun command packet check error"); |
0 | 237 |
iParent.Ldd().EndpointZeroRequestError(); |
238 |
break; |
|
239 |
} |
|
240 |
||
286 | 241 |
TPtr8* ep0WriteDataPtr = NULL; |
242 |
TUint ep0Length; |
|
243 |
iEp0Buf.GetInBufferRange(((TAny*&)ep0WriteDataPtr),ep0Length); |
|
244 |
ep0WriteDataPtr->SetLength(1); //Return only 1 byte to host |
|
245 |
ep0WriteDataPtr->Fill(0); |
|
246 |
ep0WriteDataPtr->Fill(iParent.MaxLun()); // Supported Units |
|
247 |
TInt length = ep0WriteDataPtr->Length(); |
|
248 |
err = iEp0Buf.WriteBuffer((TPtr8*)(ep0WriteDataPtr->Ptr()),length,ETrue,iStatus); |
|
0 | 249 |
|
286 | 250 |
iCurrentState = ESendMaxLun; |
251 |
SetActive(); |
|
0 | 252 |
|
286 | 253 |
return; |
254 |
} |
|
255 |
// |
|
256 |
// RESET (0xFF) |
|
257 |
// |
|
258 |
case TUsbRequestHdr::EReqReset: |
|
259 |
{ |
|
260 |
OstTrace0(TRACE_SMASSSTORAGE_BOT, DECODEEP0DATA2_SC, ">>> EP0 BulkOnlyMassStorageReset"); |
|
0 | 261 |
|
262 |
if ( iRequestHeader.iRequestType != 0x21 //value from USB MS BOT spec |
|
263 |
|| iRequestHeader.iIndex > 15 |
|
264 |
|| iRequestHeader.iValue != 0 |
|
265 |
|| iRequestHeader.iLength != 0) |
|
266 |
{ |
|
286 | 267 |
OstTrace0(TRACE_SMASSSTORAGE_BOT, DECODEEP0DATA3_SC, "BulkOnlyMassStorageReset command packet check error"); |
0 | 268 |
iParent.Ldd().EndpointZeroRequestError(); |
269 |
break; |
|
270 |
} |
|
271 |
||
286 | 272 |
iParent.HwStop(); |
273 |
iParent.Controller().Reset(); |
|
274 |
iParent.HwStart(ETrue); |
|
0 | 275 |
|
276 |
err = iParent.Ldd().SendEp0StatusPacket(); |
|
286 | 277 |
return; |
278 |
} |
|
279 |
// |
|
280 |
// Unknown? |
|
281 |
// |
|
282 |
default: |
|
283 |
{ |
|
284 |
OstTrace0(TRACE_SMASSSTORAGE_BOT, DECODEEP0DATA4_SC, ">>> EP0 DecodeEp0Data : Unknown Request"); |
|
285 |
} |
|
286 |
} |
|
287 |
ReadEp0Data(); //try to get another request |
|
288 |
} |
|
0 | 289 |
|
290 |
||
291 |
// |
|
292 |
// --- class CBulkOnlyTransportUsbcScLdd --------------------------------------------------------- |
|
293 |
// |
|
294 |
||
295 |
CBulkOnlyTransportUsbcScLdd::CBulkOnlyTransportUsbcScLdd(TInt aNumDrives,CUsbMassStorageController& aController) |
|
286 | 296 |
:CBulkOnlyTransport(aNumDrives, aController) |
297 |
{ |
|
298 |
} |
|
0 | 299 |
|
300 |
/** |
|
301 |
Constructs the CBulkOnlyTransportUsbcScLdd object |
|
302 |
*/ |
|
303 |
void CBulkOnlyTransportUsbcScLdd::ConstructL() |
|
286 | 304 |
{ |
305 |
iControlInterface = CControlInterfaceUsbcScLdd::NewL(*this); |
|
306 |
iDeviceStateNotifier = CActiveDeviceStateNotifierBase::NewL(*this, *this); |
|
307 |
iChunk = new RChunk(); |
|
308 |
CActiveScheduler::Add(this); |
|
309 |
} |
|
0 | 310 |
|
311 |
||
312 |
CBulkOnlyTransportUsbcScLdd::~CBulkOnlyTransportUsbcScLdd() |
|
286 | 313 |
{ |
314 |
if (iInterfaceConfigured) |
|
315 |
{ |
|
269 | 316 |
Stop(); |
286 | 317 |
TInt err = iSCReadEndpointBuf.Close(); |
318 |
err = iSCWriteEndpointBuf.Close(); |
|
319 |
delete iControlInterface ; |
|
320 |
delete iDeviceStateNotifier; |
|
321 |
delete iChunk; |
|
322 |
} |
|
323 |
} |
|
0 | 324 |
|
325 |
RDevUsbcScClient& CBulkOnlyTransportUsbcScLdd::Ldd() |
|
286 | 326 |
{ |
327 |
return iLdd; |
|
328 |
} |
|
0 | 329 |
|
330 |
/** |
|
331 |
Set or unset configuration descriptor for USB MassStorage Bulk Only transport |
|
332 |
||
333 |
@param aUnset indicate whether set or unset descriptor |
|
334 |
@return KErrNone if operation was completed successfully, errorcode otherwise |
|
335 |
*/ |
|
336 |
TInt CBulkOnlyTransportUsbcScLdd::SetupConfigurationDescriptor(TBool aUnset) |
|
286 | 337 |
{ |
338 |
TInt ret(KErrNone); |
|
0 | 339 |
|
286 | 340 |
if ((ret = iLdd.Open(0)) != KErrNone) |
341 |
return ret; |
|
0 | 342 |
|
286 | 343 |
TInt configDescriptorSize(0); |
344 |
iLdd.GetConfigurationDescriptorSize(configDescriptorSize); |
|
345 |
if (static_cast<TUint>(configDescriptorSize) != KUsbDescSize_Config) |
|
346 |
{ |
|
347 |
return KErrCorrupt; |
|
348 |
} |
|
0 | 349 |
|
286 | 350 |
TBuf8<KUsbDescSize_Config> configDescriptor; |
351 |
ret = iLdd.GetConfigurationDescriptor(configDescriptor); |
|
352 |
if (ret != KErrNone) |
|
353 |
{ |
|
354 |
return ret; |
|
355 |
} |
|
0 | 356 |
|
286 | 357 |
// I beleive that other fields setted up during LDD initialisation |
358 |
if (aUnset) |
|
359 |
{ |
|
360 |
--configDescriptor[KUsbNumInterfacesOffset]; |
|
361 |
} |
|
362 |
else |
|
363 |
{ |
|
364 |
++configDescriptor[KUsbNumInterfacesOffset]; |
|
365 |
} |
|
366 |
ret = iLdd.SetConfigurationDescriptor(configDescriptor); |
|
0 | 367 |
|
286 | 368 |
if (aUnset) |
369 |
{ |
|
370 |
iLdd.Close(); |
|
371 |
} |
|
0 | 372 |
|
286 | 373 |
return ret; |
374 |
} |
|
0 | 375 |
|
376 |
/** |
|
377 |
Set up interface descriptor |
|
378 |
||
379 |
@return KErrNone if operation was completed successfully, errorcode otherwise |
|
380 |
*/ |
|
381 |
TInt CBulkOnlyTransportUsbcScLdd::SetupInterfaceDescriptors() |
|
286 | 382 |
{ |
383 |
// Device caps |
|
384 |
TUsbDeviceCaps d_caps; |
|
385 |
TInt ret = iLdd.DeviceCaps(d_caps); |
|
386 |
if (ret != KErrNone) |
|
387 |
{ |
|
388 |
return ret; |
|
389 |
} |
|
390 |
TInt totalEndpoints = d_caps().iTotalEndpoints; |
|
391 |
if (totalEndpoints < KRequiredNumberOfEndpoints) |
|
392 |
{ |
|
393 |
return KErrHardwareNotAvailable; |
|
394 |
} |
|
0 | 395 |
|
286 | 396 |
// Endpoint caps |
397 |
TUsbcEndpointData data[KUsbcMaxEndpoints]; |
|
398 |
TPtr8 dataptr(reinterpret_cast<TUint8*>(data), sizeof(data), sizeof(data)); |
|
399 |
ret = iLdd.EndpointCaps(dataptr); |
|
400 |
if (ret != KErrNone) |
|
401 |
{ |
|
402 |
return ret; |
|
403 |
} |
|
0 | 404 |
|
286 | 405 |
// Set the active interface |
406 |
TUsbcScInterfaceInfoBuf ifc; |
|
407 |
TInt ep_found = 0; |
|
408 |
TBool foundBulkIN = EFalse; |
|
409 |
TBool foundBulkOUT = EFalse; |
|
0 | 410 |
|
286 | 411 |
for (TInt i = 0; i < totalEndpoints ; i++) |
412 |
{ |
|
413 |
const TUsbcEndpointCaps* caps = &data[i].iCaps; |
|
414 |
const TInt maxPacketSize = caps->MaxPacketSize(); |
|
415 |
if (!foundBulkIN && |
|
416 |
(caps->iTypesAndDir & (KUsbEpTypeBulk | KUsbEpDirIn)) == (KUsbEpTypeBulk | KUsbEpDirIn)) |
|
417 |
{ |
|
418 |
// InEndpoint is going to be our TX (IN, write) endpoint |
|
419 |
ifc().iEndpointData[0].iType = KUsbEpTypeBulk; |
|
420 |
if((d_caps().iFeatureWord1 & KUsbDevCapsFeatureWord1_EndpointResourceAllocV2) == KUsbDevCapsFeatureWord1_EndpointResourceAllocV2) |
|
421 |
ifc().iEndpointData[0].iFeatureWord1 = KUsbcEndpointInfoFeatureWord1_DMA|KUsbcEndpointInfoFeatureWord1_DoubleBuffering; |
|
422 |
ifc().iEndpointData[0].iDir = KUsbEpDirIn; |
|
423 |
ifc().iEndpointData[0].iSize = maxPacketSize; |
|
424 |
ifc().iEndpointData[0].iInterval_Hs = 0; |
|
425 |
ifc().iEndpointData[0].iBufferSize = KMaxScBufferSize; |
|
426 |
foundBulkIN = ETrue; |
|
427 |
if (++ep_found == KRequiredNumberOfEndpoints) |
|
428 |
{ |
|
429 |
break; |
|
430 |
} |
|
431 |
continue; |
|
432 |
} |
|
433 |
if (!foundBulkOUT && |
|
434 |
(caps->iTypesAndDir & (KUsbEpTypeBulk | KUsbEpDirOut)) == (KUsbEpTypeBulk | KUsbEpDirOut)) |
|
435 |
{ |
|
436 |
// OutEndpoint is going to be our RX (OUT, read) endpoint |
|
437 |
ifc().iEndpointData[1].iType = KUsbEpTypeBulk; |
|
438 |
if((d_caps().iFeatureWord1 & KUsbDevCapsFeatureWord1_EndpointResourceAllocV2) == KUsbDevCapsFeatureWord1_EndpointResourceAllocV2) |
|
439 |
ifc().iEndpointData[1].iFeatureWord1 = KUsbcEndpointInfoFeatureWord1_DMA|KUsbcEndpointInfoFeatureWord1_DoubleBuffering; |
|
440 |
ifc().iEndpointData[1].iDir = KUsbEpDirOut; |
|
441 |
ifc().iEndpointData[1].iSize = maxPacketSize; |
|
442 |
ifc().iEndpointData[1].iInterval_Hs = 0; |
|
443 |
ifc().iEndpointData[1].iBufferSize = KMaxScBufferSize; |
|
444 |
ifc().iEndpointData[1].iReadSize = KMaxScReadSize; |
|
0 | 445 |
|
286 | 446 |
foundBulkOUT = ETrue; |
447 |
if (++ep_found == KRequiredNumberOfEndpoints) |
|
448 |
{ |
|
449 |
break; |
|
450 |
} |
|
451 |
continue; |
|
452 |
} |
|
453 |
} |
|
454 |
if (ep_found != KRequiredNumberOfEndpoints) |
|
455 |
{ |
|
456 |
return KErrHardwareNotAvailable; |
|
457 |
} |
|
0 | 458 |
|
286 | 459 |
_LIT16(string, "USB Mass Storage Interface"); |
460 |
ifc().iString = const_cast<TDesC16*>(&string); |
|
461 |
ifc().iTotalEndpointsUsed = KRequiredNumberOfEndpoints; |
|
462 |
ifc().iClass.iClassNum = 0x08; // Mass Storage |
|
463 |
ifc().iClass.iSubClassNum = 0x06; // SCSI Transparent Command Set |
|
464 |
ifc().iClass.iProtocolNum = 0x50; // Bulk Only Transport |
|
0 | 465 |
|
286 | 466 |
if (d_caps().iHighSpeed) |
467 |
{ |
|
468 |
// Tell the Protocol about it, because it might want to do some |
|
469 |
// optimizing too. |
|
470 |
iProtocol->ReportHighSpeedDevice(); |
|
471 |
} |
|
0 | 472 |
|
286 | 473 |
if ((ret = iLdd.SetInterface(0, ifc)) == KErrNone) |
474 |
{ |
|
475 |
return (iLdd.FinalizeInterface(iChunk)); |
|
476 |
} |
|
477 |
return ret; |
|
478 |
} |
|
0 | 479 |
|
480 |
void CBulkOnlyTransportUsbcScLdd::ReleaseInterface() |
|
286 | 481 |
{ |
482 |
iLdd.ReleaseInterface(0); |
|
483 |
} |
|
0 | 484 |
|
485 |
||
486 |
TInt CBulkOnlyTransportUsbcScLdd::StartControlInterface() |
|
286 | 487 |
{ |
488 |
return iControlInterface->Start(); |
|
489 |
} |
|
0 | 490 |
|
491 |
void CBulkOnlyTransportUsbcScLdd::CancelControlInterface() |
|
286 | 492 |
{ |
493 |
iControlInterface->Cancel(); |
|
494 |
} |
|
0 | 495 |
|
496 |
void CBulkOnlyTransportUsbcScLdd::ActivateDeviceStateNotifier() |
|
286 | 497 |
{ |
498 |
iDeviceStateNotifier->Activate(); |
|
499 |
} |
|
0 | 500 |
|
501 |
void CBulkOnlyTransportUsbcScLdd::CancelDeviceStateNotifier() |
|
286 | 502 |
{ |
503 |
iDeviceStateNotifier->Cancel(); |
|
504 |
} |
|
0 | 505 |
|
506 |
||
507 |
void CBulkOnlyTransportUsbcScLdd::CancelReadWriteRequests() |
|
286 | 508 |
{ |
509 |
TUsbcScChunkHeader chunkHeader(*iChunk); |
|
510 |
for (TInt i = 0; i < chunkHeader.iAltSettings->iNumOfAltSettings; i++) |
|
511 |
{ |
|
512 |
TInt8* endpoint = (TInt8*) (chunkHeader.iAltSettings->iAltTableOffset[i] + (TInt) iChunk->Base()); |
|
513 |
TInt numOfEps = chunkHeader.GetNumberOfEndpoints(i); |
|
0 | 514 |
__ASSERT_DEBUG(numOfEps >= 0, User::Invariant()); |
286 | 515 |
for (TInt j = 1; j <= numOfEps; j++) |
516 |
{ |
|
517 |
TUsbcScHdrEndpointRecord* endpointInf = (TUsbcScHdrEndpointRecord*) &(endpoint[j * chunkHeader.iAltSettings->iEpRecordSize]); |
|
518 |
if (endpointInf->Direction() == KUsbScHdrEpDirectionOut) |
|
519 |
{ |
|
520 |
iLdd.ReadCancel(endpointInf->iBufferNo); |
|
521 |
} |
|
522 |
if (endpointInf->Direction() == KUsbScHdrEpDirectionIn) |
|
523 |
{ |
|
524 |
iLdd.WriteCancel(endpointInf->iBufferNo); |
|
525 |
} |
|
526 |
} |
|
527 |
} |
|
528 |
} |
|
0 | 529 |
|
530 |
void CBulkOnlyTransportUsbcScLdd::AllocateEndpointResources() |
|
286 | 531 |
{ |
532 |
TUsbcScChunkHeader chunkHeader(*iChunk); |
|
533 |
for (TInt i = 0; i < chunkHeader.iAltSettings->iNumOfAltSettings; i++) |
|
534 |
{ |
|
535 |
TInt8* endpoint = (TInt8*) (chunkHeader.iAltSettings->iAltTableOffset[i] + (TInt) iChunk->Base()); |
|
0 | 536 |
|
286 | 537 |
for (TInt j = 1; j <= chunkHeader.GetNumberOfEndpoints(i); j++) |
538 |
{ |
|
539 |
TUsbcScHdrEndpointRecord* endpointInf = (TUsbcScHdrEndpointRecord*) &(endpoint[j * chunkHeader.iAltSettings->iEpRecordSize]); |
|
540 |
if (endpointInf->Direction() == KUsbScHdrEpDirectionOut) |
|
541 |
{ |
|
542 |
iOutEndpoint = j; |
|
543 |
} |
|
544 |
if (endpointInf->Direction() == KUsbScHdrEpDirectionIn) |
|
545 |
{ |
|
546 |
iInEndpoint = j; |
|
547 |
} |
|
548 |
} |
|
549 |
} |
|
39
5d2844f35677
Revision: 201004
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
550 |
|
286 | 551 |
TUsbDeviceCaps d_caps; |
552 |
TInt err; |
|
553 |
TInt ret = iLdd.DeviceCaps(d_caps); |
|
554 |
if (ret == KErrNone) |
|
555 |
{ |
|
556 |
if((d_caps().iFeatureWord1 & KUsbDevCapsFeatureWord1_EndpointResourceAllocV2) != KUsbDevCapsFeatureWord1_EndpointResourceAllocV2) |
|
557 |
{ |
|
558 |
// Set up DMA if possible (errors are non-critical) |
|
559 |
err = iLdd.AllocateEndpointResource(iOutEndpoint, EUsbcEndpointResourceDMA); |
|
560 |
if (err != KErrNone) |
|
561 |
{ |
|
562 |
OstTrace1(TRACE_SMASSSTORAGE_BOT, A1_SC, "Set DMA on OUT endpoint failed with error code: %d", err); |
|
563 |
} |
|
564 |
err = iLdd.AllocateEndpointResource(iInEndpoint, EUsbcEndpointResourceDMA); |
|
565 |
if (err != KErrNone) |
|
566 |
{ |
|
567 |
OstTrace1(TRACE_SMASSSTORAGE_BOT, A2_SC, "Set DMA on IN endpoint failed with error code: %d", err); |
|
568 |
} |
|
39
5d2844f35677
Revision: 201004
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
569 |
|
286 | 570 |
// Set up Double Buffering if possible (errors are non-critical) |
571 |
err = iLdd.AllocateEndpointResource(iOutEndpoint, EUsbcEndpointResourceDoubleBuffering); |
|
572 |
if (err != KErrNone) |
|
573 |
{ |
|
574 |
OstTrace1(TRACE_SMASSSTORAGE_BOT, A3_SC, "Set Double Buffering on OUT endpoint failed with error code: %d", err); |
|
575 |
} |
|
576 |
err = iLdd.AllocateEndpointResource(iInEndpoint, EUsbcEndpointResourceDoubleBuffering); |
|
577 |
if (err != KErrNone) |
|
578 |
{ |
|
579 |
OstTrace1(TRACE_SMASSSTORAGE_BOT, A4_SC, "Set Double Buffering on IN endpoint failed with error code: %d", err); |
|
580 |
} |
|
581 |
} |
|
582 |
} |
|
583 |
||
584 |
err = OpenEndpoints(); |
|
585 |
} |
|
0 | 586 |
|
587 |
TInt CBulkOnlyTransportUsbcScLdd::OpenEndpoints() |
|
286 | 588 |
{ |
589 |
TInt res = iLdd.OpenEndpoint(iSCReadEndpointBuf, iOutEndpoint); |
|
590 |
if (res == KErrNone) |
|
591 |
{ |
|
592 |
res = iLdd.OpenEndpoint(iSCWriteEndpointBuf, iInEndpoint); |
|
593 |
if (res == KErrNone) |
|
594 |
{ |
|
595 |
iSCWriteEndpointBuf.GetInBufferRange((TAny*&)iDataPtr, iInBufferLength); |
|
596 |
res = iControlInterface->OpenEp0(); |
|
597 |
} |
|
598 |
return res; |
|
599 |
} |
|
0 | 600 |
|
286 | 601 |
return res; |
602 |
} |
|
0 | 603 |
|
604 |
||
605 |
TInt CBulkOnlyTransportUsbcScLdd::GetDeviceStatus(TUsbcDeviceState& deviceStatus) |
|
286 | 606 |
{ |
607 |
return iLdd.DeviceStatus(deviceStatus); |
|
608 |
} |
|
0 | 609 |
|
610 |
void CBulkOnlyTransportUsbcScLdd::FlushData() |
|
286 | 611 |
{ |
612 |
// Intentionally Left Blank Do Nothing |
|
613 |
} |
|
0 | 614 |
|
615 |
/** |
|
616 |
* Read out rest data from OutEndpoint and discard them |
|
617 |
*/ |
|
618 |
void CBulkOnlyTransportUsbcScLdd::ReadAndDiscardData(TInt /*aBytes*/) |
|
286 | 619 |
{ |
620 |
TRequestStatus status; |
|
621 |
TInt r = KErrNone; // Lets assume there is no data |
|
622 |
do |
|
623 |
{ |
|
624 |
iSCReadSize = 0; |
|
625 |
status = KErrNone; |
|
626 |
r = iSCReadEndpointBuf.TakeBuffer(iSCReadData,iSCReadSize,iReadZlp,status); |
|
627 |
iSCReadEndpointBuf.Expire(); |
|
628 |
} |
|
629 |
while (r == KErrCompletion); |
|
0 | 630 |
|
286 | 631 |
if (r == KErrNone) |
632 |
{ |
|
633 |
TRequestStatus* stat = &status; |
|
634 |
User::RequestComplete(stat, KErrNone); |
|
635 |
} |
|
636 |
} |
|
0 | 637 |
|
638 |
/** |
|
639 |
Called by the protocol to determine how many bytes of data are available in the read buffer. |
|
640 |
||
641 |
@return The number of bytes available in the read buffer |
|
642 |
*/ |
|
643 |
TInt CBulkOnlyTransportUsbcScLdd::BytesAvailable() |
|
286 | 644 |
{ |
645 |
// Intentionally Left Blank Do Nothing |
|
646 |
return 0; |
|
647 |
} |
|
0 | 648 |
|
649 |
void CBulkOnlyTransportUsbcScLdd::StallEndpointAndWaitForClear() |
|
286 | 650 |
{ |
651 |
// Now stall this endpoint |
|
652 |
OstTrace1(TRACE_SMASSSTORAGE_BOT, D1_SC, "Stalling endpoint %d", iInEndpoint); |
|
653 |
TInt r = iLdd.HaltEndpoint(iInEndpoint); |
|
654 |
if (r != KErrNone) |
|
655 |
{ |
|
656 |
OstTraceExt2(TRACE_SMASSSTORAGE_BOT, D2_SC, "Error: stalling ep %d failed: %d", iInEndpoint, r); |
|
657 |
} |
|
658 |
TEndpointState ep_state; |
|
659 |
TInt i = 0; |
|
660 |
do |
|
661 |
{ |
|
662 |
// Wait for 10ms before checking the ep status |
|
663 |
User::After(10000); |
|
664 |
iLdd.EndpointStatus(iInEndpoint, ep_state); |
|
665 |
if (++i >= 550) |
|
666 |
{ |
|
667 |
// 5.5 secs should be enough (see 9.2.6.1 Request Processing Timing) |
|
668 |
OstTrace1(TRACE_SMASSSTORAGE_BOT, D3_SC, "Error: Checked for ep %d de-stall for 5.5s - giving up now", iInEndpoint); |
|
669 |
// We can now only hope for a Reset Recovery |
|
670 |
return; |
|
671 |
} |
|
672 |
} while ((ep_state == EEndpointStateStalled) && iStarted); |
|
673 |
OstTraceExt2(TRACE_SMASSSTORAGE_BOT, D4_SC, "Checked for ep %d de-stall: %d time(s)", iInEndpoint, i); |
|
674 |
} |
|
0 | 675 |
|
676 |
/** |
|
677 |
Read CBW data from the host and decode it. |
|
678 |
*/ |
|
679 |
void CBulkOnlyTransportUsbcScLdd::ReadCBW() |
|
286 | 680 |
{ |
681 |
if (IsActive()) |
|
682 |
{ |
|
683 |
__ASSERT_DEBUG(EFalse, User::Panic(KUsbMsSvrPncCat, EMsBulkOnlyStillActive)); |
|
684 |
return; |
|
685 |
} |
|
686 |
TInt r = KErrNone; |
|
687 |
do |
|
688 |
{ |
|
689 |
r = ReadUsb(); |
|
690 |
if ((r == KErrCompletion) && (iSCReadSize == KCbwLength)) |
|
691 |
{ |
|
692 |
iCurrentState = EWaitForCBW; |
|
693 |
iStatus = KErrNone; |
|
694 |
DecodeCBW(); |
|
695 |
iSCReadSize = 0; |
|
696 |
} |
|
697 |
else if (r == KErrNone) |
|
698 |
{ |
|
699 |
iCurrentState = EWaitForCBW; |
|
700 |
SetActive(); |
|
701 |
} |
|
702 |
else if ((r == KErrCompletion) && (iSCReadSize != KCbwLength)) |
|
703 |
{ |
|
704 |
ExpireData(iSCReadData); |
|
705 |
} |
|
706 |
}while ((r == KErrCompletion) && (!IsActive())); |
|
707 |
} |
|
0 | 708 |
|
709 |
void CBulkOnlyTransportUsbcScLdd::ExpireData(TAny* aAddress) |
|
286 | 710 |
{ |
711 |
if (aAddress) |
|
712 |
{ |
|
713 |
iSCReadEndpointBuf.Expire(aAddress); |
|
714 |
} |
|
715 |
else |
|
716 |
{ |
|
717 |
iSCReadEndpointBuf.Expire(); |
|
718 |
} |
|
719 |
} |
|
0 | 720 |
|
721 |
void CBulkOnlyTransportUsbcScLdd::ProcessCbwEvent() |
|
286 | 722 |
{ |
723 |
ReadCBW(); |
|
724 |
} |
|
0 | 725 |
|
726 |
/** |
|
727 |
Request data form the host for the protocol |
|
728 |
||
729 |
@param aLength amount of data (in bytes) to be received from the host |
|
730 |
*/ |
|
731 |
void CBulkOnlyTransportUsbcScLdd::ReadData(TUint /*aLength*/) |
|
286 | 732 |
{ |
733 |
if (IsActive()) |
|
734 |
{ |
|
735 |
__ASSERT_DEBUG(EFalse, User::Panic(KUsbMsSvrPncCat, EMsBulkOnlyStillActive)); |
|
736 |
return; |
|
737 |
} |
|
738 |
TInt r = KErrNone; |
|
739 |
do |
|
740 |
{ |
|
741 |
r = ReadUsb(); |
|
742 |
iCurrentState = EReadingData; |
|
743 |
if (r == KErrCompletion) |
|
744 |
{ |
|
745 |
iReadBufPtr.Set((TUint8*) iSCReadData, iSCReadSize, iSCReadSize); |
|
746 |
iStatus = KErrNone; |
|
747 |
ProcessDataFromHost(); |
|
748 |
iSCReadSize = 0; |
|
749 |
} |
|
750 |
else if (r == KErrNone) |
|
751 |
{ |
|
752 |
iSCReadSize = 0; |
|
753 |
SetActive(); |
|
754 |
} |
|
0 | 755 |
|
286 | 756 |
}while ((r == KErrCompletion) && (!IsActive())); |
757 |
} |
|
0 | 758 |
|
759 |
void CBulkOnlyTransportUsbcScLdd::ProcessDataFromHost() |
|
286 | 760 |
{ |
761 |
TInt ret = KErrNone; |
|
762 |
{ |
|
763 |
if (iReadSetUp) |
|
764 |
{ |
|
765 |
ret = iProtocol->ReadComplete(KErrNone); |
|
766 |
} |
|
0 | 767 |
#ifndef MSDC_MULTITHREADED |
286 | 768 |
ExpireData(iSCReadData); |
0 | 769 |
#endif |
286 | 770 |
TUint deviceDataLength = iSCReadSize; // What was written to the disk // static_cast<TUint>(iReadBuf.Length()); |
771 |
if(ret == KErrCompletion) |
|
772 |
{ |
|
773 |
// The protocol has indicated with KErrCompletion that sufficient |
|
774 |
// data is available in the buffer to process the transfer immediately. |
|
0 | 775 |
|
286 | 776 |
//iDataResidue is initially set to host data length as we do not know how much data is there in the 'LDD transfer'. |
777 |
//After a TakeBuffer call, iSCReadSize in updated and set to deviceDataLength |
|
778 |
iDataResidue -= deviceDataLength; |
|
779 |
} |
|
780 |
else |
|
781 |
{ |
|
782 |
iDataResidue -= deviceDataLength; |
|
0 | 783 |
|
286 | 784 |
// The protocol has indicated that transfer is |
785 |
// complete, so send the CSW response to the host. |
|
786 |
iReadSetUp = EFalse; |
|
0 | 787 |
|
286 | 788 |
if (ret != KErrNone) |
789 |
{ |
|
790 |
iCmdStatus = ECommandFailed; |
|
791 |
} |
|
0 | 792 |
|
286 | 793 |
if (iDataResidue) |
794 |
{ |
|
795 |
OstTrace0(TRACE_SMASSSTORAGE_BOT, E2_SC, "Discarding residue"); |
|
796 |
// we have to read as much data as available that host PC sends; |
|
797 |
// otherwise, bulk-out endpoint will need to keep sending NAK back. |
|
798 |
ReadAndDiscardData(iDataResidue); |
|
799 |
} |
|
0 | 800 |
|
286 | 801 |
SendCSW(iCbwTag, iDataResidue, iCmdStatus); |
802 |
} |
|
803 |
} |
|
804 |
} |
|
0 | 805 |
|
806 |
void CBulkOnlyTransportUsbcScLdd::WriteUsb(TRequestStatus& aStatus, TPtrC8& aDes, TUint aLength, TBool aZlpRequired) |
|
286 | 807 |
{ |
808 |
TUint aOffset = (TUint) aDes.Ptr() - (TUint) iChunk->Base(); |
|
809 |
iSCWriteEndpointBuf.WriteBuffer(aOffset, aLength, aZlpRequired, aStatus); |
|
810 |
} |
|
0 | 811 |
|
812 |
void CBulkOnlyTransportUsbcScLdd::SetCbwPtr() |
|
286 | 813 |
{ |
814 |
iCbwBufPtr.Set((TUint8*)iSCReadData, iSCReadSize); |
|
815 |
} |
|
0 | 816 |
|
817 |
TPtr8& CBulkOnlyTransportUsbcScLdd::SetCommandBufPtr(TUint aLength) |
|
286 | 818 |
{ |
819 |
TPtr8 writeBuf((TUint8*) iDataPtr, aLength); |
|
820 |
iCommandBufPtr.Set(writeBuf); |
|
821 |
return iCommandBufPtr; |
|
822 |
} |
|
0 | 823 |
|
824 |
void CBulkOnlyTransportUsbcScLdd::SetReadDataBufPtr(TUint /*aLength*/) |
|
286 | 825 |
{ |
826 |
// Do nothing for now |
|
827 |
} |
|
0 | 828 |
|
829 |
TPtr8& CBulkOnlyTransportUsbcScLdd::SetDataBufPtr() |
|
286 | 830 |
{ |
831 |
TPtr8 writeBuf((TUint8*) iDataPtr, iInBufferLength); |
|
832 |
iDataBufPtr.Set(writeBuf); |
|
833 |
return iDataBufPtr; |
|
834 |
} |
|
0 | 835 |
|
836 |
void CBulkOnlyTransportUsbcScLdd::SetPaddingBufPtr(TUint aLength) |
|
286 | 837 |
{ |
838 |
TPtr8 writeBuf((TUint8*) iDataPtr, aLength, aLength); |
|
839 |
iPaddingBufPtr.Set(writeBuf); |
|
840 |
} |
|
0 | 841 |
|
842 |
void CBulkOnlyTransportUsbcScLdd::SetCswBufPtr(TUint aLength) |
|
286 | 843 |
{ |
844 |
TPtr8 writeBuf((TUint8*) iDataPtr, aLength, aLength); |
|
845 |
iCswBufPtr.Set(writeBuf); |
|
846 |
} |
|
0 | 847 |
|
848 |
void CBulkOnlyTransportUsbcScLdd::ProcessReadingDataEvent() |
|
286 | 849 |
{ |
850 |
ReadData(); |
|
851 |
} |
|
0 | 852 |
|
853 |
TInt CBulkOnlyTransportUsbcScLdd::ReadUsb(TUint /*aLength*/) |
|
286 | 854 |
{ |
855 |
return iSCReadEndpointBuf.TakeBuffer(iSCReadData,iSCReadSize,iReadZlp,iStatus); |
|
856 |
} |
|
0 | 857 |
|
858 |
void CBulkOnlyTransportUsbcScLdd::DiscardData(TUint aLength) |
|
286 | 859 |
{ |
860 |
TUint c = 0; |
|
861 |
TRequestStatus status; |
|
862 |
TInt r = KErrNone; |
|
863 |
while (c < aLength) |
|
864 |
{ |
|
865 |
iSCReadSize = 0; |
|
866 |
status = KErrNone; |
|
867 |
r = iSCReadEndpointBuf.TakeBuffer(iSCReadData,iSCReadSize,iReadZlp,status); |
|
868 |
c += iSCReadSize; |
|
869 |
if (r == KErrNone) |
|
870 |
User::WaitForRequest(status); |
|
871 |
if (r == KErrCompletion) |
|
872 |
{ |
|
873 |
iSCReadEndpointBuf.Expire(); |
|
874 |
} |
|
875 |
iSCReadSize = 0; |
|
876 |
} |
|
877 |
} |
|
0 | 878 |
|
879 |
void CBulkOnlyTransportUsbcScLdd::WriteToClient(TUint aLength) |
|
286 | 880 |
{ |
881 |
TUint c = 0; |
|
882 |
TRequestStatus status; |
|
883 |
TInt r = KErrNone; |
|
884 |
while (c < aLength) |
|
885 |
{ |
|
886 |
iSCReadSize = 0; |
|
887 |
status = KErrNone; |
|
888 |
r = iSCReadEndpointBuf.TakeBuffer(iSCReadData,iSCReadSize,iReadZlp,status); |
|
889 |
c += iSCReadSize; |
|
890 |
iProtocol->ReadComplete(KErrGeneral); |
|
891 |
if (r == KErrNone) |
|
892 |
User::WaitForRequest(status); |
|
893 |
if (r == KErrCompletion) |
|
894 |
{ |
|
895 |
iSCReadEndpointBuf.Expire(); |
|
896 |
} |
|
897 |
iSCReadSize = 0; |
|
898 |
} |
|
899 |
} |
|
0 | 900 |
|
901 |
#ifdef MSDC_MULTITHREADED |
|
902 |
void CBulkOnlyTransportUsbcScLdd::GetBufferPointers(TPtr8& aDes1, TPtr8& aDes2) |
|
286 | 903 |
{ |
904 |
//for DB |
|
905 |
TUint length = (TUint) (iInBufferLength - KCswBufferSize) / 2; |
|
0 | 906 |
|
286 | 907 |
TUint8* start = (TUint8*) (iDataPtr) + KCswBufferSize ; // 'first' buffer |
908 |
aDes1.Set(start, length, length); |
|
0 | 909 |
|
286 | 910 |
start = (TUint8*) (iDataPtr) + KCswBufferSize + length; // 'second' buffer |
911 |
aDes2.Set(start, length, length); |
|
912 |
} |
|
0 | 913 |
#endif |
914 |
||
915 |
void CBulkOnlyTransportUsbcScLdd::Activate(TRequestStatus& aStatus, TUint& aDeviceState) |
|
916 |
{ |
|
286 | 917 |
iLdd.AlternateDeviceStatusNotify(aStatus, aDeviceState); |
0 | 918 |
} |
919 |
||
920 |
||
921 |
void CBulkOnlyTransportUsbcScLdd::Cancel() |
|
922 |
{ |
|
286 | 923 |
iLdd.AlternateDeviceStatusNotifyCancel(); |
0 | 924 |
} |
925 |
||
926 |
||
927 |
||
928 |