1 /* ---------------------------------------------------------------------------- 2 * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. All rights reserved. 3 * Description: LiteOS USB Driver DFU Protocol HeadFile 4 * Author: huangjieliang 5 * Create: 2017-12-25 6 * Redistribution and use in source and binary forms, with or without modification, 7 * are permitted provided that the following conditions are met: 8 * 1. Redistributions of source code must retain the above copyright notice, this list of 9 * conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 * of conditions and the following disclaimer in the documentation and/or other materials 12 * provided with the distribution. 13 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 14 * to endorse or promote products derived from this software without specific prior written 15 * permission. 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * --------------------------------------------------------------------------- */ 28 /* ---------------------------------------------------------------------------- 29 * Notice of Export Control Law 30 * =============================================== 31 * Huawei LiteOS may be subject to applicable export control laws and regulations, which might 32 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located. 33 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such 34 * applicable export control laws and regulations. 35 * --------------------------------------------------------------------------- */ 36 37 #ifndef _USB_F_DFU_H_ 38 #define _USB_F_DFU_H_ 39 40 #include "gadget/dfu.h" 41 #include "gadget/usbdev.h" 42 #include "implementation/global_implementation.h" 43 44 #ifdef __cplusplus 45 #if __cplusplus 46 extern "C" { 47 #endif /* __cplusplus */ 48 #endif /* __cplusplus */ 49 50 #define DFU_NAME_MAX_LEN 32 51 52 struct usb_dfu_dev 53 { 54 struct dfu_getstatus_response_s dfu_status; 55 int dfu_state; 56 uint32_t seq_num; 57 }; 58 59 struct usb_dfu_entity 60 { 61 char dfu_name[DFU_NAME_MAX_LEN]; 62 void *ram_addr; 63 uint32_t ram_size; 64 int (*dfu_read)(uint8_t *buf, uint32_t len); 65 int (*dfu_write)(const uint8_t *buf, uint32_t len); 66 uint64_t trans_size; 67 uint32_t trans_complete; 68 uint32_t seq_num; 69 uint32_t offset; 70 }; 71 72 struct string_des 73 { 74 char *str; 75 int len; 76 }; 77 78 /* The status of Flash upgrade state */ 79 80 #define DFU_FLASH_STATE_UPGRADING 1 81 #define DFU_FLASH_STATE_UPGRADED 0 82 #define DFU_FLASH_STATE_ERROR 2 83 84 /* Minimum time, in milliseconds, that the host should wait before 85 * sending a subsequent DFU_GETSTATUS request. 86 */ 87 88 #define USB_DFU_POLLTIMEOUT 100 89 90 /* Define the return value for request */ 91 92 #define USB_DFU_RET_ERR -1 93 #define USB_DFU_RET_OK 0 94 #define USB_DFU_RET_STATUS_LEN 6 95 #define USB_DFU_RET_STATE_LEN 1 96 #define USB_DFU_STATUS_OK 0 97 98 /* Define the type for request */ 99 100 #define USB_DFU_DETACH 0 101 #define USB_DFU_DNLOAD 1 102 #define USB_DFU_UPLOAD 2 103 #define USB_DFU_GET_STATUS 3 104 #define USB_DFU_CLR_STATUS 4 105 #define USB_DFU_GET_STATE 5 106 #define USB_DFU_ABORT 6 107 #define USB_DFU_FLASH_STATE 7 108 109 /* Define the state for request */ 110 111 #define USB_DFU_APP_IDLE 0 112 #define USB_DFU_APP_DETACH 1 113 #define USB_DFU_IDLE 2 114 #define USB_DFU_DNLOAD_SYNC 3 115 #define USB_DFU_DNBUSY 4 116 #define USB_DFU_DNLOAD_IDLE 5 117 #define USB_DFU_MANIFEST_SYNC 6 118 #define USB_DFU_MANIFEST 7 119 #define USB_DFU_MANIFEST_WAIT_RESET 8 120 #define USB_DFU_UPLOAD_IDLE 9 121 #define USB_DFU_ERROR 10 122 123 /* Define the status for request */ 124 125 #define USB_DFU_OK 0x00 126 #define USB_DFU_ERR_TARGET 0x01 127 #define USB_DFU_ERR_FILE 0x02 128 #define USB_DFU_ERR_WRITE 0x03 129 #define USB_DFU_ERR_ERASE 0x04 130 #define USB_DFU_ERR_CHECK_ERASED 0x05 131 #define USB_DFU_ERR_PROG 0x06 132 #define USB_DFU_ERR_VERIFY 0x07 133 #define USB_DFU_ERR_ADDRESS 0x08 134 #define USB_DFU_ERR_NOTDONE 0x09 135 #define USB_DFU_ERR_FIRMWARE 0x0A 136 #define USB_DFU_ERR_VENDOR 0x0B 137 #define USB_DFU_ERR_USBR 0x0C 138 #define USB_DFU_ERR_POR 0x0D 139 #define USB_DFU_ERR_UNKNOWN 0x0E 140 #define USB_DFU_ERR_STALLEDPKT 0x0F 141 142 extern struct usb_config_descriptor g_dfu_config_desc; 143 144 bool usb_dfu_running(void); 145 void usbdev_dfu_mkdevdesc(uint8_t *buf); 146 int usbdev_dfu_mkstrdesc(uint8_t id, uint8_t *buf); 147 void usbdev_dfu_set_config(void); 148 void usbdev_dfu_init(void); 149 void usbdev_dfu_dev_init(void); 150 void usbdev_dfu_dev_deinit(void); 151 void usbdev_dfu_transaction_cleanup(void); 152 int usbdev_dfu_class_requests(const struct usbdev_s *dev, 153 const struct usb_device_request *ctrl, 154 struct usbdev_req_s *ctrlreq); 155 156 void usb_dfu_free_entities(void); 157 int usb_dfu_init_env_entities(char *type, char *envstr, char *devstr); 158 struct usb_dfu_entity *usb_dfu_get_entity(int alter); 159 void set_flash_state(volatile uint8_t flash_state); 160 void get_flash_state(struct usbdev_req_s *req); 161 uint32_t usb_dfu_update_status(void); 162 uint64_t *usb_dfu_update_size_get(void); 163 int usb_dfu_read(struct usb_dfu_entity *dfu, void *buf, int size, uint32_t blk_seq_num); 164 int usb_dfu_write(struct usb_dfu_entity *dfu, void *buf, int size, uint32_t blk_seq_num); 165 extern char uart_putc(char); 166 167 void set_manufacturer_string(struct string_des *des); 168 void set_serial_number_string(struct string_des *des); 169 void set_product_string(struct string_des *des); 170 171 #ifdef __cplusplus 172 #if __cplusplus 173 } 174 #endif /* __cplusplus */ 175 #endif /* __cplusplus */ 176 177 #endif /* _USB_F_DFU_H_ */ 178