os/kernelhwsrv/userlibandfileserver/fileserver/shostmassstorage/server/protocol/tsbcclientinterface.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
/**
sl@0
    17
 @file
sl@0
    18
 @internalTechnology
sl@0
    19
*/
sl@0
    20
sl@0
    21
#include <e32base.h>
sl@0
    22
sl@0
    23
#include "msdebug.h"
sl@0
    24
#include "debug.h"
sl@0
    25
#include "msctypes.h"
sl@0
    26
sl@0
    27
#include "mtransport.h"
sl@0
    28
#include "mprotocol.h"
sl@0
    29
#include "tscsiclientreq.h"
sl@0
    30
sl@0
    31
#include "tblocktransfer.h"
sl@0
    32
#include "mscutils.h"
sl@0
    33
#include "usbmshostpanic.h"
sl@0
    34
#include "tscsiblockcmds.h"
sl@0
    35
#include "tsbcclientinterface.h"
sl@0
    36
sl@0
    37
/**
sl@0
    38
Constructor.
sl@0
    39
sl@0
    40
@param aTransport The Transport interface to be used
sl@0
    41
*/
sl@0
    42
TSbcClientInterface::TSbcClientInterface(MTransport& aTransport)
sl@0
    43
:   iTransport(aTransport)
sl@0
    44
    {
sl@0
    45
    __MSFNLOG
sl@0
    46
    }
sl@0
    47
sl@0
    48
sl@0
    49
TSbcClientInterface::~TSbcClientInterface()
sl@0
    50
    {
sl@0
    51
    __MSFNLOG
sl@0
    52
    }
sl@0
    53
sl@0
    54
sl@0
    55
/**
sl@0
    56
Constructor to create and send SCSI MODE SENSE (6) request to obtain the
sl@0
    57
medium's Write Protect status. The function leaves if the device response is not
sl@0
    58
compliant with the protocol standard.
sl@0
    59
sl@0
    60
@param aPageCode The SCSI PAGE CODE value
sl@0
    61
@param aWriteProtected The SCSI WP value
sl@0
    62
sl@0
    63
@return TInt KErrNone if successful, KErrCommandFailed to indicate a
sl@0
    64
device status error, KErrCommandStalled to indicate device stall
sl@0
    65
*/
sl@0
    66
TInt TSbcClientInterface::ModeSense6L(TUint aPageCode, TBool& aWriteProtected)
sl@0
    67
    {
sl@0
    68
    __MSFNLOG
sl@0
    69
    TScsiClientModeSense6Req modeSense6Req(TScsiClientModeSense6Req::ECurrentValues,
sl@0
    70
                                           aPageCode);
sl@0
    71
    TScsiClientModeSense6Resp modeSense6Resp;
sl@0
    72
    TInt err = iTransport.SendControlCmdL(&modeSense6Req, &modeSense6Resp);
sl@0
    73
    if (!err)
sl@0
    74
        {
sl@0
    75
        __SCSIPRINT1(_L("SCSI MODE SENSE (6) INFO WrProtect=%d"),
sl@0
    76
                     modeSense6Resp.iWriteProtected);
sl@0
    77
        aWriteProtected = modeSense6Resp.iWriteProtected;
sl@0
    78
        }
sl@0
    79
    else
sl@0
    80
        {
sl@0
    81
        aWriteProtected = EFalse;
sl@0
    82
        }
sl@0
    83
	return err;
sl@0
    84
    }
sl@0
    85
sl@0
    86
sl@0
    87
/**
sl@0
    88
Create and send SCSI MODE SENSE (10) request to obtain the mediums Write Protect
sl@0
    89
status. The function leaves if the device response is not compliant with the
sl@0
    90
protocol standard.
sl@0
    91
sl@0
    92
@param aPageCode The SCSI PAGE CODE value
sl@0
    93
@param aWriteProtected The SCSI WP value
sl@0
    94
sl@0
    95
@return TInt KErrNone if successful, KErrCommandFailed to indicate a
sl@0
    96
device status error, KErrCommandStalled to indicate a device stall
sl@0
    97
*/
sl@0
    98
TInt TSbcClientInterface::ModeSense10L(TUint aPageCode, TBool& aWriteProtected)
sl@0
    99
    {
sl@0
   100
    __MSFNLOG
sl@0
   101
    TScsiClientModeSense10Req modeSense10Req(TScsiClientModeSense10Req::ECurrentValues,
sl@0
   102
                                             aPageCode);
sl@0
   103
    TScsiClientModeSense10Resp modeSense10Resp;
sl@0
   104
    TInt err = iTransport.SendControlCmdL(&modeSense10Req, &modeSense10Resp);
sl@0
   105
sl@0
   106
    if (!err)
sl@0
   107
        {
sl@0
   108
        __SCSIPRINT1(_L("SCSI MODE SENSE (10) INFO WrProtect=%d"),
sl@0
   109
                     modeSense10Resp.iWriteProtected);
sl@0
   110
        aWriteProtected = modeSense10Resp.iWriteProtected;
sl@0
   111
        }
sl@0
   112
    else
sl@0
   113
        {
sl@0
   114
        aWriteProtected = EFalse;
sl@0
   115
        }
sl@0
   116
	return err;
sl@0
   117
    }
sl@0
   118
sl@0
   119
sl@0
   120
/**
sl@0
   121
Constructor to create SCSI MODE SENSE (10) request.
sl@0
   122
sl@0
   123
@param aPageControl The SCSI PAGE CODE value
sl@0
   124
@param aPageCode The SCSI WP value
sl@0
   125
@param aSubPageCode The SCSI SUB PAGE CODE value
sl@0
   126
*/
sl@0
   127
TScsiClientModeSense10Req::TScsiClientModeSense10Req(TPageControl aPageControl,
sl@0
   128
                                                     TUint aPageCode,
sl@0
   129
                                                     TUint aSubPageCode)
sl@0
   130
    :
sl@0
   131
    TScsiClientReq(EModeSense10),
sl@0
   132
    iPageControl(aPageControl),
sl@0
   133
    iPageCode(aPageCode),
sl@0
   134
    iSubPageCode(aSubPageCode),
sl@0
   135
    iAllocationLength(KResponseLength)
sl@0
   136
    {
sl@0
   137
    __MSFNLOG
sl@0
   138
    }
sl@0
   139
sl@0
   140
sl@0
   141
TInt TScsiClientModeSense10Req::EncodeRequestL(TDes8& aBuffer) const
sl@0
   142
    {
sl@0
   143
    __MSFNSLOG
sl@0
   144
    __SCSIPRINT(_L("<-- SCSI MODE SENSE (10)"));
sl@0
   145
    TInt length = TScsiClientReq::EncodeRequestL(aBuffer);
sl@0
   146
sl@0
   147
    // PC
sl@0
   148
    aBuffer[2] = iPageCode;
sl@0
   149
    aBuffer[2] |= iPageControl << 6;
sl@0
   150
    aBuffer[3] = iSubPageCode;
sl@0
   151
sl@0
   152
    BigEndian::Put16(&aBuffer[7], iAllocationLength);
sl@0
   153
    return length;
sl@0
   154
    }
sl@0
   155
sl@0
   156
/**
sl@0
   157
Create READ (10) request and send to the transport layer. This performs a
sl@0
   158
logical block read of the device server. The received data is appended into the
sl@0
   159
copy buffer. The function leaves if the device response is not compliant with
sl@0
   160
the protocol standard.
sl@0
   161
Note that TBlockTransfer must be initialised beforehand.
sl@0
   162
sl@0
   163
@param aLba The Logical Block address to read from
sl@0
   164
@param aBuffer The buffer to copy data to
sl@0
   165
@param aLen The number of bytes to be read (IN) and returns the number of bytes
sl@0
   166
actually read (OUT)
sl@0
   167
sl@0
   168
@return TInt KErrNone if successful otherwise KErrCommandFailed to indicate a
sl@0
   169
device status error or KErrArgument to indicate that aLen is too large for the
sl@0
   170
protocol.
sl@0
   171
*/
sl@0
   172
TInt TSbcClientInterface::Read10L(TLba aLba, TDes8& aBuffer, TInt& aLen)
sl@0
   173
    {
sl@0
   174
    __MSFNLOG
sl@0
   175
	__ASSERT_DEBUG(iBlockTransfer.BlockLength(), User::Panic(KUsbMsHostPanicCat, EBlockLengthNotSet));
sl@0
   176
    __ASSERT_DEBUG(aLen % iBlockTransfer.BlockLength() == 0, User::Panic(KUsbMsHostPanicCat, EBlockDevice));
sl@0
   177
sl@0
   178
    TScsiClientRead10Req read10Req;
sl@0
   179
sl@0
   180
    read10Req.iLogicalBlockAddress = aLba;
sl@0
   181
sl@0
   182
    TInt blockTransferLength = aLen / iBlockTransfer.BlockLength();
sl@0
   183
    if (blockTransferLength > static_cast<TInt>(KMaxTUint16))
sl@0
   184
        {
sl@0
   185
        User::Leave(KErrArgument);
sl@0
   186
        }
sl@0
   187
    read10Req.iBlockTransferLength = static_cast<TUint16>(blockTransferLength);
sl@0
   188
    TInt err = iTransport.SendDataRxCmdL(&read10Req, aBuffer, aLen);
sl@0
   189
	return err;
sl@0
   190
    }
sl@0
   191
sl@0
   192
/**
sl@0
   193
Create READ CAPACITY (10) request and send to the transport layer. The request
sl@0
   194
returns the device servers capacity information. The device server's response
sl@0
   195
values are also used here to initialise the TBlockTransfer values. The function
sl@0
   196
leaves if the device response is not compliant with the protocol standard.
sl@0
   197
sl@0
   198
@param aLba The Logical Block Address returned by the LU
sl@0
   199
@param aBlockSize The Block Size returned by the LU
sl@0
   200
sl@0
   201
@return TInt KErrNone if successful, KErrCommandFailed to indicate a
sl@0
   202
device status error, KErrCommandStalled to indicate a device stall
sl@0
   203
*/
sl@0
   204
TInt TSbcClientInterface::ReadCapacity10L(TLba& aLba, TUint32& aBlockSize)
sl@0
   205
    {
sl@0
   206
    __MSFNLOG
sl@0
   207
    TScsiClientReadCapacity10Req capacity10Req;
sl@0
   208
    TScsiClientReadCapacity10Resp capacity10Resp;
sl@0
   209
sl@0
   210
    TInt err = iTransport.SendControlCmdL(&capacity10Req, &capacity10Resp);
sl@0
   211
    if (!err)
sl@0
   212
        {
sl@0
   213
        aLba = capacity10Resp.iLba;
sl@0
   214
        aBlockSize = capacity10Resp.iBlockSize;
sl@0
   215
sl@0
   216
        __SCSIPRINT2(_L("Capacity LBA=0x%08x SIZE=0x%08x"),
sl@0
   217
                     aLba, aBlockSize);
sl@0
   218
sl@0
   219
        iBlockTransfer.SetCapacityL(aBlockSize, aLba);
sl@0
   220
        }
sl@0
   221
	return err;
sl@0
   222
    }
sl@0
   223
sl@0
   224
sl@0
   225
/**
sl@0
   226
Create START STOP UNIT request. The function leaves if the device response is
sl@0
   227
not compliant with the protocol standard.
sl@0
   228
sl@0
   229
@param aStart SCSI START value
sl@0
   230
sl@0
   231
@return TInt KErrNone if successful otherwise KErrCommandFailed to indicate a
sl@0
   232
device status error
sl@0
   233
*/
sl@0
   234
TInt TSbcClientInterface::StartStopUnitL(TBool aStart)
sl@0
   235
    {
sl@0
   236
    __MSFNLOG
sl@0
   237
    TScsiClientStartStopUnitReq startStopUnitReq;
sl@0
   238
sl@0
   239
    startStopUnitReq.iImmed = ETrue;
sl@0
   240
    startStopUnitReq.iLoej = EFalse;
sl@0
   241
    startStopUnitReq.iStart = aStart;
sl@0
   242
sl@0
   243
    TInt err = iTransport.SendControlCmdL(&startStopUnitReq);
sl@0
   244
sl@0
   245
	return err;
sl@0
   246
    }
sl@0
   247
sl@0
   248
sl@0
   249
/**
sl@0
   250
Create WRITE (10) request and send to the transport layer. This performs a
sl@0
   251
logical block write of the device server. Note that TBlockTransfer must be
sl@0
   252
initialised beforehand.  The function leaves if the device response is not
sl@0
   253
compliant with the protocol standard.
sl@0
   254
sl@0
   255
@param aLba Logical Block Address to write the data to
sl@0
   256
@param aBuffer Buffer containing the data
sl@0
   257
@param aPos Offset into the buffer to the data
sl@0
   258
@param aLen The number of bytes to be written (IN) and returns the bytes
sl@0
   259
actually transferred (OUT)
sl@0
   260
sl@0
   261
@return TInt KErrNone if successful otherwise KErrCommandFailed to indicate a
sl@0
   262
device status error or KErrArgument to indicate that aLen is too large for the
sl@0
   263
protocol.
sl@0
   264
*/
sl@0
   265
TInt TSbcClientInterface::Write10L(TLba aLba, TDesC8& aBuffer, TUint aPos, TInt& aLen)
sl@0
   266
    {
sl@0
   267
    __MSFNLOG
sl@0
   268
	__ASSERT_DEBUG(iBlockTransfer.BlockLength(), User::Panic(KUsbMsHostPanicCat, EBlockLengthNotSet));
sl@0
   269
    __ASSERT_DEBUG(aLen % iBlockTransfer.BlockLength() == 0, User::Panic(KUsbMsHostPanicCat, EBlockDevice));
sl@0
   270
sl@0
   271
    // check that buffer size is large enough
sl@0
   272
	if (aBuffer.Length() < (aPos + aLen))
sl@0
   273
		{
sl@0
   274
        User::Leave(KErrArgument);
sl@0
   275
		}
sl@0
   276
sl@0
   277
    TScsiClientWrite10Req write10Req;
sl@0
   278
    write10Req.iLogicalBlockAddress = aLba;
sl@0
   279
sl@0
   280
    TInt blockTransferLength = aLen / iBlockTransfer.BlockLength();
sl@0
   281
    if (blockTransferLength > static_cast<TInt>(KMaxTUint16))
sl@0
   282
        {
sl@0
   283
        User::Leave(KErrArgument);
sl@0
   284
        }
sl@0
   285
    write10Req.iBlockTransferLength = static_cast<TUint16>(blockTransferLength);
sl@0
   286
sl@0
   287
    TInt err = iTransport.SendDataTxCmdL(&write10Req, aBuffer, aPos, aLen);
sl@0
   288
	return err;
sl@0
   289
    }
sl@0
   290