1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 */
4
5
6 #include <linux/init.h>
7 #include <linux/slab.h>
8 #include <linux/usb.h>
9 #include <linux/usb/audio.h>
10 #include <linux/usb/audio-v2.h>
11 #include <linux/usb/audio-v3.h>
12
13 #include <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/control.h>
16 #include <sound/tlv.h>
17
18 #include "usbaudio.h"
19 #include "card.h"
20 #include "proc.h"
21 #include "quirks.h"
22 #include "endpoint.h"
23 #include "pcm.h"
24 #include "helper.h"
25 #include "format.h"
26 #include "clock.h"
27 #include "stream.h"
28 #include "power.h"
29 #include "media.h"
30
audioformat_free(struct audioformat * fp)31 static void audioformat_free(struct audioformat *fp)
32 {
33 list_del(&fp->list); /* unlink for avoiding double-free */
34 kfree(fp->rate_table);
35 kfree(fp->chmap);
36 kfree(fp);
37 }
38
39 /*
40 * free a substream
41 */
free_substream(struct snd_usb_substream * subs)42 static void free_substream(struct snd_usb_substream *subs)
43 {
44 struct audioformat *fp, *n;
45
46 if (!subs->num_formats)
47 return; /* not initialized */
48 list_for_each_entry_safe(fp, n, &subs->fmt_list, list)
49 audioformat_free(fp);
50 kfree(subs->str_pd);
51 snd_media_stream_delete(subs);
52 }
53
54
55 /*
56 * free a usb stream instance
57 */
snd_usb_audio_stream_free(struct snd_usb_stream * stream)58 static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
59 {
60 free_substream(&stream->substream[0]);
61 free_substream(&stream->substream[1]);
62 list_del(&stream->list);
63 kfree(stream);
64 }
65
snd_usb_audio_pcm_free(struct snd_pcm * pcm)66 static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
67 {
68 struct snd_usb_stream *stream = pcm->private_data;
69 if (stream) {
70 stream->pcm = NULL;
71 snd_usb_audio_stream_free(stream);
72 }
73 }
74
75 /*
76 * initialize the substream instance.
77 */
78
snd_usb_init_substream(struct snd_usb_stream * as,int stream,struct audioformat * fp,struct snd_usb_power_domain * pd)79 static void snd_usb_init_substream(struct snd_usb_stream *as,
80 int stream,
81 struct audioformat *fp,
82 struct snd_usb_power_domain *pd)
83 {
84 struct snd_usb_substream *subs = &as->substream[stream];
85
86 INIT_LIST_HEAD(&subs->fmt_list);
87 spin_lock_init(&subs->lock);
88
89 subs->stream = as;
90 subs->direction = stream;
91 subs->dev = as->chip->dev;
92 subs->txfr_quirk = !!(as->chip->quirk_flags & QUIRK_FLAG_ALIGN_TRANSFER);
93 subs->tx_length_quirk = !!(as->chip->quirk_flags & QUIRK_FLAG_TX_LENGTH);
94 subs->speed = snd_usb_get_speed(subs->dev);
95 subs->pkt_offset_adj = 0;
96 subs->stream_offset_adj = 0;
97
98 snd_usb_set_pcm_ops(as->pcm, stream);
99
100 list_add_tail(&fp->list, &subs->fmt_list);
101 subs->formats |= fp->formats;
102 subs->num_formats++;
103 subs->fmt_type = fp->fmt_type;
104 subs->ep_num = fp->endpoint;
105 if (fp->channels > subs->channels_max)
106 subs->channels_max = fp->channels;
107
108 if (pd) {
109 subs->str_pd = pd;
110 /* Initialize Power Domain to idle status D1 */
111 snd_usb_power_domain_set(subs->stream->chip, pd,
112 UAC3_PD_STATE_D1);
113 }
114
115 snd_usb_preallocate_buffer(subs);
116 }
117
118 /* kctl callbacks for usb-audio channel maps */
usb_chmap_ctl_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)119 static int usb_chmap_ctl_info(struct snd_kcontrol *kcontrol,
120 struct snd_ctl_elem_info *uinfo)
121 {
122 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
123 struct snd_usb_substream *subs = info->private_data;
124
125 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
126 uinfo->count = subs->channels_max;
127 uinfo->value.integer.min = 0;
128 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
129 return 0;
130 }
131
132 /* check whether a duplicated entry exists in the audiofmt list */
have_dup_chmap(struct snd_usb_substream * subs,struct audioformat * fp)133 static bool have_dup_chmap(struct snd_usb_substream *subs,
134 struct audioformat *fp)
135 {
136 struct audioformat *prev = fp;
137
138 list_for_each_entry_continue_reverse(prev, &subs->fmt_list, list) {
139 if (prev->chmap &&
140 !memcmp(prev->chmap, fp->chmap, sizeof(*fp->chmap)))
141 return true;
142 }
143 return false;
144 }
145
usb_chmap_ctl_tlv(struct snd_kcontrol * kcontrol,int op_flag,unsigned int size,unsigned int __user * tlv)146 static int usb_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
147 unsigned int size, unsigned int __user *tlv)
148 {
149 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
150 struct snd_usb_substream *subs = info->private_data;
151 struct audioformat *fp;
152 unsigned int __user *dst;
153 int count = 0;
154
155 if (size < 8)
156 return -ENOMEM;
157 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
158 return -EFAULT;
159 size -= 8;
160 dst = tlv + 2;
161 list_for_each_entry(fp, &subs->fmt_list, list) {
162 int i, ch_bytes;
163
164 if (!fp->chmap)
165 continue;
166 if (have_dup_chmap(subs, fp))
167 continue;
168 /* copy the entry */
169 ch_bytes = fp->chmap->channels * 4;
170 if (size < 8 + ch_bytes)
171 return -ENOMEM;
172 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
173 put_user(ch_bytes, dst + 1))
174 return -EFAULT;
175 dst += 2;
176 for (i = 0; i < fp->chmap->channels; i++, dst++) {
177 if (put_user(fp->chmap->map[i], dst))
178 return -EFAULT;
179 }
180
181 count += 8 + ch_bytes;
182 size -= 8 + ch_bytes;
183 }
184 if (put_user(count, tlv + 1))
185 return -EFAULT;
186 return 0;
187 }
188
usb_chmap_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)189 static int usb_chmap_ctl_get(struct snd_kcontrol *kcontrol,
190 struct snd_ctl_elem_value *ucontrol)
191 {
192 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
193 struct snd_usb_substream *subs = info->private_data;
194 struct snd_pcm_chmap_elem *chmap = NULL;
195 int i = 0;
196
197 if (subs->cur_audiofmt)
198 chmap = subs->cur_audiofmt->chmap;
199 if (chmap) {
200 for (i = 0; i < chmap->channels; i++)
201 ucontrol->value.integer.value[i] = chmap->map[i];
202 }
203 for (; i < subs->channels_max; i++)
204 ucontrol->value.integer.value[i] = 0;
205 return 0;
206 }
207
208 /* create a chmap kctl assigned to the given USB substream */
add_chmap(struct snd_pcm * pcm,int stream,struct snd_usb_substream * subs)209 static int add_chmap(struct snd_pcm *pcm, int stream,
210 struct snd_usb_substream *subs)
211 {
212 struct audioformat *fp;
213 struct snd_pcm_chmap *chmap;
214 struct snd_kcontrol *kctl;
215 int err;
216
217 list_for_each_entry(fp, &subs->fmt_list, list)
218 if (fp->chmap)
219 goto ok;
220 /* no chmap is found */
221 return 0;
222
223 ok:
224 err = snd_pcm_add_chmap_ctls(pcm, stream, NULL, 0, 0, &chmap);
225 if (err < 0)
226 return err;
227
228 /* override handlers */
229 chmap->private_data = subs;
230 kctl = chmap->kctl;
231 kctl->info = usb_chmap_ctl_info;
232 kctl->get = usb_chmap_ctl_get;
233 kctl->tlv.c = usb_chmap_ctl_tlv;
234
235 return 0;
236 }
237
238 /* convert from USB ChannelConfig bits to ALSA chmap element */
convert_chmap(int channels,unsigned int bits,int protocol)239 static struct snd_pcm_chmap_elem *convert_chmap(int channels, unsigned int bits,
240 int protocol)
241 {
242 static const unsigned int uac1_maps[] = {
243 SNDRV_CHMAP_FL, /* left front */
244 SNDRV_CHMAP_FR, /* right front */
245 SNDRV_CHMAP_FC, /* center front */
246 SNDRV_CHMAP_LFE, /* LFE */
247 SNDRV_CHMAP_RL, /* left surround */
248 SNDRV_CHMAP_RR, /* right surround */
249 SNDRV_CHMAP_FLC, /* left of center */
250 SNDRV_CHMAP_FRC, /* right of center */
251 SNDRV_CHMAP_RC, /* surround */
252 SNDRV_CHMAP_SL, /* side left */
253 SNDRV_CHMAP_SR, /* side right */
254 SNDRV_CHMAP_TC, /* top */
255 0 /* terminator */
256 };
257 static const unsigned int uac2_maps[] = {
258 SNDRV_CHMAP_FL, /* front left */
259 SNDRV_CHMAP_FR, /* front right */
260 SNDRV_CHMAP_FC, /* front center */
261 SNDRV_CHMAP_LFE, /* LFE */
262 SNDRV_CHMAP_RL, /* back left */
263 SNDRV_CHMAP_RR, /* back right */
264 SNDRV_CHMAP_FLC, /* front left of center */
265 SNDRV_CHMAP_FRC, /* front right of center */
266 SNDRV_CHMAP_RC, /* back center */
267 SNDRV_CHMAP_SL, /* side left */
268 SNDRV_CHMAP_SR, /* side right */
269 SNDRV_CHMAP_TC, /* top center */
270 SNDRV_CHMAP_TFL, /* top front left */
271 SNDRV_CHMAP_TFC, /* top front center */
272 SNDRV_CHMAP_TFR, /* top front right */
273 SNDRV_CHMAP_TRL, /* top back left */
274 SNDRV_CHMAP_TRC, /* top back center */
275 SNDRV_CHMAP_TRR, /* top back right */
276 SNDRV_CHMAP_TFLC, /* top front left of center */
277 SNDRV_CHMAP_TFRC, /* top front right of center */
278 SNDRV_CHMAP_LLFE, /* left LFE */
279 SNDRV_CHMAP_RLFE, /* right LFE */
280 SNDRV_CHMAP_TSL, /* top side left */
281 SNDRV_CHMAP_TSR, /* top side right */
282 SNDRV_CHMAP_BC, /* bottom center */
283 SNDRV_CHMAP_RLC, /* back left of center */
284 SNDRV_CHMAP_RRC, /* back right of center */
285 0 /* terminator */
286 };
287 struct snd_pcm_chmap_elem *chmap;
288 const unsigned int *maps;
289 int c;
290
291 if (channels > ARRAY_SIZE(chmap->map))
292 return NULL;
293
294 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
295 if (!chmap)
296 return NULL;
297
298 maps = protocol == UAC_VERSION_2 ? uac2_maps : uac1_maps;
299 chmap->channels = channels;
300 c = 0;
301
302 if (bits) {
303 for (; bits && *maps; maps++, bits >>= 1) {
304 if (bits & 1)
305 chmap->map[c++] = *maps;
306 if (c == chmap->channels)
307 break;
308 }
309 } else {
310 /* If we're missing wChannelConfig, then guess something
311 to make sure the channel map is not skipped entirely */
312 if (channels == 1)
313 chmap->map[c++] = SNDRV_CHMAP_MONO;
314 else
315 for (; c < channels && *maps; maps++)
316 chmap->map[c++] = *maps;
317 }
318
319 for (; c < channels; c++)
320 chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
321
322 return chmap;
323 }
324
325 /* UAC3 device stores channels information in Cluster Descriptors */
326 static struct
convert_chmap_v3(struct uac3_cluster_header_descriptor * cluster)327 snd_pcm_chmap_elem *convert_chmap_v3(struct uac3_cluster_header_descriptor
328 *cluster)
329 {
330 unsigned int channels = cluster->bNrChannels;
331 struct snd_pcm_chmap_elem *chmap;
332 void *p = cluster;
333 int len, c;
334
335 if (channels > ARRAY_SIZE(chmap->map))
336 return NULL;
337
338 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
339 if (!chmap)
340 return NULL;
341
342 len = le16_to_cpu(cluster->wLength);
343 c = 0;
344 p += sizeof(*cluster);
345 len -= sizeof(*cluster);
346
347 while (len > 0 && (c < channels)) {
348 struct uac3_cluster_segment_descriptor *cs_desc = p;
349 u16 cs_len;
350 u8 cs_type;
351
352 if (len < sizeof(*cs_desc))
353 break;
354 cs_len = le16_to_cpu(cs_desc->wLength);
355 if (len < cs_len)
356 break;
357 cs_type = cs_desc->bSegmentType;
358
359 if (cs_type == UAC3_CHANNEL_INFORMATION) {
360 struct uac3_cluster_information_segment_descriptor *is = p;
361 unsigned char map;
362
363 if (cs_len < sizeof(*is))
364 break;
365
366 /*
367 * TODO: this conversion is not complete, update it
368 * after adding UAC3 values to asound.h
369 */
370 switch (is->bChRelationship) {
371 case UAC3_CH_MONO:
372 map = SNDRV_CHMAP_MONO;
373 break;
374 case UAC3_CH_LEFT:
375 case UAC3_CH_FRONT_LEFT:
376 case UAC3_CH_HEADPHONE_LEFT:
377 map = SNDRV_CHMAP_FL;
378 break;
379 case UAC3_CH_RIGHT:
380 case UAC3_CH_FRONT_RIGHT:
381 case UAC3_CH_HEADPHONE_RIGHT:
382 map = SNDRV_CHMAP_FR;
383 break;
384 case UAC3_CH_FRONT_CENTER:
385 map = SNDRV_CHMAP_FC;
386 break;
387 case UAC3_CH_FRONT_LEFT_OF_CENTER:
388 map = SNDRV_CHMAP_FLC;
389 break;
390 case UAC3_CH_FRONT_RIGHT_OF_CENTER:
391 map = SNDRV_CHMAP_FRC;
392 break;
393 case UAC3_CH_SIDE_LEFT:
394 map = SNDRV_CHMAP_SL;
395 break;
396 case UAC3_CH_SIDE_RIGHT:
397 map = SNDRV_CHMAP_SR;
398 break;
399 case UAC3_CH_BACK_LEFT:
400 map = SNDRV_CHMAP_RL;
401 break;
402 case UAC3_CH_BACK_RIGHT:
403 map = SNDRV_CHMAP_RR;
404 break;
405 case UAC3_CH_BACK_CENTER:
406 map = SNDRV_CHMAP_RC;
407 break;
408 case UAC3_CH_BACK_LEFT_OF_CENTER:
409 map = SNDRV_CHMAP_RLC;
410 break;
411 case UAC3_CH_BACK_RIGHT_OF_CENTER:
412 map = SNDRV_CHMAP_RRC;
413 break;
414 case UAC3_CH_TOP_CENTER:
415 map = SNDRV_CHMAP_TC;
416 break;
417 case UAC3_CH_TOP_FRONT_LEFT:
418 map = SNDRV_CHMAP_TFL;
419 break;
420 case UAC3_CH_TOP_FRONT_RIGHT:
421 map = SNDRV_CHMAP_TFR;
422 break;
423 case UAC3_CH_TOP_FRONT_CENTER:
424 map = SNDRV_CHMAP_TFC;
425 break;
426 case UAC3_CH_TOP_FRONT_LOC:
427 map = SNDRV_CHMAP_TFLC;
428 break;
429 case UAC3_CH_TOP_FRONT_ROC:
430 map = SNDRV_CHMAP_TFRC;
431 break;
432 case UAC3_CH_TOP_SIDE_LEFT:
433 map = SNDRV_CHMAP_TSL;
434 break;
435 case UAC3_CH_TOP_SIDE_RIGHT:
436 map = SNDRV_CHMAP_TSR;
437 break;
438 case UAC3_CH_TOP_BACK_LEFT:
439 map = SNDRV_CHMAP_TRL;
440 break;
441 case UAC3_CH_TOP_BACK_RIGHT:
442 map = SNDRV_CHMAP_TRR;
443 break;
444 case UAC3_CH_TOP_BACK_CENTER:
445 map = SNDRV_CHMAP_TRC;
446 break;
447 case UAC3_CH_BOTTOM_CENTER:
448 map = SNDRV_CHMAP_BC;
449 break;
450 case UAC3_CH_LOW_FREQUENCY_EFFECTS:
451 map = SNDRV_CHMAP_LFE;
452 break;
453 case UAC3_CH_LFE_LEFT:
454 map = SNDRV_CHMAP_LLFE;
455 break;
456 case UAC3_CH_LFE_RIGHT:
457 map = SNDRV_CHMAP_RLFE;
458 break;
459 case UAC3_CH_RELATIONSHIP_UNDEFINED:
460 default:
461 map = SNDRV_CHMAP_UNKNOWN;
462 break;
463 }
464 chmap->map[c++] = map;
465 }
466 p += cs_len;
467 len -= cs_len;
468 }
469
470 if (channels < c)
471 pr_err("%s: channel number mismatch\n", __func__);
472
473 chmap->channels = channels;
474
475 for (; c < channels; c++)
476 chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
477
478 return chmap;
479 }
480
481 /*
482 * add this endpoint to the chip instance.
483 * if a stream with the same endpoint already exists, append to it.
484 * if not, create a new pcm stream. note, fp is added to the substream
485 * fmt_list and will be freed on the chip instance release. do not free
486 * fp or do remove it from the substream fmt_list to avoid double-free.
487 */
__snd_usb_add_audio_stream(struct snd_usb_audio * chip,int stream,struct audioformat * fp,struct snd_usb_power_domain * pd)488 static int __snd_usb_add_audio_stream(struct snd_usb_audio *chip,
489 int stream,
490 struct audioformat *fp,
491 struct snd_usb_power_domain *pd)
492
493 {
494 struct snd_usb_stream *as;
495 struct snd_usb_substream *subs;
496 struct snd_pcm *pcm;
497 int err;
498
499 list_for_each_entry(as, &chip->pcm_list, list) {
500 if (as->fmt_type != fp->fmt_type)
501 continue;
502 subs = &as->substream[stream];
503 if (subs->ep_num == fp->endpoint) {
504 list_add_tail(&fp->list, &subs->fmt_list);
505 subs->num_formats++;
506 subs->formats |= fp->formats;
507 return 0;
508 }
509 }
510
511 if (chip->card->registered)
512 chip->need_delayed_register = true;
513
514 /* look for an empty stream */
515 list_for_each_entry(as, &chip->pcm_list, list) {
516 if (as->fmt_type != fp->fmt_type)
517 continue;
518 subs = &as->substream[stream];
519 if (subs->ep_num)
520 continue;
521 err = snd_pcm_new_stream(as->pcm, stream, 1);
522 if (err < 0)
523 return err;
524 snd_usb_init_substream(as, stream, fp, pd);
525 return add_chmap(as->pcm, stream, subs);
526 }
527
528 /* create a new pcm */
529 as = kzalloc(sizeof(*as), GFP_KERNEL);
530 if (!as)
531 return -ENOMEM;
532 as->pcm_index = chip->pcm_devs;
533 as->chip = chip;
534 as->fmt_type = fp->fmt_type;
535 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
536 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
537 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
538 &pcm);
539 if (err < 0) {
540 kfree(as);
541 return err;
542 }
543 as->pcm = pcm;
544 pcm->private_data = as;
545 pcm->private_free = snd_usb_audio_pcm_free;
546 pcm->info_flags = 0;
547 if (chip->pcm_devs > 0)
548 scnprintf(pcm->name, sizeof(pcm->name), "USB Audio #%d",
549 chip->pcm_devs);
550 else
551 strscpy(pcm->name, "USB Audio");
552
553 snd_usb_init_substream(as, stream, fp, pd);
554
555 /*
556 * Keep using head insertion for M-Audio Audiophile USB (tm) which has a
557 * fix to swap capture stream order in conf/cards/USB-audio.conf
558 */
559 if (chip->usb_id == USB_ID(0x0763, 0x2003))
560 list_add(&as->list, &chip->pcm_list);
561 else
562 list_add_tail(&as->list, &chip->pcm_list);
563
564 chip->pcm_devs++;
565
566 snd_usb_proc_pcm_format_add(as);
567
568 return add_chmap(pcm, stream, &as->substream[stream]);
569 }
570
snd_usb_add_audio_stream(struct snd_usb_audio * chip,int stream,struct audioformat * fp)571 int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
572 int stream,
573 struct audioformat *fp)
574 {
575 return __snd_usb_add_audio_stream(chip, stream, fp, NULL);
576 }
577
snd_usb_add_audio_stream_v3(struct snd_usb_audio * chip,int stream,struct audioformat * fp,struct snd_usb_power_domain * pd)578 static int snd_usb_add_audio_stream_v3(struct snd_usb_audio *chip,
579 int stream,
580 struct audioformat *fp,
581 struct snd_usb_power_domain *pd)
582 {
583 return __snd_usb_add_audio_stream(chip, stream, fp, pd);
584 }
585
parse_uac_endpoint_attributes(struct snd_usb_audio * chip,struct usb_host_interface * alts,int protocol,int iface_no)586 static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
587 struct usb_host_interface *alts,
588 int protocol, int iface_no)
589 {
590 /* parsed with a v1 header here. that's ok as we only look at the
591 * header first which is the same for both versions */
592 struct uac_iso_endpoint_descriptor *csep;
593 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
594 int attributes = 0;
595
596 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
597
598 /* Creamware Noah has this descriptor after the 2nd endpoint */
599 if (!csep && altsd->bNumEndpoints >= 2)
600 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
601
602 /*
603 * If we can't locate the USB_DT_CS_ENDPOINT descriptor in the extra
604 * bytes after the first endpoint, go search the entire interface.
605 * Some devices have it directly *before* the standard endpoint.
606 */
607 if (!csep)
608 csep = snd_usb_find_desc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT);
609
610 if (!csep || csep->bLength < 7 ||
611 csep->bDescriptorSubtype != UAC_EP_GENERAL)
612 goto error;
613
614 if (protocol == UAC_VERSION_1) {
615 attributes = csep->bmAttributes;
616 } else if (protocol == UAC_VERSION_2) {
617 struct uac2_iso_endpoint_descriptor *csep2 =
618 (struct uac2_iso_endpoint_descriptor *) csep;
619
620 if (csep2->bLength < sizeof(*csep2))
621 goto error;
622 attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
623
624 /* emulate the endpoint attributes of a v1 device */
625 if (csep2->bmControls & UAC2_CONTROL_PITCH)
626 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
627 } else { /* UAC_VERSION_3 */
628 struct uac3_iso_endpoint_descriptor *csep3 =
629 (struct uac3_iso_endpoint_descriptor *) csep;
630
631 if (csep3->bLength < sizeof(*csep3))
632 goto error;
633 /* emulate the endpoint attributes of a v1 device */
634 if (le32_to_cpu(csep3->bmControls) & UAC2_CONTROL_PITCH)
635 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
636 }
637
638 return attributes;
639
640 error:
641 usb_audio_warn(chip,
642 "%u:%d : no or invalid class specific endpoint descriptor\n",
643 iface_no, altsd->bAlternateSetting);
644 return 0;
645 }
646
647 /* find an input terminal descriptor (either UAC1 or UAC2) with the given
648 * terminal id
649 */
650 static void *
snd_usb_find_input_terminal_descriptor(struct usb_host_interface * ctrl_iface,int terminal_id,int protocol)651 snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
652 int terminal_id, int protocol)
653 {
654 struct uac2_input_terminal_descriptor *term = NULL;
655
656 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
657 ctrl_iface->extralen,
658 term, UAC_INPUT_TERMINAL))) {
659 if (!snd_usb_validate_audio_desc(term, protocol))
660 continue;
661 if (term->bTerminalID == terminal_id)
662 return term;
663 }
664
665 return NULL;
666 }
667
668 static void *
snd_usb_find_output_terminal_descriptor(struct usb_host_interface * ctrl_iface,int terminal_id,int protocol)669 snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
670 int terminal_id, int protocol)
671 {
672 /* OK to use with both UAC2 and UAC3 */
673 struct uac2_output_terminal_descriptor *term = NULL;
674
675 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
676 ctrl_iface->extralen,
677 term, UAC_OUTPUT_TERMINAL))) {
678 if (!snd_usb_validate_audio_desc(term, protocol))
679 continue;
680 if (term->bTerminalID == terminal_id)
681 return term;
682 }
683
684 return NULL;
685 }
686
687 static struct audioformat *
audio_format_alloc_init(struct snd_usb_audio * chip,struct usb_host_interface * alts,int protocol,int iface_no,int altset_idx,int altno,int num_channels,int clock)688 audio_format_alloc_init(struct snd_usb_audio *chip,
689 struct usb_host_interface *alts,
690 int protocol, int iface_no, int altset_idx,
691 int altno, int num_channels, int clock)
692 {
693 struct audioformat *fp;
694
695 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
696 if (!fp)
697 return NULL;
698
699 fp->iface = iface_no;
700 fp->altsetting = altno;
701 fp->altset_idx = altset_idx;
702 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
703 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
704 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
705 fp->protocol = protocol;
706 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
707 fp->channels = num_channels;
708 if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH)
709 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
710 * (fp->maxpacksize & 0x7ff);
711 fp->clock = clock;
712 INIT_LIST_HEAD(&fp->list);
713
714 return fp;
715 }
716
717 static struct audioformat *
snd_usb_get_audioformat_uac12(struct snd_usb_audio * chip,struct usb_host_interface * alts,int protocol,int iface_no,int altset_idx,int altno,int stream,int bm_quirk)718 snd_usb_get_audioformat_uac12(struct snd_usb_audio *chip,
719 struct usb_host_interface *alts,
720 int protocol, int iface_no, int altset_idx,
721 int altno, int stream, int bm_quirk)
722 {
723 struct usb_device *dev = chip->dev;
724 struct uac_format_type_i_continuous_descriptor *fmt;
725 unsigned int num_channels = 0, chconfig = 0;
726 struct usb_host_interface *ctrl_intf;
727 struct audioformat *fp;
728 int clock = 0;
729 u64 format;
730
731 ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
732
733 /* get audio formats */
734 if (protocol == UAC_VERSION_1) {
735 struct uac1_as_header_descriptor *as =
736 snd_usb_find_csint_desc(alts->extra, alts->extralen,
737 NULL, UAC_AS_GENERAL);
738 struct uac_input_terminal_descriptor *iterm;
739
740 if (!as) {
741 dev_err(&dev->dev,
742 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
743 iface_no, altno);
744 return NULL;
745 }
746
747 if (as->bLength < sizeof(*as)) {
748 dev_err(&dev->dev,
749 "%u:%d : invalid UAC_AS_GENERAL desc\n",
750 iface_no, altno);
751 return NULL;
752 }
753
754 format = le16_to_cpu(as->wFormatTag); /* remember the format value */
755
756 iterm = snd_usb_find_input_terminal_descriptor(ctrl_intf,
757 as->bTerminalLink,
758 protocol);
759 if (iterm) {
760 num_channels = iterm->bNrChannels;
761 chconfig = le16_to_cpu(iterm->wChannelConfig);
762 }
763 } else { /* UAC_VERSION_2 */
764 struct uac2_input_terminal_descriptor *input_term;
765 struct uac2_output_terminal_descriptor *output_term;
766 struct uac2_as_header_descriptor *as =
767 snd_usb_find_csint_desc(alts->extra, alts->extralen,
768 NULL, UAC_AS_GENERAL);
769
770 if (!as) {
771 dev_err(&dev->dev,
772 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
773 iface_no, altno);
774 return NULL;
775 }
776
777 if (as->bLength < sizeof(*as)) {
778 dev_err(&dev->dev,
779 "%u:%d : invalid UAC_AS_GENERAL desc\n",
780 iface_no, altno);
781 return NULL;
782 }
783
784 num_channels = as->bNrChannels;
785 format = le32_to_cpu(as->bmFormats);
786 chconfig = le32_to_cpu(as->bmChannelConfig);
787
788 /*
789 * lookup the terminal associated to this interface
790 * to extract the clock
791 */
792 input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf,
793 as->bTerminalLink,
794 protocol);
795 if (input_term) {
796 clock = input_term->bCSourceID;
797 if (!chconfig && (num_channels == input_term->bNrChannels))
798 chconfig = le32_to_cpu(input_term->bmChannelConfig);
799 goto found_clock;
800 }
801
802 output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf,
803 as->bTerminalLink,
804 protocol);
805 if (output_term) {
806 clock = output_term->bCSourceID;
807 goto found_clock;
808 }
809
810 dev_err(&dev->dev,
811 "%u:%d : bogus bTerminalLink %d\n",
812 iface_no, altno, as->bTerminalLink);
813 return NULL;
814 }
815
816 found_clock:
817 /* get format type */
818 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
819 NULL, UAC_FORMAT_TYPE);
820 if (!fmt) {
821 dev_err(&dev->dev,
822 "%u:%d : no UAC_FORMAT_TYPE desc\n",
823 iface_no, altno);
824 return NULL;
825 }
826 if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8))
827 || ((protocol == UAC_VERSION_2) &&
828 (fmt->bLength < 6))) {
829 dev_err(&dev->dev,
830 "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
831 iface_no, altno);
832 return NULL;
833 }
834
835 /*
836 * Blue Microphones workaround: The last altsetting is
837 * identical with the previous one, except for a larger
838 * packet size, but is actually a mislabeled two-channel
839 * setting; ignore it.
840 *
841 * Part 2: analyze quirk flag and format
842 */
843 if (bm_quirk && fmt->bNrChannels == 1 && fmt->bSubframeSize == 2)
844 return NULL;
845
846 fp = audio_format_alloc_init(chip, alts, protocol, iface_no,
847 altset_idx, altno, num_channels, clock);
848 if (!fp)
849 return ERR_PTR(-ENOMEM);
850
851 fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol,
852 iface_no);
853
854 /* some quirks for attributes here */
855 snd_usb_audioformat_attributes_quirk(chip, fp, stream);
856
857 /* ok, let's parse further... */
858 if (snd_usb_parse_audio_format(chip, fp, format,
859 fmt, stream) < 0) {
860 audioformat_free(fp);
861 return NULL;
862 }
863
864 /* Create chmap */
865 if (fp->channels != num_channels)
866 chconfig = 0;
867
868 fp->chmap = convert_chmap(fp->channels, chconfig, protocol);
869
870 return fp;
871 }
872
873 static struct audioformat *
snd_usb_get_audioformat_uac3(struct snd_usb_audio * chip,struct usb_host_interface * alts,struct snd_usb_power_domain ** pd_out,int iface_no,int altset_idx,int altno,int stream)874 snd_usb_get_audioformat_uac3(struct snd_usb_audio *chip,
875 struct usb_host_interface *alts,
876 struct snd_usb_power_domain **pd_out,
877 int iface_no, int altset_idx,
878 int altno, int stream)
879 {
880 struct usb_device *dev = chip->dev;
881 struct uac3_input_terminal_descriptor *input_term;
882 struct uac3_output_terminal_descriptor *output_term;
883 struct uac3_cluster_header_descriptor *cluster;
884 struct uac3_as_header_descriptor *as = NULL;
885 struct uac3_hc_descriptor_header hc_header;
886 struct usb_host_interface *ctrl_intf;
887 struct snd_pcm_chmap_elem *chmap;
888 struct snd_usb_power_domain *pd;
889 unsigned char badd_profile;
890 u64 badd_formats = 0;
891 unsigned int num_channels;
892 struct audioformat *fp;
893 u16 cluster_id, wLength, cluster_wLength;
894 int clock = 0;
895 int err;
896
897 badd_profile = chip->badd_profile;
898 ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
899
900 if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
901 unsigned int maxpacksize =
902 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
903
904 switch (maxpacksize) {
905 default:
906 dev_err(&dev->dev,
907 "%u:%d : incorrect wMaxPacketSize for BADD profile\n",
908 iface_no, altno);
909 return NULL;
910 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
911 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
912 badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
913 num_channels = 1;
914 break;
915 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
916 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
917 badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
918 num_channels = 1;
919 break;
920 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
921 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
922 badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
923 num_channels = 2;
924 break;
925 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
926 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
927 badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
928 num_channels = 2;
929 break;
930 }
931
932 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
933 if (!chmap)
934 return ERR_PTR(-ENOMEM);
935
936 if (num_channels == 1) {
937 chmap->map[0] = SNDRV_CHMAP_MONO;
938 } else {
939 chmap->map[0] = SNDRV_CHMAP_FL;
940 chmap->map[1] = SNDRV_CHMAP_FR;
941 }
942
943 chmap->channels = num_channels;
944 clock = UAC3_BADD_CS_ID9;
945 goto found_clock;
946 }
947
948 as = snd_usb_find_csint_desc(alts->extra, alts->extralen,
949 NULL, UAC_AS_GENERAL);
950 if (!as) {
951 dev_err(&dev->dev,
952 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
953 iface_no, altno);
954 return NULL;
955 }
956
957 if (as->bLength < sizeof(*as)) {
958 dev_err(&dev->dev,
959 "%u:%d : invalid UAC_AS_GENERAL desc\n",
960 iface_no, altno);
961 return NULL;
962 }
963
964 cluster_id = le16_to_cpu(as->wClusterDescrID);
965 if (!cluster_id) {
966 dev_err(&dev->dev,
967 "%u:%d : no cluster descriptor\n",
968 iface_no, altno);
969 return NULL;
970 }
971
972 /*
973 * Get number of channels and channel map through
974 * High Capability Cluster Descriptor
975 *
976 * First step: get High Capability header and
977 * read size of Cluster Descriptor
978 */
979 err = snd_usb_ctl_msg(chip->dev,
980 usb_rcvctrlpipe(chip->dev, 0),
981 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
982 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
983 cluster_id,
984 snd_usb_ctrl_intf(ctrl_intf),
985 &hc_header, sizeof(hc_header));
986 if (err < 0)
987 return ERR_PTR(err);
988 else if (err != sizeof(hc_header)) {
989 dev_err(&dev->dev,
990 "%u:%d : can't get High Capability descriptor\n",
991 iface_no, altno);
992 return ERR_PTR(-EIO);
993 }
994
995 /*
996 * Second step: allocate needed amount of memory
997 * and request Cluster Descriptor
998 */
999 wLength = le16_to_cpu(hc_header.wLength);
1000 if (wLength < sizeof(cluster))
1001 return NULL;
1002 cluster = kzalloc(wLength, GFP_KERNEL);
1003 if (!cluster)
1004 return ERR_PTR(-ENOMEM);
1005 err = snd_usb_ctl_msg(chip->dev,
1006 usb_rcvctrlpipe(chip->dev, 0),
1007 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
1008 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1009 cluster_id,
1010 snd_usb_ctrl_intf(ctrl_intf),
1011 cluster, wLength);
1012 if (err < 0) {
1013 kfree(cluster);
1014 return ERR_PTR(err);
1015 } else if (err != wLength) {
1016 dev_err(&dev->dev,
1017 "%u:%d : can't get Cluster Descriptor\n",
1018 iface_no, altno);
1019 kfree(cluster);
1020 return ERR_PTR(-EIO);
1021 }
1022
1023 cluster_wLength = le16_to_cpu(cluster->wLength);
1024 if (cluster_wLength < sizeof(*cluster) ||
1025 cluster_wLength > wLength) {
1026 dev_err(&dev->dev,
1027 "%u:%d : invalid Cluster Descriptor size\n",
1028 iface_no, altno);
1029 kfree(cluster);
1030 return ERR_PTR(-EIO);
1031 }
1032
1033 num_channels = cluster->bNrChannels;
1034 chmap = convert_chmap_v3(cluster);
1035 kfree(cluster);
1036
1037 /*
1038 * lookup the terminal associated to this interface
1039 * to extract the clock
1040 */
1041 input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf,
1042 as->bTerminalLink,
1043 UAC_VERSION_3);
1044 if (input_term) {
1045 clock = input_term->bCSourceID;
1046 goto found_clock;
1047 }
1048
1049 output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf,
1050 as->bTerminalLink,
1051 UAC_VERSION_3);
1052 if (output_term) {
1053 clock = output_term->bCSourceID;
1054 goto found_clock;
1055 }
1056
1057 dev_err(&dev->dev, "%u:%d : bogus bTerminalLink %d\n",
1058 iface_no, altno, as->bTerminalLink);
1059 kfree(chmap);
1060 return NULL;
1061
1062 found_clock:
1063 fp = audio_format_alloc_init(chip, alts, UAC_VERSION_3, iface_no,
1064 altset_idx, altno, num_channels, clock);
1065 if (!fp) {
1066 kfree(chmap);
1067 return ERR_PTR(-ENOMEM);
1068 }
1069
1070 fp->chmap = chmap;
1071
1072 if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
1073 fp->attributes = 0; /* No attributes */
1074
1075 fp->fmt_type = UAC_FORMAT_TYPE_I;
1076 fp->formats = badd_formats;
1077
1078 fp->nr_rates = 0; /* SNDRV_PCM_RATE_CONTINUOUS */
1079 fp->rate_min = UAC3_BADD_SAMPLING_RATE;
1080 fp->rate_max = UAC3_BADD_SAMPLING_RATE;
1081 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
1082
1083 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
1084 if (!pd) {
1085 audioformat_free(fp);
1086 return NULL;
1087 }
1088 pd->pd_id = (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
1089 UAC3_BADD_PD_ID10 : UAC3_BADD_PD_ID11;
1090 pd->pd_d1d0_rec = UAC3_BADD_PD_RECOVER_D1D0;
1091 pd->pd_d2d0_rec = UAC3_BADD_PD_RECOVER_D2D0;
1092 pd->ctrl_iface = ctrl_intf;
1093
1094 } else {
1095 fp->attributes = parse_uac_endpoint_attributes(chip, alts,
1096 UAC_VERSION_3,
1097 iface_no);
1098
1099 pd = snd_usb_find_power_domain(ctrl_intf,
1100 as->bTerminalLink);
1101
1102 /* ok, let's parse further... */
1103 if (snd_usb_parse_audio_format_v3(chip, fp, as, stream) < 0) {
1104 kfree(pd);
1105 audioformat_free(fp);
1106 return NULL;
1107 }
1108 }
1109
1110 if (pd)
1111 *pd_out = pd;
1112
1113 return fp;
1114 }
1115
__snd_usb_parse_audio_interface(struct snd_usb_audio * chip,int iface_no,bool * has_non_pcm,bool non_pcm)1116 static int __snd_usb_parse_audio_interface(struct snd_usb_audio *chip,
1117 int iface_no,
1118 bool *has_non_pcm, bool non_pcm)
1119 {
1120 struct usb_device *dev;
1121 struct usb_interface *iface;
1122 struct usb_host_interface *alts;
1123 struct usb_interface_descriptor *altsd;
1124 int i, altno, err, stream;
1125 struct audioformat *fp = NULL;
1126 struct snd_usb_power_domain *pd = NULL;
1127 bool set_iface_first;
1128 int num, protocol;
1129
1130 dev = chip->dev;
1131
1132 /* parse the interface's altsettings */
1133 iface = usb_ifnum_to_if(dev, iface_no);
1134
1135 num = iface->num_altsetting;
1136
1137 /*
1138 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
1139 * one misses syncpipe, and does not produce any sound.
1140 */
1141 if (chip->usb_id == USB_ID(0x04fa, 0x4201) && num >= 4)
1142 num = 4;
1143
1144 for (i = 0; i < num; i++) {
1145 alts = &iface->altsetting[i];
1146 altsd = get_iface_desc(alts);
1147 protocol = altsd->bInterfaceProtocol;
1148 /* skip invalid one */
1149 if (((altsd->bInterfaceClass != USB_CLASS_AUDIO ||
1150 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
1151 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC)) &&
1152 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1153 altsd->bNumEndpoints < 1 ||
1154 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
1155 continue;
1156 /* must be isochronous */
1157 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1158 USB_ENDPOINT_XFER_ISOC)
1159 continue;
1160 /* check direction */
1161 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
1162 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
1163 altno = altsd->bAlternateSetting;
1164
1165 if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
1166 continue;
1167
1168 /*
1169 * Roland audio streaming interfaces are marked with protocols
1170 * 0/1/2, but are UAC 1 compatible.
1171 */
1172 if (USB_ID_VENDOR(chip->usb_id) == 0x0582 &&
1173 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
1174 protocol <= 2)
1175 protocol = UAC_VERSION_1;
1176
1177 switch (protocol) {
1178 default:
1179 dev_dbg(&dev->dev, "%u:%d: unknown interface protocol %#02x, assuming v1\n",
1180 iface_no, altno, protocol);
1181 protocol = UAC_VERSION_1;
1182 fallthrough;
1183 case UAC_VERSION_1:
1184 case UAC_VERSION_2: {
1185 int bm_quirk = 0;
1186
1187 /*
1188 * Blue Microphones workaround: The last altsetting is
1189 * identical with the previous one, except for a larger
1190 * packet size, but is actually a mislabeled two-channel
1191 * setting; ignore it.
1192 *
1193 * Part 1: prepare quirk flag
1194 */
1195 if (altno == 2 && num == 3 &&
1196 fp && fp->altsetting == 1 && fp->channels == 1 &&
1197 fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
1198 protocol == UAC_VERSION_1 &&
1199 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
1200 fp->maxpacksize * 2)
1201 bm_quirk = 1;
1202
1203 fp = snd_usb_get_audioformat_uac12(chip, alts, protocol,
1204 iface_no, i, altno,
1205 stream, bm_quirk);
1206 break;
1207 }
1208 case UAC_VERSION_3:
1209 fp = snd_usb_get_audioformat_uac3(chip, alts, &pd,
1210 iface_no, i, altno, stream);
1211 break;
1212 }
1213
1214 if (!fp)
1215 continue;
1216 else if (IS_ERR(fp))
1217 return PTR_ERR(fp);
1218
1219 if (fp->fmt_type != UAC_FORMAT_TYPE_I)
1220 *has_non_pcm = true;
1221 if ((fp->fmt_type == UAC_FORMAT_TYPE_I) == non_pcm) {
1222 audioformat_free(fp);
1223 kfree(pd);
1224 fp = NULL;
1225 pd = NULL;
1226 continue;
1227 }
1228
1229 snd_usb_audioformat_set_sync_ep(chip, fp);
1230
1231 dev_dbg(&dev->dev, "%u:%d: add audio endpoint %#x\n", iface_no, altno, fp->endpoint);
1232 if (protocol == UAC_VERSION_3)
1233 err = snd_usb_add_audio_stream_v3(chip, stream, fp, pd);
1234 else
1235 err = snd_usb_add_audio_stream(chip, stream, fp);
1236
1237 if (err < 0) {
1238 audioformat_free(fp);
1239 kfree(pd);
1240 return err;
1241 }
1242
1243 /* add endpoints */
1244 err = snd_usb_add_endpoint(chip, fp->endpoint,
1245 SND_USB_ENDPOINT_TYPE_DATA);
1246 if (err < 0)
1247 return err;
1248
1249 if (fp->sync_ep) {
1250 err = snd_usb_add_endpoint(chip, fp->sync_ep,
1251 fp->implicit_fb ?
1252 SND_USB_ENDPOINT_TYPE_DATA :
1253 SND_USB_ENDPOINT_TYPE_SYNC);
1254 if (err < 0)
1255 return err;
1256 }
1257
1258 set_iface_first = false;
1259 if (protocol == UAC_VERSION_1 ||
1260 (chip->quirk_flags & QUIRK_FLAG_SET_IFACE_FIRST))
1261 set_iface_first = true;
1262
1263 /* try to set the interface... */
1264 usb_set_interface(chip->dev, iface_no, 0);
1265 if (set_iface_first)
1266 usb_set_interface(chip->dev, iface_no, altno);
1267 snd_usb_init_pitch(chip, fp);
1268 snd_usb_init_sample_rate(chip, fp, fp->rate_max);
1269 if (!set_iface_first)
1270 usb_set_interface(chip->dev, iface_no, altno);
1271 }
1272 return 0;
1273 }
1274
snd_usb_parse_audio_interface(struct snd_usb_audio * chip,int iface_no)1275 int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
1276 {
1277 int err;
1278 bool has_non_pcm = false;
1279
1280 /* parse PCM formats */
1281 err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, false);
1282 if (err < 0)
1283 return err;
1284
1285 if (has_non_pcm) {
1286 /* parse non-PCM formats */
1287 err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, true);
1288 if (err < 0)
1289 return err;
1290 }
1291
1292 return 0;
1293 }
1294
1295