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