123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- /**
- **************************************************************************
- * @file usbd_sdr.c
- * @version v2.0.6
- * @date 2021-12-31
- * @brief usb standard device request
- **************************************************************************
- * Copyright notice & Disclaimer
- *
- * The software Board Support Package (BSP) that is made available to
- * download from Artery official website is the copyrighted work of Artery.
- * Artery authorizes customers to use, copy, and distribute the BSP
- * software and its related documentation for the purpose of design and
- * development in conjunction with Artery microcontrollers. Use of the
- * software is governed by this copyright notice and the following disclaimer.
- *
- * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
- * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
- * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
- * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
- * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
- *
- **************************************************************************
- */
- #include "usbd_sdr.h"
- #include "common_config.h"
- #ifdef PRINTF_STDLIB
- #include <stdio.h>
- #endif
- #ifdef PRINTF_CUSTOM
- #include "tinystdio.h"
- #endif
- /** @addtogroup AT32F403A_407_middlewares_usbd_drivers
- * @{
- */
-
- /** @defgroup USBD_drivers_standard_request
- * @brief usb device standard_request
- * @{
- */
- /** @defgroup USBD_sdr_private_functions
- * @{
- */
- static usb_sts_type usbd_get_descriptor(usbd_core_type *udev);
- static usb_sts_type usbd_set_address(usbd_core_type *udev);
- static usb_sts_type usbd_get_status(usbd_core_type *udev);
- static usb_sts_type usbd_clear_feature(usbd_core_type *udev);
- static usb_sts_type usbd_set_feature(usbd_core_type *udev);
- static usb_sts_type usbd_get_configuration(usbd_core_type *udev);
- static usb_sts_type usbd_set_configuration(usbd_core_type *udev);
- /**
- * @brief usb parse standard setup request
- * @param setup: setup structure
- * @param buf: setup buffer
- * @retval none
- */
- void usbd_setup_request_parse(usb_setup_type *setup, uint8_t *buf)
- {
- setup->bmRequestType = *(uint8_t *) buf;
- setup->bRequest = *(uint8_t *) (buf + 1);
- setup->wValue = SWAPBYTE(buf + 2);
- setup->wIndex = SWAPBYTE(buf + 4);
- setup->wLength = SWAPBYTE(buf + 6);
- }
- /**
- * @brief get usb standard device description request
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- static usb_sts_type usbd_get_descriptor(usbd_core_type *udev)
- {
- usb_sts_type ret = USB_OK;
- uint16_t len = 0;
- usbd_desc_t *desc = NULL;
- uint8_t desc_type = udev->setup.wValue >> 8;
-
- switch(desc_type)
- {
- case USB_DESCIPTOR_TYPE_DEVICE:
- desc = udev->desc_handler->get_device_descriptor();
- len = desc->length;
-
- if ((udev->setup.wLength == 64) || (udev->conn_state == USB_CONN_STATE_DEFAULT))
- len = 8;
-
- break;
- case USB_DESCIPTOR_TYPE_CONFIGURATION:
- desc = udev->desc_handler->get_device_configuration();
- len = desc->length;
- break;
- case USB_DESCIPTOR_TYPE_STRING:
- {
- uint8_t str_desc = (uint8_t)udev->setup.wValue;
-
- switch(str_desc)
- {
- case USB_LANGID_STRING:
- desc = udev->desc_handler->get_device_lang_id();
- len = desc->length;
- break;
- case USB_MFC_STRING:
- desc = udev->desc_handler->get_device_manufacturer_string();
- len = desc->length;
- break;
- case USB_PRODUCT_STRING:
- desc = udev->desc_handler->get_device_product_string();
- len = desc->length;
- break;
- case USB_SERIAL_STRING:
- desc = udev->desc_handler->get_device_serial_string();
- len = desc->length;
- break;
- case USB_CONFIG_STRING:
- desc = udev->desc_handler->get_device_config_string();
- len = desc->length;
- break;
- case USB_INTERFACE_STRING:
- desc = udev->desc_handler->get_device_interface_string();
- len = desc->length;
- break;
- default:
- usbd_ctrl_unsupport(udev);
- return ret;
- }
- break;
- }
- case USB_DESCIPTOR_TYPE_DEVICE_QUALIFIER:
- usbd_ctrl_unsupport(udev);
- break;
- case USB_DESCIPTOR_TYPE_OTHER_SPEED:
- usbd_ctrl_unsupport(udev);
- return ret;
- default:
- usbd_ctrl_unsupport(udev);
- return ret;
- }
-
- if(desc != NULL)
- {
- if((len != 0) && (udev->setup.wLength != 0))
- {
- len = MIN(len, udev->setup.wLength);
- usbd_ctrl_send(udev, desc->descriptor, len);
- }
- }
- return ret;
- }
- /**
- * @brief this request sets the device address
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- static usb_sts_type usbd_set_address(usbd_core_type *udev)
- {
- usb_sts_type ret = USB_OK;
- usb_setup_type *setup = &udev->setup;
- uint8_t dev_addr;
-
- /* if wIndex or wLength are non-zero, then the behavior of
- the device is not specified
- */
- if(setup->wIndex == 0 && setup->wLength == 0)
- {
- dev_addr = (uint8_t)(setup->wValue) & 0x7f;
-
- /* device behavior when this request is received
- while the device is in the configured state is not specified.*/
- if(udev->conn_state == USB_CONN_STATE_CONFIGURED )
- {
- usbd_ctrl_unsupport(udev);
- }
- else
- {
- udev->device_addr = dev_addr;
-
- if(dev_addr != 0)
- {
- udev->conn_state = USB_CONN_STATE_ADDRESSED;
- }
- else
- {
- udev->conn_state = USB_CONN_STATE_DEFAULT;
- }
- usbd_ctrl_send_status(udev);
- }
- }
- else
- {
- usbd_ctrl_unsupport(udev);
- }
- return ret;
- }
- /**
- * @brief get usb status request
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- static usb_sts_type usbd_get_status(usbd_core_type *udev)
- {
- usb_sts_type ret = USB_OK;
- switch(udev->conn_state)
- {
- case USB_CONN_STATE_ADDRESSED:
- case USB_CONN_STATE_CONFIGURED:
- if(udev->remote_wakup)
- {
- udev->config_status |= USB_CONF_REMOTE_WAKEUP;
- }
- usbd_ctrl_send(udev, (uint8_t *)(&udev->config_status), 2);
- break;
- default:
- usbd_ctrl_unsupport(udev);
- break;
- }
- return ret;
- }
- /**
- * @brief clear usb feature request
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- static usb_sts_type usbd_clear_feature(usbd_core_type *udev)
- {
- usb_sts_type ret = USB_OK;
- usb_setup_type *setup = &udev->setup;
- switch(udev->conn_state)
- {
- case USB_CONN_STATE_ADDRESSED:
- case USB_CONN_STATE_CONFIGURED:
- if(setup->wValue == USB_FEATURE_REMOTE_WAKEUP)
- {
- udev->remote_wakup = 0;
- udev->config_status &= ~USB_CONF_REMOTE_WAKEUP;
- udev->class_handler->setup_handler(udev, &udev->setup);
- usbd_ctrl_send_status(udev);
- }
- break;
- default:
- usbd_ctrl_unsupport(udev);
- break;
- }
- return ret;
- }
- /**
- * @brief set usb feature request
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- static usb_sts_type usbd_set_feature(usbd_core_type *udev)
- {
- usb_sts_type ret = USB_OK;
- usb_setup_type *setup = &udev->setup;
- if(setup->wValue == USB_FEATURE_REMOTE_WAKEUP)
- {
- udev->remote_wakup = 1;
- udev->class_handler->setup_handler(udev, &udev->setup);
- usbd_ctrl_send_status(udev);
- }
- return ret;
- }
- /**
- * @brief get usb configuration request
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- static usb_sts_type usbd_get_configuration(usbd_core_type *udev)
- {
- usb_sts_type ret = USB_OK;
- usb_setup_type *setup = &udev->setup;
- if(setup->wLength != 1)
- {
- usbd_ctrl_unsupport(udev);
- }
- else
- {
- switch(udev->conn_state)
- {
- case USB_CONN_STATE_ADDRESSED:
- udev->default_config = 0;
- usbd_ctrl_send(udev, (uint8_t *)(&udev->default_config), 1);
- break;
- case USB_CONN_STATE_CONFIGURED:
- usbd_ctrl_send(udev, (uint8_t *)(&udev->dev_config), 1);
- break;
- default:
- usbd_ctrl_unsupport(udev);
- break;
- }
- }
- return ret;
- }
- /**
- * @brief sets the usb device configuration request
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- static usb_sts_type usbd_set_configuration(usbd_core_type *udev)
- {
- usb_sts_type ret = USB_OK;
- static uint8_t config_value;
- usb_setup_type *setup = &udev->setup;
- config_value = (uint8_t)setup->wValue;
-
- if(setup->wIndex == 0 && setup->wLength == 0)
- {
- switch(udev->conn_state)
- {
- case USB_CONN_STATE_ADDRESSED:
- if(config_value)
- {
- udev->dev_config = config_value;
- udev->conn_state = USB_CONN_STATE_CONFIGURED;
- udev->class_handler->init_handler(udev);
- usbd_ctrl_send_status(udev);
- }
- else
- {
- usbd_ctrl_send_status(udev);
- }
-
- break;
- case USB_CONN_STATE_CONFIGURED:
- if(config_value == 0)
- {
- udev->conn_state = USB_CONN_STATE_ADDRESSED;
- udev->dev_config = config_value;
- udev->class_handler->clear_handler(udev);
- usbd_ctrl_send_status(udev);
- }
- else if(config_value == udev->dev_config)
- {
- udev->class_handler->clear_handler(udev);
- udev->dev_config = config_value;
- udev->class_handler->init_handler(udev);
- usbd_ctrl_send_status(udev);
- }
- else
- {
- usbd_ctrl_send_status(udev);
- }
- break;
- default:
- usbd_ctrl_unsupport(udev);
- break;
- }
- }
- else
- {
- usbd_ctrl_unsupport(udev);
- }
- return ret;
- }
- /**
- * @brief standard usb device requests
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- usb_sts_type usbd_device_request(usbd_core_type *udev)
- {
- usb_sts_type ret = USB_OK;
- usb_setup_type *setup = &udev->setup;
- if((setup->bmRequestType & USB_REQ_TYPE_RESERVED) != USB_REQ_TYPE_STANDARD)
- {
- udev->class_handler->setup_handler(udev, &udev->setup);
- return ret;
- }
- switch(udev->setup.bRequest)
- {
- case USB_STD_REQ_GET_STATUS:
- usbd_get_status(udev);
- break;
- case USB_STD_REQ_CLEAR_FEATURE:
- usbd_clear_feature(udev);
- break;
- case USB_STD_REQ_SET_FEATURE:
- usbd_set_feature(udev);
- break;
- case USB_STD_REQ_SET_ADDRESS:
- usbd_set_address(udev);
- break;
- case USB_STD_REQ_GET_DESCRIPTOR:
- usbd_get_descriptor(udev);
- break;
- case USB_STD_REQ_GET_CONFIGURATION:
- usbd_get_configuration(udev);
- break;
- case USB_STD_REQ_SET_CONFIGURATION:
- usbd_set_configuration(udev);
- break;
- default:
- usbd_ctrl_unsupport(udev);
- break;
- }
- return ret;
- }
- /**
- * @brief standard usb interface requests
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- usb_sts_type usbd_interface_request(usbd_core_type *udev)
- {
- usb_sts_type ret = USB_OK;
- usb_setup_type *setup = &udev->setup;
-
- switch(udev->conn_state)
- {
- case USB_CONN_STATE_CONFIGURED:
- udev->class_handler->setup_handler(udev, &udev->setup);
- if(setup->wLength == 0)
- {
- usbd_ctrl_send_status(udev);
- }
- break;
- default:
- usbd_ctrl_unsupport(udev);
- break;
- }
- return ret;
- }
- /**
- * @brief standard usb endpoint requests
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- usb_sts_type usbd_endpoint_request(usbd_core_type *udev)
- {
- usb_sts_type ret = USB_OK;
- usb_setup_type *setup = &udev->setup;
- uint8_t ept_addr = LBYTE(setup->wIndex);
- usb_ept_info *ept_info;
-
- if((setup->bmRequestType & USB_REQ_TYPE_RESERVED) == USB_REQ_TYPE_CLASS)
- {
- udev->class_handler->setup_handler(udev, &udev->setup);
- }
- switch(setup->bRequest)
- {
- case USB_STD_REQ_GET_STATUS:
- switch(udev->conn_state)
- {
- case USB_CONN_STATE_ADDRESSED:
- if((ept_addr & 0x7F) != 0)
- {
- usbd_set_stall(udev, ept_addr);
- }
- break;
- case USB_CONN_STATE_CONFIGURED:
- {
- if((ept_addr & 0x80) != 0)
- {
- ept_info = &udev->ept_in[ept_addr & 0x7F];
- }
- else
- {
- ept_info = &udev->ept_out[ept_addr & 0x7F];
- }
- if(ept_info->stall == 1)
- {
- ept_info->status = 0x0001;
- }
- else
- {
- ept_info->status = 0x0000;
- }
- usbd_ctrl_send(udev, (uint8_t *)(&ept_info->status), 2);
- }
- break;
- default:
- usbd_ctrl_unsupport(udev);
- break;
- }
- break;
- case USB_STD_REQ_CLEAR_FEATURE:
- switch(udev->conn_state)
- {
- case USB_CONN_STATE_ADDRESSED:
- if((ept_addr != 0x00) && (ept_addr != 0x80))
- {
- usbd_set_stall(udev, ept_addr);
- }
- break;
- case USB_CONN_STATE_CONFIGURED:
- if(setup->wValue == USB_FEATURE_EPT_HALT)
- {
- if((ept_addr & 0x7F) != 0x00 )
- {
- usbd_clear_stall(udev, ept_addr);
- udev->class_handler->setup_handler(udev, &udev->setup);
- }
- usbd_ctrl_send_status(udev);
- }
- break;
- default:
- usbd_ctrl_unsupport(udev);
- break;
- }
- break;
- case USB_STD_REQ_SET_FEATURE:
- switch(udev->conn_state)
- {
- case USB_CONN_STATE_ADDRESSED:
- if((ept_addr != 0x00) && (ept_addr != 0x80))
- {
- usbd_set_stall(udev, ept_addr);
- }
- break;
- case USB_CONN_STATE_CONFIGURED:
- if(setup->wValue == USB_FEATURE_EPT_HALT)
- {
- if((ept_addr != 0x00) && (ept_addr != 0x80))
- {
- usbd_set_stall(udev, ept_addr);
- }
- }
- udev->class_handler->setup_handler(udev, &udev->setup);
- usbd_ctrl_send_status(udev);
- break;
- default:
- usbd_ctrl_unsupport(udev);
- break;
- }
- break;
- default:
- usbd_ctrl_unsupport(udev);
- break;
- }
- return ret;
- }
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
|