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