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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Copyright (c) 2011,2012 Simon Schubert <[email protected]>.
  2. * Modifications by Jacob Alexander 2014 <[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. // ----- Functions -----
  21. void dfu_write_done( enum dfu_status err, struct dfu_ctx *ctx )
  22. {
  23. ctx->status = err;
  24. if (ctx->status == DFU_STATUS_OK) {
  25. switch (ctx->state) {
  26. case DFU_STATE_dfuDNBUSY:
  27. ctx->state = DFU_STATE_dfuDNLOAD_IDLE;
  28. break;
  29. default:
  30. break;
  31. }
  32. } else {
  33. ctx->state = DFU_STATE_dfuERROR;
  34. }
  35. }
  36. static void dfu_dnload_complete( void *buf, ssize_t len, void *cbdata )
  37. {
  38. struct dfu_ctx *ctx = cbdata;
  39. if (len > 0)
  40. ctx->state = DFU_STATE_dfuDNBUSY;
  41. else
  42. ctx->state = DFU_STATE_dfuMANIFEST;
  43. ctx->status = ctx->finish_write(buf, ctx->off, len);
  44. ctx->off += len;
  45. ctx->len = len;
  46. if (ctx->status != DFU_STATUS_async)
  47. dfu_write_done(ctx->status, ctx);
  48. usb_handle_control_status(ctx->state == DFU_STATE_dfuERROR);
  49. }
  50. static void dfu_reset_system( void *buf, ssize_t len, void *cbdata )
  51. {
  52. SOFTWARE_RESET();
  53. }
  54. static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
  55. {
  56. struct dfu_ctx *ctx = data;
  57. int fail = 1;
  58. switch ((enum dfu_ctrl_req_code)req->bRequest) {
  59. case USB_CTRL_REQ_DFU_DNLOAD: {
  60. void *buf;
  61. switch (ctx->state) {
  62. case DFU_STATE_dfuIDLE:
  63. ctx->off = 0;
  64. break;
  65. case DFU_STATE_dfuDNLOAD_IDLE:
  66. break;
  67. default:
  68. goto err;
  69. }
  70. /**
  71. * XXX we are not allowed to STALL here, and we need to eat all transferred data.
  72. * better not allow setup_write to break the protocol.
  73. */
  74. ctx->status = ctx->setup_write(ctx->off, req->wLength, &buf);
  75. if (ctx->status != DFU_STATUS_OK) {
  76. ctx->state = DFU_STATE_dfuERROR;
  77. goto err_have_status;
  78. }
  79. if (req->wLength > 0)
  80. usb_ep0_rx(buf, req->wLength, dfu_dnload_complete, ctx);
  81. else
  82. dfu_dnload_complete(NULL, 0, ctx);
  83. goto out_no_status;
  84. }
  85. case USB_CTRL_REQ_DFU_GETSTATUS: {
  86. struct dfu_status_t st;
  87. st.bState = ctx->state;
  88. st.bStatus = ctx->status;
  89. st.bwPollTimeout = 1000; /* XXX */
  90. /**
  91. * If we're in DFU_STATE_dfuMANIFEST, we just finished
  92. * the download, and we're just about to send our last
  93. * status report. Once the report has been sent, go
  94. * and reset the system to put the new firmware into
  95. * effect.
  96. */
  97. usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
  98. if (ctx->state == DFU_STATE_dfuMANIFEST) {
  99. usb_handle_control_status_cb(dfu_reset_system);
  100. goto out_no_status;
  101. }
  102. break;
  103. }
  104. case USB_CTRL_REQ_DFU_CLRSTATUS:
  105. ctx->state = DFU_STATE_dfuIDLE;
  106. ctx->status = DFU_STATUS_OK;
  107. break;
  108. case USB_CTRL_REQ_DFU_GETSTATE: {
  109. uint8_t st = ctx->state;
  110. usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
  111. break;
  112. }
  113. case USB_CTRL_REQ_DFU_ABORT:
  114. switch (ctx->state) {
  115. case DFU_STATE_dfuIDLE:
  116. case DFU_STATE_dfuDNLOAD_IDLE:
  117. /* case DFU_STATE_dfuUPLOAD_IDLE: */
  118. ctx->state = DFU_STATE_dfuIDLE;
  119. break;
  120. default:
  121. goto err;
  122. }
  123. break;
  124. /* case USB_CTRL_REQ_DFU_UPLOAD: */
  125. default:
  126. return (0);
  127. }
  128. fail = 0;
  129. goto out;
  130. err:
  131. ctx->status = DFU_STATUS_errSTALLEDPKT;
  132. err_have_status:
  133. ctx->state = DFU_STATE_dfuERROR;
  134. out:
  135. usb_handle_control_status(fail);
  136. out_no_status:
  137. return (1);
  138. }
  139. void dfu_init( dfu_setup_write_t setup_write, dfu_finish_write_t finish_write, struct dfu_ctx *ctx )
  140. {
  141. ctx->state = DFU_STATE_dfuIDLE;
  142. ctx->setup_write = setup_write;
  143. ctx->finish_write = finish_write;
  144. usb_attach_function(&dfu_function, &ctx->header);
  145. }
  146. const struct usbd_function dfu_function = {
  147. .control = dfu_handle_control,
  148. .interface_count = USB_FUNCTION_DFU_IFACE_COUNT,
  149. };