Added new callback to the Audio Class driver to allow for endpoint control manipulati...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / 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_DEVICE)
35
36 #define __INCLUDE_FROM_AUDIO_DRIVER
37 #define __INCLUDE_FROM_AUDIO_DEVICE_C
38 #include "Audio.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.wIndex != AudioInterfaceInfo->Config.StreamingInterfaceNumber)
46 return;
47
48 if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_RECIPIENT) == REQREC_INTERFACE)
49 {
50 if (USB_ControlRequest.wIndex != AudioInterfaceInfo->Config.StreamingInterfaceNumber)
51 return;
52 }
53 else if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_RECIPIENT) == REQREC_ENDPOINT)
54 {
55 if (!((AudioInterfaceInfo->Config.DataINEndpointNumber &&
56 (USB_ControlRequest.wIndex == AudioInterfaceInfo->Config.DataINEndpointNumber)) ||
57 (AudioInterfaceInfo->Config.DataOUTEndpointNumber &&
58 (USB_ControlRequest.wIndex == AudioInterfaceInfo->Config.DataOUTEndpointNumber))))
59 {
60 return;
61 }
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 }
74
75 break;
76 case AUDIO_REQ_GetStatus:
77 if ((USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) ||
78 (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_ENDPOINT)))
79 {
80 Endpoint_ClearSETUP();
81 Endpoint_ClearStatusStage();
82 }
83
84 break;
85 case AUDIO_REQ_SetCurrent:
86 case AUDIO_REQ_SetMinimum:
87 case AUDIO_REQ_SetMaximum:
88 case AUDIO_REQ_SetResolution:
89 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_ENDPOINT))
90 {
91 uint8_t EndpointProperty = USB_ControlRequest.bRequest;
92 uint8_t EndpointIndex = (uint8_t)USB_ControlRequest.wIndex;
93 uint8_t EndpointControl = (USB_ControlRequest.wValue >> 8);
94
95 if (CALLBACK_Audio_GetSetEndpointProperty(AudioInterfaceInfo, EndpointProperty, EndpointIndex, EndpointControl, NULL, NULL))
96 {
97 uint16_t ValueLength = USB_ControlRequest.wLength;
98 uint8_t Value[ValueLength];
99
100 Endpoint_ClearSETUP();
101 Endpoint_Read_Control_Stream_LE(Value, ValueLength);
102 Endpoint_ClearIN();
103
104 CALLBACK_Audio_GetSetEndpointProperty(AudioInterfaceInfo, EndpointProperty, EndpointIndex, EndpointControl, &ValueLength, Value);
105 }
106 }
107
108 break;
109 case AUDIO_REQ_GetCurrent:
110 case AUDIO_REQ_GetMinimum:
111 case AUDIO_REQ_GetMaximum:
112 case AUDIO_REQ_GetResolution:
113 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
114 {
115 uint8_t EndpointProperty = USB_ControlRequest.bRequest;
116 uint8_t EndpointIndex = (uint8_t)USB_ControlRequest.wIndex;
117 uint8_t EndpointControl = (USB_ControlRequest.wValue >> 8);
118 uint16_t ValueLength = USB_ControlRequest.wLength;
119 uint8_t Value[ValueLength];
120
121 if (CALLBACK_Audio_GetSetEndpointProperty(AudioInterfaceInfo, EndpointProperty, EndpointIndex, EndpointControl, &ValueLength, Value))
122 {
123 Endpoint_ClearSETUP();
124 Endpoint_Write_Control_Stream_LE(Value, ValueLength);
125 Endpoint_ClearOUT();
126 }
127 }
128
129 break;
130 }
131 }
132
133 bool Audio_Device_ConfigureEndpoints(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
134 {
135 memset(&AudioInterfaceInfo->State, 0x00, sizeof(AudioInterfaceInfo->State));
136
137 for (uint8_t EndpointNum = 1; EndpointNum < ENDPOINT_TOTAL_ENDPOINTS; EndpointNum++)
138 {
139 uint16_t Size;
140 uint8_t Type;
141 uint8_t Direction;
142
143 if (EndpointNum == AudioInterfaceInfo->Config.DataINEndpointNumber)
144 {
145 Size = AudioInterfaceInfo->Config.DataINEndpointSize;
146 Direction = ENDPOINT_DIR_IN;
147 Type = EP_TYPE_ISOCHRONOUS;
148 }
149 else if (EndpointNum == AudioInterfaceInfo->Config.DataOUTEndpointNumber)
150 {
151 Size = AudioInterfaceInfo->Config.DataOUTEndpointSize;
152 Direction = ENDPOINT_DIR_OUT;
153 Type = EP_TYPE_ISOCHRONOUS;
154 }
155 else
156 {
157 continue;
158 }
159
160 if (!(Endpoint_ConfigureEndpoint(EndpointNum, Type, Direction, Size, ENDPOINT_BANK_DOUBLE)))
161 return false;
162 }
163
164 return true;
165 }
166
167 #endif