]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/Demos/Device/ClassDriver/MIDI/Descriptors.c
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / lib / lufa / Demos / Device / ClassDriver / MIDI / 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              = 0x2048,
58         .ReleaseNumber          = VERSION_BCD(0,0,1),
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
113                         .InCollection             = 1,
114                         .InterfaceNumber          = INTERFACE_ID_AudioStream,
115                 },
116
117         .Audio_StreamInterface =
118                 {
119                         .Header                   = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
120
121                         .InterfaceNumber          = INTERFACE_ID_AudioStream,
122                         .AlternateSetting         = 0,
123
124                         .TotalEndpoints           = 2,
125
126                         .Class                    = AUDIO_CSCP_AudioClass,
127                         .SubClass                 = AUDIO_CSCP_MIDIStreamingSubclass,
128                         .Protocol                 = AUDIO_CSCP_StreamingProtocol,
129
130                         .InterfaceStrIndex        = NO_DESCRIPTOR
131                 },
132
133         .Audio_StreamInterface_SPC =
134                 {
135                         .Header                   = {.Size = sizeof(USB_MIDI_Descriptor_AudioInterface_AS_t), .Type = DTYPE_CSInterface},
136                         .Subtype                  = AUDIO_DSUBTYPE_CSInterface_General,
137
138                         .AudioSpecification       = VERSION_BCD(1,0,0),
139
140                         .TotalLength              = (sizeof(USB_Descriptor_Configuration_t) -
141                                                      offsetof(USB_Descriptor_Configuration_t, Audio_StreamInterface_SPC))
142                 },
143
144         .MIDI_In_Jack_Emb =
145                 {
146                         .Header                   = {.Size = sizeof(USB_MIDI_Descriptor_InputJack_t), .Type = DTYPE_CSInterface},
147                         .Subtype                  = AUDIO_DSUBTYPE_CSInterface_InputTerminal,
148
149                         .JackType                 = MIDI_JACKTYPE_Embedded,
150                         .JackID                   = 0x01,
151
152                         .JackStrIndex             = NO_DESCRIPTOR
153                 },
154
155         .MIDI_In_Jack_Ext =
156                 {
157                         .Header                   = {.Size = sizeof(USB_MIDI_Descriptor_InputJack_t), .Type = DTYPE_CSInterface},
158                         .Subtype                  = AUDIO_DSUBTYPE_CSInterface_InputTerminal,
159
160                         .JackType                 = MIDI_JACKTYPE_External,
161                         .JackID                   = 0x02,
162
163                         .JackStrIndex             = NO_DESCRIPTOR
164                 },
165
166         .MIDI_Out_Jack_Emb =
167                 {
168                         .Header                   = {.Size = sizeof(USB_MIDI_Descriptor_OutputJack_t), .Type = DTYPE_CSInterface},
169                         .Subtype                  = AUDIO_DSUBTYPE_CSInterface_OutputTerminal,
170
171                         .JackType                 = MIDI_JACKTYPE_Embedded,
172                         .JackID                   = 0x03,
173
174                         .NumberOfPins             = 1,
175                         .SourceJackID             = {0x02},
176                         .SourcePinID              = {0x01},
177
178                         .JackStrIndex             = NO_DESCRIPTOR
179                 },
180
181         .MIDI_Out_Jack_Ext =
182                 {
183                         .Header                   = {.Size = sizeof(USB_MIDI_Descriptor_OutputJack_t), .Type = DTYPE_CSInterface},
184                         .Subtype                  = AUDIO_DSUBTYPE_CSInterface_OutputTerminal,
185
186                         .JackType                 = MIDI_JACKTYPE_External,
187                         .JackID                   = 0x04,
188
189                         .NumberOfPins             = 1,
190                         .SourceJackID             = {0x01},
191                         .SourcePinID              = {0x01},
192
193                         .JackStrIndex             = NO_DESCRIPTOR
194                 },
195
196         .MIDI_In_Jack_Endpoint =
197                 {
198                         .Endpoint =
199                                 {
200                                         .Header              = {.Size = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t), .Type = DTYPE_Endpoint},
201
202                                         .EndpointAddress     = MIDI_STREAM_OUT_EPADDR,
203                                         .Attributes          = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
204                                         .EndpointSize        = MIDI_STREAM_EPSIZE,
205                                         .PollingIntervalMS   = 0x05
206                                 },
207
208                         .Refresh                  = 0,
209                         .SyncEndpointNumber       = 0
210                 },
211
212         .MIDI_In_Jack_Endpoint_SPC =
213                 {
214                         .Header                   = {.Size = sizeof(USB_MIDI_Descriptor_Jack_Endpoint_t), .Type = DTYPE_CSEndpoint},
215                         .Subtype                  = AUDIO_DSUBTYPE_CSEndpoint_General,
216
217                         .TotalEmbeddedJacks       = 0x01,
218                         .AssociatedJackID         = {0x01}
219                 },
220
221         .MIDI_Out_Jack_Endpoint =
222                 {
223                         .Endpoint =
224                                 {
225                                         .Header              = {.Size = sizeof(USB_Audio_Descriptor_StreamEndpoint_Std_t), .Type = DTYPE_Endpoint},
226
227                                         .EndpointAddress     = MIDI_STREAM_IN_EPADDR,
228                                         .Attributes          = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
229                                         .EndpointSize        = MIDI_STREAM_EPSIZE,
230                                         .PollingIntervalMS   = 0x05
231                                 },
232
233                         .Refresh                  = 0,
234                         .SyncEndpointNumber       = 0
235                 },
236
237         .MIDI_Out_Jack_Endpoint_SPC =
238                 {
239                         .Header                   = {.Size = sizeof(USB_MIDI_Descriptor_Jack_Endpoint_t), .Type = DTYPE_CSEndpoint},
240                         .Subtype                  = AUDIO_DSUBTYPE_CSEndpoint_General,
241
242                         .TotalEmbeddedJacks       = 0x01,
243                         .AssociatedJackID         = {0x03}
244                 }
245 };
246
247 /** Language descriptor structure. This descriptor, located in FLASH memory, is returned when the host requests
248  *  the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
249  *  via the language ID table available at USB.org what languages the device supports for its string descriptors.
250  */
251 const USB_Descriptor_String_t PROGMEM LanguageString = USB_STRING_DESCRIPTOR_ARRAY(LANGUAGE_ID_ENG);
252
253 /** Manufacturer descriptor string. This is a Unicode string containing the manufacturer's details in human readable
254  *  form, and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
255  *  Descriptor.
256  */
257 const USB_Descriptor_String_t PROGMEM ManufacturerString = USB_STRING_DESCRIPTOR(L"Dean Camera");
258
259 /** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
260  *  and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
261  *  Descriptor.
262  */
263 const USB_Descriptor_String_t PROGMEM ProductString = USB_STRING_DESCRIPTOR(L"LUFA MIDI Demo");
264
265 /** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
266  *  documentation) by the application code so that the address and size of a requested descriptor can be given
267  *  to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
268  *  is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
269  *  USB host.
270  */
271 uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
272                                     const uint16_t wIndex,
273                                     const void** const DescriptorAddress)
274 {
275         const uint8_t  DescriptorType   = (wValue >> 8);
276         const uint8_t  DescriptorNumber = (wValue & 0xFF);
277
278         const void* Address = NULL;
279         uint16_t    Size    = NO_DESCRIPTOR;
280
281         switch (DescriptorType)
282         {
283                 case DTYPE_Device:
284                         Address = &DeviceDescriptor;
285                         Size    = sizeof(USB_Descriptor_Device_t);
286                         break;
287                 case DTYPE_Configuration:
288                         Address = &ConfigurationDescriptor;
289                         Size    = sizeof(USB_Descriptor_Configuration_t);
290                         break;
291                 case DTYPE_String:
292                         switch (DescriptorNumber)
293                         {
294                                 case STRING_ID_Language:
295                                         Address = &LanguageString;
296                                         Size    = pgm_read_byte(&LanguageString.Header.Size);
297                                         break;
298                                 case STRING_ID_Manufacturer:
299                                         Address = &ManufacturerString;
300                                         Size    = pgm_read_byte(&ManufacturerString.Header.Size);
301                                         break;
302                                 case STRING_ID_Product:
303                                         Address = &ProductString;
304                                         Size    = pgm_read_byte(&ProductString.Header.Size);
305                                         break;
306                         }
307
308                         break;
309         }
310
311         *DescriptorAddress = Address;
312         return Size;
313 }
314