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

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