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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. #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. (const void *)&(const 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. #define USB_FUNCTION_IFACE(iface, iface_off, tx_ep_off, rx_ep_off) \
  48. ((iface_off) + (iface))
  49. #define USB_FUNCTION_TX_EP(ep, iface_off, tx_ep_off, rx_ep_off) \
  50. ((tx_ep_off) + (ep))
  51. #define USB_FUNCTION_RX_EP(ep, iface_off, tx_ep_off, rx_ep_off) \
  52. ((rx_ep_off) + (ep))
  53. #define USB__INCREMENT(i, _0) (i + 1)
  54. #define USB__COUNT_IFACE_EP(i, e) \
  55. __DEFER(USB__COUNT_IFACE_EP_)(__EXPAND i, e)
  56. #define USB__COUNT_IFACE_EP_(iface, tx_ep, rx_ep, func) \
  57. (iface + USB_FUNCTION_ ## func ## _IFACE_COUNT, \
  58. tx_ep + USB_FUNCTION_ ## func ## _TX_EP_COUNT, \
  59. rx_ep + USB_FUNCTION_ ## func ## _RX_EP_COUNT)
  60. #define USB__GET_FUNCTION_IFACE_COUNT(iter, func) \
  61. USB_FUNCTION_ ## func ## _IFACE_COUNT +
  62. #define USB__DEFINE_FUNCTION_DESC(iter, func) \
  63. USB_FUNCTION_DESC_ ## func ## _DECL __CAT(__usb_func_desc, __COUNTER__);
  64. #define USB__INIT_FUNCTION_DESC(iter, func) \
  65. USB_FUNCTION_DESC_ ## func iter,
  66. #define USB__DEFINE_CONFIG_DESC(confignum, name, ...) \
  67. &((const struct name { \
  68. struct usb_desc_config_t config; \
  69. __REPEAT_INNER(, __EAT, USB__DEFINE_FUNCTION_DESC, __VA_ARGS__) \
  70. }){ \
  71. .config = { \
  72. .bLength = sizeof(struct usb_desc_config_t), \
  73. .bDescriptorType = USB_DESC_CONFIG, \
  74. .wTotalLength = sizeof(struct name), \
  75. .bNumInterfaces = __REPEAT_INNER(, __EAT, USB__GET_FUNCTION_IFACE_COUNT, __VA_ARGS__) 0, \
  76. .bConfigurationValue = confignum, \
  77. .iConfiguration = 0, \
  78. .one = 1, \
  79. .bMaxPower = 50 \
  80. }, \
  81. __REPEAT_INNER((0, 0, 0), USB__COUNT_IFACE_EP, USB__INIT_FUNCTION_DESC, __VA_ARGS__) \
  82. }).config
  83. #define USB__DEFINE_CONFIG(iter, args) \
  84. __DEFER(USB__DEFINE_CONFIG_)(iter, __EXPAND args)
  85. #define USB__DEFINE_CONFIG_(confignum, initfun, ...) \
  86. &(const struct usbd_config){ \
  87. .init = initfun, \
  88. .desc = USB__DEFINE_CONFIG_DESC( \
  89. confignum, \
  90. __CAT(__usb_desc, __COUNTER__), \
  91. __VA_ARGS__) \
  92. },
  93. #define USB_INIT_DEVICE(vid, pid, manuf, product, ...) \
  94. { \
  95. .dev_desc = &(const struct usb_desc_dev_t){ \
  96. .bLength = sizeof(struct usb_desc_dev_t), \
  97. .bDescriptorType = USB_DESC_DEV, \
  98. .bcdUSB = { .maj = 2 }, \
  99. .bDeviceClass = USB_DEV_CLASS_SEE_IFACE, \
  100. .bDeviceSubClass = USB_DEV_SUBCLASS_SEE_IFACE, \
  101. .bDeviceProtocol = USB_DEV_PROTO_SEE_IFACE, \
  102. .bMaxPacketSize0 = EP0_BUFSIZE, \
  103. .idVendor = vid, \
  104. .idProduct = pid, \
  105. .bcdDevice = { .raw = 0 }, \
  106. .iManufacturer = 1, \
  107. .iProduct = 2, \
  108. .iSerialNumber = 3, \
  109. .bNumConfigurations = __PP_NARG(__VA_ARGS__), \
  110. }, \
  111. .string_descs = (const struct usb_desc_string_t * const []){ \
  112. USB_DESC_STRING_LANG_ENUS, \
  113. USB_DESC_STRING(manuf), \
  114. USB_DESC_STRING(product), \
  115. USB_DESC_STRING_SERIALNO, \
  116. NULL \
  117. }, \
  118. .configs = { \
  119. __REPEAT(1, USB__INCREMENT, USB__DEFINE_CONFIG, __VA_ARGS__) \
  120. NULL \
  121. } \
  122. }
  123. // ----- Structs & Enumerations -----
  124. /**
  125. * Note: bitfields ahead.
  126. * GCC fills the fields lsb-to-msb on little endian.
  127. */
  128. /**
  129. * USB descriptors
  130. */
  131. enum usb_desc_type {
  132. USB_DESC_DEV = 1,
  133. USB_DESC_CONFIG = 2,
  134. USB_DESC_STRING = 3,
  135. USB_DESC_IFACE = 4,
  136. USB_DESC_EP = 5,
  137. USB_DESC_DEVQUAL = 6,
  138. USB_DESC_OTHERSPEED = 7,
  139. USB_DESC_POWER = 8
  140. };
  141. struct usb_desc_type_t {
  142. UNION_STRUCT_START(8);
  143. enum usb_desc_type id : 5;
  144. enum usb_desc_type_type {
  145. USB_DESC_TYPE_STD = 0,
  146. USB_DESC_TYPE_CLASS = 1,
  147. USB_DESC_TYPE_VENDOR = 2
  148. } type_type : 2;
  149. uint8_t _rsvd0 : 1;
  150. UNION_STRUCT_END;
  151. };
  152. CTASSERT_SIZE_BYTE(struct usb_desc_type_t, 1);
  153. enum usb_dev_class {
  154. USB_DEV_CLASS_SEE_IFACE = 0,
  155. USB_DEV_CLASS_APP = 0xfe,
  156. USB_DEV_CLASS_VENDOR = 0xff
  157. };
  158. enum usb_dev_subclass {
  159. USB_DEV_SUBCLASS_SEE_IFACE = 0,
  160. USB_DEV_SUBCLASS_VENDOR = 0xff
  161. };
  162. enum usb_dev_proto {
  163. USB_DEV_PROTO_SEE_IFACE = 0,
  164. USB_DEV_PROTO_VENDOR = 0xff
  165. };
  166. struct usb_bcd_t {
  167. UNION_STRUCT_START(16);
  168. struct {
  169. uint8_t sub : 4;
  170. uint8_t min : 4;
  171. uint16_t maj : 8;
  172. };
  173. UNION_STRUCT_END;
  174. };
  175. CTASSERT_SIZE_BYTE(struct usb_bcd_t, 2);
  176. struct usb_desc_generic_t {
  177. uint8_t bLength;
  178. struct usb_desc_type_t bDescriptorType;
  179. uint8_t data[];
  180. };
  181. CTASSERT_SIZE_BYTE(struct usb_desc_generic_t, 2);
  182. struct usb_desc_dev_t {
  183. uint8_t bLength;
  184. enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_DEV */
  185. struct usb_bcd_t bcdUSB; /* = 0x0200 */
  186. enum usb_dev_class bDeviceClass : 8;
  187. enum usb_dev_subclass bDeviceSubClass : 8;
  188. enum usb_dev_proto bDeviceProtocol : 8;
  189. uint8_t bMaxPacketSize0;
  190. uint16_t idVendor;
  191. uint16_t idProduct;
  192. struct usb_bcd_t bcdDevice;
  193. uint8_t iManufacturer;
  194. uint8_t iProduct;
  195. uint8_t iSerialNumber;
  196. uint8_t bNumConfigurations;
  197. };
  198. CTASSERT_SIZE_BYTE(struct usb_desc_dev_t, 18);
  199. struct usb_desc_ep_t {
  200. uint8_t bLength;
  201. enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_EP */
  202. union {
  203. struct {
  204. uint8_t ep_num : 4;
  205. uint8_t _rsvd0 : 3;
  206. uint8_t in : 1;
  207. };
  208. uint8_t bEndpointAddress;
  209. };
  210. struct {
  211. enum usb_ep_type {
  212. USB_EP_CONTROL = 0,
  213. USB_EP_ISO = 1,
  214. USB_EP_BULK = 2,
  215. USB_EP_INTR = 3
  216. } type : 2;
  217. enum usb_ep_iso_synctype {
  218. USB_EP_ISO_NOSYNC = 0,
  219. USB_EP_ISO_ASYNC = 1,
  220. USB_EP_ISO_ADAPTIVE = 2,
  221. USB_EP_ISO_SYNC = 3
  222. } sync_type : 2;
  223. enum usb_ep_iso_usagetype {
  224. USB_EP_ISO_DATA = 0,
  225. USB_EP_ISO_FEEDBACK = 1,
  226. USB_EP_ISO_IMPLICIT = 2
  227. } usage_type : 2;
  228. uint8_t _rsvd1 : 2;
  229. };
  230. struct {
  231. uint16_t wMaxPacketSize : 11;
  232. uint16_t _rsvd2 : 5;
  233. };
  234. uint8_t bInterval;
  235. } __packed;
  236. CTASSERT_SIZE_BYTE(struct usb_desc_ep_t, 7);
  237. struct usb_desc_iface_t {
  238. uint8_t bLength;
  239. enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_IFACE */
  240. uint8_t bInterfaceNumber;
  241. uint8_t bAlternateSetting;
  242. uint8_t bNumEndpoints;
  243. enum usb_dev_class bInterfaceClass : 8;
  244. enum usb_dev_subclass bInterfaceSubClass: 8;
  245. enum usb_dev_proto bInterfaceProtocol : 8;
  246. uint8_t iInterface;
  247. };
  248. CTASSERT_SIZE_BYTE(struct usb_desc_iface_t, 9);
  249. struct usb_desc_config_t {
  250. uint8_t bLength;
  251. enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_CONFIG */
  252. uint16_t wTotalLength; /* size of config, iface, ep */
  253. uint8_t bNumInterfaces;
  254. uint8_t bConfigurationValue;
  255. uint8_t iConfiguration;
  256. struct {
  257. uint8_t _rsvd0 : 5;
  258. uint8_t remote_wakeup : 1;
  259. uint8_t self_powered : 1;
  260. uint8_t one : 1; /* = 1 for historical reasons */
  261. };
  262. uint8_t bMaxPower; /* units of 2mA */
  263. } __packed;
  264. CTASSERT_SIZE_BYTE(struct usb_desc_config_t, 9);
  265. struct usb_desc_string_t {
  266. uint8_t bLength;
  267. enum usb_desc_type bDescriptorType : 8; /* = USB_DESC_STRING */
  268. const char16_t bString[];
  269. };
  270. CTASSERT_SIZE_BYTE(struct usb_desc_string_t, 2);
  271. struct usb_ctrl_req_t {
  272. union /* reqtype and request & u16 */ {
  273. struct /* reqtype and request */ {
  274. union /* reqtype in bitfield & u8 */ {
  275. struct /* reqtype */ {
  276. enum usb_ctrl_req_recp {
  277. USB_CTRL_REQ_DEV = 0,
  278. USB_CTRL_REQ_IFACE = 1,
  279. USB_CTRL_REQ_EP = 2,
  280. USB_CTRL_REQ_OTHER = 3
  281. } recp : 5;
  282. enum usb_ctrl_req_type {
  283. USB_CTRL_REQ_STD = 0,
  284. USB_CTRL_REQ_CLASS = 1,
  285. USB_CTRL_REQ_VENDOR = 2
  286. } type : 2;
  287. enum usb_ctrl_req_dir {
  288. USB_CTRL_REQ_OUT = 0,
  289. USB_CTRL_REQ_IN = 1
  290. } in : 1;
  291. };
  292. uint8_t bmRequestType;
  293. }; /* union */
  294. enum usb_ctrl_req_code {
  295. USB_CTRL_REQ_GET_STATUS = 0,
  296. USB_CTRL_REQ_CLEAR_FEATURE = 1,
  297. USB_CTRL_REQ_SET_FEATURE = 3,
  298. USB_CTRL_REQ_SET_ADDRESS = 5,
  299. USB_CTRL_REQ_GET_DESCRIPTOR = 6,
  300. USB_CTRL_REQ_SET_DESCRIPTOR = 7,
  301. USB_CTRL_REQ_GET_CONFIGURATION = 8,
  302. USB_CTRL_REQ_SET_CONFIGURATION = 9,
  303. USB_CTRL_REQ_GET_INTERFACE = 10,
  304. USB_CTRL_REQ_SET_INTERFACE = 11,
  305. USB_CTRL_REQ_SYNC_FRAME = 12
  306. } bRequest : 8;
  307. }; /* struct */
  308. uint16_t type_and_req;
  309. }; /* union */
  310. union {
  311. uint16_t wValue;
  312. struct {
  313. uint8_t wValueLow;
  314. uint8_t wValueHigh;
  315. };
  316. };
  317. uint16_t wIndex;
  318. uint16_t wLength;
  319. };
  320. CTASSERT_SIZE_BYTE(struct usb_ctrl_req_t, 8);
  321. /**
  322. * status replies for GET_STATUS.
  323. */
  324. struct usb_ctrl_req_status_dev_t {
  325. uint16_t self_powered : 1;
  326. uint16_t remote_wakeup : 1;
  327. uint16_t _rsvd : 14;
  328. };
  329. CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_dev_t, 16);
  330. struct usb_ctrl_req_status_iface_t {
  331. uint16_t _rsvd;
  332. };
  333. CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_iface_t, 16);
  334. struct usb_ctrl_req_status_ep_t {
  335. uint16_t halt : 1;
  336. uint16_t _rsvd : 15;
  337. };
  338. CTASSERT_SIZE_BIT(struct usb_ctrl_req_status_ep_t, 16);
  339. /**
  340. * Descriptor type (in req->value) for GET_DESCRIPTOR.
  341. */
  342. struct usb_ctrl_req_desc_t {
  343. uint8_t idx;
  344. enum usb_desc_type type : 8;
  345. };
  346. CTASSERT_SIZE_BIT(struct usb_ctrl_req_desc_t, 16);
  347. /**
  348. * Feature selector (in req->value) for CLEAR_FEATURE.
  349. */
  350. enum usb_ctrl_req_feature {
  351. USB_CTRL_REQ_FEAT_EP_HALT = 0,
  352. USB_CTRL_REQ_FEAT_DEV_REMOTE_WKUP = 1,
  353. USB_CTRL_REQ_FEAT_TEST_MODE = 2
  354. };
  355. struct usb_xfer_info;
  356. typedef void (*ep_callback_t)(void *buf, ssize_t len, void *data);
  357. /**
  358. * (Artificial) function. Aggregates one or more interfaces.
  359. */
  360. struct usbd_function {
  361. int (*configure)(int orig_iface, int iface, int altsetting, void *data);
  362. int (*control)(struct usb_ctrl_req_t *, void *);
  363. int interface_count;
  364. int ep_rx_count;
  365. int ep_tx_count;
  366. };
  367. struct usbd_function_ctx_header {
  368. struct usbd_function_ctx_header *next;
  369. const struct usbd_function *function;
  370. int interface_offset;
  371. int ep_rx_offset;
  372. int ep_tx_offset;
  373. };
  374. typedef void (usbd_init_fun_t)(int);
  375. typedef void (usbd_suspend_resume_fun_t)(void);
  376. /**
  377. * Configuration. Contains one or more functions which all will be
  378. * active concurrently.
  379. */
  380. struct usbd_config {
  381. usbd_init_fun_t *init;
  382. usbd_suspend_resume_fun_t *suspend;
  383. usbd_suspend_resume_fun_t *resume;
  384. /**
  385. * We will not set a config for now, because there is not much to
  386. * configure, except for power
  387. *
  388. * const struct usb_desc_config_t *config_desc;
  389. */
  390. const struct usb_desc_config_t *desc;
  391. const struct usbd_function *function[];
  392. };
  393. /**
  394. * Device. Contains one or more configurations, out of which only one
  395. * is active at a time.
  396. */
  397. struct usbd_device {
  398. const struct usb_desc_dev_t *dev_desc;
  399. const struct usb_desc_string_t * const *string_descs;
  400. const struct usbd_config *configs[];
  401. };
  402. /* Provided by MD code */
  403. struct usbd_ep_pipe_state_t;
  404. // ----- Functions -----
  405. void *usb_get_xfer_data(struct usb_xfer_info *);
  406. enum usb_tok_pid usb_get_xfer_pid(struct usb_xfer_info *);
  407. int usb_get_xfer_ep(struct usb_xfer_info *);
  408. enum usb_ep_dir usb_get_xfer_dir(struct usb_xfer_info *);
  409. void usb_enable_xfers(void);
  410. void usb_set_addr(int);
  411. void usb_ep_stall(int);
  412. size_t usb_ep_get_transfer_size(struct usbd_ep_pipe_state_t *);
  413. void usb_queue_next(struct usbd_ep_pipe_state_t *, void *, size_t);
  414. void usb_pipe_stall(struct usbd_ep_pipe_state_t *);
  415. void usb_pipe_unstall(struct usbd_ep_pipe_state_t *);
  416. void usb_pipe_enable(struct usbd_ep_pipe_state_t *s);
  417. void usb_pipe_disable(struct usbd_ep_pipe_state_t *s);
  418. #ifdef VUSB
  419. void vusb_main_loop(void);
  420. #else
  421. void usb_poll(void);
  422. #endif
  423. int usb_tx_serialno(size_t reqlen);
  424. /* Provided by MI code */
  425. void usb_init(const struct usbd_device *);
  426. void usb_attach_function(const struct usbd_function *function, struct usbd_function_ctx_header *ctx);
  427. void usb_handle_transaction(struct usb_xfer_info *);
  428. void usb_setup_control(void);
  429. void usb_handle_control_status_cb(ep_callback_t cb);
  430. void usb_handle_control_status(int);
  431. 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);
  432. int usb_rx(struct usbd_ep_pipe_state_t *, void *, size_t, ep_callback_t, void *);
  433. int usb_tx(struct usbd_ep_pipe_state_t *, const void *, size_t, size_t, ep_callback_t, void *);
  434. int usb_ep0_rx(void *, size_t, ep_callback_t, void *);
  435. void *usb_ep0_tx_inplace_prepare(size_t len);
  436. int usb_ep0_tx(void *buf, size_t len, size_t reqlen, ep_callback_t cb, void *cb_data);
  437. int usb_ep0_tx_cp(const void *, size_t, size_t, ep_callback_t, void *);
  438. // ----- DFU USB Additional Includes -----
  439. #include "dfu.h"