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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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 (16 bits)
  278. 0xA1, 0x00, // Collection (Physical) - Buttons
  279. 0x05, 0x09, // Usage Page (Button)
  280. 0x19, 0x01, // Usage Minimum (Button 1)
  281. 0x29, 0x10, // Usage Maximum (Button 16)
  282. 0x15, 0x00, // Logical Minimum (0)
  283. 0x25, 0x01, // Logical Maximum (1)
  284. 0x75, 0x01, // Report Size (1)
  285. 0x95, 0x10, // Report Count (16)
  286. 0x81, 0x02, // Input (Data,Var,Abs)
  287. // Pointer (32 bits)
  288. 0x05, 0x01, // Usage PAGE (Generic Desktop)
  289. 0x09, 0x30, // Usage (X)
  290. 0x09, 0x31, // Usage (Y)
  291. 0x16, 0x01, 0x80, // Logical Minimum (-32 767)
  292. 0x26, 0xFF, 0x7F, // Logical Maximum (32 767)
  293. 0x75, 0x10, // Report Size (16)
  294. 0x95, 0x02, // Report Count (2)
  295. 0x81, 0x06, // Input (Data,Var,Rel)
  296. /*
  297. // Vertical Wheel
  298. // - Multiplier (2 bits)
  299. 0xa1, 0x02, // Collection (Logical)
  300. 0x09, 0x48, // Usage (Resolution Multiplier)
  301. 0x15, 0x00, // Logical Minimum (0)
  302. 0x25, 0x01, // Logical Maximum (1)
  303. 0x35, 0x01, // Physical Minimum (1)
  304. 0x45, 0x04, // Physical Maximum (4)
  305. 0x75, 0x02, // Report Size (2)
  306. 0x95, 0x01, // Report Count (1)
  307. 0xa4, // Push
  308. 0xb1, 0x02, // Feature (Data,Var,Abs)
  309. // - Device (8 bits)
  310. 0x09, 0x38, // Usage (Wheel)
  311. 0x15, 0x81, // Logical Minimum (-127)
  312. 0x25, 0x7f, // Logical Maximum (127)
  313. 0x35, 0x00, // Physical Minimum (0) - reset physical
  314. 0x45, 0x00, // Physical Maximum (0)
  315. 0x75, 0x08, // Report Size (8)
  316. 0x81, 0x06, // Input (Data,Var,Rel)
  317. 0xc0, // End Collection - Vertical Wheel
  318. // Horizontal Wheel
  319. // - Multiplier (2 bits)
  320. 0xa1, 0x02, // Collection (Logical)
  321. 0x09, 0x48, // Usage (Resolution Multiplier)
  322. 0xb4, // Pop
  323. 0xb1, 0x02, // Feature (Data,Var,Abs)
  324. // - Padding (4 bits)
  325. 0x35, 0x00, // Physical Minimum (0) - reset physical
  326. 0x45, 0x00, // Physical Maximum (0)
  327. 0x75, 0x04, // Report Size (4)
  328. 0xb1, 0x03, // Feature (Cnst,Var,Abs)
  329. // - Device (8 bits)
  330. 0x05, 0x0c, // Usage Page (Consumer Devices)
  331. 0x0a, 0x38, 0x02, // Usage (AC Pan)
  332. 0x15, 0x81, // Logical Minimum (-127)
  333. 0x25, 0x7f, // Logical Maximum (127)
  334. 0x75, 0x08, // Report Size (8)
  335. 0x81, 0x06, // Input (Data,Var,Rel)
  336. 0xc0, // End Collection - Horizontal Wheel
  337. */
  338. 0xc0, // End Collection - Buttons
  339. 0xc0, // End Collection - Mouse Logical
  340. 0xc0 // End Collection - Mouse Application
  341. };
  342. // Joystick Protocol, HID 1.11 spec, Apendix D, page 64-65
  343. static uint8_t joystick_report_desc[] = {
  344. 0x05, 0x01, // Usage Page (Generic Desktop)
  345. 0x09, 0x04, // Usage (Joystick)
  346. 0xA1, 0x01, // Collection (Application)
  347. 0x15, 0x00, // Logical Minimum (0)
  348. 0x25, 0x01, // Logical Maximum (1)
  349. 0x75, 0x01, // Report Size (1)
  350. 0x95, 0x20, // Report Count (32)
  351. 0x05, 0x09, // Usage Page (Button)
  352. 0x19, 0x01, // Usage Minimum (Button #1)
  353. 0x29, 0x20, // Usage Maximum (Button #32)
  354. 0x81, 0x02, // Input (variable,absolute)
  355. 0x15, 0x00, // Logical Minimum (0)
  356. 0x25, 0x07, // Logical Maximum (7)
  357. 0x35, 0x00, // Physical Minimum (0)
  358. 0x46, 0x3B, 0x01, // Physical Maximum (315)
  359. 0x75, 0x04, // Report Size (4)
  360. 0x95, 0x01, // Report Count (1)
  361. 0x65, 0x14, // Unit (20)
  362. 0x05, 0x01, // Usage Page (Generic Desktop)
  363. 0x09, 0x39, // Usage (Hat switch)
  364. 0x81, 0x42, // Input (variable,absolute,null_state)
  365. 0x05, 0x01, // Usage Page (Generic Desktop)
  366. 0x09, 0x01, // Usage (Pointer)
  367. 0xA1, 0x00, // Collection ()
  368. 0x15, 0x00, // Logical Minimum (0)
  369. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  370. 0x75, 0x0A, // Report Size (10)
  371. 0x95, 0x04, // Report Count (4)
  372. 0x09, 0x30, // Usage (X)
  373. 0x09, 0x31, // Usage (Y)
  374. 0x09, 0x32, // Usage (Z)
  375. 0x09, 0x35, // Usage (Rz)
  376. 0x81, 0x02, // Input (variable,absolute)
  377. 0xC0, // End Collection
  378. 0x15, 0x00, // Logical Minimum (0)
  379. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  380. 0x75, 0x0A, // Report Size (10)
  381. 0x95, 0x02, // Report Count (2)
  382. 0x09, 0x36, // Usage (Slider)
  383. 0x09, 0x36, // Usage (Slider)
  384. 0x81, 0x02, // Input (variable,absolute)
  385. 0xC0 // End Collection
  386. };
  387. // ----- USB Configuration -----
  388. // USB Configuration Descriptor. This huge descriptor tells all
  389. // of the devices capbilities.
  390. static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
  391. // --- Configuration ---
  392. // - 9 bytes -
  393. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  394. 9, // bLength;
  395. 2, // bDescriptorType;
  396. LSB(CONFIG_DESC_SIZE), // wTotalLength
  397. MSB(CONFIG_DESC_SIZE),
  398. NUM_INTERFACE, // bNumInterfaces
  399. 1, // bConfigurationValue
  400. 0, // iConfiguration
  401. 0xA0, // bmAttributes
  402. 250, // bMaxPower - Entry Index 8
  403. // --- Keyboard HID --- Boot Mode Keyboard Interface
  404. // - 9 bytes -
  405. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  406. 9, // bLength
  407. 4, // bDescriptorType
  408. KEYBOARD_INTERFACE, // bInterfaceNumber
  409. 0, // bAlternateSetting
  410. 1, // bNumEndpoints
  411. 0x03, // bInterfaceClass (0x03 = HID)
  412. 0x01, // bInterfaceSubClass (0x00 = Non-Boot, 0x01 = Boot)
  413. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  414. KEYBOARD_INTERFACE + 4, // iInterface
  415. // - 9 bytes -
  416. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  417. 9, // bLength
  418. 0x21, // bDescriptorType
  419. 0x11, 0x01, // bcdHID
  420. KeyboardLocale_define, // bCountryCode
  421. 1, // bNumDescriptors
  422. 0x22, // bDescriptorType
  423. LSB(sizeof(keyboard_report_desc)), // wDescriptorLength
  424. MSB(sizeof(keyboard_report_desc)),
  425. // - 7 bytes -
  426. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  427. 7, // bLength
  428. 5, // bDescriptorType
  429. KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  430. 0x03, // bmAttributes (0x03=intr)
  431. KEYBOARD_SIZE, 0, // wMaxPacketSize
  432. KEYBOARD_INTERVAL, // bInterval
  433. // --- NKRO Keyboard HID --- OS Mode Keyboard Interface
  434. // - 9 bytes -
  435. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  436. 9, // bLength
  437. 4, // bDescriptorType
  438. NKRO_KEYBOARD_INTERFACE, // bInterfaceNumber
  439. 0, // bAlternateSetting
  440. 1, // bNumEndpoints
  441. 0x03, // bInterfaceClass (0x03 = HID)
  442. 0x00, // bInterfaceSubClass (0x00 = Non-Boot, 0x01 = Boot)
  443. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  444. NKRO_KEYBOARD_INTERFACE + 4, // iInterface
  445. // - 9 bytes -
  446. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  447. 9, // bLength
  448. 0x21, // bDescriptorType
  449. 0x11, 0x01, // bcdHID
  450. KeyboardLocale_define, // bCountryCode
  451. 1, // bNumDescriptors
  452. 0x22, // bDescriptorType
  453. LSB(sizeof(nkro_keyboard_report_desc)), // wDescriptorLength
  454. MSB(sizeof(nkro_keyboard_report_desc)),
  455. // - 7 bytes -
  456. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  457. 7, // bLength
  458. 5, // bDescriptorType
  459. NKRO_KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  460. 0x03, // bmAttributes (0x03=intr)
  461. NKRO_KEYBOARD_SIZE, 0, // wMaxPacketSize
  462. NKRO_KEYBOARD_INTERVAL, // bInterval
  463. // --- Serial CDC --- CDC IAD Descriptor
  464. // - 8 bytes -
  465. // interface association descriptor, USB ECN, Table 9-Z
  466. 8, // bLength
  467. 11, // bDescriptorType
  468. CDC_STATUS_INTERFACE, // bFirstInterface
  469. 2, // bInterfaceCount
  470. 0x02, // bFunctionClass
  471. 0x02, // bFunctionSubClass
  472. 0x01, // bFunctionProtocol
  473. CDC_STATUS_INTERFACE + 4, // iFunction
  474. // --- Serial CDC --- CDC Data Interface
  475. // - 9 bytes -
  476. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  477. 9, // bLength
  478. 4, // bDescriptorType
  479. CDC_STATUS_INTERFACE, // bInterfaceNumber
  480. 0, // bAlternateSetting
  481. 1, // bNumEndpoints
  482. 0x02, // bInterfaceClass
  483. 0x02, // bInterfaceSubClass
  484. 0x01, // bInterfaceProtocol
  485. CDC_STATUS_INTERFACE + 4, // iInterface
  486. // - 5 bytes -
  487. // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
  488. 5, // bFunctionLength
  489. 0x24, // bDescriptorType
  490. 0x00, // bDescriptorSubtype
  491. 0x10, 0x01, // bcdCDC
  492. // - 5 bytes -
  493. // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
  494. 5, // bFunctionLength
  495. 0x24, // bDescriptorType
  496. 0x01, // bDescriptorSubtype
  497. 0x01, // bmCapabilities
  498. CDC_DATA_INTERFACE, // bDataInterface
  499. // - 4 bytes -
  500. // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
  501. 4, // bFunctionLength
  502. 0x24, // bDescriptorType
  503. 0x02, // bDescriptorSubtype
  504. 0x06, // bmCapabilities
  505. // - 5 bytes -
  506. // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
  507. 5, // bFunctionLength
  508. 0x24, // bDescriptorType
  509. 0x06, // bDescriptorSubtype
  510. CDC_STATUS_INTERFACE, // bMasterInterface
  511. CDC_DATA_INTERFACE, // bSlaveInterface0
  512. // - 7 bytes -
  513. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  514. 7, // bLength
  515. 5, // bDescriptorType
  516. CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
  517. 0x03, // bmAttributes (0x03=intr)
  518. CDC_ACM_SIZE, 0, // wMaxPacketSize
  519. 64, // bInterval
  520. // - 9 bytes -
  521. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  522. 9, // bLength
  523. 4, // bDescriptorType
  524. CDC_DATA_INTERFACE, // bInterfaceNumber
  525. 0, // bAlternateSetting
  526. 2, // bNumEndpoints
  527. 0x0A, // bInterfaceClass
  528. 0x00, // bInterfaceSubClass
  529. 0x00, // bInterfaceProtocol
  530. CDC_DATA_INTERFACE + 4, // iInterface
  531. // - 7 bytes -
  532. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  533. 7, // bLength
  534. 5, // bDescriptorType
  535. CDC_RX_ENDPOINT, // bEndpointAddress
  536. 0x02, // bmAttributes (0x02=bulk)
  537. CDC_RX_SIZE, 0, // wMaxPacketSize
  538. 0, // bInterval
  539. // - 7 bytes -
  540. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  541. 7, // bLength
  542. 5, // bDescriptorType
  543. CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
  544. 0x02, // bmAttributes (0x02=bulk)
  545. CDC_TX_SIZE, 0, // wMaxPacketSize
  546. 0, // bInterval
  547. // --- Mouse Interface ---
  548. // - 9 bytes -
  549. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  550. 9, // bLength
  551. 4, // bDescriptorType
  552. MOUSE_INTERFACE, // bInterfaceNumber
  553. 0, // bAlternateSetting
  554. 1, // bNumEndpoints
  555. 0x03, // bInterfaceClass (0x03 = HID)
  556. 0x00, // bInterfaceSubClass (0x01 = Boot)
  557. 0x02, // bInterfaceProtocol (0x02 = Mouse)
  558. MOUSE_INTERFACE + 4, // iInterface
  559. // - 9 bytes -
  560. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  561. 9, // bLength
  562. 0x21, // bDescriptorType
  563. 0x11, 0x01, // bcdHID
  564. 0, // bCountryCode
  565. 1, // bNumDescriptors
  566. 0x22, // bDescriptorType
  567. LSB(sizeof(mouse_report_desc)), // wDescriptorLength
  568. MSB(sizeof(mouse_report_desc)),
  569. // - 7 bytes -
  570. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  571. 7, // bLength
  572. 5, // bDescriptorType
  573. MOUSE_ENDPOINT | 0x80, // bEndpointAddress
  574. 0x03, // bmAttributes (0x03=intr)
  575. MOUSE_SIZE, 0, // wMaxPacketSize
  576. MOUSE_INTERVAL, // bInterval
  577. // --- Joystick Interface ---
  578. // - 9 bytes -
  579. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  580. 9, // bLength
  581. 4, // bDescriptorType
  582. JOYSTICK_INTERFACE, // bInterfaceNumber
  583. 0, // bAlternateSetting
  584. 1, // bNumEndpoints
  585. 0x03, // bInterfaceClass (0x03 = HID)
  586. 0x00, // bInterfaceSubClass
  587. 0x00, // bInterfaceProtocol
  588. JOYSTICK_INTERFACE + 4, // iInterface
  589. // - 9 bytes -
  590. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  591. 9, // bLength
  592. 0x21, // bDescriptorType
  593. 0x11, 0x01, // bcdHID
  594. 0, // bCountryCode
  595. 1, // bNumDescriptors
  596. 0x22, // bDescriptorType
  597. LSB(sizeof(joystick_report_desc)), // wDescriptorLength
  598. MSB(sizeof(joystick_report_desc)),
  599. // - 7 bytes -
  600. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  601. 7, // bLength
  602. 5, // bDescriptorType
  603. JOYSTICK_ENDPOINT | 0x80, // bEndpointAddress
  604. 0x03, // bmAttributes (0x03=intr)
  605. JOYSTICK_SIZE, 0, // wMaxPacketSize
  606. JOYSTICK_INTERVAL, // bInterval
  607. // --- System/Consumer Control ---
  608. // - 9 bytes -
  609. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  610. 9, // bLength
  611. 4, // bDescriptorType
  612. SYS_CTRL_INTERFACE, // bInterfaceNumber
  613. 0, // bAlternateSetting
  614. 1, // bNumEndpoints
  615. 0x03, // bInterfaceClass (0x03 = HID)
  616. 0x01, // bInterfaceSubClass (0x00 = Non-Boot, 0x01 = Boot)
  617. 0x00, // bInterfaceProtocol (0x00 = None)
  618. SYS_CTRL_INTERFACE + 4, // iInterface
  619. // - 9 bytes -
  620. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  621. 9, // bLength
  622. 0x21, // bDescriptorType
  623. 0x11, 0x01, // bcdHID
  624. KeyboardLocale_define, // bCountryCode
  625. 1, // bNumDescriptors
  626. 0x22, // bDescriptorType
  627. LSB(sizeof(sys_ctrl_report_desc)), // wDescriptorLength
  628. MSB(sizeof(sys_ctrl_report_desc)),
  629. // - 7 bytes -
  630. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  631. 7, // bLength
  632. 5, // bDescriptorType
  633. SYS_CTRL_ENDPOINT | 0x80, // bEndpointAddress
  634. 0x03, // bmAttributes (0x03=intr)
  635. SYS_CTRL_SIZE, 0, // wMaxPacketSize
  636. SYS_CTRL_INTERVAL, // bInterval
  637. };
  638. uint8_t *usb_bMaxPower = &config_descriptor[8];
  639. // ----- String Descriptors -----
  640. // The descriptors above can provide human readable strings,
  641. // referenced by index numbers. These descriptors are the
  642. // actual string data
  643. struct usb_string_descriptor_struct {
  644. uint8_t bLength;
  645. uint8_t bDescriptorType;
  646. uint16_t wString[];
  647. };
  648. extern struct usb_string_descriptor_struct usb_string_manufacturer_name
  649. __attribute__ ((weak, alias("usb_string_manufacturer_name_default")));
  650. extern struct usb_string_descriptor_struct usb_string_product_name
  651. __attribute__ ((weak, alias("usb_string_product_name_default")));
  652. extern struct usb_string_descriptor_struct usb_string_serial_number
  653. __attribute__ ((weak, alias("usb_string_serial_number_default")));
  654. struct usb_string_descriptor_struct string0 = {
  655. 4,
  656. 3,
  657. {0x0409}
  658. };
  659. #define usb_string_descriptor(name, str) \
  660. struct usb_string_descriptor_struct name = { \
  661. sizeof(str), \
  662. 3, \
  663. {str} \
  664. }
  665. usb_string_descriptor( usb_string_manufacturer_name_default, STR_MANUFACTURER );
  666. usb_string_descriptor( usb_string_product_name_default, STR_PRODUCT );
  667. usb_string_descriptor( usb_string_serial_number_default, STR_SERIAL );
  668. usb_string_descriptor( usb_string_keyboard_name, KEYBOARD_NAME );
  669. usb_string_descriptor( usb_string_nkro_keyboard_name, NKRO_KEYBOARD_NAME );
  670. usb_string_descriptor( usb_string_cdc_status_name, CDC_STATUS_NAME );
  671. usb_string_descriptor( usb_string_cdc_data_name, CDC_DATA_NAME );
  672. usb_string_descriptor( usb_string_mouse_name, MOUSE_NAME );
  673. usb_string_descriptor( usb_string_joystick_name, JOYSTICK_NAME );
  674. usb_string_descriptor( usb_string_sys_ctrl_name, SYS_CTRL_NAME );
  675. // ----- Descriptors List -----
  676. // This table provides access to all the descriptor data above.
  677. const usb_descriptor_list_t usb_descriptor_list[] = {
  678. //wValue, wIndex, address, length
  679. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  680. {0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
  681. {0x0600, 0x0000, device_qualifier_descriptor, sizeof(device_qualifier_descriptor)},
  682. {0x0A00, 0x0000, usb_debug_descriptor, sizeof(usb_debug_descriptor)},
  683. {0x2200, KEYBOARD_INTERFACE, keyboard_report_desc, sizeof(keyboard_report_desc)},
  684. {0x2100, KEYBOARD_INTERFACE, config_descriptor + KEYBOARD_DESC_OFFSET, 9},
  685. {0x2200, NKRO_KEYBOARD_INTERFACE, nkro_keyboard_report_desc, sizeof(nkro_keyboard_report_desc)},
  686. {0x2100, NKRO_KEYBOARD_INTERFACE, config_descriptor + NKRO_KEYBOARD_DESC_OFFSET, 9},
  687. {0x2200, MOUSE_INTERFACE, mouse_report_desc, sizeof(mouse_report_desc)},
  688. {0x2100, MOUSE_INTERFACE, config_descriptor + MOUSE_DESC_OFFSET, 9},
  689. {0x2200, JOYSTICK_INTERFACE, joystick_report_desc, sizeof(joystick_report_desc)},
  690. {0x2100, JOYSTICK_INTERFACE, config_descriptor + JOYSTICK_DESC_OFFSET, 9},
  691. {0x2200, SYS_CTRL_INTERFACE, sys_ctrl_report_desc, sizeof(sys_ctrl_report_desc)},
  692. {0x2100, SYS_CTRL_INTERFACE, config_descriptor + SYS_CTRL_DESC_OFFSET, 9},
  693. #define iInterfaceString(num, var) \
  694. {0x0300 + 4 + num, 0x409, (const uint8_t *)&var, 0 }
  695. {0x0300, 0x0000, (const uint8_t *)&string0, 0},
  696. {0x0301, 0x0409, (const uint8_t *)&usb_string_manufacturer_name, 0},
  697. {0x0302, 0x0409, (const uint8_t *)&usb_string_product_name, 0},
  698. {0x0303, 0x0409, (const uint8_t *)&usb_string_serial_number, 0},
  699. iInterfaceString( KEYBOARD_INTERFACE, usb_string_keyboard_name ),
  700. iInterfaceString( NKRO_KEYBOARD_INTERFACE, usb_string_nkro_keyboard_name ),
  701. iInterfaceString( CDC_STATUS_INTERFACE, usb_string_cdc_status_name ),
  702. iInterfaceString( CDC_DATA_INTERFACE, usb_string_cdc_data_name ),
  703. iInterfaceString( MOUSE_INTERFACE, usb_string_mouse_name ),
  704. iInterfaceString( JOYSTICK_INTERFACE, usb_string_joystick_name ),
  705. iInterfaceString( SYS_CTRL_INTERFACE, usb_string_sys_ctrl_name ),
  706. {0, 0, NULL, 0}
  707. };
  708. // ----- Endpoint Configuration -----
  709. // See usb_desc.h for Endpoint configuration
  710. // 0x00 = not used
  711. // 0x19 = Recieve only
  712. // 0x15 = Transmit only
  713. // 0x1D = Transmit & Recieve
  714. //
  715. const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
  716. {
  717. #if (defined(ENDPOINT1_CONFIG) && NUM_ENDPOINTS >= 1)
  718. ENDPOINT1_CONFIG,
  719. #elif (NUM_ENDPOINTS >= 1)
  720. ENDPOINT_UNUSED,
  721. #endif
  722. #if (defined(ENDPOINT2_CONFIG) && NUM_ENDPOINTS >= 2)
  723. ENDPOINT2_CONFIG,
  724. #elif (NUM_ENDPOINTS >= 2)
  725. ENDPOINT_UNUSED,
  726. #endif
  727. #if (defined(ENDPOINT3_CONFIG) && NUM_ENDPOINTS >= 3)
  728. ENDPOINT3_CONFIG,
  729. #elif (NUM_ENDPOINTS >= 3)
  730. ENDPOINT_UNUSED,
  731. #endif
  732. #if (defined(ENDPOINT4_CONFIG) && NUM_ENDPOINTS >= 4)
  733. ENDPOINT4_CONFIG,
  734. #elif (NUM_ENDPOINTS >= 4)
  735. ENDPOINT_UNUSED,
  736. #endif
  737. #if (defined(ENDPOINT5_CONFIG) && NUM_ENDPOINTS >= 5)
  738. ENDPOINT5_CONFIG,
  739. #elif (NUM_ENDPOINTS >= 5)
  740. ENDPOINT_UNUSED,
  741. #endif
  742. #if (defined(ENDPOINT6_CONFIG) && NUM_ENDPOINTS >= 6)
  743. ENDPOINT6_CONFIG,
  744. #elif (NUM_ENDPOINTS >= 6)
  745. ENDPOINT_UNUSED,
  746. #endif
  747. #if (defined(ENDPOINT7_CONFIG) && NUM_ENDPOINTS >= 7)
  748. ENDPOINT7_CONFIG,
  749. #elif (NUM_ENDPOINTS >= 7)
  750. ENDPOINT_UNUSED,
  751. #endif
  752. #if (defined(ENDPOINT8_CONFIG) && NUM_ENDPOINTS >= 8)
  753. ENDPOINT8_CONFIG,
  754. #elif (NUM_ENDPOINTS >= 8)
  755. ENDPOINT_UNUSED,
  756. #endif
  757. #if (defined(ENDPOINT9_CONFIG) && NUM_ENDPOINTS >= 9)
  758. ENDPOINT9_CONFIG,
  759. #elif (NUM_ENDPOINTS >= 9)
  760. ENDPOINT_UNUSED,
  761. #endif
  762. #if (defined(ENDPOINT10_CONFIG) && NUM_ENDPOINTS >= 10)
  763. ENDPOINT10_CONFIG,
  764. #elif (NUM_ENDPOINTS >= 10)
  765. ENDPOINT_UNUSED,
  766. #endif
  767. #if (defined(ENDPOINT11_CONFIG) && NUM_ENDPOINTS >= 11)
  768. ENDPOINT11_CONFIG,
  769. #elif (NUM_ENDPOINTS >= 11)
  770. ENDPOINT_UNUSED,
  771. #endif
  772. #if (defined(ENDPOINT12_CONFIG) && NUM_ENDPOINTS >= 12)
  773. ENDPOINT12_CONFIG,
  774. #elif (NUM_ENDPOINTS >= 12)
  775. ENDPOINT_UNUSED,
  776. #endif
  777. #if (defined(ENDPOINT13_CONFIG) && NUM_ENDPOINTS >= 13)
  778. ENDPOINT13_CONFIG,
  779. #elif (NUM_ENDPOINTS >= 13)
  780. ENDPOINT_UNUSED,
  781. #endif
  782. #if (defined(ENDPOINT14_CONFIG) && NUM_ENDPOINTS >= 14)
  783. ENDPOINT14_CONFIG,
  784. #elif (NUM_ENDPOINTS >= 14)
  785. ENDPOINT_UNUSED,
  786. #endif
  787. #if (defined(ENDPOINT15_CONFIG) && NUM_ENDPOINTS >= 15)
  788. ENDPOINT15_CONFIG,
  789. #elif (NUM_ENDPOINTS >= 15)
  790. ENDPOINT_UNUSED,
  791. #endif
  792. };