Kiibohd Controller
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. {
  65. case DFU_STATE_dfuIDLE:
  66. ctx->off = 0;
  67. break;
  68. case DFU_STATE_dfuDNLOAD_IDLE:
  69. break;
  70. default:
  71. goto err;
  72. }
  73. /**
  74. * XXX we are not allowed to STALL here, and we need to eat all transferred data.
  75. * better not allow setup_write to break the protocol.
  76. */
  77. ctx->status = ctx->setup_write( ctx->off, req->wLength, &buf );
  78. if ( ctx->status != DFU_STATUS_OK )
  79. {
  80. ctx->state = DFU_STATE_dfuERROR;
  81. goto err_have_status;
  82. }
  83. if ( req->wLength > 0 )
  84. {
  85. usb_ep0_rx( buf, req->wLength, dfu_dnload_complete, ctx );
  86. }
  87. else
  88. {
  89. dfu_dnload_complete( NULL, 0, ctx );
  90. }
  91. goto out_no_status;
  92. }
  93. case USB_CTRL_REQ_DFU_UPLOAD: {
  94. #if defined(_mk20dx256vlh7_) // Kiibohd-dfu
  95. void *buf;
  96. size_t len = 0;
  97. switch ( ctx->state )
  98. {
  99. case DFU_STATE_dfuIDLE:
  100. break;
  101. case DFU_STATE_dfuUPLOAD_IDLE:
  102. break;
  103. default:
  104. goto err;
  105. }
  106. // Compute the requested offset
  107. ctx->off = USB_DFU_TRANSFER_SIZE * req->wValue;
  108. // Find which sector to read
  109. ctx->status = ctx->setup_read( ctx->off, &len, &buf );
  110. #ifdef FLASH_DEBUG
  111. print("UPLOAD req:");
  112. printHex( req->wValue );
  113. print(" off:");
  114. printHex( ctx->off );
  115. print(" len:");
  116. printHex( len );
  117. print(" reqlen: ");
  118. printHex( req->wLength );
  119. print(" addr:");
  120. printHex( (uint32_t)buf );
  121. #endif
  122. if ( ctx->status != DFU_STATUS_OK || len > req->wLength )
  123. {
  124. ctx->state = DFU_STATE_dfuERROR;
  125. goto err_have_status;
  126. }
  127. // Send bytes to Host
  128. // Successfully transferred data to USB
  129. if ( usb_ep0_tx( buf, len, len, NULL, NULL ) != -1 )
  130. {
  131. ctx->state = len < req->wLength
  132. ? DFU_STATE_dfuIDLE
  133. : DFU_STATE_dfuUPLOAD_IDLE;
  134. fail = 0;
  135. }
  136. // Problem transferring via USB
  137. else
  138. {
  139. ctx->state = DFU_STATE_dfuERROR;
  140. }
  141. #ifdef FLASH_DEBUG
  142. print(" state:");
  143. printHex( ctx->state );
  144. print( NL );
  145. #endif
  146. goto out;
  147. #else
  148. ctx->state = DFU_STATE_dfuERROR;
  149. goto out;
  150. #endif
  151. }
  152. case USB_CTRL_REQ_DFU_GETSTATUS: {
  153. struct dfu_status_t st;
  154. st.bState = ctx->state;
  155. st.bStatus = ctx->status;
  156. st.bwPollTimeout = 1000; /* XXX */
  157. /**
  158. * If we're in DFU_STATE_dfuMANIFEST, we just finished
  159. * the download, and we're just about to send our last
  160. * status report. Once the report has been sent, go
  161. * and reset the system to put the new firmware into
  162. * effect.
  163. */
  164. usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
  165. if (ctx->state == DFU_STATE_dfuMANIFEST) {
  166. usb_handle_control_status_cb(dfu_reset_system);
  167. goto out_no_status;
  168. }
  169. break;
  170. }
  171. case USB_CTRL_REQ_DFU_CLRSTATUS:
  172. ctx->state = DFU_STATE_dfuIDLE;
  173. ctx->status = DFU_STATUS_OK;
  174. break;
  175. case USB_CTRL_REQ_DFU_GETSTATE: {
  176. uint8_t st = ctx->state;
  177. usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
  178. break;
  179. }
  180. case USB_CTRL_REQ_DFU_ABORT:
  181. switch (ctx->state) {
  182. case DFU_STATE_dfuIDLE:
  183. case DFU_STATE_dfuDNLOAD_IDLE:
  184. case DFU_STATE_dfuUPLOAD_IDLE:
  185. ctx->state = DFU_STATE_dfuIDLE;
  186. break;
  187. default:
  188. goto err;
  189. }
  190. break;
  191. default:
  192. return (0);
  193. }
  194. fail = 0;
  195. goto out;
  196. err:
  197. ctx->status = DFU_STATUS_errSTALLEDPKT;
  198. err_have_status:
  199. ctx->state = DFU_STATE_dfuERROR;
  200. out:
  201. usb_handle_control_status(fail);
  202. out_no_status:
  203. return (1);
  204. }
  205. 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 )
  206. {
  207. ctx->state = DFU_STATE_dfuIDLE;
  208. #if defined(_mk20dx256vlh7_) // Kiibohd-dfu
  209. ctx->setup_read = setup_read;
  210. #endif
  211. ctx->setup_write = setup_write;
  212. ctx->finish_write = finish_write;
  213. usb_attach_function(&dfu_function, &ctx->header);
  214. }
  215. const struct usbd_function dfu_function = {
  216. .control = dfu_handle_control,
  217. .interface_count = USB_FUNCTION_DFU_IFACE_COUNT,
  218. };