فهرست منبع

msc device: correct handling tud_msc_write10_cb returned value

separate proc write10 and read10
hathach 8 سال پیش
والد
کامیت
fa2e043126
1فایلهای تغییر یافته به همراه81 افزوده شده و 29 حذف شده
  1. 81 29
      tinyusb/class/msc/msc_device.c

+ 81 - 29
tinyusb/class/msc/msc_device.c

@@ -82,7 +82,8 @@ CFG_TUSB_ATTR_USBRAM CFG_TUSB_MEM_ALIGN static uint8_t _mscd_buf[CFG_TUD_MSC_BUF
 //--------------------------------------------------------------------+
 //--------------------------------------------------------------------+
 // INTERNAL OBJECT & FUNCTION DECLARATION
 // INTERNAL OBJECT & FUNCTION DECLARATION
 //--------------------------------------------------------------------+
 //--------------------------------------------------------------------+
-static void proc_read10_write10(uint8_t rhport, mscd_interface_t* p_msc);
+static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc);
+static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc);
 
 
 static inline uint32_t rdwr10_get_lba(uint8_t const command[])
 static inline uint32_t rdwr10_get_lba(uint8_t const command[])
 {
 {
@@ -210,9 +211,13 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u
       p_msc->data_len    = p_cbw->xfer_bytes;
       p_msc->data_len    = p_cbw->xfer_bytes;
       p_msc->xferred_len = 0;
       p_msc->xferred_len = 0;
 
 
-      if ( (SCSI_CMD_READ_10 == p_cbw->command[0]) || (SCSI_CMD_WRITE_10 == p_cbw->command[0]) )
+      if (SCSI_CMD_READ_10 == p_cbw->command[0])
       {
       {
-        proc_read10_write10(rhport, p_msc);
+        proc_read10_cmd(rhport, p_msc);
+      }
+      else if (SCSI_CMD_WRITE_10 == p_cbw->command[0])
+      {
+        proc_write10_cmd(rhport, p_msc);
       }
       }
       else
       else
       {
       {
@@ -260,19 +265,51 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u
       // OUT transfer, invoke callback if needed
       // OUT transfer, invoke callback if needed
       if ( !BIT_TEST_(p_cbw->dir, 7) )
       if ( !BIT_TEST_(p_cbw->dir, 7) )
       {
       {
-        if ( SCSI_CMD_WRITE_10 == p_cbw->command[0] )
+        if ( SCSI_CMD_WRITE_10 != p_cbw->command[0] )
         {
         {
-          uint32_t lba = rdwr10_get_lba(p_cbw->command);
-
-          tud_msc_write10_cb(rhport, p_cbw->lun, lba, p_msc->xferred_len, _mscd_buf, xferred_bytes);
+          p_csw->status = (tud_msc_scsi_cb(rhport, p_cbw->lun, p_cbw->command, _mscd_buf, p_msc->data_len) >= 0 ) ? MSC_CSW_STATUS_PASSED : MSC_CSW_STATUS_FAILED;
         }
         }
         else
         else
         {
         {
-          p_csw->status = (tud_msc_scsi_cb(rhport, p_cbw->lun, p_cbw->command, _mscd_buf, p_msc->data_len) >= 0 ) ? MSC_CSW_STATUS_PASSED : MSC_CSW_STATUS_FAILED;
+          uint32_t lba = rdwr10_get_lba(p_cbw->command);
+
+          // Application can consume smaller bytes
+          int32_t nbytes = tud_msc_write10_cb(rhport, p_cbw->lun, lba, p_msc->xferred_len, _mscd_buf, xferred_bytes);
+
+          if ( nbytes < 0 )
+          {
+            // negative means error -> skip to status phase, status in CSW set to failed
+            p_csw->data_residue = p_cbw->xfer_bytes - p_msc->xferred_len;
+            p_csw->status       = MSC_CSW_STATUS_FAILED;
+
+            p_msc->stage = MSC_STAGE_STATUS;
+            break;
+          }else
+          {
+            // Application consume less than what we got (including zero)
+            if ( nbytes < xferred_bytes )
+            {
+              if ( nbytes > 0 )
+              {
+                p_msc->xferred_len += nbytes;
+                memmove(_mscd_buf, _mscd_buf+nbytes, xferred_bytes-nbytes);
+              }
+
+              // simulate an transfer complete with adjusted params
+              dcd_xfer_complete(rhport, p_msc->ep_out, xferred_bytes-nbytes, true);
+
+              return TUSB_ERROR_NONE; // skip the rest
+            }
+            else
+            {
+              // Application consume all bytes in our buffer
+              // Nothing to do, process with normal flow
+            }
+          }
         }
         }
       }
       }
 
 
-      /*------------- Prepare for DATA transfer if not complete yet -------------*/
+      // Accumulate data so far
       p_msc->xferred_len += xferred_bytes;
       p_msc->xferred_len += xferred_bytes;
 
 
       if ( p_msc->xferred_len >= p_msc->data_len )
       if ( p_msc->xferred_len >= p_msc->data_len )
@@ -282,10 +319,15 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u
       }
       }
       else
       else
       {
       {
-        if ( (SCSI_CMD_READ_10 == p_cbw->command[0]) || (SCSI_CMD_WRITE_10 == p_cbw->command[0]) )
+        // READ10 & WRITE10 Can be executed with large bulk of data e.g write 8K bytes (several flash write)
+        // We break it into multiple smaller command whose data size is up to CFG_TUD_MSC_BUFSIZE
+        if (SCSI_CMD_READ_10 == p_cbw->command[0])
+        {
+          proc_read10_cmd(rhport, p_msc);
+        }
+        else if (SCSI_CMD_WRITE_10 == p_cbw->command[0])
         {
         {
-          // Can be executed several times e.g write 8K bytes (several flash write)
-          proc_read10_write10(rhport, p_msc);
+          proc_write10_cmd(rhport, p_msc);
         }else
         }else
         {
         {
           // No other command take more than one transfer yet -> unlikely error
           // No other command take more than one transfer yet -> unlikely error
@@ -324,42 +366,52 @@ tusb_error_t mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, u
   return TUSB_ERROR_NONE;
   return TUSB_ERROR_NONE;
 }
 }
 
 
-static void proc_read10_write10(uint8_t rhport, mscd_interface_t* p_msc)
+static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
 {
 {
   msc_cbw_t const * p_cbw = &p_msc->cbw;
   msc_cbw_t const * p_cbw = &p_msc->cbw;
   msc_csw_t       * p_csw = &p_msc->csw;
   msc_csw_t       * p_csw = &p_msc->csw;
 
 
-  uint8_t const ep_data = BIT_TEST_(p_cbw->dir, 7) ? p_msc->ep_in : p_msc->ep_out;
-
-  // LBA and Block count are in Big Endian. Use memcpy first to prevent mis-aligned access
   uint32_t lba = rdwr10_get_lba(p_cbw->command);
   uint32_t lba = rdwr10_get_lba(p_cbw->command);
   uint16_t block_count = rdwr10_get_blockcount(p_cbw->command);
   uint16_t block_count = rdwr10_get_blockcount(p_cbw->command);
 
 
-  int32_t xfer_bytes = (int32_t) min32_of(sizeof(_mscd_buf), p_cbw->xfer_bytes-p_msc->xferred_len);
+  // remaining bytes capped at class buffer
+  int32_t nbytes = (int32_t) min32_of(sizeof(_mscd_buf), p_cbw->xfer_bytes-p_msc->xferred_len);
 
 
-  // Write10 callback will be called later when usb transfer complete
-  if (SCSI_CMD_READ_10 == p_cbw->command[0])
-  {
-    xfer_bytes = tud_msc_read10_cb (rhport, p_cbw->lun, lba, p_msc->xferred_len, _mscd_buf, (uint32_t) xfer_bytes);
-  }
+  // Application can consume smaller bytes
+  nbytes = tud_msc_read10_cb (rhport, p_cbw->lun, lba, p_msc->xferred_len, _mscd_buf, (uint32_t) nbytes);
 
 
-  if ( xfer_bytes < 0 )
+  if ( nbytes < 0 )
   {
   {
-    // negative is error -> pipe is stalled & status in CSW set to failed
+    // negative means error -> pipe is stalled & status in CSW set to failed
     p_csw->data_residue = p_cbw->xfer_bytes - p_msc->xferred_len;
     p_csw->data_residue = p_cbw->xfer_bytes - p_msc->xferred_len;
     p_csw->status       = MSC_CSW_STATUS_FAILED;
     p_csw->status       = MSC_CSW_STATUS_FAILED;
 
 
-    dcd_edpt_stall(rhport, ep_data);
+    dcd_edpt_stall(rhport, p_msc->ep_in);
   }
   }
-  else if ( xfer_bytes == 0 )
+  else if ( nbytes == 0 )
   {
   {
-    // zero is not ready -> try again later by simulate an transfer complete
-    dcd_xfer_complete(rhport, ep_data, 0, true);
+    // zero means not ready -> try again later by simulate an transfer complete
+    dcd_xfer_complete(rhport, p_msc->ep_in, 0, true);
   }
   }
   else
   else
   {
   {
-    TU_ASSERT( dcd_edpt_xfer(rhport, ep_data, _mscd_buf, xfer_bytes), );
+    TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_in, _mscd_buf, nbytes), );
   }
   }
 }
 }
 
 
+static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
+{
+  msc_cbw_t const * p_cbw = &p_msc->cbw;
+  msc_csw_t       * p_csw = &p_msc->csw;
+
+  uint32_t lba = rdwr10_get_lba(p_cbw->command);
+  uint16_t block_count = rdwr10_get_blockcount(p_cbw->command);
+
+  // remaining bytes capped at class buffer
+  int32_t nbytes = (int32_t) min32_of(sizeof(_mscd_buf), p_cbw->xfer_bytes-p_msc->xferred_len);
+
+  // Write10 callback will be called later when usb transfer complete
+  TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, nbytes), );
+}
+
 #endif
 #endif