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
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.
23 // **************************************************************
25 // **************************************************************
27 #define LSB(n) ((n) & 255)
28 #define MSB(n) (((n) >> 8) & 255)
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[] = {
37 DEVICE_CLASS, // bDeviceClass
41 #ifdef DEVICE_SUBCLASS
42 DEVICE_SUBCLASS, // bDeviceSubClass
46 #ifdef DEVICE_PROTOCOL
47 DEVICE_PROTOCOL, // bDeviceProtocol
51 EP0_SIZE, // bMaxPacketSize0
52 LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
53 LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
54 0x00, 0x01, // bcdDevice
58 1 // bNumConfigurations
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.
67 // **************************************************************
68 // HID Report Descriptors
69 // **************************************************************
71 // Each HID interface needs a special report descriptor that tells
72 // the meaning and format of the data.
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
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
155 #ifdef JOYSTICK_INTERFACE
156 static uint8_t joystick_report_desc[] = {
157 0x05, 0x01, // Usage Page (Generic Desktop)
158 0x09, 0x04, // Usage (Joystick)
159 0xA1, 0x01, // Collection (Application)
160 0x15, 0x00, // Logical Minimum (0)
161 0x25, 0x01, // Logical Maximum (1)
162 0x75, 0x01, // Report Size (1)
163 0x95, 0x20, // Report Count (32)
164 0x05, 0x09, // Usage Page (Button)
165 0x19, 0x01, // Usage Minimum (Button #1)
166 0x29, 0x20, // Usage Maximum (Button #32)
167 0x81, 0x02, // Input (variable,absolute)
168 0x15, 0x00, // Logical Minimum (0)
169 0x25, 0x07, // Logical Maximum (7)
170 0x35, 0x00, // Physical Minimum (0)
171 0x46, 0x3B, 0x01, // Physical Maximum (315)
172 0x75, 0x04, // Report Size (4)
173 0x95, 0x01, // Report Count (1)
174 0x65, 0x14, // Unit (20)
175 0x05, 0x01, // Usage Page (Generic Desktop)
176 0x09, 0x39, // Usage (Hat switch)
177 0x81, 0x42, // Input (variable,absolute,null_state)
178 0x05, 0x01, // Usage Page (Generic Desktop)
179 0x09, 0x01, // Usage (Pointer)
180 0xA1, 0x00, // Collection ()
181 0x15, 0x00, // Logical Minimum (0)
182 0x26, 0xFF, 0x03, // Logical Maximum (1023)
183 0x75, 0x0A, // Report Size (10)
184 0x95, 0x04, // Report Count (4)
185 0x09, 0x30, // Usage (X)
186 0x09, 0x31, // Usage (Y)
187 0x09, 0x32, // Usage (Z)
188 0x09, 0x35, // Usage (Rz)
189 0x81, 0x02, // Input (variable,absolute)
190 0xC0, // End Collection
191 0x15, 0x00, // Logical Minimum (0)
192 0x26, 0xFF, 0x03, // Logical Maximum (1023)
193 0x75, 0x0A, // Report Size (10)
194 0x95, 0x02, // Report Count (2)
195 0x09, 0x36, // Usage (Slider)
196 0x09, 0x36, // Usage (Slider)
197 0x81, 0x02, // Input (variable,absolute)
198 0xC0 // End Collection
204 // **************************************************************
206 // **************************************************************
208 // USB Configuration Descriptor. This huge descriptor tells all
209 // of the devices capbilities.
210 static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
211 // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
213 2, // bDescriptorType;
214 LSB(CONFIG_DESC_SIZE), // wTotalLength
215 MSB(CONFIG_DESC_SIZE),
216 NUM_INTERFACE, // bNumInterfaces
217 1, // bConfigurationValue
219 0xC0, // bmAttributes
222 #ifdef CDC_IAD_DESCRIPTOR
223 // interface association descriptor, USB ECN, Table 9-Z
225 11, // bDescriptorType
226 CDC_STATUS_INTERFACE, // bFirstInterface
227 2, // bInterfaceCount
228 0x02, // bFunctionClass
229 0x02, // bFunctionSubClass
230 0x01, // bFunctionProtocol
234 #ifdef CDC_DATA_INTERFACE
235 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
237 4, // bDescriptorType
238 CDC_STATUS_INTERFACE, // bInterfaceNumber
239 0, // bAlternateSetting
241 0x02, // bInterfaceClass
242 0x02, // bInterfaceSubClass
243 0x01, // bInterfaceProtocol
245 // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
246 5, // bFunctionLength
247 0x24, // bDescriptorType
248 0x00, // bDescriptorSubtype
249 0x10, 0x01, // bcdCDC
250 // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
251 5, // bFunctionLength
252 0x24, // bDescriptorType
253 0x01, // bDescriptorSubtype
254 0x01, // bmCapabilities
256 // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
257 4, // bFunctionLength
258 0x24, // bDescriptorType
259 0x02, // bDescriptorSubtype
260 0x06, // bmCapabilities
261 // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
262 5, // bFunctionLength
263 0x24, // bDescriptorType
264 0x06, // bDescriptorSubtype
265 CDC_STATUS_INTERFACE, // bMasterInterface
266 CDC_DATA_INTERFACE, // bSlaveInterface0
267 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
269 5, // bDescriptorType
270 CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
271 0x03, // bmAttributes (0x03=intr)
272 CDC_ACM_SIZE, 0, // wMaxPacketSize
274 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
276 4, // bDescriptorType
277 CDC_DATA_INTERFACE, // bInterfaceNumber
278 0, // bAlternateSetting
280 0x0A, // bInterfaceClass
281 0x00, // bInterfaceSubClass
282 0x00, // bInterfaceProtocol
284 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
286 5, // bDescriptorType
287 CDC_RX_ENDPOINT, // bEndpointAddress
288 0x02, // bmAttributes (0x02=bulk)
289 CDC_RX_SIZE, 0, // wMaxPacketSize
291 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
293 5, // bDescriptorType
294 CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
295 0x02, // bmAttributes (0x02=bulk)
296 CDC_TX_SIZE, 0, // wMaxPacketSize
298 #endif // CDC_DATA_INTERFACE
300 #ifdef KEYBOARD_INTERFACE
301 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
303 4, // bDescriptorType
304 KEYBOARD_INTERFACE, // bInterfaceNumber
305 0, // bAlternateSetting
307 0x03, // bInterfaceClass (0x03 = HID)
308 0x01, // bInterfaceSubClass (0x01 = Boot)
309 0x01, // bInterfaceProtocol (0x01 = Keyboard)
311 // HID interface descriptor, HID 1.11 spec, section 6.2.1
313 0x21, // bDescriptorType
314 0x11, 0x01, // bcdHID
316 1, // bNumDescriptors
317 0x22, // bDescriptorType
318 LSB(sizeof(keyboard_report_desc)), // wDescriptorLength
319 MSB(sizeof(keyboard_report_desc)),
320 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
322 5, // bDescriptorType
323 KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
324 0x03, // bmAttributes (0x03=intr)
325 KEYBOARD_SIZE, 0, // wMaxPacketSize
326 KEYBOARD_INTERVAL, // bInterval
327 #endif // KEYBOARD_INTERFACE
329 #ifdef MOUSE_INTERFACE
330 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
332 4, // bDescriptorType
333 MOUSE_INTERFACE, // bInterfaceNumber
334 0, // bAlternateSetting
336 0x03, // bInterfaceClass (0x03 = HID)
337 0x01, // bInterfaceSubClass (0x01 = Boot)
338 0x02, // bInterfaceProtocol (0x02 = Mouse)
340 // HID interface descriptor, HID 1.11 spec, section 6.2.1
342 0x21, // bDescriptorType
343 0x11, 0x01, // bcdHID
345 1, // bNumDescriptors
346 0x22, // bDescriptorType
347 LSB(sizeof(mouse_report_desc)), // wDescriptorLength
348 MSB(sizeof(mouse_report_desc)),
349 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
351 5, // bDescriptorType
352 MOUSE_ENDPOINT | 0x80, // bEndpointAddress
353 0x03, // bmAttributes (0x03=intr)
354 MOUSE_SIZE, 0, // wMaxPacketSize
355 MOUSE_INTERVAL, // bInterval
356 #endif // MOUSE_INTERFACE
358 #ifdef JOYSTICK_INTERFACE
359 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
361 4, // bDescriptorType
362 JOYSTICK_INTERFACE, // bInterfaceNumber
363 0, // bAlternateSetting
365 0x03, // bInterfaceClass (0x03 = HID)
366 0x00, // bInterfaceSubClass
367 0x00, // bInterfaceProtocol
369 // HID interface descriptor, HID 1.11 spec, section 6.2.1
371 0x21, // bDescriptorType
372 0x11, 0x01, // bcdHID
374 1, // bNumDescriptors
375 0x22, // bDescriptorType
376 LSB(sizeof(joystick_report_desc)), // wDescriptorLength
377 MSB(sizeof(joystick_report_desc)),
378 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
380 5, // bDescriptorType
381 JOYSTICK_ENDPOINT | 0x80, // bEndpointAddress
382 0x03, // bmAttributes (0x03=intr)
383 JOYSTICK_SIZE, 0, // wMaxPacketSize
384 JOYSTICK_INTERVAL, // bInterval
390 // **************************************************************
391 // String Descriptors
392 // **************************************************************
394 // The descriptors above can provide human readable strings,
395 // referenced by index numbers. These descriptors are the
396 // actual string data
398 struct usb_string_descriptor_struct {
400 uint8_t bDescriptorType;
404 static struct usb_string_descriptor_struct string0 = {
410 static struct usb_string_descriptor_struct string1 = {
411 sizeof(STR_MANUFACTURER),
415 static struct usb_string_descriptor_struct string2 = {
420 static struct usb_string_descriptor_struct string3 = {
427 // **************************************************************
429 // **************************************************************
431 // This table provides access to all the descriptor data above.
433 const usb_descriptor_list_t usb_descriptor_list[] = {
434 //wValue, wIndex, address, length
435 {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
436 {0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
437 #ifdef KEYBOARD_INTERFACE
438 {0x2200, KEYBOARD_INTERFACE, keyboard_report_desc, sizeof(keyboard_report_desc)},
439 {0x2100, KEYBOARD_INTERFACE, config_descriptor+KEYBOARD_DESC_OFFSET, 9},
441 #ifdef MOUSE_INTERFACE
442 {0x2200, MOUSE_INTERFACE, mouse_report_desc, sizeof(mouse_report_desc)},
443 {0x2100, MOUSE_INTERFACE, config_descriptor+MOUSE_DESC_OFFSET, 9},
445 #ifdef JOYSTICK_INTERFACE
446 {0x2200, JOYSTICK_INTERFACE, joystick_report_desc, sizeof(joystick_report_desc)},
447 {0x2100, JOYSTICK_INTERFACE, config_descriptor+JOYSTICK_DESC_OFFSET, 9},
449 {0x0300, 0x0000, (const uint8_t *)&string0, 4},
450 {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
451 {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)},
452 {0x0303, 0x0409, (const uint8_t *)&string3, sizeof(STR_SERIAL)},
457 // **************************************************************
458 // Endpoint Configuration
459 // **************************************************************
463 // 0x19 = Recieve only
464 // 0x15 = Transmit only
465 // 0x1D = Transmit & Recieve
467 const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
469 0x00, 0x15, 0x19, 0x15, 0x00, 0x00, 0x00, 0x00,
470 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
475 const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
477 #if (defined(ENDPOINT1_CONFIG) && NUM_ENDPOINTS >= 1)
479 #elif (NUM_ENDPOINTS >= 1)
482 #if (defined(ENDPOINT2_CONFIG) && NUM_ENDPOINTS >= 2)
484 #elif (NUM_ENDPOINTS >= 2)
487 #if (defined(ENDPOINT3_CONFIG) && NUM_ENDPOINTS >= 3)
489 #elif (NUM_ENDPOINTS >= 3)
492 #if (defined(ENDPOINT4_CONFIG) && NUM_ENDPOINTS >= 4)
494 #elif (NUM_ENDPOINTS >= 4)
497 #if (defined(ENDPOINT5_CONFIG) && NUM_ENDPOINTS >= 5)
499 #elif (NUM_ENDPOINTS >= 5)
502 #if (defined(ENDPOINT6_CONFIG) && NUM_ENDPOINTS >= 6)
504 #elif (NUM_ENDPOINTS >= 6)
507 #if (defined(ENDPOINT7_CONFIG) && NUM_ENDPOINTS >= 7)
509 #elif (NUM_ENDPOINTS >= 7)
512 #if (defined(ENDPOINT8_CONFIG) && NUM_ENDPOINTS >= 8)
514 #elif (NUM_ENDPOINTS >= 8)
517 #if (defined(ENDPOINT9_CONFIG) && NUM_ENDPOINTS >= 9)
519 #elif (NUM_ENDPOINTS >= 9)
522 #if (defined(ENDPOINT10_CONFIG) && NUM_ENDPOINTS >= 10)
524 #elif (NUM_ENDPOINTS >= 10)
527 #if (defined(ENDPOINT11_CONFIG) && NUM_ENDPOINTS >= 11)
529 #elif (NUM_ENDPOINTS >= 11)
532 #if (defined(ENDPOINT12_CONFIG) && NUM_ENDPOINTS >= 12)
534 #elif (NUM_ENDPOINTS >= 12)
537 #if (defined(ENDPOINT13_CONFIG) && NUM_ENDPOINTS >= 13)
539 #elif (NUM_ENDPOINTS >= 13)
542 #if (defined(ENDPOINT14_CONFIG) && NUM_ENDPOINTS >= 14)
544 #elif (NUM_ENDPOINTS >= 14)
547 #if (defined(ENDPOINT15_CONFIG) && NUM_ENDPOINTS >= 15)
549 #elif (NUM_ENDPOINTS >= 15)