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