297
|
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 |
#include <e32base.h>
|
|
17 |
#include <f32file.h>
|
|
18 |
#include <e32property.h>
|
|
19 |
|
|
20 |
#include "mstypes.h"
|
|
21 |
#include "msctypes.h"
|
|
22 |
#include "usbmsshared.h"
|
|
23 |
|
|
24 |
#include "drivemanager.h"
|
|
25 |
#include "drivepublisher.h"
|
|
26 |
#include "tscsiserverreq.h"
|
|
27 |
#include "tscsiservercmds.h"
|
|
28 |
#include "mserverprotocol.h"
|
|
29 |
#include "mdevicetransport.h"
|
|
30 |
|
|
31 |
#include "testman.h"
|
|
32 |
#include "cscsiserverprotocol.h"
|
|
33 |
#include "debug.h"
|
|
34 |
|
|
35 |
|
|
36 |
TMediaWriteMan::TMediaWriteMan()
|
|
37 |
: iActive(EFalse),
|
|
38 |
iOffset(0),
|
297
|
39 |
iMediaWriteSize(KDefaultMediaWriteSize)
|
0
|
40 |
{
|
|
41 |
}
|
|
42 |
|
|
43 |
void TMediaWriteMan::ReportHighSpeedDevice()
|
297
|
44 |
{
|
|
45 |
iMediaWriteSize = KHsMediaWriteSize;
|
|
46 |
__PRINT1(_L("HS Device reported: SCSI will use %d bytes disk write size"), iMediaWriteSize);
|
|
47 |
}
|
0
|
48 |
|
|
49 |
|
|
50 |
TInt64 TMediaWriteMan::Start(TUint32 aLba, TUint32 aLength, TUint32 aBlockSize)
|
|
51 |
{
|
|
52 |
iActive = ETrue;
|
|
53 |
iOffset = static_cast<TInt64>(aLba) * aBlockSize;
|
|
54 |
iBytesRemain = aLength * aBlockSize;
|
|
55 |
|
297
|
56 |
TInt64 theEnd = iOffset + iBytesRemain;
|
0
|
57 |
return theEnd;
|
|
58 |
}
|
|
59 |
|
|
60 |
TUint32 TMediaWriteMan::NextPacket()
|
|
61 |
{
|
|
62 |
iActive = ETrue;
|
|
63 |
return (iBytesRemain < iMediaWriteSize) ? iBytesRemain : iMediaWriteSize;
|
|
64 |
}
|
|
65 |
|
|
66 |
|
|
67 |
void TMediaWriteMan::Reset()
|
|
68 |
{
|
|
69 |
iActive = EFalse;
|
|
70 |
iOffset = 0;
|
|
71 |
}
|
|
72 |
|
|
73 |
void TMediaWriteMan::SetOffset(const TInt64& aOffset, TUint aLength)
|
|
74 |
{
|
|
75 |
iOffset = aOffset + aLength;
|
|
76 |
iBytesRemain -= aLength;
|
|
77 |
}
|
|
78 |
|
|
79 |
TUint32 TMediaWriteMan::GetPacketLength() const
|
|
80 |
{
|
|
81 |
// KMaxBufSize or the MediaWriteSize, whichever is smallest.
|
297
|
82 |
TUint32 thisLength = (iBytesRemain > KMaxBufSize) ? KMaxBufSize : iBytesRemain;
|
|
83 |
thisLength = (thisLength > iMediaWriteSize) ? iMediaWriteSize : thisLength;
|
0
|
84 |
return thisLength;
|
|
85 |
}
|
|
86 |
|
|
87 |
|
|
88 |
/**
|
|
89 |
Creates the CScsiProtocol object. Called during controller initialisation.
|
|
90 |
|
|
91 |
@param aDriveManager reference to the drive manager object
|
|
92 |
*/
|
|
93 |
CScsiServerProtocol* CScsiServerProtocol::NewL(CDriveManager& aDriveManager)
|
297
|
94 |
{
|
|
95 |
CScsiServerProtocol* self = new (ELeave) CScsiServerProtocol(aDriveManager);
|
|
96 |
CleanupStack::PushL(self);
|
|
97 |
self->ConstructL();
|
|
98 |
CleanupStack::Pop();
|
|
99 |
return self;
|
|
100 |
}
|
0
|
101 |
|
|
102 |
#ifdef MSDC_TESTMODE
|
|
103 |
CScsiServerProtocol* CScsiServerProtocol::NewL(CDriveManager& aDriveManager, TTestParser* aTestParser)
|
297
|
104 |
{
|
|
105 |
CScsiServerProtocol* self = new (ELeave) CScsiServerProtocol(aDriveManager, aTestParser);
|
|
106 |
CleanupStack::PushL(self);
|
|
107 |
self->ConstructL();
|
|
108 |
CleanupStack::Pop();
|
|
109 |
return self;
|
|
110 |
}
|
0
|
111 |
#endif
|
|
112 |
|
|
113 |
|
|
114 |
/**
|
|
115 |
c'tor
|
|
116 |
|
|
117 |
@param aDriveManager reference to the drive manager object
|
|
118 |
*/
|
|
119 |
CScsiServerProtocol::CScsiServerProtocol(CDriveManager& aDriveManager)
|
|
120 |
: iDriveManager(aDriveManager)
|
297
|
121 |
{
|
|
122 |
iWriteTransferPublisher = CUsbWriteTransferPublisher::NewL(iBytesWritten);
|
|
123 |
iReadTransferPublisher = CUsbReadTransferPublisher::NewL(iBytesRead);
|
0
|
124 |
|
297
|
125 |
for (TUint i = 0; i < KUsbMsMaxDrives; i++)
|
|
126 |
{
|
|
127 |
iBytesRead[i] = 0;
|
|
128 |
iBytesWritten[i] = 0;
|
|
129 |
}
|
|
130 |
}
|
0
|
131 |
|
|
132 |
#ifdef MSDC_TESTMODE
|
|
133 |
CScsiServerProtocol::CScsiServerProtocol(CDriveManager& aDriveManager, TTestParser* aTestParser)
|
|
134 |
: iDriveManager(aDriveManager),
|
|
135 |
iTestParser(aTestParser)
|
297
|
136 |
{
|
|
137 |
iWriteTransferPublisher = CUsbWriteTransferPublisher::NewL(iBytesWritten);
|
|
138 |
iReadTransferPublisher = CUsbReadTransferPublisher::NewL(iBytesRead);
|
0
|
139 |
|
297
|
140 |
for (TUint i = 0; i < KUsbMsMaxDrives; i++)
|
|
141 |
{
|
|
142 |
iBytesRead[i] = 0;
|
|
143 |
iBytesWritten[i] = 0;
|
|
144 |
}
|
|
145 |
}
|
0
|
146 |
#endif
|
|
147 |
|
|
148 |
|
|
149 |
CScsiServerProtocol::~CScsiServerProtocol()
|
297
|
150 |
{
|
0
|
151 |
iDataBuf.Close();
|
297
|
152 |
delete iWriteTransferPublisher;
|
|
153 |
delete iReadTransferPublisher;
|
|
154 |
}
|
0
|
155 |
|
|
156 |
|
|
157 |
void CScsiServerProtocol::ConstructL()
|
297
|
158 |
{
|
|
159 |
}
|
0
|
160 |
|
|
161 |
|
|
162 |
/**
|
|
163 |
Associates the transport with the protocol. Called during initialisation of the controller.
|
|
164 |
|
|
165 |
@param aTransport pointer to the transport object
|
|
166 |
*/
|
|
167 |
void CScsiServerProtocol::RegisterTransport(MDeviceTransport* aTransport)
|
297
|
168 |
{
|
|
169 |
iTransport = aTransport;
|
|
170 |
}
|
0
|
171 |
|
|
172 |
|
|
173 |
/**
|
|
174 |
Called by the Transport when it detects that the USB device is either running
|
|
175 |
at High Speed or is at least capable of HS operation. The Protocol can use this
|
|
176 |
information (for instance) to select the optimal write block size to use.
|
|
177 |
|
|
178 |
This function is preferably called before actual MS data transfer operation
|
|
179 |
starts, and usually only once.
|
|
180 |
|
|
181 |
*/
|
|
182 |
void CScsiServerProtocol::ReportHighSpeedDevice()
|
297
|
183 |
{
|
0
|
184 |
iMediaWriteMan.ReportHighSpeedDevice();
|
297
|
185 |
}
|
0
|
186 |
|
|
187 |
|
|
188 |
void CScsiServerProtocol::SetParameters(const TMassStorageConfig& aConfig)
|
297
|
189 |
{
|
|
190 |
iConfig = aConfig;
|
|
191 |
}
|
0
|
192 |
|
|
193 |
|
|
194 |
/**
|
|
195 |
Called by the transport layer when a packet is available for decoding.
|
|
196 |
If an error occurs, the sense code is updated and EFalse is returned.
|
|
197 |
|
|
198 |
@param aData
|
|
199 |
|
|
200 |
@return ETrue if command was decoded and executed successfully
|
|
201 |
*/
|
|
202 |
TBool CScsiServerProtocol::DecodePacket(TPtrC8& aData, TUint8 aLun)
|
297
|
203 |
{
|
0
|
204 |
TScsiServerReq* cdb = NULL;
|
|
205 |
TRAPD(err, cdb = cdb->CreateL(static_cast<TScsiServerReq::TOperationCode>(aData[0]), aData));
|
|
206 |
|
|
207 |
TBool decodeGood = EFalse;
|
|
208 |
if (err == KErrNotSupported)
|
297
|
209 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest, TSenseInfo::EInvalidCmdCode);
|
|
210 |
else if (err != KErrNone)
|
|
211 |
iSenseInfo.SetSense(TSenseInfo::EAbortedCommand, TSenseInfo::EInsufficientRes);
|
0
|
212 |
else if (cdb->iNaca) // Check the CONTROL byte
|
297
|
213 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest, TSenseInfo::EInvalidFieldInCdb);
|
0
|
214 |
else if (cdb->iLink)
|
|
215 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest, TSenseInfo::EInvalidFieldInCdb);
|
|
216 |
else
|
|
217 |
{
|
|
218 |
TScsiServerReq::TOperationCode operationCode = cdb->iOperationCode;
|
|
219 |
if (aLun > iDriveManager.MaxLun())
|
|
220 |
{
|
|
221 |
__PRINT(_L("No drive available\n"));
|
|
222 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest, TSenseInfo::ELuNotSupported);
|
|
223 |
}
|
|
224 |
else
|
|
225 |
{
|
|
226 |
iLun = aLun;
|
|
227 |
if (operationCode != TScsiServerReq::ERequestSense)
|
|
228 |
{
|
|
229 |
iSenseInfo.SetSense(TSenseInfo::ENoSense);
|
|
230 |
}
|
|
231 |
|
|
232 |
switch (operationCode)
|
|
233 |
{
|
|
234 |
case TScsiServerReq::ETestUnitReady:
|
|
235 |
HandleUnitReady();
|
|
236 |
break;
|
|
237 |
|
|
238 |
case TScsiServerReq::ERequestSense:
|
|
239 |
HandleRequestSense(*cdb);
|
|
240 |
break;
|
|
241 |
|
|
242 |
case TScsiServerReq::EInquiry:
|
|
243 |
HandleInquiry(*cdb);
|
|
244 |
break;
|
|
245 |
|
|
246 |
case TScsiServerReq::EModeSense6:
|
|
247 |
HandleModeSense6(*cdb);
|
|
248 |
break;
|
|
249 |
|
|
250 |
case TScsiServerReq::EStartStopUnit:
|
|
251 |
HandleStartStopUnit(*cdb);
|
|
252 |
break;
|
|
253 |
|
|
254 |
case TScsiServerReq::EPreventMediaRemoval:
|
|
255 |
HandlePreventMediaRemoval(*cdb);
|
|
256 |
break;
|
|
257 |
|
|
258 |
case TScsiServerReq::EReadCapacity10:
|
|
259 |
HandleReadCapacity10(*cdb);
|
|
260 |
break;
|
|
261 |
|
|
262 |
case TScsiServerReq::ERead10:
|
|
263 |
HandleRead10(*cdb);
|
|
264 |
break;
|
|
265 |
|
|
266 |
case TScsiServerReq::EWrite10:
|
|
267 |
HandleWrite10(*cdb);
|
|
268 |
break;
|
|
269 |
|
|
270 |
case TScsiServerReq::EReadFormatCapacities:
|
|
271 |
HandleReadFormatCapacities(*cdb);
|
|
272 |
break;
|
|
273 |
|
|
274 |
default:
|
|
275 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest, TSenseInfo::EInvalidCmdCode);
|
|
276 |
break;
|
|
277 |
}
|
|
278 |
}
|
|
279 |
__PRINT1(_L("DecodePacket result = %d"), iSenseInfo.SenseOk());
|
297
|
280 |
decodeGood = iSenseInfo.SenseOk();
|
0
|
281 |
}
|
|
282 |
|
|
283 |
delete cdb;
|
|
284 |
return decodeGood;
|
297
|
285 |
}
|
0
|
286 |
|
|
287 |
|
|
288 |
/**
|
|
289 |
Checks if drive ready
|
|
290 |
|
|
291 |
@param aLun Logic unit number
|
|
292 |
@return pointer to drive correspondent to LUN if drive mounted and ready, NULL otherwise
|
|
293 |
*/
|
|
294 |
CMassStorageDrive* CScsiServerProtocol::GetCheckDrive()
|
297
|
295 |
{
|
0
|
296 |
#ifdef MSDC_TESTMODE
|
|
297 |
if (iTestParser && iTestParser->SenseError() != TTestParser::ETestSenseErrorNoSense)
|
|
298 |
{
|
|
299 |
switch (iTestParser->SenseError())
|
|
300 |
{
|
|
301 |
case TTestParser::ETestSenseErrorMediaNotPresent:
|
|
302 |
__TESTMODEPRINT("Set SENSE ERROR(ENotReady, EMediaNotPresent)");
|
|
303 |
iSenseInfo.SetSense(TSenseInfo::ENotReady,
|
|
304 |
TSenseInfo::EMediaNotPresent);
|
|
305 |
break;
|
|
306 |
|
|
307 |
|
|
308 |
case TTestParser::ETestSenseErrorUnitAttention:
|
|
309 |
default:
|
|
310 |
__TESTMODEPRINT("Set SENSE ERROR(EUnitAttention, ENotReadyToReadyChange)");
|
|
311 |
iSenseInfo.SetSense(TSenseInfo::EUnitAttention,
|
|
312 |
TSenseInfo::ENotReadyToReadyChange);
|
|
313 |
break;
|
|
314 |
}
|
|
315 |
iTestParser->ClrSenseError();
|
|
316 |
return NULL;
|
|
317 |
}
|
|
318 |
#endif
|
|
319 |
|
297
|
320 |
CMassStorageDrive* drive = iDriveManager.Drive(iLun);
|
|
321 |
CMassStorageDrive::TMountState mountState = drive->MountState();
|
0
|
322 |
|
297
|
323 |
if (mountState == CMassStorageDrive::EDisconnected || mountState == CMassStorageDrive::EConnecting)
|
|
324 |
{
|
|
325 |
__PRINT(_L("Drive disconnected\n"));
|
|
326 |
iSenseInfo.SetSense(TSenseInfo::ENotReady,
|
|
327 |
TSenseInfo::EMediaNotPresent);
|
|
328 |
return NULL;
|
|
329 |
}
|
0
|
330 |
|
297
|
331 |
TLocalDriveRef::TDriveState state = drive->CheckDriveState();
|
|
332 |
if (state == TLocalDriveRef::EMediaNotPresent || state == TLocalDriveRef::ELocked)
|
|
333 |
{
|
|
334 |
__PRINT1(_L("Media not present or locked. (state =0x%X)\n"),state);
|
|
335 |
iSenseInfo.SetSense(TSenseInfo::ENotReady, TSenseInfo::EMediaNotPresent);
|
|
336 |
return NULL;
|
|
337 |
}
|
0
|
338 |
|
297
|
339 |
if (drive->IsMediaChanged(ETrue)) //reset "media changed" status
|
|
340 |
{
|
|
341 |
__PRINT(_L("Media was changed\n"));
|
|
342 |
// SAM-2 Section 5.9.5 Unit Attention Condition
|
|
343 |
iSenseInfo.SetSense(TSenseInfo::EUnitAttention, TSenseInfo::ENotReadyToReadyChange);
|
|
344 |
iDriveManager.Connect(iLun); //publish event to USB app
|
|
345 |
return NULL;
|
|
346 |
}
|
0
|
347 |
|
297
|
348 |
if (mountState == CMassStorageDrive::EDisconnecting)
|
|
349 |
{
|
|
350 |
__PRINT(_L("Drive disconnecting\n"));
|
|
351 |
iSenseInfo.SetSense(TSenseInfo::ENotReady,
|
|
352 |
TSenseInfo::EMediaNotPresent);
|
|
353 |
return NULL;
|
|
354 |
}
|
0
|
355 |
|
297
|
356 |
return drive;
|
|
357 |
}
|
0
|
358 |
|
|
359 |
|
|
360 |
/**
|
|
361 |
Command Parser for the UNIT READY command (0x00)
|
|
362 |
|
|
363 |
@param aLun Logic unit number
|
|
364 |
@return ETrue if successful,
|
|
365 |
*/
|
|
366 |
TBool CScsiServerProtocol::HandleUnitReady()
|
297
|
367 |
{
|
|
368 |
return GetCheckDrive() ? ETrue : EFalse;
|
|
369 |
}
|
0
|
370 |
|
|
371 |
|
|
372 |
/**
|
|
373 |
Command Parser for the REQUEST SENSE command (0x03)
|
|
374 |
|
|
375 |
@return ETrue if successful,
|
|
376 |
*/
|
|
377 |
TBool CScsiServerProtocol::HandleRequestSense(const TScsiServerReq& aRequest)
|
297
|
378 |
{
|
0
|
379 |
const TScsiServerRequestSenseReq request = static_cast<const TScsiServerRequestSenseReq&>(aRequest);
|
297
|
380 |
__PRINT1(_L("length = %d\n"), request.iAllocationLength);
|
0
|
381 |
|
|
382 |
TScsiServerRequestSenseResp requestSense;
|
|
383 |
requestSense.iAllocationLength = request.iAllocationLength;
|
|
384 |
|
|
385 |
requestSense.SetResponseCode(TScsiServerRequestSenseResp::ECurrentErrors);
|
|
386 |
requestSense.iSensePtr = &iSenseInfo;
|
|
387 |
requestSense.Encode(iCommandBuf);
|
|
388 |
|
297
|
389 |
__PRINT4(_L("Response=0x%x Sense=0x%x, Additional=0x%x, Qualifier=0x%x\n"),
|
|
390 |
iCommandBuf[0], iCommandBuf[02], iCommandBuf[12], iCommandBuf[13]);
|
0
|
391 |
|
297
|
392 |
TPtrC8 writeBuf = iCommandBuf.Left(request.iAllocationLength);
|
|
393 |
iTransport->SetupDataIn(writeBuf);
|
0
|
394 |
|
297
|
395 |
// clear the sense info
|
|
396 |
iSenseInfo.SetSense(TSenseInfo::ENoSense);
|
|
397 |
return ETrue;
|
|
398 |
}
|
0
|
399 |
|
|
400 |
|
|
401 |
/**
|
|
402 |
Command Parser for the INQUIRY command (0x12)
|
|
403 |
|
|
404 |
@param aLun Logic unit number
|
|
405 |
@return ETrue if successful,
|
|
406 |
*/
|
|
407 |
TBool CScsiServerProtocol::HandleInquiry(const TScsiServerReq& aRequest)
|
297
|
408 |
{
|
0
|
409 |
const TScsiServerInquiryReq request = static_cast<const TScsiServerInquiryReq&>(aRequest);
|
|
410 |
|
297
|
411 |
if (request.iCmdDt || request.iEvpd || request.iPage || iLun >= KUsbMsMaxDrives)
|
|
412 |
{
|
|
413 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest, TSenseInfo::EInvalidFieldInCdb);
|
|
414 |
return EFalse;
|
|
415 |
}
|
0
|
416 |
|
|
417 |
TScsiServerInquiryResp inquiry(iConfig);
|
|
418 |
|
|
419 |
inquiry.SetAllocationLength(request.iAllocationLength);
|
|
420 |
|
|
421 |
#ifdef MSDC_TESTMODE
|
|
422 |
if (iTestParser && !iTestParser->Removable())
|
|
423 |
{
|
|
424 |
__TESTMODEPRINT("RMB Cleared");
|
|
425 |
inquiry.SetNotRemovable();
|
|
426 |
}
|
|
427 |
#endif
|
|
428 |
|
|
429 |
inquiry.Encode(iCommandBuf);
|
|
430 |
|
|
431 |
TUint length = inquiry.Length();
|
|
432 |
|
297
|
433 |
TPtrC8 writeBuf = iCommandBuf.Left(length);
|
|
434 |
iTransport->SetupDataIn(writeBuf);
|
0
|
435 |
|
297
|
436 |
iSenseInfo.SetSense(TSenseInfo::ENoSense);
|
|
437 |
return ETrue;
|
|
438 |
}
|
0
|
439 |
|
|
440 |
|
|
441 |
/**
|
|
442 |
Command Parser for the START STOP UNIT command (0x1B)
|
|
443 |
|
|
444 |
@param aData command data (started form position 1)
|
|
445 |
@param aLun Logic unit number
|
|
446 |
@return ETrue if successful, TFalse otherwise
|
|
447 |
*/
|
|
448 |
TBool CScsiServerProtocol::HandleStartStopUnit(const TScsiServerReq& aRequest)
|
297
|
449 |
{
|
|
450 |
const TScsiServerStartStopUnitReq request = static_cast<const TScsiServerStartStopUnitReq&>(aRequest);
|
0
|
451 |
|
297
|
452 |
if (request.iLoej)
|
|
453 |
{
|
|
454 |
if(request.iStart) //Start unit
|
|
455 |
{
|
|
456 |
iDriveManager.Connect(iLun);
|
|
457 |
__PRINT(_L("Load media\n"));
|
0
|
458 |
|
|
459 |
// rd/wr publisher
|
297
|
460 |
iBytesRead[iLun] = 0;
|
|
461 |
iBytesWritten[iLun] = 0;
|
0
|
462 |
|
297
|
463 |
// publish the initial values
|
|
464 |
iWriteTransferPublisher->DoPublishDataTransferredEvent();
|
|
465 |
iReadTransferPublisher->DoPublishDataTransferredEvent();
|
|
466 |
}
|
|
467 |
else //Stop unit
|
|
468 |
{
|
|
469 |
iDriveManager.SetCritical(iLun, EFalse);
|
|
470 |
iDriveManager.Disconnect(iLun);
|
|
471 |
__PRINT(_L("Unload media\n"));
|
|
472 |
}
|
|
473 |
}
|
0
|
474 |
|
297
|
475 |
if (request.iImmed)
|
|
476 |
{
|
|
477 |
return ETrue;
|
|
478 |
}
|
0
|
479 |
|
297
|
480 |
CMassStorageDrive* drive = iDriveManager.Drive(iLun);
|
0
|
481 |
|
297
|
482 |
TInt timeLeft (20); // 1 sec timeout
|
|
483 |
CMassStorageDrive::TMountState mountState;
|
0
|
484 |
|
297
|
485 |
do
|
|
486 |
{
|
|
487 |
User::After(1000 * 50); // 50 mSec
|
|
488 |
--timeLeft;
|
|
489 |
mountState = drive->MountState();
|
0
|
490 |
|
297
|
491 |
if ((!request.iStart && mountState != CMassStorageDrive::EConnected)
|
|
492 |
||
|
|
493 |
(request.iStart &&
|
|
494 |
(mountState == CMassStorageDrive::EDisconnecting ||
|
0
|
495 |
mountState == CMassStorageDrive::EConnected)))
|
297
|
496 |
{
|
|
497 |
return ETrue;
|
|
498 |
}
|
|
499 |
} while (timeLeft>0);
|
0
|
500 |
|
297
|
501 |
//timeout happend
|
|
502 |
iSenseInfo.SetSense(TSenseInfo::ENotReady,
|
|
503 |
TSenseInfo::EAscLogicalUnitDoesNotRespondToSelection);
|
|
504 |
return EFalse;
|
|
505 |
}
|
0
|
506 |
|
|
507 |
|
|
508 |
/**
|
|
509 |
Command Parser for the PREVENT/ALLOW MEDIA REMOVAL command (0x1E)
|
|
510 |
|
|
511 |
@param aData command data (started form position 1)
|
|
512 |
@param aLun Logic unit number
|
|
513 |
@return ETrue if successful.
|
|
514 |
*/
|
|
515 |
TBool CScsiServerProtocol::HandlePreventMediaRemoval(const TScsiServerReq& aRequest)
|
297
|
516 |
{
|
|
517 |
const TScsiServerPreventMediaRemovalReq& request = static_cast<const TScsiServerPreventMediaRemovalReq&>(aRequest);
|
|
518 |
CMassStorageDrive* drive = GetCheckDrive();
|
0
|
519 |
|
297
|
520 |
if (drive == NULL)
|
|
521 |
{
|
|
522 |
return EFalse;
|
|
523 |
}
|
|
524 |
iDriveManager.SetCritical(iLun, request.iPrevent);
|
|
525 |
return ETrue;
|
|
526 |
}
|
0
|
527 |
|
|
528 |
|
|
529 |
/** Cancel active state, Invoked by transnport when it stops */
|
|
530 |
TInt CScsiServerProtocol::Cancel()
|
297
|
531 |
{
|
|
532 |
iDriveManager.SetCritical(CDriveManager::KAllLuns, EFalse);
|
|
533 |
return KErrNone;
|
|
534 |
}
|
0
|
535 |
|
|
536 |
|
|
537 |
TBool CScsiServerProtocol::HandleReadFormatCapacities(const TScsiServerReq& aRequest)
|
|
538 |
/**
|
|
539 |
* Command Parser for the READ FORMAT CAPACITIES command (0x23)
|
|
540 |
*
|
|
541 |
* @return ETrue if successful, else a standard Symbian OS error code.
|
|
542 |
*/
|
297
|
543 |
{
|
|
544 |
const TScsiServerReadFormatCapacitiesReq& request = static_cast<const TScsiServerReadFormatCapacitiesReq&>(aRequest);
|
0
|
545 |
|
|
546 |
CMassStorageDrive* drive = NULL;
|
|
547 |
for (TInt i = 0; i < 10; i++)
|
|
548 |
{
|
|
549 |
drive = GetCheckDrive();
|
|
550 |
if (drive)
|
|
551 |
{
|
|
552 |
break;
|
|
553 |
}
|
|
554 |
User::After(100000);
|
|
555 |
}
|
|
556 |
|
297
|
557 |
if (!drive)
|
|
558 |
{
|
0
|
559 |
return EFalse;
|
297
|
560 |
}
|
0
|
561 |
|
297
|
562 |
TUint32 numBlocks = I64LOW(drive->MediaParams().NumBlocks());
|
0
|
563 |
|
|
564 |
TScsiServerReadFormatCapacitiesResp response(request.AllocationLength());
|
|
565 |
response.SetNumberBlocks(numBlocks);
|
|
566 |
|
|
567 |
response.Encode(iCommandBuf);
|
297
|
568 |
TPtrC8 writeBuf = iCommandBuf;
|
|
569 |
iTransport->SetupDataIn(writeBuf);
|
|
570 |
return ETrue;
|
|
571 |
}
|
0
|
572 |
|
|
573 |
|
|
574 |
/**
|
|
575 |
Command Parser for the READ CAPACITY(10) command (0x25)
|
|
576 |
|
|
577 |
@param aData command data (started form position 1)
|
|
578 |
@param aLun Logic unit number
|
|
579 |
@return ETrue if successful.
|
|
580 |
*/
|
|
581 |
TBool CScsiServerProtocol::HandleReadCapacity10(const TScsiServerReq& aRequest)
|
297
|
582 |
{
|
|
583 |
const TScsiServerReadCapacity10Req& request = static_cast<const TScsiServerReadCapacity10Req&>(aRequest);
|
|
584 |
CMassStorageDrive* drive = GetCheckDrive();
|
|
585 |
if (drive == NULL)
|
|
586 |
{
|
|
587 |
return EFalse;
|
|
588 |
}
|
0
|
589 |
|
297
|
590 |
if (request.iPmi || request.iLogicalBlockAddress) //do not support partial medium indicator
|
|
591 |
{
|
|
592 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest, TSenseInfo::EInvalidFieldInCdb);
|
|
593 |
return EFalse;
|
|
594 |
}
|
0
|
595 |
|
|
596 |
TScsiServerReadCapacity10Resp response;
|
|
597 |
response.Set(drive->MediaParams().BlockSize(), drive->MediaParams().NumBlocks());
|
|
598 |
response.Encode(iCommandBuf);
|
|
599 |
|
297
|
600 |
TPtrC8 writeBuf = iCommandBuf;
|
|
601 |
iTransport->SetupDataIn(writeBuf);
|
0
|
602 |
|
297
|
603 |
return KErrNone;
|
|
604 |
}
|
0
|
605 |
|
|
606 |
|
|
607 |
/**
|
|
608 |
Command Parser for the READ10 command (0x28)
|
|
609 |
|
|
610 |
@param aData command data (started form position 1)
|
|
611 |
@param aLun Logic unit number
|
|
612 |
@return ETrue if successful.
|
|
613 |
*/
|
|
614 |
TBool CScsiServerProtocol::HandleRead10(const TScsiServerReq& aRequest)
|
297
|
615 |
{
|
|
616 |
const TScsiServerRead10Req& request = static_cast<const TScsiServerRead10Req&>(aRequest);
|
|
617 |
CMassStorageDrive* drive = GetCheckDrive();
|
|
618 |
if (drive == NULL)
|
|
619 |
{
|
|
620 |
return EFalse;
|
|
621 |
}
|
0
|
622 |
|
297
|
623 |
if (request.iProtect)
|
|
624 |
{
|
|
625 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest, TSenseInfo::EInvalidFieldInCdb);
|
|
626 |
return EFalse;
|
|
627 |
}
|
0
|
628 |
|
297
|
629 |
if (!request.iTransferLength)
|
|
630 |
{
|
|
631 |
return ETrue; // do nothing - this is not an error
|
|
632 |
}
|
0
|
633 |
|
|
634 |
TUint32 blockSize = drive->MediaParams().BlockSize();
|
|
635 |
|
297
|
636 |
const TInt64 bOffset = static_cast<TInt64>(request.iLogicalBlockAddress) * blockSize;
|
|
637 |
const TInt bLength = request.iTransferLength * blockSize;
|
|
638 |
const TInt64 theEnd = bOffset + bLength;
|
0
|
639 |
|
297
|
640 |
if (theEnd > drive->MediaParams().Size()) //check if media big enough for this request
|
|
641 |
{
|
|
642 |
__PRINT(_L("err - Request ends out of media\n"));
|
|
643 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest, TSenseInfo::ELbaOutOfRange);
|
|
644 |
return EFalse;
|
|
645 |
}
|
0
|
646 |
|
297
|
647 |
// check if our buffer can hold requested data
|
|
648 |
if (iDataBuf.MaxLength() < bLength)
|
|
649 |
{
|
0
|
650 |
TRAPD(err,iDataBuf.ReAllocL(bLength));
|
|
651 |
if (err)
|
|
652 |
{
|
|
653 |
__PRINT(_L("err - Buffer too small\n"));
|
|
654 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest, TSenseInfo::EInvalidFieldInCdb);
|
|
655 |
return EFalse;
|
|
656 |
}
|
297
|
657 |
}
|
0
|
658 |
|
|
659 |
iDataBuf.SetLength(bLength);
|
297
|
660 |
TInt err = drive->Read(bOffset, bLength, iDataBuf);
|
|
661 |
if (err != KErrNone)
|
|
662 |
{
|
|
663 |
__PRINT1(_L("Read failed, err=%d\n"), err);
|
|
664 |
iSenseInfo.SetSense(TSenseInfo::ENotReady, TSenseInfo::EMediaNotPresent);
|
|
665 |
return EFalse;
|
|
666 |
}
|
0
|
667 |
|
297
|
668 |
TPtrC8 writeBuf = iDataBuf;
|
0
|
669 |
|
|
670 |
// rd publisher
|
297
|
671 |
iBytesRead[iLun] += writeBuf.Length();
|
|
672 |
iReadTransferPublisher->StartTimer();
|
0
|
673 |
|
297
|
674 |
// Set up data write to the host
|
0
|
675 |
#ifdef MSDC_TESTMODE
|
|
676 |
if (iTestParser)
|
|
677 |
{
|
|
678 |
TBool test = iTestParser->DInSearch(writeBuf);
|
|
679 |
}
|
|
680 |
#endif
|
297
|
681 |
iTransport->SetupDataIn(writeBuf);
|
|
682 |
return ETrue;
|
|
683 |
}
|
0
|
684 |
|
|
685 |
|
|
686 |
/**
|
|
687 |
Command Parser for the WRITE(10) command (0x2A)
|
|
688 |
|
|
689 |
@param aData command data (started form position 1)
|
|
690 |
@param aLun Logic unit number
|
|
691 |
@return ETrue if successful.
|
|
692 |
*/
|
|
693 |
TBool CScsiServerProtocol::HandleWrite10(const TScsiServerReq& aRequest)
|
297
|
694 |
{
|
|
695 |
const TScsiServerWrite10Req& request = static_cast<const TScsiServerWrite10Req&>(aRequest);
|
|
696 |
CMassStorageDrive* drive = GetCheckDrive();
|
|
697 |
if (drive == NULL)
|
|
698 |
{
|
|
699 |
return EFalse;
|
|
700 |
}
|
|
701 |
if (request.iProtect)
|
|
702 |
{
|
|
703 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest, TSenseInfo::EInvalidFieldInCdb);
|
|
704 |
return EFalse;
|
|
705 |
}
|
0
|
706 |
|
297
|
707 |
if (!request.iTransferLength)
|
|
708 |
{
|
|
709 |
return ETrue; // do nothing - this is not an error
|
|
710 |
}
|
0
|
711 |
|
|
712 |
const TMediaParams& params = drive->MediaParams();
|
|
713 |
|
297
|
714 |
if (params.IsWriteProtected() ||
|
|
715 |
params.IsLocked())
|
|
716 |
{
|
|
717 |
iSenseInfo.SetSense(TSenseInfo::EDataProtection, TSenseInfo::EWriteProtected);
|
|
718 |
return EFalse;
|
|
719 |
}
|
0
|
720 |
|
|
721 |
TInt64 theEnd = iMediaWriteMan.Start(request.iLogicalBlockAddress, request.iTransferLength, params.BlockSize());
|
297
|
722 |
if (theEnd > params.Size()) //check if media big enough for this request
|
|
723 |
{
|
|
724 |
__PRINT(_L("err - Request ends out of media\n"));
|
|
725 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest, TSenseInfo::ELbaOutOfRange);
|
|
726 |
return EFalse;
|
|
727 |
}
|
0
|
728 |
|
|
729 |
TUint32 thisLength = iMediaWriteMan.GetPacketLength();
|
|
730 |
|
|
731 |
// check if our buffer can hold requested data
|
|
732 |
if (iDataBuf.MaxLength() < thisLength)
|
|
733 |
{
|
|
734 |
TRAPD(err,iDataBuf.ReAllocL(thisLength));
|
|
735 |
if (err)
|
|
736 |
{
|
|
737 |
__PRINT(_L("err - Buffer too small\n"));
|
|
738 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest, TSenseInfo::EInvalidFieldInCdb);
|
|
739 |
return EFalse;
|
|
740 |
}
|
|
741 |
}
|
|
742 |
|
297
|
743 |
iDataBuf.SetLength(thisLength);
|
|
744 |
TPtr8 readBuf = iDataBuf.LeftTPtr(iDataBuf.Length());
|
0
|
745 |
|
|
746 |
// wr publisher
|
297
|
747 |
iBytesWritten[iLun] += readBuf.Length();
|
|
748 |
iWriteTransferPublisher->StartTimer();
|
|
749 |
iTransport->SetupDataOut(readBuf);
|
|
750 |
return ETrue;
|
|
751 |
}
|
0
|
752 |
|
|
753 |
|
|
754 |
void CScsiServerProtocol::MediaWriteAbort()
|
|
755 |
{
|
|
756 |
iMediaWriteMan.Reset();
|
|
757 |
iSenseInfo.SetSense(TSenseInfo::EAbortedCommand);
|
|
758 |
}
|
|
759 |
|
|
760 |
/**
|
|
761 |
Called by the transport when the requested data has been read or an error has
|
|
762 |
occurred during the read.
|
|
763 |
|
|
764 |
@param aError Indicate if an error occurs during reading data by transport.
|
|
765 |
@return KErrAbort if command processing is complete but has failed,
|
|
766 |
KErrCompletion if sufficient data is available in the buffer to process
|
|
767 |
the transfer immediately, KErrNotReady if insufficient data is
|
|
768 |
available in the buffer so the transport should wait for it to arrive,
|
|
769 |
KErrNone if command processing is complete and was successful.
|
|
770 |
*/
|
|
771 |
TInt CScsiServerProtocol::MediaWritePacket(TUint& aBytesWritten)
|
297
|
772 |
{
|
0
|
773 |
aBytesWritten = 0;
|
|
774 |
if (iMediaWriteMan.Active() == EFalse)
|
|
775 |
{
|
|
776 |
iSenseInfo.SetSense(TSenseInfo::EAbortedCommand);
|
|
777 |
return KErrAbort;
|
|
778 |
}
|
|
779 |
|
|
780 |
CMassStorageDrive* drive = GetCheckDrive();
|
|
781 |
if (drive == NULL)
|
|
782 |
{
|
|
783 |
return KErrAbort;
|
|
784 |
}
|
|
785 |
|
|
786 |
#ifdef MSDC_TESTMODE
|
|
787 |
if (iTestParser && iTestParser->Enabled())
|
|
788 |
{
|
|
789 |
TInt testCase = iTestParser->TestCase();
|
|
790 |
if (testCase == TTestParser::ETestCaseDoStallData)
|
|
791 |
{
|
|
792 |
iTestParser->DecTestCounter();
|
|
793 |
|
|
794 |
TInt testCounter = iTestParser->TestCounter();
|
|
795 |
if (testCounter == 1)
|
|
796 |
{
|
|
797 |
__TESTMODEPRINT1("Aborting MediaWritePacket (Data Stall) x%x", iMediaWriteMan.BytesRemain());
|
|
798 |
__TESTMODEPRINT2("Offset=0x%lx Length=%x",
|
|
799 |
iMediaWriteMan.Offset(), iDataBuf.Length());
|
|
800 |
return KErrNone;
|
|
801 |
}
|
|
802 |
else if (testCounter == 0)
|
|
803 |
{
|
|
804 |
// Display the next write and clear the test
|
|
805 |
iTestParser->ClrTestCase();
|
|
806 |
__TESTMODEPRINT2("Offset=0x%lx Length=%x",
|
|
807 |
iMediaWriteMan.Offset(), iDataBuf.Length());
|
|
808 |
}
|
|
809 |
}
|
|
810 |
else if (testCase == TTestParser::ETestCaseDoResidue)
|
|
811 |
{
|
|
812 |
iTestParser->DecTestCounter();
|
|
813 |
TInt testCounter = iTestParser->TestCounter();
|
|
814 |
|
|
815 |
if (testCounter == 1)
|
|
816 |
{
|
|
817 |
// abort write and leave residue
|
|
818 |
__TESTMODEPRINT1("Aborting MediaWritePacket (Data Residue) x%x", iMediaWriteMan.BytesRemain());
|
|
819 |
__TESTMODEPRINT2("Offset=0x%lx Length=0x%x",
|
|
820 |
iMediaWriteMan.Offset(), iDataBuf.Length());
|
|
821 |
aBytesWritten = 0;
|
|
822 |
return KErrAbort;
|
|
823 |
}
|
|
824 |
else if (testCounter == 0)
|
|
825 |
{
|
|
826 |
// Display the next write and clear the test
|
|
827 |
iTestParser->ClrTestCase();
|
|
828 |
__TESTMODEPRINT2("MediaWritePacket Offset=0x%lx Length=0x%x",
|
|
829 |
iMediaWriteMan.Offset(), iDataBuf.Length());
|
|
830 |
}
|
|
831 |
else
|
|
832 |
{
|
|
833 |
__TESTMODEPRINT3("MediaWritePacket[%x] Offset=0x%lx Length=0x%x",
|
|
834 |
testCounter, iMediaWriteMan.Offset(), iDataBuf.Length());
|
|
835 |
}
|
|
836 |
|
|
837 |
}
|
|
838 |
}
|
|
839 |
#endif
|
|
840 |
|
297
|
841 |
const TInt64 bOffset = iMediaWriteMan.Offset();
|
0
|
842 |
iMediaWriteMan.Reset();
|
|
843 |
|
297
|
844 |
__PRINT1(_L("SCSI: writing %d bytes\n"), iDataBuf.Length());
|
0
|
845 |
|
|
846 |
TInt err = KErrNone;
|
|
847 |
#ifdef MSDC_TESTMODE
|
|
848 |
if (iTestParser)
|
|
849 |
{
|
|
850 |
TBool test = iTestParser->DoutSearch(iDataBuf);
|
|
851 |
if (test)
|
|
852 |
{
|
|
853 |
// Do not write test control blocks to media
|
|
854 |
}
|
|
855 |
else
|
|
856 |
{
|
|
857 |
// ********* Write data to the drive ********
|
|
858 |
err = drive->Write(bOffset, iDataBuf);
|
|
859 |
}
|
|
860 |
}
|
|
861 |
#else
|
|
862 |
// ********* Write data to the drive ********
|
297
|
863 |
err = drive->Write(bOffset, iDataBuf);
|
0
|
864 |
#endif
|
297
|
865 |
if (err != KErrNone)
|
|
866 |
{
|
|
867 |
__PRINT1(_L("Error after write = 0x%X \n"), err);
|
|
868 |
iSenseInfo.SetSense(TSenseInfo::EAbortedCommand);
|
|
869 |
return KErrAbort;
|
|
870 |
}
|
0
|
871 |
|
297
|
872 |
TUint thisLength = iDataBuf.Length();
|
0
|
873 |
aBytesWritten = thisLength;
|
|
874 |
|
|
875 |
iMediaWriteMan.SetOffset(bOffset, thisLength);
|
|
876 |
|
297
|
877 |
if (iMediaWriteMan.BytesRemain() == 0)
|
0
|
878 |
{
|
|
879 |
return iSenseInfo.SenseOk() ? KErrNone : KErrAbort;
|
|
880 |
}
|
|
881 |
|
|
882 |
// More data is expected - set up another request to read from the host
|
|
883 |
const TUint32 nextPacketLength = iMediaWriteMan.NextPacket();
|
297
|
884 |
TUint bytesAvail = iTransport->BytesAvailable() & ~(drive->MediaParams().BlockSize()-1);
|
0
|
885 |
|
297
|
886 |
TBool wait = EFalse;
|
0
|
887 |
|
|
888 |
thisLength = nextPacketLength;
|
|
889 |
if (bytesAvail)
|
|
890 |
{
|
|
891 |
if (bytesAvail < nextPacketLength)
|
|
892 |
{
|
|
893 |
// Not enough data is available at the transport to satisfy the
|
|
894 |
// request, so return KErrNotReady to indicate that the transport
|
|
895 |
// should wait.
|
|
896 |
thisLength = nextPacketLength;
|
|
897 |
wait = ETrue;
|
|
898 |
}
|
|
899 |
}
|
|
900 |
|
297
|
901 |
thisLength = (thisLength > KMaxBufSize) ? KMaxBufSize : thisLength;
|
0
|
902 |
|
297
|
903 |
iDataBuf.SetLength(thisLength);
|
|
904 |
TPtr8 readBuf = iDataBuf.LeftTPtr(iDataBuf.Length());
|
0
|
905 |
iTransport->SetupDataOut(readBuf);
|
|
906 |
return wait ? KErrNotReady : KErrCompletion;
|
297
|
907 |
}
|
0
|
908 |
|
|
909 |
|
|
910 |
/**
|
|
911 |
Command Parser for the MODE SENSE(06) command (0x1A)
|
|
912 |
|
|
913 |
@return ETrue if successful.
|
|
914 |
*/
|
|
915 |
TBool CScsiServerProtocol::HandleModeSense6(const TScsiServerReq& aRequest)
|
297
|
916 |
{
|
|
917 |
const TScsiServerModeSense6Req& request = static_cast<const TScsiServerModeSense6Req&>(aRequest);
|
0
|
918 |
|
297
|
919 |
TScsiServerModeSense6Resp response;
|
0
|
920 |
response.SetAllocationLength(request.iAllocationLength);
|
|
921 |
|
297
|
922 |
if (request.iPageCode != TScsiServerModeSense6Req::KAllPages ||
|
0
|
923 |
request.iPageControl == TScsiServerModeSense6Req::EChangeableValues)
|
297
|
924 |
{
|
|
925 |
__PRINT(_L("TSenseInfo::EIllegalRequest,TSenseInfo::EInvalidFieldInCdb"));
|
|
926 |
iSenseInfo.SetSense(TSenseInfo::EIllegalRequest,TSenseInfo::EInvalidFieldInCdb);
|
|
927 |
return EFalse;
|
|
928 |
}
|
|
929 |
if (request.iPageControl != TScsiServerModeSense6Req::EDefaultValues)
|
|
930 |
{
|
|
931 |
//check if drive write protected
|
|
932 |
CMassStorageDrive* drive = GetCheckDrive();
|
|
933 |
if (drive == NULL)
|
|
934 |
{
|
|
935 |
__PRINT(_L("drive == null"));
|
|
936 |
return EFalse;
|
|
937 |
}
|
0
|
938 |
|
|
939 |
#ifdef MSDC_TESTMODE
|
|
940 |
if (iTestParser)
|
|
941 |
{
|
|
942 |
response.SetWp(iTestParser->WriteProtect());
|
|
943 |
}
|
|
944 |
#else
|
|
945 |
response.SetWp(drive->MediaParams().IsWriteProtected());
|
|
946 |
#endif
|
|
947 |
}
|
|
948 |
|
|
949 |
response.Encode(iCommandBuf);
|
|
950 |
|
297
|
951 |
TPtrC8 writeBuf = iCommandBuf;
|
|
952 |
iTransport->SetupDataIn(writeBuf);
|
|
953 |
return iSenseInfo.SenseOk();
|
|
954 |
}
|