]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Host/LowLevel/AudioInputHost/ConfigDescriptor.c
allow overriding of TARGET
[qmk_firmware.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Host / LowLevel / AudioInputHost / ConfigDescriptor.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  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 Configuration Descriptor processing routines, to determine the correct pipe configurations
34  *  needed to communication with an attached USB device. Descriptors are special  computer-readable structures
35  *  which the host requests upon device enumeration, to determine the device's capabilities and functions.
36  */
37
38 #include "ConfigDescriptor.h"
39
40 /** Index of the currently used Audio Streaming Interface within the device. */
41 uint8_t StreamingInterfaceIndex      = 0;
42
43 /** Alternative Setting of the currently used Audio Streaming Interface within the device. */
44 uint8_t StreamingInterfaceAltSetting = 0;
45
46 /** Address of the streaming audio endpoint currently in use within the device. */
47 uint8_t StreamingEndpointAddress     = 0;
48
49
50 /** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This
51  *  routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate
52  *  with compatible devices.
53  *
54  *  This routine searches for a Streaming Audio interface descriptor containing a valid Isochronous audio endpoint.
55  *
56  *  \return An error code from the \ref AudioHost_GetConfigDescriptorDataCodes_t enum.
57  */
58 uint8_t ProcessConfigurationDescriptor(void)
59 {
60         uint8_t  ConfigDescriptorData[512];
61         void*    CurrConfigLocation = ConfigDescriptorData;
62         uint16_t CurrConfigBytesRem;
63
64         USB_Descriptor_Interface_t* AudioControlInterface   = NULL;
65         USB_Descriptor_Interface_t* AudioStreamingInterface = NULL;
66         USB_Descriptor_Endpoint_t*  DataINEndpoint          = NULL;
67
68         /* Retrieve the entire configuration descriptor into the allocated buffer */
69         switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
70         {
71                 case HOST_GETCONFIG_Successful:
72                         break;
73                 case HOST_GETCONFIG_InvalidData:
74                         return InvalidConfigDataReturned;
75                 case HOST_GETCONFIG_BuffOverflow:
76                         return DescriptorTooLarge;
77                 default:
78                         return ControlError;
79         }
80
81         while (!(DataINEndpoint))
82         {
83                 /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */
84                 if (!(AudioControlInterface) ||
85                     USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
86                                               DComp_NextAudioInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
87                 {
88                         /* Check if we haven't found an Audio Control interface yet, or if we have run out of related Audio Streaming interfaces */
89                         if (!(AudioControlInterface) ||
90                             USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
91                                                       DComp_NextAudioStreamInterface) != DESCRIPTOR_SEARCH_COMP_Found)
92                         {
93                                 /* Find a new Audio Control interface if the current one doesn't contain a compatible streaming interface */
94                                 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
95                                                               DComp_NextAudioControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
96                                 {
97                                         /* Descriptor not found, error out */
98                                         return NoCompatibleInterfaceFound;
99                                 }
100
101                                 /* Save the interface in case we need to refer back to it later */
102                                 AudioControlInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
103
104                                 /* Find the next Audio Streaming interface within that Audio Control interface */
105                                 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
106                                                               DComp_NextAudioStreamInterface) != DESCRIPTOR_SEARCH_COMP_Found)
107                                 {
108                                         /* Descriptor not found, error out */
109                                         return NoCompatibleInterfaceFound;
110                                 }
111                         }
112
113                         /* Save the interface in case we need to refer back to it later */
114                         AudioStreamingInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
115
116                         /* Skip the remainder of the loop as we have not found an endpoint yet */
117                         continue;
118                 }
119
120                 /* Retrieve the endpoint address from the endpoint descriptor */
121                 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
122
123                 /* Save the endpoint if it is an IN type endpoint */
124                 if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
125                   DataINEndpoint = EndpointData;
126         }
127
128         StreamingInterfaceIndex      = AudioStreamingInterface->InterfaceNumber;
129         StreamingInterfaceAltSetting = AudioStreamingInterface->AlternateSetting;
130         StreamingEndpointAddress     = DataINEndpoint->EndpointAddress;
131
132         /* Configure the Audio data IN pipe */
133         Pipe_ConfigurePipe(AUDIO_DATA_IN_PIPE, EP_TYPE_ISOCHRONOUS, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 2);
134
135         /* Valid data found, return success */
136         return SuccessfulConfigRead;
137 }
138
139 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
140  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
141  *  descriptor processing if an incompatible descriptor configuration is found.
142  *
143  *  This comparator searches for the next Interface descriptor of the correct Audio Control Class, Subclass and Protocol values.
144  *
145  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum
146  */
147 uint8_t DComp_NextAudioControlInterface(void* CurrentDescriptor)
148 {
149         USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
150
151         if (Header->Type == DTYPE_Interface)
152         {
153                 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
154
155                 if ((Interface->Class    == AUDIO_CSCP_AudioClass) &&
156                     (Interface->SubClass == AUDIO_CSCP_ControlSubclass) &&
157                     (Interface->Protocol == AUDIO_CSCP_ControlProtocol))
158                 {
159                         return DESCRIPTOR_SEARCH_Found;
160                 }
161         }
162
163         return DESCRIPTOR_SEARCH_NotFound;
164 }
165
166 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
167  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
168  *  descriptor processing if an incompatible descriptor configuration is found.
169  *
170  *  This comparator searches for the next Interface descriptor of the correct Audio Streaming Class, Subclass and Protocol values.
171  *
172  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum
173  */
174 uint8_t DComp_NextAudioStreamInterface(void* CurrentDescriptor)
175 {
176         USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
177
178         if (Header->Type == DTYPE_Interface)
179         {
180                 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
181
182                 if ((Interface->Class    == AUDIO_CSCP_AudioClass) &&
183                     (Interface->SubClass == AUDIO_CSCP_AudioStreamingSubclass) &&
184                     (Interface->Protocol == AUDIO_CSCP_StreamingProtocol))
185                 {
186                         return DESCRIPTOR_SEARCH_Found;
187                 }
188         }
189
190         return DESCRIPTOR_SEARCH_NotFound;
191 }
192
193 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
194  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
195  *  descriptor processing if an incompatible descriptor configuration is found.
196  *
197  *  This comparator searches for the next Isochronous Endpoint descriptor within the current interface, aborting the
198  *  search if another interface descriptor is found before the next endpoint.
199  *
200  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum
201  */
202 uint8_t DComp_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor)
203 {
204         USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
205
206         if (Header->Type == DTYPE_Endpoint)
207         {
208                 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
209
210                 if ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_ISOCHRONOUS)
211                   return DESCRIPTOR_SEARCH_Found;
212         }
213         else if (Header->Type == DTYPE_Interface)
214         {
215                 return DESCRIPTOR_SEARCH_Fail;
216         }
217
218         return DESCRIPTOR_SEARCH_NotFound;
219 }
220