summaryrefslogtreecommitdiff
path: root/common/usbx_host_classes/src/ux_host_class_audio_raw_sampling_parse.c
blob: 46ee9c8253482ae9fd6f287a895ef147d25aa323 (plain)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
/***************************************************************************
 * 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"


struct UX_HOST_CLASS_AUDIO10_SAM_PARSER {
    UINT    (*parse_function)(VOID  *arg,
                              UCHAR *packed_interface_descriptor,
                              UX_HOST_CLASS_AUDIO_SAMPLING_CHARACTERISTICS *sam_attr
                            );
    UCHAR   *arg;
    UX_HOST_CLASS_AUDIO
            *audio;
};
struct UX_HOST_CLASS_AUDIO20_SAM_PARSER {
    UINT    (*parse_function)(VOID  *arg,
                              UCHAR *packed_interface_descriptor,
                              UX_HOST_CLASS_AUDIO_SAMPLING_CHARACTERISTICS *sam_attr
                            );
    UCHAR   *arg;
    UX_HOST_CLASS_AUDIO
            *audio;
    UCHAR   *as_header;
    UINT    status;
};


static UINT _ux_host_class_audio10_sam_parse_func(VOID *arg,
                            UCHAR *packed_interface_descriptor,
                            UCHAR *packed_endpoint_descriptor,
                            UCHAR *packed_audio_descriptor);
static inline UINT _ux_host_class_audio10_sampling_parse(UX_HOST_CLASS_AUDIO *audio,
        UINT(*parse_function)(VOID  *arg,
                              UCHAR *packed_interface_descriptor,
                              UX_HOST_CLASS_AUDIO_SAMPLING_CHARACTERISTICS *sam_attr),
        VOID* arg);
#if defined(UX_HOST_CLASS_AUDIO_2_SUPPORT)
static UINT _ux_host_class_audio20_sam_parse_func(VOID *arg,
                            UCHAR *packed_interface_descriptor,
                            UCHAR *packed_endpoint_descriptor,
                            UCHAR *packed_audio_descriptor);

static inline UINT _ux_host_class_audio20_sampling_parse(UX_HOST_CLASS_AUDIO *audio,
        UINT(*parse_function)(VOID  *arg,
                              UCHAR *packed_interface_descriptor,
                              UX_HOST_CLASS_AUDIO_SAMPLING_CHARACTERISTICS *sam_attr),
        VOID* arg);
#endif


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_class_audio_raw_sampling_parse             PORTABLE C      */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function parses all possible RAW (PCM like) sampling           */
/*    characteristics for current clock source and clock selector         */
/*    settings.                                                           */
/*                                                                        */
/*    For sampling characteristics of different clock settings,           */
/*    use ux_host_class_audio_descriptors_parse to get their IDs and      */
/*    use _ux_host_class_audio_control_request to change settings.        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    audio                                 Pointer to audio class        */
/*    parse_function                        Pointer to parse function,    */
/*                                          returns 0 to continue parse,  */
/*                                          others to terminate parse.    */
/*    arg                                   Pointer to parse arguments    */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_stack_class_instance_verify  Verify instance is valid      */
/*    _ux_host_semaphore_get                Get semaphore                 */
/*    _ux_host_semaphore_put                Put semaphore                 */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*    Audio Class                                                         */
/*                                                                        */
/**************************************************************************/
UINT _ux_host_class_audio_raw_sampling_parse(UX_HOST_CLASS_AUDIO *audio,
        UINT(*parse_function)(VOID  *arg,
                              UCHAR *packed_interface_descriptor,
                              UX_HOST_CLASS_AUDIO_SAMPLING_CHARACTERISTICS *sam_attr),
        VOID* arg)
{

    /* Ensure the instance is valid.  */
    if (_ux_host_stack_class_instance_verify(_ux_system_host_class_audio_name, (VOID *) audio) != UX_SUCCESS)
    {

        /* Error trap. */
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);

        /* If trace is enabled, insert this event into the trace buffer.  */
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, audio, 0, 0, UX_TRACE_ERRORS, 0, 0)

        return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
    }

#if defined(UX_HOST_CLASS_AUDIO_2_SUPPORT)
    if (_ux_host_class_audio_protocol_get(audio) == UX_HOST_CLASS_AUDIO_PROTOCOL_IP_VERSION_02_00)
        return(_ux_host_class_audio20_sampling_parse(audio, parse_function, arg));
#endif

    return(_ux_host_class_audio10_sampling_parse(audio, parse_function, arg));
}

static UINT _ux_host_class_audio10_sam_parse_func(VOID *arg,
                            UCHAR *packed_interface_descriptor,
                            UCHAR *packed_endpoint_descriptor,
                            UCHAR *packed_audio_descriptor)
{
struct UX_HOST_CLASS_AUDIO10_SAM_PARSER           *parser = (struct UX_HOST_CLASS_AUDIO10_SAM_PARSER *)arg;
UX_HOST_CLASS_AUDIO_SAMPLING_CHARACTERISTICS    sam_attr;
ULONG                                           n, offset;
UINT                                            status;
UINT descriptor_length = packed_audio_descriptor[0];

    UX_PARAMETER_NOT_USED(packed_endpoint_descriptor);

    /* Check bInterfaceNumber @ 2.  */
    if (packed_interface_descriptor[2] !=
        parser -> audio -> ux_host_class_audio_streaming_interface ->
                            ux_interface_descriptor.bInterfaceNumber)
        return(0);

    /* Check bDescriptorType @ 1.  */
    if (packed_audio_descriptor[1] != UX_HOST_CLASS_AUDIO_CS_INTERFACE)
        return(0);

    /* Check bDescriptorSubType @ 2.  */
    if (packed_audio_descriptor[2] != UX_HOST_CLASS_AUDIO_CS_FORMAT_TYPE)
        return(0);

    if (descriptor_length < 4)
    {
        /* Error trap. */
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);

        /* If trace is enabled, insert this event into the trace buffer.  */
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor_length, 0, 0, UX_TRACE_ERRORS, 0, 0)

        return(UX_DESCRIPTOR_CORRUPTED);
    }

    /* Check bFormatType @ 3.  */
    if (packed_audio_descriptor[3] != UX_HOST_CLASS_AUDIO_FORMAT_TYPE_I)
        return(0);

    if (descriptor_length < 8)
    {
        /* Error trap. */
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);

        /* If trace is enabled, insert this event into the trace buffer.  */
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor_length, 0, 0, UX_TRACE_ERRORS, 0, 0)

        return(UX_DESCRIPTOR_CORRUPTED);
    }

    /* Get bNrChannels @ 4.  */
    sam_attr.ux_host_class_audio_sampling_characteristics_channels = packed_audio_descriptor[4];

    /* Get bBitResolution @ 6.  */
    sam_attr.ux_host_class_audio_sampling_characteristics_resolution = packed_audio_descriptor[6];

    /* No clock ID (set to 0), no mul, div (set to 1).  */
    sam_attr.ux_host_class_audio_sampling_characteristics_descriptor = packed_audio_descriptor;
    sam_attr.ux_host_class_audio_sampling_characteristics_clock_mul = 1;
    sam_attr.ux_host_class_audio_sampling_characteristics_clock_div = 1;

    /* Check bSamFreqType @ 7.  */
    if (packed_audio_descriptor[7] == 0)
    {
        if (descriptor_length < 14)
        {
          /* Error trap. */
          _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);

          /* If trace is enabled, insert this event into the trace buffer.  */
          UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor_length, 0, 0, UX_TRACE_ERRORS, 0, 0)

          return(UX_DESCRIPTOR_CORRUPTED);
        }

        /* Continuous, get dLowSamFreq and dHighSamFreq.  */
        sam_attr.ux_host_class_audio_sampling_characteristics_frequency_low =
                                    ((ULONG)packed_audio_descriptor[8]) +
                                    ((ULONG)packed_audio_descriptor[9]  << 8) +
                                    ((ULONG)packed_audio_descriptor[10] << 16);
        sam_attr.ux_host_class_audio_sampling_characteristics_frequency_high =
                                    ((ULONG)packed_audio_descriptor[11]) +
                                    ((ULONG)packed_audio_descriptor[12]  << 8) +
                                    ((ULONG)packed_audio_descriptor[13] << 16);

        /* Parse this sampling characteristic.  */
        status = parser->parse_function(parser->arg, packed_interface_descriptor, &sam_attr);
        return(status);
    }
    else
    {
        if (descriptor_length < (UINT)(8 + (3 * packed_audio_descriptor[7])))
        {
          /* Error trap. */
          _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);

          /* If trace is enabled, insert this event into the trace buffer.  */
          UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor_length, 0, 0, UX_TRACE_ERRORS, 0, 0)

          return(UX_DESCRIPTOR_CORRUPTED);
        }

        /* Parse list of sampling characteristics.  */
        for (n = 0, offset = 8;
             n < packed_audio_descriptor[7];
             n ++, offset += 3)
        {
            sam_attr.ux_host_class_audio_sampling_characteristics_frequency_low =
                                    ((ULONG)packed_audio_descriptor[offset]) +
                                    ((ULONG)packed_audio_descriptor[offset+1]  << 8) +
                                    ((ULONG)packed_audio_descriptor[offset+2] << 16);

            /* Not range, frequency high = low.  */
            sam_attr.ux_host_class_audio_sampling_characteristics_frequency_high =
                sam_attr.ux_host_class_audio_sampling_characteristics_frequency_low;

            /* Parse this sampling characteristic.  */
            status = parser->parse_function(parser->arg, packed_interface_descriptor, &sam_attr);

            /* If status is not 0, it terminate parsing.  */
            if (status != 0)
                return(status);
        }
    }
    return(0);
}

static inline UINT _ux_host_class_audio10_sampling_parse(UX_HOST_CLASS_AUDIO *audio,
        UINT(*parse_function)(VOID  *arg,
                              UCHAR *packed_interface_descriptor,
                              UX_HOST_CLASS_AUDIO_SAMPLING_CHARACTERISTICS *sam_attr),
        VOID* arg)
{

struct UX_HOST_CLASS_AUDIO10_SAM_PARSER     parser;

    /* No need to protect thread reentry to this instance (no requests).  */

    parser.parse_function = parse_function;
    parser.arg = arg;
    parser.audio = audio;
    return(_ux_host_class_audio_descriptors_parse(audio,
                    _ux_host_class_audio10_sam_parse_func, (VOID *)&parser));
}

#if defined(UX_HOST_CLASS_AUDIO_2_SUPPORT)
struct UX_HOST_CLASS_AUDIO_AC_DESCR_FINDER_STRUCT
{
    UCHAR   *descriptor;
    UCHAR   interface_num;
    UCHAR   subtype;
    UCHAR   id;
};
static UINT  _ux_host_class_audio_ac_find_parse(VOID  *arg,
                              UCHAR *packed_interface_descriptor,
                              UCHAR *packed_endpoint_descriptor,
                              UCHAR *packed_audio_descriptor)
{
struct UX_HOST_CLASS_AUDIO_AC_DESCR_FINDER_STRUCT *finder = (struct UX_HOST_CLASS_AUDIO_AC_DESCR_FINDER_STRUCT *)arg;
UINT descriptor_length = packed_audio_descriptor[0];

    UX_PARAMETER_NOT_USED(packed_endpoint_descriptor);

    /* Check interface number (bInterfaceNumber @ 2).  */
    if (packed_interface_descriptor[2] != finder -> interface_num)
        return(0);

    /* Check bDescriptorType @ 1.  */
    if (packed_audio_descriptor[1] != UX_HOST_CLASS_AUDIO_CS_INTERFACE)
        return(0);

    /* Get finding data.  */
    finder = (struct UX_HOST_CLASS_AUDIO_AC_DESCR_FINDER_STRUCT *)arg;

    /* Check bDescriptorSubType @ 2.  */
    if (packed_audio_descriptor[2] != finder -> subtype)
        return(0);

    if (descriptor_length < 4)
    {
        /* Error trap. */
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);

        /* If trace is enabled, insert this event into the trace buffer.  */
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor_length, 0, 0, UX_TRACE_ERRORS, 0, 0)

        return(0);
    }

    /* Check bEntityID @ 3.  */
    if (packed_audio_descriptor[3] != finder -> id)
        return(0);

    /* Found it, break parsing loop.  */
    finder -> descriptor = packed_audio_descriptor;
    return(1);
}
static UCHAR *_ux_host_class_audio_ac_find(UX_HOST_CLASS_AUDIO *audio, UCHAR subtype, UCHAR id)
{

struct UX_HOST_CLASS_AUDIO_AC_DESCR_FINDER_STRUCT   finder;
UINT                                                status;

    finder.descriptor    = UX_NULL;
    finder.interface_num = (UCHAR)audio -> ux_host_class_audio_control_interface_number;
    finder.subtype       = subtype;
    finder.id            = id;
    status = _ux_host_class_audio_descriptors_parse(audio,
                            _ux_host_class_audio_ac_find_parse,
                            &finder);
    if (status != UX_SUCCESS)
        return(UX_NULL);
    return(finder.descriptor);
}

static inline UINT _ux_host_class_audio20_clock_get(UX_HOST_CLASS_AUDIO *audio,
                    UCHAR terminal_link,
                    UCHAR **csd, ULONG *clk_numerator, ULONG *clk_denominator)
{

UX_DEVICE       *device;
UX_ENDPOINT     *endpoint;
UX_TRANSFER     *request;
UCHAR           *control_buffer = UX_NULL;
UCHAR           *descriptor;
UCHAR           id;
ULONG           numerator = 1, denominator = 1;
UCHAR           scan_round = 0;
UINT            status;

    if (audio -> ux_host_class_audio_type == UX_HOST_CLASS_AUDIO_INPUT)
    {

        /* If audio input, streaming is from output terminal (OT).  */
        descriptor = _ux_host_class_audio_ac_find(audio,
                                        UX_CLASS_AUDIO20_AC_OUTPUT_TERMINAL,
                                        (UCHAR)terminal_link);
        if (descriptor == UX_NULL)
            return(UX_DESCRIPTOR_CORRUPTED);

        /* Get OTD::bCSourceID @ 8.  */
        id = descriptor[8];
    }
    else
    {

        /* If audio output, streaming is to input terminal (IT).  */
        descriptor = _ux_host_class_audio_ac_find(audio,
                                        UX_CLASS_AUDIO20_AC_INPUT_TERMINAL,
                                        (UCHAR)terminal_link);
        if (descriptor == UX_NULL)
            return(UX_DESCRIPTOR_CORRUPTED);

        /* Get ITD::bCSourceID @ 7.  */
        id = descriptor[7];
    }

    /* Get control endpoint and request, for possible control requests.  */
    device = audio -> ux_host_class_audio_device;
    endpoint = &device -> ux_device_control_endpoint;
    request = &endpoint -> ux_endpoint_transfer_request;

    /* Get clock source descriptor.  */
    do
    {

        /* Try to find ClockSource CSD.  */
        descriptor = _ux_host_class_audio_ac_find(audio,
                                    UX_CLASS_AUDIO20_AC_CLOCK_SOURCE, id);
        if (descriptor != UX_NULL)
        {

            /* Finally we get clock source.  */
            *csd = descriptor;
            *clk_numerator = numerator;
            *clk_denominator = denominator;
            status = UX_SUCCESS;
            break;
        }

        /* Try to find ClockMultiplier CMD.  */
        descriptor = _ux_host_class_audio_ac_find(audio,
                                    UX_CLASS_AUDIO20_AC_CLOCK_MULTIPLIER, id);
        if (descriptor != UX_NULL)
        {

            /* Get numerator, denominator.  */

            /* Allocate buffer for requests.  */
            control_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, 4);
            if (control_buffer == UX_NULL)
            {

                /* Unprotect device control.  */
                status = UX_MEMORY_INSUFFICIENT;
                break;
            }

            /* Protect device control endpoint.  */
            status = _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
            if (status != UX_SUCCESS)
            {
                status = UX_SEMAPHORE_ERROR;
                break;
            }

            /* Create a transfer request for the GET_CUR LAYOUT 2 request.  */
            request -> ux_transfer_request_data_pointer =      control_buffer;
            request -> ux_transfer_request_requested_length =  2;
            request -> ux_transfer_request_function =          UX_CLASS_AUDIO20_CUR;
            request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
            request -> ux_transfer_request_value =             0 | (UX_CLASS_AUDIO20_CM_NUMERATOR_CONTROL << 8);
            request -> ux_transfer_request_index =             audio -> ux_host_class_audio_control_interface_number | ((ULONG)id << 8);
            *(ULONG *)control_buffer = 0;
            status = _ux_host_stack_transfer_request(request);
            if ((status != UX_SUCCESS) ||
                (request -> ux_transfer_request_actual_length != 2) ||
                (*(ULONG *)control_buffer == 0))
            {
                status = UX_TRANSFER_ERROR;
                break;
            }

            /* Update numerator.  */
            numerator *= *(ULONG *)control_buffer;

            /* Protect device control endpoint.  */
            status = _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
            if (status != UX_SUCCESS)
            {
                status = UX_SEMAPHORE_ERROR;
                break;
            }

            /* Create a transfer request for the GET_CUR request.  */
            request -> ux_transfer_request_data_pointer =      control_buffer;
            request -> ux_transfer_request_requested_length =  2;
            request -> ux_transfer_request_function =          UX_CLASS_AUDIO20_CUR;
            request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
            request -> ux_transfer_request_value =             0 | (UX_CLASS_AUDIO20_CM_DENOMINATOR_CONTROL << 8);
            request -> ux_transfer_request_index =             audio -> ux_host_class_audio_control_interface_number | ((ULONG)id << 8);
            *(ULONG *)control_buffer = 0;
            status = _ux_host_stack_transfer_request(request);
            if ((status != UX_SUCCESS) ||
                (request -> ux_transfer_request_actual_length != 2) ||
                (*(ULONG *)control_buffer == 0))
            {
                status = UX_TRANSFER_ERROR;
                break;
            }

            /* Update denominator.  */
            denominator *= *(ULONG *)control_buffer;

            /* Get CMD::bCSourceID @ 4.  */
            id = descriptor[4];

            /* Next round of scan.  */
            scan_round ++;
            continue;
        }

        /* Try to find ClockSelector CSD.  */
        descriptor = _ux_host_class_audio_ac_find(audio,
                                    UX_CLASS_AUDIO20_AC_CLOCK_SELECTOR, id);
        if (descriptor != UX_NULL)
        {

            /* Get current selected InPin.  */

            /* Allocate buffer for requests.  */
            if (control_buffer == UX_NULL)
            {
                control_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, 4);
                if (control_buffer == UX_NULL)
                {

                    /* Unprotect device control.  */
                    status = UX_MEMORY_INSUFFICIENT;
                    break;
                }
            }

            /* Protect device control endpoint.  */
            status = _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
            if (status != UX_SUCCESS)
            {
                status = UX_SEMAPHORE_ERROR;
                break;
            }

            /* Create a transfer request for the GET_CUR LAYOUT 1 request.  */
            request -> ux_transfer_request_data_pointer =      control_buffer;
            request -> ux_transfer_request_requested_length =  1;
            request -> ux_transfer_request_function =          UX_CLASS_AUDIO20_CUR;
            request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
            request -> ux_transfer_request_value =             0 | (UX_CLASS_AUDIO20_CX_CLOCK_SELECTOR_CONTROL << 8);
            request -> ux_transfer_request_index =             audio -> ux_host_class_audio_control_interface_number | ((ULONG)id << 8);
            *(ULONG *)control_buffer = 0;
            status = _ux_host_stack_transfer_request(request);
            if ((status != UX_SUCCESS) ||
                (request -> ux_transfer_request_actual_length != 1) ||
                (*(ULONG *)control_buffer == 0))
            {
                status = UX_TRANSFER_ERROR;
                break;
            }

            /* Update id from CSD::baCSourceID @ 5.  */
            id = descriptor[5 + *(ULONG *)control_buffer - 1];

            /* Next round of scan.  */
            scan_round ++;
            continue;
        }

        /* Nothing found!!!  */
        status = UX_DESCRIPTOR_CORRUPTED;
    } while(status == UX_SUCCESS);

    /* Free allocated resources.  */
    if (control_buffer)
        _ux_utility_memory_free(control_buffer);

    /* Return with status.  */
    return(status);
}

static UINT _ux_host_class_audio20_sam_parse_func(VOID *arg,
                            UCHAR *packed_interface_descriptor,
                            UCHAR *packed_endpoint_descriptor,
                            UCHAR *packed_audio_descriptor)
{

struct UX_HOST_CLASS_AUDIO20_SAM_PARSER         *parser = (struct UX_HOST_CLASS_AUDIO20_SAM_PARSER *)arg;
UX_HOST_CLASS_AUDIO                             *audio = parser -> audio;

UCHAR                                           *csd;
ULONG                                           clk_id, clk_mul = 1, clk_div = 1;
UINT                                            status;
UX_DEVICE                                       *device;
UX_ENDPOINT                                     *endpoint;
UX_TRANSFER                                     *transfer;
UCHAR                                           *buffer;
ULONG                                           n_sub, param_len, offset;
UX_HOST_CLASS_AUDIO_SAMPLING_CHARACTERISTICS    sam_attr;
UINT descriptor_length = packed_audio_descriptor[0];

    UX_PARAMETER_NOT_USED(packed_endpoint_descriptor);

    /* Check bInterfaceNumber @ 2 to confirm inside AS.  */
    if (packed_interface_descriptor[2] !=
        audio -> ux_host_class_audio_streaming_interface ->
                    ux_interface_descriptor.bInterfaceNumber)
        return(0);

    /* Check bDescriptorType @ 1 to confirm AS CS descriptor.  */
    if (packed_audio_descriptor[1] != UX_CLASS_AUDIO20_CS_INTERFACE)
        return(0);

    /* Check bDescriptorSubType@2 to confirm AS_HEADER.  */
    if (packed_audio_descriptor[2] == UX_CLASS_AUDIO20_AS_GENERAL)
    {

        /* Save AS_HEADER for future use.  */
        parser -> as_header = packed_audio_descriptor;
        return(0);
    }

    /* Check bDescriptorSubType@2, bFormatType@3 to confirm FORMAT_TYPE_I.  */
    if (packed_audio_descriptor[2] != UX_CLASS_AUDIO20_AS_FORMAT_TYPE)
        return(0);

    if (descriptor_length < 6)
    {
        /* Error trap. */
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);

        /* If trace is enabled, insert this event into the trace buffer.  */
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor_length, 0, 0, UX_TRACE_ERRORS, 0, 0)

        parser -> status = UX_DESCRIPTOR_CORRUPTED;
        return(1);
    }

    if (packed_audio_descriptor[3] != UX_CLASS_AUDIO20_FORMAT_TYPE_I)
        return(0);

    /* Save FORMAT_TYPE_I::bBitResolution@5.  */
    sam_attr.ux_host_class_audio_sampling_characteristics_resolution = packed_audio_descriptor[5];

    /* If AS_HEADER is not ready, fail.  */
    if (parser -> as_header == UX_NULL)
    {
        parser -> status = UX_DESCRIPTOR_CORRUPTED;
        return(1);
    }

    /* Check AS_HEADER::bTerminalLink@3 to find frequencies.  */
    status = _ux_host_class_audio20_clock_get(audio, parser -> as_header[3],
                                                &csd, &clk_mul, &clk_div);
    if (status != UX_SUCCESS)
    {
        parser -> status = status;
        return(1);
    }

    /* Get CSD::bClockID@3.  */
    clk_id = csd[3];

    /* Issue GET_RANGE to get sampling frequency.  */

    /* Get device, endpoint, transfer.  */
    device = audio -> ux_host_class_audio_device;
    endpoint = &device -> ux_device_control_endpoint;
    transfer = &endpoint -> ux_endpoint_transfer_request;

    /* Allocate buffer for GET_RANGE.  */
    buffer = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_CACHE_SAFE_MEMORY, 2);
    if (buffer == UX_NULL)
    {
        parser -> status = UX_MEMORY_INSUFFICIENT;
        return(1);
    }

    /* Protect control transfer.  */
    parser -> status = _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
    if (parser -> status != UX_SUCCESS)
    {
        _ux_utility_memory_free(buffer);
        return(1);
    }

    /* Issue GET_RANGE request.  */
    transfer -> ux_transfer_request_data_pointer =     buffer;
    transfer -> ux_transfer_request_requested_length = 2;
    transfer -> ux_transfer_request_function =         UX_CLASS_AUDIO20_RANGE;
    transfer -> ux_transfer_request_type =             UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
    transfer -> ux_transfer_request_value =            0 | (UX_CLASS_AUDIO20_CS_SAM_FREQ_CONTROL << 8);
    transfer -> ux_transfer_request_index =            audio -> ux_host_class_audio_control_interface_number | (clk_id << 8);
    parser -> status = _ux_host_stack_transfer_request(transfer);

    /* Get wNumSubRanges.  */
    n_sub = (ULONG)(*(USHORT *)buffer);
    _ux_utility_memory_free(buffer);

    /* Check errors.  */
    if (parser -> status != UX_SUCCESS)
        return(1);
    if (n_sub == 0)
    {
        parser -> status = UX_TRANSFER_ERROR;
        return(1);
    }

    /* Calculate parameter length, with overflow check.  */
    param_len = n_sub * (4 * 3);
    if (param_len > 0xFFFF)
    {
        parser -> status = UX_MATH_OVERFLOW;
        return(1);
    }
    param_len += 2;
    if (param_len > 0xFFFF)
    {
        parser -> status = UX_MATH_OVERFLOW;
        return(1);
    }

    /* Allocate buffer for GET_RANGE.  */
    buffer = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_CACHE_SAFE_MEMORY, param_len);
    if (buffer == UX_NULL)
    {
        parser -> status = UX_MEMORY_INSUFFICIENT;
        return(1);
    }

    /* Protect control transfer.  */
    parser -> status = _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
    if (parser -> status != UX_SUCCESS)
    {
        _ux_utility_memory_free(buffer);
        return(1);
    }

    /* Issue GET_RANGE request.  */
    transfer -> ux_transfer_request_data_pointer =     buffer;
    transfer -> ux_transfer_request_requested_length = param_len;
    transfer -> ux_transfer_request_function =         UX_CLASS_AUDIO20_RANGE;
    transfer -> ux_transfer_request_type =             UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
    transfer -> ux_transfer_request_value =            0 | (UX_CLASS_AUDIO20_CS_SAM_FREQ_CONTROL << 8);
    transfer -> ux_transfer_request_index =            audio -> ux_host_class_audio_control_interface_number | (clk_id << 8);
    parser -> status = _ux_host_stack_transfer_request(transfer);
    if (parser -> status != UX_SUCCESS)
    {
        _ux_utility_memory_free(buffer);
        return(1);
    }

    /* Save AS_HEADER::bNrChannels@10.  */
    sam_attr.ux_host_class_audio_sampling_characteristics_channels = parser -> as_header[10];

    /* Save CSD.  */
    sam_attr.ux_host_class_audio_sampling_characteristics_descriptor = csd;

    /* Parse ranges.  */
    for (offset = 2; offset < param_len; offset += (4*3))
    {

        /* Save dMIN, dMAX.  */
        sam_attr.ux_host_class_audio_sampling_characteristics_frequency_low =
                                    _ux_utility_long_get(buffer + offset);
        sam_attr.ux_host_class_audio_sampling_characteristics_frequency_high =
                                    _ux_utility_long_get(buffer + offset + 4);

        /* Save MUL, DIV.  */
        sam_attr.ux_host_class_audio_sampling_characteristics_clock_mul = clk_mul;
        sam_attr.ux_host_class_audio_sampling_characteristics_clock_div = clk_div;

        /* Parse frequency.  */
        status = parser -> parse_function(parser -> arg, packed_interface_descriptor, &sam_attr);
        if (status)
        {
            _ux_utility_memory_free(buffer);
            return(status);
        }
    }
    _ux_utility_memory_free(buffer);

    /* Continue parsing.  */
    return(0);
}
static inline UINT _ux_host_class_audio20_sampling_parse(UX_HOST_CLASS_AUDIO *audio,
        UINT(*parse_function)(VOID  *arg,
                              UCHAR *packed_interface_descriptor,
                              UX_HOST_CLASS_AUDIO_SAMPLING_CHARACTERISTICS *sam_attr),
        VOID* arg)
{
struct UX_HOST_CLASS_AUDIO20_SAM_PARSER parser;
UINT                                    status;

    /* Run parse process.  */
    parser.parse_function = parse_function;
    parser.arg = arg;
    parser.audio = audio;
    parser.as_header = UX_NULL;
    parser.status = UX_SUCCESS;
    status = _ux_host_class_audio_descriptors_parse(audio,
                    _ux_host_class_audio20_sam_parse_func, (VOID *)&parser);

    /* Return parser status.  */
    if (status == UX_SUCCESS)
        return(parser.status);

    /* Return parsing error status.  */
    return(status);
}
#endif /* defined(UX_HOST_CLASS_AUDIO_2_SUPPORT) */


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _uxe_host_class_audio_raw_sampling_parse            PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks errors in audio RAW sampling parse function    */
/*    call.                                                               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    audio                                 Pointer to audio control (AC) */
/*                                          instance                      */
/*    callback_function                     Callback function invoked on  */
/*                                          interrupt message reception   */
/*    arg                                   Callback argument passed to   */
/*                                          callback function             */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Status                                                              */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_class_audio_raw_sampling_parse                             */
/*                                          Parse RAW sampling properties */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*                                                                        */
/**************************************************************************/
UINT _uxe_host_class_audio_raw_sampling_parse(UX_HOST_CLASS_AUDIO *audio,
        UINT(*parse_function)(VOID  *arg,
                              UCHAR *packed_interface_descriptor,
                              UX_HOST_CLASS_AUDIO_SAMPLING_CHARACTERISTICS *sam_attr),
        VOID* arg)
{

    /* Sanity checks.  */
    if ((audio == UX_NULL) || (parse_function == UX_NULL))
        return(UX_INVALID_PARAMETER);

    /* Invoke sampling characteristics parse function.  */
    return(_ux_host_class_audio_raw_sampling_parse(audio, parse_function, arg));
}