Update file contributor copyrights for 2012.
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / AudioClassDevice.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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../Core/USBMode.h"
33
34 #if defined(USB_CAN_BE_DEVICE)
35
36 #define __INCLUDE_FROM_AUDIO_DRIVER
37 #define __INCLUDE_FROM_AUDIO_DEVICE_C
38 #include "AudioClassDevice.h"
39
40 void Audio_Device_ProcessControlRequest(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
41 {
42 if (!(Endpoint_IsSETUPReceived()))
43 return;
44
45 if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_RECIPIENT) == REQREC_INTERFACE)
46 {
47 if ((USB_ControlRequest.wIndex & 0xFF) != AudioInterfaceInfo->Config.StreamingInterfaceNumber)
48 return;
49 }
50 else if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_RECIPIENT) == REQREC_ENDPOINT)
51 {
52 bool EndpointFilterMatch = false;
53
54 EndpointFilterMatch |= (AudioInterfaceInfo->Config.DataINEndpointNumber &&
55 ((uint8_t)USB_ControlRequest.wIndex == (ENDPOINT_DIR_IN | AudioInterfaceInfo->Config.DataINEndpointNumber)));
56
57 EndpointFilterMatch |= (AudioInterfaceInfo->Config.DataOUTEndpointNumber &&
58 ((uint8_t)USB_ControlRequest.wIndex == (ENDPOINT_DIR_OUT | AudioInterfaceInfo->Config.DataOUTEndpointNumber)));
59
60 if (!(EndpointFilterMatch))
61 return;
62 }
63
64 switch (USB_ControlRequest.bRequest)
65 {
66 case REQ_SetInterface:
67 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE))
68 {
69 Endpoint_ClearSETUP();
70 Endpoint_ClearStatusStage();
71
72 AudioInterfaceInfo->State.InterfaceEnabled = ((USB_ControlRequest.wValue & 0xFF) != 0);
73 EVENT_Audio_Device_StreamStartStop(AudioInterfaceInfo);
74 }
75
76 break;
77 case AUDIO_REQ_GetStatus:
78 if ((USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) ||
79 (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_ENDPOINT)))
80 {
81 Endpoint_ClearSETUP();
82 Endpoint_ClearStatusStage();
83 }
84
85 break;
86 case AUDIO_REQ_SetCurrent:
87 case AUDIO_REQ_SetMinimum:
88 case AUDIO_REQ_SetMaximum:
89 case AUDIO_REQ_SetResolution:
90 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_ENDPOINT))
91 {
92 uint8_t EndpointProperty = USB_ControlRequest.bRequest;
93 uint8_t EndpointAddress = (uint8_t)USB_ControlRequest.wIndex;
94 uint8_t EndpointControl = (USB_ControlRequest.wValue >> 8);
95
96 if (CALLBACK_Audio_Device_GetSetEndpointProperty(AudioInterfaceInfo, EndpointProperty, EndpointAddress,
97 EndpointControl, NULL, NULL))
98 {
99 uint16_t ValueLength = USB_ControlRequest.wLength;
100 uint8_t Value[ValueLength];
101
102 Endpoint_ClearSETUP();
103 Endpoint_Read_Control_Stream_LE(Value, ValueLength);
104 Endpoint_ClearIN();
105
106 CALLBACK_Audio_Device_GetSetEndpointProperty(AudioInterfaceInfo, EndpointProperty, EndpointAddress,
107 EndpointControl, &ValueLength, Value);
108 }
109 }
110 else if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
111 {
112 uint8_t Property = USB_ControlRequest.bRequest;
113 uint8_t Entity = (USB_ControlRequest.wIndex >> 8);
114 uint16_t Parameter = USB_ControlRequest.wValue;
115
116 if (CALLBACK_Audio_Device_GetSetInterfaceProperty(AudioInterfaceInfo, Property, Entity,
117 Parameter, NULL, NULL))
118 {
119 uint16_t ValueLength = USB_ControlRequest.wLength;
120 uint8_t Value[ValueLength];
121
122 Endpoint_ClearSETUP();
123 Endpoint_Read_Control_Stream_LE(Value, ValueLength);
124 Endpoint_ClearIN();
125
126 CALLBACK_Audio_Device_GetSetInterfaceProperty(AudioInterfaceInfo, Property, Entity,
127 Parameter, NULL, NULL);
128 }
129 }
130
131 break;
132 case AUDIO_REQ_GetCurrent:
133 case AUDIO_REQ_GetMinimum:
134 case AUDIO_REQ_GetMaximum:
135 case AUDIO_REQ_GetResolution:
136 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
137 {
138 uint8_t EndpointProperty = USB_ControlRequest.bRequest;
139 uint8_t EndpointAddress = (uint8_t)USB_ControlRequest.wIndex;
140 uint8_t EndpointControl = (USB_ControlRequest.wValue >> 8);
141 uint16_t ValueLength = USB_ControlRequest.wLength;
142 uint8_t Value[ValueLength];
143
144 if (CALLBACK_Audio_Device_GetSetEndpointProperty(AudioInterfaceInfo, EndpointProperty, EndpointAddress,
145 EndpointControl, &ValueLength, Value))
146 {
147 Endpoint_ClearSETUP();
148 Endpoint_Write_Control_Stream_LE(Value, ValueLength);
149 Endpoint_ClearOUT();
150 }
151 }
152 else if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
153 {
154 uint8_t Property = USB_ControlRequest.bRequest;
155 uint8_t Entity = (USB_ControlRequest.wIndex >> 8);
156 uint16_t Parameter = USB_ControlRequest.wValue;
157 uint16_t ValueLength = USB_ControlRequest.wLength;
158 uint8_t Value[ValueLength];
159
160 if (CALLBACK_Audio_Device_GetSetInterfaceProperty(AudioInterfaceInfo, Property, Entity,
161 Parameter, &ValueLength, Value))
162 {
163 Endpoint_ClearSETUP();
164 Endpoint_Write_Control_Stream_LE(Value, ValueLength);
165 Endpoint_ClearOUT();
166 }
167 }
168
169 break;
170 }
171 }
172
173 bool Audio_Device_ConfigureEndpoints(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
174 {
175 memset(&AudioInterfaceInfo->State, 0x00, sizeof(AudioInterfaceInfo->State));
176
177 for (uint8_t EndpointNum = 1; EndpointNum < ENDPOINT_TOTAL_ENDPOINTS; EndpointNum++)
178 {
179 uint16_t Size;
180 uint8_t Type;
181 uint8_t Direction;
182 bool DoubleBanked;
183
184 if (EndpointNum == AudioInterfaceInfo->Config.DataINEndpointNumber)
185 {
186 Size = AudioInterfaceInfo->Config.DataINEndpointSize;
187 Direction = ENDPOINT_DIR_IN;
188 Type = EP_TYPE_ISOCHRONOUS;
189 DoubleBanked = true;
190 }
191 else if (EndpointNum == AudioInterfaceInfo->Config.DataOUTEndpointNumber)
192 {
193 Size = AudioInterfaceInfo->Config.DataOUTEndpointSize;
194 Direction = ENDPOINT_DIR_OUT;
195 Type = EP_TYPE_ISOCHRONOUS;
196 DoubleBanked = true;
197 }
198 else
199 {
200 continue;
201 }
202
203 if (!(Endpoint_ConfigureEndpoint(EndpointNum, Type, Direction, Size,
204 DoubleBanked ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
205 {
206 return false;
207 }
208 }
209
210 return true;
211 }
212
213 void Audio_Device_Event_Stub(void)
214 {
215
216 }
217
218 #endif
219