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_desc.c 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. * Modified by Jacob Alexander (2013-2016)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * 1. The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * 2. If the Software is incorporated into a build system that allows
  18. * selection among a list of target devices, then similar target
  19. * devices manufactured by PJRC.COM must be included in the list of
  20. * target devices and selectable in the same manner.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  26. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  27. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  28. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. */
  31. // ----- Includes -----
  32. // Local Includes
  33. #include "usb_desc.h"
  34. // Generated Includes
  35. #include <kll_defs.h>
  36. // ----- Macros -----
  37. #define LSB(n) ((n) & 255)
  38. #define MSB(n) (((n) >> 8) & 255)
  39. // ----- USB Device Descriptor -----
  40. // USB Device Descriptor. The USB host reads this first, to learn
  41. // what type of device is connected.
  42. static uint8_t device_descriptor[] = {
  43. 18, // bLength
  44. 1, // bDescriptorType
  45. 0x00, 0x02, // bcdUSB
  46. DEVICE_CLASS, // bDeviceClass
  47. DEVICE_SUBCLASS, // bDeviceSubClass
  48. DEVICE_PROTOCOL, // bDeviceProtocol
  49. EP0_SIZE, // bMaxPacketSize0
  50. LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
  51. LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
  52. 0x00, 0x01, // bcdDevice
  53. 1, // iManufacturer
  54. 2, // iProduct
  55. 3, // iSerialNumber
  56. 1 // bNumConfigurations
  57. };
  58. // USB Device Qualifier Descriptor
  59. static uint8_t device_qualifier_descriptor[] = {
  60. 0 // Indicate only single speed
  61. /* Device qualifier example (used for specifying multiple USB speeds)
  62. 10, // bLength
  63. 6, // bDescriptorType
  64. 0x00, 0x02, // bcdUSB
  65. DEVICE_CLASS, // bDeviceClass
  66. DEVICE_SUBCLASS, // bDeviceSubClass
  67. DEVICE_PROTOCOL, // bDeviceProtocol
  68. EP0_SIZE, // bMaxPacketSize0
  69. 0, // bNumOtherSpeedConfigurations
  70. 0 // bReserved
  71. */
  72. };
  73. // USB Debug Descriptor
  74. // XXX Not sure of exact use, lsusb requests it
  75. static uint8_t usb_debug_descriptor[] = {
  76. 0
  77. };
  78. // XXX
  79. // These descriptors must NOT be "const", because the USB DMA
  80. // has trouble accessing flash memory with enough bandwidth
  81. // while the processor is executing from flash.
  82. // XXX
  83. // ----- USB HID Report Descriptors -----
  84. // Each HID interface needs a special report descriptor that tells
  85. // the meaning and format of the data.
  86. // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
  87. static uint8_t keyboard_report_desc[] = {
  88. // Keyboard Collection
  89. 0x05, 0x01, // Usage Page (Generic Desktop),
  90. 0x09, 0x06, // Usage (Keyboard),
  91. 0xA1, 0x01, // Collection (Application) - Keyboard,
  92. // Modifier Byte
  93. 0x75, 0x01, // Report Size (1),
  94. 0x95, 0x08, // Report Count (8),
  95. 0x05, 0x07, // Usage Page (Key Codes),
  96. 0x19, 0xE0, // Usage Minimum (224),
  97. 0x29, 0xE7, // Usage Maximum (231),
  98. 0x15, 0x00, // Logical Minimum (0),
  99. 0x25, 0x01, // Logical Maximum (1),
  100. 0x81, 0x02, // Input (Data, Variable, Absolute),
  101. // Reserved Byte
  102. 0x75, 0x08, // Report Size (8),
  103. 0x95, 0x01, // Report Count (1),
  104. 0x81, 0x03, // Output (Constant),
  105. // LED Report
  106. 0x75, 0x01, // Report Size (1),
  107. 0x95, 0x05, // Report Count (5),
  108. 0x05, 0x08, // Usage Page (LEDs),
  109. 0x19, 0x01, // Usage Minimum (1),
  110. 0x29, 0x05, // Usage Maximum (5),
  111. 0x91, 0x02, // Output (Data, Variable, Absolute),
  112. // LED Report Padding
  113. 0x75, 0x03, // Report Size (3),
  114. 0x95, 0x01, // Report Count (1),
  115. 0x91, 0x03, // Output (Constant),
  116. // Normal Keys
  117. 0x75, 0x08, // Report Size (8),
  118. 0x95, 0x06, // Report Count (6),
  119. 0x15, 0x00, // Logical Minimum (0),
  120. 0x25, 0x7F, // Logical Maximum(104),
  121. 0x05, 0x07, // Usage Page (Key Codes),
  122. 0x19, 0x00, // Usage Minimum (0),
  123. 0x29, 0x7F, // Usage Maximum (104),
  124. 0x81, 0x00, // Input (Data, Array),
  125. 0xc0, // End Collection - Keyboard
  126. };
  127. // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
  128. static uint8_t nkro_keyboard_report_desc[] = {
  129. // Keyboard Collection
  130. 0x05, 0x01, // Usage Page (Generic Desktop),
  131. 0x09, 0x06, // Usage (Keyboard),
  132. 0xA1, 0x01, // Collection (Application) - Keyboard,
  133. // LED Report
  134. 0x85, 0x01, // Report ID (1),
  135. 0x75, 0x01, // Report Size (1),
  136. 0x95, 0x05, // Report Count (5),
  137. 0x05, 0x08, // Usage Page (LEDs),
  138. 0x19, 0x01, // Usage Minimum (1),
  139. 0x29, 0x05, // Usage Maximum (5),
  140. 0x91, 0x02, // Output (Data, Variable, Absolute),
  141. // LED Report Padding
  142. 0x75, 0x03, // Report Size (3),
  143. 0x95, 0x01, // Report Count (1),
  144. 0x91, 0x03, // Output (Constant),
  145. // Normal Keys - Using an NKRO Bitmap
  146. //
  147. // NOTES:
  148. // Supports all keys defined by the spec, except 1-3 which define error events
  149. // and 0 which is "no keys pressed"
  150. // See http://www.usb.org/developers/hidpage/Hut1_12v2.pdf Chapter 10
  151. // Or Macros/PartialMap/usb_hid.h
  152. //
  153. // 50 (ISO \ due to \ bug) and 156 (Clear due to Delete bug) must be excluded
  154. // due to a Linux bug with bitmaps (not useful anyways)
  155. // 165-175 are reserved/unused as well as 222-223 and 232-65535
  156. //
  157. // Compatibility Notes:
  158. // - Using a second endpoint for a boot mode device helps with compatibility
  159. // - DO NOT use Padding in the descriptor for bitfields
  160. // (Mac OSX silently fails... Windows/Linux work correctly)
  161. // - DO NOT use Report IDs, Windows 8.1 will not update keyboard correctly (modifiers disappear)
  162. // (all other OSs, including OSX work fine...)
  163. // (you can use them *iff* you only have 1 per collection)
  164. // - Mac OSX and Windows 8.1 are extremely picky about padding
  165. //
  166. // Packing of bitmaps are as follows:
  167. // 4-49 : 6 bytes (0x04-0x31) ( 46 bits + 2 padding bits for 6 bytes total)
  168. // 51-155 : 14 bytes (0x33-0x9B) (105 bits + 6 padding bits for 15 bytes total)
  169. // 157-164 : 1 byte (0x9D-0xA4) ( 8 bits)
  170. // 176-221 : 6 bytes (0xB0-0xDD) ( 46 bits + 2 padding bits for 6 bytes total)
  171. // 224-231 : 1 byte (0xE0-0xE7) ( 8 bits)
  172. // Modifier Byte
  173. 0x75, 0x01, // Report Size (1),
  174. 0x95, 0x08, // Report Count (8),
  175. 0x15, 0x00, // Logical Minimum (0),
  176. 0x25, 0x01, // Logical Maximum (1),
  177. 0x05, 0x07, // Usage Page (Key Codes),
  178. 0x19, 0xE0, // Usage Minimum (224),
  179. 0x29, 0xE7, // Usage Maximum (231),
  180. 0x81, 0x02, // Input (Data, Variable, Absolute),
  181. // 4-49 (6 bytes/46 bits) - MainKeys
  182. 0x75, 0x01, // Report Size (1),
  183. 0x95, 0x2E, // Report Count (46),
  184. 0x15, 0x00, // Logical Minimum (0),
  185. 0x25, 0x01, // Logical Maximum (1),
  186. 0x05, 0x07, // Usage Page (Key Codes),
  187. 0x19, 0x04, // Usage Minimum (4),
  188. 0x29, 0x31, // Usage Maximum (49),
  189. 0x81, 0x02, // Input (Data, Variable, Absolute, Bitfield),
  190. // Padding (2 bits)
  191. 0x75, 0x02, // Report Size (2),
  192. 0x95, 0x01, // Report Count (1),
  193. 0x81, 0x03, // Input (Constant),
  194. // 51-155 (14 bytes/105 bits) - SecondaryKeys
  195. 0x75, 0x01, // Report Size (1),
  196. 0x95, 0x69, // Report Count (105),
  197. 0x15, 0x00, // Logical Minimum (0),
  198. 0x25, 0x01, // Logical Maximum (1),
  199. 0x05, 0x07, // Usage Page (Key Codes),
  200. 0x19, 0x33, // Usage Minimum (51),
  201. 0x29, 0x9B, // Usage Maximum (155),
  202. 0x81, 0x02, // Input (Data, Variable, Absolute, Bitfield),
  203. // Padding (7 bits)
  204. 0x75, 0x07, // Report Size (7),
  205. 0x95, 0x01, // Report Count (1),
  206. 0x81, 0x03, // Input (Constant),
  207. // 157-164 (1 byte/8 bits) - TertiaryKeys
  208. 0x75, 0x01, // Report Size (1),
  209. 0x95, 0x08, // Report Count (8),
  210. 0x15, 0x00, // Logical Minimum (0),
  211. 0x25, 0x01, // Logical Maximum (1),
  212. 0x05, 0x07, // Usage Page (Key Codes),
  213. 0x19, 0x9D, // Usage Minimum (157),
  214. 0x29, 0xA4, // Usage Maximum (164),
  215. 0x81, 0x02, // Input (Data, Variable, Absolute, Bitfield),
  216. // 176-221 (6 bytes/46 bits) - QuartiaryKeys
  217. 0x75, 0x01, // Report Size (1),
  218. 0x95, 0x2E, // Report Count (46),
  219. 0x15, 0x00, // Logical Minimum (0),
  220. 0x25, 0x01, // Logical Maximum (1),
  221. 0x05, 0x07, // Usage Page (Key Codes),
  222. 0x19, 0xB0, // Usage Minimum (176),
  223. 0x29, 0xDD, // Usage Maximum (221),
  224. 0x81, 0x02, // Input (Data, Variable, Absolute, Bitfield),
  225. // Padding (2 bits)
  226. 0x75, 0x02, // Report Size (2),
  227. 0x95, 0x01, // Report Count (1),
  228. 0x81, 0x03, // Input (Constant),
  229. 0xc0, // End Collection - Keyboard
  230. };
  231. // System Control and Consumer Control
  232. static uint8_t sys_ctrl_report_desc[] = {
  233. // System Control Collection (8 bits)
  234. //
  235. // NOTES:
  236. // Not bothering with NKRO for this table. If there's need, I can implement it. -HaaTa
  237. // Using a 1KRO scheme
  238. 0x05, 0x01, // Usage Page (Generic Desktop),
  239. 0x09, 0x80, // Usage (System Control),
  240. 0xA1, 0x01, // Collection (Application),
  241. 0x85, 0x02, // Report ID (2),
  242. 0x75, 0x08, // Report Size (8),
  243. 0x95, 0x01, // Report Count (1),
  244. 0x16, 0x81, 0x00, // Logical Minimum (129),
  245. 0x26, 0xB7, 0x00, // Logical Maximum (183),
  246. 0x19, 0x81, // Usage Minimum (129),
  247. 0x29, 0xB7, // Usage Maximum (183),
  248. 0x81, 0x00, // Input (Data, Array),
  249. 0xc0, // End Collection - System Control
  250. // Consumer Control Collection - Media Keys (16 bits)
  251. //
  252. // NOTES:
  253. // Not bothering with NKRO for this table. If there's a need, I can implement it. -HaaTa
  254. // Using a 1KRO scheme
  255. 0x05, 0x0c, // Usage Page (Consumer),
  256. 0x09, 0x01, // Usage (Consumer Control),
  257. 0xA1, 0x01, // Collection (Application),
  258. 0x85, 0x03, // Report ID (3),
  259. 0x75, 0x10, // Report Size (16),
  260. 0x95, 0x01, // Report Count (1),
  261. 0x16, 0x01, 0x00, // Logical Minimum (1),
  262. 0x26, 0x9D, 0x02, // Logical Maximum (669),
  263. 0x05, 0x0C, // Usage Page (Consumer),
  264. 0x19, 0x01, // Usage Minimum (1),
  265. 0x2A, 0x9D, 0x02, // Usage Maximum (669),
  266. 0x81, 0x00, // Input (Data, Array),
  267. 0xc0, // End Collection - Consumer Control
  268. };
  269. // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
  270. static uint8_t mouse_report_desc[] = {
  271. 0x05, 0x01, // Usage Page (Generic Desktop)
  272. 0x09, 0x02, // Usage (Mouse)
  273. 0xa1, 0x01, // Collection (Application)
  274. 0x09, 0x02, // Usage (Mouse)
  275. 0xa1, 0x02, // Collection (Logical)
  276. 0x09, 0x01, // Usage (Pointer)
  277. // Buttons (8 bits)
  278. 0xa1, 0x00, // Collection (Physical) - Buttons
  279. 0x05, 0x09, // Usage Page (Button)
  280. 0x19, 0x01, // Usage Minimum (Button 1)
  281. 0x29, 0x08, // Usage Maximum (Button 8)
  282. 0x15, 0x00, // Logical Minimum (0)
  283. 0x25, 0x01, // Logical Maximum (1)
  284. 0x75, 0x01, // Report Size (1)
  285. 0x95, 0x08, // Report Count (8)
  286. 0x81, 0x02, // Input (Data,Var,Abs)
  287. // Pointer (16 bits)
  288. 0x05, 0x01, // Usage PAGE (Generic Desktop)
  289. 0x09, 0x30, // Usage (X)
  290. 0x09, 0x31, // Usage (Y)
  291. 0x15, 0x81, // Logical Minimum (-127)
  292. 0x25, 0x7f, // Logical Maximum (127)
  293. 0x75, 0x08, // Report Size (8)
  294. 0x95, 0x02, // Report Count (2)
  295. 0x81, 0x06, // Input (Data,Var,Rel)
  296. // Vertical Wheel
  297. // - Multiplier (2 bits)
  298. 0xa1, 0x02, // Collection (Logical)
  299. 0x09, 0x48, // Usage (Resolution Multiplier)
  300. 0x15, 0x00, // Logical Minimum (0)
  301. 0x25, 0x01, // Logical Maximum (1)
  302. 0x35, 0x01, // Physical Minimum (1)
  303. 0x45, 0x04, // Physical Maximum (4)
  304. 0x75, 0x02, // Report Size (2)
  305. 0x95, 0x01, // Report Count (1)
  306. 0xa4, // Push
  307. 0xb1, 0x02, // Feature (Data,Var,Abs)
  308. // - Device (8 bits)
  309. 0x09, 0x38, // Usage (Wheel)
  310. 0x15, 0x81, // Logical Minimum (-127)
  311. 0x25, 0x7f, // Logical Maximum (127)
  312. 0x35, 0x00, // Physical Minimum (0) - reset physical
  313. 0x45, 0x00, // Physical Maximum (0)
  314. 0x75, 0x08, // Report Size (8)
  315. 0x81, 0x06, // Input (Data,Var,Rel)
  316. 0xc0, // End Collection - Vertical Wheel
  317. // Horizontal Wheel
  318. // - Multiplier (2 bits)
  319. 0xa1, 0x02, // Collection (Logical)
  320. 0x09, 0x48, // Usage (Resolution Multiplier)
  321. 0xb4, // Pop
  322. 0xb1, 0x02, // Feature (Data,Var,Abs)
  323. // - Padding (4 bits)
  324. 0x35, 0x00, // Physical Minimum (0) - reset physical
  325. 0x45, 0x00, // Physical Maximum (0)
  326. 0x75, 0x04, // Report Size (4)
  327. 0xb1, 0x03, // Feature (Cnst,Var,Abs)
  328. // - Device (8 bits)
  329. 0x05, 0x0c, // Usage Page (Consumer Devices)
  330. 0x0a, 0x38, 0x02, // Usage (AC Pan)
  331. 0x15, 0x81, // Logical Minimum (-127)
  332. 0x25, 0x7f, // Logical Maximum (127)
  333. 0x75, 0x08, // Report Size (8)
  334. 0x81, 0x06, // Input (Data,Var,Rel)
  335. 0xc0, // End Collection - Horizontal Wheel
  336. 0xc0, // End Collection - Buttons
  337. 0xc0, // End Collection - Mouse Logical
  338. 0xc0 // End Collection - Mouse Application
  339. };
  340. // Joystick Protocol, HID 1.11 spec, Apendix D, page 64-65
  341. static uint8_t joystick_report_desc[] = {
  342. 0x05, 0x01, // Usage Page (Generic Desktop)
  343. 0x09, 0x04, // Usage (Joystick)
  344. 0xA1, 0x01, // Collection (Application)
  345. 0x15, 0x00, // Logical Minimum (0)
  346. 0x25, 0x01, // Logical Maximum (1)
  347. 0x75, 0x01, // Report Size (1)
  348. 0x95, 0x20, // Report Count (32)
  349. 0x05, 0x09, // Usage Page (Button)
  350. 0x19, 0x01, // Usage Minimum (Button #1)
  351. 0x29, 0x20, // Usage Maximum (Button #32)
  352. 0x81, 0x02, // Input (variable,absolute)
  353. 0x15, 0x00, // Logical Minimum (0)
  354. 0x25, 0x07, // Logical Maximum (7)
  355. 0x35, 0x00, // Physical Minimum (0)
  356. 0x46, 0x3B, 0x01, // Physical Maximum (315)
  357. 0x75, 0x04, // Report Size (4)
  358. 0x95, 0x01, // Report Count (1)
  359. 0x65, 0x14, // Unit (20)
  360. 0x05, 0x01, // Usage Page (Generic Desktop)
  361. 0x09, 0x39, // Usage (Hat switch)
  362. 0x81, 0x42, // Input (variable,absolute,null_state)
  363. 0x05, 0x01, // Usage Page (Generic Desktop)
  364. 0x09, 0x01, // Usage (Pointer)
  365. 0xA1, 0x00, // Collection ()
  366. 0x15, 0x00, // Logical Minimum (0)
  367. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  368. 0x75, 0x0A, // Report Size (10)
  369. 0x95, 0x04, // Report Count (4)
  370. 0x09, 0x30, // Usage (X)
  371. 0x09, 0x31, // Usage (Y)
  372. 0x09, 0x32, // Usage (Z)
  373. 0x09, 0x35, // Usage (Rz)
  374. 0x81, 0x02, // Input (variable,absolute)
  375. 0xC0, // End Collection
  376. 0x15, 0x00, // Logical Minimum (0)
  377. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  378. 0x75, 0x0A, // Report Size (10)
  379. 0x95, 0x02, // Report Count (2)
  380. 0x09, 0x36, // Usage (Slider)
  381. 0x09, 0x36, // Usage (Slider)
  382. 0x81, 0x02, // Input (variable,absolute)
  383. 0xC0 // End Collection
  384. };
  385. // ----- USB Configuration -----
  386. // USB Configuration Descriptor. This huge descriptor tells all
  387. // of the devices capbilities.
  388. static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
  389. // --- Configuration ---
  390. // - 9 bytes -
  391. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  392. 9, // bLength;
  393. 2, // bDescriptorType;
  394. LSB(CONFIG_DESC_SIZE), // wTotalLength
  395. MSB(CONFIG_DESC_SIZE),
  396. NUM_INTERFACE, // bNumInterfaces
  397. 1, // bConfigurationValue
  398. 0, // iConfiguration
  399. 0xA0, // bmAttributes
  400. 250, // bMaxPower - Entry Index 8
  401. // --- Keyboard HID --- Boot Mode Keyboard Interface
  402. // - 9 bytes -
  403. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  404. 9, // bLength
  405. 4, // bDescriptorType
  406. KEYBOARD_INTERFACE, // bInterfaceNumber
  407. 0, // bAlternateSetting
  408. 1, // bNumEndpoints
  409. 0x03, // bInterfaceClass (0x03 = HID)
  410. 0x01, // bInterfaceSubClass (0x00 = Non-Boot, 0x01 = Boot)
  411. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  412. KEYBOARD_INTERFACE + 4, // iInterface
  413. // - 9 bytes -
  414. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  415. 9, // bLength
  416. 0x21, // bDescriptorType
  417. 0x11, 0x01, // bcdHID
  418. KeyboardLocale_define, // bCountryCode
  419. 1, // bNumDescriptors
  420. 0x22, // bDescriptorType
  421. LSB(sizeof(keyboard_report_desc)), // wDescriptorLength
  422. MSB(sizeof(keyboard_report_desc)),
  423. // - 7 bytes -
  424. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  425. 7, // bLength
  426. 5, // bDescriptorType
  427. KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  428. 0x03, // bmAttributes (0x03=intr)
  429. KEYBOARD_SIZE, 0, // wMaxPacketSize
  430. KEYBOARD_INTERVAL, // bInterval
  431. // --- NKRO Keyboard HID --- OS Mode Keyboard Interface
  432. // - 9 bytes -
  433. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  434. 9, // bLength
  435. 4, // bDescriptorType
  436. NKRO_KEYBOARD_INTERFACE, // bInterfaceNumber
  437. 0, // bAlternateSetting
  438. 1, // bNumEndpoints
  439. 0x03, // bInterfaceClass (0x03 = HID)
  440. 0x00, // bInterfaceSubClass (0x00 = Non-Boot, 0x01 = Boot)
  441. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  442. NKRO_KEYBOARD_INTERFACE + 4, // iInterface
  443. // - 9 bytes -
  444. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  445. 9, // bLength
  446. 0x21, // bDescriptorType
  447. 0x11, 0x01, // bcdHID
  448. KeyboardLocale_define, // bCountryCode
  449. 1, // bNumDescriptors
  450. 0x22, // bDescriptorType
  451. LSB(sizeof(nkro_keyboard_report_desc)), // wDescriptorLength
  452. MSB(sizeof(nkro_keyboard_report_desc)),
  453. // - 7 bytes -
  454. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  455. 7, // bLength
  456. 5, // bDescriptorType
  457. NKRO_KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  458. 0x03, // bmAttributes (0x03=intr)
  459. NKRO_KEYBOARD_SIZE, 0, // wMaxPacketSize
  460. NKRO_KEYBOARD_INTERVAL, // bInterval
  461. // --- Serial CDC --- CDC IAD Descriptor
  462. // - 8 bytes -
  463. // interface association descriptor, USB ECN, Table 9-Z
  464. 8, // bLength
  465. 11, // bDescriptorType
  466. CDC_STATUS_INTERFACE, // bFirstInterface
  467. 2, // bInterfaceCount
  468. 0x02, // bFunctionClass
  469. 0x02, // bFunctionSubClass
  470. 0x01, // bFunctionProtocol
  471. CDC_STATUS_INTERFACE + 4, // iFunction
  472. // --- Serial CDC --- CDC Data Interface
  473. // - 9 bytes -
  474. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  475. 9, // bLength
  476. 4, // bDescriptorType
  477. CDC_STATUS_INTERFACE, // bInterfaceNumber
  478. 0, // bAlternateSetting
  479. 1, // bNumEndpoints
  480. 0x02, // bInterfaceClass
  481. 0x02, // bInterfaceSubClass
  482. 0x01, // bInterfaceProtocol
  483. CDC_STATUS_INTERFACE + 4, // iInterface
  484. // - 5 bytes -
  485. // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
  486. 5, // bFunctionLength
  487. 0x24, // bDescriptorType
  488. 0x00, // bDescriptorSubtype
  489. 0x10, 0x01, // bcdCDC
  490. // - 5 bytes -
  491. // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
  492. 5, // bFunctionLength
  493. 0x24, // bDescriptorType
  494. 0x01, // bDescriptorSubtype
  495. 0x01, // bmCapabilities
  496. CDC_DATA_INTERFACE, // bDataInterface
  497. // - 4 bytes -
  498. // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
  499. 4, // bFunctionLength
  500. 0x24, // bDescriptorType
  501. 0x02, // bDescriptorSubtype
  502. 0x06, // bmCapabilities
  503. // - 5 bytes -
  504. // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
  505. 5, // bFunctionLength
  506. 0x24, // bDescriptorType
  507. 0x06, // bDescriptorSubtype
  508. CDC_STATUS_INTERFACE, // bMasterInterface
  509. CDC_DATA_INTERFACE, // bSlaveInterface0
  510. // - 7 bytes -
  511. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  512. 7, // bLength
  513. 5, // bDescriptorType
  514. CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
  515. 0x03, // bmAttributes (0x03=intr)
  516. CDC_ACM_SIZE, 0, // wMaxPacketSize
  517. 64, // bInterval
  518. // - 9 bytes -
  519. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  520. 9, // bLength
  521. 4, // bDescriptorType
  522. CDC_DATA_INTERFACE, // bInterfaceNumber
  523. 0, // bAlternateSetting
  524. 2, // bNumEndpoints
  525. 0x0A, // bInterfaceClass
  526. 0x00, // bInterfaceSubClass
  527. 0x00, // bInterfaceProtocol
  528. CDC_DATA_INTERFACE + 4, // iInterface
  529. // - 7 bytes -
  530. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  531. 7, // bLength
  532. 5, // bDescriptorType
  533. CDC_RX_ENDPOINT, // bEndpointAddress
  534. 0x02, // bmAttributes (0x02=bulk)
  535. CDC_RX_SIZE, 0, // wMaxPacketSize
  536. 0, // bInterval
  537. // - 7 bytes -
  538. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  539. 7, // bLength
  540. 5, // bDescriptorType
  541. CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
  542. 0x02, // bmAttributes (0x02=bulk)
  543. CDC_TX_SIZE, 0, // wMaxPacketSize
  544. 0, // bInterval
  545. // --- Mouse Interface ---
  546. // - 9 bytes -
  547. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  548. 9, // bLength
  549. 4, // bDescriptorType
  550. MOUSE_INTERFACE, // bInterfaceNumber
  551. 0, // bAlternateSetting
  552. 1, // bNumEndpoints
  553. 0x03, // bInterfaceClass (0x03 = HID)
  554. 0x00, // bInterfaceSubClass (0x01 = Boot)
  555. 0x02, // bInterfaceProtocol (0x02 = Mouse)
  556. MOUSE_INTERFACE + 4, // iInterface
  557. // - 9 bytes -
  558. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  559. 9, // bLength
  560. 0x21, // bDescriptorType
  561. 0x11, 0x01, // bcdHID
  562. 0, // bCountryCode
  563. 1, // bNumDescriptors
  564. 0x22, // bDescriptorType
  565. LSB(sizeof(mouse_report_desc)), // wDescriptorLength
  566. MSB(sizeof(mouse_report_desc)),
  567. // - 7 bytes -
  568. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  569. 7, // bLength
  570. 5, // bDescriptorType
  571. MOUSE_ENDPOINT | 0x80, // bEndpointAddress
  572. 0x03, // bmAttributes (0x03=intr)
  573. MOUSE_SIZE, 0, // wMaxPacketSize
  574. MOUSE_INTERVAL, // bInterval
  575. // --- Joystick Interface ---
  576. // - 9 bytes -
  577. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  578. 9, // bLength
  579. 4, // bDescriptorType
  580. JOYSTICK_INTERFACE, // bInterfaceNumber
  581. 0, // bAlternateSetting
  582. 1, // bNumEndpoints
  583. 0x03, // bInterfaceClass (0x03 = HID)
  584. 0x00, // bInterfaceSubClass
  585. 0x00, // bInterfaceProtocol
  586. JOYSTICK_INTERFACE + 4, // iInterface
  587. // - 9 bytes -
  588. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  589. 9, // bLength
  590. 0x21, // bDescriptorType
  591. 0x11, 0x01, // bcdHID
  592. 0, // bCountryCode
  593. 1, // bNumDescriptors
  594. 0x22, // bDescriptorType
  595. LSB(sizeof(joystick_report_desc)), // wDescriptorLength
  596. MSB(sizeof(joystick_report_desc)),
  597. // - 7 bytes -
  598. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  599. 7, // bLength
  600. 5, // bDescriptorType
  601. JOYSTICK_ENDPOINT | 0x80, // bEndpointAddress
  602. 0x03, // bmAttributes (0x03=intr)
  603. JOYSTICK_SIZE, 0, // wMaxPacketSize
  604. JOYSTICK_INTERVAL, // bInterval
  605. // --- System/Consumer Control ---
  606. // - 9 bytes -
  607. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  608. 9, // bLength
  609. 4, // bDescriptorType
  610. SYS_CTRL_INTERFACE, // bInterfaceNumber
  611. 0, // bAlternateSetting
  612. 1, // bNumEndpoints
  613. 0x03, // bInterfaceClass (0x03 = HID)
  614. 0x01, // bInterfaceSubClass (0x00 = Non-Boot, 0x01 = Boot)
  615. 0x00, // bInterfaceProtocol (0x00 = None)
  616. SYS_CTRL_INTERFACE + 4, // iInterface
  617. // - 9 bytes -
  618. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  619. 9, // bLength
  620. 0x21, // bDescriptorType
  621. 0x11, 0x01, // bcdHID
  622. KeyboardLocale_define, // bCountryCode
  623. 1, // bNumDescriptors
  624. 0x22, // bDescriptorType
  625. LSB(sizeof(sys_ctrl_report_desc)), // wDescriptorLength
  626. MSB(sizeof(sys_ctrl_report_desc)),
  627. // - 7 bytes -
  628. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  629. 7, // bLength
  630. 5, // bDescriptorType
  631. SYS_CTRL_ENDPOINT | 0x80, // bEndpointAddress
  632. 0x03, // bmAttributes (0x03=intr)
  633. SYS_CTRL_SIZE, 0, // wMaxPacketSize
  634. SYS_CTRL_INTERVAL, // bInterval
  635. };
  636. uint8_t *usb_bMaxPower = &config_descriptor[8];
  637. // ----- String Descriptors -----
  638. // The descriptors above can provide human readable strings,
  639. // referenced by index numbers. These descriptors are the
  640. // actual string data
  641. struct usb_string_descriptor_struct {
  642. uint8_t bLength;
  643. uint8_t bDescriptorType;
  644. uint16_t wString[];
  645. };
  646. extern struct usb_string_descriptor_struct usb_string_manufacturer_name
  647. __attribute__ ((weak, alias("usb_string_manufacturer_name_default")));
  648. extern struct usb_string_descriptor_struct usb_string_product_name
  649. __attribute__ ((weak, alias("usb_string_product_name_default")));
  650. extern struct usb_string_descriptor_struct usb_string_serial_number
  651. __attribute__ ((weak, alias("usb_string_serial_number_default")));
  652. struct usb_string_descriptor_struct string0 = {
  653. 4,
  654. 3,
  655. {0x0409}
  656. };
  657. #define usb_string_descriptor(name, str) \
  658. struct usb_string_descriptor_struct name = { \
  659. sizeof(str), \
  660. 3, \
  661. {str} \
  662. }
  663. usb_string_descriptor( usb_string_manufacturer_name_default, STR_MANUFACTURER );
  664. usb_string_descriptor( usb_string_product_name_default, STR_PRODUCT );
  665. usb_string_descriptor( usb_string_serial_number_default, STR_SERIAL );
  666. usb_string_descriptor( usb_string_keyboard_name, KEYBOARD_NAME );
  667. usb_string_descriptor( usb_string_nkro_keyboard_name, NKRO_KEYBOARD_NAME );
  668. usb_string_descriptor( usb_string_cdc_status_name, CDC_STATUS_NAME );
  669. usb_string_descriptor( usb_string_cdc_data_name, CDC_DATA_NAME );
  670. usb_string_descriptor( usb_string_mouse_name, MOUSE_NAME );
  671. usb_string_descriptor( usb_string_joystick_name, JOYSTICK_NAME );
  672. usb_string_descriptor( usb_string_sys_ctrl_name, SYS_CTRL_NAME );
  673. // ----- Descriptors List -----
  674. // This table provides access to all the descriptor data above.
  675. const usb_descriptor_list_t usb_descriptor_list[] = {
  676. //wValue, wIndex, address, length
  677. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  678. {0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
  679. {0x0600, 0x0000, device_qualifier_descriptor, sizeof(device_qualifier_descriptor)},
  680. {0x0A00, 0x0000, usb_debug_descriptor, sizeof(usb_debug_descriptor)},
  681. {0x2200, KEYBOARD_INTERFACE, keyboard_report_desc, sizeof(keyboard_report_desc)},
  682. {0x2100, KEYBOARD_INTERFACE, config_descriptor + KEYBOARD_DESC_OFFSET, 9},
  683. {0x2200, NKRO_KEYBOARD_INTERFACE, nkro_keyboard_report_desc, sizeof(nkro_keyboard_report_desc)},
  684. {0x2100, NKRO_KEYBOARD_INTERFACE, config_descriptor + NKRO_KEYBOARD_DESC_OFFSET, 9},
  685. {0x2200, MOUSE_INTERFACE, mouse_report_desc, sizeof(mouse_report_desc)},
  686. {0x2100, MOUSE_INTERFACE, config_descriptor + MOUSE_DESC_OFFSET, 9},
  687. {0x2200, JOYSTICK_INTERFACE, joystick_report_desc, sizeof(joystick_report_desc)},
  688. {0x2100, JOYSTICK_INTERFACE, config_descriptor + JOYSTICK_DESC_OFFSET, 9},
  689. {0x2200, SYS_CTRL_INTERFACE, sys_ctrl_report_desc, sizeof(sys_ctrl_report_desc)},
  690. {0x2100, SYS_CTRL_INTERFACE, config_descriptor + SYS_CTRL_DESC_OFFSET, 9},
  691. #define iInterfaceString(num, var) \
  692. {0x0300 + 4 + num, 0x409, (const uint8_t *)&var, 0 }
  693. {0x0300, 0x0000, (const uint8_t *)&string0, 0},
  694. {0x0301, 0x0409, (const uint8_t *)&usb_string_manufacturer_name, 0},
  695. {0x0302, 0x0409, (const uint8_t *)&usb_string_product_name, 0},
  696. {0x0303, 0x0409, (const uint8_t *)&usb_string_serial_number, 0},
  697. iInterfaceString( KEYBOARD_INTERFACE, usb_string_keyboard_name ),
  698. iInterfaceString( NKRO_KEYBOARD_INTERFACE, usb_string_nkro_keyboard_name ),
  699. iInterfaceString( CDC_STATUS_INTERFACE, usb_string_cdc_status_name ),
  700. iInterfaceString( CDC_DATA_INTERFACE, usb_string_cdc_data_name ),
  701. iInterfaceString( MOUSE_INTERFACE, usb_string_mouse_name ),
  702. iInterfaceString( JOYSTICK_INTERFACE, usb_string_joystick_name ),
  703. iInterfaceString( SYS_CTRL_INTERFACE, usb_string_sys_ctrl_name ),
  704. {0, 0, NULL, 0}
  705. };
  706. // ----- Endpoint Configuration -----
  707. // See usb_desc.h for Endpoint configuration
  708. // 0x00 = not used
  709. // 0x19 = Recieve only
  710. // 0x15 = Transmit only
  711. // 0x1D = Transmit & Recieve
  712. //
  713. const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
  714. {
  715. #if (defined(ENDPOINT1_CONFIG) && NUM_ENDPOINTS >= 1)
  716. ENDPOINT1_CONFIG,
  717. #elif (NUM_ENDPOINTS >= 1)
  718. ENDPOINT_UNUSED,
  719. #endif
  720. #if (defined(ENDPOINT2_CONFIG) && NUM_ENDPOINTS >= 2)
  721. ENDPOINT2_CONFIG,
  722. #elif (NUM_ENDPOINTS >= 2)
  723. ENDPOINT_UNUSED,
  724. #endif
  725. #if (defined(ENDPOINT3_CONFIG) && NUM_ENDPOINTS >= 3)
  726. ENDPOINT3_CONFIG,
  727. #elif (NUM_ENDPOINTS >= 3)
  728. ENDPOINT_UNUSED,
  729. #endif
  730. #if (defined(ENDPOINT4_CONFIG) && NUM_ENDPOINTS >= 4)
  731. ENDPOINT4_CONFIG,
  732. #elif (NUM_ENDPOINTS >= 4)
  733. ENDPOINT_UNUSED,
  734. #endif
  735. #if (defined(ENDPOINT5_CONFIG) && NUM_ENDPOINTS >= 5)
  736. ENDPOINT5_CONFIG,
  737. #elif (NUM_ENDPOINTS >= 5)
  738. ENDPOINT_UNUSED,
  739. #endif
  740. #if (defined(ENDPOINT6_CONFIG) && NUM_ENDPOINTS >= 6)
  741. ENDPOINT6_CONFIG,
  742. #elif (NUM_ENDPOINTS >= 6)
  743. ENDPOINT_UNUSED,
  744. #endif
  745. #if (defined(ENDPOINT7_CONFIG) && NUM_ENDPOINTS >= 7)
  746. ENDPOINT7_CONFIG,
  747. #elif (NUM_ENDPOINTS >= 7)
  748. ENDPOINT_UNUSED,
  749. #endif
  750. #if (defined(ENDPOINT8_CONFIG) && NUM_ENDPOINTS >= 8)
  751. ENDPOINT8_CONFIG,
  752. #elif (NUM_ENDPOINTS >= 8)
  753. ENDPOINT_UNUSED,
  754. #endif
  755. #if (defined(ENDPOINT9_CONFIG) && NUM_ENDPOINTS >= 9)
  756. ENDPOINT9_CONFIG,
  757. #elif (NUM_ENDPOINTS >= 9)
  758. ENDPOINT_UNUSED,
  759. #endif
  760. #if (defined(ENDPOINT10_CONFIG) && NUM_ENDPOINTS >= 10)
  761. ENDPOINT10_CONFIG,
  762. #elif (NUM_ENDPOINTS >= 10)
  763. ENDPOINT_UNUSED,
  764. #endif
  765. #if (defined(ENDPOINT11_CONFIG) && NUM_ENDPOINTS >= 11)
  766. ENDPOINT11_CONFIG,
  767. #elif (NUM_ENDPOINTS >= 11)
  768. ENDPOINT_UNUSED,
  769. #endif
  770. #if (defined(ENDPOINT12_CONFIG) && NUM_ENDPOINTS >= 12)
  771. ENDPOINT12_CONFIG,
  772. #elif (NUM_ENDPOINTS >= 12)
  773. ENDPOINT_UNUSED,
  774. #endif
  775. #if (defined(ENDPOINT13_CONFIG) && NUM_ENDPOINTS >= 13)
  776. ENDPOINT13_CONFIG,
  777. #elif (NUM_ENDPOINTS >= 13)
  778. ENDPOINT_UNUSED,
  779. #endif
  780. #if (defined(ENDPOINT14_CONFIG) && NUM_ENDPOINTS >= 14)
  781. ENDPOINT14_CONFIG,
  782. #elif (NUM_ENDPOINTS >= 14)
  783. ENDPOINT_UNUSED,
  784. #endif
  785. #if (defined(ENDPOINT15_CONFIG) && NUM_ENDPOINTS >= 15)
  786. ENDPOINT15_CONFIG,
  787. #elif (NUM_ENDPOINTS >= 15)
  788. ENDPOINT_UNUSED,
  789. #endif
  790. };