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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #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. // ----- Macros -----
  32. #define USB_FUNCTION_DESC_DFU(state...) \
  33. { \
  34. .iface = { \
  35. .bLength = sizeof(struct usb_desc_iface_t), \
  36. .bDescriptorType = USB_DESC_IFACE, \
  37. .bInterfaceNumber = USB_FUNCTION_IFACE(0, state), \
  38. .bAlternateSetting = 0, \
  39. .bNumEndpoints = 0, \
  40. .bInterfaceClass = USB_DEV_CLASS_APP, \
  41. .bInterfaceSubClass = USB_DEV_SUBCLASS_APP_DFU, \
  42. .bInterfaceProtocol = USB_DEV_PROTO_DFU_DFU, \
  43. .iInterface = 0, \
  44. }, \
  45. .dfu = { \
  46. .bLength = sizeof(struct dfu_desc_functional), \
  47. .bDescriptorType = { \
  48. .id = 0x1, \
  49. .type_type = USB_DESC_TYPE_CLASS \
  50. }, \
  51. .will_detach = 1, \
  52. .manifestation_tolerant = 0, \
  53. .can_upload = 0, \
  54. .can_download = 1, \
  55. .wDetachTimeOut = 0, \
  56. .wTransferSize = USB_DFU_TRANSFER_SIZE, \
  57. .bcdDFUVersion = { .maj = 1, .min = 1 } \
  58. } \
  59. }
  60. // ----- Enumerations -----
  61. enum dfu_dev_subclass {
  62. USB_DEV_SUBCLASS_APP_DFU = 0x01
  63. };
  64. enum dfu_dev_proto {
  65. USB_DEV_PROTO_DFU_APP = 0x01,
  66. USB_DEV_PROTO_DFU_DFU = 0x02
  67. };
  68. enum dfu_ctrl_req_code {
  69. USB_CTRL_REQ_DFU_DETACH = 0,
  70. USB_CTRL_REQ_DFU_DNLOAD = 1,
  71. USB_CTRL_REQ_DFU_UPLOAD = 2,
  72. USB_CTRL_REQ_DFU_GETSTATUS = 3,
  73. USB_CTRL_REQ_DFU_CLRSTATUS = 4,
  74. USB_CTRL_REQ_DFU_GETSTATE = 5,
  75. USB_CTRL_REQ_DFU_ABORT = 6
  76. };
  77. enum dfu_status {
  78. DFU_STATUS_async = 0xff,
  79. DFU_STATUS_OK = 0x00,
  80. DFU_STATUS_errTARGET = 0x01,
  81. DFU_STATUS_errFILE = 0x02,
  82. DFU_STATUS_errWRITE = 0x03,
  83. DFU_STATUS_errERASE = 0x04,
  84. DFU_STATUS_errCHECK_ERASED = 0x05,
  85. DFU_STATUS_errPROG = 0x06,
  86. DFU_STATUS_errVERIFY = 0x07,
  87. DFU_STATUS_errADDRESS = 0x08,
  88. DFU_STATUS_errNOTDONE = 0x09,
  89. DFU_STATUS_errFIRMWARE = 0x0a,
  90. DFU_STATUS_errVENDOR = 0x0b,
  91. DFU_STATUS_errUSBR = 0x0c,
  92. DFU_STATUS_errPOR = 0x0d,
  93. DFU_STATUS_errUNKNOWN = 0x0e,
  94. DFU_STATUS_errSTALLEDPKT = 0x0f
  95. };
  96. enum dfu_state {
  97. DFU_STATE_appIDLE = 0,
  98. DFU_STATE_appDETACH = 1,
  99. DFU_STATE_dfuIDLE = 2,
  100. DFU_STATE_dfuDNLOAD_SYNC = 3,
  101. DFU_STATE_dfuDNBUSY = 4,
  102. DFU_STATE_dfuDNLOAD_IDLE = 5,
  103. DFU_STATE_dfuMANIFEST_SYNC = 6,
  104. DFU_STATE_dfuMANIFEST = 7,
  105. DFU_STATE_dfuMANIFEST_WAIT_RESET = 8,
  106. DFU_STATE_dfuUPLOAD_IDLE = 9,
  107. DFU_STATE_dfuERROR = 10
  108. };
  109. // ----- Structs -----
  110. struct dfu_status_t {
  111. enum dfu_status bStatus : 8;
  112. uint32_t bwPollTimeout : 24;
  113. enum dfu_state bState : 8;
  114. uint8_t iString;
  115. } __packed;
  116. CTASSERT_SIZE_BYTE(struct dfu_status_t, 6);
  117. typedef enum dfu_status (*dfu_setup_write_t)(size_t off, size_t len, void **buf);
  118. typedef enum dfu_status (*dfu_finish_write_t)(void *, size_t off, size_t len);
  119. typedef void (*dfu_detach_t)(void);
  120. struct dfu_ctx {
  121. struct usbd_function_ctx_header header;
  122. enum dfu_state state;
  123. enum dfu_status status;
  124. dfu_setup_write_t setup_write;
  125. dfu_finish_write_t finish_write;
  126. size_t off;
  127. size_t len;
  128. };
  129. struct dfu_desc_functional {
  130. uint8_t bLength;
  131. struct usb_desc_type_t bDescriptorType; /* = class DFU/0x1 FUNCTIONAL */
  132. union {
  133. struct {
  134. uint8_t can_download : 1;
  135. uint8_t can_upload : 1;
  136. uint8_t manifestation_tolerant : 1;
  137. uint8_t will_detach : 1;
  138. uint8_t _rsvd0 : 4;
  139. };
  140. uint8_t bmAttributes;
  141. };
  142. uint16_t wDetachTimeOut;
  143. uint16_t wTransferSize;
  144. struct usb_bcd_t bcdDFUVersion;
  145. } __packed;
  146. CTASSERT_SIZE_BYTE(struct dfu_desc_functional, 9);
  147. struct dfu_function_desc {
  148. struct usb_desc_iface_t iface;
  149. struct dfu_desc_functional dfu;
  150. };
  151. extern const struct usbd_function dfu_function;
  152. extern const struct usbd_function dfu_app_function;
  153. // ----- Functions -----
  154. void dfu_write_done(enum dfu_status, struct dfu_ctx *ctx);
  155. void dfu_init(dfu_setup_write_t setup_write, dfu_finish_write_t finish_write, struct dfu_ctx *ctx);
  156. void dfu_app_init(dfu_detach_t detachcb);
  157. #endif