|
|
@@ -69,12 +69,13 @@ static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data ) |
|
|
|
struct dfu_ctx *ctx = data; |
|
|
|
int fail = 1; |
|
|
|
|
|
|
|
switch ((enum dfu_ctrl_req_code)req->bRequest) |
|
|
|
switch ( (enum dfu_ctrl_req_code)req->bRequest ) |
|
|
|
{ |
|
|
|
case USB_CTRL_REQ_DFU_DNLOAD: { |
|
|
|
void *buf; |
|
|
|
|
|
|
|
switch (ctx->state) { |
|
|
|
switch ( ctx->state ) |
|
|
|
{ |
|
|
|
case DFU_STATE_dfuIDLE: |
|
|
|
ctx->off = 0; |
|
|
|
break; |
|
|
@@ -88,20 +89,25 @@ static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data ) |
|
|
|
* 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->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); |
|
|
|
if ( req->wLength > 0 ) |
|
|
|
{ |
|
|
|
usb_ep0_rx( buf, req->wLength, dfu_dnload_complete, ctx ); |
|
|
|
} |
|
|
|
else |
|
|
|
dfu_dnload_complete(NULL, 0, ctx); |
|
|
|
{ |
|
|
|
dfu_dnload_complete( NULL, 0, ctx ); |
|
|
|
} |
|
|
|
goto out_no_status; |
|
|
|
} |
|
|
|
case USB_CTRL_REQ_DFU_UPLOAD: { |
|
|
|
/* |
|
|
|
#if defined(_mk20dx256vlh7_) // Kiibohd-dfu |
|
|
|
void *buf; |
|
|
|
size_t len = 0; |
|
|
|
|
|
|
@@ -115,15 +121,23 @@ static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data ) |
|
|
|
goto err; |
|
|
|
} |
|
|
|
|
|
|
|
// Compute the requested offset |
|
|
|
ctx->off = USB_DFU_TRANSFER_SIZE * req->wValue; |
|
|
|
|
|
|
|
// Find which sector to read |
|
|
|
ctx->status = ctx->setup_read(ctx->off, &len, &buf); |
|
|
|
print("UPLOAD off:"); |
|
|
|
ctx->status = ctx->setup_read( ctx->off, &len, &buf ); |
|
|
|
#ifdef FLASH_DEBUG |
|
|
|
print("UPLOAD req:"); |
|
|
|
printHex( req->wValue ); |
|
|
|
print(" off:"); |
|
|
|
printHex( ctx->off ); |
|
|
|
print(" len:"); |
|
|
|
printHex( len ); |
|
|
|
print(" reqlen: "); |
|
|
|
printHex( req->wLength ); |
|
|
|
print(" addr:"); |
|
|
|
printHex( (uint32_t)buf ); |
|
|
|
print( NL ); |
|
|
|
#endif |
|
|
|
|
|
|
|
if ( ctx->status != DFU_STATUS_OK || len > req->wLength ) |
|
|
|
{ |
|
|
@@ -132,18 +146,30 @@ static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data ) |
|
|
|
} |
|
|
|
|
|
|
|
// Send bytes to Host |
|
|
|
if ( len > 0 ) |
|
|
|
// Successfully transferred data to USB |
|
|
|
if ( usb_ep0_tx( buf, len, len, NULL, NULL ) != -1 ) |
|
|
|
{ |
|
|
|
usb_ep0_rx( buf, len, NULL, NULL ); |
|
|
|
ctx->state = len < req->wLength |
|
|
|
? DFU_STATE_dfuIDLE |
|
|
|
: DFU_STATE_dfuUPLOAD_IDLE; |
|
|
|
fail = 0; |
|
|
|
} |
|
|
|
// Problem transferring via USB |
|
|
|
else |
|
|
|
{ |
|
|
|
ctx->state = DFU_STATE_dfuIDLE; |
|
|
|
ctx->state = DFU_STATE_dfuERROR; |
|
|
|
} |
|
|
|
#ifdef FLASH_DEBUG |
|
|
|
print(" state:"); |
|
|
|
printHex( ctx->state ); |
|
|
|
print( NL ); |
|
|
|
#endif |
|
|
|
|
|
|
|
goto out_no_status; |
|
|
|
*/ |
|
|
|
return 0; |
|
|
|
goto out; |
|
|
|
#else |
|
|
|
ctx->state = DFU_STATE_dfuERROR; |
|
|
|
goto out; |
|
|
|
#endif |
|
|
|
} |
|
|
|
case USB_CTRL_REQ_DFU_GETSTATUS: { |
|
|
|
struct dfu_status_t st; |
|
|
@@ -206,7 +232,9 @@ out_no_status: |
|
|
|
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 ) |
|
|
|
{ |
|
|
|
ctx->state = DFU_STATE_dfuIDLE; |
|
|
|
#if defined(_mk20dx256vlh7_) // Kiibohd-dfu |
|
|
|
ctx->setup_read = setup_read; |
|
|
|
#endif |
|
|
|
ctx->setup_write = setup_write; |
|
|
|
ctx->finish_write = finish_write; |
|
|
|
usb_attach_function(&dfu_function, &ctx->header); |