1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2026-present Eclipse ThreadX contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** USBX Component */
/** */
/** Audio Class */
/** */
/**************************************************************************/
/**************************************************************************/
/* Include necessary system files. */
#define UX_SOURCE_CODE
#include "ux_api.h"
#include "ux_host_class_audio.h"
#include "ux_host_stack.h"
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _ux_host_class_audio_streaming_sampling_set PORTABLE C */
/* 6.2.1 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function selects the right alternate setting for the audio */
/* streaming interface based on the RAW (PCM like) sampling values */
/* specified by the user, and send sampling frequency requests to */
/* select expected sample rate (if necessary). */
/* */
/* INPUT */
/* */
/* audio Pointer to audio class */
/* audio_sampling Pointer to audio sampling */
/* */
/* OUTPUT */
/* */
/* Completion Status */
/* */
/* CALLS */
/* */
/* _ux_host_class_audio_alternate_setting_locate */
/* Locate alternate setting */
/* _ux_host_stack_class_instance_verify Verify instance is valid */
/* _ux_host_stack_interface_endpoint_get Get interface endpoint */
/* _ux_host_stack_interface_setting_select Select interface */
/* _ux_host_semaphore_get Get semaphore */
/* _ux_host_semaphore_put Put semaphore */
/* _ux_host_mutex_on Get mutex */
/* _ux_host_mutex_off Release mutex */
/* */
/* CALLED BY */
/* */
/* Application */
/* Audio Class */
/* */
/**************************************************************************/
UINT _ux_host_class_audio_streaming_sampling_set(UX_HOST_CLASS_AUDIO *audio, UX_HOST_CLASS_AUDIO_SAMPLING *audio_sampling)
{
UINT status;
UINT alternate_setting;
UX_CONFIGURATION *configuration;
UX_INTERFACE *interface_ptr;
UINT streaming_interface;
UCHAR *descriptor;
ULONG set_frequency;
UCHAR *control_buffer;
UX_DEVICE *device;
UX_TRANSFER *transfer;
ULONG frequency = 0;
ULONG res_bytes;
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_AUDIO_STREAMING_SAMPLING_SET, audio, audio_sampling, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
/* Ensure the instance is valid. */
if (_ux_host_stack_class_instance_verify(_ux_system_host_class_audio_name, (VOID *) audio) != UX_SUCCESS)
return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
/* Protect thread reentry to this instance. */
_ux_host_mutex_on(&audio -> ux_host_class_audio_mutex);
/* Find the correct alternate setting for the sampling desired. */
status = _ux_host_class_audio_alternate_setting_locate(audio, audio_sampling, &alternate_setting);
/* Did we find the alternate setting? */
if (status != UX_SUCCESS)
{
/* Unprotect thread reentry to this instance. */
_ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
return(status);
}
/* We found the alternate setting for the sampling values demanded, now we need
to search its container. */
configuration = audio -> ux_host_class_audio_streaming_interface -> ux_interface_configuration;
interface_ptr = configuration -> ux_configuration_first_interface;
streaming_interface = audio -> ux_host_class_audio_streaming_interface -> ux_interface_descriptor.bInterfaceNumber;
device = audio -> ux_host_class_audio_device;
/* Scan all interfaces. */
while (interface_ptr != UX_NULL)
{
/* We search for both the right interface and alternate setting. */
if ((interface_ptr -> ux_interface_descriptor.bInterfaceNumber == streaming_interface) &&
(interface_ptr -> ux_interface_descriptor.bAlternateSetting == alternate_setting))
{
/* We have found the right interface/alternate setting combination
The stack will select it for us. */
status = _ux_host_stack_interface_setting_select(interface_ptr);
/* If the alternate setting for the streaming interface could be selected, we memorize it. */
if (status == UX_SUCCESS)
{
/* Memorize the interface. */
audio -> ux_host_class_audio_streaming_interface = interface_ptr;
/* Get streaming endpoints. */
status = _ux_host_class_audio_endpoints_get(audio);
if (status != UX_SUCCESS)
{
/* Unprotect thread reentry to this instance. */
_ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
return(status);
}
/* Check if clock frequency needs modification through requests. */
set_frequency = UX_TRUE;
descriptor = audio -> ux_host_class_audio_sampling_descriptor;
#if defined(UX_HOST_CLASS_AUDIO_2_SUPPORT)
if (_ux_host_class_audio_protocol_get(audio) != UX_HOST_CLASS_AUDIO_PROTOCOL_IP_VERSION_01_00)
{
/* Check CSD::[email protected]~1. */
/* If not programmable, no need to set. */
if ((descriptor[5] & UX_CLASS_AUDIO20_CONTROL_MASK) !=
UX_CLASS_AUDIO20_CONTROL_PROGRAMMABLE)
set_frequency = UX_FALSE;
}
else
#endif
{
/* Check FormatTypeI::bSamFreqType@7, tSamFreq@8, tSamFreq@11. */
/* If there is only one frequency, no need to set. */
if ((descriptor[7] == 1) || ((descriptor[7] == 0) &&
(descriptor[8] == descriptor[11]) &&
(descriptor[9] == descriptor[12]) &&
(descriptor[10] == descriptor[13])))
set_frequency = UX_FALSE;
}
/* Send requests to set frequency. */
if (set_frequency)
{
/* Allocate buffer. */
control_buffer = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_CACHE_SAFE_MEMORY, 4);
if (control_buffer == UX_NULL)
{
_ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
return(UX_MEMORY_INSUFFICIENT);
}
/* Get control transfer. */
transfer = &device -> ux_device_control_endpoint.ux_endpoint_transfer_request;
/* Set data to send. */
control_buffer[0] = (UCHAR)UX_DW0(audio_sampling -> ux_host_class_audio_sampling_frequency);
control_buffer[1] = (UCHAR)UX_DW1(audio_sampling -> ux_host_class_audio_sampling_frequency);
control_buffer[2] = (UCHAR)UX_DW2(audio_sampling -> ux_host_class_audio_sampling_frequency);
/* Protect the control endpoint semaphore here. It will be unprotected in the
transfer request function. */
status = _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
if (status != UX_SUCCESS)
{
_ux_utility_memory_free(control_buffer);
_ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
return(status);
}
#if defined(UX_HOST_CLASS_AUDIO_2_SUPPORT)
if (_ux_host_class_audio_protocol_get(audio) != UX_HOST_CLASS_AUDIO_PROTOCOL_IP_VERSION_01_00)
{
/* Set last byte. */
control_buffer[3] = (UCHAR)UX_DW3(audio_sampling -> ux_host_class_audio_sampling_frequency);
/* Create a transfer request for the CUR request. */
transfer -> ux_transfer_request_data_pointer = control_buffer;
transfer -> ux_transfer_request_requested_length = 4;
transfer -> ux_transfer_request_function = UX_CLASS_AUDIO20_CUR;
transfer -> ux_transfer_request_type = UX_REQUEST_OUT | UX_REQUEST_TARGET_INTERFACE | UX_REQUEST_TYPE_CLASS;
transfer -> ux_transfer_request_value = UX_CLASS_AUDIO20_CS_SAM_FREQ_CONTROL << 8;
transfer -> ux_transfer_request_index = audio -> ux_host_class_audio_control_interface_number | ((UINT)descriptor[3] << 8);
status = _ux_host_stack_transfer_request(transfer);
if (status != UX_SUCCESS)
{
_ux_utility_memory_free(control_buffer);
_ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
return(status);
}
/* Issue CUR request. */
status = _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
if (status != UX_SUCCESS)
{
_ux_utility_memory_free(control_buffer);
_ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
return(status);
}
/* Create a transfer request for the CUR request. */
transfer -> ux_transfer_request_data_pointer = control_buffer;
transfer -> ux_transfer_request_requested_length = 4;
transfer -> ux_transfer_request_function = UX_CLASS_AUDIO20_CUR;
transfer -> ux_transfer_request_type = UX_REQUEST_IN | UX_REQUEST_TARGET_INTERFACE | UX_REQUEST_TYPE_CLASS;
transfer -> ux_transfer_request_value = UX_CLASS_AUDIO20_CS_SAM_FREQ_CONTROL << 8;
transfer -> ux_transfer_request_index = audio -> ux_host_class_audio_control_interface_number | ((UINT)descriptor[3] << 8);
status = _ux_host_stack_transfer_request(transfer);
}
else
#endif
{
/* Create a transfer request for the SET_CUR request. */
transfer -> ux_transfer_request_data_pointer = control_buffer;
transfer -> ux_transfer_request_requested_length = 3;
transfer -> ux_transfer_request_function = UX_CLASS_AUDIO10_SET_CUR;
transfer -> ux_transfer_request_type = UX_REQUEST_OUT | UX_REQUEST_TARGET_ENDPOINT | UX_REQUEST_TYPE_CLASS;
transfer -> ux_transfer_request_value = UX_CLASS_AUDIO10_EP_SAMPLING_FREQ_CONTROL << 8;
transfer -> ux_transfer_request_index = audio -> ux_host_class_audio_isochronous_endpoint -> ux_endpoint_descriptor.bEndpointAddress;
status = _ux_host_stack_transfer_request(transfer);
if (status != UX_SUCCESS)
{
_ux_utility_memory_free(control_buffer);
_ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
return(status);
}
/* Issue GET_CUR request. */
status = _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
if (status != UX_SUCCESS)
{
_ux_utility_memory_free(control_buffer);
_ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
return(status);
}
/* Create a transfer request for the GET_CUR request. */
transfer -> ux_transfer_request_data_pointer = control_buffer;
transfer -> ux_transfer_request_requested_length = 3;
transfer -> ux_transfer_request_function = UX_CLASS_AUDIO10_GET_CUR;
transfer -> ux_transfer_request_type = UX_REQUEST_IN | UX_REQUEST_TARGET_ENDPOINT | UX_REQUEST_TYPE_CLASS;
transfer -> ux_transfer_request_value = UX_CLASS_AUDIO10_EP_SAMPLING_FREQ_CONTROL << 8;
transfer -> ux_transfer_request_index = audio -> ux_host_class_audio_isochronous_endpoint -> ux_endpoint_descriptor.bEndpointAddress;
status = _ux_host_stack_transfer_request(transfer);
/* Clear 4th byte in case it's changed occasionally. */
control_buffer[3] = 0;
}
/* Check frequency. */
if (status == UX_SUCCESS)
{
frequency = _ux_utility_long_get(control_buffer);
if (audio_sampling -> ux_host_class_audio_sampling_frequency != frequency)
status = UX_HOST_CLASS_AUDIO_WRONG_FREQUENCY;
}
/* Free buffer. */
_ux_utility_memory_free(control_buffer);
}
/* Update packet processing parameters. */
if (status == UX_SUCCESS)
{
audio -> ux_host_class_audio_packet_freq =
(device -> ux_device_speed == UX_HIGH_SPEED_DEVICE) ?
8000 : 1000;
audio -> ux_host_class_audio_packet_freq <<=
audio -> ux_host_class_audio_isochronous_endpoint ->
ux_endpoint_descriptor.bInterval - 1; /* Max 8000 << 15, no overflow. */
res_bytes = audio_sampling -> ux_host_class_audio_sampling_resolution;
if (UX_OVERFLOW_CHECK_ADD_ULONG(res_bytes, 7))
status = UX_MATH_OVERFLOW;
}
if (status == UX_SUCCESS)
{
res_bytes += 7;
res_bytes >>= 3;
frequency = audio_sampling -> ux_host_class_audio_sampling_frequency;
if (UX_OVERFLOW_CHECK_MULV_ULONG(frequency, audio_sampling -> ux_host_class_audio_sampling_channels))
status = UX_MATH_OVERFLOW;
}
if (status == UX_SUCCESS)
{
frequency *= audio_sampling -> ux_host_class_audio_sampling_channels;
if (UX_OVERFLOW_CHECK_MULV_ULONG(frequency, res_bytes))
status = UX_MATH_OVERFLOW;
}
if (status == UX_SUCCESS)
{
frequency *= res_bytes;
audio -> ux_host_class_audio_packet_fraction =
frequency % audio -> ux_host_class_audio_packet_freq;
audio -> ux_host_class_audio_packet_size =
frequency / audio -> ux_host_class_audio_packet_freq;
}
/* Unprotect thread reentry to this instance. */
_ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
/* Return completion status. */
return(status);
}
}
/* Move to next interface. */
interface_ptr = interface_ptr -> ux_interface_next_interface;
}
/* Unprotect thread reentry to this instance. */
_ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
/* Error trap. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_NO_ALTERNATE_SETTING);
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_NO_ALTERNATE_SETTING, audio, 0, 0, UX_TRACE_ERRORS, 0, 0)
/* We get here if we could not get the right alternate setting. */
return(UX_NO_ALTERNATE_SETTING);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _uxe_host_class_audio_streaming_sampling_set PORTABLE C */
/* 6.3.0 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function checks errors in audio sampling characteristics set */
/* function call. */
/* */
/* INPUT */
/* */
/* audio Pointer to audio class */
/* audio_sampling Pointer to audio sampling */
/* */
/* OUTPUT */
/* */
/* Status */
/* */
/* CALLS */
/* */
/* _ux_host_class_audio_streaming_sampling_set */
/* Set sampling characteristics */
/* */
/* CALLED BY */
/* */
/* Application */
/* */
/**************************************************************************/
UINT _uxe_host_class_audio_streaming_sampling_set(UX_HOST_CLASS_AUDIO *audio, UX_HOST_CLASS_AUDIO_SAMPLING *audio_sampling)
{
/* Sanity checks. */
if ((audio == UX_NULL) || (audio_sampling == UX_NULL))
return(UX_INVALID_PARAMETER);
/* Invoke sampling characteristics set function. */
return(_ux_host_class_audio_streaming_sampling_set(audio, audio_sampling));
}
|