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