Ensure all USB device class drivers have the same three main functions as their inter...
[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 "Audio.h"
32
33 void USB_Audio_ProcessControlPacket(USB_ClassInfo_Audio_t* AudioInterfaceInfo)
34 {
35 if (!(Endpoint_IsSETUPReceived()))
36 return;
37
38 if (USB_ControlRequest.wIndex != AudioInterfaceInfo->StreamingInterfaceNumber)
39 return;
40
41 switch (USB_ControlRequest.bRequest)
42 {
43 case REQ_SetInterface:
44 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE))
45 {
46 Endpoint_ClearSETUP();
47
48 AudioInterfaceInfo->InterfaceEnabled = (USB_ControlRequest.wValue != 0);
49
50 while (!(Endpoint_IsINReady()));
51 Endpoint_ClearIN();
52 }
53
54 break;
55 }
56 }
57
58 bool USB_Audio_ConfigureEndpoints(USB_ClassInfo_Audio_t* AudioInterfaceInfo)
59 {
60 if (AudioInterfaceInfo->DataINEndpointNumber)
61 {
62 if (!(Endpoint_ConfigureEndpoint(AudioInterfaceInfo->DataINEndpointNumber, EP_TYPE_ISOCHRONOUS,
63 ENDPOINT_DIR_IN, AudioInterfaceInfo->DataINEndpointSize,
64 ENDPOINT_BANK_DOUBLE)))
65 {
66 return false;
67 }
68 }
69
70 if (AudioInterfaceInfo->DataOUTEndpointNumber)
71 {
72 if (!(Endpoint_ConfigureEndpoint(AudioInterfaceInfo->DataOUTEndpointNumber, EP_TYPE_ISOCHRONOUS,
73 ENDPOINT_DIR_OUT, AudioInterfaceInfo->DataOUTEndpointSize,
74 ENDPOINT_BANK_DOUBLE)))
75 {
76 return false;
77 }
78 }
79
80 return true;
81 }
82
83 void USB_Audio_USBTask(USB_ClassInfo_Audio_t* AudioInterfaceInfo)
84 {
85
86 }
87
88 int8_t USB_Audio_ReadSample8(void)
89 {
90 int8_t Sample;
91
92 Sample = Endpoint_Read_Byte();
93
94 if (!(Endpoint_IsReadWriteAllowed()))
95 Endpoint_ClearOUT();
96
97 return Sample;
98 }
99
100 int16_t USB_Audio_ReadSample16(void)
101 {
102 int16_t Sample;
103
104 Sample = (int16_t)Endpoint_Read_Word_LE();
105
106 if (!(Endpoint_IsReadWriteAllowed()))
107 Endpoint_ClearOUT();
108
109 return Sample;
110 }
111
112 int32_t USB_Audio_ReadSample24(void)
113 {
114 int32_t Sample;
115
116 Sample = (((uint32_t)Endpoint_Read_Byte() << 16) | Endpoint_Read_Word_LE());
117
118 if (!(Endpoint_IsReadWriteAllowed()))
119 Endpoint_ClearOUT();
120
121 return Sample;
122 }
123
124 void USB_Audio_WriteSample8(int8_t Sample)
125 {
126 Endpoint_Write_Byte(Sample);
127
128 if (!(Endpoint_IsReadWriteAllowed()))
129 Endpoint_ClearIN();
130 }
131
132 void USB_Audio_WriteSample16(int16_t Sample)
133 {
134 Endpoint_Write_Word_LE(Sample);
135
136 if (!(Endpoint_IsReadWriteAllowed()))
137 Endpoint_ClearIN();
138 }
139
140 void USB_Audio_WriteSample24(int32_t Sample)
141 {
142 Endpoint_Write_Byte(Sample >> 16);
143 Endpoint_Write_Word_LE(Sample);
144
145 if (!(Endpoint_IsReadWriteAllowed()))
146 Endpoint_ClearIN();
147 }
148
149 bool USB_Audio_IsSampleReceived(USB_ClassInfo_Audio_t* AudioInterfaceInfo)
150 {
151 Endpoint_SelectEndpoint(AudioInterfaceInfo->DataOUTEndpointNumber);
152 return Endpoint_IsOUTReceived();
153 }
154
155 bool USB_Audio_IsReadyForNextSample(USB_ClassInfo_Audio_t* AudioInterfaceInfo)
156 {
157 Endpoint_SelectEndpoint(AudioInterfaceInfo->DataINEndpointNumber);
158 return Endpoint_IsINReady();
159 }