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                                 Endpoint_Write_8(MSInterfaceInfo
->Config
.TotalLUNs 
- 1); 
  66                                 Endpoint_ClearStatusStage(); 
  73 bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
  75         memset(&MSInterfaceInfo
->State
, 0x00, sizeof(MSInterfaceInfo
->State
)); 
  77         for (uint8_t EndpointNum 
= 1; EndpointNum 
< ENDPOINT_TOTAL_ENDPOINTS
; EndpointNum
++) 
  84                 if (EndpointNum 
== MSInterfaceInfo
->Config
.DataINEndpointNumber
) 
  86                         Size         
= MSInterfaceInfo
->Config
.DataINEndpointSize
; 
  87                         Direction    
= ENDPOINT_DIR_IN
; 
  89                         DoubleBanked 
= MSInterfaceInfo
->Config
.DataINEndpointDoubleBank
; 
  91                 else if (EndpointNum 
== MSInterfaceInfo
->Config
.DataOUTEndpointNumber
) 
  93                         Size         
= MSInterfaceInfo
->Config
.DataOUTEndpointSize
; 
  94                         Direction    
= ENDPOINT_DIR_OUT
; 
  96                         DoubleBanked 
= MSInterfaceInfo
->Config
.DataOUTEndpointDoubleBank
; 
 103                 if (!(Endpoint_ConfigureEndpoint(EndpointNum
, Type
, Direction
, Size
, 
 104                                                  DoubleBanked ? ENDPOINT_BANK_DOUBLE 
: ENDPOINT_BANK_SINGLE
))) 
 113 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 115         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 118         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 120         if (Endpoint_IsReadWriteAllowed()) 
 122                 if (MS_Device_ReadInCommandBlock(MSInterfaceInfo
)) 
 124                         if (MSInterfaceInfo
->State
.CommandBlock
.Flags 
& MS_COMMAND_DIR_DATA_IN
) 
 125                           Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 127                         MSInterfaceInfo
->State
.CommandStatus
.Status 
= CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo
) ?
 
 128                                                                        MS_SCSI_COMMAND_Pass 
: MS_SCSI_COMMAND_Fail
; 
 129                         MSInterfaceInfo
->State
.CommandStatus
.Signature           
= MS_CSW_SIGNATURE
; 
 130                         MSInterfaceInfo
->State
.CommandStatus
.Tag                 
= MSInterfaceInfo
->State
.CommandBlock
.Tag
; 
 131                         MSInterfaceInfo
->State
.CommandStatus
.DataTransferResidue 
= MSInterfaceInfo
->State
.CommandBlock
.DataTransferLength
; 
 133                         if ((MSInterfaceInfo
->State
.CommandStatus
.Status 
== MS_SCSI_COMMAND_Fail
) && 
 134                             (MSInterfaceInfo
->State
.CommandStatus
.DataTransferResidue
)) 
 136                                 Endpoint_StallTransaction(); 
 139                         MS_Device_ReturnCommandStatus(MSInterfaceInfo
); 
 143         if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 145                 Endpoint_ResetEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 146                 Endpoint_ResetEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 148                 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 149                 Endpoint_ClearStall(); 
 150                 Endpoint_ResetDataToggle(); 
 151                 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 152                 Endpoint_ClearStall(); 
 153                 Endpoint_ResetDataToggle(); 
 155                 MSInterfaceInfo
->State
.IsMassStoreReset 
= false; 
 159 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 161         uint16_t BytesProcessed
; 
 163         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 166         while (Endpoint_Read_Stream_LE(&MSInterfaceInfo
->State
.CommandBlock
, 
 167                                        (sizeof(MS_CommandBlockWrapper_t
) - 16), &BytesProcessed
) == 
 168                                        ENDPOINT_RWSTREAM_IncompleteTransfer
) 
 170                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 174         if ((MSInterfaceInfo
->State
.CommandBlock
.Signature         
!= MS_CBW_SIGNATURE
)                  || 
 175             (MSInterfaceInfo
->State
.CommandBlock
.LUN               
>= MSInterfaceInfo
->Config
.TotalLUNs
) || 
 176                 (MSInterfaceInfo
->State
.CommandBlock
.Flags              
& 0x1F)                              || 
 177                 (MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength 
== 0)                                 || 
 178                 (MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength 
>  16)) 
 180                 Endpoint_StallTransaction(); 
 181                 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 182                 Endpoint_StallTransaction(); 
 188         while (Endpoint_Read_Stream_LE(&MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
, 
 189                                         MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength
, &BytesProcessed
) == 
 190                                         ENDPOINT_RWSTREAM_IncompleteTransfer
) 
 192                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 201 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 203         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 205         while (Endpoint_IsStalled()) 
 207                 #if !defined(INTERRUPT_CONTROL_ENDPOINT) 
 211                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 215         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 217         while (Endpoint_IsStalled()) 
 219                 #if !defined(INTERRUPT_CONTROL_ENDPOINT) 
 223                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 227         uint16_t BytesProcessed 
= 0; 
 228         while (Endpoint_Write_Stream_LE(&MSInterfaceInfo
->State
.CommandStatus
, 
 229                                         sizeof(MS_CommandStatusWrapper_t
), &BytesProcessed
) == 
 230                                         ENDPOINT_RWSTREAM_IncompleteTransfer
) 
 232                 if (MSInterfaceInfo
->State
.IsMassStoreReset
)