3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 USB_ClassInfo_MS_Device_t
* CallbackMSInterfaceInfo
;
39 void MS_Device_ProcessControlPacket(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 while (!(Endpoint_IsINReady()));
62 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
64 Endpoint_ClearSETUP();
66 Endpoint_Write_Byte(MSInterfaceInfo
->Config
.TotalLUNs
- 1);
70 while (!(Endpoint_IsOUTReceived()));
78 bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
)
80 if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
, EP_TYPE_BULK
,
81 ENDPOINT_DIR_IN
, MSInterfaceInfo
->Config
.DataINEndpointSize
,
82 ENDPOINT_BANK_SINGLE
)))
87 if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
, EP_TYPE_BULK
,
88 ENDPOINT_DIR_OUT
, MSInterfaceInfo
->Config
.DataOUTEndpointSize
,
89 ENDPOINT_BANK_SINGLE
)))
97 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
)
99 if (!(USB_IsConnected
) || !(USB_ConfigurationNumber
))
102 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
);
104 if (Endpoint_IsReadWriteAllowed())
106 if (MS_Device_ReadInCommandBlock(MSInterfaceInfo
))
108 if (MSInterfaceInfo
->State
.CommandBlock
.Flags
& MS_COMMAND_DIR_DATA_IN
)
109 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
);
111 MSInterfaceInfo
->State
.CommandStatus
.Status
= CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo
) ?
112 SCSI_Command_Pass
: SCSI_Command_Fail
;
113 MSInterfaceInfo
->State
.CommandStatus
.Signature
= MS_CSW_SIGNATURE
;
114 MSInterfaceInfo
->State
.CommandStatus
.Tag
= MSInterfaceInfo
->State
.CommandBlock
.Tag
;
115 MSInterfaceInfo
->State
.CommandStatus
.DataTransferResidue
= MSInterfaceInfo
->State
.CommandBlock
.DataTransferLength
;
117 if ((MSInterfaceInfo
->State
.CommandStatus
.Status
== SCSI_Command_Fail
) &&
118 (MSInterfaceInfo
->State
.CommandStatus
.DataTransferResidue
))
120 Endpoint_StallTransaction();
123 MS_Device_ReturnCommandStatus(MSInterfaceInfo
);
125 if (MSInterfaceInfo
->State
.IsMassStoreReset
)
127 Endpoint_ResetFIFO(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
);
128 Endpoint_ResetFIFO(MSInterfaceInfo
->Config
.DataINEndpointNumber
);
130 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
);
131 Endpoint_ClearStall();
132 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
);
133 Endpoint_ClearStall();
138 MSInterfaceInfo
->State
.IsMassStoreReset
= false;
141 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
)
143 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
);
145 CallbackMSInterfaceInfo
= MSInterfaceInfo
;
146 Endpoint_Read_Stream_LE(&MSInterfaceInfo
->State
.CommandBlock
,
147 (sizeof(MS_CommandBlockWrapper_t
) - 16),
148 StreamCallback_MS_Device_AbortOnMassStoreReset
);
150 if ((MSInterfaceInfo
->State
.CommandBlock
.Signature
!= MS_CBW_SIGNATURE
) ||
151 (MSInterfaceInfo
->State
.CommandBlock
.LUN
>= MSInterfaceInfo
->Config
.TotalLUNs
) ||
152 (MSInterfaceInfo
->State
.CommandBlock
.Flags
& 0x1F) ||
153 (MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength
== 0) ||
154 (MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength
> 16))
156 Endpoint_StallTransaction();
157 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
);
158 Endpoint_StallTransaction();
163 CallbackMSInterfaceInfo
= MSInterfaceInfo
;
164 Endpoint_Read_Stream_LE(&MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
,
165 MSInterfaceInfo
->State
.CommandBlock
.SCSICommandLength
,
166 StreamCallback_MS_Device_AbortOnMassStoreReset
);
170 if (MSInterfaceInfo
->State
.IsMassStoreReset
)
176 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
)
178 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataOUTEndpointNumber
);
180 while (Endpoint_IsStalled())
184 if (MSInterfaceInfo
->State
.IsMassStoreReset
)
188 Endpoint_SelectEndpoint(MSInterfaceInfo
->Config
.DataINEndpointNumber
);
190 while (Endpoint_IsStalled())
194 if (MSInterfaceInfo
->State
.IsMassStoreReset
)
198 CallbackMSInterfaceInfo
= MSInterfaceInfo
;
199 Endpoint_Write_Stream_LE(&MSInterfaceInfo
->State
.CommandStatus
, sizeof(MS_CommandStatusWrapper_t
),
200 StreamCallback_MS_Device_AbortOnMassStoreReset
);
204 if (MSInterfaceInfo
->State
.IsMassStoreReset
)
208 static uint8_t StreamCallback_MS_Device_AbortOnMassStoreReset(void)
210 MS_Device_USBTask(CallbackMSInterfaceInfo
);
212 if (CallbackMSInterfaceInfo
->State
.IsMassStoreReset
)
213 return STREAMCALLBACK_Abort
;
215 return STREAMCALLBACK_Continue
;