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

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