7397ef9864a61b8d044495ee6151519e8719318f
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / Audio.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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../Core/USBMode.h"
33
34 #if defined(USB_CAN_BE_HOST)
35
36 #define __INCLUDE_FROM_AUDIO_DRIVER
37 #define __INCLUDE_FROM_AUDIO_HOST_C
38 #include "Audio.h"
39
40 uint8_t Audio_Host_ConfigurePipes(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
41 uint16_t ConfigDescriptorSize,
42 void* ConfigDescriptorData)
43 {
44 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
45 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
46 USB_Descriptor_Interface_t* AudioControlInterface = NULL;
47 USB_Descriptor_Interface_t* AudioStreamingInterface = NULL;
48
49 memset(&AudioInterfaceInfo->State, 0x00, sizeof(AudioInterfaceInfo->State));
50
51 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
52 return AUDIO_ENUMERROR_InvalidConfigDescriptor;
53
54 while (!(DataINEndpoint) || !(DataOUTEndpoint))
55 {
56 if (!(AudioControlInterface) ||
57 USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
58 DComp_NextAudioInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
59 {
60 if (!(AudioControlInterface) ||
61 USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
62 DComp_NextAudioStreamInterface) != DESCRIPTOR_SEARCH_COMP_Found)
63 {
64 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
65 DComp_NextAudioControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
66 {
67 return AUDIO_ENUMERROR_NoCompatibleInterfaceFound;
68 }
69
70 AudioControlInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
71
72 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
73 DComp_NextAudioStreamInterface) != DESCRIPTOR_SEARCH_COMP_Found)
74 {
75 return AUDIO_ENUMERROR_NoCompatibleInterfaceFound;
76 }
77 }
78
79 AudioStreamingInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
80
81 continue;
82 }
83
84 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
85
86 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
87 DataINEndpoint = EndpointData;
88 else
89 DataOUTEndpoint = EndpointData;
90 }
91
92 for (uint8_t PipeNum = 1; PipeNum < PIPE_TOTAL_PIPES; PipeNum++)
93 {
94 uint16_t Size;
95 uint8_t Type;
96 uint8_t Token;
97 uint8_t EndpointAddress;
98 bool DoubleBanked;
99
100 if (PipeNum == AudioInterfaceInfo->Config.DataINPipeNumber)
101 {
102 Size = DataINEndpoint->EndpointSize;
103 EndpointAddress = DataINEndpoint->EndpointAddress;
104 Token = PIPE_TOKEN_IN;
105 Type = EP_TYPE_BULK;
106 DoubleBanked = true;
107
108 AudioInterfaceInfo->State.DataINPipeSize = DataINEndpoint->EndpointSize;
109 }
110 else if (PipeNum == AudioInterfaceInfo->Config.DataOUTPipeNumber)
111 {
112 Size = DataOUTEndpoint->EndpointSize;
113 EndpointAddress = DataOUTEndpoint->EndpointAddress;
114 Token = PIPE_TOKEN_OUT;
115 Type = EP_TYPE_ISOCHRONOUS;
116 DoubleBanked = true;
117
118 AudioInterfaceInfo->State.DataOUTPipeSize = DataOUTEndpoint->EndpointSize;
119 }
120 else
121 {
122 continue;
123 }
124
125 if (!(Pipe_ConfigurePipe(PipeNum, Type, Token, EndpointAddress, Size,
126 DoubleBanked ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE)))
127 {
128 return AUDIO_ENUMERROR_PipeConfigurationFailed;
129 }
130 }
131
132 AudioInterfaceInfo->State.ControlInterfaceNumber = AudioControlInterface->InterfaceNumber;
133 AudioInterfaceInfo->State.StreamingInterfaceNumber = AudioStreamingInterface->InterfaceNumber;
134 AudioInterfaceInfo->State.EnabledStreamingAltIndex = AudioStreamingInterface->AlternateSetting;
135 AudioInterfaceInfo->State.IsActive = true;
136
137 return AUDIO_ENUMERROR_NoError;
138 }
139
140 static uint8_t DComp_NextAudioControlInterface(void* CurrentDescriptor)
141 {
142 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
143
144 if (Header->Type == DTYPE_Interface)
145 {
146 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
147
148 if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
149 (Interface->SubClass == AUDIO_CSCP_ControlSubclass) &&
150 (Interface->Protocol == AUDIO_CSCP_ControlProtocol))
151 {
152 return DESCRIPTOR_SEARCH_Found;
153 }
154 }
155
156 return DESCRIPTOR_SEARCH_NotFound;
157 }
158
159 static uint8_t DComp_NextAudioStreamInterface(void* CurrentDescriptor)
160 {
161 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
162
163 if (Header->Type == DTYPE_Interface)
164 {
165 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
166
167 if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
168 (Interface->SubClass == AUDIO_CSCP_AudioStreamingSubclass) &&
169 (Interface->Protocol == AUDIO_CSCP_StreamingProtocol))
170 {
171 return DESCRIPTOR_SEARCH_Found;
172 }
173 }
174
175 return DESCRIPTOR_SEARCH_NotFound;
176 }
177
178 static uint8_t DComp_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor)
179 {
180 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
181
182 if (Header->Type == DTYPE_Endpoint)
183 {
184 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
185
186 if ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_ISOCHRONOUS)
187 return DESCRIPTOR_SEARCH_Found;
188 }
189 else if (Header->Type == DTYPE_Interface)
190 {
191 return DESCRIPTOR_SEARCH_Fail;
192 }
193
194 return DESCRIPTOR_SEARCH_NotFound;
195 }
196
197 uint8_t AUDIO_Host_StartStopStreaming(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
198 bool EnableStreaming)
199 {
200 if (!(AudioInterfaceInfo->State.IsActive))
201 return HOST_SENDCONTROL_DeviceDisconnected;
202
203 return USB_Host_SetInterfaceAltSetting(AudioInterfaceInfo->State.StreamingInterfaceNumber,
204 EnableStreaming ? AudioInterfaceInfo->State.EnabledStreamingAltIndex : 0);
205 }
206
207 #endif
208