Add 'teensytp/' from commit 'be48e888050f18a31e788269c8f47358036a8e3b'
[megadrive.git] / teensytp / teensy3 / usb_desc.c
1 /* Teensyduino Core Library
2  * http://www.pjrc.com/teensy/
3  * Copyright (c) 2013 PJRC.COM, LLC.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * 1. The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * 2. If the Software is incorporated into a build system that allows
17  * selection among a list of target devices, then similar target
18  * devices manufactured by PJRC.COM must be included in the list of
19  * target devices and selectable in the same manner.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
25  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
26  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  */
30
31 #if F_CPU >= 20000000
32
33 #include "usb_desc.h"
34 #ifdef NUM_ENDPOINTS
35 #include "usb_names.h"
36 #include "kinetis.h"
37 #include "avr_functions.h"
38
39 // USB Descriptors are binary data which the USB host reads to
40 // automatically detect a USB device's capabilities.  The format
41 // and meaning of every field is documented in numerous USB
42 // standards.  When working with USB descriptors, despite the
43 // complexity of the standards and poor writing quality in many
44 // of those documents, remember descriptors are nothing more
45 // than constant binary data that tells the USB host what the
46 // device can do.  Computers will load drivers based on this data.
47 // Those drivers then communicate on the endpoints specified by
48 // the descriptors.
49
50 // To configure a new combination of interfaces or make minor
51 // changes to existing configuration (eg, change the name or ID
52 // numbers), usually you would edit "usb_desc.h".  This file
53 // is meant to be configured by the header, so generally it is
54 // only edited to add completely new USB interfaces or features.
55
56
57
58 // **************************************************************
59 //   USB Device
60 // **************************************************************
61
62 #define LSB(n) ((n) & 255)
63 #define MSB(n) (((n) >> 8) & 255)
64
65 // USB Device Descriptor.  The USB host reads this first, to learn
66 // what type of device is connected.
67 static uint8_t device_descriptor[] = {
68         18,                                     // bLength
69         1,                                      // bDescriptorType
70         0x00, 0x02,                             // bcdUSB
71 #ifdef DEVICE_CLASS
72         DEVICE_CLASS,                           // bDeviceClass
73 #else
74         0,
75 #endif
76 #ifdef DEVICE_SUBCLASS
77         DEVICE_SUBCLASS,                        // bDeviceSubClass
78 #else
79         0,
80 #endif
81 #ifdef DEVICE_PROTOCOL
82         DEVICE_PROTOCOL,                        // bDeviceProtocol
83 #else
84         0,
85 #endif
86         EP0_SIZE,                               // bMaxPacketSize0
87         LSB(VENDOR_ID), MSB(VENDOR_ID),         // idVendor
88         LSB(PRODUCT_ID), MSB(PRODUCT_ID),       // idProduct
89         0x00, 0x01,                             // bcdDevice
90         1,                                      // iManufacturer
91         2,                                      // iProduct
92         3,                                      // iSerialNumber
93         1                                       // bNumConfigurations
94 };
95
96 // These descriptors must NOT be "const", because the USB DMA
97 // has trouble accessing flash memory with enough bandwidth
98 // while the processor is executing from flash.
99
100
101
102 // **************************************************************
103 //   HID Report Descriptors
104 // **************************************************************
105
106 // Each HID interface needs a special report descriptor that tells
107 // the meaning and format of the data.
108
109 #ifdef KEYBOARD_INTERFACE
110 // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
111 static uint8_t keyboard_report_desc[] = {
112         0x05, 0x01,             //  Usage Page (Generic Desktop),
113         0x09, 0x06,             //  Usage (Keyboard),
114         0xA1, 0x01,             //  Collection (Application),
115         0x75, 0x01,             //  Report Size (1),
116         0x95, 0x08,             //  Report Count (8),
117         0x05, 0x07,             //  Usage Page (Key Codes),
118         0x19, 0xE0,             //  Usage Minimum (224),
119         0x29, 0xE7,             //  Usage Maximum (231),
120         0x15, 0x00,             //  Logical Minimum (0),
121         0x25, 0x01,             //  Logical Maximum (1),
122         0x81, 0x02,             //  Input (Data, Variable, Absolute), ;Modifier byte
123         0x95, 0x08,             //  Report Count (8),
124         0x75, 0x01,             //  Report Size (1),
125         0x15, 0x00,             //  Logical Minimum (0),
126         0x25, 0x01,             //  Logical Maximum (1),
127         0x05, 0x0C,             //  Usage Page (Consumer),
128         0x09, 0xE9,             //  Usage (Volume Increment),
129         0x09, 0xEA,             //  Usage (Volume Decrement),
130         0x09, 0xE2,             //  Usage (Mute),
131         0x09, 0xCD,             //  Usage (Play/Pause),
132         0x09, 0xB5,             //  Usage (Scan Next Track),
133         0x09, 0xB6,             //  Usage (Scan Previous Track),
134         0x09, 0xB7,             //  Usage (Stop),
135         0x09, 0xB8,             //  Usage (Eject),
136         0x81, 0x02,             //  Input (Data, Variable, Absolute), ;Media keys
137         0x95, 0x05,             //  Report Count (5),
138         0x75, 0x01,             //  Report Size (1),
139         0x05, 0x08,             //  Usage Page (LEDs),
140         0x19, 0x01,             //  Usage Minimum (1),
141         0x29, 0x05,             //  Usage Maximum (5),
142         0x91, 0x02,             //  Output (Data, Variable, Absolute), ;LED report
143         0x95, 0x01,             //  Report Count (1),
144         0x75, 0x03,             //  Report Size (3),
145         0x91, 0x03,             //  Output (Constant),                 ;LED report padding
146         0x95, 0x06,             //  Report Count (6),
147         0x75, 0x08,             //  Report Size (8),
148         0x15, 0x00,             //  Logical Minimum (0),
149         0x25, 0x7F,             //  Logical Maximum(104),
150         0x05, 0x07,             //  Usage Page (Key Codes),
151         0x19, 0x00,             //  Usage Minimum (0),
152         0x29, 0x7F,             //  Usage Maximum (104),
153         0x81, 0x00,             //  Input (Data, Array),                ;Normal keys
154         0xc0                    // End Collection
155 };
156 #endif
157
158 #ifdef MOUSE_INTERFACE
159 // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
160 static uint8_t mouse_report_desc[] = {
161         0x05, 0x01,                     // Usage Page (Generic Desktop)
162         0x09, 0x02,                     // Usage (Mouse)
163         0xA1, 0x01,                     // Collection (Application)
164         0x85, 0x01,                     //   REPORT_ID (1)
165         0x05, 0x09,                     //   Usage Page (Button)
166         0x19, 0x01,                     //   Usage Minimum (Button #1)
167         0x29, 0x08,                     //   Usage Maximum (Button #8)
168         0x15, 0x00,                     //   Logical Minimum (0)
169         0x25, 0x01,                     //   Logical Maximum (1)
170         0x95, 0x08,                     //   Report Count (8)
171         0x75, 0x01,                     //   Report Size (1)
172         0x81, 0x02,                     //   Input (Data, Variable, Absolute)
173         0x05, 0x01,                     //   Usage Page (Generic Desktop)
174         0x09, 0x30,                     //   Usage (X)
175         0x09, 0x31,                     //   Usage (Y)
176         0x09, 0x38,                     //   Usage (Wheel)
177         0x15, 0x81,                     //   Logical Minimum (-127)
178         0x25, 0x7F,                     //   Logical Maximum (127)
179         0x75, 0x08,                     //   Report Size (8),
180         0x95, 0x03,                     //   Report Count (3),
181         0x81, 0x06,                     //   Input (Data, Variable, Relative)
182         0xC0,                           // End Collection
183         0x05, 0x01,                     // Usage Page (Generic Desktop)
184         0x09, 0x02,                     // Usage (Mouse)
185         0xA1, 0x01,                     // Collection (Application)
186         0x85, 0x02,                     //   REPORT_ID (2)
187         0x05, 0x01,                     //   Usage Page (Generic Desktop)
188         0x09, 0x30,                     //   Usage (X)
189         0x09, 0x31,                     //   Usage (Y)
190         0x15, 0x00,                     //   Logical Minimum (0)
191         0x26, 0xFF, 0x7F,               //   Logical Maximum (32767)
192         0x75, 0x10,                     //   Report Size (16),
193         0x95, 0x02,                     //   Report Count (2),
194         0x81, 0x02,                     //   Input (Data, Variable, Absolute)
195         0xC0                            // End Collection
196 };
197 #endif
198
199 #ifdef JOYSTICK_INTERFACE
200 static uint8_t joystick_report_desc[] = {
201         0x05, 0x01,                     // Usage Page (Generic Desktop)
202         0x09, 0x04,                     // Usage (Joystick)
203         0xA1, 0x01,                     // Collection (Application)
204         0x15, 0x00,                     // Logical Minimum (0)
205         0x25, 0x01,                     // Logical Maximum (1)
206         0x75, 0x01,                     // Report Size (1)
207         0x95, 0x20,                     // Report Count (32)
208         0x05, 0x09,                     // Usage Page (Button)
209         0x19, 0x01,                     // Usage Minimum (Button #1)
210         0x29, 0x20,                     // Usage Maximum (Button #32)
211         0x81, 0x02,                     // Input (variable,absolute)
212         0x15, 0x00,                     // Logical Minimum (0)
213         0x25, 0x07,                     // Logical Maximum (7)
214         0x35, 0x00,                     // Physical Minimum (0)
215         0x46, 0x3B, 0x01,               // Physical Maximum (315)
216         0x75, 0x04,                     // Report Size (4)
217         0x95, 0x01,                     // Report Count (1)
218         0x65, 0x14,                     // Unit (20)
219         0x05, 0x01,                     // Usage Page (Generic Desktop)
220         0x09, 0x39,                     // Usage (Hat switch)
221         0x81, 0x42,                     // Input (variable,absolute,null_state)
222         0x05, 0x01,                     // Usage Page (Generic Desktop)
223         0x09, 0x01,                     // Usage (Pointer)
224         0xA1, 0x00,                     // Collection ()
225         0x15, 0x00,                     //   Logical Minimum (0)
226         0x26, 0xFF, 0x03,               //   Logical Maximum (1023)
227         0x75, 0x0A,                     //   Report Size (10)
228         0x95, 0x04,                     //   Report Count (4)
229         0x09, 0x30,                     //   Usage (X)
230         0x09, 0x31,                     //   Usage (Y)
231         0x09, 0x32,                     //   Usage (Z)
232         0x09, 0x35,                     //   Usage (Rz)
233         0x81, 0x02,                     //   Input (variable,absolute)
234         0xC0,                           // End Collection
235         0x15, 0x00,                     // Logical Minimum (0)
236         0x26, 0xFF, 0x03,               // Logical Maximum (1023)
237         0x75, 0x0A,                     // Report Size (10)
238         0x95, 0x02,                     // Report Count (2)
239         0x09, 0x36,                     // Usage (Slider)
240         0x09, 0x36,                     // Usage (Slider)
241         0x81, 0x02,                     // Input (variable,absolute)
242         0xC0                            // End Collection
243 };
244 #endif
245
246 #ifdef SEREMU_INTERFACE
247 static uint8_t seremu_report_desc[] = {
248         0x06, 0xC9, 0xFF,                       // Usage Page 0xFFC9 (vendor defined)
249         0x09, 0x04,                             // Usage 0x04
250         0xA1, 0x5C,                             // Collection 0x5C
251         0x75, 0x08,                             // report size = 8 bits (global)
252         0x15, 0x00,                             // logical minimum = 0 (global)
253         0x26, 0xFF, 0x00,                       // logical maximum = 255 (global)
254         0x95, SEREMU_TX_SIZE,                   // report count (global)
255         0x09, 0x75,                             // usage (local)
256         0x81, 0x02,                             // Input
257         0x95, SEREMU_RX_SIZE,                   // report count (global)
258         0x09, 0x76,                             // usage (local)
259         0x91, 0x02,                             // Output
260         0x95, 0x04,                             // report count (global)
261         0x09, 0x76,                             // usage (local)
262         0xB1, 0x02,                             // Feature
263         0xC0                                    // end collection
264 };
265 #endif
266
267 #ifdef RAWHID_INTERFACE
268 static uint8_t rawhid_report_desc[] = {
269         0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE),
270         0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
271         0xA1, 0x01,                             // Collection 0x01
272         0x75, 0x08,                             // report size = 8 bits
273         0x15, 0x00,                             // logical minimum = 0
274         0x26, 0xFF, 0x00,                       // logical maximum = 255
275         0x95, RAWHID_TX_SIZE,                   // report count
276         0x09, 0x01,                             // usage
277         0x81, 0x02,                             // Input (array)
278         0x95, RAWHID_RX_SIZE,                   // report count
279         0x09, 0x02,                             // usage
280         0x91, 0x02,                             // Output (array)
281         0xC0                                    // end collection
282 };
283 #endif
284
285 #ifdef FLIGHTSIM_INTERFACE
286 static uint8_t flightsim_report_desc[] = {
287         0x06, 0x1C, 0xFF,                       // Usage page = 0xFF1C
288         0x0A, 0x39, 0xA7,                       // Usage = 0xA739
289         0xA1, 0x01,                             // Collection 0x01
290         0x75, 0x08,                             // report size = 8 bits
291         0x15, 0x00,                             // logical minimum = 0
292         0x26, 0xFF, 0x00,                       // logical maximum = 255
293         0x95, FLIGHTSIM_TX_SIZE,                // report count
294         0x09, 0x01,                             // usage
295         0x81, 0x02,                             // Input (array)
296         0x95, FLIGHTSIM_RX_SIZE,                // report count
297         0x09, 0x02,                             // usage
298         0x91, 0x02,                             // Output (array)
299         0xC0                                    // end collection
300 };
301 #endif
302
303
304
305 // **************************************************************
306 //   USB Configuration
307 // **************************************************************
308
309 // USB Configuration Descriptor.  This huge descriptor tells all
310 // of the devices capbilities.
311 static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
312         // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
313         9,                                      // bLength;
314         2,                                      // bDescriptorType;
315         LSB(CONFIG_DESC_SIZE),                 // wTotalLength
316         MSB(CONFIG_DESC_SIZE),
317         NUM_INTERFACE,                          // bNumInterfaces
318         1,                                      // bConfigurationValue
319         0,                                      // iConfiguration
320         0xC0,                                   // bmAttributes
321         50,                                     // bMaxPower
322
323 #ifdef CDC_IAD_DESCRIPTOR
324         // interface association descriptor, USB ECN, Table 9-Z
325         8,                                      // bLength
326         11,                                     // bDescriptorType
327         CDC_STATUS_INTERFACE,                   // bFirstInterface
328         2,                                      // bInterfaceCount
329         0x02,                                   // bFunctionClass
330         0x02,                                   // bFunctionSubClass
331         0x01,                                   // bFunctionProtocol
332         4,                                      // iFunction
333 #endif
334
335 #ifdef CDC_DATA_INTERFACE
336         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
337         9,                                      // bLength
338         4,                                      // bDescriptorType
339         CDC_STATUS_INTERFACE,                   // bInterfaceNumber
340         0,                                      // bAlternateSetting
341         1,                                      // bNumEndpoints
342         0x02,                                   // bInterfaceClass
343         0x02,                                   // bInterfaceSubClass
344         0x01,                                   // bInterfaceProtocol
345         0,                                      // iInterface
346         // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
347         5,                                      // bFunctionLength
348         0x24,                                   // bDescriptorType
349         0x00,                                   // bDescriptorSubtype
350         0x10, 0x01,                             // bcdCDC
351         // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
352         5,                                      // bFunctionLength
353         0x24,                                   // bDescriptorType
354         0x01,                                   // bDescriptorSubtype
355         0x01,                                   // bmCapabilities
356         1,                                      // bDataInterface
357         // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
358         4,                                      // bFunctionLength
359         0x24,                                   // bDescriptorType
360         0x02,                                   // bDescriptorSubtype
361         0x06,                                   // bmCapabilities
362         // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
363         5,                                      // bFunctionLength
364         0x24,                                   // bDescriptorType
365         0x06,                                   // bDescriptorSubtype
366         CDC_STATUS_INTERFACE,                   // bMasterInterface
367         CDC_DATA_INTERFACE,                     // bSlaveInterface0
368         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
369         7,                                      // bLength
370         5,                                      // bDescriptorType
371         CDC_ACM_ENDPOINT | 0x80,                // bEndpointAddress
372         0x03,                                   // bmAttributes (0x03=intr)
373         CDC_ACM_SIZE, 0,                        // wMaxPacketSize
374         64,                                     // bInterval
375         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
376         9,                                      // bLength
377         4,                                      // bDescriptorType
378         CDC_DATA_INTERFACE,                     // bInterfaceNumber
379         0,                                      // bAlternateSetting
380         2,                                      // bNumEndpoints
381         0x0A,                                   // bInterfaceClass
382         0x00,                                   // bInterfaceSubClass
383         0x00,                                   // bInterfaceProtocol
384         0,                                      // iInterface
385         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
386         7,                                      // bLength
387         5,                                      // bDescriptorType
388         CDC_RX_ENDPOINT,                        // bEndpointAddress
389         0x02,                                   // bmAttributes (0x02=bulk)
390         CDC_RX_SIZE, 0,                         // wMaxPacketSize
391         0,                                      // bInterval
392         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
393         7,                                      // bLength
394         5,                                      // bDescriptorType
395         CDC_TX_ENDPOINT | 0x80,                 // bEndpointAddress
396         0x02,                                   // bmAttributes (0x02=bulk)
397         CDC_TX_SIZE, 0,                         // wMaxPacketSize
398         0,                                      // bInterval
399 #endif // CDC_DATA_INTERFACE
400
401 #ifdef MIDI_INTERFACE
402         // Standard MS Interface Descriptor,
403         9,                                      // bLength
404         4,                                      // bDescriptorType
405         MIDI_INTERFACE,                         // bInterfaceNumber
406         0,                                      // bAlternateSetting
407         2,                                      // bNumEndpoints
408         0x01,                                   // bInterfaceClass (0x01 = Audio)
409         0x03,                                   // bInterfaceSubClass (0x03 = MIDI)
410         0x00,                                   // bInterfaceProtocol (unused for MIDI)
411         0,                                      // iInterface
412         // MIDI MS Interface Header, USB MIDI 6.1.2.1, page 21, Table 6-2
413         7,                                      // bLength
414         0x24,                                   // bDescriptorType = CS_INTERFACE
415         0x01,                                   // bDescriptorSubtype = MS_HEADER
416         0x00, 0x01,                             // bcdMSC = revision 01.00
417         0x41, 0x00,                             // wTotalLength
418         // MIDI IN Jack Descriptor, B.4.3, Table B-7 (embedded), page 40
419         6,                                      // bLength
420         0x24,                                   // bDescriptorType = CS_INTERFACE
421         0x02,                                   // bDescriptorSubtype = MIDI_IN_JACK
422         0x01,                                   // bJackType = EMBEDDED
423         1,                                      // bJackID, ID = 1
424         0,                                      // iJack
425         // MIDI IN Jack Descriptor, B.4.3, Table B-8 (external), page 40
426         6,                                      // bLength
427         0x24,                                   // bDescriptorType = CS_INTERFACE
428         0x02,                                   // bDescriptorSubtype = MIDI_IN_JACK
429         0x02,                                   // bJackType = EXTERNAL
430         2,                                      // bJackID, ID = 2
431         0,                                      // iJack
432         // MIDI OUT Jack Descriptor, B.4.4, Table B-9, page 41
433         9,
434         0x24,                                   // bDescriptorType = CS_INTERFACE
435         0x03,                                   // bDescriptorSubtype = MIDI_OUT_JACK
436         0x01,                                   // bJackType = EMBEDDED
437         3,                                      // bJackID, ID = 3
438         1,                                      // bNrInputPins = 1 pin
439         2,                                      // BaSourceID(1) = 2
440         1,                                      // BaSourcePin(1) = first pin
441         0,                                      // iJack
442         // MIDI OUT Jack Descriptor, B.4.4, Table B-10, page 41
443         9,
444         0x24,                                   // bDescriptorType = CS_INTERFACE
445         0x03,                                   // bDescriptorSubtype = MIDI_OUT_JACK
446         0x02,                                   // bJackType = EXTERNAL
447         4,                                      // bJackID, ID = 4
448         1,                                      // bNrInputPins = 1 pin
449         1,                                      // BaSourceID(1) = 1
450         1,                                      // BaSourcePin(1) = first pin
451         0,                                      // iJack
452         // Standard Bulk OUT Endpoint Descriptor, B.5.1, Table B-11, pae 42
453         9,                                      // bLength
454         5,                                      // bDescriptorType = ENDPOINT
455         MIDI_RX_ENDPOINT,                       // bEndpointAddress
456         0x02,                                   // bmAttributes (0x02=bulk)
457         MIDI_RX_SIZE, 0,                        // wMaxPacketSize
458         0,                                      // bInterval
459         0,                                      // bRefresh
460         0,                                      // bSynchAddress
461         // Class-specific MS Bulk OUT Endpoint Descriptor, B.5.2, Table B-12, page 42
462         5,                                      // bLength
463         0x25,                                   // bDescriptorSubtype = CS_ENDPOINT
464         0x01,                                   // bJackType = MS_GENERAL
465         1,                                      // bNumEmbMIDIJack = 1 jack
466         1,                                      // BaAssocJackID(1) = jack ID #1
467         // Standard Bulk IN Endpoint Descriptor, B.5.1, Table B-11, pae 42
468         9,                                      // bLength
469         5,                                      // bDescriptorType = ENDPOINT
470         MIDI_TX_ENDPOINT | 0x80,                // bEndpointAddress
471         0x02,                                   // bmAttributes (0x02=bulk)
472         MIDI_TX_SIZE, 0,                        // wMaxPacketSize
473         0,                                      // bInterval
474         0,                                      // bRefresh
475         0,                                      // bSynchAddress
476         // Class-specific MS Bulk IN Endpoint Descriptor, B.5.2, Table B-12, page 42
477         5,                                      // bLength
478         0x25,                                   // bDescriptorSubtype = CS_ENDPOINT
479         0x01,                                   // bJackType = MS_GENERAL
480         1,                                      // bNumEmbMIDIJack = 1 jack
481         3,                                      // BaAssocJackID(1) = jack ID #3
482 #endif // MIDI_INTERFACE
483
484 #ifdef KEYBOARD_INTERFACE
485         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
486         9,                                      // bLength
487         4,                                      // bDescriptorType
488         KEYBOARD_INTERFACE,                     // bInterfaceNumber
489         0,                                      // bAlternateSetting
490         1,                                      // bNumEndpoints
491         0x03,                                   // bInterfaceClass (0x03 = HID)
492         0x01,                                   // bInterfaceSubClass (0x01 = Boot)
493         0x01,                                   // bInterfaceProtocol (0x01 = Keyboard)
494         0,                                      // iInterface
495         // HID interface descriptor, HID 1.11 spec, section 6.2.1
496         9,                                      // bLength
497         0x21,                                   // bDescriptorType
498         0x11, 0x01,                             // bcdHID
499         0,                                      // bCountryCode
500         1,                                      // bNumDescriptors
501         0x22,                                   // bDescriptorType
502         LSB(sizeof(keyboard_report_desc)),      // wDescriptorLength
503         MSB(sizeof(keyboard_report_desc)),
504         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
505         7,                                      // bLength
506         5,                                      // bDescriptorType
507         KEYBOARD_ENDPOINT | 0x80,               // bEndpointAddress
508         0x03,                                   // bmAttributes (0x03=intr)
509         KEYBOARD_SIZE, 0,                       // wMaxPacketSize
510         KEYBOARD_INTERVAL,                      // bInterval
511 #endif // KEYBOARD_INTERFACE
512
513 #ifdef MOUSE_INTERFACE
514         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
515         9,                                      // bLength
516         4,                                      // bDescriptorType
517         MOUSE_INTERFACE,                        // bInterfaceNumber
518         0,                                      // bAlternateSetting
519         1,                                      // bNumEndpoints
520         0x03,                                   // bInterfaceClass (0x03 = HID)
521         0x00,                                   // bInterfaceSubClass (0x01 = Boot)
522         0x00,                                   // bInterfaceProtocol (0x02 = Mouse)
523         0,                                      // iInterface
524         // HID interface descriptor, HID 1.11 spec, section 6.2.1
525         9,                                      // bLength
526         0x21,                                   // bDescriptorType
527         0x11, 0x01,                             // bcdHID
528         0,                                      // bCountryCode
529         1,                                      // bNumDescriptors
530         0x22,                                   // bDescriptorType
531         LSB(sizeof(mouse_report_desc)),         // wDescriptorLength
532         MSB(sizeof(mouse_report_desc)),
533         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
534         7,                                      // bLength
535         5,                                      // bDescriptorType
536         MOUSE_ENDPOINT | 0x80,                  // bEndpointAddress
537         0x03,                                   // bmAttributes (0x03=intr)
538         MOUSE_SIZE, 0,                          // wMaxPacketSize
539         MOUSE_INTERVAL,                         // bInterval
540 #endif // MOUSE_INTERFACE
541
542 #ifdef RAWHID_INTERFACE
543         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
544         9,                                      // bLength
545         4,                                      // bDescriptorType
546         RAWHID_INTERFACE,                       // bInterfaceNumber
547         0,                                      // bAlternateSetting
548         2,                                      // bNumEndpoints
549         0x03,                                   // bInterfaceClass (0x03 = HID)
550         0x00,                                   // bInterfaceSubClass
551         0x00,                                   // bInterfaceProtocol
552         0,                                      // iInterface
553         // HID interface descriptor, HID 1.11 spec, section 6.2.1
554         9,                                      // bLength
555         0x21,                                   // bDescriptorType
556         0x11, 0x01,                             // bcdHID
557         0,                                      // bCountryCode
558         1,                                      // bNumDescriptors
559         0x22,                                   // bDescriptorType
560         LSB(sizeof(rawhid_report_desc)),        // wDescriptorLength
561         MSB(sizeof(rawhid_report_desc)),
562         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
563         7,                                      // bLength
564         5,                                      // bDescriptorType
565         RAWHID_TX_ENDPOINT | 0x80,              // bEndpointAddress
566         0x03,                                   // bmAttributes (0x03=intr)
567         RAWHID_TX_SIZE, 0,                      // wMaxPacketSize
568         RAWHID_TX_INTERVAL,                     // bInterval
569         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
570         7,                                      // bLength
571         5,                                      // bDescriptorType
572         RAWHID_RX_ENDPOINT,                     // bEndpointAddress
573         0x03,                                   // bmAttributes (0x03=intr)
574         RAWHID_RX_SIZE, 0,                      // wMaxPacketSize
575         RAWHID_RX_INTERVAL,                     // bInterval
576 #endif // RAWHID_INTERFACE
577
578 #ifdef FLIGHTSIM_INTERFACE
579         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
580         9,                                      // bLength
581         4,                                      // bDescriptorType
582         FLIGHTSIM_INTERFACE,                    // bInterfaceNumber
583         0,                                      // bAlternateSetting
584         2,                                      // bNumEndpoints
585         0x03,                                   // bInterfaceClass (0x03 = HID)
586         0x00,                                   // bInterfaceSubClass
587         0x00,                                   // bInterfaceProtocol
588         0,                                      // iInterface
589         // HID interface descriptor, HID 1.11 spec, section 6.2.1
590         9,                                      // bLength
591         0x21,                                   // bDescriptorType
592         0x11, 0x01,                             // bcdHID
593         0,                                      // bCountryCode
594         1,                                      // bNumDescriptors
595         0x22,                                   // bDescriptorType
596         LSB(sizeof(flightsim_report_desc)),     // wDescriptorLength
597         MSB(sizeof(flightsim_report_desc)),
598         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
599         7,                                      // bLength
600         5,                                      // bDescriptorType
601         FLIGHTSIM_TX_ENDPOINT | 0x80,           // bEndpointAddress
602         0x03,                                   // bmAttributes (0x03=intr)
603         FLIGHTSIM_TX_SIZE, 0,                   // wMaxPacketSize
604         FLIGHTSIM_TX_INTERVAL,                  // bInterval
605         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
606         7,                                      // bLength
607         5,                                      // bDescriptorType
608         FLIGHTSIM_RX_ENDPOINT,                  // bEndpointAddress
609         0x03,                                   // bmAttributes (0x03=intr)
610         FLIGHTSIM_RX_SIZE, 0,                   // wMaxPacketSize
611         FLIGHTSIM_RX_INTERVAL,                  // bInterval
612 #endif // FLIGHTSIM_INTERFACE
613
614 #ifdef SEREMU_INTERFACE
615         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
616         9,                                      // bLength
617         4,                                      // bDescriptorType
618         SEREMU_INTERFACE,                       // bInterfaceNumber
619         0,                                      // bAlternateSetting
620         2,                                      // bNumEndpoints
621         0x03,                                   // bInterfaceClass (0x03 = HID)
622         0x00,                                   // bInterfaceSubClass
623         0x00,                                   // bInterfaceProtocol
624         0,                                      // iInterface
625         // HID interface descriptor, HID 1.11 spec, section 6.2.1
626         9,                                      // bLength
627         0x21,                                   // bDescriptorType
628         0x11, 0x01,                             // bcdHID
629         0,                                      // bCountryCode
630         1,                                      // bNumDescriptors
631         0x22,                                   // bDescriptorType
632         LSB(sizeof(seremu_report_desc)),        // wDescriptorLength
633         MSB(sizeof(seremu_report_desc)),
634         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
635         7,                                      // bLength
636         5,                                      // bDescriptorType
637         SEREMU_TX_ENDPOINT | 0x80,              // bEndpointAddress
638         0x03,                                   // bmAttributes (0x03=intr)
639         SEREMU_TX_SIZE, 0,                      // wMaxPacketSize
640         SEREMU_TX_INTERVAL,                     // bInterval
641         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
642         7,                                      // bLength
643         5,                                      // bDescriptorType
644         SEREMU_RX_ENDPOINT,                     // bEndpointAddress
645         0x03,                                   // bmAttributes (0x03=intr)
646         SEREMU_RX_SIZE, 0,                      // wMaxPacketSize
647         SEREMU_RX_INTERVAL,                     // bInterval
648 #endif // SEREMU_INTERFACE
649
650 #ifdef JOYSTICK_INTERFACE
651         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
652         9,                                      // bLength
653         4,                                      // bDescriptorType
654         JOYSTICK_INTERFACE,                     // bInterfaceNumber
655         0,                                      // bAlternateSetting
656         1,                                      // bNumEndpoints
657         0x03,                                   // bInterfaceClass (0x03 = HID)
658         0x00,                                   // bInterfaceSubClass
659         0x00,                                   // bInterfaceProtocol
660         0,                                      // iInterface
661         // HID interface descriptor, HID 1.11 spec, section 6.2.1
662         9,                                      // bLength
663         0x21,                                   // bDescriptorType
664         0x11, 0x01,                             // bcdHID
665         0,                                      // bCountryCode
666         1,                                      // bNumDescriptors
667         0x22,                                   // bDescriptorType
668         LSB(sizeof(joystick_report_desc)),      // wDescriptorLength
669         MSB(sizeof(joystick_report_desc)),
670         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
671         7,                                      // bLength
672         5,                                      // bDescriptorType
673         JOYSTICK_ENDPOINT | 0x80,               // bEndpointAddress
674         0x03,                                   // bmAttributes (0x03=intr)
675         JOYSTICK_SIZE, 0,                       // wMaxPacketSize
676         JOYSTICK_INTERVAL,                      // bInterval
677 #endif // JOYSTICK_INTERFACE
678
679 };
680
681
682 // **************************************************************
683 //   String Descriptors
684 // **************************************************************
685
686 // The descriptors above can provide human readable strings,
687 // referenced by index numbers.  These descriptors are the
688 // actual string data
689
690 /* defined in usb_names.h
691 struct usb_string_descriptor_struct {
692         uint8_t bLength;
693         uint8_t bDescriptorType;
694         uint16_t wString[];
695 };
696 */
697
698 extern struct usb_string_descriptor_struct usb_string_manufacturer_name
699         __attribute__ ((weak, alias("usb_string_manufacturer_name_default")));
700 extern struct usb_string_descriptor_struct usb_string_product_name
701         __attribute__ ((weak, alias("usb_string_product_name_default")));
702 extern struct usb_string_descriptor_struct usb_string_serial_number
703         __attribute__ ((weak, alias("usb_string_serial_number_default")));
704
705 struct usb_string_descriptor_struct string0 = {
706         4,
707         3,
708         {0x0409}
709 };
710
711 struct usb_string_descriptor_struct usb_string_manufacturer_name_default = {
712         2 + MANUFACTURER_NAME_LEN * 2,
713         3,
714         MANUFACTURER_NAME
715 };
716 struct usb_string_descriptor_struct usb_string_product_name_default = {
717         2 + PRODUCT_NAME_LEN * 2,
718         3,
719         PRODUCT_NAME
720 };
721 struct usb_string_descriptor_struct usb_string_serial_number_default = {
722         12,
723         3,
724         {0,0,0,0,0,0,0,0,0,0}
725 };
726
727 void usb_init_serialnumber(void)
728 {
729         char buf[11];
730         uint32_t i, num;
731
732         __disable_irq();
733         FTFL_FSTAT = FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL;
734         FTFL_FCCOB0 = 0x41;
735         FTFL_FCCOB1 = 15;
736         FTFL_FSTAT = FTFL_FSTAT_CCIF;
737         while (!(FTFL_FSTAT & FTFL_FSTAT_CCIF)) ; // wait
738         num = *(uint32_t *)&FTFL_FCCOB7;
739         __enable_irq();
740         // add extra zero to work around OS-X CDC-ACM driver bug
741         if (num < 10000000) num = num * 10;
742         ultoa(num, buf, 10);
743         for (i=0; i<10; i++) {
744                 char c = buf[i];
745                 if (!c) break;
746                 usb_string_serial_number_default.wString[i] = c;
747         }
748         usb_string_serial_number_default.bLength = i * 2 + 2;
749 }
750
751
752 // **************************************************************
753 //   Descriptors List
754 // **************************************************************
755
756 // This table provides access to all the descriptor data above.
757
758 const usb_descriptor_list_t usb_descriptor_list[] = {
759         //wValue, wIndex, address,          length
760         {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
761         {0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
762 #ifdef SEREMU_INTERFACE
763         {0x2200, SEREMU_INTERFACE, seremu_report_desc, sizeof(seremu_report_desc)},
764         {0x2100, SEREMU_INTERFACE, config_descriptor+SEREMU_DESC_OFFSET, 9},
765 #endif
766 #ifdef KEYBOARD_INTERFACE
767         {0x2200, KEYBOARD_INTERFACE, keyboard_report_desc, sizeof(keyboard_report_desc)},
768         {0x2100, KEYBOARD_INTERFACE, config_descriptor+KEYBOARD_DESC_OFFSET, 9},
769 #endif
770 #ifdef MOUSE_INTERFACE
771         {0x2200, MOUSE_INTERFACE, mouse_report_desc, sizeof(mouse_report_desc)},
772         {0x2100, MOUSE_INTERFACE, config_descriptor+MOUSE_DESC_OFFSET, 9},
773 #endif
774 #ifdef JOYSTICK_INTERFACE
775         {0x2200, JOYSTICK_INTERFACE, joystick_report_desc, sizeof(joystick_report_desc)},
776         {0x2100, JOYSTICK_INTERFACE, config_descriptor+JOYSTICK_DESC_OFFSET, 9},
777 #endif
778 #ifdef RAWHID_INTERFACE
779         {0x2200, RAWHID_INTERFACE, rawhid_report_desc, sizeof(rawhid_report_desc)},
780         {0x2100, RAWHID_INTERFACE, config_descriptor+RAWHID_DESC_OFFSET, 9},
781 #endif
782 #ifdef FLIGHTSIM_INTERFACE
783         {0x2200, FLIGHTSIM_INTERFACE, flightsim_report_desc, sizeof(flightsim_report_desc)},
784         {0x2100, FLIGHTSIM_INTERFACE, config_descriptor+FLIGHTSIM_DESC_OFFSET, 9},
785 #endif
786         {0x0300, 0x0000, (const uint8_t *)&string0, 0},
787         {0x0301, 0x0409, (const uint8_t *)&usb_string_manufacturer_name, 0},
788         {0x0302, 0x0409, (const uint8_t *)&usb_string_product_name, 0},
789         {0x0303, 0x0409, (const uint8_t *)&usb_string_serial_number, 0},
790         //{0x0301, 0x0409, (const uint8_t *)&string1, 0},
791         //{0x0302, 0x0409, (const uint8_t *)&string2, 0},
792         //{0x0303, 0x0409, (const uint8_t *)&string3, 0},
793         {0, 0, NULL, 0}
794 };
795
796
797 // **************************************************************
798 //   Endpoint Configuration
799 // **************************************************************
800
801 #if 0
802 // 0x00 = not used
803 // 0x19 = Recieve only
804 // 0x15 = Transmit only
805 // 0x1D = Transmit & Recieve
806 //
807 const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
808 {
809         0x00, 0x15, 0x19, 0x15, 0x00, 0x00, 0x00, 0x00,
810         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
811 };
812 #endif
813
814
815 const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
816 {
817 #if (defined(ENDPOINT1_CONFIG) && NUM_ENDPOINTS >= 1)
818         ENDPOINT1_CONFIG,
819 #elif (NUM_ENDPOINTS >= 1)
820         ENDPOINT_UNUSED,
821 #endif
822 #if (defined(ENDPOINT2_CONFIG) && NUM_ENDPOINTS >= 2)
823         ENDPOINT2_CONFIG,
824 #elif (NUM_ENDPOINTS >= 2)
825         ENDPOINT_UNUSED,
826 #endif
827 #if (defined(ENDPOINT3_CONFIG) && NUM_ENDPOINTS >= 3)
828         ENDPOINT3_CONFIG,
829 #elif (NUM_ENDPOINTS >= 3)
830         ENDPOINT_UNUSED,
831 #endif
832 #if (defined(ENDPOINT4_CONFIG) && NUM_ENDPOINTS >= 4)
833         ENDPOINT4_CONFIG,
834 #elif (NUM_ENDPOINTS >= 4)
835         ENDPOINT_UNUSED,
836 #endif
837 #if (defined(ENDPOINT5_CONFIG) && NUM_ENDPOINTS >= 5)
838         ENDPOINT5_CONFIG,
839 #elif (NUM_ENDPOINTS >= 5)
840         ENDPOINT_UNUSED,
841 #endif
842 #if (defined(ENDPOINT6_CONFIG) && NUM_ENDPOINTS >= 6)
843         ENDPOINT6_CONFIG,
844 #elif (NUM_ENDPOINTS >= 6)
845         ENDPOINT_UNUSED,
846 #endif
847 #if (defined(ENDPOINT7_CONFIG) && NUM_ENDPOINTS >= 7)
848         ENDPOINT7_CONFIG,
849 #elif (NUM_ENDPOINTS >= 7)
850         ENDPOINT_UNUSED,
851 #endif
852 #if (defined(ENDPOINT8_CONFIG) && NUM_ENDPOINTS >= 8)
853         ENDPOINT8_CONFIG,
854 #elif (NUM_ENDPOINTS >= 8)
855         ENDPOINT_UNUSED,
856 #endif
857 #if (defined(ENDPOINT9_CONFIG) && NUM_ENDPOINTS >= 9)
858         ENDPOINT9_CONFIG,
859 #elif (NUM_ENDPOINTS >= 9)
860         ENDPOINT_UNUSED,
861 #endif
862 #if (defined(ENDPOINT10_CONFIG) && NUM_ENDPOINTS >= 10)
863         ENDPOINT10_CONFIG,
864 #elif (NUM_ENDPOINTS >= 10)
865         ENDPOINT_UNUSED,
866 #endif
867 #if (defined(ENDPOINT11_CONFIG) && NUM_ENDPOINTS >= 11)
868         ENDPOINT11_CONFIG,
869 #elif (NUM_ENDPOINTS >= 11)
870         ENDPOINT_UNUSED,
871 #endif
872 #if (defined(ENDPOINT12_CONFIG) && NUM_ENDPOINTS >= 12)
873         ENDPOINT12_CONFIG,
874 #elif (NUM_ENDPOINTS >= 12)
875         ENDPOINT_UNUSED,
876 #endif
877 #if (defined(ENDPOINT13_CONFIG) && NUM_ENDPOINTS >= 13)
878         ENDPOINT13_CONFIG,
879 #elif (NUM_ENDPOINTS >= 13)
880         ENDPOINT_UNUSED,
881 #endif
882 #if (defined(ENDPOINT14_CONFIG) && NUM_ENDPOINTS >= 14)
883         ENDPOINT14_CONFIG,
884 #elif (NUM_ENDPOINTS >= 14)
885         ENDPOINT_UNUSED,
886 #endif
887 #if (defined(ENDPOINT15_CONFIG) && NUM_ENDPOINTS >= 15)
888         ENDPOINT15_CONFIG,
889 #elif (NUM_ENDPOINTS >= 15)
890         ENDPOINT_UNUSED,
891 #endif
892 };
893
894
895 #endif // NUM_ENDPOINTS
896 #endif // F_CPU >= 20 MHz