]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/pjrcUSB/arm/usb_desc.c
FIxing Media Keys and general USB compatibilty
[kiibohd-controller.git] / Output / pjrcUSB / arm / usb_desc.c
1 /* Teensyduino Core Library
2  * http://www.pjrc.com/teensy/
3  * Copyright (c) 2013 PJRC.COM, LLC.
4  * Modified by Jacob Alexander (2013-2015)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * 1. The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * 2. If the Software is incorporated into a build system that allows
18  * selection among a list of target devices, then similar target
19  * devices manufactured by PJRC.COM must be included in the list of
20  * target devices and selectable in the same manner.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29  * SOFTWARE.
30  */
31
32 // ----- Includes -----
33
34 // Local Includes
35 #include "usb_desc.h"
36
37 // Generated Includes
38 #include <kll_defs.h>
39
40
41
42 // ----- Macros -----
43
44 #define LSB(n) ((n) & 255)
45 #define MSB(n) (((n) >> 8) & 255)
46
47
48
49 // ----- USB Device Descriptor -----
50
51 // USB Device Descriptor.  The USB host reads this first, to learn
52 // what type of device is connected.
53 static uint8_t device_descriptor[] = {
54         18,                                     // bLength
55         1,                                      // bDescriptorType
56         0x00, 0x02,                             // bcdUSB
57         DEVICE_CLASS,                           // bDeviceClass
58         DEVICE_SUBCLASS,                        // bDeviceSubClass
59         DEVICE_PROTOCOL,                        // bDeviceProtocol
60         EP0_SIZE,                               // bMaxPacketSize0
61         LSB(VENDOR_ID), MSB(VENDOR_ID),         // idVendor
62         LSB(PRODUCT_ID), MSB(PRODUCT_ID),       // idProduct
63         0x00, 0x01,                             // bcdDevice
64         1,                                      // iManufacturer
65         2,                                      // iProduct
66         3,                                      // iSerialNumber
67         1                                       // bNumConfigurations
68 };
69
70 // USB Device Qualifier Descriptor
71 static uint8_t device_qualifier_descriptor[] = {
72         0                                       // Indicate only single speed
73         /* Device qualifier example (used for specifying multiple USB speeds)
74         10,                                     // bLength
75         6,                                      // bDescriptorType
76         0x00, 0x02,                             // bcdUSB
77         DEVICE_CLASS,                           // bDeviceClass
78         DEVICE_SUBCLASS,                        // bDeviceSubClass
79         DEVICE_PROTOCOL,                        // bDeviceProtocol
80         EP0_SIZE,                               // bMaxPacketSize0
81         0,                                      // bNumOtherSpeedConfigurations
82         0                                       // bReserved
83         */
84 };
85
86 // USB Debug Descriptor
87 // XXX Not sure of exact use, lsusb requests it
88 static uint8_t usb_debug_descriptor[] = {
89         0
90 };
91
92 // XXX
93 // These descriptors must NOT be "const", because the USB DMA
94 // has trouble accessing flash memory with enough bandwidth
95 // while the processor is executing from flash.
96 // XXX
97
98
99
100 // ----- USB HID Report Descriptsors -----
101
102 // Each HID interface needs a special report descriptor that tells
103 // the meaning and format of the data.
104
105 // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
106 static uint8_t keyboard_report_desc[] = {
107         // Keyboard Collection
108         0x05, 0x01,          // Usage Page (Generic Desktop),
109         0x09, 0x06,          // Usage (Keyboard),
110         0xA1, 0x01,          // Collection (Application) - Keyboard,
111
112         // Modifier Byte
113         0x75, 0x01,          //   Report Size (1),
114         0x95, 0x08,          //   Report Count (8),
115         0x05, 0x07,          //   Usage Page (Key Codes),
116         0x19, 0xE0,          //   Usage Minimum (224),
117         0x29, 0xE7,          //   Usage Maximum (231),
118         0x15, 0x00,          //   Logical Minimum (0),
119         0x25, 0x01,          //   Logical Maximum (1),
120         0x81, 0x02,          //   Input (Data, Variable, Absolute),
121
122         // Reserved Byte
123         0x75, 0x08,          //   Report Size (8),
124         0x95, 0x01,          //   Report Count (1),
125         0x81, 0x03,          //   Output (Constant),
126
127         // LED Report
128         0x75, 0x01,          //   Report Size (1),
129         0x95, 0x05,          //   Report Count (5),
130         0x05, 0x08,          //   Usage Page (LEDs),
131         0x19, 0x01,          //   Usage Minimum (1),
132         0x29, 0x05,          //   Usage Maximum (5),
133         0x91, 0x02,          //   Output (Data, Variable, Absolute),
134
135         // LED Report Padding
136         0x75, 0x03,          //   Report Size (3),
137         0x95, 0x01,          //   Report Count (1),
138         0x91, 0x03,          //   Output (Constant),
139
140         // Normal Keys
141         0x75, 0x08,          //   Report Size (8),
142         0x95, 0x06,          //   Report Count (6),
143         0x15, 0x00,          //   Logical Minimum (0),
144         0x25, 0x7F,          //   Logical Maximum(104),
145         0x05, 0x07,          //   Usage Page (Key Codes),
146         0x19, 0x00,          //   Usage Minimum (0),
147         0x29, 0x7F,          //   Usage Maximum (104),
148         0x81, 0x00,          //   Input (Data, Array),
149         0xc0,                // End Collection - Keyboard
150 };
151
152 // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
153 static uint8_t nkro_keyboard_report_desc[] = {
154         // Keyboard Collection
155         0x05, 0x01,          // Usage Page (Generic Desktop),
156         0x09, 0x06,          // Usage (Keyboard),
157         0xA1, 0x01,          // Collection (Application) - Keyboard,
158
159         // LED Report
160         0x85, 0x01,          //   Report ID (1),
161         0x75, 0x01,          //   Report Size (1),
162         0x95, 0x05,          //   Report Count (5),
163         0x05, 0x08,          //   Usage Page (LEDs),
164         0x19, 0x01,          //   Usage Minimum (1),
165         0x29, 0x05,          //   Usage Maximum (5),
166         0x91, 0x02,          //   Output (Data, Variable, Absolute),
167
168         // LED Report Padding
169         0x75, 0x03,          //   Report Size (3),
170         0x95, 0x01,          //   Report Count (1),
171         0x91, 0x03,          //   Output (Constant),
172
173         // Normal Keys - Using an NKRO Bitmap
174         //
175         // NOTES:
176         // Supports all keys defined by the spec, except 1-3 which define error events
177         //  and 0 which is "no keys pressed"
178         // See http://www.usb.org/developers/hidpage/Hut1_12v2.pdf Chapter 10
179         // Or Macros/PartialMap/usb_hid.h
180         //
181         // 50 (ISO \ due to \ bug) and 156 (Clear due to Delete bug) must be excluded
182         //  due to a Linux bug with bitmaps (not useful anyways)
183         // 165-175 are reserved/unused as well as 222-223 and 232-65535
184         //
185         // Compatibility Notes:
186         //  - Using a second endpoint for a boot mode device helps with compatibility
187         //  - DO NOT use Padding in the descriptor for bitfields
188         //    (Mac OSX silently fails... Windows/Linux work correctly)
189         //  - DO NOT use Report IDs, Windows 8.1 will not update keyboard correctly (modifiers disappear)
190         //    (all other OSs, including OSX work fine...)
191         //    (you can use them *iff* you only have 1 per collection)
192         //  - Mac OSX and Windows 8.1 are extremely picky about padding
193         //
194         // Packing of bitmaps are as follows:
195         //   4-49  :  6 bytes (0x04-0x31) ( 46 bits + 2 padding bits for 6 bytes total)
196         //  51-155 : 14 bytes (0x33-0x9B) (105 bits + 6 padding bits for 15 bytes total)
197         // 157-164 :  1 byte  (0x9D-0xA4) (  8 bits)
198         // 176-221 :  6 bytes (0xB0-0xDD) ( 46 bits + 2 padding bits for 6 bytes total)
199         // 224-231 :  1 byte  (0xE0-0xE7) (  8 bits)
200
201         // Modifier Byte
202         0x75, 0x01,          //   Report Size (1),
203         0x95, 0x08,          //   Report Count (8),
204         0x15, 0x00,          //   Logical Minimum (0),
205         0x25, 0x01,          //   Logical Maximum (1),
206         0x05, 0x07,          //   Usage Page (Key Codes),
207         0x19, 0xE0,          //   Usage Minimum (224),
208         0x29, 0xE7,          //   Usage Maximum (231),
209         0x81, 0x02,          //   Input (Data, Variable, Absolute),
210
211         // 4-49 (6 bytes/46 bits) - MainKeys
212         0x75, 0x01,          //   Report Size (1),
213         0x95, 0x2E,          //   Report Count (46),
214         0x15, 0x00,          //   Logical Minimum (0),
215         0x25, 0x01,          //   Logical Maximum (1),
216         0x05, 0x07,          //   Usage Page (Key Codes),
217         0x19, 0x04,          //   Usage Minimum (4),
218         0x29, 0x31,          //   Usage Maximum (49),
219         0x81, 0x02,          //   Input (Data, Variable, Absolute, Bitfield),
220
221         // Padding (2 bits)
222         0x75, 0x02,          //   Report Size (2),
223         0x95, 0x01,          //   Report Count (1),
224         0x81, 0x03,          //   Input (Constant),
225
226         // 51-155 (14 bytes/105 bits) - SecondaryKeys
227         0x75, 0x01,          //   Report Size (1),
228         0x95, 0x69,          //   Report Count (105),
229         0x15, 0x00,          //   Logical Minimum (0),
230         0x25, 0x01,          //   Logical Maximum (1),
231         0x05, 0x07,          //   Usage Page (Key Codes),
232         0x19, 0x33,          //   Usage Minimum (51),
233         0x29, 0x9B,          //   Usage Maximum (155),
234         0x81, 0x02,          //   Input (Data, Variable, Absolute, Bitfield),
235
236         // Padding (7 bits)
237         0x75, 0x07,          //   Report Size (7),
238         0x95, 0x01,          //   Report Count (1),
239         0x81, 0x03,          //   Input (Constant),
240
241         // 157-164 (1 byte/8 bits) - TertiaryKeys
242         0x75, 0x01,          //   Report Size (1),
243         0x95, 0x08,          //   Report Count (8),
244         0x15, 0x00,          //   Logical Minimum (0),
245         0x25, 0x01,          //   Logical Maximum (1),
246         0x05, 0x07,          //   Usage Page (Key Codes),
247         0x19, 0x9D,          //   Usage Minimum (157),
248         0x29, 0xA4,          //   Usage Maximum (164),
249         0x81, 0x02,          //   Input (Data, Variable, Absolute, Bitfield),
250
251         // 176-221 (6 bytes/46 bits) - QuartiaryKeys
252         0x75, 0x01,          //   Report Size (1),
253         0x95, 0x2E,          //   Report Count (46),
254         0x15, 0x00,          //   Logical Minimum (0),
255         0x25, 0x01,          //   Logical Maximum (1),
256         0x05, 0x07,          //   Usage Page (Key Codes),
257         0x19, 0xB0,          //   Usage Minimum (176),
258         0x29, 0xDD,          //   Usage Maximum (221),
259         0x81, 0x02,          //   Input (Data, Variable, Absolute, Bitfield),
260
261         // Padding (2 bits)
262         0x75, 0x02,          //   Report Size (2),
263         0x95, 0x01,          //   Report Count (1),
264         0x81, 0x03,          //   Input (Constant),
265         0xc0,                // End Collection - Keyboard
266 };
267
268 // System Control and Consumer Control
269 static uint8_t sys_ctrl_report_desc[] = {
270         // System Control Collection
271         //
272         // NOTES:
273         // Not bothering with NKRO for this table. If there's need, I can implement it. -HaaTa
274         // Using a 1KRO scheme
275         0x05, 0x01,          // Usage Page (Generic Desktop),
276         0x09, 0x80,          // Usage (System Control),
277         0xA1, 0x01,          // Collection (Application),
278         0x85, 0x02,          //   Report ID (2),
279         0x75, 0x08,          //   Report Size (8),
280         0x95, 0x01,          //   Report Count (1),
281         0x16, 0x81, 0x00,    //   Logical Minimum (129),
282         0x26, 0xB7, 0x00,    //   Logical Maximum (183),
283         0x19, 0x81,          //   Usage Minimum (129),
284         0x29, 0xB7,          //   Usage Maximum (183),
285         0x81, 0x00,          //   Input (Data, Array),
286         0xc0,                // End Collection - System Control
287
288         // Consumer Control Collection - Media Keys
289         //
290         // NOTES:
291         // Not bothering with NKRO for this table. If there's a need, I can implement it. -HaaTa
292         // Using a 1KRO scheme
293         0x05, 0x0c,          // Usage Page (Consumer),
294         0x09, 0x01,          // Usage (Consumer Control),
295         0xA1, 0x01,          // Collection (Application),
296         0x85, 0x03,          //   Report ID (3),
297         0x75, 0x10,          //   Report Size (16),
298         0x95, 0x01,          //   Report Count (1),
299         0x16, 0x01, 0x00,    //   Logical Minimum (1),
300         0x26, 0x9C, 0x02,    //   Logical Maximum (668),
301         0x05, 0x0C,          //   Usage Page (Consumer),
302         0x19, 0x01,          //   Usage Minimum (1),
303         0x2A, 0x9C, 0x02,    //   Usage Maximum (668),
304         0x81, 0x00,          //   Input (Data, Array),
305         0xc0,                // End Collection - Consumer Control
306 };
307
308 // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
309 static uint8_t mouse_report_desc[] = {
310         0x05, 0x01,                     // Usage Page (Generic Desktop)
311         0x09, 0x02,                     // Usage (Mouse)
312         0xA1, 0x01,                     // Collection (Application)
313         0x05, 0x09,                     //   Usage Page (Button)
314         0x19, 0x01,                     //   Usage Minimum (Button #1)
315         0x29, 0x03,                     //   Usage Maximum (Button #3)
316         0x15, 0x00,                     //   Logical Minimum (0)
317         0x25, 0x01,                     //   Logical Maximum (1)
318         0x95, 0x03,                     //   Report Count (3)
319         0x75, 0x01,                     //   Report Size (1)
320         0x81, 0x02,                     //   Input (Data, Variable, Absolute)
321         0x95, 0x01,                     //   Report Count (1)
322         0x75, 0x05,                     //   Report Size (5)
323         0x81, 0x03,                     //   Input (Constant)
324         0x05, 0x01,                     //   Usage Page (Generic Desktop)
325         0x09, 0x30,                     //   Usage (X)
326         0x09, 0x31,                     //   Usage (Y)
327         0x15, 0x00,                     //   Logical Minimum (0)
328         0x26, 0xFF, 0x7F,               //   Logical Maximum (32767)
329         0x75, 0x10,                     //   Report Size (16),
330         0x95, 0x02,                     //   Report Count (2),
331         0x81, 0x02,                     //   Input (Data, Variable, Absolute)
332         0x09, 0x38,                     //   Usage (Wheel)
333         0x15, 0x81,                     //   Logical Minimum (-127)
334         0x25, 0x7F,                     //   Logical Maximum (127)
335         0x75, 0x08,                     //   Report Size (8),
336         0x95, 0x01,                     //   Report Count (1),
337         0x81, 0x06,                     //   Input (Data, Variable, Relative)
338         0xC0                            // End Collection
339 };
340
341 // Joystick Protocol, HID 1.11 spec, Apendix D, page 64-65
342 static uint8_t joystick_report_desc[] = {
343         0x05, 0x01,                     // Usage Page (Generic Desktop)
344         0x09, 0x04,                     // Usage (Joystick)
345         0xA1, 0x01,                     // Collection (Application)
346         0x15, 0x00,                     // Logical Minimum (0)
347         0x25, 0x01,                     // Logical Maximum (1)
348         0x75, 0x01,                     // Report Size (1)
349         0x95, 0x20,                     // Report Count (32)
350         0x05, 0x09,                     // Usage Page (Button)
351         0x19, 0x01,                     // Usage Minimum (Button #1)
352         0x29, 0x20,                     // Usage Maximum (Button #32)
353         0x81, 0x02,                     // Input (variable,absolute)
354         0x15, 0x00,                     // Logical Minimum (0)
355         0x25, 0x07,                     // Logical Maximum (7)
356         0x35, 0x00,                     // Physical Minimum (0)
357         0x46, 0x3B, 0x01,               // Physical Maximum (315)
358         0x75, 0x04,                     // Report Size (4)
359         0x95, 0x01,                     // Report Count (1)
360         0x65, 0x14,                     // Unit (20)
361         0x05, 0x01,                     // Usage Page (Generic Desktop)
362         0x09, 0x39,                     // Usage (Hat switch)
363         0x81, 0x42,                     // Input (variable,absolute,null_state)
364         0x05, 0x01,                     // Usage Page (Generic Desktop)
365         0x09, 0x01,                     // Usage (Pointer)
366         0xA1, 0x00,                     // Collection ()
367         0x15, 0x00,                     //   Logical Minimum (0)
368         0x26, 0xFF, 0x03,               //   Logical Maximum (1023)
369         0x75, 0x0A,                     //   Report Size (10)
370         0x95, 0x04,                     //   Report Count (4)
371         0x09, 0x30,                     //   Usage (X)
372         0x09, 0x31,                     //   Usage (Y)
373         0x09, 0x32,                     //   Usage (Z)
374         0x09, 0x35,                     //   Usage (Rz)
375         0x81, 0x02,                     //   Input (variable,absolute)
376         0xC0,                           // End Collection
377         0x15, 0x00,                     // Logical Minimum (0)
378         0x26, 0xFF, 0x03,               // Logical Maximum (1023)
379         0x75, 0x0A,                     // Report Size (10)
380         0x95, 0x02,                     // Report Count (2)
381         0x09, 0x36,                     // Usage (Slider)
382         0x09, 0x36,                     // Usage (Slider)
383         0x81, 0x02,                     // Input (variable,absolute)
384         0xC0                            // End Collection
385 };
386
387
388
389 // ----- USB Configuration -----
390
391 // USB Configuration Descriptor.  This huge descriptor tells all
392 // of the devices capbilities.
393 static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
394 // --- Configuration ---
395 // - 9 bytes -
396         // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
397         9,                                      // bLength;
398         2,                                      // bDescriptorType;
399         LSB(CONFIG_DESC_SIZE),                  // wTotalLength
400         MSB(CONFIG_DESC_SIZE),
401         NUM_INTERFACE,                          // bNumInterfaces
402         1,                                      // bConfigurationValue
403         0,                                      // iConfiguration
404         0xA0,                                   // bmAttributes
405         250,                                    // bMaxPower
406
407 // --- Keyboard HID --- Boot Mode Keyboard Interface
408 // - 9 bytes -
409         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
410         9,                                      // bLength
411         4,                                      // bDescriptorType
412         KEYBOARD_INTERFACE,                     // bInterfaceNumber
413         0,                                      // bAlternateSetting
414         1,                                      // bNumEndpoints
415         0x03,                                   // bInterfaceClass (0x03 = HID)
416         0x01,                                   // bInterfaceSubClass (0x00 = Non-Boot, 0x01 = Boot)
417         0x01,                                   // bInterfaceProtocol (0x01 = Keyboard)
418         KEYBOARD_INTERFACE + 4,                 // iInterface
419 // - 9 bytes -
420         // HID interface descriptor, HID 1.11 spec, section 6.2.1
421         9,                                      // bLength
422         0x21,                                   // bDescriptorType
423         0x11, 0x01,                             // bcdHID
424         KeyboardLocale_define,                  // bCountryCode
425         1,                                      // bNumDescriptors
426         0x22,                                   // bDescriptorType
427         LSB(sizeof(keyboard_report_desc)),      // wDescriptorLength
428         MSB(sizeof(keyboard_report_desc)),
429 // - 7 bytes -
430         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
431         7,                                      // bLength
432         5,                                      // bDescriptorType
433         KEYBOARD_ENDPOINT | 0x80,               // bEndpointAddress
434         0x03,                                   // bmAttributes (0x03=intr)
435         KEYBOARD_SIZE, 0,                       // wMaxPacketSize
436         KEYBOARD_INTERVAL,                      // bInterval
437
438 // --- NKRO Keyboard HID --- OS Mode Keyboard Interface
439 // - 9 bytes -
440         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
441         9,                                      // bLength
442         4,                                      // bDescriptorType
443         NKRO_KEYBOARD_INTERFACE,                // bInterfaceNumber
444         0,                                      // bAlternateSetting
445         1,                                      // bNumEndpoints
446         0x03,                                   // bInterfaceClass (0x03 = HID)
447         0x00,                                   // bInterfaceSubClass (0x00 = Non-Boot, 0x01 = Boot)
448         0x01,                                   // bInterfaceProtocol (0x01 = Keyboard)
449         NKRO_KEYBOARD_INTERFACE + 4,            // iInterface
450 // - 9 bytes -
451         // HID interface descriptor, HID 1.11 spec, section 6.2.1
452         9,                                      // bLength
453         0x21,                                   // bDescriptorType
454         0x11, 0x01,                             // bcdHID
455         KeyboardLocale_define,                  // bCountryCode
456         1,                                      // bNumDescriptors
457         0x22,                                   // bDescriptorType
458         LSB(sizeof(nkro_keyboard_report_desc)), // wDescriptorLength
459         MSB(sizeof(nkro_keyboard_report_desc)),
460 // - 7 bytes -
461         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
462         7,                                      // bLength
463         5,                                      // bDescriptorType
464         NKRO_KEYBOARD_ENDPOINT | 0x80,          // bEndpointAddress
465         0x03,                                   // bmAttributes (0x03=intr)
466         NKRO_KEYBOARD_SIZE, 0,                  // wMaxPacketSize
467         NKRO_KEYBOARD_INTERVAL,                 // bInterval
468
469 // --- Serial CDC --- CDC IAD Descriptor
470 // - 8 bytes -
471         // interface association descriptor, USB ECN, Table 9-Z
472         8,                                      // bLength
473         11,                                     // bDescriptorType
474         CDC_STATUS_INTERFACE,                   // bFirstInterface
475         2,                                      // bInterfaceCount
476         0x02,                                   // bFunctionClass
477         0x02,                                   // bFunctionSubClass
478         0x01,                                   // bFunctionProtocol
479         CDC_STATUS_INTERFACE + 4,               // iFunction
480
481 // --- Serial CDC --- CDC Data Interface
482 // - 9 bytes -
483         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
484         9,                                      // bLength
485         4,                                      // bDescriptorType
486         CDC_STATUS_INTERFACE,                   // bInterfaceNumber
487         0,                                      // bAlternateSetting
488         1,                                      // bNumEndpoints
489         0x02,                                   // bInterfaceClass
490         0x02,                                   // bInterfaceSubClass
491         0x01,                                   // bInterfaceProtocol
492         CDC_STATUS_INTERFACE + 4,               // iInterface
493 // - 5 bytes -
494         // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
495         5,                                      // bFunctionLength
496         0x24,                                   // bDescriptorType
497         0x00,                                   // bDescriptorSubtype
498         0x10, 0x01,                             // bcdCDC
499 // - 5 bytes -
500         // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
501         5,                                      // bFunctionLength
502         0x24,                                   // bDescriptorType
503         0x01,                                   // bDescriptorSubtype
504         0x01,                                   // bmCapabilities
505         CDC_DATA_INTERFACE,                     // bDataInterface
506 // - 4 bytes -
507         // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
508         4,                                      // bFunctionLength
509         0x24,                                   // bDescriptorType
510         0x02,                                   // bDescriptorSubtype
511         0x06,                                   // bmCapabilities
512 // - 5 bytes -
513         // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
514         5,                                      // bFunctionLength
515         0x24,                                   // bDescriptorType
516         0x06,                                   // bDescriptorSubtype
517         CDC_STATUS_INTERFACE,                   // bMasterInterface
518         CDC_DATA_INTERFACE,                     // bSlaveInterface0
519 // - 7 bytes -
520         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
521         7,                                      // bLength
522         5,                                      // bDescriptorType
523         CDC_ACM_ENDPOINT | 0x80,                // bEndpointAddress
524         0x03,                                   // bmAttributes (0x03=intr)
525         CDC_ACM_SIZE, 0,                        // wMaxPacketSize
526         64,                                     // bInterval
527 // - 9 bytes -
528         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
529         9,                                      // bLength
530         4,                                      // bDescriptorType
531         CDC_DATA_INTERFACE,                     // bInterfaceNumber
532         0,                                      // bAlternateSetting
533         2,                                      // bNumEndpoints
534         0x0A,                                   // bInterfaceClass
535         0x00,                                   // bInterfaceSubClass
536         0x00,                                   // bInterfaceProtocol
537         CDC_DATA_INTERFACE + 4,                 // iInterface
538 // - 7 bytes -
539         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
540         7,                                      // bLength
541         5,                                      // bDescriptorType
542         CDC_RX_ENDPOINT,                        // bEndpointAddress
543         0x02,                                   // bmAttributes (0x02=bulk)
544         CDC_RX_SIZE, 0,                         // wMaxPacketSize
545         0,                                      // bInterval
546 // - 7 bytes -
547         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
548         7,                                      // bLength
549         5,                                      // bDescriptorType
550         CDC_TX_ENDPOINT | 0x80,                 // bEndpointAddress
551         0x02,                                   // bmAttributes (0x02=bulk)
552         CDC_TX_SIZE, 0,                         // wMaxPacketSize
553         0,                                      // bInterval
554
555 // --- Mouse Interface ---
556 // - 9 bytes -
557         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
558         9,                                      // bLength
559         4,                                      // bDescriptorType
560         MOUSE_INTERFACE,                        // bInterfaceNumber
561         0,                                      // bAlternateSetting
562         1,                                      // bNumEndpoints
563         0x03,                                   // bInterfaceClass (0x03 = HID)
564         0x00,                                   // bInterfaceSubClass (0x01 = Boot)
565         0x02,                                   // bInterfaceProtocol (0x02 = Mouse)
566         MOUSE_INTERFACE + 4,                    // iInterface
567 // - 9 bytes -
568         // HID interface descriptor, HID 1.11 spec, section 6.2.1
569         9,                                      // bLength
570         0x21,                                   // bDescriptorType
571         0x11, 0x01,                             // bcdHID
572         0,                                      // bCountryCode
573         1,                                      // bNumDescriptors
574         0x22,                                   // bDescriptorType
575         LSB(sizeof(mouse_report_desc)),         // wDescriptorLength
576         MSB(sizeof(mouse_report_desc)),
577 // - 7 bytes -
578         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
579         7,                                      // bLength
580         5,                                      // bDescriptorType
581         MOUSE_ENDPOINT | 0x80,                  // bEndpointAddress
582         0x03,                                   // bmAttributes (0x03=intr)
583         MOUSE_SIZE, 0,                          // wMaxPacketSize
584         MOUSE_INTERVAL,                         // bInterval
585
586 // --- Joystick Interface ---
587 // - 9 bytes -
588         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
589         9,                                      // bLength
590         4,                                      // bDescriptorType
591         JOYSTICK_INTERFACE,                     // bInterfaceNumber
592         0,                                      // bAlternateSetting
593         1,                                      // bNumEndpoints
594         0x03,                                   // bInterfaceClass (0x03 = HID)
595         0x00,                                   // bInterfaceSubClass
596         0x00,                                   // bInterfaceProtocol
597         JOYSTICK_INTERFACE + 4,                 // iInterface
598 // - 9 bytes -
599         // HID interface descriptor, HID 1.11 spec, section 6.2.1
600         9,                                      // bLength
601         0x21,                                   // bDescriptorType
602         0x11, 0x01,                             // bcdHID
603         0,                                      // bCountryCode
604         1,                                      // bNumDescriptors
605         0x22,                                   // bDescriptorType
606         LSB(sizeof(joystick_report_desc)),      // wDescriptorLength
607         MSB(sizeof(joystick_report_desc)),
608 // - 7 bytes -
609         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
610         7,                                      // bLength
611         5,                                      // bDescriptorType
612         JOYSTICK_ENDPOINT | 0x80,               // bEndpointAddress
613         0x03,                                   // bmAttributes (0x03=intr)
614         JOYSTICK_SIZE, 0,                       // wMaxPacketSize
615         JOYSTICK_INTERVAL,                      // bInterval
616
617 // --- System/Consumer Control ---
618 // - 9 bytes -
619         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
620         9,                                      // bLength
621         4,                                      // bDescriptorType
622         SYS_CTRL_INTERFACE,                     // bInterfaceNumber
623         0,                                      // bAlternateSetting
624         1,                                      // bNumEndpoints
625         0x03,                                   // bInterfaceClass (0x03 = HID)
626         0x01,                                   // bInterfaceSubClass (0x00 = Non-Boot, 0x01 = Boot)
627         0x00,                                   // bInterfaceProtocol (0x00 = None)
628         SYS_CTRL_INTERFACE + 4,                 // iInterface
629 // - 9 bytes -
630         // HID interface descriptor, HID 1.11 spec, section 6.2.1
631         9,                                      // bLength
632         0x21,                                   // bDescriptorType
633         0x11, 0x01,                             // bcdHID
634         KeyboardLocale_define,                  // bCountryCode
635         1,                                      // bNumDescriptors
636         0x22,                                   // bDescriptorType
637         LSB(sizeof(sys_ctrl_report_desc)),      // wDescriptorLength
638         MSB(sizeof(sys_ctrl_report_desc)),
639 // - 7 bytes -
640         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
641         7,                                      // bLength
642         5,                                      // bDescriptorType
643         SYS_CTRL_ENDPOINT | 0x80,               // bEndpointAddress
644         0x03,                                   // bmAttributes (0x03=intr)
645         SYS_CTRL_SIZE, 0,                       // wMaxPacketSize
646         SYS_CTRL_INTERVAL,                      // bInterval
647 };
648
649
650
651 // ----- String Descriptors -----
652
653 // The descriptors above can provide human readable strings,
654 // referenced by index numbers.  These descriptors are the
655 // actual string data
656
657 struct usb_string_descriptor_struct {
658         uint8_t bLength;
659         uint8_t bDescriptorType;
660         uint16_t wString[];
661 };
662
663 extern struct usb_string_descriptor_struct usb_string_manufacturer_name
664         __attribute__ ((weak, alias("usb_string_manufacturer_name_default")));
665 extern struct usb_string_descriptor_struct usb_string_product_name
666         __attribute__ ((weak, alias("usb_string_product_name_default")));
667 extern struct usb_string_descriptor_struct usb_string_serial_number
668         __attribute__ ((weak, alias("usb_string_serial_number_default")));
669
670 struct usb_string_descriptor_struct string0 = {
671         4,
672         3,
673         {0x0409}
674 };
675
676 #define usb_string_descriptor(name, str) \
677         struct usb_string_descriptor_struct name = { \
678                 sizeof(str), \
679                 3, \
680                 {str} \
681         }
682
683 usb_string_descriptor( usb_string_manufacturer_name_default, STR_MANUFACTURER );
684 usb_string_descriptor( usb_string_product_name_default, STR_PRODUCT );
685 usb_string_descriptor( usb_string_serial_number_default, STR_SERIAL );
686 usb_string_descriptor( usb_string_keyboard_name, KEYBOARD_NAME );
687 usb_string_descriptor( usb_string_nkro_keyboard_name, NKRO_KEYBOARD_NAME );
688 usb_string_descriptor( usb_string_cdc_status_name, CDC_STATUS_NAME );
689 usb_string_descriptor( usb_string_cdc_data_name, CDC_DATA_NAME );
690 usb_string_descriptor( usb_string_mouse_name, MOUSE_NAME );
691 usb_string_descriptor( usb_string_joystick_name, JOYSTICK_NAME );
692 usb_string_descriptor( usb_string_sys_ctrl_name, SYS_CTRL_NAME );
693
694
695
696 // ----- Descriptors List -----
697
698 // This table provides access to all the descriptor data above.
699
700 const usb_descriptor_list_t usb_descriptor_list[] = {
701         //wValue, wIndex, address,          length
702         {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
703         {0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
704         {0x0600, 0x0000, device_qualifier_descriptor, sizeof(device_qualifier_descriptor)},
705         {0x0A00, 0x0000, usb_debug_descriptor, sizeof(usb_debug_descriptor)},
706
707         {0x2200, KEYBOARD_INTERFACE, keyboard_report_desc, sizeof(keyboard_report_desc)},
708         {0x2100, KEYBOARD_INTERFACE, config_descriptor + KEYBOARD_DESC_OFFSET, 9},
709
710         {0x2200, NKRO_KEYBOARD_INTERFACE, nkro_keyboard_report_desc, sizeof(nkro_keyboard_report_desc)},
711         {0x2100, NKRO_KEYBOARD_INTERFACE, config_descriptor + NKRO_KEYBOARD_DESC_OFFSET, 9},
712
713         {0x2200, MOUSE_INTERFACE, mouse_report_desc, sizeof(mouse_report_desc)},
714         {0x2100, MOUSE_INTERFACE, config_descriptor + MOUSE_DESC_OFFSET, 9},
715
716         {0x2200, JOYSTICK_INTERFACE, joystick_report_desc, sizeof(joystick_report_desc)},
717         {0x2100, JOYSTICK_INTERFACE, config_descriptor + JOYSTICK_DESC_OFFSET, 9},
718
719         {0x2200, SYS_CTRL_INTERFACE, sys_ctrl_report_desc, sizeof(sys_ctrl_report_desc)},
720         {0x2100, SYS_CTRL_INTERFACE, config_descriptor + SYS_CTRL_DESC_OFFSET, 9},
721
722 #define iInterfaceString(num, var) \
723         {0x0300 + 4 + num, 0x409, (const uint8_t *)&var, 0 }
724
725         {0x0300, 0x0000, (const uint8_t *)&string0, 0},
726         {0x0301, 0x0409, (const uint8_t *)&usb_string_manufacturer_name, 0},
727         {0x0302, 0x0409, (const uint8_t *)&usb_string_product_name, 0},
728         {0x0303, 0x0409, (const uint8_t *)&usb_string_serial_number, 0},
729         iInterfaceString( KEYBOARD_INTERFACE, usb_string_keyboard_name ),
730         iInterfaceString( NKRO_KEYBOARD_INTERFACE, usb_string_nkro_keyboard_name ),
731         iInterfaceString( CDC_STATUS_INTERFACE, usb_string_cdc_status_name ),
732         iInterfaceString( CDC_DATA_INTERFACE, usb_string_cdc_data_name ),
733         iInterfaceString( MOUSE_INTERFACE, usb_string_mouse_name ),
734         iInterfaceString( JOYSTICK_INTERFACE, usb_string_joystick_name ),
735         iInterfaceString( SYS_CTRL_INTERFACE, usb_string_sys_ctrl_name ),
736         {0, 0, NULL, 0}
737 };
738
739
740
741 // ----- Endpoint Configuration -----
742
743 // See usb_desc.h for Endpoint configuration
744 // 0x00 = not used
745 // 0x19 = Recieve only
746 // 0x15 = Transmit only
747 // 0x1D = Transmit & Recieve
748 //
749 const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
750 {
751 #if (defined(ENDPOINT1_CONFIG) && NUM_ENDPOINTS >= 1)
752         ENDPOINT1_CONFIG,
753 #elif (NUM_ENDPOINTS >= 1)
754         ENDPOINT_UNUSED,
755 #endif
756 #if (defined(ENDPOINT2_CONFIG) && NUM_ENDPOINTS >= 2)
757         ENDPOINT2_CONFIG,
758 #elif (NUM_ENDPOINTS >= 2)
759         ENDPOINT_UNUSED,
760 #endif
761 #if (defined(ENDPOINT3_CONFIG) && NUM_ENDPOINTS >= 3)
762         ENDPOINT3_CONFIG,
763 #elif (NUM_ENDPOINTS >= 3)
764         ENDPOINT_UNUSED,
765 #endif
766 #if (defined(ENDPOINT4_CONFIG) && NUM_ENDPOINTS >= 4)
767         ENDPOINT4_CONFIG,
768 #elif (NUM_ENDPOINTS >= 4)
769         ENDPOINT_UNUSED,
770 #endif
771 #if (defined(ENDPOINT5_CONFIG) && NUM_ENDPOINTS >= 5)
772         ENDPOINT5_CONFIG,
773 #elif (NUM_ENDPOINTS >= 5)
774         ENDPOINT_UNUSED,
775 #endif
776 #if (defined(ENDPOINT6_CONFIG) && NUM_ENDPOINTS >= 6)
777         ENDPOINT6_CONFIG,
778 #elif (NUM_ENDPOINTS >= 6)
779         ENDPOINT_UNUSED,
780 #endif
781 #if (defined(ENDPOINT7_CONFIG) && NUM_ENDPOINTS >= 7)
782         ENDPOINT7_CONFIG,
783 #elif (NUM_ENDPOINTS >= 7)
784         ENDPOINT_UNUSED,
785 #endif
786 #if (defined(ENDPOINT8_CONFIG) && NUM_ENDPOINTS >= 8)
787         ENDPOINT8_CONFIG,
788 #elif (NUM_ENDPOINTS >= 8)
789         ENDPOINT_UNUSED,
790 #endif
791 #if (defined(ENDPOINT9_CONFIG) && NUM_ENDPOINTS >= 9)
792         ENDPOINT9_CONFIG,
793 #elif (NUM_ENDPOINTS >= 9)
794         ENDPOINT_UNUSED,
795 #endif
796 #if (defined(ENDPOINT10_CONFIG) && NUM_ENDPOINTS >= 10)
797         ENDPOINT10_CONFIG,
798 #elif (NUM_ENDPOINTS >= 10)
799         ENDPOINT_UNUSED,
800 #endif
801 #if (defined(ENDPOINT11_CONFIG) && NUM_ENDPOINTS >= 11)
802         ENDPOINT11_CONFIG,
803 #elif (NUM_ENDPOINTS >= 11)
804         ENDPOINT_UNUSED,
805 #endif
806 #if (defined(ENDPOINT12_CONFIG) && NUM_ENDPOINTS >= 12)
807         ENDPOINT12_CONFIG,
808 #elif (NUM_ENDPOINTS >= 12)
809         ENDPOINT_UNUSED,
810 #endif
811 #if (defined(ENDPOINT13_CONFIG) && NUM_ENDPOINTS >= 13)
812         ENDPOINT13_CONFIG,
813 #elif (NUM_ENDPOINTS >= 13)
814         ENDPOINT_UNUSED,
815 #endif
816 #if (defined(ENDPOINT14_CONFIG) && NUM_ENDPOINTS >= 14)
817         ENDPOINT14_CONFIG,
818 #elif (NUM_ENDPOINTS >= 14)
819         ENDPOINT_UNUSED,
820 #endif
821 #if (defined(ENDPOINT15_CONFIG) && NUM_ENDPOINTS >= 15)
822         ENDPOINT15_CONFIG,
823 #elif (NUM_ENDPOINTS >= 15)
824         ENDPOINT_UNUSED,
825 #endif
826 };
827
828