1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   Audio Class                                                         */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
27 
28 #include "ux_api.h"
29 #include "ux_host_class_audio.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_audio_write                          PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function writes to the audio streaming interface.              */
46 /*                                                                        */
47 /*    Note the request packet size should not exceed endpoint max packet  */
48 /*    size, and the request length should be aligned with packet size.    */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    audio                                 Pointer to audio class        */
53 /*    audio_transfer_request                Pointer to transfer request   */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Completion Status                                                   */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_host_class_audio_transfer_request Start audio transfer request  */
62 /*    _ux_host_stack_class_instance_verify  Verify instance is valid      */
63 /*    _ux_host_mutex_on                     Get mutex                     */
64 /*    _ux_host_mutex_off                    Release mutex                 */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application                                                         */
69 /*    Audio Class                                                         */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
76 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            refined macros names,       */
80 /*                                            resulting in version 6.1.10 */
81 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
82 /*                                            fixed standalone compile,   */
83 /*                                            resulting in version 6.1.11 */
84 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
85 /*                                            refined packet size manage, */
86 /*                                            protect reentry with mutex, */
87 /*                                            refined transfer implement, */
88 /*                                            fixed error return code,    */
89 /*                                            resulting in version 6.1.12 */
90 /*                                                                        */
91 /**************************************************************************/
_ux_host_class_audio_write(UX_HOST_CLASS_AUDIO * audio,UX_HOST_CLASS_AUDIO_TRANSFER_REQUEST * audio_transfer_request)92 UINT  _ux_host_class_audio_write(UX_HOST_CLASS_AUDIO *audio, UX_HOST_CLASS_AUDIO_TRANSFER_REQUEST *audio_transfer_request)
93 {
94 
95 UINT        status;
96 ULONG       mps;
97 
98     /* If trace is enabled, insert this event into the trace buffer.  */
99     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_AUDIO_WRITE, audio, audio_transfer_request -> ux_host_class_audio_transfer_request_data_pointer,
100                             audio_transfer_request -> ux_host_class_audio_transfer_request_requested_length, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
101 
102     /* Ensure the instance is valid.  */
103     if (_ux_host_stack_class_instance_verify(_ux_system_host_class_audio_name, (VOID *) audio) != UX_SUCCESS)
104     {
105 
106         /* Error trap. */
107         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
108 
109         /* If trace is enabled, insert this event into the trace buffer.  */
110         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, audio, 0, 0, UX_TRACE_ERRORS, 0, 0)
111 
112         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
113     }
114 
115     /* Protect thread reentry to this instance.  */
116     _ux_host_mutex_on(&audio -> ux_host_class_audio_mutex);
117 
118     /* Ensure we have a selected interface that allows isoch transmission.  */
119     if (audio -> ux_host_class_audio_isochronous_endpoint -> ux_endpoint_descriptor.wMaxPacketSize == 0)
120     {
121 
122         /* Unprotect thread reentry to this instance.  */
123         _ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
124 
125         /* Error trap. */
126         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_AUDIO_WRONG_INTERFACE);
127 
128         /* Return error status.  */
129         return(UX_HOST_CLASS_AUDIO_WRONG_INTERFACE);
130     }
131 
132     /* Correct packet size to apply (not exceeding endpoint max packet size).  */
133     mps = _ux_host_class_audio_max_packet_size_get(audio);
134     if ((audio_transfer_request -> ux_host_class_audio_transfer_request_packet_size == 0) ||
135         (audio_transfer_request -> ux_host_class_audio_transfer_request_packet_size > mps))
136         audio_transfer_request -> ux_host_class_audio_transfer_request_packet_size = mps;
137 
138      /* Ask the stack to hook this transfer request to the iso ED.  */
139     status =  _ux_host_class_audio_transfer_request(audio, audio_transfer_request);
140 
141     /* Unprotect thread reentry to this instance.  */
142     _ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
143 
144     /* Return completion status.  */
145     return(status);
146 }
147