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