Example #1
0
//!
//! @brief USB Command Block Wrapper (CBW) management
//!
//! This function decodes the CBW command and stores the SCSI command.
//!
static void usb_mass_storage_cbw(void)
{
  bool cbw_error;

  Usb_reset_endpoint_fifo_access(EP_MS_OUT);

  //! Check if dCBWSignature is correct
  cbw_error = (Usb_read_endpoint_data(EP_MS_OUT, 32) != *(U32 *)&"USBC");

  //! Store CBW Tag to be repeated in CSW
  dCBWTag = Usb_read_endpoint_data(EP_MS_OUT, 32);

  g_scsi_data_remaining = Usb_read_endpoint_data(EP_MS_OUT, 32);
  g_scsi_data_remaining = usb_format_usb_to_mcu_data(32, g_scsi_data_remaining);

  //! if (bmCBWFlags.bit7 == 1) {direction = IN;}
  if (Usb_read_endpoint_data(EP_MS_OUT, 8))
  {
    ms_endpoint = EP_MS_IN;
    if (cbw_error)
    {
      Usb_ack_out_received_free(EP_MS_OUT);
      Usb_enable_stall_handshake(EP_MS_IN);
      return;
    }
  }
  else
  {
    ms_endpoint = EP_MS_OUT;
    if (cbw_error)
    {
      Usb_enable_stall_handshake(EP_MS_OUT);
      Usb_ack_out_received_free(EP_MS_OUT);
      return;
    }
  }

  usb_LUN = Usb_read_endpoint_data(EP_MS_OUT, 8);

  if (!ms_multiple_drive)
  {
    usb_LUN = get_cur_lun();
  }

  //! Dummy CBWCBLength read
  Usb_read_endpoint_data(EP_MS_OUT, 8);

  //! Store scsi_command
  usb_read_ep_rxpacket(EP_MS_OUT, g_scsi_command, sizeof(g_scsi_command), NULL);

  Usb_ack_out_received_free(EP_MS_OUT);

  // Take the USB Mutex(i.e. we're allowed to perform a ms cmd).
  /* if( ( pdFALSE == ( xGiveUsbMutex = x_supervisor_SemaphoreTake( xUSBMutex, 0 ) ) ) || ( !scsi_decode_command() && g_scsi_data_remaining ) ) */
  if( ( pdFALSE == (xGiveUsbMutex = x_supervisor_SemaphoreTake( xUSBMutex, 0 ) ) )
      || ( !scsi_decode_command() && g_scsi_data_remaining ) )
  {
    Usb_enable_stall_handshake(ms_endpoint);
  }
}
// !
// ! @brief USB Command Block Wrapper (CBW) management
// !
// ! This function decodes the CBW command and stores the SCSI command.
// !
static void usb_mass_storage_cbw (void)
{
    Bool cbw_error;

    Usb_reset_endpoint_fifo_access (EP_MS_OUT);

    // ! Check if dCBWSignature is correct
    cbw_error = (Usb_read_endpoint_data (EP_MS_OUT, 32) != *(U32 *) & "USBC");

    // ! Store CBW Tag to be repeated in CSW
    dCBWTag = Usb_read_endpoint_data (EP_MS_OUT, 32);

    g_scsi_data_remaining = Usb_read_endpoint_data (EP_MS_OUT, 32);
    g_scsi_data_remaining = usb_format_usb_to_mcu_data (32, g_scsi_data_remaining);

    /* Show the remaining bytes { U8 Text_u8[20]; CI_StringOut (" - "); itoa ((S32)g_scsi_data_remaining,Text_u8); CI_StringOut (Text_u8);
       CI_StringOut (" - "); } */
    // ! if (bmCBWFlags.bit7 == 1) {direction = IN;}
    if (Usb_read_endpoint_data (EP_MS_OUT, 8))
    {
        ms_endpoint = EP_MS_IN;
        if (cbw_error)
        {
            Usb_ack_out_received_free (EP_MS_OUT);
            Usb_enable_stall_handshake (EP_MS_IN);
            return;
        }
    }
    else
    {
        ms_endpoint = EP_MS_OUT;
        if (cbw_error)
        {
            Usb_enable_stall_handshake (EP_MS_OUT);
            Usb_ack_out_received_free (EP_MS_OUT);
            return;
        }
    }

    usb_LUN = Usb_read_endpoint_data (EP_MS_OUT, 8);

    if (!ms_multiple_drive)
    {
        usb_LUN = get_cur_lun ();
    }

    // ! Dummy CBWCBLength read
    Usb_read_endpoint_data (EP_MS_OUT, 8);

    // ! Store scsi_command
    usb_read_ep_rxpacket (EP_MS_OUT, g_scsi_command, sizeof (g_scsi_command), NULL);

    Usb_ack_out_received_free (EP_MS_OUT);

    if (!scsi_decode_command ())
    {
        Usb_enable_stall_handshake (ms_endpoint);
    }
}