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 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. // ----- 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_UPLOAD: {
  86. return (0); // TODO
  87. /*
  88. void *buf;
  89. switch (ctx->state) {
  90. case DFU_STATE_dfuIDLE:
  91. ctx->off = 0;
  92. break;
  93. case DFU_STATE_dfuUPLOAD_IDLE:
  94. break;
  95. default:
  96. goto err;
  97. }
  98. // XXX Don't STALL? -HaaTa
  99. // TODO
  100. ctx->status = ctx->setup_write(ctx->off, req->wLength, &buf);
  101. if (ctx->status != DFU_STATUS_OK) {
  102. ctx->state = DFU_STATE_dfuERROR;
  103. goto err_have_status;
  104. }
  105. if (req->wLength > 0)
  106. usb_ep0_rx(buf, req->wLength, dfu_dnload_complete, ctx);
  107. else
  108. dfu_dnload_complete(NULL, 0, ctx);
  109. goto out_no_status;
  110. */
  111. }
  112. case USB_CTRL_REQ_DFU_GETSTATUS: {
  113. struct dfu_status_t st;
  114. st.bState = ctx->state;
  115. st.bStatus = ctx->status;
  116. st.bwPollTimeout = 1000; /* XXX */
  117. // XXX FAKE WRITE
  118. if ( ctx->state == DFU_STATE_dfuMANIFEST )
  119. {
  120. uint8_t data[] = { 0x10, 0x20, 0x30, 0x40 };
  121. flash_program_longword((uintptr_t)&_app_rom, data);
  122. }
  123. /*
  124. uint32_t *position = &_app_rom + 0x100;
  125. for ( ; position < &_app_rom + 0x200; position++ )
  126. //for ( ; position < &_app_rom + 0x800; position++ )
  127. {
  128. if ( *position != 0xFFFFFFFF )
  129. {
  130. while( 1 )
  131. {
  132. GPIOA_PTOR |= (1<<5);
  133. for (uint32_t d = 0; d < 7200000; d++ );
  134. }
  135. }
  136. }*/
  137. // Check to see if vector table was flashed correctly
  138. // Return a flash error if it was not
  139. if (_app_rom == 0xffffffff && ctx->state == DFU_STATE_dfuMANIFEST)
  140. st.bStatus = DFU_STATUS_errPROG;
  141. //}
  142. /*
  143. if (ctx->state == DFU_STATE_dfuMANIFEST)
  144. {
  145. uint8_t *addr = (uint8_t*)_app_rom;
  146. //while (*(addr++) != 0x80);
  147. //st.bStatus = DFU_STATUS_errPROG;
  148. st.bStatus = (uint8_t)((uint32_t)(&_app_rom) >> 16);
  149. }
  150. */
  151. /**
  152. * If we're in DFU_STATE_dfuMANIFEST, we just finished
  153. * the download, and we're just about to send our last
  154. * status report. Once the report has been sent, go
  155. * and reset the system to put the new firmware into
  156. * effect.
  157. */
  158. usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
  159. if (ctx->state == DFU_STATE_dfuMANIFEST) {
  160. usb_handle_control_status_cb(dfu_reset_system);
  161. goto out_no_status;
  162. }
  163. break;
  164. }
  165. case USB_CTRL_REQ_DFU_CLRSTATUS:
  166. ctx->state = DFU_STATE_dfuIDLE;
  167. ctx->status = DFU_STATUS_OK;
  168. break;
  169. case USB_CTRL_REQ_DFU_GETSTATE: {
  170. uint8_t st = ctx->state;
  171. usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
  172. break;
  173. }
  174. case USB_CTRL_REQ_DFU_ABORT:
  175. switch (ctx->state) {
  176. case DFU_STATE_dfuIDLE:
  177. case DFU_STATE_dfuDNLOAD_IDLE:
  178. case DFU_STATE_dfuUPLOAD_IDLE:
  179. ctx->state = DFU_STATE_dfuIDLE;
  180. break;
  181. default:
  182. goto err;
  183. }
  184. break;
  185. default:
  186. return (0);
  187. }
  188. fail = 0;
  189. goto out;
  190. err:
  191. ctx->status = DFU_STATUS_errSTALLEDPKT;
  192. err_have_status:
  193. ctx->state = DFU_STATE_dfuERROR;
  194. out:
  195. usb_handle_control_status(fail);
  196. out_no_status:
  197. return (1);
  198. }
  199. void dfu_init( dfu_setup_write_t setup_write, dfu_finish_write_t finish_write, struct dfu_ctx *ctx )
  200. {
  201. ctx->state = DFU_STATE_dfuIDLE;
  202. ctx->setup_write = setup_write;
  203. ctx->finish_write = finish_write;
  204. usb_attach_function(&dfu_function, &ctx->header);
  205. }
  206. const struct usbd_function dfu_function = {
  207. .control = dfu_handle_control,
  208. .interface_count = USB_FUNCTION_DFU_IFACE_COUNT,
  209. };