xref: /linux/sound/usb/stream.c (revision a8e7ef3cec99ba2487110e01d77a8a278593b3e9) !
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 
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  */
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  */
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 
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 
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 */
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 */
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 
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 
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 */
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 */
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_obj(*chmap);
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
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_obj(*chmap);
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 			 * NOTE: not all UAC3 channel relationship have a
370 			 * direct ALSA chmap equivalent.
371 			 */
372 			switch (is->bChRelationship) {
373 			case UAC3_CH_MONO:
374 				map = SNDRV_CHMAP_MONO;
375 				break;
376 			case UAC3_CH_LEFT:
377 			case UAC3_CH_FRONT_LEFT:
378 			case UAC3_CH_HEADPHONE_LEFT:
379 				map = SNDRV_CHMAP_FL;
380 				break;
381 			case UAC3_CH_RIGHT:
382 			case UAC3_CH_FRONT_RIGHT:
383 			case UAC3_CH_HEADPHONE_RIGHT:
384 				map = SNDRV_CHMAP_FR;
385 				break;
386 			case UAC3_CH_FRONT_CENTER:
387 				map = SNDRV_CHMAP_FC;
388 				break;
389 			case UAC3_CH_FRONT_LEFT_OF_CENTER:
390 				map = SNDRV_CHMAP_FLC;
391 				break;
392 			case UAC3_CH_FRONT_RIGHT_OF_CENTER:
393 				map = SNDRV_CHMAP_FRC;
394 				break;
395 			case UAC3_CH_FRONT_WIDE_LEFT:
396 				map = SNDRV_CHMAP_FLW;
397 				break;
398 			case UAC3_CH_FRONT_WIDE_RIGHT:
399 				map = SNDRV_CHMAP_FRW;
400 				break;
401 			case UAC3_CH_SIDE_LEFT:
402 				map = SNDRV_CHMAP_SL;
403 				break;
404 			case UAC3_CH_SIDE_RIGHT:
405 				map = SNDRV_CHMAP_SR;
406 				break;
407 			case UAC3_CH_BACK_LEFT:
408 				map = SNDRV_CHMAP_RL;
409 				break;
410 			case UAC3_CH_BACK_RIGHT:
411 				map = SNDRV_CHMAP_RR;
412 				break;
413 			case UAC3_CH_BACK_CENTER:
414 				map = SNDRV_CHMAP_RC;
415 				break;
416 			case UAC3_CH_BACK_LEFT_OF_CENTER:
417 				map = SNDRV_CHMAP_RLC;
418 				break;
419 			case UAC3_CH_BACK_RIGHT_OF_CENTER:
420 				map = SNDRV_CHMAP_RRC;
421 				break;
422 			case UAC3_CH_TOP_CENTER:
423 				map = SNDRV_CHMAP_TC;
424 				break;
425 			case UAC3_CH_TOP_FRONT_LEFT:
426 				map = SNDRV_CHMAP_TFL;
427 				break;
428 			case UAC3_CH_TOP_FRONT_RIGHT:
429 				map = SNDRV_CHMAP_TFR;
430 				break;
431 			case UAC3_CH_TOP_FRONT_CENTER:
432 				map = SNDRV_CHMAP_TFC;
433 				break;
434 			case UAC3_CH_TOP_FRONT_LOC:
435 				map = SNDRV_CHMAP_TFLC;
436 				break;
437 			case UAC3_CH_TOP_FRONT_ROC:
438 				map = SNDRV_CHMAP_TFRC;
439 				break;
440 			case UAC3_CH_TOP_SIDE_LEFT:
441 				map = SNDRV_CHMAP_TSL;
442 				break;
443 			case UAC3_CH_TOP_SIDE_RIGHT:
444 				map = SNDRV_CHMAP_TSR;
445 				break;
446 			case UAC3_CH_TOP_BACK_LEFT:
447 				map = SNDRV_CHMAP_TRL;
448 				break;
449 			case UAC3_CH_TOP_BACK_RIGHT:
450 				map = SNDRV_CHMAP_TRR;
451 				break;
452 			case UAC3_CH_TOP_BACK_CENTER:
453 				map = SNDRV_CHMAP_TRC;
454 				break;
455 			case UAC3_CH_BOTTOM_CENTER:
456 				map = SNDRV_CHMAP_BC;
457 				break;
458 			case UAC3_CH_LOW_FREQUENCY_EFFECTS:
459 				map = SNDRV_CHMAP_LFE;
460 				break;
461 			case UAC3_CH_LFE_LEFT:
462 				map = SNDRV_CHMAP_LLFE;
463 				break;
464 			case UAC3_CH_LFE_RIGHT:
465 				map = SNDRV_CHMAP_RLFE;
466 				break;
467 			case UAC3_CH_RELATIONSHIP_UNDEFINED:
468 			default:
469 				map = SNDRV_CHMAP_UNKNOWN;
470 				break;
471 			}
472 			chmap->map[c++] = map;
473 		}
474 		p += cs_len;
475 		len -= cs_len;
476 	}
477 
478 	if (channels < c)
479 		pr_err("%s: channel number mismatch\n", __func__);
480 
481 	chmap->channels = channels;
482 
483 	for (; c < channels; c++)
484 		chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
485 
486 	return chmap;
487 }
488 
489 /*
490  * add this endpoint to the chip instance.
491  * if a stream with the same endpoint already exists, append to it.
492  * if not, create a new pcm stream. note, fp is added to the substream
493  * fmt_list and will be freed on the chip instance release. do not free
494  * fp or do remove it from the substream fmt_list to avoid double-free.
495  */
496 static int __snd_usb_add_audio_stream(struct snd_usb_audio *chip,
497 				      int stream,
498 				      struct audioformat *fp,
499 				      struct snd_usb_power_domain *pd)
500 
501 {
502 	struct snd_usb_stream *as;
503 	struct snd_usb_substream *subs;
504 	struct snd_pcm *pcm;
505 	int err;
506 
507 	list_for_each_entry(as, &chip->pcm_list, list) {
508 		if (as->fmt_type != fp->fmt_type)
509 			continue;
510 		subs = &as->substream[stream];
511 		if (subs->ep_num == fp->endpoint) {
512 			list_add_tail(&fp->list, &subs->fmt_list);
513 			subs->num_formats++;
514 			subs->formats |= fp->formats;
515 			return 0;
516 		}
517 	}
518 
519 	if (chip->card->registered)
520 		chip->need_delayed_register = true;
521 
522 	/* look for an empty stream */
523 	list_for_each_entry(as, &chip->pcm_list, list) {
524 		if (as->fmt_type != fp->fmt_type)
525 			continue;
526 		subs = &as->substream[stream];
527 		if (subs->ep_num)
528 			continue;
529 		err = snd_pcm_new_stream(as->pcm, stream, 1);
530 		if (err < 0)
531 			return err;
532 		snd_usb_init_substream(as, stream, fp, pd);
533 		return add_chmap(as->pcm, stream, subs);
534 	}
535 
536 	/* create a new pcm */
537 	as = kzalloc_obj(*as);
538 	if (!as)
539 		return -ENOMEM;
540 	as->pcm_index = chip->pcm_devs;
541 	as->chip = chip;
542 	as->fmt_type = fp->fmt_type;
543 	err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
544 			  stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
545 			  stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
546 			  &pcm);
547 	if (err < 0) {
548 		kfree(as);
549 		return err;
550 	}
551 	as->pcm = pcm;
552 	pcm->private_data = as;
553 	pcm->private_free = snd_usb_audio_pcm_free;
554 	pcm->info_flags = 0;
555 	if (chip->pcm_devs > 0)
556 		scnprintf(pcm->name, sizeof(pcm->name), "USB Audio #%d",
557 			  chip->pcm_devs);
558 	else
559 		strscpy(pcm->name, "USB Audio");
560 
561 	snd_usb_init_substream(as, stream, fp, pd);
562 
563 	/*
564 	 * Keep using head insertion for M-Audio Audiophile USB (tm) which has a
565 	 * fix to swap capture stream order in conf/cards/USB-audio.conf
566 	 */
567 	if (chip->usb_id == USB_ID(0x0763, 0x2003))
568 		list_add(&as->list, &chip->pcm_list);
569 	else
570 		list_add_tail(&as->list, &chip->pcm_list);
571 
572 	chip->pcm_devs++;
573 
574 	snd_usb_proc_pcm_format_add(as);
575 
576 	return add_chmap(pcm, stream, &as->substream[stream]);
577 }
578 
579 int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
580 			     int stream,
581 			     struct audioformat *fp)
582 {
583 	return __snd_usb_add_audio_stream(chip, stream, fp, NULL);
584 }
585 
586 static int snd_usb_add_audio_stream_v3(struct snd_usb_audio *chip,
587 				       int stream,
588 				       struct audioformat *fp,
589 				       struct snd_usb_power_domain *pd)
590 {
591 	return __snd_usb_add_audio_stream(chip, stream, fp, pd);
592 }
593 
594 static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
595 					 struct usb_host_interface *alts,
596 					 int protocol, int iface_no)
597 {
598 	/* parsed with a v1 header here. that's ok as we only look at the
599 	 * header first which is the same for both versions */
600 	struct uac_iso_endpoint_descriptor *csep;
601 	struct usb_interface_descriptor *altsd = get_iface_desc(alts);
602 	int attributes = 0;
603 
604 	csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
605 
606 	/* Creamware Noah has this descriptor after the 2nd endpoint */
607 	if (!csep && altsd->bNumEndpoints >= 2)
608 		csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
609 
610 	/*
611 	 * If we can't locate the USB_DT_CS_ENDPOINT descriptor in the extra
612 	 * bytes after the first endpoint, go search the entire interface.
613 	 * Some devices have it directly *before* the standard endpoint.
614 	 */
615 	if (!csep)
616 		csep = snd_usb_find_desc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT);
617 
618 	if (!csep || csep->bLength < 7 ||
619 	    csep->bDescriptorSubtype != UAC_EP_GENERAL)
620 		goto error;
621 
622 	if (protocol == UAC_VERSION_1) {
623 		attributes = csep->bmAttributes;
624 	} else if (protocol == UAC_VERSION_2) {
625 		struct uac2_iso_endpoint_descriptor *csep2 =
626 			(struct uac2_iso_endpoint_descriptor *) csep;
627 
628 		if (csep2->bLength < sizeof(*csep2))
629 			goto error;
630 		attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
631 
632 		/* emulate the endpoint attributes of a v1 device */
633 		if (csep2->bmControls & UAC2_CONTROL_PITCH)
634 			attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
635 	} else { /* UAC_VERSION_3 */
636 		struct uac3_iso_endpoint_descriptor *csep3 =
637 			(struct uac3_iso_endpoint_descriptor *) csep;
638 
639 		if (csep3->bLength < sizeof(*csep3))
640 			goto error;
641 		/* emulate the endpoint attributes of a v1 device */
642 		if (le32_to_cpu(csep3->bmControls) & UAC2_CONTROL_PITCH)
643 			attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
644 	}
645 
646 	return attributes;
647 
648  error:
649 	usb_audio_warn(chip,
650 		       "%u:%d : no or invalid class specific endpoint descriptor\n",
651 		       iface_no, altsd->bAlternateSetting);
652 	return 0;
653 }
654 
655 /* find an input terminal descriptor (either UAC1 or UAC2) with the given
656  * terminal id
657  */
658 static void *
659 snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
660 				       int terminal_id, int protocol)
661 {
662 	struct uac2_input_terminal_descriptor *term = NULL;
663 
664 	while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
665 					       ctrl_iface->extralen,
666 					       term, UAC_INPUT_TERMINAL))) {
667 		if (!snd_usb_validate_audio_desc(term, protocol))
668 			continue;
669 		if (term->bTerminalID == terminal_id)
670 			return term;
671 	}
672 
673 	return NULL;
674 }
675 
676 static void *
677 snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
678 					int terminal_id, int protocol)
679 {
680 	/* OK to use with both UAC2 and UAC3 */
681 	struct uac2_output_terminal_descriptor *term = NULL;
682 
683 	while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
684 					       ctrl_iface->extralen,
685 					       term, UAC_OUTPUT_TERMINAL))) {
686 		if (!snd_usb_validate_audio_desc(term, protocol))
687 			continue;
688 		if (term->bTerminalID == terminal_id)
689 			return term;
690 	}
691 
692 	return NULL;
693 }
694 
695 static struct audioformat *
696 audio_format_alloc_init(struct snd_usb_audio *chip,
697 		       struct usb_host_interface *alts,
698 		       int protocol, int iface_no, int altset_idx,
699 		       int altno, int num_channels, int clock)
700 {
701 	struct usb_host_endpoint *ep = &alts->endpoint[0];
702 	struct audioformat *fp;
703 
704 	fp = kzalloc_obj(*fp);
705 	if (!fp)
706 		return NULL;
707 
708 	fp->iface = iface_no;
709 	fp->altsetting = altno;
710 	fp->altset_idx = altset_idx;
711 	fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
712 	fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
713 	fp->datainterval = snd_usb_parse_datainterval(chip, alts);
714 	fp->protocol = protocol;
715 	fp->maxpacksize = usb_endpoint_max_periodic_payload(chip->dev, ep);
716 	fp->channels = num_channels;
717 	fp->clock = clock;
718 	INIT_LIST_HEAD(&fp->list);
719 
720 	return fp;
721 }
722 
723 static struct audioformat *
724 snd_usb_get_audioformat_uac12(struct snd_usb_audio *chip,
725 			      struct usb_host_interface *alts,
726 			      int protocol, int iface_no, int altset_idx,
727 			      int altno, int stream, int bm_quirk)
728 {
729 	struct usb_device *dev = chip->dev;
730 	struct uac_format_type_i_continuous_descriptor *fmt;
731 	unsigned int num_channels = 0, chconfig = 0;
732 	struct usb_host_interface *ctrl_intf;
733 	struct audioformat *fp;
734 	int clock = 0;
735 	u64 format;
736 
737 	ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
738 
739 	/* get audio formats */
740 	if (protocol == UAC_VERSION_1) {
741 		struct uac1_as_header_descriptor *as =
742 			snd_usb_find_csint_desc(alts->extra, alts->extralen,
743 						NULL, UAC_AS_GENERAL);
744 		struct uac_input_terminal_descriptor *iterm;
745 
746 		if (!as) {
747 			dev_err(&dev->dev,
748 				"%u:%d : UAC_AS_GENERAL descriptor not found\n",
749 				iface_no, altno);
750 			return NULL;
751 		}
752 
753 		if (as->bLength < sizeof(*as)) {
754 			dev_err(&dev->dev,
755 				"%u:%d : invalid UAC_AS_GENERAL desc\n",
756 				iface_no, altno);
757 			return NULL;
758 		}
759 
760 		format = le16_to_cpu(as->wFormatTag); /* remember the format value */
761 
762 		iterm = snd_usb_find_input_terminal_descriptor(ctrl_intf,
763 							       as->bTerminalLink,
764 							       protocol);
765 		if (iterm) {
766 			num_channels = iterm->bNrChannels;
767 			chconfig = le16_to_cpu(iterm->wChannelConfig);
768 		}
769 	} else { /* UAC_VERSION_2 */
770 		struct uac2_input_terminal_descriptor *input_term;
771 		struct uac2_output_terminal_descriptor *output_term;
772 		struct uac2_as_header_descriptor *as =
773 			snd_usb_find_csint_desc(alts->extra, alts->extralen,
774 						NULL, UAC_AS_GENERAL);
775 
776 		if (!as) {
777 			dev_err(&dev->dev,
778 				"%u:%d : UAC_AS_GENERAL descriptor not found\n",
779 				iface_no, altno);
780 			return NULL;
781 		}
782 
783 		if (as->bLength < sizeof(*as)) {
784 			dev_err(&dev->dev,
785 				"%u:%d : invalid UAC_AS_GENERAL desc\n",
786 				iface_no, altno);
787 			return NULL;
788 		}
789 
790 		num_channels = as->bNrChannels;
791 		format = le32_to_cpu(as->bmFormats);
792 		chconfig = le32_to_cpu(as->bmChannelConfig);
793 
794 		/*
795 		 * lookup the terminal associated to this interface
796 		 * to extract the clock
797 		 */
798 		input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf,
799 								    as->bTerminalLink,
800 								    protocol);
801 		if (input_term) {
802 			clock = input_term->bCSourceID;
803 			if (!chconfig && (num_channels == input_term->bNrChannels))
804 				chconfig = le32_to_cpu(input_term->bmChannelConfig);
805 			goto found_clock;
806 		}
807 
808 		output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf,
809 								      as->bTerminalLink,
810 								      protocol);
811 		if (output_term) {
812 			clock = output_term->bCSourceID;
813 			goto found_clock;
814 		}
815 
816 		dev_err(&dev->dev,
817 			"%u:%d : bogus bTerminalLink %d\n",
818 			iface_no, altno, as->bTerminalLink);
819 		return NULL;
820 	}
821 
822 found_clock:
823 	/* get format type */
824 	fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
825 				      NULL, UAC_FORMAT_TYPE);
826 	if (!fmt) {
827 		dev_err(&dev->dev,
828 			"%u:%d : no UAC_FORMAT_TYPE desc\n",
829 			iface_no, altno);
830 		return NULL;
831 	}
832 	if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8))
833 			|| ((protocol == UAC_VERSION_2) &&
834 					(fmt->bLength < 6))) {
835 		dev_err(&dev->dev,
836 			"%u:%d : invalid UAC_FORMAT_TYPE desc\n",
837 			iface_no, altno);
838 		return NULL;
839 	}
840 
841 	/*
842 	 * Blue Microphones workaround: The last altsetting is
843 	 * identical with the previous one, except for a larger
844 	 * packet size, but is actually a mislabeled two-channel
845 	 * setting; ignore it.
846 	 *
847 	 * Part 2: analyze quirk flag and format
848 	 */
849 	if (bm_quirk && fmt->bNrChannels == 1 && fmt->bSubframeSize == 2)
850 		return NULL;
851 
852 	fp = audio_format_alloc_init(chip, alts, protocol, iface_no,
853 				     altset_idx, altno, num_channels, clock);
854 	if (!fp)
855 		return ERR_PTR(-ENOMEM);
856 
857 	fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol,
858 						       iface_no);
859 
860 	/* some quirks for attributes here */
861 	snd_usb_audioformat_attributes_quirk(chip, fp, stream);
862 
863 	/* ok, let's parse further... */
864 	if (snd_usb_parse_audio_format(chip, fp, format,
865 					fmt, stream) < 0) {
866 		audioformat_free(fp);
867 		return NULL;
868 	}
869 
870 	/* Create chmap */
871 	if (fp->channels != num_channels)
872 		chconfig = 0;
873 
874 	fp->chmap = convert_chmap(fp->channels, chconfig, protocol);
875 
876 	return fp;
877 }
878 
879 static struct audioformat *
880 snd_usb_get_audioformat_uac3(struct snd_usb_audio *chip,
881 			     struct usb_host_interface *alts,
882 			     struct snd_usb_power_domain **pd_out,
883 			     int iface_no, int altset_idx,
884 			     int altno, int stream)
885 {
886 	struct usb_device *dev = chip->dev;
887 	struct uac3_input_terminal_descriptor *input_term;
888 	struct uac3_output_terminal_descriptor *output_term;
889 	struct uac3_cluster_header_descriptor *cluster;
890 	struct uac3_as_header_descriptor *as = NULL;
891 	struct uac3_hc_descriptor_header hc_header;
892 	struct usb_host_interface *ctrl_intf;
893 	struct snd_pcm_chmap_elem *chmap;
894 	struct snd_usb_power_domain *pd;
895 	unsigned char badd_profile;
896 	u64 badd_formats = 0;
897 	unsigned int num_channels;
898 	struct audioformat *fp;
899 	u16 cluster_id, wLength, cluster_wLength;
900 	int clock = 0;
901 	int err;
902 
903 	badd_profile = chip->badd_profile;
904 	ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
905 
906 	if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
907 		unsigned int maxpacksize =
908 			le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
909 
910 		switch (maxpacksize) {
911 		default:
912 			dev_err(&dev->dev,
913 				"%u:%d : incorrect wMaxPacketSize for BADD profile\n",
914 				iface_no, altno);
915 			return NULL;
916 		case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
917 		case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
918 			badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
919 			num_channels = 1;
920 			break;
921 		case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
922 		case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
923 			badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
924 			num_channels = 1;
925 			break;
926 		case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
927 		case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
928 			badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
929 			num_channels = 2;
930 			break;
931 		case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
932 		case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
933 			badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
934 			num_channels = 2;
935 			break;
936 		}
937 
938 		chmap = kzalloc_obj(*chmap);
939 		if (!chmap)
940 			return ERR_PTR(-ENOMEM);
941 
942 		if (num_channels == 1) {
943 			chmap->map[0] = SNDRV_CHMAP_MONO;
944 		} else {
945 			chmap->map[0] = SNDRV_CHMAP_FL;
946 			chmap->map[1] = SNDRV_CHMAP_FR;
947 		}
948 
949 		chmap->channels = num_channels;
950 		clock = UAC3_BADD_CS_ID9;
951 		goto found_clock;
952 	}
953 
954 	as = snd_usb_find_csint_desc(alts->extra, alts->extralen,
955 				     NULL, UAC_AS_GENERAL);
956 	if (!as) {
957 		dev_err(&dev->dev,
958 			"%u:%d : UAC_AS_GENERAL descriptor not found\n",
959 			iface_no, altno);
960 		return NULL;
961 	}
962 
963 	if (as->bLength < sizeof(*as)) {
964 		dev_err(&dev->dev,
965 			"%u:%d : invalid UAC_AS_GENERAL desc\n",
966 			iface_no, altno);
967 		return NULL;
968 	}
969 
970 	cluster_id = le16_to_cpu(as->wClusterDescrID);
971 	if (!cluster_id) {
972 		dev_err(&dev->dev,
973 			"%u:%d : no cluster descriptor\n",
974 			iface_no, altno);
975 		return NULL;
976 	}
977 
978 	/*
979 	 * Get number of channels and channel map through
980 	 * High Capability Cluster Descriptor
981 	 *
982 	 * First step: get High Capability header and
983 	 * read size of Cluster Descriptor
984 	 */
985 	err = snd_usb_ctl_msg(chip->dev,
986 			usb_rcvctrlpipe(chip->dev, 0),
987 			UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
988 			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
989 			cluster_id,
990 			snd_usb_ctrl_intf(ctrl_intf),
991 			&hc_header, sizeof(hc_header));
992 	if (err < 0)
993 		return ERR_PTR(err);
994 	else if (err != sizeof(hc_header)) {
995 		dev_err(&dev->dev,
996 			"%u:%d : can't get High Capability descriptor\n",
997 			iface_no, altno);
998 		return ERR_PTR(-EIO);
999 	}
1000 
1001 	/*
1002 	 * Second step: allocate needed amount of memory
1003 	 * and request Cluster Descriptor
1004 	 */
1005 	wLength = le16_to_cpu(hc_header.wLength);
1006 	if (wLength < sizeof(cluster))
1007 		return NULL;
1008 	cluster = kzalloc(wLength, GFP_KERNEL);
1009 	if (!cluster)
1010 		return ERR_PTR(-ENOMEM);
1011 	err = snd_usb_ctl_msg(chip->dev,
1012 			usb_rcvctrlpipe(chip->dev, 0),
1013 			UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
1014 			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1015 			cluster_id,
1016 			snd_usb_ctrl_intf(ctrl_intf),
1017 			cluster, wLength);
1018 	if (err < 0) {
1019 		kfree(cluster);
1020 		return ERR_PTR(err);
1021 	} else if (err != wLength) {
1022 		dev_err(&dev->dev,
1023 			"%u:%d : can't get Cluster Descriptor\n",
1024 			iface_no, altno);
1025 		kfree(cluster);
1026 		return ERR_PTR(-EIO);
1027 	}
1028 
1029 	cluster_wLength = le16_to_cpu(cluster->wLength);
1030 	if (cluster_wLength < sizeof(*cluster) ||
1031 	    cluster_wLength > wLength) {
1032 		dev_err(&dev->dev,
1033 			"%u:%d : invalid Cluster Descriptor size\n",
1034 			iface_no, altno);
1035 		kfree(cluster);
1036 		return ERR_PTR(-EIO);
1037 	}
1038 
1039 	num_channels = cluster->bNrChannels;
1040 	chmap = convert_chmap_v3(cluster);
1041 	kfree(cluster);
1042 
1043 	/*
1044 	 * lookup the terminal associated to this interface
1045 	 * to extract the clock
1046 	 */
1047 	input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf,
1048 							    as->bTerminalLink,
1049 							    UAC_VERSION_3);
1050 	if (input_term) {
1051 		clock = input_term->bCSourceID;
1052 		goto found_clock;
1053 	}
1054 
1055 	output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf,
1056 							      as->bTerminalLink,
1057 							      UAC_VERSION_3);
1058 	if (output_term) {
1059 		clock = output_term->bCSourceID;
1060 		goto found_clock;
1061 	}
1062 
1063 	dev_err(&dev->dev, "%u:%d : bogus bTerminalLink %d\n",
1064 			iface_no, altno, as->bTerminalLink);
1065 	kfree(chmap);
1066 	return NULL;
1067 
1068 found_clock:
1069 	fp = audio_format_alloc_init(chip, alts, UAC_VERSION_3, iface_no,
1070 				     altset_idx, altno, num_channels, clock);
1071 	if (!fp) {
1072 		kfree(chmap);
1073 		return ERR_PTR(-ENOMEM);
1074 	}
1075 
1076 	fp->chmap = chmap;
1077 
1078 	if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
1079 		fp->attributes = 0; /* No attributes */
1080 
1081 		fp->fmt_type = UAC_FORMAT_TYPE_I;
1082 		fp->formats = badd_formats;
1083 
1084 		fp->nr_rates = 0;	/* SNDRV_PCM_RATE_CONTINUOUS */
1085 		fp->rate_min = UAC3_BADD_SAMPLING_RATE;
1086 		fp->rate_max = UAC3_BADD_SAMPLING_RATE;
1087 		fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
1088 
1089 		pd = kzalloc_obj(*pd);
1090 		if (!pd) {
1091 			audioformat_free(fp);
1092 			return NULL;
1093 		}
1094 		pd->pd_id = (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
1095 					UAC3_BADD_PD_ID10 : UAC3_BADD_PD_ID11;
1096 		pd->pd_d1d0_rec = UAC3_BADD_PD_RECOVER_D1D0;
1097 		pd->pd_d2d0_rec = UAC3_BADD_PD_RECOVER_D2D0;
1098 		pd->ctrl_iface = ctrl_intf;
1099 
1100 	} else {
1101 		fp->attributes = parse_uac_endpoint_attributes(chip, alts,
1102 							       UAC_VERSION_3,
1103 							       iface_no);
1104 
1105 		pd = snd_usb_find_power_domain(ctrl_intf,
1106 					       as->bTerminalLink);
1107 
1108 		/* ok, let's parse further... */
1109 		if (snd_usb_parse_audio_format_v3(chip, fp, as, stream) < 0) {
1110 			kfree(pd);
1111 			audioformat_free(fp);
1112 			return NULL;
1113 		}
1114 	}
1115 
1116 	if (pd)
1117 		*pd_out = pd;
1118 
1119 	return fp;
1120 }
1121 
1122 static int __snd_usb_parse_audio_interface(struct snd_usb_audio *chip,
1123 					   int iface_no,
1124 					   bool *has_non_pcm, bool non_pcm)
1125 {
1126 	struct usb_device *dev;
1127 	struct usb_interface *iface;
1128 	struct usb_host_interface *alts;
1129 	struct usb_interface_descriptor *altsd;
1130 	int i, altno, err, stream;
1131 	struct audioformat *fp = NULL;
1132 	struct snd_usb_power_domain *pd = NULL;
1133 	bool set_iface_first;
1134 	int num, protocol;
1135 
1136 	dev = chip->dev;
1137 
1138 	/* parse the interface's altsettings */
1139 	iface = usb_ifnum_to_if(dev, iface_no);
1140 
1141 	num = iface->num_altsetting;
1142 
1143 	/*
1144 	 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
1145 	 * one misses syncpipe, and does not produce any sound.
1146 	 */
1147 	if (chip->usb_id == USB_ID(0x04fa, 0x4201) && num >= 4)
1148 		num = 4;
1149 
1150 	for (i = 0; i < num; i++) {
1151 		alts = &iface->altsetting[i];
1152 		altsd = get_iface_desc(alts);
1153 		protocol = altsd->bInterfaceProtocol;
1154 		/* skip invalid one */
1155 		if (((altsd->bInterfaceClass != USB_CLASS_AUDIO ||
1156 		      (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
1157 		       altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC)) &&
1158 		     altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1159 		    altsd->bNumEndpoints < 1 ||
1160 		    le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
1161 			continue;
1162 		/* must be isochronous */
1163 		if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1164 		    USB_ENDPOINT_XFER_ISOC)
1165 			continue;
1166 		/* check direction */
1167 		stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
1168 			SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
1169 		altno = altsd->bAlternateSetting;
1170 
1171 		if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
1172 			continue;
1173 
1174 		/*
1175 		 * Roland audio streaming interfaces are marked with protocols
1176 		 * 0/1/2, but are UAC 1 compatible.
1177 		 */
1178 		if (USB_ID_VENDOR(chip->usb_id) == 0x0582 &&
1179 		    altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
1180 		    protocol <= 2)
1181 			protocol = UAC_VERSION_1;
1182 
1183 		switch (protocol) {
1184 		default:
1185 			dev_dbg(&dev->dev, "%u:%d: unknown interface protocol %#02x, assuming v1\n",
1186 				iface_no, altno, protocol);
1187 			protocol = UAC_VERSION_1;
1188 			fallthrough;
1189 		case UAC_VERSION_1:
1190 		case UAC_VERSION_2: {
1191 			int bm_quirk = 0;
1192 
1193 			/*
1194 			 * Blue Microphones workaround: The last altsetting is
1195 			 * identical with the previous one, except for a larger
1196 			 * packet size, but is actually a mislabeled two-channel
1197 			 * setting; ignore it.
1198 			 *
1199 			 * Part 1: prepare quirk flag
1200 			 */
1201 			if (altno == 2 && num == 3 &&
1202 			    fp && fp->altsetting == 1 && fp->channels == 1 &&
1203 			    fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
1204 			    protocol == UAC_VERSION_1 &&
1205 			    le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
1206 							fp->maxpacksize * 2)
1207 				bm_quirk = 1;
1208 
1209 			fp = snd_usb_get_audioformat_uac12(chip, alts, protocol,
1210 							   iface_no, i, altno,
1211 							   stream, bm_quirk);
1212 			break;
1213 		}
1214 		case UAC_VERSION_3:
1215 			fp = snd_usb_get_audioformat_uac3(chip, alts, &pd,
1216 						iface_no, i, altno, stream);
1217 			break;
1218 		}
1219 
1220 		if (!fp)
1221 			continue;
1222 		else if (IS_ERR(fp))
1223 			return PTR_ERR(fp);
1224 
1225 		if (fp->fmt_type != UAC_FORMAT_TYPE_I)
1226 			*has_non_pcm = true;
1227 		if ((fp->fmt_type == UAC_FORMAT_TYPE_I) == non_pcm) {
1228 			audioformat_free(fp);
1229 			kfree(pd);
1230 			fp = NULL;
1231 			pd = NULL;
1232 			continue;
1233 		}
1234 
1235 		snd_usb_audioformat_set_sync_ep(chip, fp);
1236 
1237 		dev_dbg(&dev->dev, "%u:%d: add audio endpoint %#x\n", iface_no, altno, fp->endpoint);
1238 		if (protocol == UAC_VERSION_3)
1239 			err = snd_usb_add_audio_stream_v3(chip, stream, fp, pd);
1240 		else
1241 			err = snd_usb_add_audio_stream(chip, stream, fp);
1242 
1243 		if (err < 0) {
1244 			audioformat_free(fp);
1245 			kfree(pd);
1246 			return err;
1247 		}
1248 
1249 		/* add endpoints */
1250 		err = snd_usb_add_endpoint(chip, fp->endpoint,
1251 					   SND_USB_ENDPOINT_TYPE_DATA);
1252 		if (err < 0)
1253 			return err;
1254 
1255 		if (fp->sync_ep) {
1256 			err = snd_usb_add_endpoint(chip, fp->sync_ep,
1257 						   fp->implicit_fb ?
1258 						   SND_USB_ENDPOINT_TYPE_DATA :
1259 						   SND_USB_ENDPOINT_TYPE_SYNC);
1260 			if (err < 0)
1261 				return err;
1262 		}
1263 
1264 		set_iface_first = false;
1265 		if (protocol == UAC_VERSION_1 ||
1266 		    (chip->quirk_flags & QUIRK_FLAG_SET_IFACE_FIRST))
1267 			set_iface_first = true;
1268 
1269 		/* try to set the interface... */
1270 		if (chip->quirk_flags & QUIRK_FLAG_SKIP_IFACE_SETUP)
1271 			continue;
1272 
1273 		usb_set_interface(chip->dev, iface_no, 0);
1274 		if (set_iface_first)
1275 			usb_set_interface(chip->dev, iface_no, altno);
1276 		snd_usb_init_pitch(chip, fp);
1277 		snd_usb_init_sample_rate(chip, fp, fp->rate_max);
1278 		if (!set_iface_first)
1279 			usb_set_interface(chip->dev, iface_no, altno);
1280 	}
1281 	return 0;
1282 }
1283 
1284 int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
1285 {
1286 	int err;
1287 	bool has_non_pcm = false;
1288 
1289 	/* parse PCM formats */
1290 	err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, false);
1291 	if (err < 0)
1292 		return err;
1293 
1294 	if (has_non_pcm) {
1295 		/* parse non-PCM formats */
1296 		err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, true);
1297 		if (err < 0)
1298 			return err;
1299 	}
1300 
1301 	return 0;
1302 }
1303 
1304