xref: /linux/sound/usb/format.c (revision bbcb2cd6751a9457275728f7004600320703a788)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  */
4 
5 #include <linux/init.h>
6 #include <linux/slab.h>
7 #include <linux/usb.h>
8 #include <linux/usb/audio.h>
9 #include <linux/usb/audio-v2.h>
10 #include <linux/usb/audio-v3.h>
11 
12 #include <sound/core.h>
13 #include <sound/pcm.h>
14 
15 #include "usbaudio.h"
16 #include "card.h"
17 #include "quirks.h"
18 #include "helper.h"
19 #include "clock.h"
20 #include "format.h"
21 
22 /*
23  * parse the audio format type I descriptor
24  * and returns the corresponding pcm format
25  *
26  * @dev: usb device
27  * @fp: audioformat record
28  * @format: the format tag (wFormatTag)
29  * @fmt: the format type descriptor (v1/v2) or AudioStreaming descriptor (v3)
30  */
31 static u64 parse_audio_format_i_type(struct snd_usb_audio *chip,
32 				     struct audioformat *fp,
33 				     u64 format, void *_fmt)
34 {
35 	int sample_width, sample_bytes;
36 	u64 pcm_formats = 0;
37 	u64 dsd_formats = 0;
38 
39 	switch (fp->protocol) {
40 	case UAC_VERSION_1:
41 	default: {
42 		struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
43 		if (format >= 64) {
44 			usb_audio_info(chip,
45 				       "%u:%d: invalid format type 0x%llx is detected, processed as PCM\n",
46 				       fp->iface, fp->altsetting, format);
47 			format = UAC_FORMAT_TYPE_I_PCM;
48 		}
49 		sample_width = fmt->bBitResolution;
50 		sample_bytes = fmt->bSubframeSize;
51 		format = 1ULL << format;
52 		break;
53 	}
54 
55 	case UAC_VERSION_2: {
56 		struct uac_format_type_i_ext_descriptor *fmt = _fmt;
57 		sample_width = fmt->bBitResolution;
58 		sample_bytes = fmt->bSubslotSize;
59 
60 		if (format & UAC2_FORMAT_TYPE_I_RAW_DATA) {
61 			pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
62 			/* flag potentially raw DSD capable altsettings */
63 			fp->dsd_raw = true;
64 			/* clear special format bit to avoid "unsupported format" msg below */
65 			format &= ~UAC2_FORMAT_TYPE_I_RAW_DATA;
66 		}
67 
68 		format <<= 1;
69 		break;
70 	}
71 	case UAC_VERSION_3: {
72 		struct uac3_as_header_descriptor *as = _fmt;
73 
74 		sample_width = as->bBitResolution;
75 		sample_bytes = as->bSubslotSize;
76 
77 		if (format & UAC3_FORMAT_TYPE_I_RAW_DATA) {
78 			pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
79 			/* clear special format bit to avoid "unsupported format" msg below */
80 			format &= ~UAC3_FORMAT_TYPE_I_RAW_DATA;
81 		}
82 
83 		format <<= 1;
84 		break;
85 	}
86 	}
87 
88 	fp->fmt_bits = sample_width;
89 	fp->fmt_sz = sample_bytes;
90 
91 	if ((pcm_formats == 0) &&
92 	    (format == 0 || format == BIT(UAC_FORMAT_TYPE_I_UNDEFINED))) {
93 		/* some devices don't define this correctly... */
94 		usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM\n",
95 			fp->iface, fp->altsetting);
96 		format = BIT(UAC_FORMAT_TYPE_I_PCM);
97 	}
98 	if (format & BIT(UAC_FORMAT_TYPE_I_PCM)) {
99 		if (((chip->usb_id == USB_ID(0x0582, 0x0016)) ||
100 		     /* Edirol SD-90 */
101 		     (chip->usb_id == USB_ID(0x0582, 0x000c))) &&
102 		     /* Roland SC-D70 */
103 		    sample_width == 24 && sample_bytes == 2)
104 			sample_bytes = 3;
105 		else if (sample_width > sample_bytes * 8) {
106 			usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d\n",
107 				 fp->iface, fp->altsetting,
108 				 sample_width, sample_bytes);
109 		}
110 		/* check the format byte size */
111 		switch (sample_bytes) {
112 		case 1:
113 			pcm_formats |= SNDRV_PCM_FMTBIT_S8;
114 			break;
115 		case 2:
116 			if (snd_usb_is_big_endian_format(chip, fp))
117 				pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */
118 			else
119 				pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE;
120 			break;
121 		case 3:
122 			if (snd_usb_is_big_endian_format(chip, fp))
123 				pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */
124 			else
125 				pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE;
126 			break;
127 		case 4:
128 			pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE;
129 			break;
130 		default:
131 			usb_audio_info(chip,
132 				 "%u:%d : unsupported sample bitwidth %d in %d bytes\n",
133 				 fp->iface, fp->altsetting,
134 				 sample_width, sample_bytes);
135 			break;
136 		}
137 	}
138 	if (format & BIT(UAC_FORMAT_TYPE_I_PCM8)) {
139 		/* Dallas DS4201 workaround: it advertises U8 format, but really
140 		   supports S8. */
141 		if (chip->usb_id == USB_ID(0x04fa, 0x4201))
142 			pcm_formats |= SNDRV_PCM_FMTBIT_S8;
143 		else
144 			pcm_formats |= SNDRV_PCM_FMTBIT_U8;
145 	}
146 	if (format & BIT(UAC_FORMAT_TYPE_I_IEEE_FLOAT))
147 		pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
148 	if (format & BIT(UAC_FORMAT_TYPE_I_ALAW))
149 		pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;
150 	if (format & BIT(UAC_FORMAT_TYPE_I_MULAW))
151 		pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;
152 	if (format & ~0x3f) {
153 		usb_audio_info(chip,
154 			 "%u:%d : unsupported format bits %#llx\n",
155 			 fp->iface, fp->altsetting, format);
156 	}
157 
158 	dsd_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes);
159 	if (dsd_formats && !fp->dsd_dop)
160 		pcm_formats = dsd_formats;
161 
162 	return pcm_formats;
163 }
164 
165 static int set_fixed_rate(struct audioformat *fp, int rate, int rate_bits)
166 {
167 	kfree(fp->rate_table);
168 	fp->rate_table = kmalloc(sizeof(int), GFP_KERNEL);
169 	if (!fp->rate_table)
170 		return -ENOMEM;
171 	fp->nr_rates = 1;
172 	fp->rate_min = rate;
173 	fp->rate_max = rate;
174 	fp->rates = rate_bits;
175 	fp->rate_table[0] = rate;
176 	return 0;
177 }
178 
179 /* set up rate_min, rate_max and rates from the rate table */
180 static void set_rate_table_min_max(struct audioformat *fp)
181 {
182 	unsigned int rate;
183 	int i;
184 
185 	fp->rate_min = INT_MAX;
186 	fp->rate_max = 0;
187 	fp->rates = 0;
188 	for (i = 0; i < fp->nr_rates; i++) {
189 		rate = fp->rate_table[i];
190 		fp->rate_min = min(fp->rate_min, rate);
191 		fp->rate_max = max(fp->rate_max, rate);
192 		fp->rates |= snd_pcm_rate_to_rate_bit(rate);
193 	}
194 }
195 
196 /*
197  * parse the format descriptor and stores the possible sample rates
198  * on the audioformat table (audio class v1).
199  *
200  * @dev: usb device
201  * @fp: audioformat record
202  * @fmt: the format descriptor
203  * @offset: the start offset of descriptor pointing the rate type
204  *          (7 for type I and II, 8 for type II)
205  */
206 static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
207 				       unsigned char *fmt, int offset)
208 {
209 	int nr_rates = fmt[offset];
210 
211 	if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
212 		usb_audio_err(chip,
213 			"%u:%d : invalid UAC_FORMAT_TYPE desc\n",
214 			fp->iface, fp->altsetting);
215 		return -EINVAL;
216 	}
217 
218 	if (nr_rates) {
219 		/*
220 		 * build the rate table and bitmap flags
221 		 */
222 		int r, idx;
223 
224 		fp->rate_table = kmalloc_array(nr_rates, sizeof(int),
225 					       GFP_KERNEL);
226 		if (fp->rate_table == NULL)
227 			return -ENOMEM;
228 
229 		fp->nr_rates = 0;
230 		for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
231 			unsigned int rate = combine_triple(&fmt[idx]);
232 			if (!rate)
233 				continue;
234 			/* C-Media CM6501 mislabels its 96 kHz altsetting */
235 			/* Terratec Aureon 7.1 USB C-Media 6206, too */
236 			/* Ozone Z90 USB C-Media, too */
237 			if (rate == 48000 && nr_rates == 1 &&
238 			    (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
239 			     chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
240 			     chip->usb_id == USB_ID(0x0d8c, 0x0078) ||
241 			     chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
242 			    fp->altsetting == 5 && fp->maxpacksize == 392)
243 				rate = 96000;
244 			/* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
245 			if (rate == 16000 &&
246 			    (chip->usb_id == USB_ID(0x041e, 0x4064) ||
247 			     chip->usb_id == USB_ID(0x041e, 0x4068)))
248 				rate = 8000;
249 
250 			fp->rate_table[fp->nr_rates++] = rate;
251 		}
252 		if (!fp->nr_rates) {
253 			usb_audio_info(chip,
254 				       "%u:%d: All rates were zero\n",
255 				       fp->iface, fp->altsetting);
256 			return -EINVAL;
257 		}
258 		set_rate_table_min_max(fp);
259 	} else {
260 		/* continuous rates */
261 		fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
262 		fp->rate_min = combine_triple(&fmt[offset + 1]);
263 		fp->rate_max = combine_triple(&fmt[offset + 4]);
264 	}
265 
266 	/* Jabra Evolve 65 headset */
267 	if (chip->usb_id == USB_ID(0x0b0e, 0x030b) ||
268 	    chip->usb_id == USB_ID(0x0b0e, 0x030c)) {
269 		/* only 48kHz for playback while keeping 16kHz for capture */
270 		if (fp->nr_rates != 1)
271 			return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000);
272 	}
273 
274 	return 0;
275 }
276 
277 
278 /*
279  * Presonus Studio 1810c supports a limited set of sampling
280  * rates per altsetting but reports the full set each time.
281  * If we don't filter out the unsupported rates and attempt
282  * to configure the card, it will hang refusing to do any
283  * further audio I/O until a hard reset is performed.
284  *
285  * The list of supported rates per altsetting (set of available
286  * I/O channels) is described in the owner's manual, section 2.2.
287  */
288 static bool s1810c_valid_sample_rate(struct audioformat *fp,
289 				     unsigned int rate)
290 {
291 	switch (fp->altsetting) {
292 	case 1:
293 		/* All ADAT ports available */
294 		return rate <= 48000;
295 	case 2:
296 		/* Half of ADAT ports available */
297 		return (rate == 88200 || rate == 96000);
298 	case 3:
299 		/* Analog I/O only (no S/PDIF nor ADAT) */
300 		return rate >= 176400;
301 	default:
302 		return false;
303 	}
304 	return false;
305 }
306 
307 /*
308  * Focusrite devices use rate pairs: 44100/48000, 88200/96000, and
309  * 176400/192000. Return true if rate is in the pair for max_rate.
310  */
311 static bool focusrite_rate_pair(unsigned int rate,
312 				unsigned int max_rate)
313 {
314 	switch (max_rate) {
315 	case 48000:  return rate == 44100 || rate == 48000;
316 	case 96000:  return rate == 88200 || rate == 96000;
317 	case 192000: return rate == 176400 || rate == 192000;
318 	default:     return true;
319 	}
320 }
321 
322 /*
323  * Focusrite devices report all supported rates in a single clock
324  * source but only a subset is valid per altsetting.
325  *
326  * Detection uses two descriptor features:
327  *
328  * 1. Format Type descriptor bLength == 10: non-standard extension
329  *    with max sample rate in bytes 6..9.
330  *
331  * 2. bmControls VAL_ALT_SETTINGS readable bit: when set, the device
332  *    only supports the highest rate pair for that altsetting, and when
333  *    clear, all rates up to max_rate are valid.
334  *
335  * For devices without the bLength == 10 extension but with
336  * VAL_ALT_SETTINGS readable and multiple altsettings (only seen in
337  * Scarlett 18i8 3rd Gen playback), fall back to the Focusrite
338  * convention: alt 1 = 48kHz, alt 2 = 96kHz, alt 3 = 192kHz.
339  */
340 static bool focusrite_valid_sample_rate(struct snd_usb_audio *chip,
341 					struct audioformat *fp,
342 					unsigned int rate)
343 {
344 	struct usb_interface *iface;
345 	struct usb_host_interface *alts;
346 	struct uac2_as_header_descriptor *as;
347 	unsigned char *fmt;
348 	unsigned int max_rate;
349 	bool val_alt;
350 
351 	alts = snd_usb_get_host_interface(chip, fp->iface, fp->altsetting);
352 	if (!alts)
353 		return true;
354 
355 	fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
356 				      NULL, UAC_FORMAT_TYPE);
357 	if (!fmt)
358 		return true;
359 
360 	as = snd_usb_find_csint_desc(alts->extra, alts->extralen,
361 				     NULL, UAC_AS_GENERAL);
362 	if (!as)
363 		return true;
364 
365 	val_alt = uac_v2v3_control_is_readable(as->bmControls,
366 					       UAC2_AS_VAL_ALT_SETTINGS);
367 
368 	if (fmt[0] == 10) { /* bLength */
369 		max_rate = combine_quad(&fmt[6]);
370 
371 		if (val_alt)
372 			return focusrite_rate_pair(rate, max_rate);
373 
374 		/* No val_alt: rates fall through from higher */
375 		switch (max_rate) {
376 		case 192000:
377 			if (rate == 176400 || rate == 192000)
378 				return true;
379 			fallthrough;
380 		case 96000:
381 			if (rate == 88200 || rate == 96000)
382 				return true;
383 			fallthrough;
384 		case 48000:
385 			return (rate == 44100 || rate == 48000);
386 		default:
387 			usb_audio_info(chip,
388 				"%u:%d : unexpected max rate: %u\n",
389 				fp->iface, fp->altsetting, max_rate);
390 			return true;
391 		}
392 	}
393 
394 	if (!val_alt)
395 		return true;
396 
397 	/* Multi-altsetting device with val_alt but no max_rate
398 	 * in the format descriptor. Use Focusrite convention:
399 	 * alt 1 = 48kHz, alt 2 = 96kHz, alt 3 = 192kHz.
400 	 */
401 	iface = usb_ifnum_to_if(chip->dev, fp->iface);
402 	if (!iface || iface->num_altsetting <= 2)
403 		return true;
404 
405 	switch (fp->altsetting) {
406 	case 1:		max_rate = 48000; break;
407 	case 2:		max_rate = 96000; break;
408 	case 3:		max_rate = 192000; break;
409 	default:	return true;
410 	}
411 
412 	return focusrite_rate_pair(rate, max_rate);
413 }
414 
415 /*
416  * Helper function to walk the array of sample rate triplets reported by
417  * the device. The problem is that we need to parse whole array first to
418  * get to know how many sample rates we have to expect.
419  * Then fp->rate_table can be allocated and filled.
420  */
421 static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
422 					struct audioformat *fp, int nr_triplets,
423 					const unsigned char *data)
424 {
425 	int i, nr_rates = 0;
426 
427 	for (i = 0; i < nr_triplets; i++) {
428 		int min = combine_quad(&data[2 + 12 * i]);
429 		int max = combine_quad(&data[6 + 12 * i]);
430 		int res = combine_quad(&data[10 + 12 * i]);
431 		unsigned int rate;
432 
433 		if ((max < 0) || (min < 0) || (res < 0) || (max < min))
434 			continue;
435 
436 		/*
437 		 * for ranges with res == 1, we announce a continuous sample
438 		 * rate range, and this function should return 0 for no further
439 		 * parsing.
440 		 */
441 		if (res == 1) {
442 			fp->rate_min = min;
443 			fp->rate_max = max;
444 			fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
445 			return 0;
446 		}
447 
448 		for (rate = min; rate <= max; rate += res) {
449 
450 			/* Filter out invalid rates on Presonus Studio 1810c */
451 			if (chip->usb_id == USB_ID(0x194f, 0x010c) &&
452 			    !s1810c_valid_sample_rate(fp, rate))
453 				goto skip_rate;
454 			/* Filter out invalid rates on Presonus Studio 1824c */
455 			if (chip->usb_id == USB_ID(0x194f, 0x010d) &&
456 			    !s1810c_valid_sample_rate(fp, rate))
457 				goto skip_rate;
458 
459 			/* Filter out invalid rates on Focusrite devices */
460 			if (USB_ID_VENDOR(chip->usb_id) == 0x1235 &&
461 			    !focusrite_valid_sample_rate(chip, fp, rate))
462 				goto skip_rate;
463 
464 			if (fp->rate_table)
465 				fp->rate_table[nr_rates] = rate;
466 			nr_rates++;
467 			if (nr_rates >= MAX_NR_RATES) {
468 				usb_audio_err(chip, "invalid uac2 rates\n");
469 				break;
470 			}
471 
472 skip_rate:
473 			/* avoid endless loop */
474 			if (res == 0)
475 				break;
476 		}
477 	}
478 
479 	return nr_rates;
480 }
481 
482 /* Line6 Helix series and the Rode Rodecaster Pro don't support the
483  * UAC2_CS_RANGE usb function call. Return a static table of known
484  * clock rates.
485  */
486 static int line6_parse_audio_format_rates_quirk(struct snd_usb_audio *chip,
487 						struct audioformat *fp)
488 {
489 	switch (chip->usb_id) {
490 	case USB_ID(0x0e41, 0x4241): /* Line6 Helix */
491 	case USB_ID(0x0e41, 0x4242): /* Line6 Helix Rack */
492 	case USB_ID(0x0e41, 0x4244): /* Line6 Helix LT */
493 	case USB_ID(0x0e41, 0x4246): /* Line6 HX-Stomp */
494 	case USB_ID(0x0e41, 0x4253): /* Line6 HX-Stomp XL */
495 	case USB_ID(0x0e41, 0x4247): /* Line6 Pod Go */
496 	case USB_ID(0x0e41, 0x4248): /* Line6 Helix >= fw 2.82 */
497 	case USB_ID(0x0e41, 0x4249): /* Line6 Helix Rack >= fw 2.82 */
498 	case USB_ID(0x0e41, 0x424a): /* Line6 Helix LT >= fw 2.82 */
499 	case USB_ID(0x0e41, 0x424b): /* Line6 Pod Go */
500 	case USB_ID(0x19f7, 0x0011): /* Rode Rodecaster Pro */
501 		return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000);
502 	}
503 
504 	return -ENODEV;
505 }
506 
507 /* check whether the given altsetting is supported for the already set rate */
508 static bool check_valid_altsetting_v2v3(struct snd_usb_audio *chip, int iface,
509 					int altsetting)
510 {
511 	struct usb_device *dev = chip->dev;
512 	__le64 raw_data = 0;
513 	u64 data;
514 	int err;
515 
516 	/* we assume 64bit is enough for any altsettings */
517 	if (snd_BUG_ON(altsetting >= 64 - 8))
518 		return false;
519 
520 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
521 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
522 			      UAC2_AS_VAL_ALT_SETTINGS << 8,
523 			      iface, &raw_data, sizeof(raw_data));
524 	if (err < 0)
525 		return false;
526 
527 	data = le64_to_cpu(raw_data);
528 	/* first byte contains the bitmap size */
529 	if ((data & 0xff) * 8 < altsetting)
530 		return false;
531 	if (data & (1ULL << (altsetting + 8)))
532 		return true;
533 
534 	return false;
535 }
536 
537 /*
538  * Validate each sample rate with the altsetting
539  * Rebuild the rate table if only partial values are valid
540  */
541 static int validate_sample_rate_table_v2v3(struct snd_usb_audio *chip,
542 					   struct audioformat *fp,
543 					   int clock)
544 {
545 	struct usb_device *dev = chip->dev;
546 	struct usb_host_interface *alts;
547 	unsigned int *table;
548 	unsigned int nr_rates;
549 	int i, err;
550 	u32 bmControls;
551 
552 	/* performing the rate verification may lead to unexpected USB bus
553 	 * behavior afterwards by some unknown reason.  Do this only for the
554 	 * known devices.
555 	 */
556 	if (!(chip->quirk_flags & QUIRK_FLAG_VALIDATE_RATES))
557 		return 0; /* don't perform the validation as default */
558 
559 	alts = snd_usb_get_host_interface(chip, fp->iface, fp->altsetting);
560 	if (!alts)
561 		return 0;
562 
563 	if (fp->protocol == UAC_VERSION_3) {
564 		struct uac3_as_header_descriptor *as = snd_usb_find_csint_desc(
565 				alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
566 		bmControls = le32_to_cpu(as->bmControls);
567 	} else {
568 		struct uac2_as_header_descriptor *as = snd_usb_find_csint_desc(
569 				alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
570 		bmControls = as->bmControls;
571 	}
572 
573 	if (!uac_v2v3_control_is_readable(bmControls,
574 				UAC2_AS_VAL_ALT_SETTINGS))
575 		return 0;
576 
577 	table = kcalloc(fp->nr_rates, sizeof(*table), GFP_KERNEL);
578 	if (!table)
579 		return -ENOMEM;
580 
581 	/* clear the interface altsetting at first */
582 	usb_set_interface(dev, fp->iface, 0);
583 
584 	nr_rates = 0;
585 	for (i = 0; i < fp->nr_rates; i++) {
586 		err = snd_usb_set_sample_rate_v2v3(chip, fp, clock,
587 						   fp->rate_table[i]);
588 		if (err < 0)
589 			continue;
590 
591 		if (check_valid_altsetting_v2v3(chip, fp->iface, fp->altsetting))
592 			table[nr_rates++] = fp->rate_table[i];
593 	}
594 
595 	if (!nr_rates) {
596 		usb_audio_dbg(chip,
597 			      "No valid sample rate available for %d:%d, assuming a firmware bug\n",
598 			      fp->iface, fp->altsetting);
599 		nr_rates = fp->nr_rates; /* continue as is */
600 	}
601 
602 	if (fp->nr_rates == nr_rates) {
603 		kfree(table);
604 		return 0;
605 	}
606 
607 	kfree(fp->rate_table);
608 	fp->rate_table = table;
609 	fp->nr_rates = nr_rates;
610 	return 0;
611 }
612 
613 /*
614  * parse the format descriptor and stores the possible sample rates
615  * on the audioformat table (audio class v2 and v3).
616  */
617 static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,
618 				       struct audioformat *fp)
619 {
620 	struct usb_device *dev = chip->dev;
621 	unsigned char tmp[2], *data;
622 	int nr_triplets, data_size, ret = 0, ret_l6;
623 	int clock = snd_usb_clock_find_source(chip, fp, false);
624 	struct usb_host_interface *ctrl_intf;
625 
626 	ctrl_intf = snd_usb_find_ctrl_interface(chip, fp->iface);
627 	if (clock < 0) {
628 		dev_err(&dev->dev,
629 			"%s(): unable to find clock source (clock %d)\n",
630 				__func__, clock);
631 		goto err;
632 	}
633 
634 	/* get the number of sample rates first by only fetching 2 bytes */
635 	ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
636 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
637 			      UAC2_CS_CONTROL_SAM_FREQ << 8,
638 			      snd_usb_ctrl_intf(ctrl_intf) | (clock << 8),
639 			      tmp, sizeof(tmp));
640 
641 	if (ret < 0) {
642 		/* line6 helix devices don't support UAC2_CS_CONTROL_SAM_FREQ call */
643 		ret_l6 = line6_parse_audio_format_rates_quirk(chip, fp);
644 		if (ret_l6 == -ENODEV) {
645 			/* no line6 device found continue showing the error */
646 			dev_err(&dev->dev,
647 				"%s(): unable to retrieve number of sample rates (clock %d)\n",
648 				__func__, clock);
649 			goto err;
650 		}
651 		if (ret_l6 == 0) {
652 			dev_info(&dev->dev,
653 				"%s(): unable to retrieve number of sample rates: set it to a predefined value (clock %d).\n",
654 				__func__, clock);
655 			return 0;
656 		}
657 		ret = ret_l6;
658 		goto err;
659 	}
660 
661 	nr_triplets = (tmp[1] << 8) | tmp[0];
662 	data_size = 2 + 12 * nr_triplets;
663 	data = kzalloc(data_size, GFP_KERNEL);
664 	if (!data) {
665 		ret = -ENOMEM;
666 		goto err;
667 	}
668 
669 	/* now get the full information */
670 	ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
671 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
672 			      UAC2_CS_CONTROL_SAM_FREQ << 8,
673 			      snd_usb_ctrl_intf(ctrl_intf) | (clock << 8),
674 			      data, data_size);
675 
676 	if (ret < 0) {
677 		dev_err(&dev->dev,
678 			"%s(): unable to retrieve sample rate range (clock %d)\n",
679 				__func__, clock);
680 		ret = -EINVAL;
681 		goto err_free;
682 	}
683 
684 	/* Call the triplet parser, and make sure fp->rate_table is NULL.
685 	 * We just use the return value to know how many sample rates we
686 	 * will have to deal with. */
687 	kfree(fp->rate_table);
688 	fp->rate_table = NULL;
689 	fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
690 
691 	if (fp->nr_rates == 0) {
692 		/* SNDRV_PCM_RATE_CONTINUOUS */
693 		ret = 0;
694 		goto err_free;
695 	}
696 
697 	fp->rate_table = kmalloc_array(fp->nr_rates, sizeof(int), GFP_KERNEL);
698 	if (!fp->rate_table) {
699 		ret = -ENOMEM;
700 		goto err_free;
701 	}
702 
703 	/* Call the triplet parser again, but this time, fp->rate_table is
704 	 * allocated, so the rates will be stored */
705 	parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
706 
707 	ret = validate_sample_rate_table_v2v3(chip, fp, clock);
708 	if (ret < 0)
709 		goto err_free;
710 
711 	set_rate_table_min_max(fp);
712 
713 err_free:
714 	kfree(data);
715 err:
716 	return ret;
717 }
718 
719 /*
720  * parse the format type I and III descriptors
721  */
722 static int parse_audio_format_i(struct snd_usb_audio *chip,
723 				struct audioformat *fp, u64 format,
724 				void *_fmt)
725 {
726 	snd_pcm_format_t pcm_format;
727 	unsigned int fmt_type;
728 	int ret;
729 
730 	switch (fp->protocol) {
731 	default:
732 	case UAC_VERSION_1:
733 	case UAC_VERSION_2: {
734 		struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
735 
736 		fmt_type = fmt->bFormatType;
737 		break;
738 	}
739 	case UAC_VERSION_3: {
740 		/* fp->fmt_type is already set in this case */
741 		fmt_type = fp->fmt_type;
742 		break;
743 	}
744 	}
745 
746 	if (fmt_type == UAC_FORMAT_TYPE_III) {
747 		/* FIXME: the format type is really IECxxx
748 		 *        but we give normal PCM format to get the existing
749 		 *        apps working...
750 		 */
751 		switch (chip->usb_id) {
752 
753 		case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
754 			if (chip->setup == 0x00 &&
755 			    fp->altsetting == 6)
756 				pcm_format = SNDRV_PCM_FORMAT_S16_BE;
757 			else
758 				pcm_format = SNDRV_PCM_FORMAT_S16_LE;
759 			break;
760 		default:
761 			pcm_format = SNDRV_PCM_FORMAT_S16_LE;
762 		}
763 		fp->formats = pcm_format_to_bits(pcm_format);
764 	} else {
765 		fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt);
766 		if (!fp->formats)
767 			return -EINVAL;
768 	}
769 
770 	/* gather possible sample rates */
771 	/* audio class v1 reports possible sample rates as part of the
772 	 * proprietary class specific descriptor.
773 	 * audio class v2 uses class specific EP0 range requests for that.
774 	 */
775 	switch (fp->protocol) {
776 	default:
777 	case UAC_VERSION_1: {
778 		struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
779 
780 		fp->channels = fmt->bNrChannels;
781 		ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
782 		break;
783 	}
784 	case UAC_VERSION_2:
785 	case UAC_VERSION_3: {
786 		/* fp->channels is already set in this case */
787 		ret = parse_audio_format_rates_v2v3(chip, fp);
788 		break;
789 	}
790 	}
791 
792 	if (fp->channels < 1) {
793 		usb_audio_err(chip,
794 			"%u:%d : invalid channels %d\n",
795 			fp->iface, fp->altsetting, fp->channels);
796 		return -EINVAL;
797 	}
798 
799 	return ret;
800 }
801 
802 /*
803  * parse the format type II descriptor
804  */
805 static int parse_audio_format_ii(struct snd_usb_audio *chip,
806 				 struct audioformat *fp,
807 				 u64 format, void *_fmt)
808 {
809 	int brate, framesize, ret;
810 
811 	switch (format) {
812 	case UAC_FORMAT_TYPE_II_AC3:
813 		/* FIXME: there is no AC3 format defined yet */
814 		// fp->formats = SNDRV_PCM_FMTBIT_AC3;
815 		fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
816 		break;
817 	case UAC_FORMAT_TYPE_II_MPEG:
818 		fp->formats = SNDRV_PCM_FMTBIT_MPEG;
819 		break;
820 	default:
821 		usb_audio_info(chip,
822 			 "%u:%d : unknown format tag %#llx is detected.  processed as MPEG.\n",
823 			 fp->iface, fp->altsetting, format);
824 		fp->formats = SNDRV_PCM_FMTBIT_MPEG;
825 		break;
826 	}
827 
828 	fp->channels = 1;
829 
830 	switch (fp->protocol) {
831 	default:
832 	case UAC_VERSION_1: {
833 		struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
834 		brate = le16_to_cpu(fmt->wMaxBitRate);
835 		framesize = le16_to_cpu(fmt->wSamplesPerFrame);
836 		usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
837 		fp->frame_size = framesize;
838 		ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
839 		break;
840 	}
841 	case UAC_VERSION_2: {
842 		struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
843 		brate = le16_to_cpu(fmt->wMaxBitRate);
844 		framesize = le16_to_cpu(fmt->wSamplesPerFrame);
845 		usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
846 		fp->frame_size = framesize;
847 		ret = parse_audio_format_rates_v2v3(chip, fp);
848 		break;
849 	}
850 	}
851 
852 	return ret;
853 }
854 
855 int snd_usb_parse_audio_format(struct snd_usb_audio *chip,
856 			       struct audioformat *fp, u64 format,
857 			       struct uac_format_type_i_continuous_descriptor *fmt,
858 			       int stream)
859 {
860 	int err;
861 
862 	switch (fmt->bFormatType) {
863 	case UAC_FORMAT_TYPE_I:
864 	case UAC_FORMAT_TYPE_III:
865 		err = parse_audio_format_i(chip, fp, format, fmt);
866 		break;
867 	case UAC_FORMAT_TYPE_II:
868 		err = parse_audio_format_ii(chip, fp, format, fmt);
869 		break;
870 	default:
871 		usb_audio_info(chip,
872 			 "%u:%d : format type %d is not supported yet\n",
873 			 fp->iface, fp->altsetting,
874 			 fmt->bFormatType);
875 		return -ENOTSUPP;
876 	}
877 	fp->fmt_type = fmt->bFormatType;
878 	if (err < 0)
879 		return err;
880 #if 1
881 	/* FIXME: temporary hack for extigy/audigy 2 nx/zs */
882 	/* extigy apparently supports sample rates other than 48k
883 	 * but not in ordinary way.  so we enable only 48k atm.
884 	 */
885 	if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
886 	    chip->usb_id == USB_ID(0x041e, 0x3020) ||
887 	    chip->usb_id == USB_ID(0x041e, 0x3061)) {
888 		if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
889 		    fp->rates != SNDRV_PCM_RATE_48000 &&
890 		    fp->rates != SNDRV_PCM_RATE_96000)
891 			return -ENOTSUPP;
892 	}
893 #endif
894 	return 0;
895 }
896 
897 int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip,
898 			       struct audioformat *fp,
899 			       struct uac3_as_header_descriptor *as,
900 			       int stream)
901 {
902 	u64 format = le64_to_cpu(as->bmFormats);
903 	int err;
904 
905 	/*
906 	 * Type I format bits are D0..D6
907 	 * This test works because type IV is not supported
908 	 */
909 	if (format & 0x7f)
910 		fp->fmt_type = UAC_FORMAT_TYPE_I;
911 	else
912 		fp->fmt_type = UAC_FORMAT_TYPE_III;
913 
914 	err = parse_audio_format_i(chip, fp, format, as);
915 	if (err < 0)
916 		return err;
917 
918 	return 0;
919 }
920