Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <MSDescriptors.h>
00041
00042 #include <MSDDriver.h>
00043 #include <USBLib_Trace.h>
00044 #include "USBD.h"
00045 #include <USBD_HAL.h>
00046 #include <USBDDriver.h>
00047
00048
00049
00050
00051
00052
00053 typedef struct _MSDParseData {
00054
00055 MSDDriver *pMsdd;
00056
00057 USBInterfaceDescriptor *pIf;
00058 } MSDParseData;
00059
00060
00061
00062
00063
00064
00065
00066 COMPILER_ALIGNED(32) static MSDDriver msdFunction;
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 static uint8_t MSDFunction_Parse(USBGenericDescriptor *desc, MSDParseData *arg)
00078 {
00079 MSDDriver *pMsdd = arg->pMsdd;
00080 USBInterfaceDescriptor *pIf;
00081
00082
00083 if (desc->bLength == 0)
00084 return USBD_STATUS_INVALID_PARAMETER;
00085
00086
00087 if (desc->bDescriptorType == USBGenericDescriptor_INTERFACE) {
00088 pIf = (USBInterfaceDescriptor *)desc;
00089
00090 if (pIf->bInterfaceClass == MSInterfaceDescriptor_CLASS) {
00091
00092 if (pMsdd->interfaceNb == 0xFF) {
00093 pMsdd->interfaceNb = pIf->bInterfaceNumber;
00094 arg->pIf = pIf;
00095 }
00096
00097 else if (pMsdd->interfaceNb == pIf->bInterfaceNumber)
00098 arg->pIf = pIf;
00099
00100 }
00101 }
00102
00103
00104 if (arg->pIf) {
00105 if (desc->bDescriptorType == USBGenericDescriptor_ENDPOINT) {
00106 USBEndpointDescriptor *pEP = (USBEndpointDescriptor *)desc;
00107
00108 if (pEP->bmAttributes == USBEndpointDescriptor_BULK) {
00109 if (pEP->bEndpointAddress & 0x80)
00110 pMsdd->commandState.pipeIN = pEP->bEndpointAddress & 0x7F;
00111 else
00112 pMsdd->commandState.pipeOUT = pEP->bEndpointAddress;
00113 }
00114 }
00115
00116
00117 if (pMsdd->commandState.pipeIN != 0
00118 && pMsdd->commandState.pipeOUT != 0)
00119 return USBRC_FINISHED;
00120 }
00121
00122 return 0;
00123 }
00124
00125
00126
00127
00128 static void MSDFunction_Reset(void)
00129 {
00130 MSDDriver *pMsdd = &msdFunction;
00131
00132 TRACE_INFO_WP("MSDReset ");
00133
00134 pMsdd->state = MSDD_STATE_READ_CBW;
00135 pMsdd->waitResetRecovery = 0;
00136 pMsdd->commandState.state = 0;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 void MSDFunction_Initialize(
00152 USBDDriver *pUsbd, uint8_t bInterfaceNb,
00153 MSDLun *pLuns, uint8_t numLuns)
00154 {
00155 MSDDriver *pMsdDriver = &msdFunction;
00156
00157 TRACE_INFO("MSDFun init\n\r");
00158
00159
00160 pMsdDriver->pUsbd = pUsbd;
00161 pMsdDriver->interfaceNb = bInterfaceNb;
00162
00163
00164 pMsdDriver->commandState.state = 0;
00165 pMsdDriver->commandState.postprocess = 0;
00166 pMsdDriver->commandState.length = 0;
00167 pMsdDriver->commandState.transfer.semaphore = 0;
00168
00169
00170 pMsdDriver->luns = pLuns;
00171 pMsdDriver->maxLun = (uint8_t) (numLuns - 1);
00172
00173
00174 MSDFunction_Reset();
00175 }
00176
00177
00178
00179
00180
00181
00182
00183 void MSDFunction_Configure(USBGenericDescriptor *pDescriptors,
00184 uint16_t wLength)
00185 {
00186 MSDDriver *pMsdDriver = &msdFunction;
00187 MSDParseData parseData;
00188
00189 TRACE_INFO_WP("MSDFunCfg ");
00190
00191 pMsdDriver->state = MSDD_STATE_READ_CBW;
00192 pMsdDriver->waitResetRecovery = 0;
00193 pMsdDriver->commandState.state = 0;
00194
00195 parseData.pIf = 0;
00196 parseData.pMsdd = pMsdDriver;
00197 USBGenericDescriptor_Parse((USBGenericDescriptor *)pDescriptors, wLength,
00198 (USBDescriptorParseFunction)MSDFunction_Parse, &parseData);
00199
00200 MSDFunction_Reset();
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 uint32_t MSDFunction_RequestHandler(
00212 const USBGenericRequest *request)
00213 {
00214 MSDDriver *pMsdDriver = &msdFunction;
00215 uint32_t reqCode = (USBGenericRequest_GetType(request) << 8)
00216 | (USBGenericRequest_GetRequest(request));
00217
00218 TRACE_INFO_WP("Msdf ");
00219
00220
00221 switch (reqCode) {
00222
00223 case USBGenericRequest_CLEARFEATURE:
00224
00225 TRACE_INFO_WP("ClrFeat ");
00226
00227 switch (USBFeatureRequest_GetFeatureSelector(request)) {
00228
00229
00230 case USBFeatureRequest_ENDPOINTHALT:
00231
00232 TRACE_INFO_WP("Hlt ");
00233
00234
00235
00236 if (!pMsdDriver->waitResetRecovery) {
00237
00238
00239 USBDDriver_RequestHandler(USBD_GetDriver(), request);
00240 } else
00241
00242 TRACE_INFO_WP("No ");
00243
00244 USBD_Write(0, 0, 0, 0, 0);
00245
00246 return USBRC_SUCCESS;
00247
00248 }
00249
00250 break;
00251
00252
00253 case (USBGenericRequest_CLASS<<8)|MSD_GET_MAX_LUN:
00254
00255 TRACE_INFO_WP("gMaxLun ");
00256
00257
00258 if ((request->wValue == 0)
00259 && (request->wIndex == pMsdDriver->interfaceNb)
00260 && (request->wLength == 1))
00261
00262 USBD_Write(0, &(pMsdDriver->maxLun), 1, 0, 0);
00263
00264 else {
00265
00266 TRACE_WARNING(
00267 "MSDDriver_RequestHandler: GetMaxLUN(%d,%d,%d)\n\r",
00268 request->wValue, request->wIndex, request->wLength);
00269 USBD_Stall(0);
00270 }
00271
00272 return USBRC_SUCCESS;
00273
00274
00275 case (USBGenericRequest_CLASS<<8)|MSD_BULK_ONLY_RESET:
00276
00277 TRACE_INFO_WP("Rst ");
00278
00279
00280 if ((request->wValue == 0)
00281 && (request->wIndex == pMsdDriver->interfaceNb)
00282 && (request->wLength == 0)) {
00283
00284
00285 MSDFunction_Reset();
00286 USBD_Write(0, 0, 0, 0, 0);
00287 } else {
00288
00289 TRACE_WARNING(
00290 "MSDDriver_RequestHandler: Reset(%d,%d,%d)\n\r",
00291 request->wValue, request->wIndex, request->wLength);
00292 USBD_Stall(0);
00293 }
00294
00295 return USBRC_SUCCESS;
00296 }
00297
00298 return USBRC_PARAM_ERR;
00299 }
00300
00301
00302
00303
00304 void MSDFunction_StateMachine(void)
00305 {
00306 if (USBD_GetState() < USBD_STATE_CONFIGURED) {}
00307 else MSDD_StateMachine(&msdFunction);
00308
00309 }
00310
00311
00312