Add host mode USB Class driver stubs, add beginnings of a CDC host class driver.
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / MassStorage.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_DEVICE)
33
34 #define INCLUDE_FROM_MS_CLASS_DEVICE_C
35 #include "MassStorage.h"
36
37 static USB_ClassInfo_MS_t* CallbackMSInterfaceInfo;
38
39 void MS_Device_ProcessControlPacket(USB_ClassInfo_MS_t* MSInterfaceInfo)
40 {
41 if (!(Endpoint_IsSETUPReceived()))
42 return;
43
44 if (USB_ControlRequest.wIndex != MSInterfaceInfo->InterfaceNumber)
45 return;
46
47 switch (USB_ControlRequest.bRequest)
48 {
49 case REQ_MassStorageReset:
50 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
51 {
52 Endpoint_ClearSETUP();
53
54 MSInterfaceInfo->IsMassStoreReset = true;
55
56 while (!(Endpoint_IsINReady()));
57 Endpoint_ClearIN();
58 }
59
60 break;
61 case REQ_GetMaxLUN:
62 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
63 {
64 Endpoint_ClearSETUP();
65
66 Endpoint_Write_Byte(MSInterfaceInfo->TotalLUNs - 1);
67
68 Endpoint_ClearIN();
69
70 while (!(Endpoint_IsOUTReceived()));
71 Endpoint_ClearOUT();
72 }
73
74 break;
75 }
76 }
77
78 bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_t* MSInterfaceInfo)
79 {
80 if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->DataINEndpointNumber, EP_TYPE_BULK,
81 ENDPOINT_DIR_IN, MSInterfaceInfo->DataINEndpointSize,
82 ENDPOINT_BANK_SINGLE)))
83 {
84 return false;
85 }
86
87 if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->DataOUTEndpointNumber, EP_TYPE_BULK,
88 ENDPOINT_DIR_OUT, MSInterfaceInfo->DataOUTEndpointSize,
89 ENDPOINT_BANK_SINGLE)))
90 {
91 return false;
92 }
93
94 return true;
95 }
96
97 void MS_Device_USBTask(USB_ClassInfo_MS_t* MSInterfaceInfo)
98 {
99 if (!(USB_IsConnected))
100 return;
101
102 Endpoint_SelectEndpoint(MSInterfaceInfo->DataOUTEndpointNumber);
103
104 if (Endpoint_IsReadWriteAllowed())
105 {
106 if (MS_Device_ReadInCommandBlock(MSInterfaceInfo))
107 {
108 if (MSInterfaceInfo->CommandBlock.Flags & MS_COMMAND_DIR_DATA_IN)
109 Endpoint_SelectEndpoint(MSInterfaceInfo->DataINEndpointNumber);
110
111 MSInterfaceInfo->CommandStatus.Status = CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo) ?
112 SCSI_Command_Pass : SCSI_Command_Fail;
113 MSInterfaceInfo->CommandStatus.Signature = MS_CSW_SIGNATURE;
114 MSInterfaceInfo->CommandStatus.Tag = MSInterfaceInfo->CommandBlock.Tag;
115 MSInterfaceInfo->CommandStatus.DataTransferResidue = MSInterfaceInfo->CommandBlock.DataTransferLength;
116
117 if ((MSInterfaceInfo->CommandStatus.Status == SCSI_Command_Fail) && (MSInterfaceInfo->CommandStatus.DataTransferResidue))
118 Endpoint_StallTransaction();
119
120 MS_Device_ReturnCommandStatus(MSInterfaceInfo);
121
122 if (MSInterfaceInfo->IsMassStoreReset)
123 {
124 Endpoint_ResetFIFO(MSInterfaceInfo->DataOUTEndpointNumber);
125 Endpoint_ResetFIFO(MSInterfaceInfo->DataINEndpointNumber);
126
127 Endpoint_SelectEndpoint(MSInterfaceInfo->DataOUTEndpointNumber);
128 Endpoint_ClearStall();
129 Endpoint_SelectEndpoint(MSInterfaceInfo->DataINEndpointNumber);
130 Endpoint_ClearStall();
131
132 MSInterfaceInfo->IsMassStoreReset = false;
133 }
134 }
135 }
136 }
137
138 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_t* MSInterfaceInfo)
139 {
140 Endpoint_SelectEndpoint(MSInterfaceInfo->DataOUTEndpointNumber);
141
142 CallbackMSInterfaceInfo = MSInterfaceInfo;
143 Endpoint_Read_Stream_LE(&MSInterfaceInfo->CommandBlock,
144 (sizeof(CommandBlockWrapper_t) - 16),
145 StreamCallback_MS_Device_AbortOnMassStoreReset);
146
147 if ((MSInterfaceInfo->CommandBlock.Signature != MS_CBW_SIGNATURE) ||
148 (MSInterfaceInfo->CommandBlock.LUN >= MSInterfaceInfo->TotalLUNs) ||
149 (MSInterfaceInfo->CommandBlock.SCSICommandLength > 16))
150 {
151 Endpoint_StallTransaction();
152 Endpoint_SelectEndpoint(MSInterfaceInfo->DataINEndpointNumber);
153 Endpoint_StallTransaction();
154
155 return false;
156 }
157
158 CallbackMSInterfaceInfo = MSInterfaceInfo;
159 Endpoint_Read_Stream_LE(&MSInterfaceInfo->CommandBlock.SCSICommandData,
160 MSInterfaceInfo->CommandBlock.SCSICommandLength,
161 StreamCallback_MS_Device_AbortOnMassStoreReset);
162
163 Endpoint_ClearOUT();
164
165 if (MSInterfaceInfo->IsMassStoreReset)
166 return false;
167
168 return true;
169 }
170
171 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_t* MSInterfaceInfo)
172 {
173 Endpoint_SelectEndpoint(MSInterfaceInfo->DataOUTEndpointNumber);
174
175 while (Endpoint_IsStalled())
176 {
177 USB_USBTask();
178
179 if (MSInterfaceInfo->IsMassStoreReset)
180 return;
181 }
182
183 Endpoint_SelectEndpoint(MSInterfaceInfo->DataINEndpointNumber);
184
185 while (Endpoint_IsStalled())
186 {
187 USB_USBTask();
188
189 if (MSInterfaceInfo->IsMassStoreReset)
190 return;
191 }
192
193 CallbackMSInterfaceInfo = MSInterfaceInfo;
194 Endpoint_Write_Stream_LE(&MSInterfaceInfo->CommandStatus, sizeof(CommandStatusWrapper_t),
195 StreamCallback_MS_Device_AbortOnMassStoreReset);
196
197 Endpoint_ClearIN();
198
199 if (MSInterfaceInfo->IsMassStoreReset)
200 return;
201 }
202
203 static uint8_t StreamCallback_MS_Device_AbortOnMassStoreReset(void)
204 {
205 MS_Device_USBTask(CallbackMSInterfaceInfo);
206
207 if (CallbackMSInterfaceInfo->IsMassStoreReset)
208 return STREAMCALLBACK_Abort;
209 else
210 return STREAMCALLBACK_Continue;
211 }
212
213 #endif