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