300
|
1 |
// Copyright (c) 2009-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 |
|
|
18 |
/**
|
|
19 |
@file
|
|
20 |
@internalTechnology
|
|
21 |
*/
|
|
22 |
|
|
23 |
#include <e32base.h>
|
|
24 |
#include <d32usbc.h>
|
|
25 |
|
|
26 |
#include "mstypes.h"
|
|
27 |
#include "msctypes.h"
|
|
28 |
#include "usbmsclientpanic.h"
|
|
29 |
#include "botmsctypes.h"
|
|
30 |
#include "testman.h"
|
|
31 |
#include "botmscserver.h"
|
|
32 |
|
|
33 |
#include "mserverprotocol.h"
|
|
34 |
#include "mdevicetransport.h"
|
|
35 |
|
|
36 |
#include "mstypes.h"
|
|
37 |
#include "botcontrolinterface.h"
|
|
38 |
#include "debug.h"
|
|
39 |
#include "cusbmassstorageserver.h"
|
|
40 |
#include "cusbmassstoragecontroller.h"
|
|
41 |
#include "drivemanager.h"
|
|
42 |
#include "cbulkonlytransport.h"
|
|
43 |
|
|
44 |
#define KInEndpoint EEndpoint1
|
|
45 |
#define KOutEndpoint EEndpoint2
|
|
46 |
|
|
47 |
|
|
48 |
//-------------------------------------
|
|
49 |
/**
|
|
50 |
Create CBulkOnlyTransport object
|
|
51 |
@param aNumDrives - The number of drives available for MS
|
|
52 |
@param aController - reference to the parent
|
|
53 |
@return pointer to newly created object
|
|
54 |
*/
|
|
55 |
CBulkOnlyTransport* CBulkOnlyTransport::NewL(TInt aNumDrives,
|
|
56 |
CUsbMassStorageController& aController)
|
300
|
57 |
{
|
|
58 |
if (aNumDrives <=0 || static_cast<TUint>(aNumDrives) > KUsbMsMaxDrives)
|
|
59 |
{
|
|
60 |
User::Leave(KErrArgument);
|
|
61 |
}
|
|
62 |
CBulkOnlyTransport* self = new(ELeave) CBulkOnlyTransport(aNumDrives, aController);
|
|
63 |
CleanupStack::PushL(self);
|
|
64 |
self->ConstructL();
|
|
65 |
CleanupStack::Pop(self);
|
|
66 |
return self;
|
|
67 |
}
|
0
|
68 |
|
|
69 |
#ifdef MSDC_TESTMODE
|
|
70 |
CBulkOnlyTransport* CBulkOnlyTransport::NewL(TInt aNumDrives,
|
|
71 |
CUsbMassStorageController& aController,
|
|
72 |
TTestParser* aTestParser)
|
300
|
73 |
{
|
|
74 |
if (aNumDrives <=0 || static_cast<TUint>(aNumDrives) > KUsbMsMaxDrives)
|
|
75 |
{
|
|
76 |
User::Leave(KErrArgument);
|
|
77 |
}
|
|
78 |
CBulkOnlyTransport* self = new(ELeave) CBulkOnlyTransport(aNumDrives,
|
0
|
79 |
aController,
|
|
80 |
aTestParser);
|
300
|
81 |
CleanupStack::PushL(self);
|
|
82 |
self->ConstructL();
|
|
83 |
CleanupStack::Pop(self);
|
|
84 |
return self;
|
|
85 |
}
|
0
|
86 |
#endif
|
|
87 |
|
|
88 |
|
|
89 |
/**
|
|
90 |
c'tor
|
|
91 |
@param aNumDrives - The number of drives available for MS
|
|
92 |
@param aController - reference to the parent
|
|
93 |
*/
|
|
94 |
CBulkOnlyTransport::CBulkOnlyTransport(TInt aNumDrives,
|
|
95 |
CUsbMassStorageController& aController):
|
300
|
96 |
CActive(EPriorityStandard),
|
|
97 |
iMaxLun(aNumDrives-1),
|
|
98 |
iController(aController),
|
|
99 |
iStallAllowed(ETrue)
|
|
100 |
{
|
|
101 |
}
|
0
|
102 |
|
|
103 |
#ifdef MSDC_TESTMODE
|
|
104 |
CBulkOnlyTransport::CBulkOnlyTransport(TInt aNumDrives,
|
|
105 |
CUsbMassStorageController& aController,
|
|
106 |
TTestParser* aTestParser)
|
|
107 |
: CActive(EPriorityStandard),
|
300
|
108 |
iMaxLun(aNumDrives-1),
|
|
109 |
iController(aController),
|
|
110 |
iStallAllowed(ETrue),
|
0
|
111 |
iTestParser(aTestParser)
|
300
|
112 |
{
|
|
113 |
}
|
0
|
114 |
#endif
|
|
115 |
|
|
116 |
|
|
117 |
/**
|
|
118 |
Constructs the CBulkOnlyTranspor object
|
|
119 |
*/
|
|
120 |
void CBulkOnlyTransport::ConstructL()
|
300
|
121 |
{
|
|
122 |
iBotControlInterface = CBotControlInterface::NewL(*this);
|
|
123 |
iDeviceStateNotifier = CActiveDeviceStateNotifier::NewL(*this);
|
|
124 |
CActiveScheduler::Add(this);
|
|
125 |
}
|
0
|
126 |
|
|
127 |
|
|
128 |
/**
|
|
129 |
Destructor
|
|
130 |
*/
|
|
131 |
CBulkOnlyTransport::~CBulkOnlyTransport()
|
300
|
132 |
{
|
|
133 |
if (iInterfaceConfigured)
|
|
134 |
{
|
|
135 |
Stop();
|
|
136 |
}
|
|
137 |
delete iBotControlInterface;
|
|
138 |
delete iDeviceStateNotifier;
|
|
139 |
}
|
0
|
140 |
|
|
141 |
|
|
142 |
/**
|
|
143 |
Set or unset configuration descriptor for USB MassStorage Bulk Only transport
|
|
144 |
|
|
145 |
@param aUnset indicate whether set or unset descriptor
|
|
146 |
@return KErrNone if operation was completed successfully, errorcode otherwise
|
|
147 |
*/
|
|
148 |
TInt CBulkOnlyTransport::SetupConfigurationDescriptor(TBool aUnset)
|
300
|
149 |
{
|
|
150 |
TInt ret(KErrNone);
|
|
151 |
TInt configDescriptorSize(0);
|
0
|
152 |
|
300
|
153 |
iLdd.GetConfigurationDescriptorSize(configDescriptorSize);
|
|
154 |
if (static_cast<TUint>(configDescriptorSize) != KUsbDescSize_Config)
|
|
155 |
{
|
|
156 |
return KErrCorrupt;
|
|
157 |
}
|
0
|
158 |
|
300
|
159 |
TBuf8<KUsbDescSize_Config> configDescriptor;
|
|
160 |
ret = iLdd.GetConfigurationDescriptor(configDescriptor);
|
|
161 |
if (ret != KErrNone)
|
|
162 |
{
|
|
163 |
return ret;
|
|
164 |
}
|
0
|
165 |
|
300
|
166 |
// I beleive that other fields setted up during LDD initialisation
|
|
167 |
if (aUnset)
|
|
168 |
{
|
|
169 |
--configDescriptor[KUsbNumInterfacesOffset];
|
|
170 |
}
|
|
171 |
else
|
|
172 |
{
|
|
173 |
++configDescriptor[KUsbNumInterfacesOffset];
|
|
174 |
}
|
|
175 |
ret = iLdd.SetConfigurationDescriptor(configDescriptor);
|
0
|
176 |
|
300
|
177 |
return ret;
|
|
178 |
}
|
0
|
179 |
|
|
180 |
|
|
181 |
/**
|
|
182 |
Set up interface descriptor
|
|
183 |
|
|
184 |
@return KErrNone if operation was completed successfully, errorcode otherwise
|
|
185 |
*/
|
|
186 |
TInt CBulkOnlyTransport::SetupInterfaceDescriptors()
|
300
|
187 |
{
|
|
188 |
// Device caps
|
|
189 |
TUsbDeviceCaps d_caps;
|
|
190 |
TInt ret = iLdd.DeviceCaps(d_caps);
|
|
191 |
if (ret != KErrNone)
|
|
192 |
{
|
|
193 |
return ret;
|
|
194 |
}
|
|
195 |
TInt totalEndpoints = d_caps().iTotalEndpoints;
|
|
196 |
if (totalEndpoints < KRequiredNumberOfEndpoints)
|
|
197 |
{
|
|
198 |
return KErrHardwareNotAvailable;
|
|
199 |
}
|
0
|
200 |
|
300
|
201 |
// Endpoint caps
|
|
202 |
TUsbcEndpointData data[KUsbcMaxEndpoints];
|
|
203 |
TPtr8 dataptr(reinterpret_cast<TUint8*>(data), sizeof(data), sizeof(data));
|
|
204 |
ret = iLdd.EndpointCaps(dataptr);
|
|
205 |
if (ret != KErrNone)
|
|
206 |
{
|
|
207 |
return ret;
|
|
208 |
}
|
0
|
209 |
|
300
|
210 |
// Set the active interface
|
|
211 |
TUsbcInterfaceInfoBuf ifc;
|
|
212 |
TInt ep_found = 0;
|
|
213 |
TBool foundBulkIN = EFalse;
|
|
214 |
TBool foundBulkOUT = EFalse;
|
0
|
215 |
|
300
|
216 |
for (TInt i = 0; i < totalEndpoints ; i++)
|
|
217 |
{
|
|
218 |
const TUsbcEndpointCaps* caps = &data[i].iCaps;
|
|
219 |
const TInt maxPacketSize = caps->MaxPacketSize();
|
|
220 |
if (!foundBulkIN &&
|
|
221 |
(caps->iTypesAndDir & (KUsbEpTypeBulk | KUsbEpDirIn)) == (KUsbEpTypeBulk | KUsbEpDirIn))
|
|
222 |
{
|
|
223 |
// KInEndpoint is going to be our TX (IN, write) endpoint
|
|
224 |
ifc().iEndpointData[0].iType = KUsbEpTypeBulk;
|
|
225 |
if((d_caps().iFeatureWord1 & KUsbDevCapsFeatureWord1_EndpointResourceAllocV2) == KUsbDevCapsFeatureWord1_EndpointResourceAllocV2)
|
|
226 |
ifc().iEndpointData[0].iFeatureWord1 = KUsbcEndpointInfoFeatureWord1_DMA|KUsbcEndpointInfoFeatureWord1_DoubleBuffering;
|
|
227 |
ifc().iEndpointData[0].iDir = KUsbEpDirIn;
|
|
228 |
ifc().iEndpointData[0].iSize = maxPacketSize;
|
|
229 |
ifc().iEndpointData[0].iInterval_Hs = 0;
|
|
230 |
foundBulkIN = ETrue;
|
|
231 |
if (++ep_found == KRequiredNumberOfEndpoints)
|
|
232 |
{
|
|
233 |
break;
|
|
234 |
}
|
|
235 |
continue;
|
|
236 |
}
|
|
237 |
if (!foundBulkOUT &&
|
|
238 |
(caps->iTypesAndDir & (KUsbEpTypeBulk | KUsbEpDirOut)) == (KUsbEpTypeBulk | KUsbEpDirOut))
|
|
239 |
{
|
|
240 |
// KOutEndpoint is going to be our RX (OUT, read) endpoint
|
|
241 |
ifc().iEndpointData[1].iType = KUsbEpTypeBulk;
|
|
242 |
if((d_caps().iFeatureWord1 & KUsbDevCapsFeatureWord1_EndpointResourceAllocV2) == KUsbDevCapsFeatureWord1_EndpointResourceAllocV2)
|
|
243 |
ifc().iEndpointData[1].iFeatureWord1 = KUsbcEndpointInfoFeatureWord1_DMA|KUsbcEndpointInfoFeatureWord1_DoubleBuffering;
|
|
244 |
ifc().iEndpointData[1].iDir = KUsbEpDirOut;
|
|
245 |
ifc().iEndpointData[1].iSize = maxPacketSize;
|
|
246 |
ifc().iEndpointData[1].iInterval_Hs = 0;
|
|
247 |
foundBulkOUT = ETrue;
|
|
248 |
if (++ep_found == KRequiredNumberOfEndpoints)
|
|
249 |
{
|
|
250 |
break;
|
|
251 |
}
|
|
252 |
continue;
|
|
253 |
}
|
|
254 |
}
|
|
255 |
if (ep_found != KRequiredNumberOfEndpoints)
|
|
256 |
{
|
|
257 |
return KErrHardwareNotAvailable;
|
|
258 |
}
|
0
|
259 |
|
300
|
260 |
_LIT16(string, "USB Mass Storage Interface");
|
|
261 |
ifc().iString = const_cast<TDesC16*>(&string);
|
|
262 |
ifc().iTotalEndpointsUsed = KRequiredNumberOfEndpoints;
|
|
263 |
ifc().iClass.iClassNum = 0x08; // Mass Storage
|
|
264 |
ifc().iClass.iSubClassNum = 0x06; // SCSI Transparent Command Set
|
|
265 |
ifc().iClass.iProtocolNum = 0x50; // Bulk Only Transport
|
0
|
266 |
|
300
|
267 |
TUint bandwidth_priority = (EUsbcBandwidthOUTDefault | EUsbcBandwidthINDefault);
|
|
268 |
if (d_caps().iHighSpeed)
|
|
269 |
{
|
|
270 |
// If this device supports USB High-speed, then we request 64KB buffers
|
|
271 |
// (otherwise the default 4KB ones will do).
|
|
272 |
bandwidth_priority = (EUsbcBandwidthOUTPlus2 | EUsbcBandwidthINPlus2);
|
|
273 |
// Also, tell the Protocol about it, because it might want to do some
|
|
274 |
// optimizing too.
|
|
275 |
iProtocol->ReportHighSpeedDevice();
|
|
276 |
}
|
|
277 |
ret = iLdd.SetInterface(0, ifc, bandwidth_priority);
|
|
278 |
return ret;
|
|
279 |
}
|
0
|
280 |
|
|
281 |
|
|
282 |
/**
|
|
283 |
Called by the protocol after processing the packet to indicate that more data is required.
|
|
284 |
|
|
285 |
@param aData reference to the data buffer.
|
|
286 |
*/
|
|
287 |
void CBulkOnlyTransport::SetupDataOut(TPtr8& aData)
|
|
288 |
{
|
|
289 |
iDataTransferMan.SetModeDataOut(aData);
|
|
290 |
}
|
|
291 |
|
|
292 |
/**
|
|
293 |
Called by the protocol after processing the packet to indicate that data should be written to the host.
|
|
294 |
|
|
295 |
@param aData reference to the data buffer.
|
|
296 |
*/
|
|
297 |
void CBulkOnlyTransport::SetupDataIn(TPtrC8& aData)
|
300
|
298 |
{
|
0
|
299 |
iDataTransferMan.SetModeDataIn(aData);
|
300
|
300 |
}
|
0
|
301 |
|
|
302 |
|
|
303 |
TInt CBulkOnlyTransport::Start()
|
300
|
304 |
{
|
|
305 |
TInt err = KErrNone;
|
0
|
306 |
|
300
|
307 |
if (!iProtocol)
|
|
308 |
{
|
|
309 |
return KErrBadHandle; //protocol should be set up before start
|
|
310 |
}
|
0
|
311 |
|
300
|
312 |
if (IsActive())
|
|
313 |
{
|
|
314 |
__PRINT(_L("CBulkOnlyTransport::Start - active before start!\n"));
|
|
315 |
return KErrInUse;
|
|
316 |
}
|
0
|
317 |
|
300
|
318 |
if ((err = iLdd.Open(0)) != KErrNone ||
|
|
319 |
(err = SetupConfigurationDescriptor()) != KErrNone ||
|
|
320 |
(err = SetupInterfaceDescriptors()) != KErrNone )
|
|
321 |
{
|
|
322 |
__PRINT(_L("CBulkOnlyTransport::Start - Error during descriptors setup!\n"));
|
|
323 |
return err;
|
|
324 |
}
|
0
|
325 |
|
300
|
326 |
iDeviceStateNotifier->Activate(); // activate notifier wich will wait until USB became configured
|
|
327 |
TUsbcDeviceState deviceStatus = EUsbcDeviceStateDefault;
|
|
328 |
err = iLdd.DeviceStatus(deviceStatus);
|
|
329 |
__PRINT1(_L("CBulkOnlyTransport::Start - Device status = %d\n"), deviceStatus);
|
|
330 |
if (err == KErrNone && deviceStatus == EUsbcDeviceStateConfigured)
|
|
331 |
{
|
|
332 |
__PRINT(_L("CBulkOnlyTransport::Start - Starting bulk only transport\n"));
|
|
333 |
err = HwStart();
|
|
334 |
}
|
|
335 |
iInterfaceConfigured = ETrue;
|
|
336 |
return err;
|
|
337 |
}
|
0
|
338 |
|
|
339 |
TInt CBulkOnlyTransport::HwStart(TBool aDiscard)
|
300
|
340 |
{
|
0
|
341 |
TInt lun = MaxLun();
|
|
342 |
do
|
|
343 |
{
|
|
344 |
Controller().DriveManager().Connect(lun);
|
|
345 |
}
|
|
346 |
while(--lun >= 0);
|
|
347 |
|
300
|
348 |
TInt res = iBotControlInterface->Start() ;
|
0
|
349 |
|
300
|
350 |
iCurrentState = ENone;
|
0
|
351 |
iDataTransferMan.Init();
|
300
|
352 |
iStarted = ETrue;
|
0
|
353 |
|
300
|
354 |
TUsbDeviceCaps d_caps;
|
|
355 |
TInt ret = iLdd.DeviceCaps(d_caps);
|
|
356 |
if (ret == KErrNone)
|
|
357 |
{
|
|
358 |
if((d_caps().iFeatureWord1 & KUsbDevCapsFeatureWord1_EndpointResourceAllocV2) != KUsbDevCapsFeatureWord1_EndpointResourceAllocV2)
|
|
359 |
{
|
|
360 |
// Set up DMA if possible (errors are non-critical)
|
|
361 |
TInt err = iLdd.AllocateEndpointResource(KOutEndpoint, EUsbcEndpointResourceDMA);
|
|
362 |
if (err != KErrNone)
|
|
363 |
{
|
|
364 |
__PRINT1(_L("Set DMA on OUT endpoint failed with error code: %d"), err);
|
|
365 |
}
|
|
366 |
err = iLdd.AllocateEndpointResource(KInEndpoint, EUsbcEndpointResourceDMA);
|
|
367 |
if (err != KErrNone)
|
|
368 |
{
|
|
369 |
__PRINT1(_L("Set DMA on IN endpoint failed with error code: %d"), err);
|
|
370 |
}
|
0
|
371 |
|
300
|
372 |
// Set up Double Buffering if possible (errors are non-critical)
|
|
373 |
err = iLdd.AllocateEndpointResource(KOutEndpoint, EUsbcEndpointResourceDoubleBuffering);
|
|
374 |
if (err != KErrNone)
|
|
375 |
{
|
|
376 |
__PRINT1(_L("Set Double Buffering on OUT endpoint failed with error code: %d"), err);
|
|
377 |
}
|
|
378 |
err = iLdd.AllocateEndpointResource(KInEndpoint, EUsbcEndpointResourceDoubleBuffering);
|
|
379 |
if (err != KErrNone)
|
|
380 |
{
|
|
381 |
__PRINT1(_L("Set Double Buffering on IN endpoint failed with error code: %d"), err);
|
|
382 |
}
|
|
383 |
}
|
|
384 |
}
|
0
|
385 |
|
|
386 |
if (aDiscard)
|
300
|
387 |
{
|
|
388 |
TInt bytes;
|
|
389 |
const TInt err = iLdd.QueryReceiveBuffer(KOutEndpoint, bytes);
|
|
390 |
if (err != KErrNone || bytes <= 0)
|
|
391 |
{
|
|
392 |
__PRINT1(_L("Error: err=%d bytes=%d"), bytes);
|
|
393 |
}
|
|
394 |
else
|
|
395 |
{
|
|
396 |
__PRINT1(_L("RxBuffer has %d bytes"), bytes);
|
|
397 |
FlushDataOut(bytes);
|
|
398 |
}
|
|
399 |
}
|
0
|
400 |
|
300
|
401 |
ClientReadCbw();
|
|
402 |
return res;
|
|
403 |
}
|
0
|
404 |
|
|
405 |
|
|
406 |
void CBulkOnlyTransport::HwStop()
|
300
|
407 |
{
|
|
408 |
if (iStarted)
|
|
409 |
{
|
0
|
410 |
iController.DriveManager().Disconnect();
|
300
|
411 |
Cancel();
|
|
412 |
iBotControlInterface->Cancel();
|
|
413 |
iProtocol->Cancel();
|
|
414 |
iStarted = EFalse;
|
|
415 |
}
|
|
416 |
}
|
0
|
417 |
|
|
418 |
|
|
419 |
void CBulkOnlyTransport::HwSuspend()
|
300
|
420 |
{
|
0
|
421 |
iController.DriveManager().Disconnect();
|
300
|
422 |
}
|
0
|
423 |
|
|
424 |
|
|
425 |
void CBulkOnlyTransport::HwResume()
|
300
|
426 |
{
|
0
|
427 |
iController.DriveManager().Connect();
|
300
|
428 |
}
|
0
|
429 |
|
|
430 |
|
|
431 |
/**
|
|
432 |
Stops the Bulk Only Transport
|
|
433 |
*/
|
|
434 |
TInt CBulkOnlyTransport::Stop()
|
300
|
435 |
{
|
|
436 |
iBotControlInterface->Cancel();
|
|
437 |
iDeviceStateNotifier->Cancel();
|
|
438 |
Cancel();
|
|
439 |
if (iInterfaceConfigured)
|
|
440 |
{
|
|
441 |
iLdd.ReleaseInterface(0);
|
|
442 |
SetupConfigurationDescriptor(ETrue);
|
|
443 |
iLdd.Close();
|
|
444 |
}
|
|
445 |
iCurrentState = ENone;
|
|
446 |
iInterfaceConfigured = EFalse;
|
0
|
447 |
|
300
|
448 |
return KErrNone;
|
|
449 |
}
|
0
|
450 |
|
|
451 |
|
|
452 |
/**
|
|
453 |
Read aLength bytes of data from the host into the read buffer.
|
|
454 |
@param aLength The number of bytes to read from the host.
|
|
455 |
*/
|
|
456 |
void CBulkOnlyTransport::ClientReadCbw()
|
300
|
457 |
{
|
|
458 |
if (IsActive())
|
|
459 |
{
|
|
460 |
__PRINT(_L("Still active\n"));
|
|
461 |
__ASSERT_DEBUG(EFalse, User::Panic(KUsbMsClientPanicCat, EMsBulkOnlyStillActive));
|
|
462 |
return;
|
|
463 |
}
|
0
|
464 |
|
|
465 |
ReadCbw();
|
|
466 |
}
|
|
467 |
|
|
468 |
|
|
469 |
void CBulkOnlyTransport::ReadCbw()
|
300
|
470 |
{
|
|
471 |
iCbwBuf.SetMax();
|
|
472 |
iLdd.ReadUntilShort(iStatus, KOutEndpoint, iCbwBuf, iCbwBuf.Length());
|
|
473 |
iCurrentState = EWaitForCBW;
|
|
474 |
SetActive();
|
|
475 |
}
|
0
|
476 |
|
|
477 |
|
|
478 |
void CBulkOnlyTransport::DoCancel()
|
300
|
479 |
{
|
|
480 |
iLdd.WriteCancel(KInEndpoint);
|
|
481 |
iLdd.ReadCancel(KOutEndpoint);
|
|
482 |
}
|
0
|
483 |
|
|
484 |
|
|
485 |
void CBulkOnlyTransport::Activate(TInt aReason)
|
|
486 |
{
|
|
487 |
SetActive();
|
|
488 |
TRequestStatus* r = &iStatus;
|
|
489 |
User::RequestComplete(r, aReason);
|
|
490 |
}
|
|
491 |
|
|
492 |
|
|
493 |
void CBulkOnlyTransport::RunL()
|
300
|
494 |
{
|
|
495 |
if (iStatus != KErrNone)
|
|
496 |
{
|
|
497 |
__PRINT1(_L("Error %d in RunL, halt endpoints \n"), iStatus.Int());
|
|
498 |
SetPermError(); //halt endpoints for reset recovery
|
|
499 |
return;
|
|
500 |
}
|
0
|
501 |
|
300
|
502 |
switch (iCurrentState)
|
|
503 |
{
|
0
|
504 |
case EWaitForCBW:
|
|
505 |
__PRINT(_L("EWaitForCBW"));
|
|
506 |
TRAPD(err, DecodeCbwL());
|
|
507 |
if (err)
|
|
508 |
{
|
|
509 |
// CBW not valid or meaningful
|
|
510 |
// Specification says: "If the CBW is not valid, the device
|
|
511 |
// shall STALL the Bulk-In pipe. Also, the device shall either
|
|
512 |
// STALL the Bulk-Out pipe, or the device shall accept and
|
|
513 |
// discard any Bulk-Out data. The device shall maintain this
|
|
514 |
// state until a Reset Recovery." Here we keep bulk-in ep
|
|
515 |
// stalled and ignore bulk-out ep.
|
|
516 |
SetPermError();
|
|
517 |
return;
|
|
518 |
}
|
300
|
519 |
break;
|
0
|
520 |
|
|
521 |
case EHandleDataIn:
|
|
522 |
{
|
|
523 |
__PRINT(_L("EHandleDataIn"));
|
|
524 |
iDataTransferMan.SetModeNoData();
|
|
525 |
|
300
|
526 |
if (iDataTransferMan.iDataResidue && iStallAllowed)
|
|
527 |
{
|
|
528 |
StallEndpointAndWaitForClear(KInEndpoint);
|
|
529 |
}
|
0
|
530 |
#ifdef MSDC_TESTMODE
|
|
531 |
if (iTestParser && iTestParser->Enabled())
|
|
532 |
{
|
|
533 |
if (iTestParser->TestCase() == TTestParser::ETestCaseDiStallCsw)
|
|
534 |
{
|
|
535 |
iTestParser->ClrTestCase();
|
|
536 |
__TESTMODEPRINT1("CBW Tag=0x%x: Stalling CSW Data-In", iCbwTag);
|
|
537 |
StallEndpointAndWaitForClear(KInEndpoint);
|
|
538 |
}
|
|
539 |
}
|
|
540 |
#endif
|
300
|
541 |
SendCsw(iCbwTag, iDataTransferMan.iDataResidue, iCmdStatus);
|
0
|
542 |
}
|
300
|
543 |
break;
|
0
|
544 |
|
300
|
545 |
case EHandleDataOut:
|
|
546 |
{
|
0
|
547 |
TUint bytesWritten;
|
|
548 |
TInt ret = iProtocol->MediaWritePacket(bytesWritten);
|
|
549 |
iDataTransferMan.iDataResidue -= bytesWritten;
|
|
550 |
|
300
|
551 |
switch(ret)
|
0
|
552 |
{
|
|
553 |
case KErrCompletion:
|
|
554 |
{
|
|
555 |
// The protocol has indicated with KErrCompletion that
|
|
556 |
// sufficient data is available in the buffer to process the
|
|
557 |
// transfer immediately.
|
|
558 |
|
|
559 |
TInt deviceDataLength = iDataTransferMan.iReadBuf.Length();
|
|
560 |
iLdd.Read(iStatus, KOutEndpoint, iDataTransferMan.iReadBuf, deviceDataLength);
|
|
561 |
SetActive();
|
|
562 |
}
|
|
563 |
break;
|
|
564 |
case KErrNotReady:
|
|
565 |
{
|
|
566 |
// The protocol has indicated with KErrNotReady that
|
|
567 |
// insufficient data is available in the buffer, so should wait
|
|
568 |
// for it to arrive.
|
|
569 |
TInt deviceDataLength = iDataTransferMan.iReadBuf.Length();
|
|
570 |
iLdd.Read(iStatus, KOutEndpoint, iDataTransferMan.iReadBuf, deviceDataLength);
|
|
571 |
SetActive();
|
|
572 |
}
|
|
573 |
break;
|
|
574 |
case KErrAbort:
|
|
575 |
{
|
|
576 |
iDataTransferMan.SetModeNoData();
|
|
577 |
iCmdStatus = TBotCsw::ECommandFailed;
|
|
578 |
if (iDataTransferMan.iDataResidue)
|
|
579 |
{
|
|
580 |
__PRINT(_L("Discarding residue"));
|
|
581 |
FlushDataOut(iDataTransferMan.iTransportResidue);
|
|
582 |
}
|
|
583 |
SendCsw(iCbwTag, iDataTransferMan.iDataResidue, iCmdStatus);
|
|
584 |
}
|
|
585 |
break;
|
|
586 |
|
|
587 |
case KErrNone:
|
|
588 |
default:
|
|
589 |
{
|
|
590 |
// The protocol has indicated that transfer is complete, so
|
|
591 |
// send the CSW response to the host.
|
|
592 |
iDataTransferMan.SetModeNoData();
|
|
593 |
#ifdef MSDC_TESTMODE
|
|
594 |
if (iDataTransferMan.iDataResidue)
|
|
595 |
{
|
|
596 |
TBool done = EFalse;
|
|
597 |
if (iTestParser && iTestParser->Enabled())
|
|
598 |
{
|
|
599 |
TInt testCase = iTestParser->TestCase();
|
|
600 |
if (testCase == TTestParser::ETestCaseDoStallCsw)
|
|
601 |
{
|
|
602 |
iTestParser->ClrTestCase();
|
|
603 |
__TESTMODEPRINT1("CBW Tag=0x%x: Stalling CSW Data-Out", iCbwTag);
|
|
604 |
StallEndpointAndWaitForClear(KInEndpoint);
|
|
605 |
done = ETrue;
|
|
606 |
}
|
|
607 |
}
|
|
608 |
if (!done)
|
|
609 |
{
|
|
610 |
__PRINT(_L("Discarding residue"));
|
|
611 |
// we have to read as much data as available that host PC
|
|
612 |
// sends; otherwise, bulk-out endpoint will need to keep
|
|
613 |
// sending NAK back.
|
|
614 |
FlushDataOut(iDataTransferMan.iDataResidue);
|
|
615 |
__TESTMODEPRINT2("CBW Tag=0x%x: Residue = x%x",
|
|
616 |
iCbwTag, iDataTransferMan.iDataResidue);
|
|
617 |
}
|
|
618 |
}
|
|
619 |
else
|
|
620 |
{
|
|
621 |
if (iTestParser && iTestParser->Enabled())
|
|
622 |
{
|
|
623 |
TInt testCase = iTestParser->TestCase();
|
|
624 |
if (testCase == TTestParser::ETestCaseDoStallCsw)
|
|
625 |
{
|
|
626 |
iTestParser->ClrTestCase();
|
|
627 |
__TESTMODEPRINT1("CBW Tag=0x%x: Stalling CSW Data-Out", iCbwTag);
|
|
628 |
StallEndpointAndWaitForClear(KInEndpoint);
|
|
629 |
}
|
|
630 |
}
|
|
631 |
}
|
|
632 |
|
|
633 |
#else
|
|
634 |
if (iDataTransferMan.iDataResidue)
|
|
635 |
{
|
|
636 |
__PRINT(_L("Discarding residue"));
|
|
637 |
// we have to read as much data as available that host PC
|
|
638 |
// sends; otherwise, bulk-out endpoint will need to keep
|
|
639 |
// sending NAK back.
|
|
640 |
FlushDataOut(iDataTransferMan.iDataResidue);
|
|
641 |
}
|
|
642 |
#endif
|
|
643 |
SendCsw(iCbwTag, iDataTransferMan.iDataResidue, iCmdStatus);
|
|
644 |
}
|
|
645 |
break; // KErrNone
|
|
646 |
} // switch
|
|
647 |
}
|
300
|
648 |
break; // EReadingData
|
0
|
649 |
|
300
|
650 |
case ESendingCSW:
|
|
651 |
__PRINT(_L("ESendingCSW"));
|
|
652 |
ReadCbw();
|
|
653 |
break;
|
0
|
654 |
|
|
655 |
case EPermErr:
|
300
|
656 |
__PRINT(_L("EPermErr"));
|
|
657 |
StallEndpointAndWaitForClear(KInEndpoint);
|
0
|
658 |
break;
|
|
659 |
|
|
660 |
default:
|
300
|
661 |
SetPermError(); // unexpected state
|
|
662 |
}
|
|
663 |
}
|
0
|
664 |
|
|
665 |
|
|
666 |
/**
|
|
667 |
Decode the CBW received from the host via KOutEndpoint
|
|
668 |
|
|
669 |
- If the header is valid, the data content is passed to the parser.
|
|
670 |
- Depending on the command, more data may be transmitted/received.
|
|
671 |
- ...or the CSW is sent (if not a data command).
|
|
672 |
|
|
673 |
*/
|
|
674 |
void CBulkOnlyTransport::DecodeCbwL()
|
300
|
675 |
{
|
0
|
676 |
TBotServerReq req;
|
|
677 |
req.DecodeL(iCbwBuf);
|
|
678 |
|
|
679 |
if (!(req.IsValidCbw() && req.IsMeaningfulCbw(iMaxLun)))
|
|
680 |
{
|
|
681 |
User::Leave(KErrGeneral);
|
|
682 |
}
|
|
683 |
|
|
684 |
TPtrC8 aData(&iCbwBuf[TBotCbw::KCbwCbOffset], TBotCbw::KMaxCbwcbLength);
|
300
|
685 |
//aData.Set(&iCbwBuf[TBotCbw::KCbwCbOffset], TBotCbw::KMaxCbwcbLength);
|
0
|
686 |
TUint8 lun = req.Lun();
|
|
687 |
|
300
|
688 |
iCbwTag = req.Tag();
|
0
|
689 |
TUint32 hostDataLength = req.DataTransferLength();
|
|
690 |
iDataTransferMan.SetHostDataLen(hostDataLength);
|
|
691 |
|
|
692 |
TBotServerReq::TCbwDirection dataToHost = req.Direction();
|
|
693 |
|
|
694 |
__PRINT4(_L("lun =%d, hostDataLength=%d, CBWtag = 0x%X\n, dataToHost=%d\n"),
|
|
695 |
lun, hostDataLength, iCbwTag, dataToHost);
|
|
696 |
|
300
|
697 |
//////////////////////////////////////////////
|
|
698 |
TBool ret = iProtocol->DecodePacket(aData, lun);
|
|
699 |
//////////////////////////////////////////////
|
0
|
700 |
|
300
|
701 |
iStallAllowed = ETrue;
|
0
|
702 |
|
300
|
703 |
if (!ret)
|
|
704 |
{
|
|
705 |
__PRINT(_L("Command Failed\n"));
|
|
706 |
iCmdStatus = TBotCsw::ECommandFailed;
|
|
707 |
}
|
|
708 |
else
|
|
709 |
{
|
|
710 |
__PRINT(_L("Command Passed\n"));
|
|
711 |
iCmdStatus = TBotCsw::ECommandPassed;
|
|
712 |
}
|
0
|
713 |
|
300
|
714 |
if (hostDataLength) // Host expected data transfer
|
|
715 |
{
|
|
716 |
if (dataToHost == TBotServerReq::EDataIn) // send data to host
|
|
717 |
{
|
0
|
718 |
if (!iDataTransferMan.IsModeDataIn())
|
300
|
719 |
{
|
|
720 |
__PRINT(_L("Write buffer was not setup\n"));
|
0
|
721 |
|
300
|
722 |
if (hostDataLength <= KBOTMaxBufSize)
|
|
723 |
{
|
|
724 |
// Case 4 or 8"
|
|
725 |
iBuf.FillZ(hostDataLength);
|
|
726 |
iLdd.Write(iStatus, KInEndpoint, iBuf, hostDataLength);
|
|
727 |
SetActive();
|
|
728 |
iCurrentState = EHandleDataIn;
|
|
729 |
iStallAllowed = EFalse;
|
|
730 |
if (iDataTransferMan.IsModeDataOut())
|
|
731 |
{
|
0
|
732 |
//read buffer WAS set up - case (8)
|
300
|
733 |
iCmdStatus = TBotCsw::EPhaseError;
|
|
734 |
}
|
|
735 |
return;
|
|
736 |
}
|
|
737 |
else
|
|
738 |
{
|
0
|
739 |
// Use next block instead of StallEndpointAndWaitForClear(KInEndpoint);
|
|
740 |
FlushDataIn(hostDataLength);
|
300
|
741 |
}
|
0
|
742 |
|
300
|
743 |
if (iDataTransferMan.IsModeDataOut())
|
|
744 |
{
|
0
|
745 |
//read buffer WAS set up - case (8)
|
300
|
746 |
SendCsw(iCbwTag, hostDataLength, TBotCsw::EPhaseError);
|
|
747 |
//don't care to reset any flag - should get reset recovery
|
|
748 |
}
|
|
749 |
else
|
|
750 |
{
|
0
|
751 |
// case (4)
|
300
|
752 |
SendCsw(iCbwTag, hostDataLength, iCmdStatus);
|
|
753 |
}
|
|
754 |
return;
|
|
755 |
}
|
0
|
756 |
|
|
757 |
//==================
|
300
|
758 |
TInt deviceDataLength = iDataTransferMan.iWriteBuf.Length();
|
0
|
759 |
iDataTransferMan.iDataResidue = hostDataLength - deviceDataLength;
|
|
760 |
|
|
761 |
#ifdef MSDC_TESTMODE
|
|
762 |
if (iTestParser && iTestParser->Enabled())
|
|
763 |
{
|
|
764 |
TInt testCase = iTestParser->TestCase();
|
|
765 |
if (testCase == TTestParser::ETestCaseDiResidue)
|
|
766 |
{
|
|
767 |
__TESTMODEPRINT2("DataIN (Data Residue) %x %x", iTestParser->TestCounter(), hostDataLength);
|
|
768 |
|
|
769 |
const TInt KTransferLength = 0x100;
|
|
770 |
const TInt KResidueA = KTransferLength/4 -10;
|
|
771 |
const TInt KResidueB = KTransferLength/4 - 10;
|
|
772 |
if (hostDataLength > KTransferLength)
|
|
773 |
{
|
|
774 |
TInt residue = 0;
|
|
775 |
TInt i = iTestParser->TestCounter();
|
|
776 |
switch (i)
|
|
777 |
{
|
|
778 |
case 0:
|
|
779 |
iTestParser->ClrTestCase();
|
|
780 |
break;
|
|
781 |
case 1:
|
|
782 |
residue = KResidueA;
|
|
783 |
break;
|
|
784 |
case 2:
|
|
785 |
residue = KResidueB;
|
|
786 |
break;
|
|
787 |
|
|
788 |
default:
|
|
789 |
break;
|
|
790 |
}
|
|
791 |
|
|
792 |
//deviceDataLength -= residue;
|
|
793 |
iDataTransferMan.iDataResidue = residue;
|
|
794 |
iStallAllowed = EFalse;
|
|
795 |
iTestParser->DecTestCounter();
|
|
796 |
|
|
797 |
__TESTMODEPRINT4("CBW Tag=0x%x: Setting Residue (Data-In) hdl=%x ddl=%x residue=%x",
|
|
798 |
iCbwTag, hostDataLength, deviceDataLength, residue);
|
|
799 |
}
|
|
800 |
}
|
|
801 |
}
|
|
802 |
#endif
|
300
|
803 |
if (deviceDataLength < hostDataLength && hostDataLength <= KBOTMaxBufSize)
|
|
804 |
{
|
0
|
805 |
// do not pad
|
|
806 |
// iStallAllowed = ETrue;
|
|
807 |
// __PRINT1(_L("iBuf.Length=%d\n"),iBuf.Length());
|
|
808 |
// iLdd.Write(iStatus, KInEndpoint, iBuf, deviceDataLength);
|
|
809 |
// SetActive();
|
|
810 |
//iCurrentState = EWritingData;
|
|
811 |
|
|
812 |
// pad
|
|
813 |
iBuf.Zero();
|
|
814 |
iBuf.Append(iDataTransferMan.iWriteBuf);
|
|
815 |
iBuf.SetLength(hostDataLength);
|
|
816 |
iStallAllowed = EFalse;
|
|
817 |
__PRINT1(_L("iBuf.Length=%d\n"),iBuf.Length());
|
|
818 |
iLdd.Write(iStatus, KInEndpoint, iBuf, hostDataLength);
|
|
819 |
SetActive();
|
|
820 |
iCurrentState = EHandleDataIn;
|
|
821 |
|
300
|
822 |
return;
|
|
823 |
}
|
0
|
824 |
|
300
|
825 |
if (deviceDataLength == hostDataLength)
|
|
826 |
{
|
0
|
827 |
//case (6)[==]
|
|
828 |
#ifdef MSDC_TESTMODE
|
|
829 |
if (iTestParser && iTestParser->Enabled())
|
|
830 |
{
|
|
831 |
if (iTestParser->TestCase() == TTestParser::ETestCaseDiStallData)
|
|
832 |
{
|
|
833 |
iTestParser->ClrTestCase();
|
|
834 |
__TESTMODEPRINT1("CBW Tag=0x%x: Stalling CSW Data-In", iCbwTag);
|
|
835 |
StallEndpointAndWaitForClear(KInEndpoint);
|
|
836 |
}
|
|
837 |
}
|
|
838 |
#endif
|
|
839 |
|
300
|
840 |
DataInWriteRequest(deviceDataLength);
|
|
841 |
return;
|
|
842 |
}
|
|
843 |
else if (deviceDataLength < hostDataLength)
|
|
844 |
{
|
0
|
845 |
//case (5)[<]
|
300
|
846 |
DataInWriteRequest(deviceDataLength, ETrue); // Send ZLP
|
|
847 |
return;
|
|
848 |
}
|
|
849 |
else
|
|
850 |
{
|
0
|
851 |
// deviceDataLength > hostDataLength - case (7)
|
300
|
852 |
iCmdStatus = TBotCsw::EPhaseError;
|
|
853 |
iDataTransferMan.iDataResidue = 0;
|
|
854 |
DataInWriteRequest(hostDataLength);
|
|
855 |
return;
|
|
856 |
}
|
|
857 |
}
|
|
858 |
else // read data from host
|
|
859 |
{
|
|
860 |
if (!iDataTransferMan.IsModeDataOut())
|
|
861 |
{
|
|
862 |
__PRINT(_L("Read buffer was not setup\n"));
|
0
|
863 |
|
|
864 |
// Use next block instead of StallEndpointAndWaitForClear(KOutEndpoint);
|
|
865 |
FlushDataOut(hostDataLength);
|
300
|
866 |
if (iDataTransferMan.IsModeDataIn())
|
|
867 |
{
|
0
|
868 |
//case (10)
|
300
|
869 |
SendCsw(iCbwTag, hostDataLength, TBotCsw::EPhaseError);
|
|
870 |
}
|
|
871 |
else
|
|
872 |
{
|
0
|
873 |
// case (9)
|
300
|
874 |
SendCsw(iCbwTag, hostDataLength, iCmdStatus);
|
|
875 |
}
|
|
876 |
return;
|
|
877 |
}
|
0
|
878 |
|
300
|
879 |
TInt deviceDataLength = iDataTransferMan.iReadBuf.Length();
|
0
|
880 |
|
300
|
881 |
if (deviceDataLength <= hostDataLength)
|
|
882 |
{
|
0
|
883 |
// case (11) and (12)
|
|
884 |
#ifdef MSDC_TESTMODE
|
|
885 |
TBool done = EFalse;
|
|
886 |
if (iTestParser && iTestParser->Enabled())
|
|
887 |
{
|
|
888 |
TInt testCase = iTestParser->TestCase();
|
|
889 |
|
|
890 |
if (testCase == TTestParser::ETestCaseDoStallData)
|
|
891 |
{
|
|
892 |
iTestParser->ClrTestCase();
|
|
893 |
// Stall Data Out
|
|
894 |
__TESTMODEPRINT2("CBW Tag=0x%x: Stalling Data (Data-Out) Residue = x%x",
|
|
895 |
iCbwTag, iDataTransferMan.iDataResidue);
|
|
896 |
StallEndpointAndWaitForClear(KOutEndpoint);
|
|
897 |
TInt bytes;
|
|
898 |
const TInt err = iLdd.QueryReceiveBuffer(KOutEndpoint, bytes);
|
|
899 |
__PRINT1(_L("Error: err=%d bytes=%d"), bytes);
|
|
900 |
FlushDataOut(bytes);
|
|
901 |
}
|
|
902 |
}
|
|
903 |
if (!done)
|
|
904 |
{
|
|
905 |
DataOutReadRequest(deviceDataLength);
|
|
906 |
}
|
|
907 |
#else
|
300
|
908 |
DataOutReadRequest(deviceDataLength);
|
0
|
909 |
#endif
|
300
|
910 |
return;
|
|
911 |
}
|
|
912 |
else
|
|
913 |
{
|
0
|
914 |
// case (13)
|
|
915 |
/**
|
|
916 |
* Comment following line in order to pass compliant test.
|
|
917 |
* As spec said in case 13:"The device may receive data up to a
|
|
918 |
* total of dCBWDataTransferLength."
|
|
919 |
* Here we choose to ignore incoming data.
|
|
920 |
*/
|
300
|
921 |
//StallEndpointAndWaitForClear(KOutEndpoint); //Stall Out endpoint
|
0
|
922 |
if (iDataTransferMan.IsModeDataOut())
|
|
923 |
{
|
|
924 |
iDataTransferMan.SetModeNoData();
|
|
925 |
iLdd.Read(iStatus, KOutEndpoint, iDataTransferMan.iReadBuf, hostDataLength);
|
|
926 |
User::WaitForRequest(iStatus);
|
|
927 |
iProtocol->MediaWriteAbort();
|
|
928 |
}
|
|
929 |
SendCsw(iCbwTag, hostDataLength, TBotCsw::EPhaseError);
|
300
|
930 |
return;
|
|
931 |
}
|
|
932 |
}
|
|
933 |
}
|
|
934 |
else // Host expected no data transfer
|
|
935 |
{
|
|
936 |
__PRINT(_L("No data transfer expected\n"));
|
|
937 |
iDataTransferMan.iDataResidue = 0;
|
|
938 |
if (!iDataTransferMan.IsModeNoData())
|
|
939 |
{
|
0
|
940 |
// case (2) and (3)
|
300
|
941 |
SendCsw(iCbwTag, 0, TBotCsw::EPhaseError);
|
|
942 |
}
|
|
943 |
else
|
|
944 |
{
|
0
|
945 |
//case (1)
|
|
946 |
#ifdef MSDC_TESTMODE
|
|
947 |
if (iTestParser && iTestParser->Enabled())
|
|
948 |
{
|
|
949 |
TInt testCase = iTestParser->TestCase();
|
|
950 |
if (testCase == TTestParser::ETestCaseNoDataStallCsw)
|
|
951 |
{
|
|
952 |
__TESTMODEPRINT1("CBW Tag=0x%x: Stalling CSW for Case 1 (Hn=Dn)", iCbwTag);
|
|
953 |
iTestParser->ClrTestCase();
|
|
954 |
StallEndpointAndWaitForClear(KInEndpoint);
|
|
955 |
}
|
|
956 |
|
|
957 |
if (testCase == TTestParser::ETestCaseNoDataPhaseError)
|
|
958 |
{
|
|
959 |
__TESTMODEPRINT1("CBW Tag=0x%x: Enabling phase error (no data)", iCbwTag);
|
|
960 |
iTestParser->SetPhaseError();
|
|
961 |
}
|
|
962 |
}
|
|
963 |
#endif
|
300
|
964 |
SendCsw(iCbwTag, 0, iCmdStatus);
|
|
965 |
}
|
|
966 |
}
|
|
967 |
}
|
0
|
968 |
|
|
969 |
|
|
970 |
/**
|
|
971 |
Initiate stalling of bulk IN endpoint.
|
|
972 |
Used when protocol wants to force host to initiate a reset recovery.
|
|
973 |
*/
|
|
974 |
void CBulkOnlyTransport::SetPermError()
|
300
|
975 |
{
|
0
|
976 |
iCurrentState = EPermErr;
|
|
977 |
Activate(KErrNone);
|
300
|
978 |
}
|
0
|
979 |
|
|
980 |
|
|
981 |
/**
|
|
982 |
Send data provided by protocol to the host
|
|
983 |
|
|
984 |
@param aLength amount of data (in bytes) to be send to host
|
|
985 |
*/
|
|
986 |
void CBulkOnlyTransport::DataInWriteRequest(TUint aLength, TBool aZlpRequired)
|
300
|
987 |
{
|
|
988 |
iLdd.Write(iStatus, KInEndpoint, iDataTransferMan.iWriteBuf, aLength, aZlpRequired);
|
0
|
989 |
iDataTransferMan.iTransportResidue -= aLength;
|
300
|
990 |
iCurrentState = EHandleDataIn;
|
|
991 |
SetActive();
|
|
992 |
}
|
0
|
993 |
|
|
994 |
|
|
995 |
/**
|
|
996 |
Request data form the host for the protocol
|
|
997 |
|
|
998 |
@param aLength amount of data (in bytes) to be received from the host
|
|
999 |
*/
|
|
1000 |
void CBulkOnlyTransport::DataOutReadRequest(TUint aLength)
|
300
|
1001 |
{
|
|
1002 |
iLdd.Read(iStatus, KOutEndpoint, iDataTransferMan.iReadBuf, aLength);
|
|
1003 |
iDataTransferMan.iTransportResidue -= aLength;
|
|
1004 |
iCurrentState = EHandleDataOut;
|
0
|
1005 |
SetActive();
|
300
|
1006 |
}
|
0
|
1007 |
|
|
1008 |
|
|
1009 |
/**
|
|
1010 |
Send Command Status Wrapper to the host
|
|
1011 |
|
|
1012 |
@param aTag Echo of Command Block Tag sent by the host.
|
|
1013 |
@param aDataResidue the difference between the amount of data expected by the
|
|
1014 |
host, and the actual amount of data processed by the device.
|
|
1015 |
@param aStatus indicates the success or failure of the command.
|
|
1016 |
*/
|
|
1017 |
void CBulkOnlyTransport::SendCsw(TUint aTag, TUint32 aDataResidue, TBotCsw::TCswStatus aStatus)
|
300
|
1018 |
{
|
|
1019 |
__PRINT2(_L("DataResidue = %d, Status = %d \n"), aDataResidue, aStatus);
|
0
|
1020 |
iCsw.SetLength(TBotCsw::KCswLength);
|
|
1021 |
TBotServerResp resp(iCbwTag, aDataResidue, aStatus);
|
|
1022 |
#ifdef MSDC_TESTMODE
|
|
1023 |
resp.EncodeL(iCsw, iTestParser);
|
|
1024 |
#else
|
|
1025 |
resp.EncodeL(iCsw);
|
|
1026 |
#endif
|
300
|
1027 |
iLdd.Write(iStatus, KInEndpoint, iCsw, TBotCsw::KCswLength);
|
|
1028 |
iCurrentState = ESendingCSW;
|
|
1029 |
SetActive();
|
|
1030 |
}
|
0
|
1031 |
|
|
1032 |
|
|
1033 |
/**
|
|
1034 |
Associates the transport with the protocol. Called during initialization of the controller.
|
|
1035 |
|
|
1036 |
@param aProtocol reference to the protocol
|
|
1037 |
*/
|
|
1038 |
void CBulkOnlyTransport::RegisterProtocol(MServerProtocol& aProtocol)
|
300
|
1039 |
{
|
|
1040 |
iProtocol = &aProtocol;
|
|
1041 |
}
|
0
|
1042 |
|
|
1043 |
|
|
1044 |
/**
|
|
1045 |
Used by CControlInterface
|
|
1046 |
|
|
1047 |
@return reference to the controller which instantiate the CBulkOnlyTransport
|
|
1048 |
*/
|
|
1049 |
CUsbMassStorageController& CBulkOnlyTransport::Controller()
|
300
|
1050 |
{
|
|
1051 |
return iController;
|
|
1052 |
}
|
0
|
1053 |
|
|
1054 |
|
|
1055 |
/**
|
|
1056 |
@return the number of logical units supported by the device.
|
|
1057 |
Logical Unit Numbers on the device shall be numbered contiguously starting from LUN
|
|
1058 |
0 to a maximum LUN of 15 (Fh).
|
|
1059 |
*/
|
|
1060 |
TInt CBulkOnlyTransport::MaxLun()
|
300
|
1061 |
{
|
|
1062 |
return iMaxLun;
|
|
1063 |
}
|
0
|
1064 |
|
|
1065 |
|
|
1066 |
/**
|
|
1067 |
Used by CControlInterface
|
|
1068 |
@return reference to USB logical driver
|
|
1069 |
*/
|
|
1070 |
RDevUsbcClient& CBulkOnlyTransport::Ldd()
|
300
|
1071 |
{
|
|
1072 |
return iLdd;
|
|
1073 |
}
|
0
|
1074 |
|
|
1075 |
|
|
1076 |
void CBulkOnlyTransport::StallEndpointAndWaitForClear(TEndpointNumber aEndpoint)
|
300
|
1077 |
{
|
|
1078 |
__ASSERT_DEBUG(aEndpoint != EEndpoint0, User::Panic(KUsbMsClientPanicCat, EMsWrongEndpoint));
|
0
|
1079 |
|
300
|
1080 |
// Now stall this endpoint
|
|
1081 |
__PRINT1(_L("Stalling endpoint %d"), aEndpoint);
|
|
1082 |
__TESTMODEPRINT2("CBW Tag=0x%x: Stalling endpoint %d", iCbwTag, aEndpoint);
|
|
1083 |
TInt r = iLdd.HaltEndpoint(aEndpoint);
|
|
1084 |
if (r != KErrNone)
|
|
1085 |
{
|
|
1086 |
__PRINT2(_L("Error: stalling ep %d failed: %d"), aEndpoint, r);
|
|
1087 |
}
|
|
1088 |
TEndpointState ep_state;
|
|
1089 |
TInt i = 0;
|
|
1090 |
do
|
|
1091 |
{
|
|
1092 |
// Wait for 10ms before checking the ep status
|
|
1093 |
User::After(10000);
|
0
|
1094 |
|
|
1095 |
if (aEndpoint == KInEndpoint)
|
|
1096 |
{
|
|
1097 |
// Discard BULK-OUT data
|
|
1098 |
TInt bytes;
|
|
1099 |
const TInt err = iLdd.QueryReceiveBuffer(KOutEndpoint, bytes);
|
|
1100 |
if (err != KErrNone || bytes <= 0)
|
|
1101 |
{
|
|
1102 |
__PRINT1(_L("Error: err=%d bytes=%d"), bytes);
|
|
1103 |
}
|
|
1104 |
else
|
|
1105 |
{
|
|
1106 |
__PRINT1(_L("RxBuffer has %d bytes"), bytes);
|
|
1107 |
FlushDataOut(bytes);
|
|
1108 |
}
|
|
1109 |
}
|
|
1110 |
|
300
|
1111 |
iLdd.EndpointStatus(aEndpoint, ep_state);
|
|
1112 |
if (++i >= 550)
|
|
1113 |
{
|
|
1114 |
// 5.5 secs should be enough (see 9.2.6.1 Request Processing Timing)
|
|
1115 |
__PRINT1(_L("Error: Checked for ep %d de-stall for 5.5s - giving up now"), aEndpoint);
|
0
|
1116 |
__TESTMODEPRINT1("Error: Checked for ep %d de-stall for 5.5s - giving up now", aEndpoint);
|
300
|
1117 |
// We can now only hope for a Reset Recovery
|
|
1118 |
return;
|
|
1119 |
}
|
|
1120 |
} while ((ep_state == EEndpointStateStalled) && iStarted);
|
|
1121 |
__PRINT2(_L("Checked for ep %d de-stall: %d time(s)"), aEndpoint, i);
|
0
|
1122 |
__TESTMODEPRINT2("Checked for ep %d de-stall: %d time(s)", aEndpoint, i);
|
300
|
1123 |
}
|
0
|
1124 |
|
|
1125 |
|
|
1126 |
|
|
1127 |
/**
|
|
1128 |
Called by the protocol to determine how many bytes of data are available in the read buffer.
|
|
1129 |
|
|
1130 |
@return The number of bytes available in the read buffer
|
|
1131 |
*/
|
|
1132 |
TInt CBulkOnlyTransport::BytesAvailable()
|
300
|
1133 |
{
|
|
1134 |
TInt bytes = 0;
|
|
1135 |
TInt err = iLdd.QueryReceiveBuffer(KOutEndpoint, bytes);
|
|
1136 |
if (err != KErrNone)
|
|
1137 |
bytes = 0;
|
|
1138 |
return bytes;
|
|
1139 |
}
|
0
|
1140 |
|
|
1141 |
|
|
1142 |
/**
|
|
1143 |
* Read out rest data from KOutEndpoint and discard them
|
|
1144 |
*/
|
|
1145 |
void CBulkOnlyTransport::FlushDataOut(TInt aLength)
|
300
|
1146 |
{
|
|
1147 |
iBuf.SetMax();
|
|
1148 |
TRequestStatus status;
|
|
1149 |
while (aLength > 0)
|
|
1150 |
{
|
|
1151 |
iLdd.ReadOneOrMore(status, KOutEndpoint, iBuf, iBuf.Length());
|
|
1152 |
User::WaitForRequest(status);
|
|
1153 |
TInt err = status.Int();
|
|
1154 |
if (err != KErrNone)
|
|
1155 |
{
|
|
1156 |
// Bad.
|
|
1157 |
break;
|
|
1158 |
}
|
|
1159 |
aLength -= iBuf.Length();
|
|
1160 |
}
|
|
1161 |
}
|
0
|
1162 |
|
|
1163 |
|
|
1164 |
void CBulkOnlyTransport::FlushDataIn(TInt aLength)
|
|
1165 |
{
|
300
|
1166 |
iBuf.SetMax();
|
|
1167 |
TInt c = 0;
|
0
|
1168 |
TInt len;
|
300
|
1169 |
TRequestStatus status;
|
|
1170 |
while (c < aLength)
|
|
1171 |
{
|
0
|
1172 |
len = aLength - c;
|
300
|
1173 |
if (len > KBOTMaxBufSize)
|
|
1174 |
{
|
|
1175 |
len = KBOTMaxBufSize;
|
|
1176 |
}
|
|
1177 |
iLdd.Write(status, KInEndpoint, iBuf, len);
|
|
1178 |
User::WaitForRequest(status);
|
|
1179 |
c += KBOTMaxBufSize;
|
|
1180 |
}
|
0
|
1181 |
}
|
|
1182 |
|
|
1183 |
//
|
|
1184 |
// --- class CActiveDeviceStateNotifier ---------------------------------------------------------
|
|
1185 |
//
|
|
1186 |
CActiveDeviceStateNotifier::CActiveDeviceStateNotifier(CBulkOnlyTransport& aParent)
|
|
1187 |
: CActive(EPriorityStandard),
|
300
|
1188 |
iParent(aParent),
|
|
1189 |
iDeviceState(EUsbcNoState),
|
|
1190 |
iOldDeviceState(EUsbcNoState)
|
|
1191 |
{
|
|
1192 |
}
|
0
|
1193 |
|
|
1194 |
|
|
1195 |
CActiveDeviceStateNotifier* CActiveDeviceStateNotifier::NewL(CBulkOnlyTransport& aParent)
|
300
|
1196 |
{
|
|
1197 |
CActiveDeviceStateNotifier* self = new (ELeave) CActiveDeviceStateNotifier(aParent);
|
|
1198 |
CleanupStack::PushL(self);
|
|
1199 |
self->ConstructL();
|
|
1200 |
CActiveScheduler::Add(self);
|
|
1201 |
CleanupStack::Pop(); // self
|
|
1202 |
return (self);
|
|
1203 |
}
|
0
|
1204 |
|
|
1205 |
|
|
1206 |
void CActiveDeviceStateNotifier::ConstructL()
|
300
|
1207 |
{
|
|
1208 |
}
|
0
|
1209 |
|
|
1210 |
|
|
1211 |
CActiveDeviceStateNotifier::~CActiveDeviceStateNotifier()
|
300
|
1212 |
{
|
|
1213 |
Cancel(); // base class
|
|
1214 |
}
|
0
|
1215 |
|
|
1216 |
|
|
1217 |
void CActiveDeviceStateNotifier::DoCancel()
|
|
1218 |
/**
|
|
1219 |
*
|
|
1220 |
*/
|
300
|
1221 |
{
|
|
1222 |
iParent.Ldd().AlternateDeviceStatusNotifyCancel();
|
|
1223 |
}
|
0
|
1224 |
|
|
1225 |
|
|
1226 |
void CActiveDeviceStateNotifier::RunL()
|
|
1227 |
/**
|
|
1228 |
*
|
|
1229 |
*/
|
300
|
1230 |
{
|
|
1231 |
// This displays the device state.
|
|
1232 |
// In a real world program, the user could take here appropriate action (cancel a
|
|
1233 |
// transfer request or whatever).
|
|
1234 |
if (!(iDeviceState & KUsbAlternateSetting))
|
|
1235 |
{
|
|
1236 |
switch (iDeviceState)
|
|
1237 |
{
|
|
1238 |
case EUsbcDeviceStateUndefined:
|
|
1239 |
__PRINT(_L("Device State notifier: Undefined\n"));
|
|
1240 |
iParent.HwStop();
|
|
1241 |
break;
|
|
1242 |
case EUsbcDeviceStateAttached:
|
|
1243 |
__PRINT(_L("Device State notifier: Attached\n"));
|
|
1244 |
iParent.HwStop();
|
|
1245 |
break;
|
|
1246 |
case EUsbcDeviceStatePowered:
|
|
1247 |
__PRINT(_L("Device State notifier: Powered\n"));
|
|
1248 |
iParent.HwStop();
|
|
1249 |
break;
|
|
1250 |
case EUsbcDeviceStateDefault:
|
|
1251 |
__PRINT(_L("Device State notifier: Default\n"));
|
|
1252 |
iParent.HwStop();
|
|
1253 |
break;
|
|
1254 |
case EUsbcDeviceStateAddress:
|
|
1255 |
__PRINT(_L("Device State notifier: Address\n"));
|
|
1256 |
iParent.HwStop();
|
|
1257 |
break;
|
|
1258 |
case EUsbcDeviceStateConfigured:
|
|
1259 |
__PRINT(_L("Device State notifier: Configured\n"));
|
|
1260 |
if (iOldDeviceState == EUsbcDeviceStateSuspended)
|
|
1261 |
{
|
|
1262 |
iParent.HwResume();
|
|
1263 |
}
|
|
1264 |
else
|
|
1265 |
{
|
|
1266 |
iParent.HwStart();
|
|
1267 |
}
|
|
1268 |
break;
|
|
1269 |
case EUsbcDeviceStateSuspended:
|
|
1270 |
__PRINT(_L("Device State notifier: Suspended\n"));
|
|
1271 |
if (iOldDeviceState == EUsbcDeviceStateConfigured)
|
|
1272 |
{
|
|
1273 |
iParent.HwSuspend();
|
|
1274 |
}
|
|
1275 |
break;
|
|
1276 |
default:
|
|
1277 |
__PRINT(_L("Device State notifier: ***BAD***\n"));
|
|
1278 |
iParent.HwStop();
|
|
1279 |
break;
|
|
1280 |
}
|
|
1281 |
iOldDeviceState = iDeviceState;
|
|
1282 |
}
|
|
1283 |
else if (iDeviceState & KUsbAlternateSetting)
|
|
1284 |
{
|
|
1285 |
__PRINT1(_L("Device State notifier: Alternate interface setting has changed: now %d\n"), iDeviceState & ~KUsbAlternateSetting);
|
|
1286 |
}
|
|
1287 |
Activate();
|
|
1288 |
}
|
0
|
1289 |
|
|
1290 |
|
|
1291 |
void CActiveDeviceStateNotifier::Activate()
|
|
1292 |
/**
|
|
1293 |
*
|
|
1294 |
*/
|
300
|
1295 |
{
|
|
1296 |
if (IsActive())
|
|
1297 |
{
|
|
1298 |
__PRINT(_L("Still active\n"));
|
|
1299 |
return;
|
|
1300 |
}
|
|
1301 |
iParent.Ldd().AlternateDeviceStatusNotify(iStatus, iDeviceState);
|
|
1302 |
SetActive();
|
|
1303 |
}
|