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