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