]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/LUFA/Drivers/USB/Class/Device/MassStorageClassDevice.c
Merge commit '5a0132f1c1c9a14fd2941f0a5e29bbf5e31da20c' into master-core-pull
[max/tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / LUFA / Drivers / USB / Class / Device / MassStorageClassDevice.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
21   The author disclaims 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
28   this software.
29 */
30
31 #define  __INCLUDE_FROM_USB_DRIVER
32 #include "../../Core/USBMode.h"
33
34 #if defined(USB_CAN_BE_DEVICE)
35
36 #define  __INCLUDE_FROM_MS_DRIVER
37 #define  __INCLUDE_FROM_MASSSTORAGE_DEVICE_C
38 #include "MassStorageClassDevice.h"
39
40 void MS_Device_ProcessControlRequest(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
41 {
42         if (!(Endpoint_IsSETUPReceived()))
43           return;
44
45         if (USB_ControlRequest.wIndex != MSInterfaceInfo->Config.InterfaceNumber)
46           return;
47
48         switch (USB_ControlRequest.bRequest)
49         {
50                 case MS_REQ_MassStorageReset:
51                         if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
52                         {
53                                 Endpoint_ClearSETUP();
54                                 Endpoint_ClearStatusStage();
55
56                                 MSInterfaceInfo->State.IsMassStoreReset = true;
57                         }
58
59                         break;
60                 case MS_REQ_GetMaxLUN:
61                         if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
62                         {
63                                 Endpoint_ClearSETUP();
64                                 while (!(Endpoint_IsINReady()));
65                                 Endpoint_Write_8(MSInterfaceInfo->Config.TotalLUNs - 1);
66                                 Endpoint_ClearIN();
67                                 Endpoint_ClearStatusStage();
68                         }
69
70                         break;
71         }
72 }
73
74 bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
75 {
76         memset(&MSInterfaceInfo->State, 0x00, sizeof(MSInterfaceInfo->State));
77
78         MSInterfaceInfo->Config.DataINEndpoint.Type  = EP_TYPE_BULK;
79         MSInterfaceInfo->Config.DataOUTEndpoint.Type = EP_TYPE_BULK;
80
81         if (!(Endpoint_ConfigureEndpointTable(&MSInterfaceInfo->Config.DataINEndpoint, 1)))
82           return false;
83
84         if (!(Endpoint_ConfigureEndpointTable(&MSInterfaceInfo->Config.DataOUTEndpoint, 1)))
85           return false;
86
87         return true;
88 }
89
90 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
91 {
92         if (USB_DeviceState != DEVICE_STATE_Configured)
93           return;
94
95         Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpoint.Address);
96
97         if (Endpoint_IsOUTReceived())
98         {
99                 if (MS_Device_ReadInCommandBlock(MSInterfaceInfo))
100                 {
101                         if (MSInterfaceInfo->State.CommandBlock.Flags & MS_COMMAND_DIR_DATA_IN)
102                           Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpoint.Address);
103
104                         bool SCSICommandResult = CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo);
105
106                         MSInterfaceInfo->State.CommandStatus.Status              = (SCSICommandResult) ? MS_SCSI_COMMAND_Pass : MS_SCSI_COMMAND_Fail;
107                         MSInterfaceInfo->State.CommandStatus.Signature           = CPU_TO_LE32(MS_CSW_SIGNATURE);
108                         MSInterfaceInfo->State.CommandStatus.Tag                 = MSInterfaceInfo->State.CommandBlock.Tag;
109                         MSInterfaceInfo->State.CommandStatus.DataTransferResidue = MSInterfaceInfo->State.CommandBlock.DataTransferLength;
110
111                         if (!(SCSICommandResult) && (le32_to_cpu(MSInterfaceInfo->State.CommandStatus.DataTransferResidue)))
112                           Endpoint_StallTransaction();
113
114                         MS_Device_ReturnCommandStatus(MSInterfaceInfo);
115                 }
116         }
117
118         if (MSInterfaceInfo->State.IsMassStoreReset)
119         {
120                 Endpoint_ResetEndpoint(MSInterfaceInfo->Config.DataOUTEndpoint.Address);
121                 Endpoint_ResetEndpoint(MSInterfaceInfo->Config.DataINEndpoint.Address);
122
123                 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpoint.Address);
124                 Endpoint_ClearStall();
125                 Endpoint_ResetDataToggle();
126                 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpoint.Address);
127                 Endpoint_ClearStall();
128                 Endpoint_ResetDataToggle();
129
130                 MSInterfaceInfo->State.IsMassStoreReset = false;
131         }
132 }
133
134 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
135 {
136         uint16_t BytesProcessed;
137
138         Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpoint.Address);
139
140         BytesProcessed = 0;
141         while (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock,
142                                        (sizeof(MS_CommandBlockWrapper_t) - 16), &BytesProcessed) ==
143                                        ENDPOINT_RWSTREAM_IncompleteTransfer)
144         {
145                 if (MSInterfaceInfo->State.IsMassStoreReset)
146                   return false;
147         }
148
149         if ((MSInterfaceInfo->State.CommandBlock.Signature         != CPU_TO_LE32(MS_CBW_SIGNATURE))     ||
150             (MSInterfaceInfo->State.CommandBlock.LUN               >= MSInterfaceInfo->Config.TotalLUNs) ||
151                 (MSInterfaceInfo->State.CommandBlock.Flags              & 0x1F)                              ||
152                 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength == 0)                                 ||
153                 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength >  16))
154         {
155                 Endpoint_StallTransaction();
156                 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpoint.Address);
157                 Endpoint_StallTransaction();
158
159                 return false;
160         }
161
162         BytesProcessed = 0;
163         while (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData,
164                                         MSInterfaceInfo->State.CommandBlock.SCSICommandLength, &BytesProcessed) ==
165                                         ENDPOINT_RWSTREAM_IncompleteTransfer)
166         {
167                 if (MSInterfaceInfo->State.IsMassStoreReset)
168                   return false;
169         }
170
171         Endpoint_ClearOUT();
172
173         return true;
174 }
175
176 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
177 {
178         Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpoint.Address);
179
180         while (Endpoint_IsStalled())
181         {
182                 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
183                 USB_USBTask();
184                 #endif
185
186                 if (MSInterfaceInfo->State.IsMassStoreReset)
187                   return;
188         }
189
190         Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpoint.Address);
191
192         while (Endpoint_IsStalled())
193         {
194                 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
195                 USB_USBTask();
196                 #endif
197
198                 if (MSInterfaceInfo->State.IsMassStoreReset)
199                   return;
200         }
201
202         uint16_t BytesProcessed = 0;
203         while (Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus,
204                                         sizeof(MS_CommandStatusWrapper_t), &BytesProcessed) ==
205                                         ENDPOINT_RWSTREAM_IncompleteTransfer)
206         {
207                 if (MSInterfaceInfo->State.IsMassStoreReset)
208                   return;
209         }
210
211         Endpoint_ClearIN();
212 }
213
214 #endif
215