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