Add new Audio Class Driver Host demos.
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / Audio.h
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 /** \file
32 * \brief Host mode driver for the library USB Audio 1.0 Class driver.
33 *
34 * Host mode driver for the library USB Audio 1.0 Class driver.
35 *
36 * \note This file should not be included directly. It is automatically included as needed by the USB module driver
37 * dispatch header located in LUFA/Drivers/USB.h.
38 */
39
40 /** \ingroup Group_USBClassAudio
41 * \defgroup Group_USBClassAudioHost Audio 1.0 Class Host Mode Driver
42 *
43 * \section Sec_Dependencies Module Source Dependencies
44 * The following files must be built with any user project that uses this module:
45 * - LUFA/Drivers/USB/Class/Host/Audio.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i>
46 *
47 * \section Sec_ModDescription Module Description
48 * Host Mode USB Class driver framework interface, for the Audio 1.0 USB Class driver.
49 *
50 * @{
51 */
52
53 #ifndef __AUDIO_CLASS_HOST_H__
54 #define __AUDIO_CLASS_HOST_H__
55
56 /* Includes: */
57 #include "../../USB.h"
58 #include "../Common/Audio.h"
59
60 /* Enable C linkage for C++ Compilers: */
61 #if defined(__cplusplus)
62 extern "C" {
63 #endif
64
65 /* Preprocessor Checks: */
66 #if !defined(__INCLUDE_FROM_AUDIO_DRIVER)
67 #error Do not include this file directly. Include LUFA/Drivers/USB.h instead.
68 #endif
69
70 /* Public Interface - May be used in end-application: */
71 /* Type Defines: */
72 /** \brief Audio Class Host Mode Configuration and State Structure.
73 *
74 * Class state structure. An instance of this structure should be made within the user application,
75 * and passed to each of the Audio class driver functions as the \c AudioInterfaceInfo parameter. This
76 * stores each Audio interface's configuration and state information.
77 */
78 typedef struct
79 {
80 const struct
81 {
82 uint8_t DataINPipeNumber; /**< Pipe number of the Audio interface's IN data pipe. */
83 uint8_t DataOUTPipeNumber; /**< Pipe number of the Audio interface's OUT data pipe. */
84 } Config; /**< Config data for the USB class interface within the device. All elements in this section
85 * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
86 */
87 struct
88 {
89 bool IsActive; /**< Indicates if the current interface instance is connected to an attached device, valid
90 * after \ref Audio_Host_ConfigurePipes() is called and the Host state machine is in the
91 * Configured state.
92 */
93 uint8_t ControlInterfaceNumber; /**< Interface index of the Audio Control interface within the attached device. */
94 uint8_t StreamingInterfaceNumber; /**< Interface index of the Audio Streaming interface within the attached device. */
95
96 uint8_t EnabledStreamingAltIndex; /**< Alternative setting index of the Audio Streaming interface when the stream is enabled. */
97
98 uint16_t DataINPipeSize; /**< Size in bytes of the Audio interface's IN data pipe. */
99 uint16_t DataOUTPipeSize; /**< Size in bytes of the Audio interface's OUT data pipe. */
100 } State; /**< State data for the USB class interface within the device. All elements in this section
101 * <b>may</b> be set to initial values, but may also be ignored to default to sane values when
102 * the interface is enumerated.
103 */
104 } USB_ClassInfo_Audio_Host_t;
105
106 /* Enums: */
107 /** Enum for the possible error codes returned by the \ref Audio_Host_ConfigurePipes() function. */
108 enum AUDIO_Host_EnumerationFailure_ErrorCodes_t
109 {
110 AUDIO_ENUMERROR_NoError = 0, /**< Configuration Descriptor was processed successfully. */
111 AUDIO_ENUMERROR_InvalidConfigDescriptor = 1, /**< The device returned an invalid Configuration Descriptor. */
112 AUDIO_ENUMERROR_NoCompatibleInterfaceFound = 2, /**< A compatible AUDIO interface was not found in the device's Configuration Descriptor. */
113 AUDIO_ENUMERROR_PipeConfigurationFailed = 3, /**< One or more pipes for the specified interface could not be configured correctly. */
114 };
115
116 /* Function Prototypes: */
117 /** Host interface configuration routine, to configure a given Audio host interface instance using the Configuration
118 * Descriptor read from an attached USB device. This function automatically updates the given Audio Host instance's
119 * state values and configures the pipes required to communicate with the interface if it is found within the
120 * device. This should be called once after the stack has enumerated the attached device, while the host state
121 * machine is in the Addressed state.
122 *
123 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class host configuration and state.
124 * \param[in] ConfigDescriptorSize Length of the attached device's Configuration Descriptor.
125 * \param[in] DeviceConfigDescriptor Pointer to a buffer containing the attached device's Configuration Descriptor.
126 *
127 * \return A value from the \ref AUDIO_Host_EnumerationFailure_ErrorCodes_t enum.
128 */
129 uint8_t Audio_Host_ConfigurePipes(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
130 uint16_t ConfigDescriptorSize,
131 void* DeviceConfigDescriptor) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(3);
132
133 /** Starts or stops the audio streaming for the given configured Audio Host interface, allowing for audio samples to be
134 * send and/or received.
135 *
136 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class host configuration and state.
137 * \param[in] EnableStreaming Boolean true to enable streaming of the specified interface, false to disable
138 *
139 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.
140 */
141 uint8_t Audio_Host_StartStopStreaming(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
142 bool EnableStreaming);
143
144 /** Gets or sets the specified property of a streaming audio class endpoint that is bound to a pipe in the given
145 * class instance.
146 *
147 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class host configuration and state.
148 * \param[in] DataPipeIndex Index of the data pipe whose bound endpoint is to be altered.
149 * \param[in] EndpointProperty Property of the endpoint to get or set, a value from \ref Audio_ClassRequests_t.
150 * \param[in] EndpointControl Parameter of the endpoint to get or set, a value from \ref Audio_EndpointControls_t.
151 * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
152 * length of the retrieved data.
153 * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where
154 * the retrieved data is to be stored for GET operations.
155 *
156 * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.
157 */
158 uint8_t Audio_GetSetEndpointProperty(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
159 const uint8_t DataPipeIndex,
160 const uint8_t EndpointProperty,
161 const uint8_t EndpointControl,
162 uint16_t const DataLength,
163 void* const Data);
164
165 /* Inline Functions: */
166 /** General management task for a given Audio host class interface, required for the correct operation of
167 * the interface. This should be called frequently in the main program loop, before the master USB management task
168 * \ref USB_USBTask().
169 *
170 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class host configuration and state.
171 */
172 static inline void Audio_Host_USBTask(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo);
173 static inline void Audio_Host_USBTask(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
174 {
175 (void)AudioInterfaceInfo;
176 }
177
178 /** Determines if the given audio interface is ready for a sample to be read from it, and selects the streaming
179 * IN pipe ready for reading.
180 *
181 * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or
182 * the call will fail.
183 *
184 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
185 *
186 * \return Boolean \c true if the given Audio interface has a sample to be read, \c false otherwise.
187 */
188 static inline bool Audio_Host_IsSampleReceived(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
189 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
190 static inline bool Audio_Host_IsSampleReceived(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
191 {
192 if ((USB_HostState != HOST_STATE_Configured) || !(AudioInterfaceInfo->State.IsActive))
193 return false;
194
195 bool SampleReceived = false;
196
197 Pipe_SelectPipe(AudioInterfaceInfo->Config.DataOUTPipeNumber);
198 Pipe_Unfreeze();
199 SampleReceived = Pipe_IsINReceived();
200 Pipe_Freeze();
201 return SampleReceived;
202 }
203
204 /** Determines if the given audio interface is ready to accept the next sample to be written to it, and selects
205 * the streaming OUT pipe ready for writing.
206 *
207 * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or
208 * the call will fail.
209 *
210 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
211 *
212 * \return Boolean \c true if the given Audio interface is ready to accept the next sample, \c false otherwise.
213 */
214 static inline bool Audio_Host_IsReadyForNextSample(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
215 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
216 static inline bool Audio_Host_IsReadyForNextSample(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
217 {
218 if ((USB_HostState != HOST_STATE_Configured) || !(AudioInterfaceInfo->State.IsActive))
219 return false;
220
221 Pipe_SelectPipe(AudioInterfaceInfo->Config.DataINPipeNumber);
222 return Pipe_IsOUTReady();
223 }
224
225 /** Reads the next 8-bit audio sample from the current audio interface.
226 *
227 * \pre This should be preceded immediately by a call to the \ref Audio_Host_IsSampleReceived() function to ensure
228 * that the correct pipe is selected and ready for data.
229 *
230 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
231 *
232 * \return Signed 8-bit audio sample from the audio interface.
233 */
234 static inline int8_t Audio_Host_ReadSample8(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
235 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
236 static inline int8_t Audio_Host_ReadSample8(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
237 {
238 int8_t Sample;
239
240 (void)AudioInterfaceInfo;
241
242 Sample = Pipe_Read_8();
243
244 if (!(Pipe_BytesInPipe()))
245 {
246 Pipe_Unfreeze();
247 Pipe_ClearIN();
248 Pipe_Freeze();
249 }
250
251 return Sample;
252 }
253
254 /** Reads the next 16-bit audio sample from the current audio interface.
255 *
256 * \pre This should be preceded immediately by a call to the \ref Audio_Host_IsSampleReceived() function to ensure
257 * that the correct pipe is selected and ready for data.
258 *
259 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
260 *
261 * \return Signed 16-bit audio sample from the audio interface.
262 */
263 static inline int16_t Audio_Host_ReadSample16(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
264 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
265 static inline int16_t Audio_Host_ReadSample16(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
266 {
267 int16_t Sample;
268
269 (void)AudioInterfaceInfo;
270
271 Sample = (int16_t)Pipe_Read_16_LE();
272
273 if (!(Pipe_BytesInPipe()))
274 {
275 Pipe_Unfreeze();
276 Pipe_ClearIN();
277 Pipe_Freeze();
278 }
279
280 return Sample;
281 }
282
283 /** Reads the next 24-bit audio sample from the current audio interface.
284 *
285 * \pre This should be preceded immediately by a call to the \ref Audio_Host_IsSampleReceived() function to ensure
286 * that the correct pipe is selected and ready for data.
287 *
288 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
289 *
290 * \return Signed 24-bit audio sample from the audio interface.
291 */
292 static inline int32_t Audio_Host_ReadSample24(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
293 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
294 static inline int32_t Audio_Host_ReadSample24(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
295 {
296 int32_t Sample;
297
298 (void)AudioInterfaceInfo;
299
300 Sample = (((uint32_t)Pipe_Read_8() << 16) | Pipe_Read_16_LE());
301
302 if (!(Pipe_BytesInPipe()))
303 {
304 Pipe_Unfreeze();
305 Pipe_ClearIN();
306 Pipe_Freeze();
307 }
308
309 return Sample;
310 }
311
312 /** Writes the next 8-bit audio sample to the current audio interface.
313 *
314 * \pre This should be preceded immediately by a call to the \ref Audio_Host_IsReadyForNextSample() function to
315 * ensure that the correct pipe is selected and ready for data.
316 *
317 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
318 * \param[in] Sample Signed 8-bit audio sample.
319 */
320 static inline void Audio_Host_WriteSample8(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
321 const int8_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
322 static inline void Audio_Host_WriteSample8(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
323 const int8_t Sample)
324 {
325 Pipe_Write_8(Sample);
326
327 if (Pipe_BytesInPipe() == AudioInterfaceInfo->State.DataINPipeSize)
328 {
329 Pipe_Unfreeze();
330 Pipe_ClearOUT();
331 Pipe_Freeze();
332 }
333 }
334
335 /** Writes the next 16-bit audio sample to the current audio interface.
336 *
337 * \pre This should be preceded immediately by a call to the \ref Audio_Host_IsReadyForNextSample() function to
338 * ensure that the correct pipe is selected and ready for data.
339 *
340 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
341 * \param[in] Sample Signed 16-bit audio sample.
342 */
343 static inline void Audio_Host_WriteSample16(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
344 const int16_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
345 static inline void Audio_Host_WriteSample16(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
346 const int16_t Sample)
347 {
348 Pipe_Write_16_LE(Sample);
349
350 if (Pipe_BytesInPipe() == AudioInterfaceInfo->State.DataINPipeSize)
351 {
352 Pipe_Unfreeze();
353 Pipe_ClearOUT();
354 Pipe_Freeze();
355 }
356 }
357
358 /** Writes the next 24-bit audio sample to the current audio interface.
359 *
360 * \pre This should be preceded immediately by a call to the \ref Audio_Host_IsReadyForNextSample() function to
361 * ensure that the correct pipe is selected and ready for data.
362 *
363 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
364 * \param[in] Sample Signed 24-bit audio sample.
365 */
366 static inline void Audio_Host_WriteSample24(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
367 const int32_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
368 static inline void Audio_Host_WriteSample24(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
369 const int32_t Sample)
370 {
371 Pipe_Write_16_LE(Sample);
372 Pipe_Write_8(Sample >> 16);
373
374 if (Pipe_BytesInPipe() == AudioInterfaceInfo->State.DataINPipeSize)
375 {
376 Pipe_Unfreeze();
377 Pipe_ClearOUT();
378 Pipe_Freeze();
379 }
380 }
381
382 /* Private Interface - For use in library only: */
383 #if !defined(__DOXYGEN__)
384 /* Function Prototypes: */
385 #if defined(__INCLUDE_FROM_AUDIO_HOST_C)
386 static uint8_t DComp_NextAudioControlInterface(void* CurrentDescriptor);
387 static uint8_t DComp_NextAudioStreamInterface(void* CurrentDescriptor);
388 static uint8_t DComp_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor);
389 #endif
390 #endif
391
392 /* Disable C linkage for C++ Compilers: */
393 #if defined(__cplusplus)
394 }
395 #endif
396
397 #endif
398
399 /** @} */
400