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.h 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifndef _USB_DFU_H
  18. #define _USB_DFU_H
  19. // ----- Compiler Includes -----
  20. #include <sys/types.h>
  21. // ----- Defines -----
  22. #define USB_FUNCTION_DFU_IFACE_COUNT 1
  23. #ifndef USB_DFU_TRANSFER_SIZE
  24. #define USB_DFU_TRANSFER_SIZE FLASH_SECTOR_SIZE
  25. #endif
  26. #define USB_FUNCTION_DESC_DFU_DECL \
  27. struct dfu_function_desc
  28. #define USB_FUNCTION_DFU_IFACE_COUNT 1
  29. #define USB_FUNCTION_DFU_RX_EP_COUNT 0
  30. #define USB_FUNCTION_DFU_TX_EP_COUNT 0
  31. // ----- Enumerations -----
  32. enum dfu_dev_subclass {
  33. USB_DEV_SUBCLASS_APP_DFU = 0x01
  34. };
  35. enum dfu_dev_proto {
  36. USB_DEV_PROTO_DFU_APP = 0x01,
  37. USB_DEV_PROTO_DFU_DFU = 0x02
  38. };
  39. enum dfu_ctrl_req_code {
  40. USB_CTRL_REQ_DFU_DETACH = 0,
  41. USB_CTRL_REQ_DFU_DNLOAD = 1,
  42. USB_CTRL_REQ_DFU_UPLOAD = 2,
  43. USB_CTRL_REQ_DFU_GETSTATUS = 3,
  44. USB_CTRL_REQ_DFU_CLRSTATUS = 4,
  45. USB_CTRL_REQ_DFU_GETSTATE = 5,
  46. USB_CTRL_REQ_DFU_ABORT = 6
  47. };
  48. enum dfu_status {
  49. DFU_STATUS_async = 0xff,
  50. DFU_STATUS_OK = 0x00,
  51. DFU_STATUS_errTARGET = 0x01,
  52. DFU_STATUS_errFILE = 0x02,
  53. DFU_STATUS_errWRITE = 0x03,
  54. DFU_STATUS_errERASE = 0x04,
  55. DFU_STATUS_errCHECK_ERASED = 0x05,
  56. DFU_STATUS_errPROG = 0x06,
  57. DFU_STATUS_errVERIFY = 0x07,
  58. DFU_STATUS_errADDRESS = 0x08,
  59. DFU_STATUS_errNOTDONE = 0x09,
  60. DFU_STATUS_errFIRMWARE = 0x0a,
  61. DFU_STATUS_errVENDOR = 0x0b,
  62. DFU_STATUS_errUSBR = 0x0c,
  63. DFU_STATUS_errPOR = 0x0d,
  64. DFU_STATUS_errUNKNOWN = 0x0e,
  65. DFU_STATUS_errSTALLEDPKT = 0x0f
  66. };
  67. enum dfu_state {
  68. DFU_STATE_appIDLE = 0,
  69. DFU_STATE_appDETACH = 1,
  70. DFU_STATE_dfuIDLE = 2,
  71. DFU_STATE_dfuDNLOAD_SYNC = 3,
  72. DFU_STATE_dfuDNBUSY = 4,
  73. DFU_STATE_dfuDNLOAD_IDLE = 5,
  74. DFU_STATE_dfuMANIFEST_SYNC = 6,
  75. DFU_STATE_dfuMANIFEST = 7,
  76. DFU_STATE_dfuMANIFEST_WAIT_RESET = 8,
  77. DFU_STATE_dfuUPLOAD_IDLE = 9,
  78. DFU_STATE_dfuERROR = 10
  79. };
  80. // ----- Structs -----
  81. struct dfu_status_t {
  82. enum dfu_status bStatus : 8;
  83. uint32_t bwPollTimeout : 24;
  84. enum dfu_state bState : 8;
  85. uint8_t iString;
  86. } __packed;
  87. CTASSERT_SIZE_BYTE(struct dfu_status_t, 6);
  88. typedef enum dfu_status (*dfu_setup_read_t)(size_t off, size_t *len, void **buf);
  89. typedef enum dfu_status (*dfu_setup_write_t)(size_t off, size_t len, void **buf);
  90. typedef enum dfu_status (*dfu_finish_write_t)(void *, size_t off, size_t len);
  91. typedef void (*dfu_detach_t)(void);
  92. struct dfu_ctx {
  93. struct usbd_function_ctx_header header;
  94. enum dfu_state state;
  95. enum dfu_status status;
  96. dfu_setup_read_t setup_read;
  97. dfu_setup_write_t setup_write;
  98. dfu_finish_write_t finish_write;
  99. size_t off;
  100. size_t len;
  101. };
  102. struct dfu_desc_functional {
  103. uint8_t bLength;
  104. struct usb_desc_type_t bDescriptorType; /* = class DFU/0x1 FUNCTIONAL */
  105. union {
  106. struct {
  107. uint8_t can_download : 1;
  108. uint8_t can_upload : 1;
  109. uint8_t manifestation_tolerant : 1;
  110. uint8_t will_detach : 1;
  111. uint8_t _rsvd0 : 4;
  112. };
  113. uint8_t bmAttributes;
  114. };
  115. uint16_t wDetachTimeOut;
  116. uint16_t wTransferSize;
  117. struct usb_bcd_t bcdDFUVersion;
  118. } __packed;
  119. CTASSERT_SIZE_BYTE(struct dfu_desc_functional, 9);
  120. struct dfu_function_desc {
  121. struct usb_desc_iface_t iface;
  122. struct dfu_desc_functional dfu;
  123. };
  124. extern const struct usbd_function dfu_function;
  125. extern const struct usbd_function dfu_app_function;
  126. // ----- Functions -----
  127. void dfu_write_done( enum dfu_status, struct dfu_ctx *ctx );
  128. 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 );
  129. void dfu_app_init( dfu_detach_t detachcb );
  130. #endif