3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
33 * USB Device Descriptors, for library use when in USB device mode. Descriptors are special
34 * computer-readable structures which the host requests upon device enumeration, to determine
35 * the device's capabilities and functions.
38 #include "Descriptors.h"
40 /* On some devices, there is a factory set internal serial number which can be automatically sent to the host as
41 * the device's serial number when the Device Descriptor's .SerialNumStrIndex entry is set to USE_INTERNAL_SERIAL.
42 * This allows the host to track a device across insertions on different ports, allowing them to retain allocated
43 * resources like COM port numbers and drivers. On demos using this feature, give a warning on unsupported devices
44 * so that the user can supply their own serial number descriptor instead or remove the USE_INTERNAL_SERIAL value
45 * from the Device Descriptor (forcing the host to generate a serial number for each device from the VID, PID and
48 #if (USE_INTERNAL_SERIAL == NO_DESCRIPTOR)
49 #warning USE_INTERNAL_SERIAL is not available on this AVR - please manually construct a device serial descriptor.
52 /** HID class report descriptor. This is a special descriptor constructed with values from the
53 * USBIF HID class specification to describe the reports and capabilities of the HID device. This
54 * descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
55 * the device will send, and what it may be sent back from the host. Refer to the HID specification for
56 * more details on HID report descriptors.
58 USB_Descriptor_HIDReport_Datatype_t PROGMEM GenericReport
[] =
60 HID_RI_USAGE_PAGE(16), 0x00, 0xFF, /* Vendor Page 1 */
61 HID_RI_USAGE(8), 0x01, /* Vendor Usage 1 */
62 HID_RI_COLLECTION(8), 0x01, /* Vendor Usage 1 */
63 HID_RI_USAGE(8), 0x02, /* Vendor Usage 2 */
64 HID_RI_LOGICAL_MINIMUM(8), 0x00,
65 HID_RI_LOGICAL_MAXIMUM(8), 0xFF,
66 HID_RI_REPORT_SIZE(8), 8,
67 HID_RI_REPORT_COUNT(8), GENERIC_REPORT_SIZE
,
68 HID_RI_INPUT(8), (HID_IOF_DATA
| HID_IOF_VARIABLE
| HID_IOF_ABSOLUTE
| HID_IOF_VOLATILE
),
70 HID_RI_USAGE(8), 0x03, /* Vendor Usage 3 */
71 HID_RI_LOGICAL_MINIMUM(8), 0x00,
72 HID_RI_LOGICAL_MAXIMUM(8), 0xFF,
73 HID_RI_REPORT_SIZE(8), 8,
74 HID_RI_REPORT_COUNT(8), GENERIC_REPORT_SIZE
,
75 HID_RI_OUTPUT(8), (HID_IOF_DATA
| HID_IOF_VARIABLE
| HID_IOF_ABSOLUTE
| HID_IOF_VOLATILE
),
76 HID_RI_END_COLLECTION(0),
79 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
80 * device characteristics, including the supported USB version, control endpoint size and the
81 * number of device configurations. The descriptor is read out by the USB host when the enumeration
84 USB_Descriptor_Device_t PROGMEM DeviceDescriptor
=
86 .Header
= {.Size
= sizeof(USB_Descriptor_Device_t
), .Type
= DTYPE_Device
},
88 .USBSpecification
= VERSION_BCD(01.10),
89 .Class
= USB_CSCP_NoDeviceClass
,
90 .SubClass
= USB_CSCP_NoDeviceSubclass
,
91 .Protocol
= USB_CSCP_NoDeviceProtocol
,
93 .Endpoint0Size
= FIXED_CONTROL_ENDPOINT_SIZE
,
97 .ReleaseNumber
= VERSION_BCD(00.01),
99 .ManufacturerStrIndex
= 0x01,
100 .ProductStrIndex
= 0x02,
101 .SerialNumStrIndex
= USE_INTERNAL_SERIAL
,
103 .NumberOfConfigurations
= FIXED_NUM_CONFIGURATIONS
106 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
107 * of the device in one of its supported configurations, including information about any device interfaces
108 * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
109 * a configuration so that the host may correctly communicate with the USB device.
111 USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor
=
115 .Header
= {.Size
= sizeof(USB_Descriptor_Configuration_Header_t
), .Type
= DTYPE_Configuration
},
117 .TotalConfigurationSize
= sizeof(USB_Descriptor_Configuration_t
),
118 .TotalInterfaces
= 2,
120 .ConfigurationNumber
= 1,
121 .ConfigurationStrIndex
= NO_DESCRIPTOR
,
123 .ConfigAttributes
= (USB_CONFIG_ATTR_BUSPOWERED
| USB_CONFIG_ATTR_SELFPOWERED
),
125 .MaxPowerConsumption
= USB_CONFIG_POWER_MA(100)
130 .Header
= {.Size
= sizeof(USB_Descriptor_Interface_t
), .Type
= DTYPE_Interface
},
132 .InterfaceNumber
= 0,
133 .AlternateSetting
= 0,
137 .Class
= MS_CSCP_MassStorageClass
,
138 .SubClass
= MS_CSCP_SCSITransparentSubclass
,
139 .Protocol
= MS_CSCP_BulkOnlyTransportProtocol
,
141 .InterfaceStrIndex
= NO_DESCRIPTOR
146 .Header
= {.Size
= sizeof(USB_Descriptor_Endpoint_t
), .Type
= DTYPE_Endpoint
},
148 .EndpointAddress
= (ENDPOINT_DESCRIPTOR_DIR_IN
| MASS_STORAGE_IN_EPNUM
),
149 .Attributes
= (EP_TYPE_BULK
| ENDPOINT_ATTR_NO_SYNC
| ENDPOINT_USAGE_DATA
),
150 .EndpointSize
= MASS_STORAGE_IO_EPSIZE
,
151 .PollingIntervalMS
= 0x01
154 .MS_DataOutEndpoint
=
156 .Header
= {.Size
= sizeof(USB_Descriptor_Endpoint_t
), .Type
= DTYPE_Endpoint
},
158 .EndpointAddress
= (ENDPOINT_DESCRIPTOR_DIR_OUT
| MASS_STORAGE_OUT_EPNUM
),
159 .Attributes
= (EP_TYPE_BULK
| ENDPOINT_ATTR_NO_SYNC
| ENDPOINT_USAGE_DATA
),
160 .EndpointSize
= MASS_STORAGE_IO_EPSIZE
,
161 .PollingIntervalMS
= 0x01
166 .Header
= {.Size
= sizeof(USB_Descriptor_Interface_t
), .Type
= DTYPE_Interface
},
168 .InterfaceNumber
= 1,
169 .AlternateSetting
= 0,
173 .Class
= HID_CSCP_HIDClass
,
174 .SubClass
= HID_CSCP_NonBootSubclass
,
175 .Protocol
= HID_CSCP_NonBootProtocol
,
177 .InterfaceStrIndex
= NO_DESCRIPTOR
182 .Header
= {.Size
= sizeof(USB_HID_Descriptor_HID_t
), .Type
= HID_DTYPE_HID
},
184 .HIDSpec
= VERSION_BCD(01.11),
186 .TotalReportDescriptors
= 1,
187 .HIDReportType
= HID_DTYPE_Report
,
188 .HIDReportLength
= sizeof(GenericReport
)
191 .HID_ReportINEndpoint
=
193 .Header
= {.Size
= sizeof(USB_Descriptor_Endpoint_t
), .Type
= DTYPE_Endpoint
},
195 .EndpointAddress
= (ENDPOINT_DESCRIPTOR_DIR_IN
| GENERIC_IN_EPNUM
),
196 .Attributes
= (EP_TYPE_INTERRUPT
| ENDPOINT_ATTR_NO_SYNC
| ENDPOINT_USAGE_DATA
),
197 .EndpointSize
= GENERIC_EPSIZE
,
198 .PollingIntervalMS
= 0x01
202 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
203 * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
204 * via the language ID table available at USB.org what languages the device supports for its string descriptors.
206 USB_Descriptor_String_t PROGMEM LanguageString
=
208 .Header
= {.Size
= USB_STRING_LEN(1), .Type
= DTYPE_String
},
210 .UnicodeString
= {LANGUAGE_ID_ENG
}
213 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
214 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
217 USB_Descriptor_String_t PROGMEM ManufacturerString
=
219 .Header
= {.Size
= USB_STRING_LEN(11), .Type
= DTYPE_String
},
221 .UnicodeString
= L
"Dean Camera"
224 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
225 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
228 USB_Descriptor_String_t PROGMEM ProductString
=
230 .Header
= {.Size
= USB_STRING_LEN(22), .Type
= DTYPE_String
},
232 .UnicodeString
= L
"Temperature Datalogger"
235 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
236 * documentation) by the application code so that the address and size of a requested descriptor can be given
237 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
238 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
241 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue
,
242 const uint8_t wIndex
,
243 const void** const DescriptorAddress
)
245 const uint8_t DescriptorType
= (wValue
>> 8);
246 const uint8_t DescriptorNumber
= (wValue
& 0xFF);
248 const void* Address
= NULL
;
249 uint16_t Size
= NO_DESCRIPTOR
;
251 switch (DescriptorType
)
254 Address
= &DeviceDescriptor
;
255 Size
= sizeof(USB_Descriptor_Device_t
);
257 case DTYPE_Configuration
:
258 Address
= &ConfigurationDescriptor
;
259 Size
= sizeof(USB_Descriptor_Configuration_t
);
262 switch (DescriptorNumber
)
265 Address
= &LanguageString
;
266 Size
= pgm_read_byte(&LanguageString
.Header
.Size
);
269 Address
= &ManufacturerString
;
270 Size
= pgm_read_byte(&ManufacturerString
.Header
.Size
);
273 Address
= &ProductString
;
274 Size
= pgm_read_byte(&ProductString
.Header
.Size
);
280 Address
= &ConfigurationDescriptor
.HID_GenericHID
;
281 Size
= sizeof(USB_HID_Descriptor_HID_t
);
283 case HID_DTYPE_Report
:
284 Address
= &GenericReport
;
285 Size
= sizeof(GenericReport
);
289 *DescriptorAddress
= Address
;