3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  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 #include "../../HighLevel/USBMode.h" 
  32 #if defined(USB_CAN_BE_DEVICE) 
  34 #define  INCLUDE_FROM_MS_CLASS_DEVICE_C 
  35 #include "MassStorage.h" 
  37 static volatile bool* CallbackIsResetSource
; 
  39 void MS_Device_ProcessControlRequest(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
  41         if (!(Endpoint_IsSETUPReceived())) 
  44         if (USB_ControlRequest
.wIndex 
!= MSInterfaceInfo
->Config
.InterfaceNumber
) 
  47         switch (USB_ControlRequest
.bRequest
) 
  49                 case REQ_MassStorageReset
: 
  50                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
  52                                 Endpoint_ClearSETUP(); 
  54                                 MSInterfaceInfo
->State
.IsMassStoreReset 
= true;                  
  56                                 Endpoint_ClearStatusStage(); 
  61                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
  63                                 Endpoint_ClearSETUP(); 
  65                                 Endpoint_Write_Byte(MSInterfaceInfo
->Config
.TotalLUNs 
- 1);                              
  68                                 Endpoint_ClearStatusStage(); 
  75 bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
  77         memset(&MSInterfaceInfo
->State
, 0x00, sizeof(MSInterfaceInfo
->State
)); 
  79         if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
, EP_TYPE_BULK
, 
  80                                                                  ENDPOINT_DIR_IN
, MSInterfaceInfo
->Config
.DataINEndpointSize
, 
  81                                                                  MSInterfaceInfo
->Config
.DataINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE 
: ENDPOINT_BANK_SINGLE
))) 
  86         if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
, EP_TYPE_BULK
, 
  87                                          ENDPOINT_DIR_OUT
, MSInterfaceInfo
->Config
.DataOUTEndpointSize
, 
  88                                          MSInterfaceInfo
->Config
.DataOUTEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE 
: ENDPOINT_BANK_SINGLE
))) 
  96 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
  98         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 101         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 103         if (Endpoint_IsReadWriteAllowed()) 
 105                 if (MS_Device_ReadInCommandBlock(MSInterfaceInfo
)) 
 107                         if (MSInterfaceInfo
->State
.CommandBlock
.Flags 
& MS_COMMAND_DIR_DATA_IN
) 
 108                           Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 110                         MSInterfaceInfo
->State
.CommandStatus
.Status 
= CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo
) ?
 
 111                                                                                          SCSI_Command_Pass 
: SCSI_Command_Fail
; 
 112                         MSInterfaceInfo
->State
.CommandStatus
.Signature           
= MS_CSW_SIGNATURE
; 
 113                         MSInterfaceInfo
->State
.CommandStatus
.Tag                 
= MSInterfaceInfo
->State
.CommandBlock
.Tag
; 
 114                         MSInterfaceInfo
->State
.CommandStatus
.DataTransferResidue 
= MSInterfaceInfo
->State
.CommandBlock
.DataTransferLength
; 
 116                         if ((MSInterfaceInfo
->State
.CommandStatus
.Status 
== SCSI_Command_Fail
) && 
 117                             (MSInterfaceInfo
->State
.CommandStatus
.DataTransferResidue
)) 
 119                                 Endpoint_StallTransaction(); 
 122                         MS_Device_ReturnCommandStatus(MSInterfaceInfo
); 
 126         if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 128                 Endpoint_ResetFIFO(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 129                 Endpoint_ResetFIFO(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 131                 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 132                 Endpoint_ClearStall(); 
 133                 Endpoint_ResetDataToggle(); 
 134                 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 135                 Endpoint_ClearStall(); 
 136                 Endpoint_ResetDataToggle(); 
 138                 MSInterfaceInfo
->State
.IsMassStoreReset 
= false; 
 142 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 144         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 146         CallbackIsResetSource 
= &MSInterfaceInfo
->State
.IsMassStoreReset
; 
 147         if (Endpoint_Read_Stream_LE(&MSInterfaceInfo
->State
.CommandBlock
, 
 148                                     (sizeof(MS_CommandBlockWrapper_t
) - 16), 
 149                                     StreamCallback_MS_Device_AbortOnMassStoreReset
)) 
 154         if ((MSInterfaceInfo
->State
.CommandBlock
.Signature         
!= MS_CBW_SIGNATURE
)                  || 
 155             (MSInterfaceInfo
->State
.CommandBlock
.LUN               
>= MSInterfaceInfo
->Config
.TotalLUNs
) || 
 156                 (MSInterfaceInfo
->State
.CommandBlock
.Flags              
& 0x1F)                              || 
 157                 (MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength 
== 0)                                 || 
 158                 (MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength 
>  16)) 
 160                 Endpoint_StallTransaction(); 
 161                 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 162                 Endpoint_StallTransaction(); 
 167         CallbackIsResetSource 
= &MSInterfaceInfo
->State
.IsMassStoreReset
; 
 168         if (Endpoint_Read_Stream_LE(&MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
, 
 169                                     MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength
, 
 170                                     StreamCallback_MS_Device_AbortOnMassStoreReset
)) 
 180 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 182         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
); 
 184         while (Endpoint_IsStalled()) 
 186                 #if !defined(INTERRUPT_CONTROL_ENDPOINT) 
 190                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 194         Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
); 
 196         while (Endpoint_IsStalled()) 
 198                 #if !defined(INTERRUPT_CONTROL_ENDPOINT) 
 202                 if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 206         CallbackIsResetSource 
= &MSInterfaceInfo
->State
.IsMassStoreReset
; 
 207         if (Endpoint_Write_Stream_LE(&MSInterfaceInfo
->State
.CommandStatus
, sizeof(MS_CommandStatusWrapper_t
), 
 208                                      StreamCallback_MS_Device_AbortOnMassStoreReset
)) 
 216 static uint8_t StreamCallback_MS_Device_AbortOnMassStoreReset(void) 
 218         #if !defined(INTERRUPT_CONTROL_ENDPOINT) 
 222         if (*CallbackIsResetSource
) 
 223           return STREAMCALLBACK_Abort
; 
 225           return STREAMCALLBACK_Continue
;