1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2 * Modifications by Jacob Alexander 2014 <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 -----
25 // ----- Functions -----
27 void dfu_write_done( enum dfu_status err, struct dfu_ctx *ctx )
30 if (ctx->status == DFU_STATUS_OK) {
32 case DFU_STATE_dfuDNBUSY:
33 ctx->state = DFU_STATE_dfuDNLOAD_IDLE;
39 ctx->state = DFU_STATE_dfuERROR;
43 static void dfu_dnload_complete( void *buf, ssize_t len, void *cbdata )
45 struct dfu_ctx *ctx = cbdata;
48 ctx->state = DFU_STATE_dfuDNBUSY;
50 ctx->state = DFU_STATE_dfuMANIFEST;
51 ctx->status = ctx->finish_write(buf, ctx->off, len);
55 if (ctx->status != DFU_STATUS_async)
56 dfu_write_done(ctx->status, ctx);
58 usb_handle_control_status(ctx->state == DFU_STATE_dfuERROR);
61 static void dfu_reset_system( void *buf, ssize_t len, void *cbdata )
66 static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
68 struct dfu_ctx *ctx = data;
71 switch ((enum dfu_ctrl_req_code)req->bRequest) {
72 case USB_CTRL_REQ_DFU_DNLOAD: {
76 case DFU_STATE_dfuIDLE:
79 case DFU_STATE_dfuDNLOAD_IDLE:
86 * XXX we are not allowed to STALL here, and we need to eat all transferred data.
87 * better not allow setup_write to break the protocol.
89 ctx->status = ctx->setup_write(ctx->off, req->wLength, &buf);
90 if (ctx->status != DFU_STATUS_OK) {
91 ctx->state = DFU_STATE_dfuERROR;
96 usb_ep0_rx(buf, req->wLength, dfu_dnload_complete, ctx);
98 dfu_dnload_complete(NULL, 0, ctx);
101 case USB_CTRL_REQ_DFU_GETSTATUS: {
102 struct dfu_status_t st;
104 st.bState = ctx->state;
105 st.bStatus = ctx->status;
106 st.bwPollTimeout = 1000; /* XXX */
108 * If we're in DFU_STATE_dfuMANIFEST, we just finished
109 * the download, and we're just about to send our last
110 * status report. Once the report has been sent, go
111 * and reset the system to put the new firmware into
114 usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
115 if (ctx->state == DFU_STATE_dfuMANIFEST) {
116 usb_handle_control_status_cb(dfu_reset_system);
121 case USB_CTRL_REQ_DFU_CLRSTATUS:
122 ctx->state = DFU_STATE_dfuIDLE;
123 ctx->status = DFU_STATUS_OK;
125 case USB_CTRL_REQ_DFU_GETSTATE: {
126 uint8_t st = ctx->state;
127 usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
130 case USB_CTRL_REQ_DFU_ABORT:
131 switch (ctx->state) {
132 case DFU_STATE_dfuIDLE:
133 case DFU_STATE_dfuDNLOAD_IDLE:
134 /* case DFU_STATE_dfuUPLOAD_IDLE: */
135 ctx->state = DFU_STATE_dfuIDLE;
141 /* case USB_CTRL_REQ_DFU_UPLOAD: */
150 ctx->status = DFU_STATUS_errSTALLEDPKT;
152 ctx->state = DFU_STATE_dfuERROR;
154 usb_handle_control_status(fail);
159 void dfu_init( dfu_setup_write_t setup_write, dfu_finish_write_t finish_write, struct dfu_ctx *ctx )
161 ctx->state = DFU_STATE_dfuIDLE;
162 ctx->setup_write = setup_write;
163 ctx->finish_write = finish_write;
164 usb_attach_function(&dfu_function, &ctx->header);
167 const struct usbd_function dfu_function = {
168 .control = dfu_handle_control,
169 .interface_count = USB_FUNCTION_DFU_IFACE_COUNT,