Added new incomplete AudioInputHost Host LowLevel demo.
[pub/USBasp.git] / Demos / Host / Incomplete / AudioInputHost / ConfigDescriptor.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 uint8_t StreamingInterfaceIndex = 0;
41 uint8_t StreamingInterfaceAltSetting = 0;
42 uint8_t StreamingEndpointAddress = 0;
43
44 uint8_t ProcessConfigurationDescriptor(void)
45 {
46 uint8_t ConfigDescriptorData[512];
47 void* CurrConfigLocation = ConfigDescriptorData;
48 uint16_t CurrConfigBytesRem;
49
50 USB_Descriptor_Interface_t* AudioControlInterface = NULL;
51 USB_Descriptor_Interface_t* AudioStreamingInterface = NULL;
52 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
53
54 /* Retrieve the entire configuration descriptor into the allocated buffer */
55 switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
56 {
57 case HOST_GETCONFIG_Successful:
58 break;
59 case HOST_GETCONFIG_InvalidData:
60 return InvalidConfigDataReturned;
61 case HOST_GETCONFIG_BuffOverflow:
62 return DescriptorTooLarge;
63 default:
64 return ControlError;
65 }
66
67 while (!(DataINEndpoint))
68 {
69 if (!(AudioControlInterface) ||
70 USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
71 DComp_NextAudioInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
72 {
73 if (!(AudioControlInterface))
74 {
75 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
76 DComp_NextAudioControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
77 {
78 /* Descriptor not found, error out */
79 return NoCompatibleInterfaceFound;
80 }
81
82 /* Save the interface in case we need to refer back to it later */
83 AudioControlInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
84 }
85
86 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
87 DComp_NextAudioStreamInterface) != DESCRIPTOR_SEARCH_COMP_Found)
88 {
89 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
90 DComp_NextAudioControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
91 {
92 /* Descriptor not found, error out */
93 return NoCompatibleInterfaceFound;
94 }
95
96 /* Save the interface in case we need to refer back to it later */
97 AudioControlInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
98 }
99
100 /* Save the interface in case we need to refer back to it later */
101 AudioStreamingInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
102
103 /* Skip the remainder of the loop as we have not found an endpoint yet */
104 continue;
105 }
106
107 /* Retrieve the endpoint address from the endpoint descriptor */
108 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
109
110 /* If the endpoint is a IN type endpoint */
111 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
112 DataINEndpoint = EndpointData;
113 }
114
115 StreamingInterfaceIndex = AudioStreamingInterface->InterfaceNumber;
116 StreamingInterfaceAltSetting = AudioStreamingInterface->AlternateSetting;
117 StreamingEndpointAddress = DataINEndpoint->EndpointAddress;
118
119 /* Configure the HID data IN pipe */
120 Pipe_ConfigurePipe(AUDIO_DATA_IN_PIPE, EP_TYPE_ISOCHRONOUS, PIPE_TOKEN_IN,
121 DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, PIPE_BANK_DOUBLE);
122
123 /* Valid data found, return success */
124 return SuccessfulConfigRead;
125 }
126
127 uint8_t DComp_NextAudioControlInterface(void* CurrentDescriptor)
128 {
129 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
130
131 if (Header->Type == DTYPE_Interface)
132 {
133 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
134
135 if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
136 (Interface->SubClass == AUDIO_CSCP_ControlSubclass) &&
137 (Interface->Protocol == AUDIO_CSCP_ControlProtocol))
138 {
139 return DESCRIPTOR_SEARCH_Found;
140 }
141 }
142
143 return DESCRIPTOR_SEARCH_NotFound;
144 }
145
146 uint8_t DComp_NextAudioStreamInterface(void* CurrentDescriptor)
147 {
148 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
149
150 if (Header->Type == DTYPE_Interface)
151 {
152 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
153
154 if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
155 (Interface->SubClass == AUDIO_CSCP_AudioStreamingSubclass) &&
156 (Interface->Protocol == AUDIO_CSCP_StreamingProtocol))
157 {
158 return DESCRIPTOR_SEARCH_Found;
159 }
160 }
161
162 return DESCRIPTOR_SEARCH_NotFound;
163 }
164
165 uint8_t DComp_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor)
166 {
167 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
168
169 if (Header->Type == DTYPE_Endpoint)
170 {
171 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
172
173 if ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_ISOCHRONOUS)
174 return DESCRIPTOR_SEARCH_Found;
175 }
176 else if (Header->Type == DTYPE_Interface)
177 {
178 return DESCRIPTOR_SEARCH_Fail;
179 }
180
181 return DESCRIPTOR_SEARCH_NotFound;
182 }
183