]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/LUFA/Drivers/USB/Class/Host/AudioClassHost.h
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / lib / lufa / LUFA / Drivers / USB / Class / Host / AudioClassHost.h
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2017.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2017  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 disclaims 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_USBClassAudioHost_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/AudioClassHost.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i>
46  *
47  *  \section Sec_USBClassAudioHost_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/AudioClassCommon.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                                 struct
81                                 {
82                                         USB_Pipe_Table_t DataINPipe; /**< Data IN Pipe configuration table. */
83                                         USB_Pipe_Table_t DataOUTPipe; /**< Data OUT Pipe configuration table. */
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                                 } State; /**< State data for the USB class interface within the device. All elements in this section
98                                                   *   <b>may</b> be set to initial values, but may also be ignored to default to sane values when
99                                                   *   the interface is enumerated.
100                                                   */
101                         } USB_ClassInfo_Audio_Host_t;
102
103                 /* Enums: */
104                         /** Enum for the possible error codes returned by the \ref Audio_Host_ConfigurePipes() function. */
105                         enum AUDIO_Host_EnumerationFailure_ErrorCodes_t
106                         {
107                                 AUDIO_ENUMERROR_NoError                    = 0, /**< Configuration Descriptor was processed successfully. */
108                                 AUDIO_ENUMERROR_InvalidConfigDescriptor    = 1, /**< The device returned an invalid Configuration Descriptor. */
109                                 AUDIO_ENUMERROR_NoCompatibleInterfaceFound = 2, /**< A compatible AUDIO interface was not found in the device's Configuration Descriptor. */
110                                 AUDIO_ENUMERROR_PipeConfigurationFailed    = 3, /**< One or more pipes for the specified interface could not be configured correctly. */
111                         };
112
113                 /* Function Prototypes: */
114                         /** Host interface configuration routine, to configure a given Audio host interface instance using the Configuration
115                          *  Descriptor read from an attached USB device. This function automatically updates the given Audio Host instance's
116                          *  state values and configures the pipes required to communicate with the interface if it is found within the
117                          *  device. This should be called once after the stack has enumerated the attached device, while the host state
118                          *  machine is in the Addressed state.
119                          *
120                          *  \param[in,out] AudioInterfaceInfo    Pointer to a structure containing an Audio Class host configuration and state.
121                          *  \param[in]     ConfigDescriptorSize  Length of the attached device's Configuration Descriptor.
122                          *  \param[in]     ConfigDescriptorData  Pointer to a buffer containing the attached device's Configuration Descriptor.
123                          *
124                          *  \return A value from the \ref AUDIO_Host_EnumerationFailure_ErrorCodes_t enum.
125                          */
126                         uint8_t Audio_Host_ConfigurePipes(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
127                                                           uint16_t ConfigDescriptorSize,
128                                                           void* ConfigDescriptorData) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(3);
129
130                         /** Starts or stops the audio streaming for the given configured Audio Host interface, allowing for audio samples to be
131                          *  send and/or received.
132                          *
133                          *  \param[in,out] AudioInterfaceInfo  Pointer to a structure containing an Audio Class host configuration and state.
134                          *  \param[in]     EnableStreaming     Boolean true to enable streaming of the specified interface, \c false to disable
135                          *
136                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.
137                          */
138                         uint8_t Audio_Host_StartStopStreaming(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
139                                                               const bool EnableStreaming) ATTR_NON_NULL_PTR_ARG(1);
140
141                         /** Gets or sets the specified property of a streaming audio class endpoint that is bound to a pipe in the given
142                          *  class instance.
143                          *
144                          *  \param[in,out] AudioInterfaceInfo  Pointer to a structure containing an Audio Class host configuration and state.
145                          *  \param[in]     DataPipeIndex       Index of the data pipe whose bound endpoint is to be altered.
146                          *  \param[in]     EndpointProperty    Property of the endpoint to get or set, a value from \ref Audio_ClassRequests_t.
147                          *  \param[in]     EndpointControl     Parameter of the endpoint to get or set, a value from \ref Audio_EndpointControls_t.
148                          *  \param[in,out] DataLength          For SET operations, the length of the parameter data to set. For GET operations, the maximum
149                          *                                     length of the retrieved data.
150                          *  \param[in,out] Data                Pointer to a location where the parameter data is stored for SET operations, or where
151                          *                                     the retrieved data is to be stored for GET operations.
152                          *
153                          *  \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.
154                          */
155                         uint8_t Audio_Host_GetSetEndpointProperty(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
156                                                                   const uint8_t DataPipeIndex,
157                                                                   const uint8_t EndpointProperty,
158                                                                   const uint8_t EndpointControl,
159                                                                   const uint16_t DataLength,
160                                                                   void* const Data) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(6);
161
162                 /* Inline Functions: */
163                         /** General management task for a given Audio host class interface, required for the correct operation of
164                          *  the interface. This should be called frequently in the main program loop, before the master USB management task
165                          *  \ref USB_USBTask().
166                          *
167                          *  \param[in,out] AudioInterfaceInfo  Pointer to a structure containing an Audio Class host configuration and state.
168                          */
169                         static inline void Audio_Host_USBTask(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
170                                                               ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
171                         static inline void Audio_Host_USBTask(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
172                         {
173                                 (void)AudioInterfaceInfo;
174                         }
175
176                         /** Determines if the given audio interface is ready for a sample to be read from it, and selects the streaming
177                          *  IN pipe ready for reading.
178                          *
179                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or
180                          *       the call will fail.
181                          *
182                          *  \param[in,out] AudioInterfaceInfo  Pointer to a structure containing an Audio Class configuration and state.
183                          *
184                          *  \return Boolean \c true if the given Audio interface has a sample to be read, \c false otherwise.
185                          */
186                         static inline bool Audio_Host_IsSampleReceived(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
187                                                                        ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
188                         static inline bool Audio_Host_IsSampleReceived(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
189                         {
190                                 if ((USB_HostState != HOST_STATE_Configured) || !(AudioInterfaceInfo->State.IsActive))
191                                   return false;
192
193                                 bool SampleReceived = false;
194
195                                 Pipe_SelectPipe(AudioInterfaceInfo->Config.DataINPipe.Address);
196                                 Pipe_Unfreeze();
197                                 SampleReceived = Pipe_IsINReceived();
198                                 Pipe_Freeze();
199
200                                 return SampleReceived;
201                         }
202
203                         /** Determines if the given audio interface is ready to accept the next sample to be written to it, and selects
204                          *  the streaming OUT pipe ready for writing.
205                          *
206                          *  \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or
207                          *       the call will fail.
208                          *
209                          *  \param[in,out] AudioInterfaceInfo  Pointer to a structure containing an Audio Class configuration and state.
210                          *
211                          *  \return Boolean \c true if the given Audio interface is ready to accept the next sample, \c false otherwise.
212                          */
213                         static inline bool Audio_Host_IsReadyForNextSample(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
214                                                                            ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
215                         static inline bool Audio_Host_IsReadyForNextSample(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
216                         {
217                                 if ((USB_HostState != HOST_STATE_Configured) || !(AudioInterfaceInfo->State.IsActive))
218                                   return false;
219
220                                 Pipe_SelectPipe(AudioInterfaceInfo->Config.DataOUTPipe.Address);
221                                 return Pipe_IsOUTReady();
222                         }
223
224                         /** Reads the next 8-bit audio sample from the current audio interface.
225                          *
226                          *  \pre This should be preceded immediately by a call to the \ref Audio_Host_IsSampleReceived() function to ensure
227                          *       that the correct pipe is selected and ready for data.
228                          *
229                          *  \param[in,out] AudioInterfaceInfo  Pointer to a structure containing an Audio Class configuration and state.
230                          *
231                          *  \return  Signed 8-bit audio sample from the audio interface.
232                          */
233                         static inline int8_t Audio_Host_ReadSample8(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
234                                                                     ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
235                         static inline int8_t Audio_Host_ReadSample8(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
236                         {
237                                 int8_t Sample;
238
239                                 (void)AudioInterfaceInfo;
240
241                                 Sample = Pipe_Read_8();
242
243                                 if (!(Pipe_BytesInPipe()))
244                                 {
245                                         Pipe_Unfreeze();
246                                         Pipe_ClearIN();
247                                         Pipe_Freeze();
248                                 }
249
250                                 return Sample;
251                         }
252
253                         /** Reads the next 16-bit audio sample from the current audio interface.
254                          *
255                          *  \pre This should be preceded immediately by a call to the \ref Audio_Host_IsSampleReceived() function to ensure
256                          *       that the correct pipe is selected and ready for data.
257                          *
258                          *  \param[in,out] AudioInterfaceInfo  Pointer to a structure containing an Audio Class configuration and state.
259                          *
260                          *  \return  Signed 16-bit audio sample from the audio interface.
261                          */
262                         static inline int16_t Audio_Host_ReadSample16(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
263                                                                       ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
264                         static inline int16_t Audio_Host_ReadSample16(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
265                         {
266                                 int16_t Sample;
267
268                                 (void)AudioInterfaceInfo;
269
270                                 Sample = (int16_t)Pipe_Read_16_LE();
271
272                                 if (!(Pipe_BytesInPipe()))
273                                 {
274                                         Pipe_Unfreeze();
275                                         Pipe_ClearIN();
276                                         Pipe_Freeze();
277                                 }
278
279                                 return Sample;
280                         }
281
282                         /** Reads the next 24-bit audio sample from the current audio interface.
283                          *
284                          *  \pre This should be preceded immediately by a call to the \ref Audio_Host_IsSampleReceived() function to ensure
285                          *       that the correct pipe is selected and ready for data.
286                          *
287                          *  \param[in,out] AudioInterfaceInfo  Pointer to a structure containing an Audio Class configuration and state.
288                          *
289                          *  \return Signed 24-bit audio sample from the audio interface.
290                          */
291                         static inline int32_t Audio_Host_ReadSample24(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
292                                                                       ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
293                         static inline int32_t Audio_Host_ReadSample24(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo)
294                         {
295                                 int32_t Sample;
296
297                                 (void)AudioInterfaceInfo;
298
299                                 Sample = (((uint32_t)Pipe_Read_8() << 16) | Pipe_Read_16_LE());
300
301                                 if (!(Pipe_BytesInPipe()))
302                                 {
303                                         Pipe_Unfreeze();
304                                         Pipe_ClearIN();
305                                         Pipe_Freeze();
306                                 }
307
308                                 return Sample;
309                         }
310
311                         /** Writes the next 8-bit audio sample to the current audio interface.
312                          *
313                          *  \pre This should be preceded immediately by a call to the \ref Audio_Host_IsReadyForNextSample() function to
314                          *       ensure that the correct pipe is selected and ready for data.
315                          *
316                          *  \param[in,out] AudioInterfaceInfo  Pointer to a structure containing an Audio Class configuration and state.
317                          *  \param[in]     Sample              Signed 8-bit audio sample.
318                          */
319                         static inline void Audio_Host_WriteSample8(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
320                                                                    const int8_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
321                         static inline void Audio_Host_WriteSample8(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
322                                                                    const int8_t Sample)
323                         {
324                                 (void)AudioInterfaceInfo;
325
326                                 Pipe_Write_8(Sample);
327
328                                 if (!(Pipe_IsReadWriteAllowed()))
329                                 {
330                                         Pipe_Unfreeze();
331                                         Pipe_ClearOUT();
332                                         Pipe_WaitUntilReady();
333                                         Pipe_Freeze();
334                                 }
335                         }
336
337                         /** Writes the next 16-bit audio sample to the current audio interface.
338                          *
339                          *  \pre This should be preceded immediately by a call to the \ref Audio_Host_IsReadyForNextSample() function to
340                          *       ensure that the correct pipe is selected and ready for data.
341                          *
342                          *  \param[in,out] AudioInterfaceInfo  Pointer to a structure containing an Audio Class configuration and state.
343                          *  \param[in]     Sample              Signed 16-bit audio sample.
344                          */
345                         static inline void Audio_Host_WriteSample16(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
346                                                                     const int16_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
347                         static inline void Audio_Host_WriteSample16(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
348                                                                     const int16_t Sample)
349                         {
350                                 (void)AudioInterfaceInfo;
351
352                                 Pipe_Write_16_LE(Sample);
353
354                                 if (!(Pipe_IsReadWriteAllowed()))
355                                 {
356                                         Pipe_Unfreeze();
357                                         Pipe_ClearOUT();
358                                         Pipe_WaitUntilReady();
359                                         Pipe_Freeze();
360                                 }
361                         }
362
363                         /** Writes the next 24-bit audio sample to the current audio interface.
364                          *
365                          *  \pre This should be preceded immediately by a call to the \ref Audio_Host_IsReadyForNextSample() function to
366                          *       ensure that the correct pipe is selected and ready for data.
367                          *
368                          *  \param[in,out] AudioInterfaceInfo  Pointer to a structure containing an Audio Class configuration and state.
369                          *  \param[in]     Sample              Signed 24-bit audio sample.
370                          */
371                         static inline void Audio_Host_WriteSample24(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
372                                                                     const int32_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
373                         static inline void Audio_Host_WriteSample24(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
374                                                                     const int32_t Sample)
375                         {
376                                 (void)AudioInterfaceInfo;
377
378                                 Pipe_Write_16_LE(Sample);
379                                 Pipe_Write_8(Sample >> 16);
380
381                                 if (!(Pipe_IsReadWriteAllowed()))
382                                 {
383                                         Pipe_Unfreeze();
384                                         Pipe_ClearOUT();
385                                         Pipe_WaitUntilReady();
386                                         Pipe_Freeze();
387                                 }
388                         }
389
390         /* Private Interface - For use in library only: */
391         #if !defined(__DOXYGEN__)
392                 /* Function Prototypes: */
393                         #if defined(__INCLUDE_FROM_AUDIO_HOST_C)
394                                 static uint8_t DCOMP_Audio_Host_NextAudioControlInterface(void* CurrentDescriptor)
395                                                                                           ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
396                                 static uint8_t DCOMP_Audio_Host_NextAudioStreamInterface(void* CurrentDescriptor)
397                                                                                          ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
398                                 static uint8_t DCOMP_Audio_Host_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor)
399                                                                                                ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
400                         #endif
401         #endif
402
403         /* Disable C linkage for C++ Compilers: */
404                 #if defined(__cplusplus)
405                         }
406                 #endif
407
408 #endif
409
410 /** @} */
411