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_keyboard_serial.h 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /* USB Keyboard and CDC Serial Device for Teensy USB Development Board
  2. * Copyright (c) 2009 PJRC.COM, LLC
  3. * Modifications by Jacob Alexander (2011-2015)
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #pragma once
  24. // ----- Includes -----
  25. // Compiler Includes
  26. #include <stdint.h>
  27. // AVR Includes
  28. #include <avr/io.h>
  29. #include <avr/pgmspace.h>
  30. #include <avr/interrupt.h>
  31. #include <avr/wdt.h>
  32. // AVR Util Includes
  33. #include <util/delay.h>
  34. // Local Includes
  35. #include "output_com.h"
  36. // ----- Function Declarations -----
  37. // Basic USB Configuration
  38. uint8_t usb_init(); // initialize everything
  39. uint8_t usb_configured(); // is the USB port configured
  40. // Keyboard HID Functions
  41. void usb_keyboard_send();
  42. // Chip Level Functions
  43. void usb_device_reload(); // Enable firmware reflash mode
  44. // USB Serial CDC Functions
  45. int16_t usb_serial_getchar(); // receive a character (-1 if timeout/error)
  46. uint8_t usb_serial_available(); // number of bytes in receive buffer
  47. void usb_serial_flush_input(); // discard any buffered input
  48. // transmitting data
  49. int8_t usb_serial_putchar(uint8_t c); // transmit a character
  50. int8_t usb_serial_putchar_nowait(uint8_t c); // transmit a character, do not wait
  51. int8_t usb_serial_write(const char *buffer, uint16_t size); // transmit a buffer
  52. void usb_serial_flush_output(); // immediately transmit any buffered output
  53. // serial parameters
  54. uint32_t usb_serial_get_baud(); // get the baud rate
  55. uint8_t usb_serial_get_stopbits(); // get the number of stop bits
  56. uint8_t usb_serial_get_paritytype(); // get the parity type
  57. uint8_t usb_serial_get_numbits(); // get the number of data bits
  58. uint8_t usb_serial_get_control(); // get the RTS and DTR signal state
  59. int8_t usb_serial_set_control(uint8_t signals); // set DSR, DCD, RI, etc
  60. // ----- Macros -----
  61. // Software reset the chip
  62. #define usb_device_software_reset() do { wdt_enable( WDTO_15MS ); for(;;); } while(0)
  63. // See EPSIZE -> UECFG1X - 128 and 256 bytes are for endpoint 1 only
  64. #define EP_SIZE(s) ((s) == 256 ? 0x50 : \
  65. ((s) == 128 ? 0x40 : \
  66. ((s) == 64 ? 0x30 : \
  67. ((s) == 32 ? 0x20 : \
  68. ((s) == 16 ? 0x10 : \
  69. 0x00)))))
  70. #define LSB(n) (n & 255)
  71. #define MSB(n) ((n >> 8) & 255)
  72. // ----- Defines -----
  73. // constants corresponding to the various serial parameters
  74. #define USB_SERIAL_DTR 0x01
  75. #define USB_SERIAL_RTS 0x02
  76. #define USB_SERIAL_1_STOP 0
  77. #define USB_SERIAL_1_5_STOP 1
  78. #define USB_SERIAL_2_STOP 2
  79. #define USB_SERIAL_PARITY_NONE 0
  80. #define USB_SERIAL_PARITY_ODD 1
  81. #define USB_SERIAL_PARITY_EVEN 2
  82. #define USB_SERIAL_PARITY_MARK 3
  83. #define USB_SERIAL_PARITY_SPACE 4
  84. #define USB_SERIAL_DCD 0x01
  85. #define USB_SERIAL_DSR 0x02
  86. #define USB_SERIAL_BREAK 0x04
  87. #define USB_SERIAL_RI 0x08
  88. #define USB_SERIAL_FRAME_ERR 0x10
  89. #define USB_SERIAL_PARITY_ERR 0x20
  90. #define USB_SERIAL_OVERRUN_ERR 0x40
  91. #define EP_TYPE_CONTROL 0x00
  92. #define EP_TYPE_BULK_IN 0x81
  93. #define EP_TYPE_BULK_OUT 0x80
  94. #define EP_TYPE_INTERRUPT_IN 0xC1
  95. #define EP_TYPE_INTERRUPT_OUT 0xC0
  96. #define EP_TYPE_ISOCHRONOUS_IN 0x41
  97. #define EP_TYPE_ISOCHRONOUS_OUT 0x40
  98. #define EP_SINGLE_BUFFER 0x02
  99. #define EP_DOUBLE_BUFFER 0x06
  100. #define MAX_ENDPOINT 4
  101. #if defined(__AVR_AT90USB162__)
  102. #define HW_CONFIG()
  103. #define PLL_CONFIG() (PLLCSR = ((1<<PLLE)|(1<<PLLP0)))
  104. #define USB_CONFIG() (USBCON = (1<<USBE))
  105. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  106. #elif defined(__AVR_ATmega32U4__)
  107. #define HW_CONFIG() (UHWCON = 0x01)
  108. #define PLL_CONFIG() (PLLCSR = 0x12)
  109. #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
  110. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  111. #elif defined(__AVR_AT90USB646__)
  112. #define HW_CONFIG() (UHWCON = 0x81)
  113. #define PLL_CONFIG() (PLLCSR = 0x1A)
  114. #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
  115. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  116. #elif defined(__AVR_AT90USB1286__)
  117. #define HW_CONFIG() (UHWCON = 0x81)
  118. #define PLL_CONFIG() (PLLCSR = 0x16)
  119. #define USB_CONFIG() (USBCON = ((1<<USBE)|(1<<OTGPADE)))
  120. #define USB_FREEZE() (USBCON = ((1<<USBE)|(1<<FRZCLK)))
  121. #endif
  122. // standard control endpoint request types
  123. #define GET_STATUS 0
  124. #define CLEAR_FEATURE 1
  125. #define SET_FEATURE 3
  126. #define SET_ADDRESS 5
  127. #define GET_DESCRIPTOR 6
  128. #define GET_CONFIGURATION 8
  129. #define SET_CONFIGURATION 9
  130. #define GET_INTERFACE 10
  131. #define SET_INTERFACE 11
  132. // HID (human interface device)
  133. #define HID_GET_REPORT 1
  134. #define HID_GET_IDLE 2
  135. #define HID_GET_PROTOCOL 3
  136. #define HID_SET_REPORT 9
  137. #define HID_SET_IDLE 10
  138. #define HID_SET_PROTOCOL 11
  139. // CDC (communication class device)
  140. #define CDC_SET_LINE_CODING 0x20
  141. #define CDC_GET_LINE_CODING 0x21
  142. #define CDC_SET_CONTROL_LINE_STATE 0x22
  143. // CDC Configuration
  144. // When you write data, it goes into a USB endpoint buffer, which
  145. // is transmitted to the PC when it becomes full, or after a timeout
  146. // with no more writes. Even if you write in exactly packet-size
  147. // increments, this timeout is used to send a "zero length packet"
  148. // that tells the PC no more data is expected and it should pass
  149. // any buffered data to the application that may be waiting. If
  150. // you want data sent immediately, call usb_serial_flush_output().
  151. #define TRANSMIT_FLUSH_TIMEOUT 5 /* in milliseconds */
  152. // If the PC is connected but not "listening", this is the length
  153. // of time before usb_serial_getchar() returns with an error. This
  154. // is roughly equivilant to a real UART simply transmitting the
  155. // bits on a wire where nobody is listening, except you get an error
  156. // code which you can ignore for serial-like discard of data, or
  157. // use to know your data wasn't sent.
  158. #define TRANSMIT_TIMEOUT 25 /* in milliseconds */
  159. // ----- Endpoint Configuration -----
  160. #define ENDPOINT0_SIZE 32
  161. #define KEYBOARD_NKRO_INTERFACE 0
  162. #define KEYBOARD_NKRO_ENDPOINT 1
  163. #define KEYBOARD_NKRO_SIZE 64
  164. #define KEYBOARD_NKRO_HID_BUFFER EP_DOUBLE_BUFFER
  165. #define KEYBOARD_INTERFACE 1
  166. #define KEYBOARD_ENDPOINT 2
  167. #define KEYBOARD_SIZE 8
  168. #define KEYBOARD_HID_BUFFER EP_DOUBLE_BUFFER
  169. #define CDC_IAD_DESCRIPTOR 1
  170. #define CDC_STATUS_INTERFACE 2
  171. #define CDC_DATA_INTERFACE 3
  172. #define CDC_ACM_ENDPOINT 3
  173. #define CDC_RX_ENDPOINT 4
  174. #define CDC_TX_ENDPOINT 5
  175. #if defined(__AVR_AT90USB162__)
  176. #define CDC_ACM_SIZE 16
  177. #define CDC_ACM_BUFFER EP_SINGLE_BUFFER
  178. #define CDC_RX_SIZE 32
  179. #define CDC_RX_BUFFER EP_DOUBLE_BUFFER
  180. #define CDC_TX_SIZE 32
  181. #define CDC_TX_BUFFER EP_DOUBLE_BUFFER
  182. #else
  183. #define CDC_ACM_SIZE 16
  184. #define CDC_ACM_BUFFER EP_SINGLE_BUFFER
  185. #define CDC_RX_SIZE 64
  186. #define CDC_RX_BUFFER EP_DOUBLE_BUFFER
  187. #define CDC_TX_SIZE 64
  188. #define CDC_TX_BUFFER EP_DOUBLE_BUFFER
  189. #endif
  190. // Endpoint 0 is reserved for the control endpoint
  191. // Endpoint 1 has a 256 byte buffer
  192. // Endpoints 2-6 have 64 byte buffers
  193. static const uint8_t PROGMEM endpoint_config_table[] = {
  194. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KEYBOARD_NKRO_SIZE) | KEYBOARD_NKRO_HID_BUFFER, // 256 byte
  195. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_HID_BUFFER, // 64 byte
  196. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(CDC_ACM_SIZE) | CDC_ACM_BUFFER, // 64 byte
  197. 1, EP_TYPE_BULK_OUT, EP_SIZE(CDC_RX_SIZE) | CDC_RX_BUFFER, // 64 byte
  198. 1, EP_TYPE_BULK_IN, EP_SIZE(CDC_TX_SIZE) | CDC_TX_BUFFER, // 64 byte
  199. 0, // 64 byte
  200. };
  201. // ----- Descriptor Configuration -----
  202. // Descriptors are the data that your computer reads when it auto-detects
  203. // this USB device (called "enumeration" in USB lingo). The most commonly
  204. // changed items are editable at the top of this file. Changing things
  205. // in here should only be done by those who've read chapter 9 of the USB
  206. // spec and relevant portions of any USB class specifications!
  207. static const uint8_t PROGMEM device_descriptor[] = {
  208. 18, // bLength
  209. 1, // bDescriptorType
  210. 0x00, 0x02, // bcdUSB
  211. 0x00, // bDeviceClass - Composite device, 0x00 is required for Windows
  212. 0, // bDeviceSubClass
  213. 0, // bDeviceProtocol
  214. ENDPOINT0_SIZE, // bMaxPacketSize0
  215. LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
  216. LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
  217. 0x00, 0x01, // bcdDevice
  218. 1, // iManufacturer
  219. 2, // iProduct
  220. 3, // iSerialNumber
  221. 1 // bNumConfigurations
  222. };
  223. // Specify only a single USB speed
  224. static const uint8_t PROGMEM device_qualifier_descriptor[] = {
  225. 0
  226. };
  227. // Disable USB debug descriptor
  228. static const uint8_t PROGMEM usb_debug_descriptor[] = {
  229. 0
  230. };
  231. // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
  232. static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
  233. // Keyboard Collection
  234. 0x05, 0x01, // Usage Page (Generic Desktop),
  235. 0x09, 0x06, // Usage (Keyboard),
  236. 0xA1, 0x01, // Collection (Application) - Keyboard,
  237. // Modifier Byte
  238. 0x75, 0x01, // Report Size (1),
  239. 0x95, 0x08, // Report Count (8),
  240. 0x05, 0x07, // Usage Page (Key Codes),
  241. 0x19, 0xE0, // Usage Minimum (224),
  242. 0x29, 0xE7, // Usage Maximum (231),
  243. 0x15, 0x00, // Logical Minimum (0),
  244. 0x25, 0x01, // Logical Maximum (1),
  245. 0x81, 0x02, // Input (Data, Variable, Absolute),
  246. // Reserved Byte
  247. 0x75, 0x08, // Report Size (8),
  248. 0x95, 0x01, // Report Count (1),
  249. 0x81, 0x03, // Output (Constant),
  250. // LED Report
  251. 0x75, 0x01, // Report Size (1),
  252. 0x95, 0x05, // Report Count (5),
  253. 0x05, 0x08, // Usage Page (LEDs),
  254. 0x19, 0x01, // Usage Minimum (1),
  255. 0x29, 0x05, // Usage Maximum (5),
  256. 0x91, 0x02, // Output (Data, Variable, Absolute),
  257. // LED Report Padding
  258. 0x75, 0x03, // Report Size (3),
  259. 0x95, 0x01, // Report Count (1),
  260. 0x91, 0x03, // Output (Constant),
  261. // Normal Keys
  262. 0x75, 0x08, // Report Size (8),
  263. 0x95, 0x06, // Report Count (6),
  264. 0x15, 0x00, // Logical Minimum (0),
  265. 0x25, 0x7F, // Logical Maximum(104),
  266. 0x05, 0x07, // Usage Page (Key Codes),
  267. 0x19, 0x00, // Usage Minimum (0),
  268. 0x29, 0x7F, // Usage Maximum (104),
  269. 0x81, 0x00, // Input (Data, Array),
  270. 0xc0, // End Collection - Keyboard
  271. };
  272. // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
  273. static const uint8_t PROGMEM keyboard_nkro_hid_report_desc[] = {
  274. // Keyboard Collection
  275. 0x05, 0x01, // Usage Page (Generic Desktop),
  276. 0x09, 0x06, // Usage (Keyboard),
  277. 0xA1, 0x01, // Collection (Application) - Keyboard,
  278. // LED Report
  279. 0x85, 0x01, // Report ID (1),
  280. 0x75, 0x01, // Report Size (1),
  281. 0x95, 0x05, // Report Count (5),
  282. 0x05, 0x08, // Usage Page (LEDs),
  283. 0x19, 0x01, // Usage Minimum (1),
  284. 0x29, 0x05, // Usage Maximum (5),
  285. 0x91, 0x02, // Output (Data, Variable, Absolute),
  286. // LED Report Padding
  287. 0x75, 0x03, // Report Size (3),
  288. 0x95, 0x01, // Report Count (1),
  289. 0x91, 0x03, // Output (Constant),
  290. // Normal Keys - Using an NKRO Bitmap
  291. //
  292. // NOTES:
  293. // Supports all keys defined by the spec, except 1-3 which define error events
  294. // and 0 which is "no keys pressed"
  295. // See http://www.usb.org/developers/hidpage/Hut1_12v2.pdf Chapter 10
  296. // Or Macros/PartialMap/usb_hid.h
  297. //
  298. // 50 (ISO \ due to \ bug) and 156 (Clear due to Delete bug) must be excluded
  299. // due to a Linux bug with bitmaps (not useful anyways)
  300. // 165-175 are reserved/unused as well as 222-223 and 232-65535
  301. //
  302. // Compatibility Notes:
  303. // - Using a second endpoint for a boot mode device helps with compatibility
  304. // - DO NOT use Padding in the descriptor for bitfields
  305. // (Mac OSX silently fails... Windows/Linux work correctly)
  306. // - DO NOT use Report IDs, Windows 8.1 will not update keyboard correctly (modifiers disappear)
  307. // (all other OSs, including OSX work fine...)
  308. // (you can use them *iff* you only have 1 per collection)
  309. // - Mac OSX and Windows 8.1 are extremely picky about padding
  310. //
  311. // Packing of bitmaps are as follows:
  312. // 4-49 : 6 bytes (0x04-0x31) ( 46 bits + 2 padding bits for 6 bytes total)
  313. // 51-155 : 14 bytes (0x33-0x9B) (105 bits + 6 padding bits for 15 bytes total)
  314. // 157-164 : 1 byte (0x9D-0xA4) ( 8 bits)
  315. // 176-221 : 6 bytes (0xB0-0xDD) ( 46 bits + 2 padding bits for 6 bytes total)
  316. // 224-231 : 1 byte (0xE0-0xE7) ( 8 bits)
  317. // Modifier Byte
  318. 0x75, 0x01, // Report Size (1),
  319. 0x95, 0x08, // Report Count (8),
  320. 0x15, 0x00, // Logical Minimum (0),
  321. 0x25, 0x01, // Logical Maximum (1),
  322. 0x05, 0x07, // Usage Page (Key Codes),
  323. 0x19, 0xE0, // Usage Minimum (224),
  324. 0x29, 0xE7, // Usage Maximum (231),
  325. 0x81, 0x02, // Input (Data, Variable, Absolute),
  326. // 4-49 (6 bytes/46 bits) - MainKeys
  327. 0x75, 0x01, // Report Size (1),
  328. 0x95, 0x2E, // Report Count (46),
  329. 0x15, 0x00, // Logical Minimum (0),
  330. 0x25, 0x01, // Logical Maximum (1),
  331. 0x05, 0x07, // Usage Page (Key Codes),
  332. 0x19, 0x04, // Usage Minimum (4),
  333. 0x29, 0x31, // Usage Maximum (49),
  334. 0x81, 0x02, // Input (Data, Variable, Absolute, Bitfield),
  335. // Padding (2 bits)
  336. 0x75, 0x02, // Report Size (2),
  337. 0x95, 0x01, // Report Count (1),
  338. 0x81, 0x03, // Input (Constant),
  339. // 51-155 (14 bytes/105 bits) - SecondaryKeys
  340. 0x75, 0x01, // Report Size (1),
  341. 0x95, 0x69, // Report Count (105),
  342. 0x15, 0x00, // Logical Minimum (0),
  343. 0x25, 0x01, // Logical Maximum (1),
  344. 0x05, 0x07, // Usage Page (Key Codes),
  345. 0x19, 0x33, // Usage Minimum (51),
  346. 0x29, 0x9B, // Usage Maximum (155),
  347. 0x81, 0x02, // Input (Data, Variable, Absolute, Bitfield),
  348. // Padding (7 bits)
  349. 0x75, 0x07, // Report Size (7),
  350. 0x95, 0x01, // Report Count (1),
  351. 0x81, 0x03, // Input (Constant),
  352. // 157-164 (1 byte/8 bits) - TertiaryKeys
  353. 0x75, 0x01, // Report Size (1),
  354. 0x95, 0x08, // Report Count (8),
  355. 0x15, 0x00, // Logical Minimum (0),
  356. 0x25, 0x01, // Logical Maximum (1),
  357. 0x05, 0x07, // Usage Page (Key Codes),
  358. 0x19, 0x9D, // Usage Minimum (157),
  359. 0x29, 0xA4, // Usage Maximum (164),
  360. 0x81, 0x02, // Input (Data, Variable, Absolute, Bitfield),
  361. // 176-221 (6 bytes/46 bits) - QuartiaryKeys
  362. 0x75, 0x01, // Report Size (1),
  363. 0x95, 0x2E, // Report Count (46),
  364. 0x15, 0x00, // Logical Minimum (0),
  365. 0x25, 0x01, // Logical Maximum (1),
  366. 0x05, 0x07, // Usage Page (Key Codes),
  367. 0x19, 0xB0, // Usage Minimum (176),
  368. 0x29, 0xDD, // Usage Maximum (221),
  369. 0x81, 0x02, // Input (Data, Variable, Absolute, Bitfield),
  370. // Padding (2 bits)
  371. 0x75, 0x02, // Report Size (2),
  372. 0x95, 0x01, // Report Count (1),
  373. 0x81, 0x03, // Input (Constant),
  374. 0xc0, // End Collection - Keyboard
  375. // System Control Collection
  376. //
  377. // NOTES:
  378. // Not bothering with NKRO for this table. If there's need, I can implement it. -HaaTa
  379. // Using a 1KRO scheme
  380. 0x05, 0x01, // Usage Page (Generic Desktop),
  381. 0x09, 0x80, // Usage (System Control),
  382. 0xA1, 0x01, // Collection (Application),
  383. 0x85, 0x02, // Report ID (2),
  384. 0x75, 0x08, // Report Size (8),
  385. 0x95, 0x01, // Report Count (1),
  386. 0x16, 0x81, 0x00, // Logical Minimum (129),
  387. 0x26, 0xB7, 0x00, // Logical Maximum (183),
  388. 0x19, 0x81, // Usage Minimum (129),
  389. 0x29, 0xB7, // Usage Maximum (183),
  390. 0x81, 0x00, // Input (Data, Array),
  391. 0xc0, // End Collection - System Control
  392. // Consumer Control Collection - Media Keys
  393. //
  394. // NOTES:
  395. // Not bothering with NKRO for this table. If there's a need, I can implement it. -HaaTa
  396. // Using a 1KRO scheme
  397. 0x05, 0x0c, // Usage Page (Consumer),
  398. 0x09, 0x01, // Usage (Consumer Control),
  399. 0xA1, 0x01, // Collection (Application),
  400. 0x85, 0x03, // Report ID (3),
  401. 0x75, 0x10, // Report Size (16),
  402. 0x95, 0x01, // Report Count (1),
  403. 0x16, 0x20, 0x00, // Logical Minimum (32),
  404. 0x26, 0x9C, 0x02, // Logical Maximum (668),
  405. 0x05, 0x0C, // Usage Page (Consumer),
  406. 0x19, 0x20, // Usage Minimum (32),
  407. 0x2A, 0x9C, 0x02, // Usage Maximum (668),
  408. 0x81, 0x00, // Input (Data, Array),
  409. 0xc0, // End Collection - Consumer Control
  410. };
  411. // <Configuration> + <Keyboard HID> + <NKRO Keyboard HID> + <Serial CDC>
  412. #define CONFIG1_DESC_SIZE (9 + 9+9+7 + 9+9+7 + 8+9+5+5+4+5+7+9+7+7)
  413. #define KEYBOARD_HID_DESC_OFFSET (9 + 9)
  414. #define KEYBOARD_NKRO_HID_DESC_OFFSET (9 + 9+9+7 + 9)
  415. #define SERIAL_CDC_DESC_OFFSET (9 + 9+9+7 + 9+9+7)
  416. static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
  417. // --- Configuration ---
  418. // - 9 bytes -
  419. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  420. 9, // bLength;
  421. 2, // bDescriptorType;
  422. LSB(CONFIG1_DESC_SIZE), // wTotalLength
  423. MSB(CONFIG1_DESC_SIZE),
  424. 4, // bNumInterfaces
  425. 1, // bConfigurationValue
  426. 0, // iConfiguration
  427. 0x80, // bmAttributes
  428. 250, // bMaxPower
  429. // --- Keyboard HID ---
  430. // - 9 bytes -
  431. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  432. 9, // bLength
  433. 4, // bDescriptorType
  434. KEYBOARD_INTERFACE, // bInterfaceNumber
  435. 0, // bAlternateSetting
  436. 1, // bNumEndpoints
  437. 0x03, // bInterfaceClass (0x03 = HID)
  438. 0x01, // bInterfaceSubClass (0x00 = Non-Boot, 0x01 = Boot)
  439. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  440. 0, // iInterface
  441. // - 9 bytes -
  442. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  443. 9, // bLength
  444. 0x21, // bDescriptorType
  445. 0x11, 0x01, // bcdHID
  446. 0, // bCountryCode - Setting to 0/Undefined
  447. 1, // bNumDescriptors
  448. 0x22, // bDescriptorType
  449. LSB(sizeof(keyboard_hid_report_desc)), // wDescriptorLength
  450. MSB(sizeof(keyboard_hid_report_desc)),
  451. // - 7 bytes -
  452. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  453. 7, // bLength
  454. 5, // bDescriptorType
  455. KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  456. 0x03, // bmAttributes (0x03=intr)
  457. KEYBOARD_SIZE, 0, // wMaxPacketSize
  458. 1, // bInterval
  459. // --- NKRO Keyboard HID ---
  460. // - 9 bytes -
  461. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  462. 9, // bLength
  463. 4, // bDescriptorType
  464. KEYBOARD_NKRO_INTERFACE, // bInterfaceNumber
  465. 0, // bAlternateSetting
  466. 1, // bNumEndpoints
  467. 0x03, // bInterfaceClass (0x03 = HID)
  468. 0x00, // bInterfaceSubClass (0x00 = Non-Boot, 0x01 = Boot)
  469. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  470. 0, // iInterface
  471. // - 9 bytes -
  472. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  473. 9, // bLength
  474. 0x21, // bDescriptorType
  475. 0x11, 0x01, // bcdHID
  476. 0, // bCountryCode - Setting to 0/Undefined
  477. 1, // bNumDescriptors
  478. 0x22, // bDescriptorType
  479. // wDescriptorLength
  480. LSB(sizeof(keyboard_nkro_hid_report_desc)),
  481. MSB(sizeof(keyboard_nkro_hid_report_desc)),
  482. // - 7 bytes -
  483. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  484. 7, // bLength
  485. 5, // bDescriptorType
  486. KEYBOARD_NKRO_ENDPOINT | 0x80, // bEndpointAddress
  487. 0x03, // bmAttributes (0x03=intr)
  488. KEYBOARD_NKRO_SIZE, 0, // wMaxPacketSize
  489. 1, // bInterval
  490. // --- Serial CDC ---
  491. // - 8 bytes -
  492. // interface association descriptor, USB ECN, Table 9-Z
  493. 8, // bLength
  494. 11, // bDescriptorType
  495. CDC_STATUS_INTERFACE, // bFirstInterface
  496. 2, // bInterfaceCount
  497. 0x02, // bFunctionClass
  498. 0x02, // bFunctionSubClass
  499. 0x01, // bFunctionProtocol
  500. 4, // iFunction
  501. // - 9 bytes -
  502. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  503. 9, // bLength
  504. 4, // bDescriptorType
  505. CDC_STATUS_INTERFACE, // bInterfaceNumber
  506. 0, // bAlternateSetting
  507. 1, // bNumEndpoints
  508. 0x02, // bInterfaceClass
  509. 0x02, // bInterfaceSubClass
  510. 0x01, // bInterfaceProtocol
  511. 0, // iInterface
  512. // - 5 bytes -
  513. // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
  514. 5, // bFunctionLength
  515. 0x24, // bDescriptorType
  516. 0x00, // bDescriptorSubtype
  517. 0x10, 0x01, // bcdCDC
  518. // - 5 bytes -
  519. // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
  520. 5, // bFunctionLength
  521. 0x24, // bDescriptorType
  522. 0x01, // bDescriptorSubtype
  523. 0x01, // bmCapabilities
  524. 1, // bDataInterface
  525. // - 4 bytes -
  526. // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
  527. 4, // bFunctionLength
  528. 0x24, // bDescriptorType
  529. 0x02, // bDescriptorSubtype
  530. 0x06, // bmCapabilities
  531. // - 5 bytes -
  532. // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
  533. 5, // bFunctionLength
  534. 0x24, // bDescriptorType
  535. 0x06, // bDescriptorSubtype
  536. CDC_STATUS_INTERFACE, // bMasterInterface
  537. CDC_DATA_INTERFACE, // bSlaveInterface0
  538. // - 7 bytes -
  539. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  540. 7, // bLength
  541. 5, // bDescriptorType
  542. CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
  543. 0x03, // bmAttributes (0x03=intr)
  544. CDC_ACM_SIZE, 0, // wMaxPacketSize
  545. 64, // bInterval
  546. // - 9 bytes -
  547. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  548. 9, // bLength
  549. 4, // bDescriptorType
  550. CDC_DATA_INTERFACE, // bInterfaceNumber
  551. 0, // bAlternateSetting
  552. 2, // bNumEndpoints
  553. 0x0A, // bInterfaceClass
  554. 0x00, // bInterfaceSubClass
  555. 0x00, // bInterfaceProtocol
  556. 0, // iInterface
  557. // - 7 bytes -
  558. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  559. 7, // bLength
  560. 5, // bDescriptorType
  561. CDC_RX_ENDPOINT, // bEndpointAddress
  562. 0x02, // bmAttributes (0x02=bulk)
  563. CDC_RX_SIZE, 0, // wMaxPacketSize
  564. 0, // bInterval
  565. // - 7 bytes -
  566. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  567. 7, // bLength
  568. 5, // bDescriptorType
  569. CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
  570. 0x02, // bmAttributes (0x02=bulk)
  571. CDC_TX_SIZE, 0, // wMaxPacketSize
  572. 0, // bInterval
  573. };
  574. // Configuration Endpoint (0) Descriptor
  575. struct usb_string_descriptor_struct {
  576. uint8_t bLength;
  577. uint8_t bDescriptorType;
  578. int16_t wString[];
  579. };
  580. static const struct usb_string_descriptor_struct PROGMEM string0 = {
  581. 4,
  582. 3,
  583. {0x0409}
  584. };
  585. static const struct usb_string_descriptor_struct PROGMEM string1 = {
  586. sizeof(STR_MANUFACTURER),
  587. 3,
  588. STR_MANUFACTURER
  589. };
  590. static const struct usb_string_descriptor_struct PROGMEM string2 = {
  591. sizeof(STR_PRODUCT),
  592. 3,
  593. STR_PRODUCT
  594. };
  595. static const struct usb_string_descriptor_struct PROGMEM string3 = {
  596. sizeof(STR_SERIAL),
  597. 3,
  598. STR_SERIAL
  599. };
  600. // This table defines which descriptor data is sent for each specific
  601. // request from the host (in wValue and wIndex).
  602. static const struct descriptor_list_struct {
  603. uint16_t wValue;
  604. uint16_t wIndex;
  605. const uint8_t *addr;
  606. uint8_t length;
  607. } PROGMEM descriptor_list[] = {
  608. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  609. {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
  610. {0x0600, 0x0000, device_qualifier_descriptor, sizeof(device_qualifier_descriptor)},
  611. {0x0A00, 0x0000, usb_debug_descriptor, sizeof(usb_debug_descriptor)},
  612. {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
  613. {0x2100, KEYBOARD_INTERFACE, config1_descriptor + KEYBOARD_HID_DESC_OFFSET, 9},
  614. {0x2200, KEYBOARD_NKRO_INTERFACE, keyboard_nkro_hid_report_desc, sizeof(keyboard_nkro_hid_report_desc)},
  615. {0x2100, KEYBOARD_NKRO_INTERFACE, config1_descriptor + KEYBOARD_NKRO_HID_DESC_OFFSET, 9},
  616. {0x0300, 0x0000, (const uint8_t *)&string0, 4},
  617. {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
  618. {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
  619. {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL)}
  620. };
  621. #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))