1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/usb.h>
12 #include <linux/usb/audio-v2.h>
13 #include <linux/usb/audio-v3.h>
14 #include <linux/usb/audio.h>
15 
16 #include "audio_driver_log.h"
17 #include "audio_usb_linux.h"
18 #include "audio_usb_validate_desc.h"
19 #include "hdf_base.h"
20 
21 #define HDF_LOG_TAG HDF_AUDIO_USB
22 
23 /* Formats - A.2 Format Type Codes */
24 #define UAC_FORMAT_TYPE_UNDEFINED 0x0
25 #define UAC_FORMAT_TYPE_I         0x1
26 #define UAC_FORMAT_TYPE_II        0x2
27 #define UAC_FORMAT_TYPE_III       0x3
28 
29 #define FRAME_SIZE_2        2
30 #define FORMAT_LRENGTH_6    6
31 #define MOVE_8_BIT          8
32 #define FORMAT_LRENGTH_8    8
33 #define MOVE_11_BIT         11
34 #define MOVE_16_BIT         16
35 #define SAMPLE_WIDTH        24
36 #define READ_MAX_ERR_COUNT  2
37 #define READ_RATE_ERR_COUNT 3
38 
39 #define MOVE_VAL     (1u)
40 #define INVALID_RATE 0
41 
42 #define AUDIO_USB_PCM_FMTBIT_S8       (1 << 0)
43 #define AUDIO_USB_PCM_FMTBIT_U8       (1 << 1)
44 #define AUDIO_USB_PCM_FMTBIT_S16_LE   (1 << 2)
45 #define AUDIO_USB_PCM_FMTBIT_S16_BE   (1 << 3)
46 #define AUDIO_USB_PCM_FMTBIT_S32_LE   (1 << 10)
47 #define AUDIO_USB_PCM_FMTBIT_FLOAT_LE (1 << 14)
48 #define AUDIO_USB_PCM_FMTBIT_MU_LAW   (1 << 20)
49 #define AUDIO_USB_PCM_FMTBIT_A_LAW    (1 << 21)
50 #define AUDIO_USB_PCM_FMTBIT_SPECIAL  (1ULL << 31)
51 #define AUDIO_USB_PCM_FMTBIT_S24_3LE  (1ULL << 32)
52 #define AUDIO_USB_PCM_FMTBIT_S24_3BE  (1ULL << 33)
53 
54 #define AUDIO_USB_PCM_RATE_KNOT (1u << 31) /* supports more non-continuos rates */
55 
56 #define AUDIO_USB_PCM_RATE_5512   (1 << 0)  /* 5512Hz */
57 #define AUDIO_USB_PCM_RATE_8000   (1 << 1)  /* 8000Hz */
58 #define AUDIO_USB_PCM_RATE_16000  (1 << 3)  /* 16000Hz */
59 #define AUDIO_USB_PCM_RATE_32000  (1 << 5)  /* 32000Hz */
60 #define AUDIO_USB_PCM_RATE_44100  (1 << 6)  /* 44100Hz */
61 #define AUDIO_USB_PCM_RATE_48000  (1 << 7)  /* 48000Hz */
62 #define AUDIO_USB_PCM_RATE_96000  (1 << 10) /* 96000Hz */
63 #define AUDIO_USB_PCM_RATE_192000 (1 << 12) /* 192000Hz */
64 
65 #define AUDIO_USB_FORMAT_NUM_3 3
66 #define AUDIO_USB_FORMAT_NUM_4 4
67 
68 #define FORMAT_QUIRK_ENABLE    1
69 #define FORMAT_QUIRK_DISENABLE 0
70 
71 #define FORMAT_ALTSETTING_1 1
72 #define FORMAT_ALTSETTING_2 2
73 #define FORMAT_ALTSETTING_3 3
74 #define FORMAT_ALTSETTING_4 4
75 #define FORMAT_ALTSETTING_5 5
76 #define FORMAT_ALTSETTING_6 6
77 #define FORMAT_ALTSETTING_7 7
78 #define FORMAT_MAX_SIZE     64
79 
80 #define ENDPOINT_NUM_2 2
81 
82 #define DESP_LENTH         7
83 #define SAMPLE_BYTE_1      1
84 #define SAMPLE_BYTE_2      2
85 #define SAMPLE_BYTE_3      3
86 #define SAMPLE_BYTE_4      4
87 #define SAMPLE_RATE_2      2
88 #define SAMPLE_RATE_3      3
89 #define SAMPLE_RATE_OFFSET 3
90 
91 #define PACK_SIZE   3
92 #define RATE_OFFSET 7
93 
94 #define ARRAY_INDEX_0 0
95 #define ARRAY_INDEX_1 1
96 #define ARRAY_INDEX_2 2
97 
98 #define MAX_PACKET_SIZE 392 /* USB sound card device descriptor default maxPackSize */
99 
100 enum AudioUsbPcmStreamTpye {
101     AUDIO_USB_PCM_STREAM_PLAYBACK = 0,
102     AUDIO_USB_PCM_STREAM_CAPTURE,
103     AUDIO_USB_PCM_STREAM_LAST = AUDIO_USB_PCM_STREAM_CAPTURE,
104 };
105 
106 struct AudioFmtInfo {
107     int32_t sampleWidth;
108     int32_t sampleBytes;
109     uint64_t pcmFormats;
110 };
111 
112 struct AudioUsbUacFormat {
113     int32_t altno;
114     int32_t bmQuirk;
115     int32_t clock;
116     int32_t ifaceNo;
117     int32_t num;
118     int32_t protocol;
119     int32_t stream;
120     uint32_t channelsNum;
121     uint32_t chconfig;
122     uint64_t format;
123     struct usb_interface_descriptor *altsd;
124 };
125 
126 /* find an input terminal descriptor (either UAC1 or UAC2) with the given
127  * terminal id
128  */
AudioUsbFindInputDesc(struct usb_host_interface * ctlIface,int32_t terminalId,int32_t protocol)129 static void *AudioUsbFindInputDesc(struct usb_host_interface *ctlIface, int32_t terminalId, int32_t protocol)
130 {
131     struct uac2_input_terminal_descriptor *term = NULL;
132     if (ctlIface == NULL) {
133         AUDIO_DRIVER_LOG_ERR("ctlIface is NULL.");
134         return NULL;
135     }
136 
137     term = AudioUsbFindCsintDesc(ctlIface->extra, ctlIface->extralen, term, UAC_INPUT_TERMINAL);
138     while (term != NULL) {
139         if (!AudioUsbValidateAudioDesc(term, protocol)) {
140             term = AudioUsbFindCsintDesc(ctlIface->extra, ctlIface->extralen, term, UAC_INPUT_TERMINAL);
141             continue;
142         }
143         if (term->bTerminalID == terminalId) {
144             return term;
145         }
146         term = AudioUsbFindCsintDesc(ctlIface->extra, ctlIface->extralen, term, UAC_INPUT_TERMINAL);
147     }
148     return NULL;
149 }
150 
ParseUacEndpointAttr(struct usb_host_interface * alts,int32_t protocol,int32_t * attributes)151 static int32_t ParseUacEndpointAttr(struct usb_host_interface *alts, int32_t protocol, int32_t *attributes)
152 {
153     struct uac_iso_endpoint_descriptor *csep = NULL;
154     struct usb_interface_descriptor *altsd = AudioUsbGetIfaceDesc(alts);
155 
156     csep = AudioUsbFindDesc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
157     /* Creamware Noah has this descriptor after the 2nd endpoint */
158     if (csep == NULL && altsd->bNumEndpoints >= ENDPOINT_NUM_2) {
159         csep = AudioUsbFindDesc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
160     }
161 
162     if (csep == NULL) {
163         csep = AudioUsbFindDesc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT);
164     }
165     if (csep == NULL || csep->bLength < DESP_LENTH || csep->bDescriptorSubtype != UAC_EP_GENERAL) {
166         AUDIO_DRIVER_LOG_ERR("uac iso endpoint descriptor is invalid.");
167         return HDF_FAILURE;
168     }
169 
170     if (protocol == UAC_VERSION_1) {
171         *attributes = csep->bmAttributes;
172     } else if (protocol == UAC_VERSION_2) {
173         struct uac2_iso_endpoint_descriptor *csep2 = (struct uac2_iso_endpoint_descriptor *)csep;
174         if (csep2->bLength < sizeof(*csep2)) {
175             AUDIO_DRIVER_LOG_ERR("uac2 iso endpoint descriptor length is invalid.");
176             return HDF_FAILURE;
177         }
178         *attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
179         /* emulate the endpoint attributes of a v1 device */
180         if (csep2->bmControls & UAC2_CONTROL_PITCH) {
181             *attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
182         }
183     } else { /* UAC_VERSION_3 */
184         struct uac3_iso_endpoint_descriptor *csep3 = (struct uac3_iso_endpoint_descriptor *)csep;
185         if (csep3->bLength < sizeof(*csep3)) {
186             AUDIO_DRIVER_LOG_ERR("uac3 iso endpoint descriptor length is invalid.");
187             return HDF_FAILURE;
188         }
189         /* emulate the endpoint attributes of a v1 device */
190         if (le32_to_cpu(csep3->bmControls) & UAC2_CONTROL_PITCH) {
191             *attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
192         }
193     }
194 
195     return HDF_SUCCESS;
196 }
197 
198 /* check if the device uses big-endian samples */
AudioUsbCheckFormat(struct AudioUsbDriver * audioUsbDriver,struct AudioUsbFormat * audioUsbFormat)199 static bool AudioUsbCheckFormat(struct AudioUsbDriver *audioUsbDriver, struct AudioUsbFormat *audioUsbFormat)
200 {
201     /* it depends on altsetting whether the device is big-endian or not */
202     switch (audioUsbDriver->usbId) {
203         case 0x7632001: /* 0x0763 0x2001 M-Audio Quattro: captured data only */
204             if (audioUsbFormat->altsetting == FORMAT_ALTSETTING_2 ||
205                 audioUsbFormat->altsetting == FORMAT_ALTSETTING_3 ||
206                 audioUsbFormat->altsetting == FORMAT_ALTSETTING_5 ||
207                 audioUsbFormat->altsetting == FORMAT_ALTSETTING_6) {
208                 return true;
209             }
210             break;
211         case 0x7632003: /* 0x0763 0x2003 M-Audio Audiophile USB */
212             if (audioUsbDriver->setup == 0x00 || audioUsbFormat->altsetting == 1 ||
213                 audioUsbFormat->altsetting == FORMAT_ALTSETTING_2 ||
214                 audioUsbFormat->altsetting == FORMAT_ALTSETTING_3) {
215                 return true;
216             }
217             break;
218         case 0x7632012: /* 0x0763 0x2012 M-Audio Fast Track Pro */
219             if (audioUsbFormat->altsetting == FORMAT_ALTSETTING_2 ||
220                 audioUsbFormat->altsetting == FORMAT_ALTSETTING_3 ||
221                 audioUsbFormat->altsetting == FORMAT_ALTSETTING_5 ||
222                 audioUsbFormat->altsetting == FORMAT_ALTSETTING_6) {
223                 return true;
224             }
225             break;
226         default:
227             AUDIO_DRIVER_LOG_WARNING("usb device not support.");
228             return false;
229     }
230     return false;
231 }
232 
AudioUsbFormatTypeInit(struct AudioUsbFormat * audioUsbFormat,struct AudioFmtInfo * audioFmtInfo,void * pFmt,uint64_t * format)233 static int32_t AudioUsbFormatTypeInit(
234     struct AudioUsbFormat *audioUsbFormat, struct AudioFmtInfo *audioFmtInfo, void *pFmt, uint64_t *format)
235 {
236     switch (audioUsbFormat->protocol) {
237         case UAC_VERSION_1: {
238             struct uac_format_type_i_discrete_descriptor *fmt = pFmt;
239             if (*format >= FORMAT_MAX_SIZE) {
240                 return HDF_ERR_INVALID_PARAM; /* invalid format */
241             }
242             audioFmtInfo->sampleWidth = fmt->bBitResolution;
243             audioFmtInfo->sampleBytes = fmt->bSubframeSize;
244             *format = 1ULL << *format;
245             break;
246         }
247         case UAC_VERSION_2: {
248             struct uac_format_type_i_ext_descriptor *fmt = pFmt;
249             audioFmtInfo->sampleWidth = fmt->bBitResolution;
250             audioFmtInfo->sampleBytes = fmt->bSubslotSize;
251             if (*format & UAC2_FORMAT_TYPE_I_RAW_DATA) {
252                 audioFmtInfo->pcmFormats |= AUDIO_USB_PCM_FMTBIT_SPECIAL;
253                 /* flag potentially raw DSD capable altsettings */
254                 audioUsbFormat->dsdRaw = true;
255             }
256             *format <<= 1;
257             break;
258         }
259         case UAC_VERSION_3: {
260             struct uac3_as_header_descriptor *as = pFmt;
261             audioFmtInfo->sampleWidth = as->bBitResolution;
262             audioFmtInfo->sampleBytes = as->bSubslotSize;
263             if (*format & UAC3_FORMAT_TYPE_I_RAW_DATA) {
264                 audioFmtInfo->pcmFormats |= AUDIO_USB_PCM_FMTBIT_SPECIAL;
265             }
266             *format <<= 1;
267             break;
268         }
269         default: {
270             struct uac_format_type_i_discrete_descriptor *fmt = pFmt;
271             if (*format >= FORMAT_MAX_SIZE) {
272                 return HDF_ERR_INVALID_PARAM; /* invalid format */
273             }
274             audioFmtInfo->sampleWidth = fmt->bBitResolution;
275             audioFmtInfo->sampleBytes = fmt->bSubframeSize;
276             *format = 1ULL << *format;
277             break;
278         }
279     }
280 
281     return HDF_SUCCESS;
282 }
283 
AudioUsbPcmFormatSub(struct AudioUsbDriver * audioUsbDriver,struct AudioUsbFormat * audioUsbFmt,struct AudioFmtInfo * audioFmtInfo)284 static void AudioUsbPcmFormatSub(
285     struct AudioUsbDriver *audioUsbDriver, struct AudioUsbFormat *audioUsbFmt, struct AudioFmtInfo *audioFmtInfo)
286 {
287     if (((audioUsbDriver->usbId == AudioUsbGetUsbId(0x0582, 0x0016)) ||     /* Edirol SD-90 */
288             (audioUsbDriver->usbId == AudioUsbGetUsbId(0x0582, 0x000c))) && /* Roland SC-D70 */
289         audioFmtInfo->sampleWidth == SAMPLE_WIDTH &&
290         audioFmtInfo->sampleBytes == SAMPLE_BYTE_2) {
291         audioFmtInfo->sampleBytes = SAMPLE_BYTE_3;
292     } else if (audioFmtInfo->sampleWidth > (audioFmtInfo->sampleBytes) * 8) { /* 8 for Byte to bits */
293         AUDIO_DRIVER_LOG_INFO("%u:%d : sample bitwidth %d in over sample bytes %d.", audioUsbFmt->iface,
294             audioUsbFmt->altsetting, audioFmtInfo->sampleWidth, audioFmtInfo->sampleBytes);
295     }
296     /* check the format byte size */
297     switch (audioFmtInfo->sampleBytes) {
298         case SAMPLE_BYTE_1:
299             audioFmtInfo->pcmFormats |= AUDIO_USB_PCM_FMTBIT_S8;
300             break;
301         case SAMPLE_BYTE_2:
302             if (AudioUsbCheckFormat(audioUsbDriver, audioUsbFmt)) {
303                 audioFmtInfo->pcmFormats |= AUDIO_USB_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */
304             } else {
305                 audioFmtInfo->pcmFormats |= AUDIO_USB_PCM_FMTBIT_S16_LE;
306             }
307             break;
308         case SAMPLE_BYTE_3:
309             if (AudioUsbCheckFormat(audioUsbDriver, audioUsbFmt)) {
310                 audioFmtInfo->pcmFormats |= AUDIO_USB_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */
311             } else {
312                 audioFmtInfo->pcmFormats |= AUDIO_USB_PCM_FMTBIT_S24_3LE;
313             }
314             break;
315         case SAMPLE_BYTE_4:
316             audioFmtInfo->pcmFormats |= AUDIO_USB_PCM_FMTBIT_S32_LE;
317             break;
318         default:
319             AUDIO_DRIVER_LOG_INFO("%u:%d : unsupported sample bitwidth %d in %d bytes.", audioUsbFmt->iface,
320                 audioUsbFmt->altsetting, audioFmtInfo->sampleWidth, audioFmtInfo->sampleBytes);
321             break;
322     }
323 }
324 
ParseAudioFormatType(struct AudioUsbDriver * audioUsbDriver,struct AudioUsbFormat * audioUsbFormat,uint64_t format,void * pFmt)325 static uint64_t ParseAudioFormatType(
326     struct AudioUsbDriver *audioUsbDriver, struct AudioUsbFormat *audioUsbFormat, uint64_t format, void *pFmt)
327 {
328     uint64_t pcmFormats = 0;
329 
330     struct AudioFmtInfo audioFmtInfo;
331     (void)memset_s(&audioFmtInfo, sizeof(struct AudioFmtInfo), 0, sizeof(struct AudioFmtInfo));
332     int32_t ret = AudioUsbFormatTypeInit(audioUsbFormat, &audioFmtInfo, pFmt, &format);
333     if (ret == HDF_ERR_INVALID_PARAM) {
334         return 0; /* invalid format, return 0 */
335     }
336     audioUsbFormat->fmtBits = audioFmtInfo.sampleWidth;
337     pcmFormats = audioFmtInfo.pcmFormats;
338     if ((pcmFormats == 0) && (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED))) {
339         AUDIO_DRIVER_LOG_INFO(
340             "%u:%d : format type 0 is detected, processed as PCM.", audioUsbFormat->iface, audioUsbFormat->altsetting);
341         format = 1 << UAC_FORMAT_TYPE_I_PCM;
342     }
343     if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) { /* format type is pcm */
344         AudioUsbPcmFormatSub(audioUsbDriver, audioUsbFormat, &audioFmtInfo);
345         pcmFormats = audioFmtInfo.pcmFormats;
346     }
347     if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) {                        /* format type is pcm8 */
348         if (audioUsbDriver->usbId == AudioUsbGetUsbId(0x04fa, 0x4201)) { /* Dallas DS4201 workaround */
349             pcmFormats = pcmFormats | AUDIO_USB_PCM_FMTBIT_S8;
350         } else {
351             pcmFormats = pcmFormats | AUDIO_USB_PCM_FMTBIT_U8;
352         }
353     }
354     if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) { /* format type is alaw */
355         pcmFormats = pcmFormats | AUDIO_USB_PCM_FMTBIT_A_LAW;
356     }
357 
358     if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) { /* format type is ieee float */
359         pcmFormats = pcmFormats | AUDIO_USB_PCM_FMTBIT_FLOAT_LE;
360     }
361 
362     if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) { /* format type is mulaw */
363         pcmFormats = pcmFormats | AUDIO_USB_PCM_FMTBIT_MU_LAW;
364     }
365 
366     return pcmFormats;
367 }
368 
AudioPcmRateToRateBit(uint32_t rate)369 static uint32_t AudioPcmRateToRateBit(uint32_t rate)
370 {
371     uint32_t i;
372     uint32_t count;
373     uint32_t rateMask;
374     static const uint32_t rates[] = {5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000, 88200, 96000, 176400,
375         192000, 352800, 384000}; /* Pcm sampling rate */
376 
377     count = sizeof(rates) / sizeof(rates[0]);
378     for (i = 0; i < count; i++) {
379         if (rates[i] == rate) {
380             rateMask = MOVE_VAL << i;
381             return rateMask;
382         }
383     }
384     return AUDIO_USB_PCM_RATE_KNOT;
385 }
386 
AudioUsbInitRate(struct AudioUsbDriver * audioUsbDriver,struct AudioUsbFormat * audioUsbFormat,uint32_t * rate,int32_t nrRates)387 static void AudioUsbInitRate(
388     struct AudioUsbDriver *audioUsbDriver, struct AudioUsbFormat *audioUsbFormat, uint32_t *rate, int32_t nrRates)
389 {
390     if (*rate == AUDIO_SAMPLE_RATE_48000 && nrRates == 1 &&
391         (audioUsbDriver->usbId == AudioUsbGetUsbId(0x0d8c, 0x0201) ||     /* CM6501 */
392             audioUsbDriver->usbId == AudioUsbGetUsbId(0x0d8c, 0x0102) ||  /* CM6501 */
393             audioUsbDriver->usbId == AudioUsbGetUsbId(0x0d8c, 0x0078) ||  /* CM6206 */
394             audioUsbDriver->usbId == AudioUsbGetUsbId(0x0ccd, 0x00b1)) && /* Ozone Z90 */
395         audioUsbFormat->altsetting == FORMAT_ALTSETTING_5 &&
396         audioUsbFormat->maxPackSize == MAX_PACKET_SIZE) {
397         *rate = AUDIO_SAMPLE_RATE_96000;
398     }
399     /* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
400     if (*rate == AUDIO_SAMPLE_RATE_16000 &&
401         (audioUsbDriver->usbId == AudioUsbGetUsbId(0x041e, 0x4064) ||     /* VF0420 */
402             audioUsbDriver->usbId == AudioUsbGetUsbId(0x041e, 0x4068))) { /* VF0470 */
403         *rate = AUDIO_SAMPLE_RATE_8000;
404     }
405 }
406 
AudioUsbParseFormatRates(struct AudioUsbDriver * audioUsbDriver,struct AudioUsbFormat * audioUsbFormat,uint8_t * fmt,int32_t offset)407 static int32_t AudioUsbParseFormatRates(
408     struct AudioUsbDriver *audioUsbDriver, struct AudioUsbFormat *audioUsbFormat, uint8_t *fmt, int32_t offset)
409 {
410     int32_t nrRates = fmt[offset];
411     int32_t r, idx;
412     int32_t temp;
413 
414     if (nrRates == 0) {
415         nrRates = SAMPLE_RATE_2;
416     }
417     temp = offset + 1 + SAMPLE_RATE_3 * nrRates;
418     if (fmt[0] < temp) {
419         AUDIO_DRIVER_LOG_ERR("%u:%d : invalid FORMAT_TYPE desc", audioUsbFormat->iface, audioUsbFormat->altsetting);
420         return HDF_ERR_INVALID_PARAM;
421     }
422 
423     if (nrRates == INVALID_RATE) {
424         return HDF_SUCCESS;
425     }
426 
427     /*
428      * build the rate table and bitmap flags
429      */
430     audioUsbFormat->rateTable = OsalMemCalloc(nrRates * sizeof(int32_t));
431     if (audioUsbFormat->rateTable == NULL) {
432         return HDF_ERR_INVALID_OBJECT;
433     }
434     audioUsbFormat->nrRates = 0;
435     audioUsbFormat->rateMin = audioUsbFormat->rateMax = 0;
436     for (r = 0, idx = offset + 1; r < nrRates; r++, idx += SAMPLE_RATE_OFFSET) {
437         uint32_t rate = AudioUsbCombineTriple(&fmt[idx]);
438         if (rate == 0) {
439             continue;
440         }
441         /* C-Media CM6501 mislabels its 96 kHz altsetting */
442         /* Terratec Aureon 7.1 USB C-Media 6206, too */
443         /* Ozone Z90 USB C-Media, too */
444         AudioUsbInitRate(audioUsbDriver, audioUsbFormat, &rate, nrRates);
445 
446         audioUsbFormat->rateTable[audioUsbFormat->nrRates] = rate;
447         if (audioUsbFormat->rateMin == 0 || rate < audioUsbFormat->rateMin) {
448             audioUsbFormat->rateMin = rate;
449         }
450         if (audioUsbFormat->rateMax == 0 || rate > audioUsbFormat->rateMax) {
451             audioUsbFormat->rateMax = rate;
452         }
453         audioUsbFormat->rates |= AudioPcmRateToRateBit(rate);
454         audioUsbFormat->nrRates++;
455     }
456     if (audioUsbFormat->nrRates == 0) {
457         return HDF_ERR_INVALID_PARAM;
458     }
459 
460     return HDF_SUCCESS;
461 }
462 
463 /* parse the format type I and III descriptors */
AudioUsbParseFormatSub(struct AudioUsbDriver * audioUsbDriver,struct AudioUsbFormat * audioUsbFormat,uint64_t format,void * pFmt)464 static int32_t AudioUsbParseFormatSub(
465     struct AudioUsbDriver *audioUsbDriver, struct AudioUsbFormat *audioUsbFormat, uint64_t format, void *pFmt)
466 {
467     uint32_t fmtType;
468     int32_t ret;
469     struct uac_format_type_i_continuous_descriptor *fmt = NULL;
470 
471     switch (audioUsbFormat->protocol) {
472         case UAC_VERSION_1:
473         case UAC_VERSION_2: {
474             fmt = pFmt;
475             fmtType = fmt->bFormatType;
476             break;
477         }
478         case UAC_VERSION_3: {
479             /* fp->fmtType is already set in this case */
480             fmtType = audioUsbFormat->fmtType;
481             break;
482         }
483         default: {
484             fmt = pFmt;
485             fmtType = fmt->bFormatType;
486             break;
487         }
488     }
489 
490     if (fmtType != UAC_FORMAT_TYPE_III) {
491         audioUsbFormat->formats = ParseAudioFormatType(audioUsbDriver, audioUsbFormat, format, pFmt);
492         if (audioUsbFormat->formats == 0) {
493             return HDF_ERR_INVALID_PARAM;
494         }
495     }
496 
497     if (audioUsbFormat->protocol == UAC_VERSION_1) {
498         fmt = pFmt;
499         audioUsbFormat->channels = fmt->bNrChannels;
500         ret = AudioUsbParseFormatRates(audioUsbDriver, audioUsbFormat, (uint8_t *)fmt, RATE_OFFSET);
501     } else {
502         return HDF_ERR_NOT_SUPPORT;
503     }
504 
505     if (audioUsbFormat->channels == 0) {
506         AUDIO_DRIVER_LOG_ERR("%u:%d : invalid channels %d.", audioUsbFormat->iface, audioUsbFormat->altsetting,
507             audioUsbFormat->channels);
508         return HDF_ERR_INVALID_PARAM;
509     }
510 
511     return ret;
512 }
513 
AudioUsbParseFormat(struct AudioUsbDriver * audioUsbDriver,struct AudioUsbFormat * audioUsbFormat,uint64_t format,struct uac_format_type_i_continuous_descriptor * fmt,int32_t stream)514 static int32_t AudioUsbParseFormat(struct AudioUsbDriver *audioUsbDriver, struct AudioUsbFormat *audioUsbFormat,
515     uint64_t format, struct uac_format_type_i_continuous_descriptor *fmt, int32_t stream)
516 {
517     int32_t ret = 0;
518 
519     switch (fmt->bFormatType) {
520         case UAC_FORMAT_TYPE_I:
521         case UAC_FORMAT_TYPE_III:
522             ret = AudioUsbParseFormatSub(audioUsbDriver, audioUsbFormat, format, fmt);
523             break;
524         case UAC_FORMAT_TYPE_II:
525             break;
526         default:
527             AUDIO_DRIVER_LOG_ERR("%u: %d : format type %d is not supported yet.", audioUsbFormat->iface,
528                 audioUsbFormat->altsetting, fmt->bFormatType);
529             return HDF_ERR_NOT_SUPPORT;
530     }
531     audioUsbFormat->fmtType = fmt->bFormatType;
532     if (ret < HDF_SUCCESS) {
533         return ret;
534     }
535 
536     if (audioUsbDriver->usbId == AudioUsbGetUsbId(0x041e, 0x3000) || /* Extigy */
537         audioUsbDriver->usbId == AudioUsbGetUsbId(0x041e, 0x3020) || /* Audigy 2 NX */
538         audioUsbDriver->usbId == AudioUsbGetUsbId(0x041e, 0x3061)) { /* Audigy */
539         if (fmt->bFormatType == UAC_FORMAT_TYPE_I && audioUsbFormat->rates != AUDIO_USB_PCM_RATE_48000 &&
540             audioUsbFormat->rates != AUDIO_USB_PCM_RATE_96000) {
541             AUDIO_DRIVER_LOG_ERR("audio usb format is not support");
542             return HDF_ERR_NOT_SUPPORT;
543         }
544     }
545 
546     return HDF_SUCCESS;
547 }
548 
AudioUsbFreeFormat(struct AudioUsbFormat * fp)549 static void AudioUsbFreeFormat(struct AudioUsbFormat *fp)
550 {
551     if (fp != NULL) {
552         DListRemove(&fp->list);
553         OsalMemFree(fp->rateTable);
554         OsalMemFree(fp);
555     }
556 }
557 
AudioUsbFormatInit(struct AudioUsbDriver * audioUsbDriver,struct AudioUsbUacFormat * uacFmt,struct usb_host_interface * alts,int32_t altsetIdx)558 static struct AudioUsbFormat *AudioUsbFormatInit(struct AudioUsbDriver *audioUsbDriver,
559     struct AudioUsbUacFormat *uacFmt, struct usb_host_interface *alts, int32_t altsetIdx)
560 {
561     struct AudioUsbFormat *fp = NULL;
562     struct usb_device *dev = NULL;
563     struct usb_endpoint_descriptor *epDesc = NULL;
564 
565     fp = OsalMemCalloc(sizeof(struct AudioUsbFormat));
566     if (fp == NULL) {
567         AUDIO_DRIVER_LOG_ERR("OsalMemCalloc failed.");
568         return NULL;
569     }
570     dev = interface_to_usbdev(audioUsbDriver->usbIf);
571     if (dev == NULL) {
572         AUDIO_DRIVER_LOG_ERR("interface_to_usbdev failed.");
573         AudioUsbFreeFormat(fp);
574         return NULL;
575     }
576     epDesc = AudioEndpointDescriptor(alts, 0);
577     if (epDesc == NULL) {
578         AUDIO_DRIVER_LOG_ERR("epDesc is NULL.");
579         AudioUsbFreeFormat(fp);
580         return NULL;
581     }
582     fp->iface = uacFmt->ifaceNo;
583     fp->altsetting = uacFmt->altno;
584     fp->altsetIdx = altsetIdx;
585     fp->endpoint = epDesc->bEndpointAddress;
586     fp->epAttr = epDesc->bmAttributes;
587     fp->dataInterval = AudioUsbParseDataInterval(dev, alts);
588     fp->protocol = uacFmt->protocol;
589     fp->maxPackSize = le16_to_cpu(epDesc->wMaxPacketSize);
590 
591     fp->channels = uacFmt->channelsNum;
592 
593     if (AudioUsbGetSpeed(dev) == USB_SPEED_HIGH) {
594         fp->maxPackSize = (((fp->maxPackSize >> MOVE_11_BIT) & PACK_SIZE) + 1) * (fp->maxPackSize & 0x7ff);
595     }
596 
597     fp->clock = uacFmt->clock;
598     DListHeadInit(&fp->list);
599 
600     return fp;
601 }
602 
AudioUsbGetFormat(struct AudioUsbDriver * audioUsbDriver,struct usb_host_interface * alts,struct AudioUsbUacFormat * uacFmt,struct uac_format_type_i_continuous_descriptor ** fmt)603 static int32_t AudioUsbGetFormat(struct AudioUsbDriver *audioUsbDriver, struct usb_host_interface *alts,
604     struct AudioUsbUacFormat *uacFmt, struct uac_format_type_i_continuous_descriptor **fmt)
605 {
606     /* get audio formats */
607     if (uacFmt->protocol == UAC_VERSION_1) {
608         struct uac1_as_header_descriptor *as = AudioUsbFindCsintDesc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
609         struct uac_input_terminal_descriptor *iterm;
610 
611         if (as == NULL) {
612             AUDIO_DRIVER_LOG_ERR("UAC_AS_GENERAL descriptor not found.");
613             return HDF_FAILURE;
614         }
615         if (as->bLength < sizeof(*as)) {
616             AUDIO_DRIVER_LOG_ERR("invalid UAC_AS_GENERAL desc.");
617             return HDF_FAILURE;
618         }
619         uacFmt->format = le16_to_cpu(as->wFormatTag); /* remember the format value */
620         iterm = AudioUsbFindInputDesc(audioUsbDriver->ctrlIntf, as->bTerminalLink, uacFmt->protocol);
621         if (iterm != NULL) {
622             uacFmt->channelsNum = iterm->bNrChannels;
623             uacFmt->chconfig = le16_to_cpu(iterm->wChannelConfig);
624         }
625     } else { /* UAC_VERSION_2 */
626         AUDIO_DRIVER_LOG_ERR("Version type not currently supported.");
627         return HDF_ERR_NOT_SUPPORT;
628     }
629 
630     /* get format type */
631     *fmt = AudioUsbFindCsintDesc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
632     if (*fmt == NULL) {
633         AUDIO_DRIVER_LOG_ERR("%u:%d : no UAC_FORMAT_TYPE desc.", uacFmt->ifaceNo, uacFmt->altno);
634         return HDF_FAILURE;
635     }
636     if (((uacFmt->protocol == UAC_VERSION_1) && ((*fmt)->bLength < FORMAT_LRENGTH_8)) ||
637         ((uacFmt->protocol == UAC_VERSION_2) && ((*fmt)->bLength < FORMAT_LRENGTH_6))) {
638         AUDIO_DRIVER_LOG_ERR("%u:%d: invalid UAC_FORMAT_TYPE desc.", uacFmt->ifaceNo, uacFmt->altno);
639         return HDF_FAILURE;
640     }
641     audioUsbDriver->fmtType = (*fmt)->bFormatType;
642     if (uacFmt->bmQuirk && (*fmt)->bNrChannels == 1 && (*fmt)->bSubframeSize == FRAME_SIZE_2) {
643         return HDF_FAILURE;
644     }
645 
646     return HDF_SUCCESS;
647 }
648 
AudioUsbUac12GetFormat(struct AudioUsbDriver * audioUsbDriver,struct usb_host_interface * alts,struct AudioUsbUacFormat * uacFmt,int32_t altsetIdx)649 static struct AudioUsbFormat *AudioUsbUac12GetFormat(struct AudioUsbDriver *audioUsbDriver,
650     struct usb_host_interface *alts, struct AudioUsbUacFormat *uacFmt, int32_t altsetIdx)
651 {
652     struct AudioUsbFormat *audioUsbFormat = NULL;
653     struct usb_device *dev = NULL;
654     struct uac_format_type_i_continuous_descriptor *fmt = NULL;
655 
656     if (audioUsbDriver == NULL || alts == NULL || audioUsbDriver->usbIf == NULL) {
657         AUDIO_DRIVER_LOG_ERR("input para is NULL.");
658         return NULL;
659     }
660     dev = interface_to_usbdev(audioUsbDriver->usbIf);
661     if (dev == NULL) {
662         AUDIO_DRIVER_LOG_ERR("dev is NULL.");
663         return NULL;
664     }
665 
666     if (AudioUsbGetFormat(audioUsbDriver, alts, uacFmt, &fmt) != HDF_SUCCESS) {
667         AUDIO_DRIVER_LOG_ERR("AudioUsbGetFormat failed.");
668         return NULL;
669     }
670     audioUsbFormat = AudioUsbFormatInit(audioUsbDriver, uacFmt, alts, altsetIdx);
671     if (audioUsbFormat == NULL) {
672         AUDIO_DRIVER_LOG_ERR("audioUsbFormat is NULL.");
673         return NULL;
674     }
675     (void)ParseUacEndpointAttr(alts, uacFmt->protocol, &audioUsbFormat->attributes);
676     /* ok, let's parse further... */
677     if (AudioUsbParseFormat(audioUsbDriver, audioUsbFormat, uacFmt->format, fmt, uacFmt->stream) != HDF_SUCCESS) {
678         AudioUsbFreeFormat(audioUsbFormat);
679         return NULL;
680     }
681 
682     if (audioUsbFormat->channels != uacFmt->channelsNum) {
683         uacFmt->chconfig = 0; /* 0 is invalid value */
684     }
685     return audioUsbFormat;
686 }
687 
AudioUsbGetSampleRateQuirk(struct AudioUsbDriver * audioUsbDriver)688 static bool AudioUsbGetSampleRateQuirk(struct AudioUsbDriver *audioUsbDriver)
689 {
690     switch (audioUsbDriver->usbId) {
691         case 0x41E4080:  /* (0x041e, 0x4080) Creative Live Cam VF0610 */
692         case 0x4D8FEEE:  /* (0x04d8, 0xfeee) Benchmark DAC1 Pre */
693         case 0x5560014:  /* (0x0556, 0x0014) Phoenix Audio TMX320VC */
694         case 0x5A71020:  /* (0x05a7, 0x1020) Bose Companion 5 */
695         case 0x74D3553:  /* (0x074d, 0x3553) Outlaw RR2150 (Micronas UAC3553B) */
696         case 0x1395740a: /* (0x1395, 0x740a) Sennheiser DECT */
697         case 0x19010191: /* (0x1901, 0x0191) GE B850V3 CP2114 audio interface */
698         case 0x21b40081: /* (0x21b4, 0x0081) AudioQuest DragonFly */
699         case 0x291230c8: /* (0x2912, 0x30c8) Audioengine D1 */
700         case 0x413ca506: /* (0x413c, 0xa506) Dell AE515 sound bar */
701         case 0x046d084c: /* (0x046d, 0x084c) Logitech ConferenceCam Connect */
702             return true;
703         default:
704             break;
705     }
706 
707     /* devices of these vendors don't support reading rate, either */
708     switch (audioUsbDriver->usbId >> MOVE_16_BIT) {
709         case 0x045e: /* MS Lifecam */
710         case 0x047f: /* Plantronics */
711         case 0x1de7: /* Phoenix Audio */
712             return true;
713         default:
714             return false;
715     }
716 
717     return false;
718 }
719 
AudioUsbV1SetSampleRateSub(struct AudioUsbDriver * audioUsbDriver,struct usb_device * dev,uint32_t ep,uint8_t * data,uint32_t lenth)720 static int32_t AudioUsbV1SetSampleRateSub(
721     struct AudioUsbDriver *audioUsbDriver, struct usb_device *dev, uint32_t ep, uint8_t *data, uint32_t lenth)
722 {
723     struct AudioUsbCtlMsgParam usbCtlMsgParam;
724     int32_t ret;
725 
726     usbCtlMsgParam.pipe = usb_sndctrlpipe(dev, 0);
727     usbCtlMsgParam.request = UAC_SET_CUR;
728     usbCtlMsgParam.requestType = USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT;
729     usbCtlMsgParam.value = UAC_EP_CS_ATTR_SAMPLE_RATE << MOVE_8_BIT;
730     usbCtlMsgParam.index = ep;
731 
732     ret = AudioUsbCtlMsg(dev, &usbCtlMsgParam, data, lenth);
733     if (ret < HDF_SUCCESS) {
734         AUDIO_DRIVER_LOG_ERR("cannot set freq at ep %#x\n", ep);
735         return ret;
736     }
737 
738     /* Don't check the sample rate for devices which we know don't
739      * support reading */
740     if (AudioUsbGetSampleRateQuirk(audioUsbDriver)) {
741         return HDF_SUCCESS;
742     }
743     /* the firmware is likely buggy, don't repeat to fail too many times */
744     if (audioUsbDriver->sampleRateReadError > READ_MAX_ERR_COUNT) {
745         return HDF_SUCCESS;
746     }
747 
748     usbCtlMsgParam.pipe = usb_rcvctrlpipe(dev, 0);
749     usbCtlMsgParam.request = UAC_GET_CUR;
750     usbCtlMsgParam.requestType = USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN;
751     usbCtlMsgParam.value = UAC_EP_CS_ATTR_SAMPLE_RATE << MOVE_8_BIT;
752 
753     ret = AudioUsbCtlMsg(dev, &usbCtlMsgParam, data, lenth);
754     if (ret < HDF_SUCCESS) {
755         AUDIO_DRIVER_LOG_ERR("cannot get freq at ep %#x.", ep);
756         audioUsbDriver->sampleRateReadError++;
757         return HDF_SUCCESS; /* some devices don't support reading */
758     }
759     return HDF_SUCCESS;
760 }
761 
AudioUsbV1SetSampleRate(struct AudioUsbDriver * audioUsbDriver,int32_t iface,struct usb_host_interface * alts,struct AudioUsbFormat * audioUsbFormat,int32_t rate)762 static int32_t AudioUsbV1SetSampleRate(struct AudioUsbDriver *audioUsbDriver, int32_t iface,
763     struct usb_host_interface *alts, struct AudioUsbFormat *audioUsbFormat, int32_t rate)
764 {
765     struct usb_device *dev = NULL;
766     uint32_t ep;
767     uint8_t data[SAMPLE_RATE_3];
768     int32_t ret, currentRate;
769     struct usb_interface_descriptor *inteDesc = NULL;
770     struct usb_endpoint_descriptor *epDesc = NULL;
771 
772     dev = interface_to_usbdev(audioUsbDriver->usbIf);
773 
774     inteDesc = AudioUsbGetIfaceDesc(alts);
775     if (inteDesc->bNumEndpoints < 1) {
776         return HDF_ERR_INVALID_PARAM;
777     }
778     epDesc = AudioEndpointDescriptor(alts, 0);
779     if (epDesc == NULL) {
780         AUDIO_DRIVER_LOG_ERR("epDesc is NULL.");
781         return HDF_FAILURE;
782     }
783     ep = epDesc->bEndpointAddress;
784 
785     /* if endpoint doesn't have sampling rate control, bail out */
786     if ((audioUsbFormat->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE) == 0) {
787         return HDF_SUCCESS;
788     }
789     data[ARRAY_INDEX_0] = rate;
790     data[ARRAY_INDEX_1] = rate >> MOVE_8_BIT;
791     data[ARRAY_INDEX_2] = rate >> MOVE_16_BIT;
792 
793     ret = AudioUsbV1SetSampleRateSub(audioUsbDriver, dev, ep, data, sizeof(data));
794     if (ret != HDF_SUCCESS) {
795         AUDIO_DRIVER_LOG_ERR("%d:%d: cannot set freq %d to ep %#x\n", iface, audioUsbFormat->altsetting, rate, ep);
796         return ret;
797     }
798 
799     // The rate value is composed of data [0], data [1] and data [2].
800     currentRate = data[0] | (data[1] << MOVE_8_BIT) | (data[FORMAT_ALTSETTING_2] << MOVE_16_BIT);
801     if (currentRate == 0) {
802         AUDIO_DRIVER_LOG_INFO("failed to read current rate; disabling the check.");
803         audioUsbDriver->sampleRateReadError = READ_RATE_ERR_COUNT; /* three strikes, see above */
804         return HDF_SUCCESS;
805     }
806 
807     if (currentRate != rate) {
808         AUDIO_DRIVER_LOG_WARNING("current rate %d is different from the runtime rate %d.", currentRate, rate);
809     }
810 
811     return HDF_SUCCESS;
812 }
813 
AudioUsbInitSampleRate(struct AudioUsbDriver * audioUsbDriver,int32_t iface,struct usb_host_interface * alts,struct AudioUsbFormat * audioUsbFormat,int32_t rate)814 int32_t AudioUsbInitSampleRate(struct AudioUsbDriver *audioUsbDriver, int32_t iface, struct usb_host_interface *alts,
815     struct AudioUsbFormat *audioUsbFormat, int32_t rate)
816 {
817     if (audioUsbFormat->protocol == UAC_VERSION_1) {
818         return AudioUsbV1SetSampleRate(audioUsbDriver, iface, alts, audioUsbFormat, rate);
819     } else {
820         return HDF_ERR_NOT_SUPPORT;
821     }
822 }
823 
AudioUsbGetAltsd(struct usb_host_interface ** alts,struct usb_interface * iface,struct AudioUsbUacFormat * uacFmt,int32_t altsetIdx,bool * dataFlag)824 static int32_t AudioUsbGetAltsd(struct usb_host_interface **alts, struct usb_interface *iface,
825     struct AudioUsbUacFormat *uacFmt, int32_t altsetIdx, bool *dataFlag)
826 {
827     struct usb_endpoint_descriptor *epDesc = NULL;
828     bool audioInterface = false;
829     bool vendorInterface = false;
830     bool specInterface = false;
831     bool invalidEndpoint = false;
832     bool invalidPacket = false;
833 
834     *alts = &iface->altsetting[altsetIdx];
835     if (*alts == NULL) {
836         AUDIO_DRIVER_LOG_ERR("alts is NULL.");
837         return HDF_FAILURE;
838     }
839     uacFmt->altsd = AudioUsbGetIfaceDesc(*alts);
840     if (uacFmt->altsd == NULL) {
841         AUDIO_DRIVER_LOG_ERR("altsd is NULL.");
842         return HDF_FAILURE;
843     }
844     uacFmt->protocol = uacFmt->altsd->bInterfaceProtocol;
845     epDesc = AudioEndpointDescriptor(*alts, 0);
846     if (epDesc != NULL) {
847         if (le16_to_cpu(epDesc->wMaxPacketSize) == 0) {
848             invalidPacket = true;
849         }
850     }
851     if (uacFmt->altsd->bInterfaceClass != USB_CLASS_AUDIO) {
852         audioInterface = true;
853     }
854     if (uacFmt->altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
855         uacFmt->altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) {
856         vendorInterface = true;
857     }
858     if (uacFmt->altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) {
859         specInterface = true;
860     }
861     if (uacFmt->altsd->bNumEndpoints < 1) {
862         invalidEndpoint = true;
863     }
864 
865     /* skip invalid one */
866     if (((audioInterface || vendorInterface) && specInterface) || invalidEndpoint || invalidPacket) {
867         *dataFlag = false; /* invalid data flag */
868     }
869     return HDF_SUCCESS;
870 }
871 
AudioUsbInitFormatAndRate(struct AudioUsbDriver * audioUsbDriver,struct AudioUsbFormat * audioUsbFormat,struct AudioUsbUacFormat * uacFmt,struct usb_host_interface * alts,struct usb_device * dev)872 static void AudioUsbInitFormatAndRate(struct AudioUsbDriver *audioUsbDriver, struct AudioUsbFormat *audioUsbFormat,
873     struct AudioUsbUacFormat *uacFmt, struct usb_host_interface *alts, struct usb_device *dev)
874 {
875     if (uacFmt->stream == AUDIO_USB_PCM_STREAM_PLAYBACK) {
876         DListInsertHead(&audioUsbFormat->list, &audioUsbDriver->renderUsbFormatList);
877     } else {
878         DListInsertHead(&audioUsbFormat->list, &audioUsbDriver->captureUsbFormatList);
879     }
880     /* try to set the interface... */
881     usb_set_interface(dev, uacFmt->ifaceNo, uacFmt->altno);
882     (void)AudioUsbInitSampleRate(audioUsbDriver, uacFmt->ifaceNo, alts, audioUsbFormat, audioUsbFormat->rateMax);
883 }
884 
AudioUsbGetFormatSub(struct AudioUsbDriver * audioUsbDriver,struct usb_host_interface * alts,struct AudioUsbUacFormat * uacFmt,int32_t altsetIdx,struct AudioUsbFormat ** audioUsbFormat)885 static void AudioUsbGetFormatSub(struct AudioUsbDriver *audioUsbDriver, struct usb_host_interface *alts,
886     struct AudioUsbUacFormat *uacFmt, int32_t altsetIdx, struct AudioUsbFormat **audioUsbFormat)
887 {
888     struct usb_endpoint_descriptor *epDesc = NULL;
889     bool uacFmtbm = false;
890     bool usbFmtbm = false;
891     bool maxPackSizebm = false;
892 
893     epDesc = AudioEndpointDescriptor(alts, 0);
894     if (epDesc == NULL) {
895         AUDIO_DRIVER_LOG_ERR("epDesc is NULL.");
896         return;
897     }
898     /* check direction */
899     if (epDesc->bEndpointAddress & USB_DIR_IN) {
900         uacFmt->stream = AUDIO_USB_PCM_STREAM_CAPTURE;
901     } else {
902         uacFmt->stream = AUDIO_USB_PCM_STREAM_PLAYBACK;
903     }
904     uacFmt->altno = uacFmt->altsd->bAlternateSetting;
905     if ((audioUsbDriver->usbId >> MOVE_16_BIT) == 0x0582 && /* Edirol */
906         uacFmt->altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC && uacFmt->protocol <= UAC_VERSION_3) {
907         uacFmt->protocol = UAC_VERSION_1;
908     }
909 
910     if (uacFmt->protocol == UAC_VERSION_3) {
911         // UAC_VERSION_3 is not support
912         return;
913     }
914     if (uacFmt->protocol != UAC_VERSION_2) {
915         AUDIO_DRIVER_LOG_INFO(
916             "%u:%d: unknown interface protocol %#02x, assuming v1.", uacFmt->ifaceNo, uacFmt->altno, uacFmt->protocol);
917         uacFmt->protocol = UAC_VERSION_1;
918     }
919 
920     uacFmt->bmQuirk = FORMAT_QUIRK_DISENABLE;
921     if (uacFmt->altno == UAC_FORMAT_TYPE_II && uacFmt->num == AUDIO_USB_FORMAT_NUM_3) {
922         uacFmtbm = true;
923     }
924 
925     if (*audioUsbFormat != NULL) {
926         if ((*audioUsbFormat)->altsetting == FORMAT_ALTSETTING_1 && (*audioUsbFormat)->channels == 1 &&
927             (*audioUsbFormat)->formats == AUDIO_USB_PCM_FMTBIT_S16_LE) {
928             usbFmtbm = true;
929         }
930         if (le16_to_cpu(epDesc->wMaxPacketSize) == (*audioUsbFormat)->maxPackSize * FRAME_SIZE_2) {
931             maxPackSizebm = true;
932         }
933     }
934 
935     if (uacFmtbm && usbFmtbm && uacFmt->protocol == UAC_VERSION_1 && maxPackSizebm) {
936         uacFmt->bmQuirk = FORMAT_QUIRK_ENABLE;
937     }
938     *audioUsbFormat = AudioUsbUac12GetFormat(audioUsbDriver, alts, uacFmt, altsetIdx);
939     return;
940 }
941 
AudioUsbSetFormat(struct AudioUsbDriver * audioUsbDriver,struct AudioUsbUacFormat uacFmt,struct usb_interface * iface,bool * hasNonPcm,bool nonPcm)942 static int32_t AudioUsbSetFormat(struct AudioUsbDriver *audioUsbDriver, struct AudioUsbUacFormat uacFmt,
943     struct usb_interface *iface, bool *hasNonPcm, bool nonPcm)
944 {
945     int32_t altsetIdx;
946     struct usb_host_interface *alts = NULL;
947     bool dataFlag = true;
948     struct usb_endpoint_descriptor *epDesc = NULL;
949     struct AudioUsbFormat *audioUsbFormat = NULL;
950     struct usb_device *dev = audioUsbDriver->dev;
951 
952     for (altsetIdx = 0; altsetIdx < uacFmt.num; altsetIdx++) {
953         if (AudioUsbGetAltsd(&alts, iface, &uacFmt, altsetIdx, &dataFlag) != HDF_SUCCESS) {
954             AUDIO_DRIVER_LOG_ERR("AudioUsbGetAltsd failed.");
955             continue;
956         }
957         if (!dataFlag) {
958             dataFlag = true;
959             continue;
960         }
961         epDesc = AudioEndpointDescriptor(alts, 0);
962         if (epDesc == NULL) {
963             AUDIO_DRIVER_LOG_ERR("epDesc is NULL.");
964             continue;
965         }
966         /* must be isochronous */
967         if ((epDesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
968             continue;
969         }
970         AudioUsbGetFormatSub(audioUsbDriver, alts, &uacFmt, altsetIdx, &audioUsbFormat);
971         if (audioUsbFormat == NULL) {
972             continue;
973         } else if (IS_ERR(audioUsbFormat)) {
974             return PTR_ERR(audioUsbFormat);
975         }
976         if (audioUsbFormat->fmtType != UAC_FORMAT_TYPE_I) {
977             *hasNonPcm = true;
978         }
979         if ((audioUsbFormat->fmtType == UAC_FORMAT_TYPE_I) == nonPcm) {
980             AudioUsbFreeFormat(audioUsbFormat);
981             audioUsbFormat = NULL;
982             continue;
983         }
984         AudioUsbInitFormatAndRate(audioUsbDriver, audioUsbFormat, &uacFmt, alts, dev);
985     }
986     return HDF_SUCCESS;
987 }
988 
AudioUsbParseIfaceSub(struct AudioUsbDriver * audioUsbDriver,int32_t ifaceNo,bool * hasNonPcm,bool nonPcm)989 static int32_t AudioUsbParseIfaceSub(
990     struct AudioUsbDriver *audioUsbDriver, int32_t ifaceNo, bool *hasNonPcm, bool nonPcm)
991 {
992     struct AudioUsbUacFormat uacFmt;
993     struct usb_device *dev = NULL;
994     struct usb_interface *iface = NULL;
995     int ret;
996 
997     if (audioUsbDriver == NULL || hasNonPcm == NULL) {
998         AUDIO_DRIVER_LOG_ERR("audioUsbDriver is NULL.");
999         return HDF_FAILURE;
1000     }
1001     (void)memset_s(&uacFmt, sizeof(struct AudioUsbUacFormat), 0, sizeof(struct AudioUsbUacFormat));
1002     uacFmt.ifaceNo = ifaceNo;
1003     dev = audioUsbDriver->dev;
1004 
1005     /* parse the interface's altsettings */
1006     iface = usb_ifnum_to_if(dev, ifaceNo);
1007     if (iface == NULL) {
1008         AUDIO_DRIVER_LOG_ERR("iface is NULL.");
1009         return HDF_FAILURE;
1010     }
1011     uacFmt.num = iface->num_altsetting;
1012 
1013     /*
1014      * Dallas DS4201 workaround: It presents 5 altsettings, but the last
1015      * one misses syncpipe, and does not produce any sound.
1016      */
1017     if (audioUsbDriver->usbId == AudioUsbGetUsbId(0x04fa, 0x4201)) { /* Dallas DS4201 */
1018         uacFmt.num = AUDIO_USB_FORMAT_NUM_4;
1019     }
1020 
1021     ret = AudioUsbSetFormat(audioUsbDriver, uacFmt, iface, hasNonPcm, nonPcm);
1022     if (ret != HDF_SUCCESS) {
1023         AUDIO_DRIVER_LOG_ERR("AudioUsbSetFormat is failed.");
1024         return HDF_FAILURE;
1025     }
1026     return HDF_SUCCESS;
1027 }
1028 
AudioUsbParseAudioInterface(struct AudioUsbDriver * audioUsbDriver,int32_t ifaceNo)1029 int32_t AudioUsbParseAudioInterface(struct AudioUsbDriver *audioUsbDriver, int32_t ifaceNo)
1030 {
1031     int32_t ret;
1032     bool hasNonPcm = false;
1033 
1034     /* parse PCM formats */
1035     ret = AudioUsbParseIfaceSub(audioUsbDriver, ifaceNo, &hasNonPcm, false);
1036     if (ret != HDF_SUCCESS) {
1037         AUDIO_DRIVER_LOG_ERR("AudioUsbParseIfaceSub failed.");
1038         return ret;
1039     }
1040 
1041     if (!hasNonPcm) {
1042         return HDF_SUCCESS;
1043     }
1044 
1045     /* parse non-PCM formats */
1046     ret = AudioUsbParseIfaceSub(audioUsbDriver, ifaceNo, &hasNonPcm, true);
1047     if (ret < 0) {
1048         AUDIO_DRIVER_LOG_ERR("parse non-PCM formats failed.");
1049         return ret;
1050     }
1051 
1052     return HDF_SUCCESS;
1053 }
1054