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

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