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

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