Add new MediaController project, a simple Media Playback control manipulator (Play...
[pub/USBasp.git] / Projects / MediaController / Descriptors.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 /** HID class report descriptor. This is a special descriptor constructed with values from the
41 * USBIF HID class specification to describe the reports and capabilities of the HID device. This
42 * descriptor is parsed by the host and its contents used to determine what data (and in what encoding)
43 * the device will send, and what it may be sent back from the host. Refer to the HID specification for
44 * more details on HID report descriptors.
45 */
46 const USB_Descriptor_HIDReport_Datatype_t PROGMEM MediaControlReport[] =
47 {
48 HID_RI_USAGE_PAGE(8, 0x0C), /* Consumer Page */
49 HID_RI_USAGE(8, 0x01), /* Consumer Controls */
50 HID_RI_COLLECTION(8, 0x01), /* Application */
51 HID_RI_USAGE_MINIMUM(8, 0xB0),
52 HID_RI_USAGE_MAXIMUM(8, 0xB7),
53 HID_RI_LOGICAL_MINIMUM(8, 0),
54 HID_RI_LOGICAL_MAXIMUM(8, 1),
55 HID_RI_REPORT_SIZE(8, 0x01),
56 HID_RI_REPORT_COUNT(8, 0x08),
57 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
58 HID_RI_USAGE(8, 0xCD),
59 HID_RI_USAGE(8, 0xE2),
60 HID_RI_USAGE(8, 0xE9),
61 HID_RI_USAGE(8, 0xEA),
62 HID_RI_REPORT_COUNT(8, 0x04),
63 HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
64 HID_RI_REPORT_COUNT(8, 0x04),
65 HID_RI_INPUT(8, HID_IOF_CONSTANT),
66 HID_RI_END_COLLECTION(0),
67 };
68
69 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
70 * device characteristics, including the supported USB version, control endpoint size and the
71 * number of device configurations. The descriptor is read out by the USB host when the enumeration
72 * process begins.
73 */
74 const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
75 {
76 .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
77
78 .USBSpecification = VERSION_BCD(01.10),
79 .Class = USB_CSCP_NoDeviceClass,
80 .SubClass = USB_CSCP_NoDeviceSubclass,
81 .Protocol = USB_CSCP_NoDeviceProtocol,
82
83 .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
84
85 .VendorID = 0x03EB,
86 .ProductID = 0x206A,
87 .ReleaseNumber = VERSION_BCD(00.01),
88
89 .ManufacturerStrIndex = 0x01,
90 .ProductStrIndex = 0x02,
91 .SerialNumStrIndex = NO_DESCRIPTOR,
92
93 .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
94 };
95
96 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
97 * of the device in one of its supported configurations, including information about any device interfaces
98 * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
99 * a configuration so that the host may correctly communicate with the USB device.
100 */
101 const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
102 {
103 .Config =
104 {
105 .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
106
107 .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
108 .TotalInterfaces = 1,
109
110 .ConfigurationNumber = 1,
111 .ConfigurationStrIndex = NO_DESCRIPTOR,
112
113 .ConfigAttributes = (USB_CONFIG_ATTR_BUSPOWERED | USB_CONFIG_ATTR_SELFPOWERED),
114
115 .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
116 },
117
118 .HID_Interface =
119 {
120 .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
121
122 .InterfaceNumber = 0x00,
123 .AlternateSetting = 0x00,
124
125 .TotalEndpoints = 1,
126
127 .Class = HID_CSCP_HIDClass,
128 .SubClass = HID_CSCP_NonBootSubclass,
129 .Protocol = HID_CSCP_NonBootProtocol,
130
131 .InterfaceStrIndex = NO_DESCRIPTOR
132 },
133
134 .HID_MediaControlHID =
135 {
136 .Header = {.Size = sizeof(USB_HID_Descriptor_HID_t), .Type = HID_DTYPE_HID},
137
138 .HIDSpec = VERSION_BCD(01.11),
139 .CountryCode = 0x00,
140 .TotalReportDescriptors = 1,
141 .HIDReportType = HID_DTYPE_Report,
142 .HIDReportLength = sizeof(MediaControlReport)
143 },
144
145 .HID_ReportINEndpoint =
146 {
147 .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
148
149 .EndpointAddress = (ENDPOINT_DESCRIPTOR_DIR_IN | MEDIACONTROL_HID_EPNUM),
150 .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
151 .EndpointSize = MEDIACONTROL_HID_EPSIZE,
152 .PollingIntervalMS = 0x01
153 },
154 };
155
156 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
157 * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
158 * via the language ID table available at USB.org what languages the device supports for its string descriptors.
159 */
160 const USB_Descriptor_String_t PROGMEM LanguageString =
161 {
162 .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
163
164 .UnicodeString = {LANGUAGE_ID_ENG}
165 };
166
167 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
168 * form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
169 * Descriptor.
170 */
171 const USB_Descriptor_String_t PROGMEM ManufacturerString =
172 {
173 .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
174
175 .UnicodeString = L"Dean Camera"
176 };
177
178 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
179 * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
180 * Descriptor.
181 */
182 const USB_Descriptor_String_t PROGMEM ProductString =
183 {
184 .Header = {.Size = USB_STRING_LEN(21), .Type = DTYPE_String},
185
186 .UnicodeString = L"LUFA Media Controller"
187 };
188
189 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
190 * documentation) by the application code so that the address and size of a requested descriptor can be given
191 * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
192 * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
193 * USB host.
194 */
195 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
196 const uint8_t wIndex,
197 const void** const DescriptorAddress)
198 {
199 const uint8_t DescriptorType = (wValue >> 8);
200 const uint8_t DescriptorNumber = (wValue & 0xFF);
201
202 const void* Address = NULL;
203 uint16_t Size = NO_DESCRIPTOR;
204
205 switch (DescriptorType)
206 {
207 case DTYPE_Device:
208 Address = &DeviceDescriptor;
209 Size = sizeof(USB_Descriptor_Device_t);
210 break;
211 case DTYPE_Configuration:
212 Address = &ConfigurationDescriptor;
213 Size = sizeof(USB_Descriptor_Configuration_t);
214 break;
215 case DTYPE_String:
216 switch (DescriptorNumber)
217 {
218 case 0x00:
219 Address = &LanguageString;
220 Size = pgm_read_byte(&LanguageString.Header.Size);
221 break;
222 case 0x01:
223 Address = &ManufacturerString;
224 Size = pgm_read_byte(&ManufacturerString.Header.Size);
225 break;
226 case 0x02:
227 Address = &ProductString;
228 Size = pgm_read_byte(&ProductString.Header.Size);
229 break;
230 }
231
232 break;
233 case HID_DTYPE_HID:
234 Address = &ConfigurationDescriptor.HID_MediaControlHID;
235 Size = sizeof(USB_HID_Descriptor_HID_t);
236 break;
237 case HID_DTYPE_Report:
238 Address = &MediaControlReport;
239 Size = sizeof(MediaControlReport);
240 break;
241 }
242
243 *DescriptorAddress = Address;
244 return Size;
245 }
246