]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/dfu.h
Adding initial dfu-upload code and debugging for Bootloader.
[kiibohd-controller.git] / Bootloader / dfu.h
1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2  * Modifications by Jacob Alexander 2014-2015 <haata@kiibohd.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #ifndef _USB_DFU_H
19 #define _USB_DFU_H
20
21 // ----- Compiler Includes -----
22
23 #include <sys/types.h>
24
25
26
27 // ----- Defines -----
28
29 #define USB_FUNCTION_DFU_IFACE_COUNT 1
30
31
32 #ifndef USB_DFU_TRANSFER_SIZE
33 #define USB_DFU_TRANSFER_SIZE   FLASH_SECTOR_SIZE
34 #endif
35
36 #define USB_FUNCTION_DESC_DFU_DECL                         \
37         struct dfu_function_desc
38
39 #define USB_FUNCTION_DFU_IFACE_COUNT    1
40 #define USB_FUNCTION_DFU_RX_EP_COUNT    0
41 #define USB_FUNCTION_DFU_TX_EP_COUNT    0
42
43
44
45 // ----- Enumerations -----
46
47 enum dfu_dev_subclass {
48         USB_DEV_SUBCLASS_APP_DFU = 0x01
49 };
50
51 enum dfu_dev_proto {
52         USB_DEV_PROTO_DFU_APP = 0x01,
53         USB_DEV_PROTO_DFU_DFU = 0x02
54 };
55
56 enum dfu_ctrl_req_code {
57         USB_CTRL_REQ_DFU_DETACH = 0,
58         USB_CTRL_REQ_DFU_DNLOAD = 1,
59         USB_CTRL_REQ_DFU_UPLOAD = 2,
60         USB_CTRL_REQ_DFU_GETSTATUS = 3,
61         USB_CTRL_REQ_DFU_CLRSTATUS = 4,
62         USB_CTRL_REQ_DFU_GETSTATE = 5,
63         USB_CTRL_REQ_DFU_ABORT = 6
64 };
65
66 enum dfu_status {
67         DFU_STATUS_async = 0xff,
68         DFU_STATUS_OK = 0x00,
69         DFU_STATUS_errTARGET = 0x01,
70         DFU_STATUS_errFILE = 0x02,
71         DFU_STATUS_errWRITE = 0x03,
72         DFU_STATUS_errERASE = 0x04,
73         DFU_STATUS_errCHECK_ERASED = 0x05,
74         DFU_STATUS_errPROG = 0x06,
75         DFU_STATUS_errVERIFY = 0x07,
76         DFU_STATUS_errADDRESS = 0x08,
77         DFU_STATUS_errNOTDONE = 0x09,
78         DFU_STATUS_errFIRMWARE = 0x0a,
79         DFU_STATUS_errVENDOR = 0x0b,
80         DFU_STATUS_errUSBR = 0x0c,
81         DFU_STATUS_errPOR = 0x0d,
82         DFU_STATUS_errUNKNOWN = 0x0e,
83         DFU_STATUS_errSTALLEDPKT = 0x0f
84 };
85
86 enum dfu_state {
87         DFU_STATE_appIDLE = 0,
88         DFU_STATE_appDETACH = 1,
89         DFU_STATE_dfuIDLE = 2,
90         DFU_STATE_dfuDNLOAD_SYNC = 3,
91         DFU_STATE_dfuDNBUSY = 4,
92         DFU_STATE_dfuDNLOAD_IDLE = 5,
93         DFU_STATE_dfuMANIFEST_SYNC = 6,
94         DFU_STATE_dfuMANIFEST = 7,
95         DFU_STATE_dfuMANIFEST_WAIT_RESET = 8,
96         DFU_STATE_dfuUPLOAD_IDLE = 9,
97         DFU_STATE_dfuERROR = 10
98 };
99
100
101
102 // ----- Structs -----
103
104 struct dfu_status_t {
105         enum dfu_status bStatus : 8;
106         uint32_t bwPollTimeout : 24;
107         enum dfu_state bState : 8;
108         uint8_t iString;
109 } __packed;
110 CTASSERT_SIZE_BYTE(struct dfu_status_t, 6);
111
112
113 typedef enum dfu_status (*dfu_setup_read_t)(size_t off, size_t *len, void **buf);
114 typedef enum dfu_status (*dfu_setup_write_t)(size_t off, size_t len, void **buf);
115 typedef enum dfu_status (*dfu_finish_write_t)(void *, size_t off, size_t len);
116 typedef void (*dfu_detach_t)(void);
117
118 struct dfu_ctx {
119         struct usbd_function_ctx_header header;
120         enum dfu_state state;
121         enum dfu_status status;
122         dfu_setup_read_t setup_read;
123         dfu_setup_write_t setup_write;
124         dfu_finish_write_t finish_write;
125         size_t off;
126         size_t len;
127 };
128
129
130 struct dfu_desc_functional {
131         uint8_t bLength;
132         struct usb_desc_type_t bDescriptorType; /* = class DFU/0x1 FUNCTIONAL */
133         union {
134                 struct {
135                         uint8_t can_download : 1;
136                         uint8_t can_upload : 1;
137                         uint8_t manifestation_tolerant : 1;
138                         uint8_t will_detach : 1;
139                         uint8_t _rsvd0 : 4;
140                 };
141                 uint8_t bmAttributes;
142         };
143         uint16_t wDetachTimeOut;
144         uint16_t wTransferSize;
145         struct usb_bcd_t bcdDFUVersion;
146 } __packed;
147 CTASSERT_SIZE_BYTE(struct dfu_desc_functional, 9);
148
149 struct dfu_function_desc {
150         struct usb_desc_iface_t iface;
151         struct dfu_desc_functional dfu;
152 };
153
154
155 extern const struct usbd_function dfu_function;
156 extern const struct usbd_function dfu_app_function;
157
158
159
160 // ----- Functions -----
161
162 void dfu_write_done( enum dfu_status, struct dfu_ctx *ctx );
163 void dfu_init( dfu_setup_read_t setup_read, dfu_setup_write_t setup_write, dfu_finish_write_t finish_write, struct dfu_ctx *ctx );
164 void dfu_app_init( dfu_detach_t detachcb );
165
166 #endif