[G4][LIB] Take care of unused parameters in USB library
[betaflight.git] / lib / main / STM32G4 / Middlewares / ST / STM32_USB_Device_Library / Class / MSC / Src / usbd_msc.c
blobf69cef7ed7ed586b62cb1bcd59fa2d080d3af40d
1 /**
2 ******************************************************************************
3 * @file usbd_msc.c
4 * @author MCD Application Team
5 * @brief This file provides all the MSC core functions.
7 * @verbatim
9 * ===================================================================
10 * MSC Class Description
11 * ===================================================================
12 * This module manages the MSC class V1.0 following the "Universal
13 * Serial Bus Mass Storage Class (MSC) Bulk-Only Transport (BOT) Version 1.0
14 * Sep. 31, 1999".
15 * This driver implements the following aspects of the specification:
16 * - Bulk-Only Transport protocol
17 * - Subclass : SCSI transparent command set (ref. SCSI Primary Commands - 3 (SPC-3))
19 * @endverbatim
21 ******************************************************************************
22 * @attention
24 * <h2><center>&copy; Copyright (c) 2015 STMicroelectronics.
25 * All rights reserved.</center></h2>
27 * This software component is licensed by ST under Ultimate Liberty license
28 * SLA0044, the "License"; You may not use this file except in compliance with
29 * the License. You may obtain a copy of the License at:
30 * http://www.st.com/SLA0044
32 ******************************************************************************
35 /* BSPDependencies
36 - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c"
37 - "stm32xxxxx_{eval}{discovery}_io.c"
38 - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c"
39 EndBSPDependencies */
41 /* Includes ------------------------------------------------------------------*/
42 #include "usbd_msc.h"
45 /** @addtogroup STM32_USB_DEVICE_LIBRARY
46 * @{
50 /** @defgroup MSC_CORE
51 * @brief Mass storage core module
52 * @{
55 /** @defgroup MSC_CORE_Private_TypesDefinitions
56 * @{
58 /**
59 * @}
63 /** @defgroup MSC_CORE_Private_Defines
64 * @{
67 /**
68 * @}
72 /** @defgroup MSC_CORE_Private_Macros
73 * @{
75 /**
76 * @}
80 /** @defgroup MSC_CORE_Private_FunctionPrototypes
81 * @{
83 uint8_t USBD_MSC_Init (USBD_HandleTypeDef *pdev,
84 uint8_t cfgidx);
86 uint8_t USBD_MSC_DeInit (USBD_HandleTypeDef *pdev,
87 uint8_t cfgidx);
89 uint8_t USBD_MSC_Setup (USBD_HandleTypeDef *pdev,
90 USBD_SetupReqTypedef *req);
92 uint8_t USBD_MSC_DataIn (USBD_HandleTypeDef *pdev,
93 uint8_t epnum);
96 uint8_t USBD_MSC_DataOut (USBD_HandleTypeDef *pdev,
97 uint8_t epnum);
99 uint8_t *USBD_MSC_GetHSCfgDesc (uint16_t *length);
101 uint8_t *USBD_MSC_GetFSCfgDesc (uint16_t *length);
103 uint8_t *USBD_MSC_GetOtherSpeedCfgDesc (uint16_t *length);
105 uint8_t *USBD_MSC_GetDeviceQualifierDescriptor (uint16_t *length);
109 * @}
113 /** @defgroup MSC_CORE_Private_Variables
114 * @{
118 USBD_ClassTypeDef USBD_MSC =
120 USBD_MSC_Init,
121 USBD_MSC_DeInit,
122 USBD_MSC_Setup,
123 NULL, /*EP0_TxSent*/
124 NULL, /*EP0_RxReady*/
125 USBD_MSC_DataIn,
126 USBD_MSC_DataOut,
127 NULL, /*SOF */
128 NULL,
129 NULL,
130 USBD_MSC_GetHSCfgDesc,
131 USBD_MSC_GetFSCfgDesc,
132 USBD_MSC_GetOtherSpeedCfgDesc,
133 USBD_MSC_GetDeviceQualifierDescriptor,
136 /* USB Mass storage device Configuration Descriptor */
137 /* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
138 __ALIGN_BEGIN uint8_t USBD_MSC_CfgHSDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END =
141 0x09, /* bLength: Configuation Descriptor size */
142 USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
143 USB_MSC_CONFIG_DESC_SIZ,
145 0x00,
146 0x01, /* bNumInterfaces: 1 interface */
147 0x01, /* bConfigurationValue: */
148 0x04, /* iConfiguration: */
149 0xC0, /* bmAttributes: */
150 0x32, /* MaxPower 100 mA */
152 /******************** Mass Storage interface ********************/
153 0x09, /* bLength: Interface Descriptor size */
154 0x04, /* bDescriptorType: */
155 0x00, /* bInterfaceNumber: Number of Interface */
156 0x00, /* bAlternateSetting: Alternate setting */
157 0x02, /* bNumEndpoints*/
158 0x08, /* bInterfaceClass: MSC Class */
159 0x06, /* bInterfaceSubClass : SCSI transparent*/
160 0x50, /* nInterfaceProtocol */
161 0x05, /* iInterface: */
162 /******************** Mass Storage Endpoints ********************/
163 0x07, /*Endpoint descriptor length = 7*/
164 0x05, /*Endpoint descriptor type */
165 MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */
166 0x02, /*Bulk endpoint type */
167 LOBYTE(MSC_MAX_HS_PACKET),
168 HIBYTE(MSC_MAX_HS_PACKET),
169 0x00, /*Polling interval in milliseconds */
171 0x07, /*Endpoint descriptor length = 7 */
172 0x05, /*Endpoint descriptor type */
173 MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */
174 0x02, /*Bulk endpoint type */
175 LOBYTE(MSC_MAX_HS_PACKET),
176 HIBYTE(MSC_MAX_HS_PACKET),
177 0x00 /*Polling interval in milliseconds*/
180 /* USB Mass storage device Configuration Descriptor */
181 /* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
182 uint8_t USBD_MSC_CfgFSDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END =
185 0x09, /* bLength: Configuation Descriptor size */
186 USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
187 USB_MSC_CONFIG_DESC_SIZ,
189 0x00,
190 0x01, /* bNumInterfaces: 1 interface */
191 0x01, /* bConfigurationValue: */
192 0x04, /* iConfiguration: */
193 0xC0, /* bmAttributes: */
194 0x32, /* MaxPower 100 mA */
196 /******************** Mass Storage interface ********************/
197 0x09, /* bLength: Interface Descriptor size */
198 0x04, /* bDescriptorType: */
199 0x00, /* bInterfaceNumber: Number of Interface */
200 0x00, /* bAlternateSetting: Alternate setting */
201 0x02, /* bNumEndpoints*/
202 0x08, /* bInterfaceClass: MSC Class */
203 0x06, /* bInterfaceSubClass : SCSI transparent*/
204 0x50, /* nInterfaceProtocol */
205 0x05, /* iInterface: */
206 /******************** Mass Storage Endpoints ********************/
207 0x07, /*Endpoint descriptor length = 7*/
208 0x05, /*Endpoint descriptor type */
209 MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */
210 0x02, /*Bulk endpoint type */
211 LOBYTE(MSC_MAX_FS_PACKET),
212 HIBYTE(MSC_MAX_FS_PACKET),
213 0x00, /*Polling interval in milliseconds */
215 0x07, /*Endpoint descriptor length = 7 */
216 0x05, /*Endpoint descriptor type */
217 MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */
218 0x02, /*Bulk endpoint type */
219 LOBYTE(MSC_MAX_FS_PACKET),
220 HIBYTE(MSC_MAX_FS_PACKET),
221 0x00 /*Polling interval in milliseconds*/
224 __ALIGN_BEGIN uint8_t USBD_MSC_OtherSpeedCfgDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END =
227 0x09, /* bLength: Configuation Descriptor size */
228 USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION,
229 USB_MSC_CONFIG_DESC_SIZ,
231 0x00,
232 0x01, /* bNumInterfaces: 1 interface */
233 0x01, /* bConfigurationValue: */
234 0x04, /* iConfiguration: */
235 0xC0, /* bmAttributes: */
236 0x32, /* MaxPower 100 mA */
238 /******************** Mass Storage interface ********************/
239 0x09, /* bLength: Interface Descriptor size */
240 0x04, /* bDescriptorType: */
241 0x00, /* bInterfaceNumber: Number of Interface */
242 0x00, /* bAlternateSetting: Alternate setting */
243 0x02, /* bNumEndpoints*/
244 0x08, /* bInterfaceClass: MSC Class */
245 0x06, /* bInterfaceSubClass : SCSI transparent command set*/
246 0x50, /* nInterfaceProtocol */
247 0x05, /* iInterface: */
248 /******************** Mass Storage Endpoints ********************/
249 0x07, /*Endpoint descriptor length = 7*/
250 0x05, /*Endpoint descriptor type */
251 MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */
252 0x02, /*Bulk endpoint type */
253 0x40,
254 0x00,
255 0x00, /*Polling interval in milliseconds */
257 0x07, /*Endpoint descriptor length = 7 */
258 0x05, /*Endpoint descriptor type */
259 MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */
260 0x02, /*Bulk endpoint type */
261 0x40,
262 0x00,
263 0x00 /*Polling interval in milliseconds*/
266 /* USB Standard Device Descriptor */
267 __ALIGN_BEGIN uint8_t USBD_MSC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END =
269 USB_LEN_DEV_QUALIFIER_DESC,
270 USB_DESC_TYPE_DEVICE_QUALIFIER,
271 0x00,
272 0x02,
273 0x00,
274 0x00,
275 0x00,
276 MSC_MAX_FS_PACKET,
277 0x01,
278 0x00,
281 * @}
285 /** @defgroup MSC_CORE_Private_Functions
286 * @{
290 * @brief USBD_MSC_Init
291 * Initialize the mass storage configuration
292 * @param pdev: device instance
293 * @param cfgidx: configuration index
294 * @retval status
296 uint8_t USBD_MSC_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx)
298 UNUSED(cfgidx);
300 if(pdev->dev_speed == USBD_SPEED_HIGH)
302 /* Open EP OUT */
303 USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_HS_PACKET);
304 pdev->ep_out[MSC_EPOUT_ADDR & 0xFU].is_used = 1U;
306 /* Open EP IN */
307 USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_HS_PACKET);
308 pdev->ep_in[MSC_EPIN_ADDR & 0xFU].is_used = 1U;
310 else
312 /* Open EP OUT */
313 USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_FS_PACKET);
314 pdev->ep_out[MSC_EPOUT_ADDR & 0xFU].is_used = 1U;
316 /* Open EP IN */
317 USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_FS_PACKET);
318 pdev->ep_in[MSC_EPIN_ADDR & 0xFU].is_used = 1U;
320 pdev->pClassData = USBD_malloc(sizeof (USBD_MSC_BOT_HandleTypeDef));
322 if(pdev->pClassData == NULL)
324 return USBD_FAIL;
327 /* Init the BOT layer */
328 MSC_BOT_Init(pdev);
330 return USBD_OK;
334 * @brief USBD_MSC_DeInit
335 * DeInitilaize the mass storage configuration
336 * @param pdev: device instance
337 * @param cfgidx: configuration index
338 * @retval status
340 uint8_t USBD_MSC_DeInit (USBD_HandleTypeDef *pdev,
341 uint8_t cfgidx)
343 UNUSED(cfgidx);
345 /* Close MSC EPs */
346 USBD_LL_CloseEP(pdev, MSC_EPOUT_ADDR);
347 pdev->ep_out[MSC_EPOUT_ADDR & 0xFU].is_used = 0U;
349 /* Close EP IN */
350 USBD_LL_CloseEP(pdev, MSC_EPIN_ADDR);
351 pdev->ep_in[MSC_EPIN_ADDR & 0xFU].is_used = 0U;
353 /* De-Init the BOT layer */
354 MSC_BOT_DeInit(pdev);
356 /* Free MSC Class Resources */
357 if(pdev->pClassData != NULL)
359 USBD_free(pdev->pClassData);
360 pdev->pClassData = NULL;
362 return USBD_OK;
365 * @brief USBD_MSC_Setup
366 * Handle the MSC specific requests
367 * @param pdev: device instance
368 * @param req: USB request
369 * @retval status
371 uint8_t USBD_MSC_Setup (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
373 USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData;
374 uint8_t ret = USBD_OK;
375 uint16_t status_info = 0U;
377 switch (req->bmRequest & USB_REQ_TYPE_MASK)
380 /* Class request */
381 case USB_REQ_TYPE_CLASS:
382 switch (req->bRequest)
384 case BOT_GET_MAX_LUN:
385 if((req->wValue == 0U) && (req->wLength == 1U) &&
386 ((req->bmRequest & 0x80U) == 0x80U))
388 hmsc->max_lun = (uint32_t)((USBD_StorageTypeDef *)pdev->pUserData)->GetMaxLun();
389 USBD_CtlSendData (pdev, (uint8_t *)(void *)&hmsc->max_lun, 1U);
391 else
393 USBD_CtlError(pdev, req);
394 ret = USBD_FAIL;
396 break;
398 case BOT_RESET :
399 if((req->wValue == 0U) && (req->wLength == 0U) &&
400 ((req->bmRequest & 0x80U) != 0x80U))
402 MSC_BOT_Reset(pdev);
404 else
406 USBD_CtlError(pdev , req);
407 ret = USBD_FAIL;
409 break;
411 default:
412 USBD_CtlError(pdev , req);
413 ret = USBD_FAIL;
414 break;
416 break;
417 /* Interface & Endpoint request */
418 case USB_REQ_TYPE_STANDARD:
419 switch (req->bRequest)
421 case USB_REQ_GET_STATUS:
422 if (pdev->dev_state == USBD_STATE_CONFIGURED)
424 USBD_CtlSendData (pdev, (uint8_t *)(void *)&status_info, 2U);
426 else
428 USBD_CtlError (pdev, req);
429 ret = USBD_FAIL;
431 break;
433 case USB_REQ_GET_INTERFACE:
434 if (pdev->dev_state == USBD_STATE_CONFIGURED)
436 USBD_CtlSendData (pdev, (uint8_t *)(void *)&hmsc->interface, 1U);
438 else
440 USBD_CtlError (pdev, req);
441 ret = USBD_FAIL;
443 break;
445 case USB_REQ_SET_INTERFACE:
446 if (pdev->dev_state == USBD_STATE_CONFIGURED)
448 hmsc->interface = (uint8_t)(req->wValue);
450 else
452 USBD_CtlError (pdev, req);
453 ret = USBD_FAIL;
455 break;
457 case USB_REQ_CLEAR_FEATURE:
459 /* Flush the FIFO and Clear the stall status */
460 USBD_LL_FlushEP(pdev, (uint8_t)req->wIndex);
462 /* Reactivate the EP */
463 USBD_LL_CloseEP (pdev , (uint8_t)req->wIndex);
464 if((((uint8_t)req->wIndex) & 0x80U) == 0x80U)
466 pdev->ep_in[(uint8_t)req->wIndex & 0xFU].is_used = 0U;
467 if(pdev->dev_speed == USBD_SPEED_HIGH)
469 /* Open EP IN */
470 USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK,
471 MSC_MAX_HS_PACKET);
473 else
475 /* Open EP IN */
476 USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK,
477 MSC_MAX_FS_PACKET);
479 pdev->ep_in[MSC_EPIN_ADDR & 0xFU].is_used = 1U;
481 else
483 pdev->ep_out[(uint8_t)req->wIndex & 0xFU].is_used = 0U;
484 if(pdev->dev_speed == USBD_SPEED_HIGH)
486 /* Open EP OUT */
487 USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK,
488 MSC_MAX_HS_PACKET);
490 else
492 /* Open EP OUT */
493 USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK,
494 MSC_MAX_FS_PACKET);
496 pdev->ep_out[MSC_EPOUT_ADDR & 0xFU].is_used = 1U;
499 /* Handle BOT error */
500 MSC_BOT_CplClrFeature(pdev, (uint8_t)req->wIndex);
501 break;
503 default:
504 USBD_CtlError (pdev, req);
505 ret = USBD_FAIL;
506 break;
508 break;
510 default:
511 USBD_CtlError (pdev, req);
512 ret = USBD_FAIL;
513 break;
516 return ret;
520 * @brief USBD_MSC_DataIn
521 * handle data IN Stage
522 * @param pdev: device instance
523 * @param epnum: endpoint index
524 * @retval status
526 uint8_t USBD_MSC_DataIn (USBD_HandleTypeDef *pdev,
527 uint8_t epnum)
529 MSC_BOT_DataIn(pdev , epnum);
530 return USBD_OK;
534 * @brief USBD_MSC_DataOut
535 * handle data OUT Stage
536 * @param pdev: device instance
537 * @param epnum: endpoint index
538 * @retval status
540 uint8_t USBD_MSC_DataOut (USBD_HandleTypeDef *pdev,
541 uint8_t epnum)
543 MSC_BOT_DataOut(pdev , epnum);
544 return USBD_OK;
548 * @brief USBD_MSC_GetHSCfgDesc
549 * return configuration descriptor
550 * @param length : pointer data length
551 * @retval pointer to descriptor buffer
553 uint8_t *USBD_MSC_GetHSCfgDesc (uint16_t *length)
555 *length = sizeof (USBD_MSC_CfgHSDesc);
556 return USBD_MSC_CfgHSDesc;
560 * @brief USBD_MSC_GetFSCfgDesc
561 * return configuration descriptor
562 * @param length : pointer data length
563 * @retval pointer to descriptor buffer
565 uint8_t *USBD_MSC_GetFSCfgDesc (uint16_t *length)
567 *length = sizeof (USBD_MSC_CfgFSDesc);
568 return USBD_MSC_CfgFSDesc;
572 * @brief USBD_MSC_GetOtherSpeedCfgDesc
573 * return other speed configuration descriptor
574 * @param length : pointer data length
575 * @retval pointer to descriptor buffer
577 uint8_t *USBD_MSC_GetOtherSpeedCfgDesc (uint16_t *length)
579 *length = sizeof (USBD_MSC_OtherSpeedCfgDesc);
580 return USBD_MSC_OtherSpeedCfgDesc;
583 * @brief DeviceQualifierDescriptor
584 * return Device Qualifier descriptor
585 * @param length : pointer data length
586 * @retval pointer to descriptor buffer
588 uint8_t *USBD_MSC_GetDeviceQualifierDescriptor (uint16_t *length)
590 *length = sizeof (USBD_MSC_DeviceQualifierDesc);
591 return USBD_MSC_DeviceQualifierDesc;
595 * @brief USBD_MSC_RegisterStorage
596 * @param fops: storage callback
597 * @retval status
599 uint8_t USBD_MSC_RegisterStorage (USBD_HandleTypeDef *pdev,
600 USBD_StorageTypeDef *fops)
602 if(fops != NULL)
604 pdev->pUserData = fops;
606 return USBD_OK;
610 * @}
615 * @}
620 * @}
623 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/