3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  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 disclaim 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 "MassStorage.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         for (uint8_t EndpointNum 
= 1; EndpointNum 
< ENDPOINT_TOTAL_ENDPOINTS
; EndpointNum
++) 
  85                 if (EndpointNum 
== MSInterfaceInfo
->Config
.DataINEndpointNumber
) 
  87                         Size         
= MSInterfaceInfo
->Config
.DataINEndpointSize
; 
  88                         Direction    
= ENDPOINT_DIR_IN
; 
  90                         DoubleBanked 
= MSInterfaceInfo
->Config
.DataINEndpointDoubleBank
; 
  92                 else if (EndpointNum 
== MSInterfaceInfo
->Config
.DataOUTEndpointNumber
) 
  94                         Size         
= MSInterfaceInfo
->Config
.DataOUTEndpointSize
; 
  95                         Direction    
= ENDPOINT_DIR_OUT
; 
  97                         DoubleBanked 
= MSInterfaceInfo
->Config
.DataOUTEndpointDoubleBank
; 
 104                 if (!(Endpoint_ConfigureEndpoint(EndpointNum
, Type
, Direction
, Size
, 
 105                                                  DoubleBanked ? ENDPOINT_BANK_DOUBLE 
: ENDPOINT_BANK_SINGLE
))) 
 114 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 116         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 119         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 121         if (Endpoint_IsReadWriteAllowed()) 
 123                 if (MS_Device_ReadInCommandBlock(MSInterfaceInfo
)) 
 125                         if (MSInterfaceInfo
->State
.CommandBlock
.Flags 
& MS_COMMAND_DIR_DATA_IN
) 
 126                           Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 128                         bool SCSICommandResult 
= CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo
); 
 130                         MSInterfaceInfo
->State
.CommandStatus
.Status              
= (SCSICommandResult
) ? MS_SCSI_COMMAND_Pass 
: MS_SCSI_COMMAND_Fail
; 
 131                         MSInterfaceInfo
->State
.CommandStatus
.Signature           
= CPU_TO_LE32(MS_CSW_SIGNATURE
); 
 132                         MSInterfaceInfo
->State
.CommandStatus
.Tag                 
= MSInterfaceInfo
->State
.CommandBlock
.Tag
; 
 133                         MSInterfaceInfo
->State
.CommandStatus
.DataTransferResidue 
= MSInterfaceInfo
->State
.CommandBlock
.DataTransferLength
; 
 135                         if (!(SCSICommandResult
) && (le32_to_cpu(MSInterfaceInfo
->State
.CommandStatus
.DataTransferResidue
))) 
 136                           Endpoint_StallTransaction(); 
 138                         MS_Device_ReturnCommandStatus(MSInterfaceInfo
); 
 142         if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 144                 Endpoint_ResetEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 145                 Endpoint_ResetEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 147                 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 148                 Endpoint_ClearStall(); 
 149                 Endpoint_ResetDataToggle(); 
 150                 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 151                 Endpoint_ClearStall(); 
 152                 Endpoint_ResetDataToggle(); 
 154                 MSInterfaceInfo
->State
.IsMassStoreReset 
= false; 
 158 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 160         uint16_t BytesProcessed
; 
 162         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 165         while (Endpoint_Read_Stream_LE(&MSInterfaceInfo
->State
.CommandBlock
, 
 166                                        (sizeof(MS_CommandBlockWrapper_t
) - 16), &BytesProcessed
) == 
 167                                        ENDPOINT_RWSTREAM_IncompleteTransfer
) 
 169                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 173         if ((MSInterfaceInfo
->State
.CommandBlock
.Signature         
!= CPU_TO_LE32(MS_CBW_SIGNATURE
))     || 
 174             (MSInterfaceInfo
->State
.CommandBlock
.LUN               
>= MSInterfaceInfo
->Config
.TotalLUNs
) || 
 175                 (MSInterfaceInfo
->State
.CommandBlock
.Flags              
& 0x1F)                              || 
 176                 (MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength 
== 0)                                 || 
 177                 (MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength 
>  16)) 
 179                 Endpoint_StallTransaction(); 
 180                 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 181                 Endpoint_StallTransaction(); 
 187         while (Endpoint_Read_Stream_LE(&MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
, 
 188                                         MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength
, &BytesProcessed
) == 
 189                                         ENDPOINT_RWSTREAM_IncompleteTransfer
) 
 191                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 200 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 202         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 204         while (Endpoint_IsStalled()) 
 206                 #if !defined(INTERRUPT_CONTROL_ENDPOINT) 
 210                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 214         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 216         while (Endpoint_IsStalled()) 
 218                 #if !defined(INTERRUPT_CONTROL_ENDPOINT) 
 222                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 226         uint16_t BytesProcessed 
= 0; 
 227         while (Endpoint_Write_Stream_LE(&MSInterfaceInfo
->State
.CommandStatus
, 
 228                                         sizeof(MS_CommandStatusWrapper_t
), &BytesProcessed
) == 
 229                                         ENDPOINT_RWSTREAM_IncompleteTransfer
) 
 231                 if (MSInterfaceInfo
->State
.IsMassStoreReset
)