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 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. void 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 128
  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. 0x03, // bDeviceClass - 0x03 = HID Class
  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. // Modifier Byte
  280. 0x85, 0x01, // Report ID (1),
  281. 0x75, 0x01, // Report Size (1),
  282. 0x95, 0x08, // Report Count (8),
  283. 0x15, 0x00, // Logical Minimum (0),
  284. 0x25, 0x01, // Logical Maximum (1),
  285. 0x05, 0x07, // Usage Page (Key Codes),
  286. 0x19, 0xE0, // Usage Minimum (224),
  287. 0x29, 0xE7, // Usage Maximum (231),
  288. 0x81, 0x02, // Input (Data, Variable, Absolute),
  289. // LED Report
  290. 0x85, 0x02, // Report ID (2),
  291. 0x75, 0x01, // Report Size (1),
  292. 0x95, 0x05, // Report Count (5),
  293. 0x05, 0x08, // Usage Page (LEDs),
  294. 0x19, 0x01, // Usage Minimum (1),
  295. 0x29, 0x05, // Usage Maximum (5),
  296. 0x91, 0x02, // Output (Data, Variable, Absolute),
  297. // LED Report Padding
  298. 0x75, 0x03, // Report Size (3),
  299. 0x95, 0x01, // Report Count (1),
  300. 0x91, 0x03, // Output (Constant),
  301. // Normal Keys - Using an NKRO Bitmap
  302. //
  303. // NOTES:
  304. // Supports all keys defined by the spec, except 1-3 which define error events
  305. // and 0 which is "no keys pressed"
  306. // See http://www.usb.org/developers/hidpage/Hut1_12v2.pdf Chapter 10
  307. // Or Macros/PartialMap/usb_hid.h
  308. //
  309. // 165-175 are reserved/unused as well as 222-223 and 232-65535
  310. // 224-231 are used for modifiers (see above)
  311. //
  312. // Packing of bitmaps are as follows:
  313. // 4-164 : 20 bytes + 1 Report ID byte (0x04-0xA4)
  314. // 176-221 : 6 bytes + 1 Report ID byte (0xB0-0xDD) (45 bits + 3 padding bits for 6 bytes total)
  315. //
  316. // 4-164 (20 bytes/160 bits)
  317. 0x85, 0x03, // Report ID (3),
  318. 0x75, 0x01, // Report Size (1),
  319. 0x95, 0xA0, // Report Count (160),
  320. 0x15, 0x00, // Logical Minimum (0),
  321. 0x25, 0x01, // Logical Maximum (1),
  322. 0x05, 0x07, // Usage Page (Key Codes),
  323. 0x19, 0x04, // Usage Minimum (4),
  324. 0x29, 0xA4, // Usage Maximum (164),
  325. 0x81, 0x02, // Input (Data, Variable, Absolute, Bitfield),
  326. // 176-221 (45 bits)
  327. 0x85, 0x04, // Report ID (4),
  328. 0x75, 0x01, // Report Size (1),
  329. 0x95, 0x2D, // Report Count (45),
  330. 0x15, 0x00, // Logical Minimum (0),
  331. 0x25, 0x01, // Logical Maximum (1),
  332. 0x05, 0x07, // Usage Page (Key Codes),
  333. 0x19, 0xB0, // Usage Minimum (176),
  334. 0x29, 0xDD, // Usage Maximum (221),
  335. 0x81, 0x02, // Input (Data, Variable, Absolute, Bitfield),
  336. // 176-221 Padding (3 bits)
  337. 0x75, 0x03, // Report Size (3),
  338. 0x95, 0x01, // Report Count (1),
  339. 0x81, 0x03, // Input (Constant),
  340. 0xc0, // End Collection - Keyboard
  341. // System Control Collection
  342. //
  343. // NOTES:
  344. // Not bothering with NKRO for this table. If there's need, I can implement it. -HaaTa
  345. // Using a 1KRO scheme
  346. 0x05, 0x01, // Usage Page (Generic Desktop),
  347. 0x09, 0x80, // Usage (System Control),
  348. 0xA1, 0x01, // Collection (Application),
  349. 0x85, 0x05, // Report ID (5),
  350. 0x75, 0x08, // Report Size (8),
  351. 0x95, 0x01, // Report Count (1),
  352. 0x16, 0x81, 0x00, // Logical Minimum (129),
  353. 0x26, 0xB7, 0x00, // Logical Maximum (183),
  354. 0x19, 0x81, // Usage Minimum (129),
  355. 0x29, 0xB7, // Usage Maximum (183),
  356. 0x81, 0x00, // Input (Data, Array),
  357. 0xc0, // End Collection - System Control
  358. // Consumer Control Collection - Media Keys
  359. //
  360. // NOTES:
  361. // Not bothering with NKRO for this table. If there's a need, I can implement it. -HaaTa
  362. // Using a 1KRO scheme
  363. 0x05, 0x0c, // Usage Page (Consumer),
  364. 0x09, 0x01, // Usage (Consumer Control),
  365. 0xA1, 0x01, // Collection (Application),
  366. 0x85, 0x06, // Report ID (6),
  367. 0x75, 0x10, // Report Size (16),
  368. 0x95, 0x01, // Report Count (1),
  369. 0x16, 0x20, 0x00, // Logical Minimum (32),
  370. 0x26, 0x9C, 0x02, // Logical Maximum (668),
  371. 0x05, 0x0C, // Usage Page (Consumer),
  372. 0x19, 0x20, // Usage Minimum (32),
  373. 0x2A, 0x9C, 0x02, // Usage Maximum (668),
  374. 0x81, 0x00, // Input (Data, Array),
  375. 0xc0, // End Collection - Consumer Control
  376. };
  377. // <Configuration> + <Keyboard HID> + <NKRO Keyboard HID> + <Serial CDC>
  378. #define CONFIG1_DESC_SIZE (9 + 9+9+7 + 9+9+7 + 8+9+5+5+4+5+7+9+7+7)
  379. #define KEYBOARD_HID_DESC_OFFSET (9 + 9)
  380. #define KEYBOARD_NKRO_HID_DESC_OFFSET (9 + 9+9+7 + 9)
  381. #define SERIAL_CDC_DESC_OFFSET (9 + 9+9+7 + 9+9+7)
  382. static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
  383. // --- Configuration ---
  384. // - 9 bytes -
  385. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  386. 9, // bLength;
  387. 2, // bDescriptorType;
  388. LSB(CONFIG1_DESC_SIZE), // wTotalLength
  389. MSB(CONFIG1_DESC_SIZE),
  390. 4, // bNumInterfaces
  391. 1, // bConfigurationValue
  392. 0, // iConfiguration
  393. 0x80, // bmAttributes
  394. 250, // bMaxPower
  395. // --- Keyboard HID ---
  396. // - 9 bytes -
  397. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  398. 9, // bLength
  399. 4, // bDescriptorType
  400. KEYBOARD_INTERFACE, // bInterfaceNumber
  401. 0, // bAlternateSetting
  402. 1, // bNumEndpoints
  403. 0x03, // bInterfaceClass (0x03 = HID)
  404. 0x01, // bInterfaceSubClass (0x00 = Non-Boot, 0x01 = Boot)
  405. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  406. 0, // iInterface
  407. // - 9 bytes -
  408. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  409. 9, // bLength
  410. 0x21, // bDescriptorType
  411. 0x11, 0x01, // bcdHID
  412. 0, // bCountryCode - Setting to 0/Undefined
  413. 1, // bNumDescriptors
  414. 0x22, // bDescriptorType
  415. LSB(sizeof(keyboard_hid_report_desc)), // wDescriptorLength
  416. MSB(sizeof(keyboard_hid_report_desc)),
  417. // - 7 bytes -
  418. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  419. 7, // bLength
  420. 5, // bDescriptorType
  421. KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  422. 0x03, // bmAttributes (0x03=intr)
  423. KEYBOARD_SIZE, 0, // wMaxPacketSize
  424. 1, // bInterval
  425. // --- NKRO Keyboard HID ---
  426. // - 9 bytes -
  427. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  428. 9, // bLength
  429. 4, // bDescriptorType
  430. KEYBOARD_NKRO_INTERFACE, // bInterfaceNumber
  431. 0, // bAlternateSetting
  432. 1, // bNumEndpoints
  433. 0x03, // bInterfaceClass (0x03 = HID)
  434. 0x00, // bInterfaceSubClass (0x00 = Non-Boot, 0x01 = Boot)
  435. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  436. 0, // iInterface
  437. // - 9 bytes -
  438. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  439. 9, // bLength
  440. 0x21, // bDescriptorType
  441. 0x11, 0x01, // bcdHID
  442. 0, // bCountryCode - Setting to 0/Undefined
  443. 1, // bNumDescriptors
  444. 0x22, // bDescriptorType
  445. // wDescriptorLength
  446. LSB(sizeof(keyboard_nkro_hid_report_desc)),
  447. MSB(sizeof(keyboard_nkro_hid_report_desc)),
  448. // - 7 bytes -
  449. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  450. 7, // bLength
  451. 5, // bDescriptorType
  452. KEYBOARD_NKRO_ENDPOINT | 0x80, // bEndpointAddress
  453. 0x03, // bmAttributes (0x03=intr)
  454. KEYBOARD_NKRO_SIZE, 0, // wMaxPacketSize
  455. 1, // bInterval
  456. // --- Serial CDC ---
  457. // - 8 bytes -
  458. // interface association descriptor, USB ECN, Table 9-Z
  459. 8, // bLength
  460. 11, // bDescriptorType
  461. CDC_STATUS_INTERFACE, // bFirstInterface
  462. 2, // bInterfaceCount
  463. 0x02, // bFunctionClass
  464. 0x02, // bFunctionSubClass
  465. 0x01, // bFunctionProtocol
  466. 4, // iFunction
  467. // - 9 bytes -
  468. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  469. 9, // bLength
  470. 4, // bDescriptorType
  471. CDC_STATUS_INTERFACE, // bInterfaceNumber
  472. 0, // bAlternateSetting
  473. 1, // bNumEndpoints
  474. 0x02, // bInterfaceClass
  475. 0x02, // bInterfaceSubClass
  476. 0x01, // bInterfaceProtocol
  477. 0, // iInterface
  478. // - 5 bytes -
  479. // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
  480. 5, // bFunctionLength
  481. 0x24, // bDescriptorType
  482. 0x00, // bDescriptorSubtype
  483. 0x10, 0x01, // bcdCDC
  484. // - 5 bytes -
  485. // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
  486. 5, // bFunctionLength
  487. 0x24, // bDescriptorType
  488. 0x01, // bDescriptorSubtype
  489. 0x01, // bmCapabilities
  490. 1, // bDataInterface
  491. // - 4 bytes -
  492. // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
  493. 4, // bFunctionLength
  494. 0x24, // bDescriptorType
  495. 0x02, // bDescriptorSubtype
  496. 0x06, // bmCapabilities
  497. // - 5 bytes -
  498. // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
  499. 5, // bFunctionLength
  500. 0x24, // bDescriptorType
  501. 0x06, // bDescriptorSubtype
  502. CDC_STATUS_INTERFACE, // bMasterInterface
  503. CDC_DATA_INTERFACE, // bSlaveInterface0
  504. // - 7 bytes -
  505. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  506. 7, // bLength
  507. 5, // bDescriptorType
  508. CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
  509. 0x03, // bmAttributes (0x03=intr)
  510. CDC_ACM_SIZE, 0, // wMaxPacketSize
  511. 64, // bInterval
  512. // - 9 bytes -
  513. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  514. 9, // bLength
  515. 4, // bDescriptorType
  516. CDC_DATA_INTERFACE, // bInterfaceNumber
  517. 0, // bAlternateSetting
  518. 2, // bNumEndpoints
  519. 0x0A, // bInterfaceClass
  520. 0x00, // bInterfaceSubClass
  521. 0x00, // bInterfaceProtocol
  522. 0, // iInterface
  523. // - 7 bytes -
  524. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  525. 7, // bLength
  526. 5, // bDescriptorType
  527. CDC_RX_ENDPOINT, // bEndpointAddress
  528. 0x02, // bmAttributes (0x02=bulk)
  529. CDC_RX_SIZE, 0, // wMaxPacketSize
  530. 0, // bInterval
  531. // - 7 bytes -
  532. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  533. 7, // bLength
  534. 5, // bDescriptorType
  535. CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
  536. 0x02, // bmAttributes (0x02=bulk)
  537. CDC_TX_SIZE, 0, // wMaxPacketSize
  538. 0, // bInterval
  539. };
  540. // Configuration Endpoint (0) Descriptor
  541. struct usb_string_descriptor_struct {
  542. uint8_t bLength;
  543. uint8_t bDescriptorType;
  544. int16_t wString[];
  545. };
  546. static const struct usb_string_descriptor_struct PROGMEM string0 = {
  547. 4,
  548. 3,
  549. {0x0409}
  550. };
  551. static const struct usb_string_descriptor_struct PROGMEM string1 = {
  552. sizeof(STR_MANUFACTURER),
  553. 3,
  554. STR_MANUFACTURER
  555. };
  556. static const struct usb_string_descriptor_struct PROGMEM string2 = {
  557. sizeof(STR_PRODUCT),
  558. 3,
  559. STR_PRODUCT
  560. };
  561. static const struct usb_string_descriptor_struct PROGMEM string3 = {
  562. sizeof(STR_SERIAL),
  563. 3,
  564. STR_SERIAL
  565. };
  566. // This table defines which descriptor data is sent for each specific
  567. // request from the host (in wValue and wIndex).
  568. static const struct descriptor_list_struct {
  569. uint16_t wValue;
  570. uint16_t wIndex;
  571. const uint8_t *addr;
  572. uint8_t length;
  573. } PROGMEM descriptor_list[] = {
  574. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  575. {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
  576. {0x0600, 0x0000, device_qualifier_descriptor, sizeof(device_qualifier_descriptor)},
  577. {0x0A00, 0x0000, usb_debug_descriptor, sizeof(usb_debug_descriptor)},
  578. {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
  579. {0x2100, KEYBOARD_INTERFACE, config1_descriptor + KEYBOARD_HID_DESC_OFFSET, 9},
  580. {0x2200, KEYBOARD_NKRO_INTERFACE, keyboard_nkro_hid_report_desc, sizeof(keyboard_nkro_hid_report_desc)},
  581. {0x2100, KEYBOARD_NKRO_INTERFACE, config1_descriptor + KEYBOARD_NKRO_HID_DESC_OFFSET, 9},
  582. {0x0300, 0x0000, (const uint8_t *)&string0, 4},
  583. {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
  584. {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
  585. {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL)}
  586. };
  587. #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
  588. #endif // usb_keyboard_serial_h__