]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/Demos/Device/LowLevel/AudioInput/Descriptors.c
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / lib / lufa / Demos / Device / LowLevel / AudioInput / Descriptors.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2017.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2017  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 disclaims 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 /** Device descriptor structure. This descriptor, located in FLASH memory, describes the overall
41  *  device characteristics, including the supported USB version, control endpoint size and the
42  *  number of device configurations. The descriptor is read out by the USB host when the enumeration
43  *  process begins.
44  */
45 const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
46 {
47         .Header                 = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
48
49         .USBSpecification       = VERSION_BCD(1,1,0),
50         .Class                  = USB_CSCP_NoDeviceClass,
51         .SubClass               = USB_CSCP_NoDeviceSubclass,
52         .Protocol               = USB_CSCP_NoDeviceProtocol,
53
54         .Endpoint0Size          = FIXED_CONTROL_ENDPOINT_SIZE,
55
56         .VendorID               = 0x03EB,
57         .ProductID              = 0x2047,
58         .ReleaseNumber          = VERSION_BCD(0,0,2),
59
60         .ManufacturerStrIndex   = STRING_ID_Manufacturer,
61         .ProductStrIndex        = STRING_ID_Product,
62         .SerialNumStrIndex      = NO_DESCRIPTOR,
63
64         .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
65 };
66
67 /** Configuration descriptor structure. This descriptor, located in FLASH memory, describes the usage
68  *  of the device in one of its supported configurations, including information about any device interfaces
69  *  and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
70  *  a configuration so that the host may correctly communicate with the USB device.
71  */
72 const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
73 {
74         .Config =
75                 {
76                         .Header                   = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
77
78                         .TotalConfigurationSize   = sizeof(USB_Descriptor_Configuration_t),
79                         .TotalInterfaces          = 2,
80
81                         .ConfigurationNumber      = 1,
82                         .ConfigurationStrIndex    = NO_DESCRIPTOR,
83
84                         .ConfigAttributes         = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
85
86                         .MaxPowerConsumption      = USB_CONFIG_POWER_MA(100)
87                 },
88
89         .Audio_ControlInterface =
90                 {
91                         .Header                   = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
92
93                         .InterfaceNumber          = INTERFACE_ID_AudioControl,
94                         .AlternateSetting         = 0,
95
96                         .TotalEndpoints           = 0,
97
98                         .Class                    = AUDIO_CSCP_AudioClass,
99                         .SubClass                 = AUDIO_CSCP_ControlSubclass,
100                         .Protocol                 = AUDIO_CSCP_ControlProtocol,
101
102                         .InterfaceStrIndex        = NO_DESCRIPTOR
103                 },
104
105         .Audio_ControlInterface_SPC =
106                 {
107                         .Header                   = {.Size = sizeof(USB_Audio_Descriptor_Interface_AC_t), .Type = DTYPE_CSInterface},
108                         .Subtype                  = AUDIO_DSUBTYPE_CSInterface_Header,
109
110                         .ACSpecification          = VERSION_BCD(1,0,0),
111                         .TotalLength              = (sizeof(USB_Audio_Descriptor_Interface_AC_t) +
112                                                      sizeof(USB_Audio_Descriptor_InputTerminal_t) +
113                                                      sizeof(USB_Audio_Descriptor_OutputTerminal_t)),
114
115                         .InCollection             = 1,
116                         .InterfaceNumber          = INTERFACE_ID_AudioStream,
117                 },
118
119         .Audio_InputTerminal =
120                 {
121                         .Header                   = {.Size = sizeof(USB_Audio_Descriptor_InputTerminal_t), .Type = DTYPE_CSInterface},
122                         .Subtype                  = AUDIO_DSUBTYPE_CSInterface_InputTerminal,
123
124                         .TerminalID               = 0x01,
125                         .TerminalType             = AUDIO_TERMINAL_IN_MIC,
126                         .AssociatedOutputTerminal = 0x00,
127
128                         .TotalChannels            = 1,
129                         .ChannelConfig            = 0,
130
131                         .ChannelStrIndex          = NO_DESCRIPTOR,
132                         .TerminalStrIndex         = NO_DESCRIPTOR
133                 },
134
135         .Audio_OutputTerminal =
136                 {
137                         .Header                   = {.Size = sizeof(USB_Audio_Descriptor_OutputTerminal_t), .Type = DTYPE_CSInterface},
138                         .Subtype                  = AUDIO_DSUBTYPE_CSInterface_OutputTerminal,
139
140                         .TerminalID               = 0x02,
141                         .TerminalType             = AUDIO_TERMINAL_STREAMING,
142                         .AssociatedInputTerminal  = 0x00,
143
144                         .SourceID                 = 0x01,
145
146                         .TerminalStrIndex         = NO_DESCRIPTOR
147                 },
148
149         .Audio_StreamInterface_Alt0 =
150                 {
151                         .Header                   = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
152
153                         .InterfaceNumber          = INTERFACE_ID_AudioStream,
154                         .AlternateSetting         = 0,
155
156                         .TotalEndpoints           = 0,
157
158                         .Class                    = AUDIO_CSCP_AudioClass,
159                         .SubClass                 = AUDIO_CSCP_AudioStreamingSubclass,
160                         .Protocol                 = AUDIO_CSCP_StreamingProtocol,
161
162                         .InterfaceStrIndex        = NO_DESCRIPTOR
163                 },
164
165         .Audio_StreamInterface_Alt1 =
166                 {
167                         .Header                   = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
168
169                         .InterfaceNumber          = INTERFACE_ID_AudioStream,
170                         .AlternateSetting         = 1,
171
172                         .TotalEndpoints           = 1,
173
174                         .Class                    = AUDIO_CSCP_AudioClass,
175                         .SubClass                 = AUDIO_CSCP_AudioStreamingSubclass,
176                         .Protocol                 = AUDIO_CSCP_StreamingProtocol,
177
178                         .InterfaceStrIndex        = NO_DESCRIPTOR
179                 },
180
181         .Audio_StreamInterface_SPC =
182                 {
183                         .Header                   = {.Size = sizeof(USB_Audio_Descriptor_Interface_AS_t), .Type = DTYPE_CSInterface},
184                         .Subtype                  = AUDIO_DSUBTYPE_CSInterface_General,
185
186                         .TerminalLink             = 0x02,
187
188                         .FrameDelay               = 1,
189                         .AudioFormat              = 0x0001
190                 },
191
192         .Audio_AudioFormat =
193                 {
194                         .Header                   = {.Size = sizeof(USB_Audio_Descriptor_Format_t) +
195                                                              sizeof(ConfigurationDescriptor.Audio_AudioFormatSampleRates),
196                                                      .Type = DTYPE_CSInterface},
197                         .Subtype                  = AUDIO_DSUBTYPE_CSInterface_FormatType,
198
199                         .FormatType               = 0x01,
200                         .Channels                 = 0x01,
201
202                         .SubFrameSize             = 0x02,
203                         .BitResolution            = 16,
204
205                         .TotalDiscreteSampleRates = (sizeof(ConfigurationDescriptor.Audio_AudioFormatSampleRates) / sizeof(USB_Audio_SampleFreq_t)),
206                 },
207
208         .Audio_AudioFormatSampleRates =
209                 {
210                         AUDIO_SAMPLE_FREQ(8000),
211                         AUDIO_SAMPLE_FREQ(11025),
212                         AUDIO_SAMPLE_FREQ(22050),
213                         AUDIO_SAMPLE_FREQ(44100),
214                         AUDIO_SAMPLE_FREQ(48000),
215                 },
216
217         .Audio_StreamEndpoint =
218                 {
219                         .Endpoint =
220                                 {
221                                         .Header              = {.Size = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t), .Type = DTYPE_Endpoint},
222
223                                         .EndpointAddress     = AUDIO_STREAM_EPADDR,
224                                         .Attributes          = (EP_TYPE_ISOCHRONOUS | ENDPOINT_ATTR_SYNC | ENDPOINT_USAGE_DATA),
225                                         .EndpointSize        = AUDIO_STREAM_EPSIZE,
226                                         .PollingIntervalMS   = 0x01
227                                 },
228
229                         .Refresh                  = 0,
230                         .SyncEndpointNumber       = 0
231                 },
232
233         .Audio_StreamEndpoint_SPC =
234                 {
235                         .Header                   = {.Size = sizeof(USB_Audio_Descriptor_StreamEndpoint_Spc_t), .Type = DTYPE_CSEndpoint},
236                         .Subtype                  = AUDIO_DSUBTYPE_CSEndpoint_General,
237
238                         .Attributes               = (AUDIO_EP_ACCEPTS_SMALL_PACKETS | AUDIO_EP_SAMPLE_FREQ_CONTROL),
239
240                         .LockDelayUnits           = 0x00,
241                         .LockDelay                = 0x0000
242                 }
243 };
244
245 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
246  *  the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
247  *  via the language ID table available at USB.org what languages the device supports for its string descriptors.
248  */
249 const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
250
251 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
252  *  form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
253  *  Descriptor.
254  */
255 const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Dean Camera");
256
257 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
258  *  and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
259  *  Descriptor.
260  */
261 const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"LUFA Audio In Demo");
262
263 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
264  *  documentation) by the application code so that the address and size of a requested descriptor can be given
265  *  to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
266  *  is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
267  *  USB host.
268  */
269 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
270                                     const uint16_t wIndex,
271                                     const void** const DescriptorAddress)
272 {
273         const uint8_t  DescriptorType   = (wValue >> 8);
274         const uint8_t  DescriptorNumber = (wValue & 0xFF);
275
276         const void* Address = NULL;
277         uint16_t    Size    = NO_DESCRIPTOR;
278
279         switch (DescriptorType)
280         {
281                 case DTYPE_Device:
282                         Address = &DeviceDescriptor;
283                         Size    = sizeof(USB_Descriptor_Device_t);
284                         break;
285                 case DTYPE_Configuration:
286                         Address = &ConfigurationDescriptor;
287                         Size    = sizeof(USB_Descriptor_Configuration_t);
288                         break;
289                 case DTYPE_String:
290                         switch (DescriptorNumber)
291                         {
292                                 case STRING_ID_Language:
293                                         Address = &LanguageString;
294                                         Size    = pgm_read_byte(&LanguageString.Header.Size);
295                                         break;
296                                 case STRING_ID_Manufacturer:
297                                         Address = &ManufacturerString;
298                                         Size    = pgm_read_byte(&ManufacturerString.Header.Size);
299                                         break;
300                                 case STRING_ID_Product:
301                                         Address = &ProductString;
302                                         Size    = pgm_read_byte(&ProductString.Header.Size);
303                                         break;
304                         }
305
306                         break;
307         }
308
309         *DescriptorAddress = Address;
310         return Size;
311 }
312