Add new tag for the LUFA-120219-BETA release.
[pub/USBasp.git] / Demos / Host / LowLevel / AudioInputHost / ConfigDescriptor.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 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, PIPE_TOKEN_IN,
134 DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, PIPE_BANK_DOUBLE);
135
136 /* Valid data found, return success */
137 return SuccessfulConfigRead;
138 }
139
140 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
141 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
142 * descriptor processing if an incompatible descriptor configuration is found.
143 *
144 * This comparator searches for the next Interface descriptor of the correct Audio Control Class, Subclass and Protocol values.
145 *
146 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
147 */
148 uint8_t DComp_NextAudioControlInterface(void* CurrentDescriptor)
149 {
150 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
151
152 if (Header->Type == DTYPE_Interface)
153 {
154 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
155
156 if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
157 (Interface->SubClass == AUDIO_CSCP_ControlSubclass) &&
158 (Interface->Protocol == AUDIO_CSCP_ControlProtocol))
159 {
160 return DESCRIPTOR_SEARCH_Found;
161 }
162 }
163
164 return DESCRIPTOR_SEARCH_NotFound;
165 }
166
167 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
168 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
169 * descriptor processing if an incompatible descriptor configuration is found.
170 *
171 * This comparator searches for the next Interface descriptor of the correct Audio Streaming Class, Subclass and Protocol values.
172 *
173 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
174 */
175 uint8_t DComp_NextAudioStreamInterface(void* CurrentDescriptor)
176 {
177 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
178
179 if (Header->Type == DTYPE_Interface)
180 {
181 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
182
183 if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
184 (Interface->SubClass == AUDIO_CSCP_AudioStreamingSubclass) &&
185 (Interface->Protocol == AUDIO_CSCP_StreamingProtocol))
186 {
187 return DESCRIPTOR_SEARCH_Found;
188 }
189 }
190
191 return DESCRIPTOR_SEARCH_NotFound;
192 }
193
194 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
195 * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
196 * descriptor processing if an incompatible descriptor configuration is found.
197 *
198 * This comparator searches for the next Isochronous Endpoint descriptor within the current interface, aborting the
199 * search if another interface descriptor is found before the next endpoint.
200 *
201 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
202 */
203 uint8_t DComp_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor)
204 {
205 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
206
207 if (Header->Type == DTYPE_Endpoint)
208 {
209 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
210
211 if ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_ISOCHRONOUS)
212 return DESCRIPTOR_SEARCH_Found;
213 }
214 else if (Header->Type == DTYPE_Interface)
215 {
216 return DESCRIPTOR_SEARCH_Fail;
217 }
218
219 return DESCRIPTOR_SEARCH_NotFound;
220 }
221