Oops - typos in alterations to HID_DESCRIPTOR_JOYSTICK().
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / AudioClassDevice.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 Device mode driver for the library USB Audio 1.0 Class driver.
33 *
34 * Device 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_USBClassAudioDevice Audio 1.0 Class Device 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/Device/AudioClassDevice.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i>
46 *
47 * \section Sec_ModDescription Module Description
48 * Device Mode USB Class driver framework interface, for the Audio 1.0 USB Class driver.
49 *
50 * @{
51 */
52
53 #ifndef _AUDIO_CLASS_DEVICE_H_
54 #define _AUDIO_CLASS_DEVICE_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 Device Mode Configuration and State Structure.
73 *
74 * Class state structure. An instance of this structure should be made for each Audio interface
75 * within the user application, and passed to each of the Audio class driver functions as the
76 * \c AudioInterfaceInfo parameter. This stores each Audio interface's configuration and state information.
77 */
78 typedef struct
79 {
80 struct
81 {
82 uint8_t ControlInterfaceNumber; /**< Index of the Audio Control interface within the device this
83 * structure controls.
84 */
85 uint8_t StreamingInterfaceNumber; /**< Index of the Audio Streaming interface within the device this
86 * structure controls.
87 */
88
89 uint8_t DataINEndpointNumber; /**< Endpoint number of the incoming Audio Streaming data, if available
90 * (zero if unused).
91 */
92 uint16_t DataINEndpointSize; /**< Size in bytes of the incoming Audio Streaming data endpoint, if available
93 * (zero if unused).
94 */
95
96 uint8_t DataOUTEndpointNumber; /**< Endpoint number of the outgoing Audio Streaming data, if available
97 * (zero if unused).
98 */
99 uint16_t DataOUTEndpointSize; /**< Size in bytes of the outgoing Audio Streaming data endpoint, if available
100 * (zero if unused).
101 */
102 } Config; /**< Config data for the USB class interface within the device. All elements in this section
103 * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
104 */
105 struct
106 {
107 bool InterfaceEnabled; /**< Set and cleared by the class driver to indicate if the host has enabled the streaming endpoints
108 * of the Audio Streaming interface.
109 */
110 } State; /**< State data for the USB class interface within the device. All elements in this section
111 * are reset to their defaults when the interface is enumerated.
112 */
113 } USB_ClassInfo_Audio_Device_t;
114
115 /* Function Prototypes: */
116 /** Configures the endpoints of a given Audio interface, ready for use. This should be linked to the library
117 * \ref EVENT_USB_Device_ConfigurationChanged() event so that the endpoints are configured when the configuration containing the
118 * given Audio interface is selected.
119 *
120 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
121 *
122 * \return Boolean \c true if the endpoints were successfully configured, \c false otherwise.
123 */
124 bool Audio_Device_ConfigureEndpoints(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
125
126 /** Processes incoming control requests from the host, that are directed to the given Audio class interface. This should be
127 * linked to the library \ref EVENT_USB_Device_ControlRequest() event.
128 *
129 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
130 */
131 void Audio_Device_ProcessControlRequest(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
132
133 /** Audio class driver callback for the setting and retrieval of streaming endpoint properties. This callback must be implemented
134 * in the user application to handle property manipulations on streaming audio endpoints.
135 *
136 * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
137 * the given endpoint index, and should return as fast as possible. When non-NULL, this value may be altered for GET operations
138 * to indicate the size of the retreived data.
139 *
140 * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
141 * of the \c DataLength parameter.
142 *
143 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
144 * \param[in] EndpointProperty Property of the endpoint to get or set, a value from \ref Audio_ClassRequests_t.
145 * \param[in] EndpointAddress Address of the streaming endpoint whose property is being referenced.
146 * \param[in] EndpointControl Parameter of the endpoint to get or set, a value from \ref Audio_EndpointControls_t.
147 * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
148 * length of the retrieved data. When NULL, the function should return whether the given property
149 * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
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 Boolean \c true if the property GET/SET was successful, \c false otherwise
154 */
155 bool CALLBACK_Audio_Device_GetSetEndpointProperty(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
156 const uint8_t EndpointProperty,
157 const uint8_t EndpointAddress,
158 const uint8_t EndpointControl,
159 uint16_t* const DataLength,
160 uint8_t* Data) ATTR_NON_NULL_PTR_ARG(1);
161
162 /** Audio class driver callback for the setting and retrieval of streaming interface properties. This callback must be implemented
163 * in the user application to handle property manipulations on streaming audio interfaces.
164 *
165 * When the DataLength parameter is NULL, this callback should only indicate whether the specified operation is valid for
166 * the given entity and should return as fast as possible. When non-NULL, this value may be altered for GET operations
167 * to indicate the size of the retreived data.
168 *
169 * \note The length of the retrieved data stored into the Data buffer on GET operations should not exceed the initial value
170 * of the \c DataLength parameter.
171 *
172 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
173 * \param[in] Property Property of the interface to get or set, a value from \ref Audio_ClassRequests_t.
174 * \param[in] EntityAddress Address of the audio entity whose property is being referenced.
175 * \param[in] Parameter Parameter of the entity to get or set, specific to each type of entity (see USB Audio specification).
176 * \param[in,out] DataLength For SET operations, the length of the parameter data to set. For GET operations, the maximum
177 * length of the retrieved data. When NULL, the function should return whether the given property
178 * and parameter is valid for the requested endpoint without reading or modifying the Data buffer.
179 * \param[in,out] Data Pointer to a location where the parameter data is stored for SET operations, or where
180 * the retrieved data is to be stored for GET operations.
181 *
182 * \return Boolean \c true if the property GET/SET was successful, \c false otherwise
183 */
184 bool CALLBACK_Audio_Device_GetSetInterfaceProperty(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
185 const uint8_t Property,
186 const uint8_t EntityAddress,
187 const uint16_t Parameter,
188 uint16_t* const DataLength,
189 uint8_t* Data) ATTR_NON_NULL_PTR_ARG(1);
190
191 /** Audio class driver event for an Audio Stream start/stop change. This event fires each time the device receives a stream enable or
192 * disable control request from the host, to start and stop the audio stream. The current state of the stream can be determined by the
193 * State.InterfaceEnabled value inside the Audio interface structure passed as a parameter.
194 *
195 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
196 */
197 void EVENT_Audio_Device_StreamStartStop(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo);
198
199 /* Inline Functions: */
200 /** General management task for a given Audio class interface, required for the correct operation of the interface. This should
201 * be called frequently in the main program loop, before the master USB management task \ref USB_USBTask().
202 *
203 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
204 */
205 static inline void Audio_Device_USBTask(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
206 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
207 static inline void Audio_Device_USBTask(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
208 {
209 (void)AudioInterfaceInfo;
210 }
211
212 /** Determines if the given audio interface is ready for a sample to be read from it, and selects the streaming
213 * OUT endpoint ready for reading.
214 *
215 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
216 * the call will fail.
217 *
218 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
219 *
220 * \return Boolean \c true if the given Audio interface has a sample to be read, \c false otherwise.
221 */
222 static inline bool Audio_Device_IsSampleReceived(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
223 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
224 static inline bool Audio_Device_IsSampleReceived(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
225 {
226 if ((USB_DeviceState != DEVICE_STATE_Configured) || !(AudioInterfaceInfo->State.InterfaceEnabled))
227 return false;
228
229 Endpoint_SelectEndpoint(AudioInterfaceInfo->Config.DataOUTEndpointNumber);
230 return Endpoint_IsOUTReceived();
231 }
232
233 /** Determines if the given audio interface is ready to accept the next sample to be written to it, and selects
234 * the streaming IN endpoint ready for writing.
235 *
236 * \pre This function must only be called when the Device state machine is in the \ref DEVICE_STATE_Configured state or
237 * the call will fail.
238 *
239 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
240 *
241 * \return Boolean \c true if the given Audio interface is ready to accept the next sample, \c false otherwise.
242 */
243 static inline bool Audio_Device_IsReadyForNextSample(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
244 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
245 static inline bool Audio_Device_IsReadyForNextSample(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
246 {
247 if ((USB_DeviceState != DEVICE_STATE_Configured) || !(AudioInterfaceInfo->State.InterfaceEnabled))
248 return false;
249
250 Endpoint_SelectEndpoint(AudioInterfaceInfo->Config.DataINEndpointNumber);
251 return Endpoint_IsINReady();
252 }
253
254 /** Reads the next 8-bit audio sample from the current audio interface.
255 *
256 * \pre This should be preceded immediately by a call to the \ref Audio_Device_IsSampleReceived() function to ensure
257 * that the correct endpoint 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 8-bit audio sample from the audio interface.
262 */
263 static inline int8_t Audio_Device_ReadSample8(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
264 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
265 static inline int8_t Audio_Device_ReadSample8(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
266 {
267 int8_t Sample;
268
269 (void)AudioInterfaceInfo;
270
271 Sample = Endpoint_Read_8();
272
273 if (!(Endpoint_BytesInEndpoint()))
274 Endpoint_ClearOUT();
275
276 return Sample;
277 }
278
279 /** Reads the next 16-bit audio sample from the current audio interface.
280 *
281 * \pre This should be preceded immediately by a call to the \ref Audio_Device_IsSampleReceived() function to ensure
282 * that the correct endpoint is selected and ready for data.
283 *
284 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
285 *
286 * \return Signed 16-bit audio sample from the audio interface.
287 */
288 static inline int16_t Audio_Device_ReadSample16(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
289 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
290 static inline int16_t Audio_Device_ReadSample16(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
291 {
292 int16_t Sample;
293
294 (void)AudioInterfaceInfo;
295
296 Sample = (int16_t)Endpoint_Read_16_LE();
297
298 if (!(Endpoint_BytesInEndpoint()))
299 Endpoint_ClearOUT();
300
301 return Sample;
302 }
303
304 /** Reads the next 24-bit audio sample from the current audio interface.
305 *
306 * \pre This should be preceded immediately by a call to the \ref Audio_Device_IsSampleReceived() function to ensure
307 * that the correct endpoint is selected and ready for data.
308 *
309 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
310 *
311 * \return Signed 24-bit audio sample from the audio interface.
312 */
313 static inline int32_t Audio_Device_ReadSample24(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
314 ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
315 static inline int32_t Audio_Device_ReadSample24(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
316 {
317 int32_t Sample;
318
319 (void)AudioInterfaceInfo;
320
321 Sample = (((uint32_t)Endpoint_Read_8() << 16) | Endpoint_Read_16_LE());
322
323 if (!(Endpoint_BytesInEndpoint()))
324 Endpoint_ClearOUT();
325
326 return Sample;
327 }
328
329 /** Writes the next 8-bit audio sample to the current audio interface.
330 *
331 * \pre This should be preceded immediately by a call to the \ref Audio_Device_IsReadyForNextSample() function to
332 * ensure that the correct endpoint is selected and ready for data.
333 *
334 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
335 * \param[in] Sample Signed 8-bit audio sample.
336 */
337 static inline void Audio_Device_WriteSample8(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
338 const int8_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
339 static inline void Audio_Device_WriteSample8(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
340 const int8_t Sample)
341 {
342 Endpoint_Write_8(Sample);
343
344 if (Endpoint_BytesInEndpoint() == AudioInterfaceInfo->Config.DataINEndpointSize)
345 Endpoint_ClearIN();
346 }
347
348 /** Writes the next 16-bit audio sample to the current audio interface.
349 *
350 * \pre This should be preceded immediately by a call to the \ref Audio_Device_IsReadyForNextSample() function to
351 * ensure that the correct endpoint is selected and ready for data.
352 *
353 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
354 * \param[in] Sample Signed 16-bit audio sample.
355 */
356 static inline void Audio_Device_WriteSample16(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
357 const int16_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
358 static inline void Audio_Device_WriteSample16(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
359 const int16_t Sample)
360 {
361 Endpoint_Write_16_LE(Sample);
362
363 if (Endpoint_BytesInEndpoint() == AudioInterfaceInfo->Config.DataINEndpointSize)
364 Endpoint_ClearIN();
365 }
366
367 /** Writes the next 24-bit audio sample to the current audio interface.
368 *
369 * \pre This should be preceded immediately by a call to the \ref Audio_Device_IsReadyForNextSample() function to
370 * ensure that the correct endpoint is selected and ready for data.
371 *
372 * \param[in,out] AudioInterfaceInfo Pointer to a structure containing an Audio Class configuration and state.
373 * \param[in] Sample Signed 24-bit audio sample.
374 */
375 static inline void Audio_Device_WriteSample24(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
376 const int32_t Sample) ATTR_NON_NULL_PTR_ARG(1) ATTR_ALWAYS_INLINE;
377 static inline void Audio_Device_WriteSample24(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo,
378 const int32_t Sample)
379 {
380 Endpoint_Write_16_LE(Sample);
381 Endpoint_Write_8(Sample >> 16);
382
383 if (Endpoint_BytesInEndpoint() == AudioInterfaceInfo->Config.DataINEndpointSize)
384 Endpoint_ClearIN();
385 }
386
387 /* Private Interface - For use in library only: */
388 #if !defined(__DOXYGEN__)
389 /* Function Prototypes: */
390 #if defined(__INCLUDE_FROM_AUDIO_DEVICE_C)
391 void Audio_Device_Event_Stub(void) ATTR_CONST;
392
393 void EVENT_Audio_Device_StreamStartStop(USB_ClassInfo_Audio_Device_t* const AudioInterfaceInfo)
394 ATTR_WEAK ATTR_NON_NULL_PTR_ARG(1) ATTR_ALIAS(Audio_Device_Event_Stub);
395 #endif
396
397 #endif
398
399 /* Disable C linkage for C++ Compilers: */
400 #if defined(__cplusplus)
401 }
402 #endif
403
404 #endif
405
406 /** @} */
407