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