]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Output/pjrcUSB/arm/usb_desc.c
Adding CLI and CDC Serial support for Teensy 2.0 and Teensy 2.0++
[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-2014)
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 #include "usb_desc.h"
33
34
35 // USB Descriptors are binary data which the USB host reads to
36 // automatically detect a USB device's capabilities.  The format
37 // and meaning of every field is documented in numerous USB
38 // standards.  When working with USB descriptors, despite the
39 // complexity of the standards and poor writing quality in many
40 // of those documents, remember descriptors are nothing more
41 // than constant binary data that tells the USB host what the
42 // device can do.  Computers will load drivers based on this data.
43 // Those drivers then communicate on the endpoints specified by
44 // the descriptors.
45
46 // To configure a new combination of interfaces or make minor
47 // changes to existing configuration (eg, change the name or ID
48 // numbers), usually you would edit "usb_desc.h".  This file
49 // is meant to be configured by the header, so generally it is
50 // only edited to add completely new USB interfaces or features.
51
52
53
54 // **************************************************************
55 //   USB Device
56 // **************************************************************
57
58 #define LSB(n) ((n) & 255)
59 #define MSB(n) (((n) >> 8) & 255)
60
61 // USB Device Descriptor.  The USB host reads this first, to learn
62 // what type of device is connected.
63 static uint8_t device_descriptor[] = {
64         18,                                     // bLength
65         1,                                      // bDescriptorType
66         0x00, 0x02,                             // bcdUSB
67 #ifdef DEVICE_CLASS
68         DEVICE_CLASS,                           // bDeviceClass
69 #else
70         0,
71 #endif
72 #ifdef DEVICE_SUBCLASS
73         DEVICE_SUBCLASS,                        // bDeviceSubClass
74 #else
75         0,
76 #endif
77 #ifdef DEVICE_PROTOCOL
78         DEVICE_PROTOCOL,                        // bDeviceProtocol
79 #else
80         0,
81 #endif
82         EP0_SIZE,                               // bMaxPacketSize0
83         LSB(VENDOR_ID), MSB(VENDOR_ID),         // idVendor
84         LSB(PRODUCT_ID), MSB(PRODUCT_ID),       // idProduct
85         0x00, 0x01,                             // bcdDevice
86         1,                                      // iManufacturer
87         2,                                      // iProduct
88         3,                                      // iSerialNumber
89         1                                       // bNumConfigurations
90 };
91
92 // These descriptors must NOT be "const", because the USB DMA
93 // has trouble accessing flash memory with enough bandwidth
94 // while the processor is executing from flash.
95
96
97
98 // **************************************************************
99 //   HID Report Descriptors
100 // **************************************************************
101
102 // Each HID interface needs a special report descriptor that tells
103 // the meaning and format of the data.
104
105 #ifdef KEYBOARD_INTERFACE
106 // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
107 static uint8_t keyboard_report_desc[] = {
108         0x05, 0x01,             //  Usage Page (Generic Desktop),
109         0x09, 0x06,             //  Usage (Keyboard),
110         0xA1, 0x01,             //  Collection (Application),
111         0x75, 0x01,             //  Report Size (1),
112         0x95, 0x08,             //  Report Count (8),
113         0x05, 0x07,             //  Usage Page (Key Codes),
114         0x19, 0xE0,             //  Usage Minimum (224),
115         0x29, 0xE7,             //  Usage Maximum (231),
116         0x15, 0x00,             //  Logical Minimum (0),
117         0x25, 0x01,             //  Logical Maximum (1),
118         0x81, 0x02,             //  Input (Data, Variable, Absolute), ;Modifier byte
119         0x95, 0x08,             //  Report Count (8),
120         0x75, 0x01,             //  Report Size (1),
121         0x15, 0x00,             //  Logical Minimum (0),
122         0x25, 0x01,             //  Logical Maximum (1),
123         0x05, 0x0C,             //  Usage Page (Consumer),
124         0x09, 0xE9,             //  Usage (Volume Increment),
125         0x09, 0xEA,             //  Usage (Volume Decrement),
126         0x09, 0xE2,             //  Usage (Mute),
127         0x09, 0xCD,             //  Usage (Play/Pause),
128         0x09, 0xB5,             //  Usage (Scan Next Track),
129         0x09, 0xB6,             //  Usage (Scan Previous Track),
130         0x09, 0xB7,             //  Usage (Stop),
131         0x09, 0xB8,             //  Usage (Eject),
132         0x81, 0x02,             //  Input (Data, Variable, Absolute), ;Media keys
133         0x95, 0x05,             //  Report Count (5),
134         0x75, 0x01,             //  Report Size (1),
135         0x05, 0x08,             //  Usage Page (LEDs),
136         0x19, 0x01,             //  Usage Minimum (1),
137         0x29, 0x05,             //  Usage Maximum (5),
138         0x91, 0x02,             //  Output (Data, Variable, Absolute), ;LED report
139         0x95, 0x01,             //  Report Count (1),
140         0x75, 0x03,             //  Report Size (3),
141         0x91, 0x03,             //  Output (Constant),                 ;LED report padding
142         0x95, 0x06,             //  Report Count (6),
143         0x75, 0x08,             //  Report Size (8),
144         0x15, 0x00,             //  Logical Minimum (0),
145         0x25, 0x7F,             //  Logical Maximum(104),
146         0x05, 0x07,             //  Usage Page (Key Codes),
147         0x19, 0x00,             //  Usage Minimum (0),
148         0x29, 0x7F,             //  Usage Maximum (104),
149         0x81, 0x00,             //  Input (Data, Array),                ;Normal keys
150         0xc0                    // End Collection
151 };
152 #endif
153
154 #ifdef MOUSE_INTERFACE
155 // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
156 static uint8_t mouse_report_desc[] = {
157         0x05, 0x01,                     // Usage Page (Generic Desktop)
158         0x09, 0x02,                     // Usage (Mouse)
159         0xA1, 0x01,                     // Collection (Application)
160         0x05, 0x09,                     //   Usage Page (Button)
161         0x19, 0x01,                     //   Usage Minimum (Button #1)
162         0x29, 0x03,                     //   Usage Maximum (Button #3)
163         0x15, 0x00,                     //   Logical Minimum (0)
164         0x25, 0x01,                     //   Logical Maximum (1)
165         0x95, 0x03,                     //   Report Count (3)
166         0x75, 0x01,                     //   Report Size (1)
167         0x81, 0x02,                     //   Input (Data, Variable, Absolute)
168         0x95, 0x01,                     //   Report Count (1)
169         0x75, 0x05,                     //   Report Size (5)
170         0x81, 0x03,                     //   Input (Constant)
171         0x05, 0x01,                     //   Usage Page (Generic Desktop)
172         0x09, 0x30,                     //   Usage (X)
173         0x09, 0x31,                     //   Usage (Y)
174         0x15, 0x00,                     //   Logical Minimum (0)
175         0x26, 0xFF, 0x7F,               //   Logical Maximum (32767)
176         0x75, 0x10,                     //   Report Size (16),
177         0x95, 0x02,                     //   Report Count (2),
178         0x81, 0x02,                     //   Input (Data, Variable, Absolute)
179         0x09, 0x38,                     //   Usage (Wheel)
180         0x15, 0x81,                     //   Logical Minimum (-127)
181         0x25, 0x7F,                     //   Logical Maximum (127)
182         0x75, 0x08,                     //   Report Size (8),
183         0x95, 0x01,                     //   Report Count (1),
184         0x81, 0x06,                     //   Input (Data, Variable, Relative)
185         0xC0                            // End Collection
186 };
187 #endif
188
189
190
191 // **************************************************************
192 //   USB Configuration
193 // **************************************************************
194
195 // USB Configuration Descriptor.  This huge descriptor tells all
196 // of the devices capbilities.
197 static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
198         // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
199         9,                                      // bLength;
200         2,                                      // bDescriptorType;
201         LSB(CONFIG_DESC_SIZE),                 // wTotalLength
202         MSB(CONFIG_DESC_SIZE),
203         NUM_INTERFACE,                          // bNumInterfaces
204         1,                                      // bConfigurationValue
205         0,                                      // iConfiguration
206         0xC0,                                   // bmAttributes
207         50,                                     // bMaxPower
208
209 #ifdef CDC_IAD_DESCRIPTOR
210         // interface association descriptor, USB ECN, Table 9-Z
211         8,                                      // bLength
212         11,                                     // bDescriptorType
213         CDC_STATUS_INTERFACE,                   // bFirstInterface
214         2,                                      // bInterfaceCount
215         0x02,                                   // bFunctionClass
216         0x02,                                   // bFunctionSubClass
217         0x01,                                   // bFunctionProtocol
218         4,                                      // iFunction
219 #endif
220
221 #ifdef CDC_DATA_INTERFACE
222         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
223         9,                                      // bLength
224         4,                                      // bDescriptorType
225         CDC_STATUS_INTERFACE,                   // bInterfaceNumber
226         0,                                      // bAlternateSetting
227         1,                                      // bNumEndpoints
228         0x02,                                   // bInterfaceClass
229         0x02,                                   // bInterfaceSubClass
230         0x01,                                   // bInterfaceProtocol
231         0,                                      // iInterface
232         // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
233         5,                                      // bFunctionLength
234         0x24,                                   // bDescriptorType
235         0x00,                                   // bDescriptorSubtype
236         0x10, 0x01,                             // bcdCDC
237         // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
238         5,                                      // bFunctionLength
239         0x24,                                   // bDescriptorType
240         0x01,                                   // bDescriptorSubtype
241         0x01,                                   // bmCapabilities
242         1,                                      // bDataInterface
243         // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
244         4,                                      // bFunctionLength
245         0x24,                                   // bDescriptorType
246         0x02,                                   // bDescriptorSubtype
247         0x06,                                   // bmCapabilities
248         // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
249         5,                                      // bFunctionLength
250         0x24,                                   // bDescriptorType
251         0x06,                                   // bDescriptorSubtype
252         CDC_STATUS_INTERFACE,                   // bMasterInterface
253         CDC_DATA_INTERFACE,                     // bSlaveInterface0
254         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
255         7,                                      // bLength
256         5,                                      // bDescriptorType
257         CDC_ACM_ENDPOINT | 0x80,                // bEndpointAddress
258         0x03,                                   // bmAttributes (0x03=intr)
259         CDC_ACM_SIZE, 0,                        // wMaxPacketSize
260         64,                                     // bInterval
261         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
262         9,                                      // bLength
263         4,                                      // bDescriptorType
264         CDC_DATA_INTERFACE,                     // bInterfaceNumber
265         0,                                      // bAlternateSetting
266         2,                                      // bNumEndpoints
267         0x0A,                                   // bInterfaceClass
268         0x00,                                   // bInterfaceSubClass
269         0x00,                                   // bInterfaceProtocol
270         0,                                      // iInterface
271         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
272         7,                                      // bLength
273         5,                                      // bDescriptorType
274         CDC_RX_ENDPOINT,                        // bEndpointAddress
275         0x02,                                   // bmAttributes (0x02=bulk)
276         CDC_RX_SIZE, 0,                         // wMaxPacketSize
277         0,                                      // bInterval
278         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
279         7,                                      // bLength
280         5,                                      // bDescriptorType
281         CDC_TX_ENDPOINT | 0x80,                 // bEndpointAddress
282         0x02,                                   // bmAttributes (0x02=bulk)
283         CDC_TX_SIZE, 0,                         // wMaxPacketSize
284         0,                                      // bInterval
285 #endif // CDC_DATA_INTERFACE
286
287 #ifdef KEYBOARD_INTERFACE
288         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
289         9,                                      // bLength
290         4,                                      // bDescriptorType
291         KEYBOARD_INTERFACE,                     // bInterfaceNumber
292         0,                                      // bAlternateSetting
293         1,                                      // bNumEndpoints
294         0x03,                                   // bInterfaceClass (0x03 = HID)
295         0x01,                                   // bInterfaceSubClass (0x01 = Boot)
296         0x01,                                   // bInterfaceProtocol (0x01 = Keyboard)
297         0,                                      // iInterface
298         // HID interface descriptor, HID 1.11 spec, section 6.2.1
299         9,                                      // bLength
300         0x21,                                   // bDescriptorType
301         0x11, 0x01,                             // bcdHID
302         0,                                      // bCountryCode
303         1,                                      // bNumDescriptors
304         0x22,                                   // bDescriptorType
305         LSB(sizeof(keyboard_report_desc)),      // wDescriptorLength
306         MSB(sizeof(keyboard_report_desc)),
307         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
308         7,                                      // bLength
309         5,                                      // bDescriptorType
310         KEYBOARD_ENDPOINT | 0x80,               // bEndpointAddress
311         0x03,                                   // bmAttributes (0x03=intr)
312         KEYBOARD_SIZE, 0,                       // wMaxPacketSize
313         KEYBOARD_INTERVAL,                      // bInterval
314 #endif // KEYBOARD_INTERFACE
315
316 #ifdef MOUSE_INTERFACE
317         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
318         9,                                      // bLength
319         4,                                      // bDescriptorType
320         MOUSE_INTERFACE,                        // bInterfaceNumber
321         0,                                      // bAlternateSetting
322         1,                                      // bNumEndpoints
323         0x03,                                   // bInterfaceClass (0x03 = HID)
324         0x00,                                   // bInterfaceSubClass (0x01 = Boot)
325         0x00,                                   // bInterfaceProtocol (0x02 = Mouse)
326         0,                                      // iInterface
327         // HID interface descriptor, HID 1.11 spec, section 6.2.1
328         9,                                      // bLength
329         0x21,                                   // bDescriptorType
330         0x11, 0x01,                             // bcdHID
331         0,                                      // bCountryCode
332         1,                                      // bNumDescriptors
333         0x22,                                   // bDescriptorType
334         LSB(sizeof(mouse_report_desc)),         // wDescriptorLength
335         MSB(sizeof(mouse_report_desc)),
336         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
337         7,                                      // bLength
338         5,                                      // bDescriptorType
339         MOUSE_ENDPOINT | 0x80,                  // bEndpointAddress
340         0x03,                                   // bmAttributes (0x03=intr)
341         MOUSE_SIZE, 0,                          // wMaxPacketSize
342         MOUSE_INTERVAL,                         // bInterval
343 #endif // MOUSE_INTERFACE
344 };
345
346
347
348 // **************************************************************
349 //   String Descriptors
350 // **************************************************************
351
352 // The descriptors above can provide human readable strings,
353 // referenced by index numbers.  These descriptors are the
354 // actual string data
355
356 struct usb_string_descriptor_struct {
357         uint8_t bLength;
358         uint8_t bDescriptorType;
359         uint16_t wString[];
360 };
361
362 extern struct usb_string_descriptor_struct usb_string_manufacturer_name
363         __attribute__ ((weak, alias("usb_string_manufacturer_name_default")));
364 extern struct usb_string_descriptor_struct usb_string_product_name
365         __attribute__ ((weak, alias("usb_string_product_name_default")));
366 extern struct usb_string_descriptor_struct usb_string_serial_number
367         __attribute__ ((weak, alias("usb_string_serial_number_default")));
368
369 struct usb_string_descriptor_struct string0 = {
370         4,
371         3,
372         {0x0409}
373 };
374
375 struct usb_string_descriptor_struct usb_string_manufacturer_name_default = {
376         sizeof(STR_MANUFACTURER),
377         3,
378         STR_MANUFACTURER
379 };
380 struct usb_string_descriptor_struct usb_string_product_name_default = {
381         sizeof(STR_PRODUCT),
382         3,
383         STR_PRODUCT
384 };
385 struct usb_string_descriptor_struct usb_string_serial_number_default = {
386         sizeof(STR_SERIAL),
387         3,
388         STR_SERIAL
389 };
390
391
392 // **************************************************************
393 //   Descriptors List
394 // **************************************************************
395
396 // This table provides access to all the descriptor data above.
397
398 const usb_descriptor_list_t usb_descriptor_list[] = {
399         //wValue, wIndex, address,          length
400         {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
401         {0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
402 #ifdef KEYBOARD_INTERFACE
403         {0x2200, KEYBOARD_INTERFACE, keyboard_report_desc, sizeof(keyboard_report_desc)},
404         {0x2100, KEYBOARD_INTERFACE, config_descriptor+KEYBOARD_DESC_OFFSET, 9},
405 #endif
406 #ifdef MOUSE_INTERFACE
407         {0x2200, MOUSE_INTERFACE, mouse_report_desc, sizeof(mouse_report_desc)},
408         {0x2100, MOUSE_INTERFACE, config_descriptor+MOUSE_DESC_OFFSET, 9},
409 #endif
410         {0x0300, 0x0000, (const uint8_t *)&string0, 0},
411         {0x0301, 0x0409, (const uint8_t *)&usb_string_manufacturer_name, 0},
412         {0x0302, 0x0409, (const uint8_t *)&usb_string_product_name, 0},
413         {0x0303, 0x0409, (const uint8_t *)&usb_string_serial_number, 0},
414         {0, 0, NULL, 0}
415 };
416
417
418 // **************************************************************
419 //   Endpoint Configuration
420 // **************************************************************
421
422 #if 0
423 // 0x00 = not used
424 // 0x19 = Recieve only
425 // 0x15 = Transmit only
426 // 0x1D = Transmit & Recieve
427 // 
428 const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] = 
429 {
430         0x00, 0x15, 0x19, 0x15, 0x00, 0x00, 0x00, 0x00, 
431         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
432 };
433 #endif
434
435
436 const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] = 
437 {
438 #if (defined(ENDPOINT1_CONFIG) && NUM_ENDPOINTS >= 1)
439         ENDPOINT1_CONFIG,
440 #elif (NUM_ENDPOINTS >= 1)
441         ENDPOINT_UNUSED,
442 #endif
443 #if (defined(ENDPOINT2_CONFIG) && NUM_ENDPOINTS >= 2)
444         ENDPOINT2_CONFIG,
445 #elif (NUM_ENDPOINTS >= 2)
446         ENDPOINT_UNUSED,
447 #endif
448 #if (defined(ENDPOINT3_CONFIG) && NUM_ENDPOINTS >= 3)
449         ENDPOINT3_CONFIG,
450 #elif (NUM_ENDPOINTS >= 3)
451         ENDPOINT_UNUSED,
452 #endif
453 #if (defined(ENDPOINT4_CONFIG) && NUM_ENDPOINTS >= 4)
454         ENDPOINT4_CONFIG,
455 #elif (NUM_ENDPOINTS >= 4)
456         ENDPOINT_UNUSED,
457 #endif
458 #if (defined(ENDPOINT5_CONFIG) && NUM_ENDPOINTS >= 5)
459         ENDPOINT5_CONFIG,
460 #elif (NUM_ENDPOINTS >= 5)
461         ENDPOINT_UNUSED,
462 #endif
463 #if (defined(ENDPOINT6_CONFIG) && NUM_ENDPOINTS >= 6)
464         ENDPOINT6_CONFIG,
465 #elif (NUM_ENDPOINTS >= 6)
466         ENDPOINT_UNUSED,
467 #endif
468 #if (defined(ENDPOINT7_CONFIG) && NUM_ENDPOINTS >= 7)
469         ENDPOINT7_CONFIG,
470 #elif (NUM_ENDPOINTS >= 7)
471         ENDPOINT_UNUSED,
472 #endif
473 #if (defined(ENDPOINT8_CONFIG) && NUM_ENDPOINTS >= 8)
474         ENDPOINT8_CONFIG,
475 #elif (NUM_ENDPOINTS >= 8)
476         ENDPOINT_UNUSED,
477 #endif
478 #if (defined(ENDPOINT9_CONFIG) && NUM_ENDPOINTS >= 9)
479         ENDPOINT9_CONFIG,
480 #elif (NUM_ENDPOINTS >= 9)
481         ENDPOINT_UNUSED,
482 #endif
483 #if (defined(ENDPOINT10_CONFIG) && NUM_ENDPOINTS >= 10)
484         ENDPOINT10_CONFIG,
485 #elif (NUM_ENDPOINTS >= 10)
486         ENDPOINT_UNUSED,
487 #endif
488 #if (defined(ENDPOINT11_CONFIG) && NUM_ENDPOINTS >= 11)
489         ENDPOINT11_CONFIG,
490 #elif (NUM_ENDPOINTS >= 11)
491         ENDPOINT_UNUSED,
492 #endif
493 #if (defined(ENDPOINT12_CONFIG) && NUM_ENDPOINTS >= 12)
494         ENDPOINT12_CONFIG,
495 #elif (NUM_ENDPOINTS >= 12)
496         ENDPOINT_UNUSED,
497 #endif
498 #if (defined(ENDPOINT13_CONFIG) && NUM_ENDPOINTS >= 13)
499         ENDPOINT13_CONFIG,
500 #elif (NUM_ENDPOINTS >= 13)
501         ENDPOINT_UNUSED,
502 #endif
503 #if (defined(ENDPOINT14_CONFIG) && NUM_ENDPOINTS >= 14)
504         ENDPOINT14_CONFIG,
505 #elif (NUM_ENDPOINTS >= 14)
506         ENDPOINT_UNUSED,
507 #endif
508 #if (defined(ENDPOINT15_CONFIG) && NUM_ENDPOINTS >= 15)
509         ENDPOINT15_CONFIG,
510 #elif (NUM_ENDPOINTS >= 15)
511         ENDPOINT_UNUSED,
512 #endif
513 };
514