State information for class drivers is now zeroed out during enumeration (both in...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / Audio.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_DEVICE)
33
34 #include "Audio.h"
35
36 void Audio_Device_ProcessControlPacket(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
37 {
38 if (!(Endpoint_IsSETUPReceived()))
39 return;
40
41 if (USB_ControlRequest.wIndex != AudioInterfaceInfo->Config.StreamingInterfaceNumber)
42 return;
43
44 switch (USB_ControlRequest.bRequest)
45 {
46 case REQ_SetInterface:
47 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE))
48 {
49 Endpoint_ClearSETUP();
50
51 AudioInterfaceInfo->State.InterfaceEnabled = (USB_ControlRequest.wValue != 0);
52
53 Endpoint_ClearStatusStage();
54 }
55
56 break;
57 }
58 }
59
60 bool Audio_Device_ConfigureEndpoints(USB_ClassInfo_Audio_Device_t* AudioInterfaceInfo)
61 {
62 memset(&AudioInterfaceInfo->State, 0x00, sizeof(AudioInterfaceInfo->State));
63
64 if (AudioInterfaceInfo->Config.DataINEndpointNumber)
65 {
66 if (!(Endpoint_ConfigureEndpoint(AudioInterfaceInfo->Config.DataINEndpointNumber, EP_TYPE_ISOCHRONOUS,
67 ENDPOINT_DIR_IN, AudioInterfaceInfo->Config.DataINEndpointSize,
68 ENDPOINT_BANK_DOUBLE)))
69 {
70 return false;
71 }
72 }
73
74 if (AudioInterfaceInfo->Config.DataOUTEndpointNumber)
75 {
76 if (!(Endpoint_ConfigureEndpoint(AudioInterfaceInfo->Config.DataOUTEndpointNumber, EP_TYPE_ISOCHRONOUS,
77 ENDPOINT_DIR_OUT, AudioInterfaceInfo->Config.DataOUTEndpointSize,
78 ENDPOINT_BANK_DOUBLE)))
79 {
80 return false;
81 }
82 }
83
84 return true;
85 }
86
87 void Audio_Device_USBTask(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
88 {
89
90 }
91
92 int8_t Audio_Device_ReadSample8(void)
93 {
94 int8_t Sample;
95
96 Sample = Endpoint_Read_Byte();
97
98 if (!(Endpoint_IsReadWriteAllowed()))
99 Endpoint_ClearOUT();
100
101 return Sample;
102 }
103
104 int16_t Audio_Device_ReadSample16(void)
105 {
106 int16_t Sample;
107
108 Sample = (int16_t)Endpoint_Read_Word_LE();
109
110 if (!(Endpoint_IsReadWriteAllowed()))
111 Endpoint_ClearOUT();
112
113 return Sample;
114 }
115
116 int32_t Audio_Device_ReadSample24(void)
117 {
118 int32_t Sample;
119
120 Sample = (((uint32_t)Endpoint_Read_Byte() << 16) | Endpoint_Read_Word_LE());
121
122 if (!(Endpoint_IsReadWriteAllowed()))
123 Endpoint_ClearOUT();
124
125 return Sample;
126 }
127
128 void Audio_Device_WriteSample8(const int8_t Sample)
129 {
130 Endpoint_Write_Byte(Sample);
131
132 if (!(Endpoint_IsReadWriteAllowed()))
133 Endpoint_ClearIN();
134 }
135
136 void Audio_Device_WriteSample16(const int16_t Sample)
137 {
138 Endpoint_Write_Word_LE(Sample);
139
140 if (!(Endpoint_IsReadWriteAllowed()))
141 Endpoint_ClearIN();
142 }
143
144 void Audio_Device_WriteSample24(const int32_t Sample)
145 {
146 Endpoint_Write_Byte(Sample >> 16);
147 Endpoint_Write_Word_LE(Sample);
148
149 if (!(Endpoint_IsReadWriteAllowed()))
150 Endpoint_ClearIN();
151 }
152
153 bool Audio_Device_IsSampleReceived(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
154 {
155 Endpoint_SelectEndpoint(AudioInterfaceInfo->Config.DataOUTEndpointNumber);
156 return Endpoint_IsOUTReceived();
157 }
158
159 bool Audio_Device_IsReadyForNextSample(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
160 {
161 Endpoint_SelectEndpoint(AudioInterfaceInfo->Config.DataINEndpointNumber);
162 return Endpoint_IsINReady();
163 }
164
165 #endif