3      Copyright (C) Dean Camera, 2013. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2013  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this 
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in 
  15   all copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaims all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  31 #define  __INCLUDE_FROM_USB_DRIVER 
  32 #include "../../Core/USBMode.h" 
  34 #if defined(USB_CAN_BE_DEVICE) 
  36 #define  __INCLUDE_FROM_MS_DRIVER 
  37 #define  __INCLUDE_FROM_MASSSTORAGE_DEVICE_C 
  38 #include "MassStorageClassDevice.h" 
  40 void MS_Device_ProcessControlRequest(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
  42         if (!(Endpoint_IsSETUPReceived())) 
  45         if (USB_ControlRequest
.wIndex 
!= MSInterfaceInfo
->Config
.InterfaceNumber
) 
  48         switch (USB_ControlRequest
.bRequest
) 
  50                 case MS_REQ_MassStorageReset
: 
  51                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
  53                                 Endpoint_ClearSETUP(); 
  54                                 Endpoint_ClearStatusStage(); 
  56                                 MSInterfaceInfo
->State
.IsMassStoreReset 
= true; 
  60                 case MS_REQ_GetMaxLUN
: 
  61                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
  63                                 Endpoint_ClearSETUP(); 
  64                                 while (!(Endpoint_IsINReady())); 
  65                                 Endpoint_Write_8(MSInterfaceInfo
->Config
.TotalLUNs 
- 1); 
  67                                 Endpoint_ClearStatusStage(); 
  74 bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
  76         memset(&MSInterfaceInfo
->State
, 0x00, sizeof(MSInterfaceInfo
->State
)); 
  78         MSInterfaceInfo
->Config
.DataINEndpoint
.Type  
= EP_TYPE_BULK
; 
  79         MSInterfaceInfo
->Config
.DataOUTEndpoint
.Type 
= EP_TYPE_BULK
; 
  81         if (!(Endpoint_ConfigureEndpointTable(&MSInterfaceInfo
->Config
.DataINEndpoint
, 1))) 
  84         if (!(Endpoint_ConfigureEndpointTable(&MSInterfaceInfo
->Config
.DataOUTEndpoint
, 1))) 
  90 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
  92         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
  95         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpoint
.Address
); 
  97         if (Endpoint_IsOUTReceived()) 
  99                 if (MS_Device_ReadInCommandBlock(MSInterfaceInfo
)) 
 101                         if (MSInterfaceInfo
->State
.CommandBlock
.Flags 
& MS_COMMAND_DIR_DATA_IN
) 
 102                           Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpoint
.Address
); 
 104                         bool SCSICommandResult 
= CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo
); 
 106                         MSInterfaceInfo
->State
.CommandStatus
.Status              
= (SCSICommandResult
) ? MS_SCSI_COMMAND_Pass 
: MS_SCSI_COMMAND_Fail
; 
 107                         MSInterfaceInfo
->State
.CommandStatus
.Signature           
= CPU_TO_LE32(MS_CSW_SIGNATURE
); 
 108                         MSInterfaceInfo
->State
.CommandStatus
.Tag                 
= MSInterfaceInfo
->State
.CommandBlock
.Tag
; 
 109                         MSInterfaceInfo
->State
.CommandStatus
.DataTransferResidue 
= MSInterfaceInfo
->State
.CommandBlock
.DataTransferLength
; 
 111                         if (!(SCSICommandResult
) && (le32_to_cpu(MSInterfaceInfo
->State
.CommandStatus
.DataTransferResidue
))) 
 112                           Endpoint_StallTransaction(); 
 114                         MS_Device_ReturnCommandStatus(MSInterfaceInfo
); 
 118         if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 120                 Endpoint_ResetEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpoint
.Address
); 
 121                 Endpoint_ResetEndpoint(MSInterfaceInfo
->Config
.DataINEndpoint
.Address
); 
 123                 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpoint
.Address
); 
 124                 Endpoint_ClearStall(); 
 125                 Endpoint_ResetDataToggle(); 
 126                 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpoint
.Address
); 
 127                 Endpoint_ClearStall(); 
 128                 Endpoint_ResetDataToggle(); 
 130                 MSInterfaceInfo
->State
.IsMassStoreReset 
= false; 
 134 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 136         uint16_t BytesProcessed
; 
 138         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpoint
.Address
); 
 141         while (Endpoint_Read_Stream_LE(&MSInterfaceInfo
->State
.CommandBlock
, 
 142                                        (sizeof(MS_CommandBlockWrapper_t
) - 16), &BytesProcessed
) == 
 143                                        ENDPOINT_RWSTREAM_IncompleteTransfer
) 
 145                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 149         if ((MSInterfaceInfo
->State
.CommandBlock
.Signature         
!= CPU_TO_LE32(MS_CBW_SIGNATURE
))     || 
 150             (MSInterfaceInfo
->State
.CommandBlock
.LUN               
>= MSInterfaceInfo
->Config
.TotalLUNs
) || 
 151                 (MSInterfaceInfo
->State
.CommandBlock
.Flags              
& 0x1F)                              || 
 152                 (MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength 
== 0)                                 || 
 153                 (MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength 
>  16)) 
 155                 Endpoint_StallTransaction(); 
 156                 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpoint
.Address
); 
 157                 Endpoint_StallTransaction(); 
 163         while (Endpoint_Read_Stream_LE(&MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
, 
 164                                         MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength
, &BytesProcessed
) == 
 165                                         ENDPOINT_RWSTREAM_IncompleteTransfer
) 
 167                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 176 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 178         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpoint
.Address
); 
 180         while (Endpoint_IsStalled()) 
 182                 #if !defined(INTERRUPT_CONTROL_ENDPOINT) 
 186                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 190         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpoint
.Address
); 
 192         while (Endpoint_IsStalled()) 
 194                 #if !defined(INTERRUPT_CONTROL_ENDPOINT) 
 198                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 202         uint16_t BytesProcessed 
= 0; 
 203         while (Endpoint_Write_Stream_LE(&MSInterfaceInfo
->State
.CommandStatus
, 
 204                                         sizeof(MS_CommandStatusWrapper_t
), &BytesProcessed
) == 
 205                                         ENDPOINT_RWSTREAM_IncompleteTransfer
) 
 207                 if (MSInterfaceInfo
->State
.IsMassStoreReset
)