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