Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

dfu.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright (c) 2011,2012 Simon Schubert <[email protected]>.
  2. * Modifications by Jacob Alexander 2014-2015 <[email protected]>
  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. // ----- Local Includes -----
  18. #include "usb.h"
  19. #include "dfu.h"
  20. #include "debug.h"
  21. // ----- Functions -----
  22. void dfu_write_done( enum dfu_status err, struct dfu_ctx *ctx )
  23. {
  24. ctx->status = err;
  25. if (ctx->status == DFU_STATUS_OK) {
  26. switch (ctx->state) {
  27. case DFU_STATE_dfuDNBUSY:
  28. ctx->state = DFU_STATE_dfuDNLOAD_IDLE;
  29. break;
  30. default:
  31. break;
  32. }
  33. } else {
  34. ctx->state = DFU_STATE_dfuERROR;
  35. }
  36. }
  37. static void dfu_dnload_complete( void *buf, ssize_t len, void *cbdata )
  38. {
  39. struct dfu_ctx *ctx = cbdata;
  40. if (len > 0)
  41. ctx->state = DFU_STATE_dfuDNBUSY;
  42. else
  43. ctx->state = DFU_STATE_dfuMANIFEST;
  44. ctx->status = ctx->finish_write(buf, ctx->off, len);
  45. ctx->off += len;
  46. ctx->len = len;
  47. if (ctx->status != DFU_STATUS_async)
  48. dfu_write_done(ctx->status, ctx);
  49. usb_handle_control_status(ctx->state == DFU_STATE_dfuERROR);
  50. }
  51. static void dfu_reset_system( void *buf, ssize_t len, void *cbdata )
  52. {
  53. SOFTWARE_RESET();
  54. }
  55. static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
  56. {
  57. struct dfu_ctx *ctx = data;
  58. int fail = 1;
  59. switch ((enum dfu_ctrl_req_code)req->bRequest)
  60. {
  61. case USB_CTRL_REQ_DFU_DNLOAD: {
  62. void *buf;
  63. switch (ctx->state) {
  64. case DFU_STATE_dfuIDLE:
  65. ctx->off = 0;
  66. break;
  67. case DFU_STATE_dfuDNLOAD_IDLE:
  68. break;
  69. default:
  70. goto err;
  71. }
  72. /**
  73. * XXX we are not allowed to STALL here, and we need to eat all transferred data.
  74. * better not allow setup_write to break the protocol.
  75. */
  76. ctx->status = ctx->setup_write(ctx->off, req->wLength, &buf);
  77. if (ctx->status != DFU_STATUS_OK) {
  78. ctx->state = DFU_STATE_dfuERROR;
  79. goto err_have_status;
  80. }
  81. if (req->wLength > 0)
  82. usb_ep0_rx(buf, req->wLength, dfu_dnload_complete, ctx);
  83. else
  84. dfu_dnload_complete(NULL, 0, ctx);
  85. goto out_no_status;
  86. }
  87. case USB_CTRL_REQ_DFU_UPLOAD: {
  88. /*
  89. void *buf;
  90. size_t len = 0;
  91. switch ( ctx->state )
  92. {
  93. case DFU_STATE_dfuIDLE:
  94. break;
  95. case DFU_STATE_dfuUPLOAD_IDLE:
  96. break;
  97. default:
  98. goto err;
  99. }
  100. // Find which sector to read
  101. ctx->status = ctx->setup_read(ctx->off, &len, &buf);
  102. print("UPLOAD off:");
  103. printHex( ctx->off );
  104. print(" len:");
  105. printHex( len );
  106. print(" addr:");
  107. printHex( (uint32_t)buf );
  108. print( NL );
  109. if ( ctx->status != DFU_STATUS_OK || len > req->wLength )
  110. {
  111. ctx->state = DFU_STATE_dfuERROR;
  112. goto err_have_status;
  113. }
  114. // Send bytes to Host
  115. if ( len > 0 )
  116. {
  117. usb_ep0_rx( buf, len, NULL, NULL );
  118. }
  119. else
  120. {
  121. ctx->state = DFU_STATE_dfuIDLE;
  122. }
  123. goto out_no_status;
  124. */
  125. return 0;
  126. }
  127. case USB_CTRL_REQ_DFU_GETSTATUS: {
  128. struct dfu_status_t st;
  129. st.bState = ctx->state;
  130. st.bStatus = ctx->status;
  131. st.bwPollTimeout = 1000; /* XXX */
  132. /**
  133. * If we're in DFU_STATE_dfuMANIFEST, we just finished
  134. * the download, and we're just about to send our last
  135. * status report. Once the report has been sent, go
  136. * and reset the system to put the new firmware into
  137. * effect.
  138. */
  139. usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
  140. if (ctx->state == DFU_STATE_dfuMANIFEST) {
  141. usb_handle_control_status_cb(dfu_reset_system);
  142. goto out_no_status;
  143. }
  144. break;
  145. }
  146. case USB_CTRL_REQ_DFU_CLRSTATUS:
  147. ctx->state = DFU_STATE_dfuIDLE;
  148. ctx->status = DFU_STATUS_OK;
  149. break;
  150. case USB_CTRL_REQ_DFU_GETSTATE: {
  151. uint8_t st = ctx->state;
  152. usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
  153. break;
  154. }
  155. case USB_CTRL_REQ_DFU_ABORT:
  156. switch (ctx->state) {
  157. case DFU_STATE_dfuIDLE:
  158. case DFU_STATE_dfuDNLOAD_IDLE:
  159. case DFU_STATE_dfuUPLOAD_IDLE:
  160. ctx->state = DFU_STATE_dfuIDLE;
  161. break;
  162. default:
  163. goto err;
  164. }
  165. break;
  166. default:
  167. return (0);
  168. }
  169. fail = 0;
  170. goto out;
  171. err:
  172. ctx->status = DFU_STATUS_errSTALLEDPKT;
  173. err_have_status:
  174. ctx->state = DFU_STATE_dfuERROR;
  175. out:
  176. usb_handle_control_status(fail);
  177. out_no_status:
  178. return (1);
  179. }
  180. 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 )
  181. {
  182. ctx->state = DFU_STATE_dfuIDLE;
  183. ctx->setup_read = setup_read;
  184. ctx->setup_write = setup_write;
  185. ctx->finish_write = finish_write;
  186. usb_attach_function(&dfu_function, &ctx->header);
  187. }
  188. const struct usbd_function dfu_function = {
  189. .control = dfu_handle_control,
  190. .interface_count = USB_FUNCTION_DFU_IFACE_COUNT,
  191. };