STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
usbd_msc.c
Go to the documentation of this file.
1 
43 /* Includes ------------------------------------------------------------------*/
44 #include "usbd_msc.h"
45 
46 
85 uint8_t USBD_MSC_Init (USBD_HandleTypeDef *pdev,
86  uint8_t cfgidx);
87 
88 uint8_t USBD_MSC_DeInit (USBD_HandleTypeDef *pdev,
89  uint8_t cfgidx);
90 
91 uint8_t USBD_MSC_Setup (USBD_HandleTypeDef *pdev,
93 
94 uint8_t USBD_MSC_DataIn (USBD_HandleTypeDef *pdev,
95  uint8_t epnum);
96 
97 
98 uint8_t USBD_MSC_DataOut (USBD_HandleTypeDef *pdev,
99  uint8_t epnum);
100 
101 uint8_t *USBD_MSC_GetHSCfgDesc (uint16_t *length);
102 
103 uint8_t *USBD_MSC_GetFSCfgDesc (uint16_t *length);
104 
105 uint8_t *USBD_MSC_GetOtherSpeedCfgDesc (uint16_t *length);
106 
107 uint8_t *USBD_MSC_GetDeviceQualifierDescriptor (uint16_t *length);
108 
109 
121 {
125  NULL, /*EP0_TxSent*/
126  NULL, /*EP0_RxReady*/
129  NULL, /*SOF */
130  NULL,
131  NULL,
136 };
137 
138 /* USB Mass storage device Configuration Descriptor */
139 /* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
140 __ALIGN_BEGIN uint8_t USBD_MSC_CfgHSDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END =
141 {
142 
143  0x09, /* bLength: Configuation Descriptor size */
144  USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
146 
147  0x00,
148  0x01, /* bNumInterfaces: 1 interface */
149  0x01, /* bConfigurationValue: */
150  0x04, /* iConfiguration: */
151  0xC0, /* bmAttributes: */
152  0x32, /* MaxPower 100 mA */
153 
154  /******************** Mass Storage interface ********************/
155  0x09, /* bLength: Interface Descriptor size */
156  0x04, /* bDescriptorType: */
157  0x00, /* bInterfaceNumber: Number of Interface */
158  0x00, /* bAlternateSetting: Alternate setting */
159  0x02, /* bNumEndpoints*/
160  0x08, /* bInterfaceClass: MSC Class */
161  0x06, /* bInterfaceSubClass : SCSI transparent*/
162  0x50, /* nInterfaceProtocol */
163  0x05, /* iInterface: */
164  /******************** Mass Storage Endpoints ********************/
165  0x07, /*Endpoint descriptor length = 7*/
166  0x05, /*Endpoint descriptor type */
167  MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */
168  0x02, /*Bulk endpoint type */
171  0x00, /*Polling interval in milliseconds */
172 
173  0x07, /*Endpoint descriptor length = 7 */
174  0x05, /*Endpoint descriptor type */
175  MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */
176  0x02, /*Bulk endpoint type */
179  0x00 /*Polling interval in milliseconds*/
180 };
181 
182 /* USB Mass storage device Configuration Descriptor */
183 /* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
184 uint8_t USBD_MSC_CfgFSDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END =
185 {
186 
187  0x09, /* bLength: Configuation Descriptor size */
188  USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
190 
191  0x00,
192  0x01, /* bNumInterfaces: 1 interface */
193  0x01, /* bConfigurationValue: */
194  0x04, /* iConfiguration: */
195  0xC0, /* bmAttributes: */
196  0x32, /* MaxPower 100 mA */
197 
198  /******************** Mass Storage interface ********************/
199  0x09, /* bLength: Interface Descriptor size */
200  0x04, /* bDescriptorType: */
201  0x00, /* bInterfaceNumber: Number of Interface */
202  0x00, /* bAlternateSetting: Alternate setting */
203  0x02, /* bNumEndpoints*/
204  0x08, /* bInterfaceClass: MSC Class */
205  0x06, /* bInterfaceSubClass : SCSI transparent*/
206  0x50, /* nInterfaceProtocol */
207  0x05, /* iInterface: */
208  /******************** Mass Storage Endpoints ********************/
209  0x07, /*Endpoint descriptor length = 7*/
210  0x05, /*Endpoint descriptor type */
211  MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */
212  0x02, /*Bulk endpoint type */
215  0x00, /*Polling interval in milliseconds */
216 
217  0x07, /*Endpoint descriptor length = 7 */
218  0x05, /*Endpoint descriptor type */
219  MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */
220  0x02, /*Bulk endpoint type */
223  0x00 /*Polling interval in milliseconds*/
224 };
225 
226 __ALIGN_BEGIN uint8_t USBD_MSC_OtherSpeedCfgDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END =
227 {
228 
229  0x09, /* bLength: Configuation Descriptor size */
232 
233  0x00,
234  0x01, /* bNumInterfaces: 1 interface */
235  0x01, /* bConfigurationValue: */
236  0x04, /* iConfiguration: */
237  0xC0, /* bmAttributes: */
238  0x32, /* MaxPower 100 mA */
239 
240  /******************** Mass Storage interface ********************/
241  0x09, /* bLength: Interface Descriptor size */
242  0x04, /* bDescriptorType: */
243  0x00, /* bInterfaceNumber: Number of Interface */
244  0x00, /* bAlternateSetting: Alternate setting */
245  0x02, /* bNumEndpoints*/
246  0x08, /* bInterfaceClass: MSC Class */
247  0x06, /* bInterfaceSubClass : SCSI transparent command set*/
248  0x50, /* nInterfaceProtocol */
249  0x05, /* iInterface: */
250  /******************** Mass Storage Endpoints ********************/
251  0x07, /*Endpoint descriptor length = 7*/
252  0x05, /*Endpoint descriptor type */
253  MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */
254  0x02, /*Bulk endpoint type */
255  0x40,
256  0x00,
257  0x00, /*Polling interval in milliseconds */
258 
259  0x07, /*Endpoint descriptor length = 7 */
260  0x05, /*Endpoint descriptor type */
261  MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */
262  0x02, /*Bulk endpoint type */
263  0x40,
264  0x00,
265  0x00 /*Polling interval in milliseconds*/
266 };
267 
268 /* USB Standard Device Descriptor */
269 __ALIGN_BEGIN uint8_t USBD_MSC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END =
270 {
273  0x00,
274  0x02,
275  0x00,
276  0x00,
277  0x00,
279  0x01,
280  0x00,
281 };
299  uint8_t cfgidx)
300 {
301  int16_t ret = 0;
302 
303  if(pdev->dev_speed == USBD_SPEED_HIGH )
304  {
305  /* Open EP OUT */
306  USBD_LL_OpenEP(pdev,
310 
311  /* Open EP IN */
312  USBD_LL_OpenEP(pdev,
316  }
317  else
318  {
319  /* Open EP OUT */
320  USBD_LL_OpenEP(pdev,
324 
325  /* Open EP IN */
326  USBD_LL_OpenEP(pdev,
330  }
332 
333  if(pdev->pClassData == NULL)
334  {
335  ret = 1;
336  }
337  else
338  {
339  /* Init the BOT layer */
340  MSC_BOT_Init(pdev);
341  ret = 0;
342  }
343 
344  return ret;
345 }
346 
355  uint8_t cfgidx)
356 {
357  /* Close MSC EPs */
358  USBD_LL_CloseEP(pdev,
360 
361  /* Open EP IN */
362  USBD_LL_CloseEP(pdev,
363  MSC_EPIN_ADDR);
364 
365 
366  /* De-Init the BOT layer */
367  MSC_BOT_DeInit(pdev);
368 
369  /* Free MSC Class Resources */
370  if(pdev->pClassData != NULL)
371  {
372  USBD_free(pdev->pClassData);
373  pdev->pClassData = NULL;
374  }
375  return 0;
376 }
385 {
387 
388  switch (req->bmRequest & USB_REQ_TYPE_MASK)
389  {
390 
391  /* Class request */
392  case USB_REQ_TYPE_CLASS :
393  switch (req->bRequest)
394  {
395  case BOT_GET_MAX_LUN :
396 
397  if((req->wValue == 0) &&
398  (req->wLength == 1) &&
399  ((req->bmRequest & 0x80) == 0x80))
400  {
401  hmsc->max_lun = ((USBD_StorageTypeDef *)pdev->pUserData)->GetMaxLun();
402  USBD_CtlSendData (pdev,
403  (uint8_t *)&hmsc->max_lun,
404  1);
405  }
406  else
407  {
408  USBD_CtlError(pdev , req);
409  return USBD_FAIL;
410  }
411  break;
412 
413  case BOT_RESET :
414  if((req->wValue == 0) &&
415  (req->wLength == 0) &&
416  ((req->bmRequest & 0x80) != 0x80))
417  {
418  MSC_BOT_Reset(pdev);
419  }
420  else
421  {
422  USBD_CtlError(pdev , req);
423  return USBD_FAIL;
424  }
425  break;
426 
427  default:
428  USBD_CtlError(pdev , req);
429  return USBD_FAIL;
430  }
431  break;
432  /* Interface & Endpoint request */
434  switch (req->bRequest)
435  {
436  case USB_REQ_GET_INTERFACE :
437  USBD_CtlSendData (pdev,
438  (uint8_t *)&hmsc->interface,
439  1);
440  break;
441 
442  case USB_REQ_SET_INTERFACE :
443  hmsc->interface = (uint8_t)(req->wValue);
444  break;
445 
446  case USB_REQ_CLEAR_FEATURE:
447 
448  /* Flush the FIFO and Clear the stall status */
449  USBD_LL_FlushEP(pdev, (uint8_t)req->wIndex);
450 
451  /* Reactivate the EP */
452  USBD_LL_CloseEP (pdev , (uint8_t)req->wIndex);
453  if((((uint8_t)req->wIndex) & 0x80) == 0x80)
454  {
455  if(pdev->dev_speed == USBD_SPEED_HIGH )
456  {
457  /* Open EP IN */
458  USBD_LL_OpenEP(pdev,
462  }
463  else
464  {
465  /* Open EP IN */
466  USBD_LL_OpenEP(pdev,
470  }
471  }
472  else
473  {
474  if(pdev->dev_speed == USBD_SPEED_HIGH )
475  {
476  /* Open EP IN */
477  USBD_LL_OpenEP(pdev,
481  }
482  else
483  {
484  /* Open EP IN */
485  USBD_LL_OpenEP(pdev,
489  }
490  }
491 
492  /* Handle BOT error */
493  MSC_BOT_CplClrFeature(pdev, (uint8_t)req->wIndex);
494  break;
495 
496  }
497  break;
498 
499  default:
500  break;
501  }
502  return 0;
503 }
504 
513  uint8_t epnum)
514 {
515  MSC_BOT_DataIn(pdev , epnum);
516  return 0;
517 }
518 
527  uint8_t epnum)
528 {
529  MSC_BOT_DataOut(pdev , epnum);
530  return 0;
531 }
532 
539 uint8_t *USBD_MSC_GetHSCfgDesc (uint16_t *length)
540 {
541  *length = sizeof (USBD_MSC_CfgHSDesc);
542  return USBD_MSC_CfgHSDesc;
543 }
544 
551 uint8_t *USBD_MSC_GetFSCfgDesc (uint16_t *length)
552 {
553  *length = sizeof (USBD_MSC_CfgFSDesc);
554  return USBD_MSC_CfgFSDesc;
555 }
556 
563 uint8_t *USBD_MSC_GetOtherSpeedCfgDesc (uint16_t *length)
564 {
565  *length = sizeof (USBD_MSC_OtherSpeedCfgDesc);
566  return USBD_MSC_OtherSpeedCfgDesc;
567 }
574 uint8_t *USBD_MSC_GetDeviceQualifierDescriptor (uint16_t *length)
575 {
576  *length = sizeof (USBD_MSC_DeviceQualifierDesc);
577  return USBD_MSC_DeviceQualifierDesc;
578 }
579 
586  USBD_StorageTypeDef *fops)
587 {
588  if(fops != NULL)
589  {
590  pdev->pUserData= fops;
591  }
592  return 0;
593 }
594 
609 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
Flushes an endpoint of the Low Level Driver.
Definition: usbd_conf.c:504
#define USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION
Definition: usbd_def.h:101
void MSC_BOT_CplClrFeature(USBD_HandleTypeDef *pdev, uint8_t epnum)
MSC_BOT_CplClrFeature Complete the clear feature request.
Definition: usbd_msc_bot.c:378
USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
Closes an endpoint of the Low Level Driver.
Definition: usbd_conf.c:492
#define MSC_MAX_FS_PACKET
Definition: usbd_msc.h:54
#define USB_MSC_CONFIG_DESC_SIZ
Definition: usbd_msc.h:59
uint8_t bmRequest
Definition: usbd_def.h:151
uint16_t wValue
Definition: usbd_def.h:153
#define LOBYTE(x)
Definition: usbd_def.h:263
uint8_t bRequest
Definition: usbd_def.h:152
#define USB_LEN_DEV_QUALIFIER_DESC
Definition: usbd_def.h:57
void MSC_BOT_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
MSC_BOT_DataIn Handle BOT IN data stage.
Definition: usbd_msc_bot.c:166
USBD_ClassTypeDef USBD_MSC
Definition: usbd_msc.c:120
#define USB_REQ_GET_INTERFACE
Definition: usbd_def.h:91
USBD_SpeedTypeDef dev_speed
Definition: usbd_def.h:231
#define USBD_free
Definition: usbd_conf.h:74
void MSC_BOT_Reset(USBD_HandleTypeDef *pdev)
MSC_BOT_Reset Reset the BOT Machine.
Definition: usbd_msc_bot.c:133
uint8_t USBD_MSC_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
USBD_MSC_Setup Handle the MSC specific requests.
Definition: usbd_msc.c:384
Header for the usbd_msc.c file.
uint8_t USBD_MSC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
USBD_MSC_DataIn handle data IN Stage.
Definition: usbd_msc.c:512
#define NULL
Definition: usbd_def.h:53
uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef *fops)
USBD_MSC_RegisterStorage.
Definition: usbd_msc.c:585
#define USBD_malloc
Definition: usbd_conf.h:73
#define BOT_RESET
Definition: usbd_msc.h:58
void MSC_BOT_DeInit(USBD_HandleTypeDef *pdev)
MSC_BOT_DeInit Deinitialize the BOT Machine.
Definition: usbd_msc_bot.c:153
#define USB_REQ_SET_INTERFACE
Definition: usbd_def.h:92
uint16_t wLength
Definition: usbd_def.h:155
uint8_t * USBD_MSC_GetOtherSpeedCfgDesc(uint16_t *length)
USBD_MSC_GetOtherSpeedCfgDesc return other speed configuration descriptor.
Definition: usbd_msc.c:563
void USBD_CtlError(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
USBD_CtlError Handle USB low level Error.
Definition: usbd_ctlreq.c:716
uint16_t wIndex
Definition: usbd_def.h:154
#define USBD_EP_TYPE_BULK
Definition: usbd_def.h:135
#define USB_REQ_TYPE_CLASS
Definition: usbd_def.h:74
#define USB_DESC_TYPE_CONFIGURATION
Definition: usbd_def.h:96
uint8_t USBD_MSC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
USBD_MSC_Init Initialize the mass storage configuration.
Definition: usbd_msc.c:298
void MSC_BOT_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
MSC_BOT_DataOut Process MSC OUT data.
Definition: usbd_msc_bot.c:199
#define USB_REQ_TYPE_STANDARD
Definition: usbd_def.h:73
uint8_t * USBD_MSC_GetHSCfgDesc(uint16_t *length)
USBD_MSC_GetHSCfgDesc return configuration descriptor.
Definition: usbd_msc.c:539
void MSC_BOT_Init(USBD_HandleTypeDef *pdev)
MSC_BOT_Init Initialize the BOT Process.
Definition: usbd_msc_bot.c:105
USBD_StatusTypeDef USBD_CtlSendData(USBD_HandleTypeDef *pdev, uint8_t *buf, uint16_t len)
USBD_CtlSendData send data on the ctl pipe.
Definition: usbd_ioreq.c:95
#define HIBYTE(x)
Definition: usbd_def.h:264
#define BOT_GET_MAX_LUN
Definition: usbd_msc.h:57
__ALIGN_BEGIN uint8_t USBD_MSC_CfgHSDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END
Definition: usbd_msc.c:140
#define USB_DESC_TYPE_DEVICE_QUALIFIER
Definition: usbd_def.h:100
uint8_t USBD_MSC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
USBD_MSC_DataOut handle data OUT Stage.
Definition: usbd_msc.c:526
#define USB_REQ_CLEAR_FEATURE
Definition: usbd_def.h:84
#define USB_REQ_TYPE_MASK
Definition: usbd_def.h:76
#define MSC_MAX_HS_PACKET
Definition: usbd_msc.h:55
USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t ep_type, uint16_t ep_mps)
Opens an endpoint of the Low Level Driver.
Definition: usbd_conf.c:473
uint8_t USBD_MSC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
USBD_MSC_DeInit DeInitilaize the mass storage configuration.
Definition: usbd_msc.c:354
uint8_t * USBD_MSC_GetFSCfgDesc(uint16_t *length)
USBD_MSC_GetFSCfgDesc return configuration descriptor.
Definition: usbd_msc.c:551
#define MSC_EPIN_ADDR
Definition: usbd_msc.h:62
uint8_t * USBD_MSC_GetDeviceQualifierDescriptor(uint16_t *length)
DeviceQualifierDescriptor return Device Qualifier descriptor.
Definition: usbd_msc.c:574
#define MSC_EPOUT_ADDR
Definition: usbd_msc.h:63