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