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