- struct dfu_ctx *ctx = data;
- int fail = 1;
-
- switch ((enum dfu_ctrl_req_code)req->bRequest) {
- case USB_CTRL_REQ_DFU_DNLOAD: {
- void *buf;
-
- switch (ctx->state) {
- case DFU_STATE_dfuIDLE:
- ctx->off = 0;
- break;
- case DFU_STATE_dfuDNLOAD_IDLE:
- break;
- default:
- goto err;
- }
-
- /**
- * XXX we are not allowed to STALL here, and we need to eat all transferred data.
- * better not allow setup_write to break the protocol.
- */
- ctx->status = ctx->setup_write(ctx->off, req->wLength, &buf);
- if (ctx->status != DFU_STATUS_OK) {
- ctx->state = DFU_STATE_dfuERROR;
- goto err_have_status;
- }
-
- if (req->wLength > 0)
- usb_ep0_rx(buf, req->wLength, dfu_dnload_complete, ctx);
- else
- dfu_dnload_complete(NULL, 0, ctx);
- goto out_no_status;
- }
- case USB_CTRL_REQ_DFU_GETSTATUS: {
- struct dfu_status_t st;
-
- st.bState = ctx->state;
- st.bStatus = ctx->status;
- st.bwPollTimeout = 1000; /* XXX */
- /**
- * If we're in DFU_STATE_dfuMANIFEST, we just finished
- * the download, and we're just about to send our last
- * status report. Once the report has been sent, go
- * and reset the system to put the new firmware into
- * effect.
- */
- usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
- if (ctx->state == DFU_STATE_dfuMANIFEST) {
- usb_handle_control_status_cb(dfu_reset_system);
- goto out_no_status;
- }
- break;
- }
- case USB_CTRL_REQ_DFU_CLRSTATUS:
- ctx->state = DFU_STATE_dfuIDLE;
- ctx->status = DFU_STATUS_OK;
- break;
- case USB_CTRL_REQ_DFU_GETSTATE: {
- uint8_t st = ctx->state;
- usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
- break;
- }
- case USB_CTRL_REQ_DFU_ABORT:
- switch (ctx->state) {
- case DFU_STATE_dfuIDLE:
- case DFU_STATE_dfuDNLOAD_IDLE:
- /* case DFU_STATE_dfuUPLOAD_IDLE: */
- ctx->state = DFU_STATE_dfuIDLE;
- break;
- default:
- goto err;
- }
- break;
- /* case USB_CTRL_REQ_DFU_UPLOAD: */
- default:
- return (0);
- }
-
- fail = 0;
- goto out;
+ struct dfu_ctx *ctx = data;
+ int fail = 1;
+
+ switch ((enum dfu_ctrl_req_code)req->bRequest)
+ {
+ case USB_CTRL_REQ_DFU_DNLOAD: {
+ void *buf;
+
+ switch (ctx->state) {
+ case DFU_STATE_dfuIDLE:
+ ctx->off = 0;
+ break;
+ case DFU_STATE_dfuDNLOAD_IDLE:
+ break;
+ default:
+ goto err;
+ }
+
+ /**
+ * XXX we are not allowed to STALL here, and we need to eat all transferred data.
+ * better not allow setup_write to break the protocol.
+ */
+ ctx->status = ctx->setup_write(ctx->off, req->wLength, &buf);
+ if (ctx->status != DFU_STATUS_OK) {
+ ctx->state = DFU_STATE_dfuERROR;
+ goto err_have_status;
+ }
+
+ if (req->wLength > 0)
+ usb_ep0_rx(buf, req->wLength, dfu_dnload_complete, ctx);
+ else
+ dfu_dnload_complete(NULL, 0, ctx);
+ goto out_no_status;
+ }
+ case USB_CTRL_REQ_DFU_UPLOAD: {
+ /*
+ void *buf;
+ size_t len = 0;
+
+ switch ( ctx->state )
+ {
+ case DFU_STATE_dfuIDLE:
+ break;
+ case DFU_STATE_dfuUPLOAD_IDLE:
+ break;
+ default:
+ goto err;
+ }
+
+ // Find which sector to read
+ ctx->status = ctx->setup_read(ctx->off, &len, &buf);
+ print("UPLOAD off:");
+ printHex( ctx->off );
+ print(" len:");
+ printHex( len );
+ print(" addr:");
+ printHex( (uint32_t)buf );
+ print( NL );
+
+ if ( ctx->status != DFU_STATUS_OK || len > req->wLength )
+ {
+ ctx->state = DFU_STATE_dfuERROR;
+ goto err_have_status;
+ }
+
+ // Send bytes to Host
+ if ( len > 0 )
+ {
+ usb_ep0_rx( buf, len, NULL, NULL );
+ }
+ else
+ {
+ ctx->state = DFU_STATE_dfuIDLE;
+ }
+
+ goto out_no_status;
+ */
+ return 0;
+ }
+ case USB_CTRL_REQ_DFU_GETSTATUS: {
+ struct dfu_status_t st;
+
+ st.bState = ctx->state;
+ st.bStatus = ctx->status;
+ st.bwPollTimeout = 1000; /* XXX */
+
+ /**
+ * If we're in DFU_STATE_dfuMANIFEST, we just finished
+ * the download, and we're just about to send our last
+ * status report. Once the report has been sent, go
+ * and reset the system to put the new firmware into
+ * effect.
+ */
+ usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
+ if (ctx->state == DFU_STATE_dfuMANIFEST) {
+ usb_handle_control_status_cb(dfu_reset_system);
+ goto out_no_status;
+ }
+ break;
+ }
+ case USB_CTRL_REQ_DFU_CLRSTATUS:
+ ctx->state = DFU_STATE_dfuIDLE;
+ ctx->status = DFU_STATUS_OK;
+ break;
+ case USB_CTRL_REQ_DFU_GETSTATE: {
+ uint8_t st = ctx->state;
+ usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
+ break;
+ }
+ case USB_CTRL_REQ_DFU_ABORT:
+ switch (ctx->state) {
+ case DFU_STATE_dfuIDLE:
+ case DFU_STATE_dfuDNLOAD_IDLE:
+ case DFU_STATE_dfuUPLOAD_IDLE:
+ ctx->state = DFU_STATE_dfuIDLE;
+ break;
+ default:
+ goto err;
+ }
+ break;
+ default:
+ return (0);
+ }
+
+ fail = 0;
+ goto out;