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