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