• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 		sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
549 	else
550 		strcpy(pcm->name, "USB Audio");
551 
552 	snd_usb_init_substream(as, stream, fp, pd);
553 
554 	/*
555 	 * Keep using head insertion for M-Audio Audiophile USB (tm) which has a
556 	 * fix to swap capture stream order in conf/cards/USB-audio.conf
557 	 */
558 	if (chip->usb_id == USB_ID(0x0763, 0x2003))
559 		list_add(&as->list, &chip->pcm_list);
560 	else
561 		list_add_tail(&as->list, &chip->pcm_list);
562 
563 	chip->pcm_devs++;
564 
565 	snd_usb_proc_pcm_format_add(as);
566 
567 	return add_chmap(pcm, stream, &as->substream[stream]);
568 }
569 
snd_usb_add_audio_stream(struct snd_usb_audio * chip,int stream,struct audioformat * fp)570 int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
571 			     int stream,
572 			     struct audioformat *fp)
573 {
574 	return __snd_usb_add_audio_stream(chip, stream, fp, NULL);
575 }
576 
snd_usb_add_audio_stream_v3(struct snd_usb_audio * chip,int stream,struct audioformat * fp,struct snd_usb_power_domain * pd)577 static int snd_usb_add_audio_stream_v3(struct snd_usb_audio *chip,
578 				       int stream,
579 				       struct audioformat *fp,
580 				       struct snd_usb_power_domain *pd)
581 {
582 	return __snd_usb_add_audio_stream(chip, stream, fp, pd);
583 }
584 
parse_uac_endpoint_attributes(struct snd_usb_audio * chip,struct usb_host_interface * alts,int protocol,int iface_no)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 *
snd_usb_find_input_terminal_descriptor(struct usb_host_interface * ctrl_iface,int terminal_id,int protocol)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 *
snd_usb_find_output_terminal_descriptor(struct usb_host_interface * ctrl_iface,int terminal_id,int protocol)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 *
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)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 audioformat *fp;
693 
694 	fp = kzalloc(sizeof(*fp), GFP_KERNEL);
695 	if (!fp)
696 		return NULL;
697 
698 	fp->iface = iface_no;
699 	fp->altsetting = altno;
700 	fp->altset_idx = altset_idx;
701 	fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
702 	fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
703 	fp->datainterval = snd_usb_parse_datainterval(chip, alts);
704 	fp->protocol = protocol;
705 	fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
706 	fp->channels = num_channels;
707 	if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH)
708 		fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
709 				* (fp->maxpacksize & 0x7ff);
710 	fp->clock = clock;
711 	INIT_LIST_HEAD(&fp->list);
712 
713 	return fp;
714 }
715 
716 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)717 snd_usb_get_audioformat_uac12(struct snd_usb_audio *chip,
718 			      struct usb_host_interface *alts,
719 			      int protocol, int iface_no, int altset_idx,
720 			      int altno, int stream, int bm_quirk)
721 {
722 	struct usb_device *dev = chip->dev;
723 	struct uac_format_type_i_continuous_descriptor *fmt;
724 	unsigned int num_channels = 0, chconfig = 0;
725 	struct usb_host_interface *ctrl_intf;
726 	struct audioformat *fp;
727 	int clock = 0;
728 	u64 format;
729 
730 	ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
731 
732 	/* get audio formats */
733 	if (protocol == UAC_VERSION_1) {
734 		struct uac1_as_header_descriptor *as =
735 			snd_usb_find_csint_desc(alts->extra, alts->extralen,
736 						NULL, UAC_AS_GENERAL);
737 		struct uac_input_terminal_descriptor *iterm;
738 
739 		if (!as) {
740 			dev_err(&dev->dev,
741 				"%u:%d : UAC_AS_GENERAL descriptor not found\n",
742 				iface_no, altno);
743 			return NULL;
744 		}
745 
746 		if (as->bLength < sizeof(*as)) {
747 			dev_err(&dev->dev,
748 				"%u:%d : invalid UAC_AS_GENERAL desc\n",
749 				iface_no, altno);
750 			return NULL;
751 		}
752 
753 		format = le16_to_cpu(as->wFormatTag); /* remember the format value */
754 
755 		iterm = snd_usb_find_input_terminal_descriptor(ctrl_intf,
756 							       as->bTerminalLink,
757 							       protocol);
758 		if (iterm) {
759 			num_channels = iterm->bNrChannels;
760 			chconfig = le16_to_cpu(iterm->wChannelConfig);
761 		}
762 	} else { /* UAC_VERSION_2 */
763 		struct uac2_input_terminal_descriptor *input_term;
764 		struct uac2_output_terminal_descriptor *output_term;
765 		struct uac2_as_header_descriptor *as =
766 			snd_usb_find_csint_desc(alts->extra, alts->extralen,
767 						NULL, UAC_AS_GENERAL);
768 
769 		if (!as) {
770 			dev_err(&dev->dev,
771 				"%u:%d : UAC_AS_GENERAL descriptor not found\n",
772 				iface_no, altno);
773 			return NULL;
774 		}
775 
776 		if (as->bLength < sizeof(*as)) {
777 			dev_err(&dev->dev,
778 				"%u:%d : invalid UAC_AS_GENERAL desc\n",
779 				iface_no, altno);
780 			return NULL;
781 		}
782 
783 		num_channels = as->bNrChannels;
784 		format = le32_to_cpu(as->bmFormats);
785 		chconfig = le32_to_cpu(as->bmChannelConfig);
786 
787 		/*
788 		 * lookup the terminal associated to this interface
789 		 * to extract the clock
790 		 */
791 		input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf,
792 								    as->bTerminalLink,
793 								    protocol);
794 		if (input_term) {
795 			clock = input_term->bCSourceID;
796 			if (!chconfig && (num_channels == input_term->bNrChannels))
797 				chconfig = le32_to_cpu(input_term->bmChannelConfig);
798 			goto found_clock;
799 		}
800 
801 		output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf,
802 								      as->bTerminalLink,
803 								      protocol);
804 		if (output_term) {
805 			clock = output_term->bCSourceID;
806 			goto found_clock;
807 		}
808 
809 		dev_err(&dev->dev,
810 			"%u:%d : bogus bTerminalLink %d\n",
811 			iface_no, altno, as->bTerminalLink);
812 		return NULL;
813 	}
814 
815 found_clock:
816 	/* get format type */
817 	fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
818 				      NULL, UAC_FORMAT_TYPE);
819 	if (!fmt) {
820 		dev_err(&dev->dev,
821 			"%u:%d : no UAC_FORMAT_TYPE desc\n",
822 			iface_no, altno);
823 		return NULL;
824 	}
825 	if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8))
826 			|| ((protocol == UAC_VERSION_2) &&
827 					(fmt->bLength < 6))) {
828 		dev_err(&dev->dev,
829 			"%u:%d : invalid UAC_FORMAT_TYPE desc\n",
830 			iface_no, altno);
831 		return NULL;
832 	}
833 
834 	/*
835 	 * Blue Microphones workaround: The last altsetting is
836 	 * identical with the previous one, except for a larger
837 	 * packet size, but is actually a mislabeled two-channel
838 	 * setting; ignore it.
839 	 *
840 	 * Part 2: analyze quirk flag and format
841 	 */
842 	if (bm_quirk && fmt->bNrChannels == 1 && fmt->bSubframeSize == 2)
843 		return NULL;
844 
845 	fp = audio_format_alloc_init(chip, alts, protocol, iface_no,
846 				     altset_idx, altno, num_channels, clock);
847 	if (!fp)
848 		return ERR_PTR(-ENOMEM);
849 
850 	fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol,
851 						       iface_no);
852 
853 	/* some quirks for attributes here */
854 	snd_usb_audioformat_attributes_quirk(chip, fp, stream);
855 
856 	/* ok, let's parse further... */
857 	if (snd_usb_parse_audio_format(chip, fp, format,
858 					fmt, stream) < 0) {
859 		audioformat_free(fp);
860 		return NULL;
861 	}
862 
863 	/* Create chmap */
864 	if (fp->channels != num_channels)
865 		chconfig = 0;
866 
867 	fp->chmap = convert_chmap(fp->channels, chconfig, protocol);
868 
869 	return fp;
870 }
871 
872 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)873 snd_usb_get_audioformat_uac3(struct snd_usb_audio *chip,
874 			     struct usb_host_interface *alts,
875 			     struct snd_usb_power_domain **pd_out,
876 			     int iface_no, int altset_idx,
877 			     int altno, int stream)
878 {
879 	struct usb_device *dev = chip->dev;
880 	struct uac3_input_terminal_descriptor *input_term;
881 	struct uac3_output_terminal_descriptor *output_term;
882 	struct uac3_cluster_header_descriptor *cluster;
883 	struct uac3_as_header_descriptor *as = NULL;
884 	struct uac3_hc_descriptor_header hc_header;
885 	struct usb_host_interface *ctrl_intf;
886 	struct snd_pcm_chmap_elem *chmap;
887 	struct snd_usb_power_domain *pd;
888 	unsigned char badd_profile;
889 	u64 badd_formats = 0;
890 	unsigned int num_channels;
891 	struct audioformat *fp;
892 	u16 cluster_id, wLength, cluster_wLength;
893 	int clock = 0;
894 	int err;
895 
896 	badd_profile = chip->badd_profile;
897 	ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
898 
899 	if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
900 		unsigned int maxpacksize =
901 			le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
902 
903 		switch (maxpacksize) {
904 		default:
905 			dev_err(&dev->dev,
906 				"%u:%d : incorrect wMaxPacketSize for BADD profile\n",
907 				iface_no, altno);
908 			return NULL;
909 		case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
910 		case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
911 			badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
912 			num_channels = 1;
913 			break;
914 		case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
915 		case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
916 			badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
917 			num_channels = 1;
918 			break;
919 		case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
920 		case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
921 			badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
922 			num_channels = 2;
923 			break;
924 		case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
925 		case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
926 			badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
927 			num_channels = 2;
928 			break;
929 		}
930 
931 		chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
932 		if (!chmap)
933 			return ERR_PTR(-ENOMEM);
934 
935 		if (num_channels == 1) {
936 			chmap->map[0] = SNDRV_CHMAP_MONO;
937 		} else {
938 			chmap->map[0] = SNDRV_CHMAP_FL;
939 			chmap->map[1] = SNDRV_CHMAP_FR;
940 		}
941 
942 		chmap->channels = num_channels;
943 		clock = UAC3_BADD_CS_ID9;
944 		goto found_clock;
945 	}
946 
947 	as = snd_usb_find_csint_desc(alts->extra, alts->extralen,
948 				     NULL, UAC_AS_GENERAL);
949 	if (!as) {
950 		dev_err(&dev->dev,
951 			"%u:%d : UAC_AS_GENERAL descriptor not found\n",
952 			iface_no, altno);
953 		return NULL;
954 	}
955 
956 	if (as->bLength < sizeof(*as)) {
957 		dev_err(&dev->dev,
958 			"%u:%d : invalid UAC_AS_GENERAL desc\n",
959 			iface_no, altno);
960 		return NULL;
961 	}
962 
963 	cluster_id = le16_to_cpu(as->wClusterDescrID);
964 	if (!cluster_id) {
965 		dev_err(&dev->dev,
966 			"%u:%d : no cluster descriptor\n",
967 			iface_no, altno);
968 		return NULL;
969 	}
970 
971 	/*
972 	 * Get number of channels and channel map through
973 	 * High Capability Cluster Descriptor
974 	 *
975 	 * First step: get High Capability header and
976 	 * read size of Cluster Descriptor
977 	 */
978 	err = snd_usb_ctl_msg(chip->dev,
979 			usb_rcvctrlpipe(chip->dev, 0),
980 			UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
981 			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
982 			cluster_id,
983 			snd_usb_ctrl_intf(ctrl_intf),
984 			&hc_header, sizeof(hc_header));
985 	if (err < 0)
986 		return ERR_PTR(err);
987 	else if (err != sizeof(hc_header)) {
988 		dev_err(&dev->dev,
989 			"%u:%d : can't get High Capability descriptor\n",
990 			iface_no, altno);
991 		return ERR_PTR(-EIO);
992 	}
993 
994 	/*
995 	 * Second step: allocate needed amount of memory
996 	 * and request Cluster Descriptor
997 	 */
998 	wLength = le16_to_cpu(hc_header.wLength);
999 	if (wLength < sizeof(cluster))
1000 		return NULL;
1001 	cluster = kzalloc(wLength, GFP_KERNEL);
1002 	if (!cluster)
1003 		return ERR_PTR(-ENOMEM);
1004 	err = snd_usb_ctl_msg(chip->dev,
1005 			usb_rcvctrlpipe(chip->dev, 0),
1006 			UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
1007 			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1008 			cluster_id,
1009 			snd_usb_ctrl_intf(ctrl_intf),
1010 			cluster, wLength);
1011 	if (err < 0) {
1012 		kfree(cluster);
1013 		return ERR_PTR(err);
1014 	} else if (err != wLength) {
1015 		dev_err(&dev->dev,
1016 			"%u:%d : can't get Cluster Descriptor\n",
1017 			iface_no, altno);
1018 		kfree(cluster);
1019 		return ERR_PTR(-EIO);
1020 	}
1021 
1022 	cluster_wLength = le16_to_cpu(cluster->wLength);
1023 	if (cluster_wLength < sizeof(*cluster) ||
1024 	    cluster_wLength > wLength) {
1025 		dev_err(&dev->dev,
1026 			"%u:%d : invalid Cluster Descriptor size\n",
1027 			iface_no, altno);
1028 		kfree(cluster);
1029 		return ERR_PTR(-EIO);
1030 	}
1031 
1032 	num_channels = cluster->bNrChannels;
1033 	chmap = convert_chmap_v3(cluster);
1034 	kfree(cluster);
1035 
1036 	/*
1037 	 * lookup the terminal associated to this interface
1038 	 * to extract the clock
1039 	 */
1040 	input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf,
1041 							    as->bTerminalLink,
1042 							    UAC_VERSION_3);
1043 	if (input_term) {
1044 		clock = input_term->bCSourceID;
1045 		goto found_clock;
1046 	}
1047 
1048 	output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf,
1049 							      as->bTerminalLink,
1050 							      UAC_VERSION_3);
1051 	if (output_term) {
1052 		clock = output_term->bCSourceID;
1053 		goto found_clock;
1054 	}
1055 
1056 	dev_err(&dev->dev, "%u:%d : bogus bTerminalLink %d\n",
1057 			iface_no, altno, as->bTerminalLink);
1058 	kfree(chmap);
1059 	return NULL;
1060 
1061 found_clock:
1062 	fp = audio_format_alloc_init(chip, alts, UAC_VERSION_3, iface_no,
1063 				     altset_idx, altno, num_channels, clock);
1064 	if (!fp) {
1065 		kfree(chmap);
1066 		return ERR_PTR(-ENOMEM);
1067 	}
1068 
1069 	fp->chmap = chmap;
1070 
1071 	if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
1072 		fp->attributes = 0; /* No attributes */
1073 
1074 		fp->fmt_type = UAC_FORMAT_TYPE_I;
1075 		fp->formats = badd_formats;
1076 
1077 		fp->nr_rates = 0;	/* SNDRV_PCM_RATE_CONTINUOUS */
1078 		fp->rate_min = UAC3_BADD_SAMPLING_RATE;
1079 		fp->rate_max = UAC3_BADD_SAMPLING_RATE;
1080 		fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
1081 
1082 		pd = kzalloc(sizeof(*pd), GFP_KERNEL);
1083 		if (!pd) {
1084 			audioformat_free(fp);
1085 			return NULL;
1086 		}
1087 		pd->pd_id = (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
1088 					UAC3_BADD_PD_ID10 : UAC3_BADD_PD_ID11;
1089 		pd->pd_d1d0_rec = UAC3_BADD_PD_RECOVER_D1D0;
1090 		pd->pd_d2d0_rec = UAC3_BADD_PD_RECOVER_D2D0;
1091 		pd->ctrl_iface = ctrl_intf;
1092 
1093 	} else {
1094 		fp->attributes = parse_uac_endpoint_attributes(chip, alts,
1095 							       UAC_VERSION_3,
1096 							       iface_no);
1097 
1098 		pd = snd_usb_find_power_domain(ctrl_intf,
1099 					       as->bTerminalLink);
1100 
1101 		/* ok, let's parse further... */
1102 		if (snd_usb_parse_audio_format_v3(chip, fp, as, stream) < 0) {
1103 			kfree(pd);
1104 			audioformat_free(fp);
1105 			return NULL;
1106 		}
1107 	}
1108 
1109 	if (pd)
1110 		*pd_out = pd;
1111 
1112 	return fp;
1113 }
1114 
__snd_usb_parse_audio_interface(struct snd_usb_audio * chip,int iface_no,bool * has_non_pcm,bool non_pcm)1115 static int __snd_usb_parse_audio_interface(struct snd_usb_audio *chip,
1116 					   int iface_no,
1117 					   bool *has_non_pcm, bool non_pcm)
1118 {
1119 	struct usb_device *dev;
1120 	struct usb_interface *iface;
1121 	struct usb_host_interface *alts;
1122 	struct usb_interface_descriptor *altsd;
1123 	int i, altno, err, stream;
1124 	struct audioformat *fp = NULL;
1125 	struct snd_usb_power_domain *pd = NULL;
1126 	bool set_iface_first;
1127 	int num, protocol;
1128 
1129 	dev = chip->dev;
1130 
1131 	/* parse the interface's altsettings */
1132 	iface = usb_ifnum_to_if(dev, iface_no);
1133 
1134 	num = iface->num_altsetting;
1135 
1136 	/*
1137 	 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
1138 	 * one misses syncpipe, and does not produce any sound.
1139 	 */
1140 	if (chip->usb_id == USB_ID(0x04fa, 0x4201) && num >= 4)
1141 		num = 4;
1142 
1143 	for (i = 0; i < num; i++) {
1144 		alts = &iface->altsetting[i];
1145 		altsd = get_iface_desc(alts);
1146 		protocol = altsd->bInterfaceProtocol;
1147 		/* skip invalid one */
1148 		if (((altsd->bInterfaceClass != USB_CLASS_AUDIO ||
1149 		      (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
1150 		       altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC)) &&
1151 		     altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1152 		    altsd->bNumEndpoints < 1 ||
1153 		    le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
1154 			continue;
1155 		/* must be isochronous */
1156 		if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1157 		    USB_ENDPOINT_XFER_ISOC)
1158 			continue;
1159 		/* check direction */
1160 		stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
1161 			SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
1162 		altno = altsd->bAlternateSetting;
1163 
1164 		if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
1165 			continue;
1166 
1167 		/*
1168 		 * Roland audio streaming interfaces are marked with protocols
1169 		 * 0/1/2, but are UAC 1 compatible.
1170 		 */
1171 		if (USB_ID_VENDOR(chip->usb_id) == 0x0582 &&
1172 		    altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
1173 		    protocol <= 2)
1174 			protocol = UAC_VERSION_1;
1175 
1176 		switch (protocol) {
1177 		default:
1178 			dev_dbg(&dev->dev, "%u:%d: unknown interface protocol %#02x, assuming v1\n",
1179 				iface_no, altno, protocol);
1180 			protocol = UAC_VERSION_1;
1181 			fallthrough;
1182 		case UAC_VERSION_1:
1183 		case UAC_VERSION_2: {
1184 			int bm_quirk = 0;
1185 
1186 			/*
1187 			 * Blue Microphones workaround: The last altsetting is
1188 			 * identical with the previous one, except for a larger
1189 			 * packet size, but is actually a mislabeled two-channel
1190 			 * setting; ignore it.
1191 			 *
1192 			 * Part 1: prepare quirk flag
1193 			 */
1194 			if (altno == 2 && num == 3 &&
1195 			    fp && fp->altsetting == 1 && fp->channels == 1 &&
1196 			    fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
1197 			    protocol == UAC_VERSION_1 &&
1198 			    le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
1199 							fp->maxpacksize * 2)
1200 				bm_quirk = 1;
1201 
1202 			fp = snd_usb_get_audioformat_uac12(chip, alts, protocol,
1203 							   iface_no, i, altno,
1204 							   stream, bm_quirk);
1205 			break;
1206 		}
1207 		case UAC_VERSION_3:
1208 			fp = snd_usb_get_audioformat_uac3(chip, alts, &pd,
1209 						iface_no, i, altno, stream);
1210 			break;
1211 		}
1212 
1213 		if (!fp)
1214 			continue;
1215 		else if (IS_ERR(fp))
1216 			return PTR_ERR(fp);
1217 
1218 		if (fp->fmt_type != UAC_FORMAT_TYPE_I)
1219 			*has_non_pcm = true;
1220 		if ((fp->fmt_type == UAC_FORMAT_TYPE_I) == non_pcm) {
1221 			audioformat_free(fp);
1222 			kfree(pd);
1223 			fp = NULL;
1224 			pd = 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 		if (protocol == UAC_VERSION_3)
1232 			err = snd_usb_add_audio_stream_v3(chip, stream, fp, pd);
1233 		else
1234 			err = snd_usb_add_audio_stream(chip, stream, fp);
1235 
1236 		if (err < 0) {
1237 			audioformat_free(fp);
1238 			kfree(pd);
1239 			return err;
1240 		}
1241 
1242 		/* add endpoints */
1243 		err = snd_usb_add_endpoint(chip, fp->endpoint,
1244 					   SND_USB_ENDPOINT_TYPE_DATA);
1245 		if (err < 0)
1246 			return err;
1247 
1248 		if (fp->sync_ep) {
1249 			err = snd_usb_add_endpoint(chip, fp->sync_ep,
1250 						   fp->implicit_fb ?
1251 						   SND_USB_ENDPOINT_TYPE_DATA :
1252 						   SND_USB_ENDPOINT_TYPE_SYNC);
1253 			if (err < 0)
1254 				return err;
1255 		}
1256 
1257 		set_iface_first = false;
1258 		if (protocol == UAC_VERSION_1 ||
1259 		    (chip->quirk_flags & QUIRK_FLAG_SET_IFACE_FIRST))
1260 			set_iface_first = true;
1261 
1262 		/* try to set the interface... */
1263 		usb_set_interface(chip->dev, iface_no, 0);
1264 		if (set_iface_first)
1265 			usb_set_interface(chip->dev, iface_no, altno);
1266 		snd_usb_init_pitch(chip, fp);
1267 		snd_usb_init_sample_rate(chip, fp, fp->rate_max);
1268 		if (!set_iface_first)
1269 			usb_set_interface(chip->dev, iface_no, altno);
1270 	}
1271 	return 0;
1272 }
1273 
snd_usb_parse_audio_interface(struct snd_usb_audio * chip,int iface_no)1274 int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
1275 {
1276 	int err;
1277 	bool has_non_pcm = false;
1278 
1279 	/* parse PCM formats */
1280 	err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, false);
1281 	if (err < 0)
1282 		return err;
1283 
1284 	if (has_non_pcm) {
1285 		/* parse non-PCM formats */
1286 		err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, true);
1287 		if (err < 0)
1288 			return err;
1289 	}
1290 
1291 	return 0;
1292 }
1293 
1294