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.

usb.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* Copyright (c) 2011,2012 Simon Schubert <[email protected]>.
  2. * Modifications by Jacob Alexander 2014-2016 <[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. #pragma once
  18. // ----- Compiler Includes -----
  19. #include <sys/types.h>
  20. // ----- Local Includes -----
  21. #include "mchck.h"
  22. #include "usb-common.h"
  23. // ----- Defines -----
  24. #define USB_CTRL_REQ_DIR_SHIFT 0
  25. #define USB_CTRL_REQ_TYPE_SHIFT 1
  26. #define USB_CTRL_REQ_RECP_SHIFT 3
  27. #define USB_CTRL_REQ_CODE_SHIFT 8
  28. #define USB_CTRL_REQ(req_inout, req_type, req_code) \
  29. (uint16_t) \
  30. ((USB_CTRL_REQ_##req_inout << USB_CTRL_REQ_DIR_SHIFT) \
  31. |(USB_CTRL_REQ_##req_type << USB_CTRL_REQ_TYPE_SHIFT) \
  32. |(USB_CTRL_REQ_##req_code << USB_CTRL_REQ_CODE_SHIFT))
  33. // ----- Macros -----
  34. #define USB_DESC_STRING(s) \
  35. (void *)&(struct { \
  36. struct usb_desc_string_t dsc; \
  37. char16_t str[sizeof(s) / 2 - 1]; \
  38. }) {{ \
  39. .bLength = sizeof(struct usb_desc_string_t) + \
  40. sizeof(s) - 2, \
  41. .bDescriptorType = USB_DESC_STRING, \
  42. }, \
  43. s \
  44. }
  45. #define USB_DESC_STRING_LANG_ENUS USB_DESC_STRING(u"\x0409")
  46. #define USB_DESC_STRING_SERIALNO ((const void *)1)
  47. // ----- Structs & Enumerations -----
  48. /**
  49. * Note: bitfields ahead.
  50. * GCC fills the fields lsb-to-msb on little endian.
  51. */
  52. /**
  53. * USB descriptors
  54. */
  55. enum usb_desc_type {
  56. USB_DESC_DEV = 1,
  57. USB_DESC_CONFIG = 2,
  58. USB_DESC_STRING = 3,
  59. USB_DESC_IFACE = 4,
  60. USB_DESC_EP = 5,
  61. USB_DESC_DEVQUAL = 6,
  62. USB_DESC_OTHERSPEED = 7,
  63. USB_DESC_POWER = 8
  64. };
  65. struct usb_desc_type_t {
  66. UNION_STRUCT_START(8);
  67. enum usb_desc_type id : 5;
  68. enum usb_desc_type_type {
  69. USB_DESC_TYPE_STD = 0,
  70. USB_DESC_TYPE_CLASS = 1,
  71. USB_DESC_TYPE_VENDOR = 2
  72. } type_type : 2;
  73. uint8_t _rsvd0 : 1;
  74. UNION_STRUCT_END;
  75. };
  76. CTASSERT_SIZE_BYTE(struct usb_desc_type_t, 1);
  77. enum usb_dev_class {
  78. USB_DEV_CLASS_SEE_IFACE = 0,
  79. USB_DEV_CLASS_APP = 0xfe,
  80. USB_DEV_CLASS_VENDOR = 0xff
  81. };
  82. enum usb_dev_subclass {
  83. USB_DEV_SUBCLASS_SEE_IFACE = 0,
  84. USB_DEV_SUBCLASS_VENDOR = 0xff
  85. };
  86. enum usb_dev_proto {
  87. USB_DEV_PROTO_SEE_IFACE = 0,
  88. USB_DEV_PROTO_VENDOR = 0xff
  89. };
  90. struct usb_bcd_t {
  91. UNION_STRUCT_START(16);
  92. struct {
  93. uint8_t min : 8;
  94. uint8_t maj : 8;
  95. };
  96. UNION_STRUCT_END;
  97. };
  98. CTASSERT_SIZE_BYTE(struct usb_bcd_t, 2);
  99. struct usb_desc_generic_t {
  100. uint8_t bLength;
  101. struct usb_desc_type_t bDescriptorType;
  102. uint8_t data[];
  103. };
  104. CTASSERT_SIZE_BYTE(struct usb_desc_generic_t, 2);
  105. struct usb_desc_dev_t {
  106. uint8_t bLength;
  107. enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_DEV */
  108. struct usb_bcd_t bcdUSB; /* = 0x0200 */
  109. enum usb_dev_class bDeviceClass : 8;
  110. enum usb_dev_subclass bDeviceSubClass : 8;
  111. enum usb_dev_proto bDeviceProtocol : 8;
  112. uint8_t bMaxPacketSize0;
  113. uint16_t idVendor;
  114. uint16_t idProduct;
  115. struct usb_bcd_t bcdDevice;
  116. uint8_t iManufacturer;
  117. uint8_t iProduct;
  118. uint8_t iSerialNumber;
  119. uint8_t bNumConfigurations;
  120. };
  121. CTASSERT_SIZE_BYTE(struct usb_desc_dev_t, 18);
  122. struct usb_desc_ep_t {
  123. uint8_t bLength;
  124. enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_EP */
  125. union {
  126. struct {
  127. uint8_t ep_num : 4;
  128. uint8_t _rsvd0 : 3;
  129. uint8_t in : 1;
  130. };
  131. uint8_t bEndpointAddress;
  132. };
  133. struct {
  134. enum usb_ep_type {
  135. USB_EP_CONTROL = 0,
  136. USB_EP_ISO = 1,
  137. USB_EP_BULK = 2,
  138. USB_EP_INTR = 3
  139. } type : 2;
  140. enum usb_ep_iso_synctype {
  141. USB_EP_ISO_NOSYNC = 0,
  142. USB_EP_ISO_ASYNC = 1,
  143. USB_EP_ISO_ADAPTIVE = 2,
  144. USB_EP_ISO_SYNC = 3
  145. } sync_type : 2;
  146. enum usb_ep_iso_usagetype {
  147. USB_EP_ISO_DATA = 0,
  148. USB_EP_ISO_FEEDBACK = 1,
  149. USB_EP_ISO_IMPLICIT = 2
  150. } usage_type : 2;
  151. uint8_t _rsvd1 : 2;
  152. };
  153. struct {
  154. uint16_t wMaxPacketSize : 11;
  155. uint16_t _rsvd2 : 5;
  156. };
  157. uint8_t bInterval;
  158. } __packed;
  159. CTASSERT_SIZE_BYTE(struct usb_desc_ep_t, 7);
  160. struct usb_desc_iface_t {
  161. uint8_t bLength;
  162. enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_IFACE */
  163. uint8_t bInterfaceNumber;
  164. uint8_t bAlternateSetting;
  165. uint8_t bNumEndpoints;
  166. enum usb_dev_class bInterfaceClass : 8;
  167. enum usb_dev_subclass bInterfaceSubClass: 8;
  168. enum usb_dev_proto bInterfaceProtocol : 8;
  169. uint8_t iInterface;
  170. };
  171. CTASSERT_SIZE_BYTE(struct usb_desc_iface_t, 9);
  172. struct usb_desc_config_t {
  173. uint8_t bLength;
  174. enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_CONFIG */
  175. uint16_t wTotalLength; /* size of config, iface, ep */
  176. uint8_t bNumInterfaces;
  177. uint8_t bConfigurationValue;
  178. uint8_t iConfiguration;
  179. struct {
  180. uint8_t _rsvd0 : 5;
  181. uint8_t remote_wakeup : 1;
  182. uint8_t self_powered : 1;
  183. uint8_t one : 1; /* = 1 for historical reasons */
  184. };
  185. uint8_t bMaxPower; /* units of 2mA */
  186. } __packed;
  187. CTASSERT_SIZE_BYTE(struct usb_desc_config_t, 9);
  188. struct usb_desc_string_t {
  189. uint8_t bLength;
  190. enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_STRING */
  191. char16_t bString[];
  192. };
  193. CTASSERT_SIZE_BYTE(struct usb_desc_string_t, 2);
  194. struct usb_ctrl_req_t {
  195. union /* reqtype and request & u16 */ {
  196. struct /* reqtype and request */ {
  197. union /* reqtype in bitfield & u8 */ {
  198. struct /* reqtype */ {
  199. enum usb_ctrl_req_recp {
  200. USB_CTRL_REQ_DEV = 0,
  201. USB_CTRL_REQ_IFACE = 1,
  202. USB_CTRL_REQ_EP = 2,
  203. USB_CTRL_REQ_OTHER = 3
  204. } recp : 5;
  205. enum usb_ctrl_req_type {
  206. USB_CTRL_REQ_STD = 0,
  207. USB_CTRL_REQ_CLASS = 1,
  208. USB_CTRL_REQ_VENDOR = 2
  209. } type : 2;
  210. enum usb_ctrl_req_dir {
  211. USB_CTRL_REQ_OUT = 0,
  212. USB_CTRL_REQ_IN = 1
  213. } in : 1;
  214. };
  215. uint8_t bmRequestType;
  216. }; /* union */
  217. enum usb_ctrl_req_code {
  218. USB_CTRL_REQ_GET_STATUS = 0,
  219. USB_CTRL_REQ_CLEAR_FEATURE = 1,
  220. USB_CTRL_REQ_SET_FEATURE = 3,
  221. USB_CTRL_REQ_SET_ADDRESS = 5,
  222. USB_CTRL_REQ_GET_DESCRIPTOR = 6,
  223. USB_CTRL_REQ_SET_DESCRIPTOR = 7,
  224. USB_CTRL_REQ_GET_CONFIGURATION = 8,
  225. USB_CTRL_REQ_SET_CONFIGURATION = 9,
  226. USB_CTRL_REQ_GET_INTERFACE = 10,
  227. USB_CTRL_REQ_SET_INTERFACE = 11,
  228. USB_CTRL_REQ_SYNC_FRAME = 12
  229. } bRequest : 8;
  230. }; /* struct */
  231. uint16_t type_and_req;
  232. }; /* union */
  233. union {
  234. uint16_t wValue;
  235. struct {
  236. uint8_t wValueLow;
  237. uint8_t wValueHigh;
  238. };
  239. };
  240. uint16_t wIndex;
  241. uint16_t wLength;
  242. };
  243. CTASSERT_SIZE_BYTE(struct usb_ctrl_req_t, 8);
  244. /**
  245. * status replies for GET_STATUS.
  246. */
  247. struct usb_ctrl_req_status_dev_t {
  248. uint16_t self_powered : 1;
  249. uint16_t remote_wakeup : 1;
  250. uint16_t _rsvd : 14;
  251. };
  252. CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_dev_t, 16);
  253. struct usb_ctrl_req_status_iface_t {
  254. uint16_t _rsvd;
  255. };
  256. CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_iface_t, 16);
  257. struct usb_ctrl_req_status_ep_t {
  258. uint16_t halt : 1;
  259. uint16_t _rsvd : 15;
  260. };
  261. CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_ep_t, 16);
  262. /**
  263. * Descriptor type (in req->value) for GET_DESCRIPTOR.
  264. */
  265. struct usb_ctrl_req_desc_t {
  266. uint8_t idx;
  267. enum usb_desc_type type : 8;
  268. };
  269. CTASSERT_SIZE_BIT(struct usb_ctrl_req_desc_t, 16);
  270. /**
  271. * Feature selector (in req->value) for CLEAR_FEATURE.
  272. */
  273. enum usb_ctrl_req_feature {
  274. USB_CTRL_REQ_FEAT_EP_HALT = 0,
  275. USB_CTRL_REQ_FEAT_DEV_REMOTE_WKUP = 1,
  276. USB_CTRL_REQ_FEAT_TEST_MODE = 2
  277. };
  278. struct usb_xfer_info;
  279. typedef void (*ep_callback_t)(void *buf, ssize_t len, void *data);
  280. /**
  281. * (Artificial) function. Aggregates one or more interfaces.
  282. */
  283. struct usbd_function {
  284. int (*configure)(int orig_iface, int iface, int altsetting, void *data);
  285. int (*control)(struct usb_ctrl_req_t *, void *);
  286. int interface_count;
  287. int ep_rx_count;
  288. int ep_tx_count;
  289. };
  290. struct usbd_function_ctx_header {
  291. struct usbd_function_ctx_header *next;
  292. const struct usbd_function *function;
  293. int interface_offset;
  294. int ep_rx_offset;
  295. int ep_tx_offset;
  296. };
  297. typedef void (usbd_init_fun_t)(int);
  298. typedef void (usbd_suspend_resume_fun_t)(void);
  299. /**
  300. * Configuration. Contains one or more functions which all will be
  301. * active concurrently.
  302. */
  303. struct usbd_config {
  304. usbd_init_fun_t *init;
  305. usbd_suspend_resume_fun_t *suspend;
  306. usbd_suspend_resume_fun_t *resume;
  307. /**
  308. * We will not set a config for now, because there is not much to
  309. * configure, except for power
  310. *
  311. * const struct usb_desc_config_t *config_desc;
  312. */
  313. const struct usb_desc_config_t *desc;
  314. const struct usbd_function *function[];
  315. };
  316. /**
  317. * Device. Contains one or more configurations, out of which only one
  318. * is active at a time.
  319. */
  320. struct usbd_device {
  321. const struct usb_desc_dev_t *dev_desc;
  322. struct usb_desc_string_t * const *string_descs;
  323. const struct usbd_config *configs[];
  324. };
  325. /* Provided by MD code */
  326. struct usbd_ep_pipe_state_t;
  327. // ----- Functions -----
  328. void *usb_get_xfer_data(struct usb_xfer_info *);
  329. enum usb_tok_pid usb_get_xfer_pid(struct usb_xfer_info *);
  330. int usb_get_xfer_ep(struct usb_xfer_info *);
  331. enum usb_ep_dir usb_get_xfer_dir(struct usb_xfer_info *);
  332. void usb_enable_xfers(void);
  333. void usb_set_addr(int);
  334. void usb_ep_stall(int);
  335. size_t usb_ep_get_transfer_size(struct usbd_ep_pipe_state_t *);
  336. void usb_queue_next(struct usbd_ep_pipe_state_t *, void *, size_t);
  337. void usb_pipe_stall(struct usbd_ep_pipe_state_t *);
  338. void usb_pipe_unstall(struct usbd_ep_pipe_state_t *);
  339. void usb_pipe_enable(struct usbd_ep_pipe_state_t *s);
  340. void usb_pipe_disable(struct usbd_ep_pipe_state_t *s);
  341. #ifdef VUSB
  342. void vusb_main_loop(void);
  343. #else
  344. void usb_poll(void);
  345. #endif
  346. int usb_tx_serialno(size_t reqlen);
  347. /* Provided by MI code */
  348. void usb_init(const struct usbd_device *);
  349. void usb_attach_function(const struct usbd_function *function, struct usbd_function_ctx_header *ctx);
  350. void usb_handle_transaction(struct usb_xfer_info *);
  351. void usb_setup_control(void);
  352. void usb_handle_control_status_cb(ep_callback_t cb);
  353. void usb_handle_control_status(int);
  354. struct usbd_ep_pipe_state_t *usb_init_ep(struct usbd_function_ctx_header *ctx, int ep, enum usb_ep_dir dir, size_t size);
  355. int usb_rx(struct usbd_ep_pipe_state_t *, void *, size_t, ep_callback_t, void *);
  356. int usb_tx(struct usbd_ep_pipe_state_t *, const void *, size_t, size_t, ep_callback_t, void *);
  357. int usb_ep0_rx(void *, size_t, ep_callback_t, void *);
  358. void *usb_ep0_tx_inplace_prepare(size_t len);
  359. int usb_ep0_tx(void *buf, size_t len, size_t reqlen, ep_callback_t cb, void *cb_data);
  360. int usb_ep0_tx_cp(const void *, size_t, size_t, ep_callback_t, void *);
  361. // ----- DFU USB Additional Includes -----
  362. #include "dfu.h"