3 Copyright (C) Dean Camera, 2014.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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.
38 #include "ConfigDescriptor.h"
40 /** Index of the currently used Audio Streaming Interface within the device. */
41 uint8_t StreamingInterfaceIndex = 0;
43 /** Alternative Setting of the currently used Audio Streaming Interface within the device. */
44 uint8_t StreamingInterfaceAltSetting = 0;
46 /** Address of the streaming audio endpoint currently in use within the device. */
47 uint8_t StreamingEndpointAddress = 0;
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.
54 * This routine searches for a Streaming Audio interface descriptor containing a valid Isochronous audio endpoint.
56 * \return An error code from the \ref AudioHost_GetConfigDescriptorDataCodes_t enum.
58 uint8_t ProcessConfigurationDescriptor(void)
60 uint8_t ConfigDescriptorData[512];
61 void* CurrConfigLocation = ConfigDescriptorData;
62 uint16_t CurrConfigBytesRem;
64 USB_Descriptor_Interface_t* AudioControlInterface = NULL;
65 USB_Descriptor_Interface_t* AudioStreamingInterface = NULL;
66 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
68 /* Retrieve the entire configuration descriptor into the allocated buffer */
69 switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
71 case HOST_GETCONFIG_Successful:
73 case HOST_GETCONFIG_InvalidData:
74 return InvalidConfigDataReturned;
75 case HOST_GETCONFIG_BuffOverflow:
76 return DescriptorTooLarge;
81 while (!(DataOUTEndpoint))
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)
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)
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)
97 /* Descriptor not found, error out */
98 return NoCompatibleInterfaceFound;
101 /* Save the interface in case we need to refer back to it later */
102 AudioControlInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
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)
108 /* Descriptor not found, error out */
109 return NoCompatibleInterfaceFound;
113 /* Save the interface in case we need to refer back to it later */
114 AudioStreamingInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
116 /* Skip the remainder of the loop as we have not found an endpoint yet */
120 /* Retrieve the endpoint address from the endpoint descriptor */
121 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
123 /* Save the endpoint if it is an OUT type endpoint */
124 if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_OUT)
125 DataOUTEndpoint = EndpointData;
128 StreamingInterfaceIndex = AudioStreamingInterface->InterfaceNumber;
129 StreamingInterfaceAltSetting = AudioStreamingInterface->AlternateSetting;
130 StreamingEndpointAddress = DataOUTEndpoint->EndpointAddress;
132 /* Configure the Audio data OUT pipe */
133 Pipe_ConfigurePipe(AUDIO_DATA_OUT_PIPE, EP_TYPE_ISOCHRONOUS, DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, 2);
135 /* Valid data found, return success */
136 return SuccessfulConfigRead;
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.
143 * This comparator searches for the next Interface descriptor of the correct Audio Control Class, Subclass and Protocol values.
145 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
147 uint8_t DComp_NextAudioControlInterface(void* CurrentDescriptor)
149 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
151 if (Header->Type == DTYPE_Interface)
153 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
155 if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
156 (Interface->SubClass == AUDIO_CSCP_ControlSubclass) &&
157 (Interface->Protocol == AUDIO_CSCP_ControlProtocol))
159 return DESCRIPTOR_SEARCH_Found;
163 return DESCRIPTOR_SEARCH_NotFound;
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.
170 * This comparator searches for the next Interface descriptor of the correct Audio Streaming Class, Subclass and Protocol values.
172 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
174 uint8_t DComp_NextAudioStreamInterface(void* CurrentDescriptor)
176 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
178 if (Header->Type == DTYPE_Interface)
180 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
182 if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
183 (Interface->SubClass == AUDIO_CSCP_AudioStreamingSubclass) &&
184 (Interface->Protocol == AUDIO_CSCP_StreamingProtocol))
186 return DESCRIPTOR_SEARCH_Found;
190 return DESCRIPTOR_SEARCH_NotFound;
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.
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.
200 * \return A value from the DSEARCH_Return_ErrorCodes_t enum
202 uint8_t DComp_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor)
204 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
206 if (Header->Type == DTYPE_Endpoint)
208 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
210 if ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_ISOCHRONOUS)
211 return DESCRIPTOR_SEARCH_Found;
213 else if (Header->Type == DTYPE_Interface)
215 return DESCRIPTOR_SEARCH_Fail;
218 return DESCRIPTOR_SEARCH_NotFound;