1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 /**************************************************************************/
12 /**************************************************************************/
13 /**                                                                       */
14 /** USBX Component                                                        */
15 /**                                                                       */
16 /**   Device Audio Class                                                    */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 #define UX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "ux_api.h"
27 #include "ux_device_class_audio.h"
28 #include "ux_device_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_class_audio_entry                        PORTABLE C      */
36 /*                                                           6.2.1        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function is the entry point of the audio class. It             */
44 /*    will be called by the device stack enumeration module when the      */
45 /*    host has sent a SET_CONFIGURATION command and the audio interface   */
46 /*    needs to be mounted.                                                */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    command                               Pointer to class command      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_device_class_audio_initialize         Initialize audio class    */
59 /*    _ux_device_class_audio_uninitialize       Uninitialize audio class  */
60 /*    _ux_device_class_audio_activate           Activate audio class      */
61 /*    _ux_device_class_audio_change             Change audio interface    */
62 /*    _ux_device_class_audio_deactivate         Deactivate audio class    */
63 /*    _ux_device_class_audio_control_request    Request control           */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Device Stack                                                        */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
74 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            returned request status,    */
78 /*                                            resulting in version 6.1.12 */
79 /*  03-08-2023     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            added error checks support, */
81 /*                                            resulting in version 6.2.1  */
82 /*                                                                        */
83 /**************************************************************************/
_ux_device_class_audio_entry(UX_SLAVE_CLASS_COMMAND * command)84 UINT  _ux_device_class_audio_entry(UX_SLAVE_CLASS_COMMAND *command)
85 {
86 
87 UINT        status;
88 
89 
90     /* The command request will tell us we need to do here, either a enumeration
91        query, an activation or a deactivation.  */
92     switch (command -> ux_slave_class_command_request)
93     {
94 
95     case UX_SLAVE_CLASS_COMMAND_INITIALIZE:
96 
97         /* Call the init function of the Audio class.  */
98 #if defined(UX_DEVICE_CLASS_AUDIO_ENABLE_ERROR_CHECKING)
99         status =  _uxe_device_class_audio_initialize(command);
100 #else
101         status =  _ux_device_class_audio_initialize(command);
102 #endif
103 
104         /* Return the completion status.  */
105         return(status);
106 
107     case UX_SLAVE_CLASS_COMMAND_UNINITIALIZE:
108 
109         /* Call the init function of the Audio class.  */
110         status =  _ux_device_class_audio_uninitialize(command);
111 
112         /* Return the completion status.  */
113         return(status);
114 
115     case UX_SLAVE_CLASS_COMMAND_QUERY:
116 
117         /* Check the CLASS definition in the interface descriptor. */
118         if (command -> ux_slave_class_command_class == UX_DEVICE_CLASS_AUDIO_CLASS)
119         {
120             if (command -> ux_slave_class_command_subclass == UX_DEVICE_CLASS_AUDIO_SUBCLASS_CONTROL)
121                 return(UX_SUCCESS);
122             if (command -> ux_slave_class_command_subclass == UX_DEVICE_CLASS_AUDIO_SUBCLASS_AUDIOSTREAMING)
123                 return(UX_SUCCESS);
124         }
125         return(UX_NO_CLASS_MATCH);
126 
127     case UX_SLAVE_CLASS_COMMAND_ACTIVATE:
128 
129         /* The activate command is used when the host has sent a SET_CONFIGURATION command
130            and this interface has to be mounted. Both Bulk endpoints have to be mounted
131            and the audio thread needs to be activated.  */
132         status =  _ux_device_class_audio_activate(command);
133 
134         /* Return the completion status.  */
135         return(status);
136 
137     case UX_SLAVE_CLASS_COMMAND_CHANGE:
138 
139         /* The change command is used when the host has sent a SET_INTERFACE command
140            to go from Alternate Setting 0 to 1 or revert to the default mode.  */
141         status =  _ux_device_class_audio_change(command);
142 
143         /* Return the completion status.  */
144         return(status);
145 
146     case UX_SLAVE_CLASS_COMMAND_DEACTIVATE:
147 
148         /* The deactivate command is used when the device has been extracted.
149            The device endpoints have to be dismounted and the audio thread canceled.  */
150         status =  _ux_device_class_audio_deactivate(command);
151 
152         /* Return the completion status.  */
153         return(status);
154 
155     case UX_SLAVE_CLASS_COMMAND_REQUEST:
156 
157         /* The request command is used when the host sends a command on the control endpoint.  */
158         return _ux_device_class_audio_control_request(command);
159 
160     default:
161 
162         /* Return an error.  */
163         return(UX_FUNCTION_NOT_SUPPORTED);
164     }
165 }
166