| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943 |
- /**
- ******************************************************************************
- * @file usbd_req.c
- * @author MCD Application Team
- * @version V2.4.2
- * @date 11-December-2015
- * @brief This file provides the standard USB requests following chapter 9.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT 2015 STMicroelectronics</center></h2>
- *
- * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.st.com/software_license_agreement_liberty_v2
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "usbd_ctlreq.h"
- #include "usbd_ioreq.h"
- static USBD_StatusTypeDef USBD_GetDescriptor(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req);
- static USBD_StatusTypeDef USBD_SetAddress(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req);
- static USBD_StatusTypeDef USBD_SetConfig(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req);
- static USBD_StatusTypeDef USBD_GetConfig(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req);
- static USBD_StatusTypeDef USBD_GetStatus(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req);
- static USBD_StatusTypeDef USBD_SetFeature(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req);
- static USBD_StatusTypeDef USBD_ClrFeature(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req);
- static USBD_StatusTypeDef USBD_GetMicrosoftDescriptor(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req);
- static uint8_t USBD_GetLen(uint8_t *buf);
- /**
- * @brief USBD_StdDevReq
- * Handle standard usb device requests
- * @param pdev: device instance
- * @param req: usb request
- * @retval status
- */
- USBD_StatusTypeDef USBD_StdDevReq (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
- {
- USBD_StatusTypeDef ret = USBD_OK;
- if(req->bRequest == USB_REQ_VENDOR_FEATURE) // Debug Äîáàâèòü îòâåò òîëüêî âî âðåìÿ ðàáîòû ñ âåíäîð ïðîòîêîëîì
- {
- ret = USBD_GetMicrosoftDescriptor(pdev, req);
-
- return ret;
- }
- // ~~~~~~~~~~~~~ Legacy support ~~~~~~~~~~~~~~~~~~~~~~~
- switch (req->bmRequest & USB_REQ_TYPE_MASK)
- {
- case USB_REQ_TYPE_VENDOR:
- {
- ret = USBD_StdEPReq( pdev, req );
- return ret;
- }
- break;
- }
- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- switch (req->bRequest)
- {
- case USB_REQ_GET_DESCRIPTOR:
- ret = USBD_GetDescriptor(pdev, req);
- break;
-
- case USB_REQ_SET_ADDRESS:
- ret = USBD_SetAddress(pdev, req);
- break;
-
- case USB_REQ_SET_CONFIGURATION:
- ret = USBD_SetConfig(pdev, req);
- break;
-
- case USB_REQ_GET_CONFIGURATION:
- ret = USBD_GetConfig(pdev, req);
- break;
- case USB_REQ_GET_INTERFACE:
- ret = USBD_FAIL;
- break;
-
- case USB_REQ_GET_STATUS:
- ret = USBD_GetStatus(pdev, req);
- break;
-
- case USB_REQ_SET_FEATURE:
- ret = USBD_SetFeature(pdev, req);
- break;
-
- case USB_REQ_CLEAR_FEATURE:
- ret = USBD_ClrFeature(pdev, req);
- break;
-
- default:
- ret = USBD_FAIL;
- break;
- }
-
- return ret;
- }
- /**
- * @brief USBD_StdItfReq
- * Handle standard usb interface requests
- * @param pdev: device instance
- * @param req: usb request
- * @retval status
- */
- USBD_StatusTypeDef USBD_StdItfReq (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
- {
- USBD_StatusTypeDef ret = USBD_OK;
-
- switch (pdev->dev_state)
- {
- case USBD_STATE_CONFIGURED:
- {
- if (LOBYTE(req->wIndex) <= USBD_MAX_NUM_INTERFACES)
- {
- ret = (USBD_StatusTypeDef)pdev->pClass->Setup(pdev, req);
-
- if( USBD_OK == ret )
- {
- if( req->wLength == 0 )
- {
- USBD_CtlSendStatus(pdev);
- }
- }
- else
- {
- ret = USBD_FAIL;
- }
- }
- else
- {
- ret = USBD_FAIL;
- }
- }
- break;
-
- default:
- ret = USBD_FAIL;
- break;
- }
- return ret;
- }
- /**
- * @brief USBD_StdEPReq
- * Handle standard usb endpoint requests
- * @param pdev: device instance
- * @param req: usb request
- * @retval status
- */
- USBD_StatusTypeDef USBD_StdEPReq (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
- {
-
- uint8_t ep_addr;
- USBD_StatusTypeDef ret = USBD_OK;
- USBD_EndpointTypeDef *pep;
- ep_addr = LOBYTE(req->wIndex);
-
- /* Check if it is a USB-class request or a USB-vendor request */
- switch (req->bmRequest & USB_REQ_TYPE_MASK)
- {
- case USB_REQ_TYPE_CLASS:
- case USB_REQ_TYPE_VENDOR:
- {
- // pass the request to the class handler
- ret = (USBD_StatusTypeDef)pdev->pClass->Setup (pdev, req);
- return ret;
- }
- break;
- }
-
- switch (req->bRequest)
- {
- case USB_REQ_SET_FEATURE :
- {
- switch (pdev->dev_state)
- {
- case USBD_STATE_ADDRESSED:
- {
- if ((ep_addr != 0x00) && (ep_addr != 0x80))
- {
- USBD_LL_StallEP(pdev, ep_addr);
- }
- }
- break;
-
- case USBD_STATE_CONFIGURED:
- {
- if (req->wValue == USB_FEATURE_EP_HALT)
- {
- if ((ep_addr != 0x00) && (ep_addr != 0x80))
- {
- USBD_LL_StallEP(pdev, ep_addr);
- }
- }
- ret = (USBD_StatusTypeDef)pdev->pClass->Setup(pdev, req);
- if( USBD_OK == ret )
- {
- USBD_CtlSendStatus(pdev);
- }
- }
- break;
-
- default:
- ret = USBD_FAIL;
- break;
- }
- }
- break;
-
- case USB_REQ_CLEAR_FEATURE:
- {
- switch( pdev->dev_state )
- {
- case USBD_STATE_ADDRESSED:
- {
- if ((ep_addr != 0x00) && (ep_addr != 0x80))
- {
- USBD_LL_StallEP(pdev, ep_addr);
- }
- }
- break;
-
- case USBD_STATE_CONFIGURED:
- {
- if (req->wValue == USB_FEATURE_EP_HALT)
- {
- if ((ep_addr & 0x7F) != 0x00)
- {
- // First: clear EP status by default (VALID STATE)
- USBD_LL_ClearStallEP(pdev, ep_addr);
- // then: call user handler (user can override EP status now)
- ret = (USBD_StatusTypeDef)pdev->pClass->Setup(pdev, req);
- }
- if( USBD_OK == ret )
- {
- USBD_CtlSendStatus(pdev);
- }
- }
- }
- break;
-
- default:
- ret = USBD_FAIL;
- break;
- }
- }
- break;
-
- case USB_REQ_GET_STATUS:
- {
- switch (pdev->dev_state)
- {
- case USBD_STATE_ADDRESSED:
- {
- if ((ep_addr & 0x7F) != 0x00)
- {
- USBD_LL_StallEP(pdev, ep_addr);
- }
- }
- break;
-
- case USBD_STATE_CONFIGURED:
- {
- pep = ((ep_addr & 0x80) == 0x80) ? &pdev->ep_in[ep_addr & 0x7F]:\
- &pdev->ep_out[ep_addr & 0x7F];
- if(USBD_LL_IsStallEP(pdev, ep_addr))
- {
- pep->status = 0x0001;
- }
- else
- {
- pep->status = 0x0000;
- }
-
- USBD_CtlSendData(pdev,
- (uint8_t *)&pep->status,
- 2);
- }
- break;
-
- default:
- ret = USBD_FAIL;
- break;
- }
- }
- break;
- default:
- ret = USBD_FAIL;
- break;
- }
- return ret;
- }
- /**
- * @brief USBD_GetMicrosoftDescriptor
- * Handle Get Microsoft Descriptor requests
- * @param pdev: device instance
- * @param req: usb request
- * @retval status
- */
- static USBD_StatusTypeDef USBD_GetMicrosoftDescriptor(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req)
- {
- uint16_t len;
- const uint8_t *pbuf = NULL;
- USBD_StatusTypeDef ret = USBD_OK;
-
- switch(req->wIndex)
- {
- case USB_REQ_EXT_COMPAT_ID_OS_FEATURE_DESCRIPTOR:
- {
- #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
- ret = USBD_CallGetDescriptorHandler( pdev,
- eGetDescriptorHandlerType_ExtCompatIdOsFeat,
- &pbuf,
- &len );
- #else
- USBD_CallGetDescriptorHandlerAlt( ret, pdev, pbuf, len, GetExtCompatIdOsFeatureDescriptor );
- #endif
- }
- break;
-
- case USB_REQ_EXT_PROPERTIES_OS_FEATURE_DESCRIPTOR:
- {
- #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
- ret = USBD_CallGetDescriptorHandler( pdev,
- eGetDescriptorHandlerType_ExtPropertiesOsFeat,
- &pbuf,
- &len );
- #else
- USBD_CallGetDescriptorHandlerAlt( ret, pdev, pbuf, len, GetExtPropertiesOsFeatureDescriptor );
- #endif
- }
- break;
- }
-
- if( USBD_FAIL != ret )
- {
- if( (len != 0) && (req->wLength != 0) )
- {
- len = MIN(len , req->wLength);
-
- USBD_CtlSendData(pdev,
- pbuf,
- len);
- }
- else
- {
- ret = USBD_FAIL;
- }
- }
-
- return ret;
- }
- /**
- * @brief USBD_GetDescriptor
- * Handle Get Descriptor requests
- * @param pdev: device instance
- * @param req: usb request
- * @retval status
- */
- static USBD_StatusTypeDef USBD_GetDescriptor(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req)
- {
- uint16_t len;
- const uint8_t *pbuf = NULL;
- USBD_StatusTypeDef ret = USBD_OK;
-
- switch (req->wValue >> 8)
- {
- case USB_DESC_TYPE_DEVICE:
- {
- #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
- ret = USBD_CallGetDescriptorHandler( pdev,
- eGetDescriptorHandlerType_Device,
- &pbuf,
- &len );
- #else
- USBD_CallGetDescriptorHandlerAlt( ret, pdev, pbuf, len, GetDeviceDescriptor );
- #endif
- }
- break;
-
- case USB_DESC_TYPE_CONFIGURATION:
- {
- #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
- ret = USBD_CallGetDescriptorHandler( pdev,
- eGetDescriptorHandlerType_Config,
- &pbuf,
- &len );
- #else
- USBD_CallGetDescriptorHandlerAlt( ret, pdev, pbuf, len, GetConfigDescriptor );
- #endif
- }
- break;
-
- case USB_DESC_TYPE_STRING:
- {
- switch ((uint8_t)(req->wValue))
- {
- case USBD_IDX_LANGID_STR:
- {
- #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
- ret = USBD_CallGetDescriptorHandler( pdev,
- eGetDescriptorHandlerType_LangIdStr,
- &pbuf,
- &len );
- #else
- USBD_CallGetDescriptorHandlerAlt( ret, pdev, pbuf, len, GetLangIDStrDescriptor );
- #endif
- }
- break;
-
- case USBD_IDX_MFC_STR:
- {
- #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
- ret = USBD_CallGetDescriptorHandler( pdev,
- eGetDescriptorHandlerType_ManufacturerStr,
- &pbuf,
- &len );
- #else
- USBD_CallGetDescriptorHandlerAlt( ret, pdev, pbuf, len, GetManufacturerStrDescriptor );
- #endif
- }
- break;
-
- case USBD_IDX_PRODUCT_STR:
- {
- #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
- ret = USBD_CallGetDescriptorHandler( pdev,
- eGetDescriptorHandlerType_ProductStr,
- &pbuf,
- &len );
- #else
- USBD_CallGetDescriptorHandlerAlt( ret, pdev, pbuf, len, GetProductStrDescriptor );
- #endif
- }
- break;
- #if USBD_IDX_SERIAL_STR != 0
- case USBD_IDX_SERIAL_STR:
- {
- #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
- ret = USBD_CallGetDescriptorHandler( pdev,
- eGetDescriptorHandlerType_SerialStr,
- &pbuf,
- &len );
- #else
- USBD_CallGetDescriptorHandlerAlt( ret, pdev, pbuf, len, GetSerialStrDescriptor );
- #endif
- }
- break;
- #endif
- case USBD_IDX_CONFIG_STR:
- {
- #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
- ret = USBD_CallGetDescriptorHandler( pdev,
- eGetDescriptorHandlerType_ConfigurationStr,
- &pbuf,
- &len );
- #else
- USBD_CallGetDescriptorHandlerAlt( ret, pdev, pbuf, len, GetConfigurationStrDescriptor );
- #endif
- }
- break;
-
- case USBD_IDX_INTERFACE_STR:
- {
- #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
- ret = USBD_CallGetDescriptorHandler( pdev,
- eGetDescriptorHandlerType_InterfaceStr,
- &pbuf,
- &len );
- #else
- USBD_CallGetDescriptorHandlerAlt( ret, pdev, pbuf, len, GetInterfaceStrDescriptor );
- #endif
- }
- break;
-
- case USB_REQ_MS_OS_STRING_DESCRIPTOR:
- {
- #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
- ret = USBD_CallGetDescriptorHandler( pdev,
- eGetDescriptorHandlerType_MsOsString,
- &pbuf,
- &len );
- #else
- USBD_CallGetDescriptorHandlerAlt( ret, pdev, pbuf, len, GetMsOsStringDescriptor );
- #endif
- }
- break;
-
- default:
- {
- #if (USBD_SUPPORT_USER_STRING == 1)
- #if USBD_GETDESCRIPTORS_ALTMETHOD == 0
- if( NULL != pdev->pDesc && pdev->pDesc->GetUsrStrDescriptor )
- pbuf = pdev->pClass->GetUsrStrDescriptor(pdev, (req->wValue), &len);
- else
- if( NULL != pdev->pClass->pDesc && pdev->pClass->pDesc->GetUsrStrDescriptor )
- pbuf = pdev->pClass->GetUsrStrDescriptor(pdev, (req->wValue), &len);
- else
- ret = USBD_FAIL;
- #else
- if( NULL != pdev->pClass->pDesc && pdev->pClass->pDesc->GetUsrStrDescriptor )
- pbuf = pdev->pClass->GetUsrStrDescriptor(pdev, (req->wValue), &len);
- else
- ret = USBD_FAIL;
- #endif
- #else
- ret = USBD_FAIL;
- #endif
- }
- break;
- }
- }
- break;
-
- default:
- ret = USBD_FAIL;
- break;
- }
-
- if( USBD_FAIL != ret )
- {
- if( (len != 0) && (req->wLength != 0) )
- {
- len = MIN(len , req->wLength);
-
- USBD_CtlSendData(pdev,
- pbuf,
- len);
- }
- else
- {
- ret = USBD_FAIL;
- }
- }
- return ret;
- }
- /**
- * @brief USBD_SetAddress
- * Set device address
- * @param pdev: device instance
- * @param req: usb request
- * @retval status
- */
- static USBD_StatusTypeDef USBD_SetAddress(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req)
- {
- uint8_t dev_addr;
- USBD_StatusTypeDef ret = USBD_OK;
-
- if ((req->wIndex == 0) && (req->wLength == 0))
- {
- dev_addr = (uint8_t)(req->wValue) & 0x7F;
-
- if (pdev->dev_state == USBD_STATE_CONFIGURED)
- {
- ret = USBD_FAIL;
- }
- else
- {
- pdev->dev_address = dev_addr;
- USBD_LL_SetUSBAddress(pdev, dev_addr);
- USBD_CtlSendStatus(pdev);
-
- if (dev_addr != 0)
- {
- pdev->dev_state = USBD_STATE_ADDRESSED;
- }
- else
- {
- pdev->dev_state = USBD_STATE_DEFAULT;
- }
- }
- }
- else
- {
- ret = USBD_FAIL;
- }
- return ret;
- }
- /**
- * @brief USBD_SetConfig
- * Handle Set device configuration request
- * @param pdev: device instance
- * @param req: usb request
- * @retval status
- */
- static USBD_StatusTypeDef USBD_SetConfig(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req)
- {
-
- static uint8_t cfgidx;
- USBD_StatusTypeDef ret = USBD_OK;
- cfgidx = (uint8_t)(req->wValue);
-
- if (cfgidx > USBD_MAX_NUM_CONFIGURATION )
- {
- ret = USBD_FAIL;
- }
- else
- {
- switch (pdev->dev_state)
- {
- case USBD_STATE_ADDRESSED:
- {
- if(cfgidx > 0)
- {
- pdev->dev_config = cfgidx;
- pdev->dev_state = USBD_STATE_CONFIGURED;
- ret = USBD_SetClassConfig(pdev, cfgidx);
-
- if( USBD_OK == ret )
- {
- USBD_CtlSendStatus(pdev);
- }
- }
- else
- {
- USBD_CtlSendStatus(pdev);
- }
- }
- break;
-
- case USBD_STATE_CONFIGURED:
- {
- if (cfgidx == 0)
- {
- pdev->dev_state = USBD_STATE_ADDRESSED;
- pdev->dev_config = cfgidx;
- USBD_ClrClassConfig(pdev, cfgidx);
- USBD_CtlSendStatus(pdev);
- }
- else if (cfgidx != pdev->dev_config)
- {
- /* Clear old configuration */
- USBD_ClrClassConfig(pdev, pdev->dev_config);
-
- /* set new configuration */
- pdev->dev_config = cfgidx;
- ret = USBD_SetClassConfig(pdev, cfgidx);
- if( USBD_OK == ret )
- {
- USBD_CtlSendStatus(pdev);
- }
- }
- else
- {
- USBD_CtlSendStatus(pdev);
- }
- }
- break;
-
- default:
- ret = USBD_FAIL;
- break;
- }
- }
- return ret;
- }
- /**
- * @brief USBD_GetConfig
- * Handle Get device configuration request
- * @param pdev: device instance
- * @param req: usb request
- * @retval status
- */
- static USBD_StatusTypeDef USBD_GetConfig(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req)
- {
- USBD_StatusTypeDef ret = USBD_OK;
- if (req->wLength != 1)
- {
- ret = USBD_FAIL;
- }
- else
- {
- switch (pdev->dev_state )
- {
- case USBD_STATE_ADDRESSED:
- {
- pdev->dev_default_config = 0;
- USBD_CtlSendData(pdev,
- (uint8_t *)&pdev->dev_default_config,
- 1);
- }
- break;
-
- case USBD_STATE_CONFIGURED:
- {
- USBD_CtlSendData(pdev,
- (uint8_t *)&pdev->dev_config,
- 1);
- }
- break;
-
- default:
- ret = USBD_FAIL;
- break;
- }
- }
- return ret;
- }
- /**
- * @brief USBD_GetStatus
- * Handle Get Status request
- * @param pdev: device instance
- * @param req: usb request
- * @retval status
- */
- static USBD_StatusTypeDef USBD_GetStatus(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req)
- {
- USBD_StatusTypeDef ret = USBD_OK;
-
- switch (pdev->dev_state)
- {
- case USBD_STATE_ADDRESSED:
- case USBD_STATE_CONFIGURED:
- {
- #if ( USBD_SELF_POWERED == 1)
- pdev->dev_config_status = USB_CONFIG_SELF_POWERED;
- #else
- pdev->dev_config_status = 0;
- #endif
-
- if (pdev->dev_remote_wakeup)
- {
- pdev->dev_config_status |= USB_CONFIG_REMOTE_WAKEUP;
- }
-
- USBD_CtlSendData(pdev,
- (const uint8_t *)& pdev->dev_config_status,
- 2);
- }
- break;
-
- default :
- ret = USBD_FAIL;
- break;
- }
- return ret;
- }
- /**
- * @brief USBD_SetFeature
- * Handle Set device feature request
- * @param pdev: device instance
- * @param req: usb request
- * @retval status
- */
- static USBD_StatusTypeDef USBD_SetFeature(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req)
- {
- USBD_StatusTypeDef ret = USBD_OK;
- if (req->wValue == USB_FEATURE_REMOTE_WAKEUP)
- {
- pdev->dev_remote_wakeup = 1;
- pdev->pClass->Setup(pdev, req);
- USBD_CtlSendStatus(pdev);
- }
- else
- {
- ret = USBD_FAIL;
- }
- return ret;
- }
- /**
- * @brief USBD_ClrFeature
- * Handle clear device feature request
- * @param pdev: device instance
- * @param req: usb request
- * @retval status
- */
- static USBD_StatusTypeDef USBD_ClrFeature(USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req)
- {
- USBD_StatusTypeDef ret = USBD_OK;
- switch (pdev->dev_state)
- {
- case USBD_STATE_ADDRESSED:
- case USBD_STATE_CONFIGURED:
- {
- if (req->wValue == USB_FEATURE_REMOTE_WAKEUP)
- {
- pdev->dev_remote_wakeup = 0;
- pdev->pClass->Setup(pdev, req);
- USBD_CtlSendStatus(pdev);
- }
- else
- {
- ret = USBD_FAIL;
- }
- }
- break;
-
- default :
- ret = USBD_FAIL;
- break;
- }
- return ret;
- }
- /**
- * @brief USBD_ParseSetupRequest
- * Copy buffer into setup structure
- * @param pdev: device instance
- * @param req: usb request
- * @retval None
- */
- void USBD_ParseSetupRequest(USBD_SetupReqTypedef *req, uint8_t *pdata)
- {
- req->bmRequest = *(uint8_t *) (pdata);
- req->bRequest = *(uint8_t *) (pdata + 1);
- req->wValue = SWAPBYTE (pdata + 2);
- req->wIndex = SWAPBYTE (pdata + 4);
- req->wLength = SWAPBYTE (pdata + 6);
- }
- /**
- * @brief USBD_CtlError
- * Handle USB low level Error
- * @param pdev: device instance
- * @param req: usb request
- * @retval None
- */
- void USBD_CtlError( USBD_HandleTypeDef *pdev,
- USBD_SetupReqTypedef *req)
- {
- USBD_LL_StallEP(pdev, 0x80);
- USBD_LL_StallEP(pdev, 0);
- }
- /**
- * @brief USBD_GetString
- * Convert Ascii string into unicode one
- * @param desc : descriptor buffer
- * @param unicode : Formatted string buffer (unicode)
- * @param len : descriptor length
- * @retval None
- */
- void USBD_GetString(uint8_t *desc, uint8_t *unicode, uint16_t *len)
- {
- uint8_t idx = 0;
-
- if (desc != NULL)
- {
- *len = USBD_GetLen(desc) * 2 + 2;
- unicode[idx++] = *len;
- unicode[idx++] = USB_DESC_TYPE_STRING;
-
- while (*desc != '\0')
- {
- unicode[idx++] = *desc++;
- unicode[idx++] = 0x00;
- }
- }
- }
- /**
- * @brief USBD_GetLen
- * return the string length
- * @param buf : pointer to the ascii string buffer
- * @retval string length
- */
- static uint8_t USBD_GetLen(uint8_t *buf)
- {
- uint8_t len = 0;
- while (*buf != '\0')
- {
- len++;
- buf++;
- }
- return len;
- }
|