@@ -21,9 +21,9 @@ func (m *msc) scsiCmdBegin() {
2121 return
2222 }
2323
24- if m .totalBytes > 0 && m .cbw .isOut () {
24+ if m .transferBytes > 0 && m .cbw .isOut () {
2525 // Reject any other multi-packet commands
26- if m .totalBytes > m .maxPacketSize {
26+ if m .transferBytes > m .maxPacketSize {
2727 m .sendScsiError (csw .StatusFailed , scsi .SenseIllegalRequest , scsi .SenseCodeInvalidCmdOpCode )
2828 return
2929 } else {
@@ -53,7 +53,7 @@ func (m *msc) scsiCmdBegin() {
5353 }
5454
5555 if len (m .buf ) == 0 {
56- if m .totalBytes > 0 {
56+ if m .transferBytes > 0 {
5757 // 6.7.2 The Thirteen Cases - Case 4 (Hi > Dn)
5858 // https://usb.org/sites/default/files/usbmassbulk_10.pdf
5959 m .sendScsiError (csw .StatusFailed , scsi .SenseIllegalRequest , 0 )
@@ -63,7 +63,7 @@ func (m *msc) scsiCmdBegin() {
6363 m .state = mscStateStatus
6464 }
6565 } else {
66- if m .totalBytes == 0 {
66+ if m .transferBytes == 0 {
6767 // 6.7.1 The Thirteen Cases - Case 2 (Hn < Di)
6868 // https://usb.org/sites/default/files/usbmassbulk_10.pdf
6969 m .sendScsiError (csw .StatusFailed , scsi .SenseIllegalRequest , 0 )
@@ -93,7 +93,7 @@ func (m *msc) scsiDataTransfer(b []byte) bool {
9393 // Update our sent bytes count to include the just-confirmed bytes
9494 m .sentBytes += m .queuedBytes
9595
96- if m .sentBytes >= m .totalBytes {
96+ if m .sentBytes >= m .transferBytes {
9797 // Transfer complete, send CSW after transfer confirmed
9898 m .state = mscStateStatus
9999 } else if cmdType == scsi .CmdRead {
@@ -158,8 +158,8 @@ func (m *msc) scsiCmdModeSense(cmd scsi.Cmd) {
158158
159159 // The host allows a good amount of leeway in response size
160160 // Reset total bytes to what we'll actually send
161- if m .totalBytes > respLen {
162- m .totalBytes = respLen
161+ if m .transferBytes > respLen {
162+ m .transferBytes = respLen
163163 m .sendZLP = true
164164 }
165165
@@ -210,7 +210,7 @@ func (m *msc) scsiCmdRequestSense() {
210210 // Set the buffer size to the SCSI sense message size and clear
211211 m .resetBuffer (scsi .RequestSenseRespLen )
212212 m .queuedBytes = scsi .RequestSenseRespLen
213- m .totalBytes = scsi .RequestSenseRespLen
213+ m .transferBytes = scsi .RequestSenseRespLen
214214
215215 // 0x70 - current error, 0x71 - deferred error (not used)
216216 m .buf [0 ] = 0xF0 // 0x70 for current error plus 0x80 for valid flag bit
@@ -266,7 +266,7 @@ func (m *msc) scsiQueueTask(cmdType scsi.CmdType, b []byte) bool {
266266 switch cmdType {
267267 case scsi .CmdWrite :
268268 // If we're writing data wait until we have a full write block of data that can be processed.
269- if m .queuedBytes == uint32 (cap (m .blockCache )) {
269+ if m .queuedBytes == uint32 (cap (m .blockCache )) || ( m . sentBytes + m . queuedBytes >= m . transferBytes ) {
270270 m .taskQueued = true
271271 }
272272 case scsi .CmdUnmap :
@@ -279,7 +279,11 @@ func (m *msc) scsiQueueTask(cmdType scsi.CmdType, b []byte) bool {
279279
280280func (m * msc ) sendScsiError (status csw.Status , key scsi.Sense , code scsi.SenseCode ) {
281281 // Generate CSW into m.cswBuf
282- residue := m .totalBytes - m .sentBytes
282+ expected := m .cbw .transferLength ()
283+ residue := uint32 (0 )
284+ if expected > m .sentBytes {
285+ residue = expected - m .sentBytes
286+ }
283287
284288 // Prepare to send CSW
285289 m .sendZLP = true // Ensure the transaction is signaled as ended before a CSW is sent
@@ -291,7 +295,7 @@ func (m *msc) sendScsiError(status csw.Status, key scsi.Sense, code scsi.SenseCo
291295 m .addlSenseCode = code
292296 m .addlSenseQualifier = 0x00 // Not used
293297
294- if m . totalBytes > 0 && residue > 0 {
298+ if expected > 0 && residue > 0 {
295299 if m .cbw .isIn () {
296300 m .stallEndpoint (usb .MSC_ENDPOINT_IN )
297301 } else {
0 commit comments