Lower bulk endpoint polling rate in the descriptors to the lowest possible value...
[pub/lufa.git] / Projects / TempDataLogger / Descriptors.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
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.
36 */
37
38 #include "Descriptors.h"
39
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
46 * port location).
47 */
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.
50 #endif
51
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.
57 */
58 USB_Descriptor_HIDReport_Datatype_t PROGMEM GenericReport[] =
59 {
60 0x06, 0x9c, 0xff, /* Usage Page (Vendor Defined) */
61 0x09, 0x01, /* Usage (Vendor Defined) */
62 0xa1, 0x01, /* Collection (Vendor Defined) */
63 0x09, 0x02, /* Usage (Vendor Defined) */
64 0x75, 0x08, /* Report Size (8) */
65 0x95, GENERIC_REPORT_SIZE, /* Report Count (GENERIC_REPORT_SIZE) */
66 0x15, 0x80, /* Logical Minimum (-128) */
67 0x25, 0x7F, /* Logical Maximum (127) */
68 0x81, 0x02, /* Input (Data, Variable, Absolute) */
69 0x09, 0x03, /* Usage (Vendor Defined) */
70 0x75, 0x08, /* Report Size (8) */
71 0x95, GENERIC_REPORT_SIZE, /* Report Count (GENERIC_REPORT_SIZE) */
72 0x15, 0x00, /* Logical Minimum (0) */
73 0x25, 0xff, /* Logical Maximum (255) */
74 0x91, 0x02, /* Output (Data, Variable, Absolute) */
75 0xc0 /* End Collection */
76 };
77
78 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
79 * device characteristics, including the supported USB version, control endpoint size and the
80 * number of device configurations. The descriptor is read out by the USB host when the enumeration
81 * process begins.
82 */
83 USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
84 {
85 .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
86
87 .USBSpecification = VERSION_BCD(01.10),
88 .Class = USB_CSCP_NoDeviceClass,
89 .SubClass = USB_CSCP_NoDeviceSubclass,
90 .Protocol = USB_CSCP_NoDeviceProtocol,
91
92 .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
93
94 .VendorID = 0x03EB,
95 .ProductID = 0x2063,
96 .ReleaseNumber = VERSION_BCD(00.01),
97
98 .ManufacturerStrIndex = 0x01,
99 .ProductStrIndex = 0x02,
100 .SerialNumStrIndex = USE_INTERNAL_SERIAL,
101
102 .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
103 };
104
105 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
106 * of the device in one of its supported configurations, including information about any device interfaces
107 * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
108 * a configuration so that the host may correctly communicate with the USB device.
109 */
110 USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
111 {
112 .Config =
113 {
114 .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
115
116 .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
117 .TotalInterfaces = 2,
118
119 .ConfigurationNumber = 1,
120 .ConfigurationStrIndex = NO_DESCRIPTOR,
121
122 .ConfigAttributes = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELFPOWERED),
123
124 .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
125 },
126
127 .MS_Interface =
128 {
129 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
130
131 .InterfaceNumber = 0,
132 .AlternateSetting = 0,
133
134 .TotalEndpoints = 2,
135
136 .Class = MS_CSCP_MassStorageClass,
137 .SubClass = MS_CSCP_SCSITransparentSubclass,
138 .Protocol = MS_CSCP_BulkOnlyTransportProtocol,
139
140 .InterfaceStrIndex = NO_DESCRIPTOR
141 },
142
143 .MS_DataInEndpoint =
144 {
145 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
146
147 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | MASS_STORAGE_IN_EPNUM),
148 .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
149 .EndpointSize = MASS_STORAGE_IO_EPSIZE,
150 .PollingIntervalMS = 0x01
151 },
152
153 .MS_DataOutEndpoint =
154 {
155 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
156
157 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_OUT | MASS_STORAGE_OUT_EPNUM),
158 .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
159 .EndpointSize = MASS_STORAGE_IO_EPSIZE,
160 .PollingIntervalMS = 0x01
161 },
162
163 .HID_Interface =
164 {
165 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
166
167 .InterfaceNumber = 1,
168 .AlternateSetting = 0,
169
170 .TotalEndpoints = 1,
171
172 .Class = HID_CSCP_HIDClass,
173 .SubClass = HID_CSCP_NonBootSubclass,
174 .Protocol = HID_CSCP_NonBootProtocol,
175
176 .InterfaceStrIndex = NO_DESCRIPTOR
177 },
178
179 .HID_GenericHID =
180 {
181 .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
182
183 .HIDSpec = VERSION_BCD(01.11),
184 .CountryCode = 0x00,
185 .TotalReportDescriptors = 1,
186 .HIDReportType = HID_DTYPE_Report,
187 .HIDReportLength = sizeof(GenericReport)
188 },
189
190 .HID_ReportINEndpoint =
191 {
192 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
193
194 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | GENERIC_IN_EPNUM),
195 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
196 .EndpointSize = GENERIC_EPSIZE,
197 .PollingIntervalMS = 0x01
198 },
199 };
200
201 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
202 * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
203 * via the language ID table available at USB.org what languages the device supports for its string descriptors.
204 */
205 USB_Descriptor_String_t PROGMEM LanguageString =
206 {
207 .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
208
209 .UnicodeString = {LANGUAGE_ID_ENG}
210 };
211
212 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
213 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
214 * Descriptor.
215 */
216 USB_Descriptor_String_t PROGMEM ManufacturerString =
217 {
218 .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
219
220 .UnicodeString = L"Dean Camera"
221 };
222
223 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
224 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
225 * Descriptor.
226 */
227 USB_Descriptor_String_t PROGMEM ProductString =
228 {
229 .Header = {.Size = USB_STRING_LEN(22), .Type = DTYPE_String},
230
231 .UnicodeString = L"Temperature Datalogger"
232 };
233
234 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
235 * documentation) by the application code so that the address and size of a requested descriptor can be given
236 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
237 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
238 * USB host.
239 */
240 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
241 const uint8_t wIndex,
242 const void** const DescriptorAddress)
243 {
244 const uint8_t DescriptorType = (wValue >> 8);
245 const uint8_t DescriptorNumber = (wValue & 0xFF);
246
247 const void* Address = NULL;
248 uint16_t Size = NO_DESCRIPTOR;
249
250 switch (DescriptorType)
251 {
252 case DTYPE_Device:
253 Address = &DeviceDescriptor;
254 Size = sizeof(USB_Descriptor_Device_t);
255 break;
256 case DTYPE_Configuration:
257 Address = &ConfigurationDescriptor;
258 Size = sizeof(USB_Descriptor_Configuration_t);
259 break;
260 case DTYPE_String:
261 switch (DescriptorNumber)
262 {
263 case 0x00:
264 Address = &LanguageString;
265 Size = pgm_read_byte(&LanguageString.Header.Size);
266 break;
267 case 0x01:
268 Address = &ManufacturerString;
269 Size = pgm_read_byte(&ManufacturerString.Header.Size);
270 break;
271 case 0x02:
272 Address = &ProductString;
273 Size = pgm_read_byte(&ProductString.Header.Size);
274 break;
275 }
276
277 break;
278 case HID_DTYPE_HID:
279 Address = &ConfigurationDescriptor.HID_GenericHID;
280 Size = sizeof(USB_HID_Descriptor_HID_t);
281 break;
282 case HID_DTYPE_Report:
283 Address = &GenericReport;
284 Size = sizeof(GenericReport);
285 break;
286 }
287
288 *DescriptorAddress = Address;
289 return Size;
290 }
291