1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2 * Modifications by Jacob Alexander 2014-2015 <haata@kiibohd.com>
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.
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.
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/>.
18 // ----- Local Includes -----
26 // ----- Functions -----
28 void dfu_write_done( enum dfu_status err, struct dfu_ctx *ctx )
31 if (ctx->status == DFU_STATUS_OK) {
33 case DFU_STATE_dfuDNBUSY:
34 ctx->state = DFU_STATE_dfuDNLOAD_IDLE;
40 ctx->state = DFU_STATE_dfuERROR;
44 static void dfu_dnload_complete( void *buf, ssize_t len, void *cbdata )
46 struct dfu_ctx *ctx = cbdata;
49 ctx->state = DFU_STATE_dfuDNBUSY;
51 ctx->state = DFU_STATE_dfuMANIFEST;
52 ctx->status = ctx->finish_write(buf, ctx->off, len);
56 if (ctx->status != DFU_STATUS_async)
57 dfu_write_done(ctx->status, ctx);
59 usb_handle_control_status(ctx->state == DFU_STATE_dfuERROR);
62 static void dfu_reset_system( void *buf, ssize_t len, void *cbdata )
67 static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
69 struct dfu_ctx *ctx = data;
72 switch ((enum dfu_ctrl_req_code)req->bRequest)
74 case USB_CTRL_REQ_DFU_DNLOAD: {
78 case DFU_STATE_dfuIDLE:
81 case DFU_STATE_dfuDNLOAD_IDLE:
88 * XXX we are not allowed to STALL here, and we need to eat all transferred data.
89 * better not allow setup_write to break the protocol.
91 ctx->status = ctx->setup_write(ctx->off, req->wLength, &buf);
92 if (ctx->status != DFU_STATUS_OK) {
93 ctx->state = DFU_STATE_dfuERROR;
98 usb_ep0_rx(buf, req->wLength, dfu_dnload_complete, ctx);
100 dfu_dnload_complete(NULL, 0, ctx);
103 case USB_CTRL_REQ_DFU_UPLOAD: {
108 switch ( ctx->state )
110 case DFU_STATE_dfuIDLE:
112 case DFU_STATE_dfuUPLOAD_IDLE:
118 // Find which sector to read
119 ctx->status = ctx->setup_read(ctx->off, &len, &buf);
120 print("UPLOAD off:");
121 printHex( ctx->off );
125 printHex( (uint32_t)buf );
128 if ( ctx->status != DFU_STATUS_OK || len > req->wLength )
130 ctx->state = DFU_STATE_dfuERROR;
131 goto err_have_status;
134 // Send bytes to Host
137 usb_ep0_rx( buf, len, NULL, NULL );
141 ctx->state = DFU_STATE_dfuIDLE;
148 case USB_CTRL_REQ_DFU_GETSTATUS: {
149 struct dfu_status_t st;
151 st.bState = ctx->state;
152 st.bStatus = ctx->status;
153 st.bwPollTimeout = 1000; /* XXX */
156 * If we're in DFU_STATE_dfuMANIFEST, we just finished
157 * the download, and we're just about to send our last
158 * status report. Once the report has been sent, go
159 * and reset the system to put the new firmware into
162 usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
163 if (ctx->state == DFU_STATE_dfuMANIFEST) {
164 usb_handle_control_status_cb(dfu_reset_system);
169 case USB_CTRL_REQ_DFU_CLRSTATUS:
170 ctx->state = DFU_STATE_dfuIDLE;
171 ctx->status = DFU_STATUS_OK;
173 case USB_CTRL_REQ_DFU_GETSTATE: {
174 uint8_t st = ctx->state;
175 usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
178 case USB_CTRL_REQ_DFU_ABORT:
179 switch (ctx->state) {
180 case DFU_STATE_dfuIDLE:
181 case DFU_STATE_dfuDNLOAD_IDLE:
182 case DFU_STATE_dfuUPLOAD_IDLE:
183 ctx->state = DFU_STATE_dfuIDLE;
197 ctx->status = DFU_STATUS_errSTALLEDPKT;
199 ctx->state = DFU_STATE_dfuERROR;
201 usb_handle_control_status(fail);
206 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 )
208 ctx->state = DFU_STATE_dfuIDLE;
209 ctx->setup_read = setup_read;
210 ctx->setup_write = setup_write;
211 ctx->finish_write = finish_write;
212 usb_attach_function(&dfu_function, &ctx->header);
215 const struct usbd_function dfu_function = {
216 .control = dfu_handle_control,
217 .interface_count = USB_FUNCTION_DFU_IFACE_COUNT,