xref: /openbmc/linux/sound/usb/format.c (revision 8dc5efe3d17cd572328ac4f1ebde629c83317f54)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e5779998SDaniel Mack /*
3e5779998SDaniel Mack  */
4e5779998SDaniel Mack 
5e5779998SDaniel Mack #include <linux/init.h>
636db0456SStephen Rothwell #include <linux/slab.h>
7e5779998SDaniel Mack #include <linux/usb.h>
8e5779998SDaniel Mack #include <linux/usb/audio.h>
97e847894SDaniel Mack #include <linux/usb/audio-v2.h>
109a2fe9b8SRuslan Bilovol #include <linux/usb/audio-v3.h>
11e5779998SDaniel Mack 
12e5779998SDaniel Mack #include <sound/core.h>
13e5779998SDaniel Mack #include <sound/pcm.h>
14e5779998SDaniel Mack 
15e5779998SDaniel Mack #include "usbaudio.h"
16e5779998SDaniel Mack #include "card.h"
17e5779998SDaniel Mack #include "quirks.h"
18e5779998SDaniel Mack #include "helper.h"
19e5779998SDaniel Mack #include "debug.h"
2079f920fbSDaniel Mack #include "clock.h"
21ee95cb61SDaniel Mack #include "format.h"
22e5779998SDaniel Mack 
23e5779998SDaniel Mack /*
24e5779998SDaniel Mack  * parse the audio format type I descriptor
25e5779998SDaniel Mack  * and returns the corresponding pcm format
26e5779998SDaniel Mack  *
27e5779998SDaniel Mack  * @dev: usb device
28e5779998SDaniel Mack  * @fp: audioformat record
29e5779998SDaniel Mack  * @format: the format tag (wFormatTag)
309a2fe9b8SRuslan Bilovol  * @fmt: the format type descriptor (v1/v2) or AudioStreaming descriptor (v3)
31e5779998SDaniel Mack  */
3229088fefSClemens Ladisch static u64 parse_audio_format_i_type(struct snd_usb_audio *chip,
33e5779998SDaniel Mack 				     struct audioformat *fp,
349a2fe9b8SRuslan Bilovol 				     u64 format, void *_fmt)
35e5779998SDaniel Mack {
36e5779998SDaniel Mack 	int sample_width, sample_bytes;
37717bfb5fSDaniel Mack 	u64 pcm_formats = 0;
38e5779998SDaniel Mack 
398f898e92SClemens Ladisch 	switch (fp->protocol) {
40a2acad82SClemens Ladisch 	case UAC_VERSION_1:
41a2acad82SClemens Ladisch 	default: {
42e5779998SDaniel Mack 		struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
43e5779998SDaniel Mack 		sample_width = fmt->bBitResolution;
44e5779998SDaniel Mack 		sample_bytes = fmt->bSubframeSize;
45b44d419bSDan Carpenter 		format = 1ULL << format;
46e5779998SDaniel Mack 		break;
47e5779998SDaniel Mack 	}
48e5779998SDaniel Mack 
49e5779998SDaniel Mack 	case UAC_VERSION_2: {
50e5779998SDaniel Mack 		struct uac_format_type_i_ext_descriptor *fmt = _fmt;
51e5779998SDaniel Mack 		sample_width = fmt->bBitResolution;
52e5779998SDaniel Mack 		sample_bytes = fmt->bSubslotSize;
53717bfb5fSDaniel Mack 
543a572d94SJussi Laako 		if (format & UAC2_FORMAT_TYPE_I_RAW_DATA) {
55717bfb5fSDaniel Mack 			pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
563a572d94SJussi Laako 			/* flag potentially raw DSD capable altsettings */
573a572d94SJussi Laako 			fp->dsd_raw = true;
583a572d94SJussi Laako 		}
59717bfb5fSDaniel Mack 
6029088fefSClemens Ladisch 		format <<= 1;
61e5779998SDaniel Mack 		break;
62e5779998SDaniel Mack 	}
639a2fe9b8SRuslan Bilovol 	case UAC_VERSION_3: {
649a2fe9b8SRuslan Bilovol 		struct uac3_as_header_descriptor *as = _fmt;
659a2fe9b8SRuslan Bilovol 
669a2fe9b8SRuslan Bilovol 		sample_width = as->bBitResolution;
679a2fe9b8SRuslan Bilovol 		sample_bytes = as->bSubslotSize;
689a2fe9b8SRuslan Bilovol 
699a2fe9b8SRuslan Bilovol 		if (format & UAC3_FORMAT_TYPE_I_RAW_DATA)
709a2fe9b8SRuslan Bilovol 			pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
719a2fe9b8SRuslan Bilovol 
729a2fe9b8SRuslan Bilovol 		format <<= 1;
739a2fe9b8SRuslan Bilovol 		break;
749a2fe9b8SRuslan Bilovol 	}
75e5779998SDaniel Mack 	}
76e5779998SDaniel Mack 
77c7a13264SJussi Laako 	fp->fmt_bits = sample_width;
78c7a13264SJussi Laako 
79717bfb5fSDaniel Mack 	if ((pcm_formats == 0) &&
80717bfb5fSDaniel Mack 	    (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED))) {
8129088fefSClemens Ladisch 		/* some devices don't define this correctly... */
820ba41d91STakashi Iwai 		usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM\n",
830ba41d91STakashi Iwai 			fp->iface, fp->altsetting);
8429088fefSClemens Ladisch 		format = 1 << UAC_FORMAT_TYPE_I_PCM;
8529088fefSClemens Ladisch 	}
8629088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) {
876d1f2f60STakamichi Horikawa 		if (((chip->usb_id == USB_ID(0x0582, 0x0016)) ||
886d1f2f60STakamichi Horikawa 		     /* Edirol SD-90 */
896d1f2f60STakamichi Horikawa 		     (chip->usb_id == USB_ID(0x0582, 0x000c))) &&
906d1f2f60STakamichi Horikawa 		     /* Roland SC-D70 */
91061b869eSClemens Ladisch 		    sample_width == 24 && sample_bytes == 2)
92061b869eSClemens Ladisch 			sample_bytes = 3;
93061b869eSClemens Ladisch 		else if (sample_width > sample_bytes * 8) {
940ba41d91STakashi Iwai 			usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d\n",
950ba41d91STakashi Iwai 				 fp->iface, fp->altsetting,
96e5779998SDaniel Mack 				 sample_width, sample_bytes);
97e5779998SDaniel Mack 		}
98e5779998SDaniel Mack 		/* check the format byte size */
99e5779998SDaniel Mack 		switch (sample_bytes) {
100e5779998SDaniel Mack 		case 1:
10129088fefSClemens Ladisch 			pcm_formats |= SNDRV_PCM_FMTBIT_S8;
102e5779998SDaniel Mack 			break;
103e5779998SDaniel Mack 		case 2:
104e5779998SDaniel Mack 			if (snd_usb_is_big_endian_format(chip, fp))
10529088fefSClemens Ladisch 				pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */
106e5779998SDaniel Mack 			else
10729088fefSClemens Ladisch 				pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE;
108e5779998SDaniel Mack 			break;
109e5779998SDaniel Mack 		case 3:
110e5779998SDaniel Mack 			if (snd_usb_is_big_endian_format(chip, fp))
11129088fefSClemens Ladisch 				pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */
112e5779998SDaniel Mack 			else
11329088fefSClemens Ladisch 				pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE;
114e5779998SDaniel Mack 			break;
115e5779998SDaniel Mack 		case 4:
11629088fefSClemens Ladisch 			pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE;
117e5779998SDaniel Mack 			break;
118e5779998SDaniel Mack 		default:
1190ba41d91STakashi Iwai 			usb_audio_info(chip,
1200ba41d91STakashi Iwai 				 "%u:%d : unsupported sample bitwidth %d in %d bytes\n",
1210ba41d91STakashi Iwai 				 fp->iface, fp->altsetting,
122e5779998SDaniel Mack 				 sample_width, sample_bytes);
123e5779998SDaniel Mack 			break;
124e5779998SDaniel Mack 		}
12529088fefSClemens Ladisch 	}
12629088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) {
127e5779998SDaniel Mack 		/* Dallas DS4201 workaround: it advertises U8 format, but really
128e5779998SDaniel Mack 		   supports S8. */
129e5779998SDaniel Mack 		if (chip->usb_id == USB_ID(0x04fa, 0x4201))
13029088fefSClemens Ladisch 			pcm_formats |= SNDRV_PCM_FMTBIT_S8;
13129088fefSClemens Ladisch 		else
13229088fefSClemens Ladisch 			pcm_formats |= SNDRV_PCM_FMTBIT_U8;
133e5779998SDaniel Mack 	}
13429088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) {
13529088fefSClemens Ladisch 		pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
13629088fefSClemens Ladisch 	}
13729088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) {
13829088fefSClemens Ladisch 		pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;
13929088fefSClemens Ladisch 	}
14029088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) {
14129088fefSClemens Ladisch 		pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;
14229088fefSClemens Ladisch 	}
14329088fefSClemens Ladisch 	if (format & ~0x3f) {
1440ba41d91STakashi Iwai 		usb_audio_info(chip,
1459a2fe9b8SRuslan Bilovol 			 "%u:%d : unsupported format bits %#llx\n",
1460ba41d91STakashi Iwai 			 fp->iface, fp->altsetting, format);
14729088fefSClemens Ladisch 	}
148126825e7SDaniel Mack 
149126825e7SDaniel Mack 	pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes);
150126825e7SDaniel Mack 
15129088fefSClemens Ladisch 	return pcm_formats;
152e5779998SDaniel Mack }
153e5779998SDaniel Mack 
154e5779998SDaniel Mack 
155e5779998SDaniel Mack /*
156e5779998SDaniel Mack  * parse the format descriptor and stores the possible sample rates
157e5779998SDaniel Mack  * on the audioformat table (audio class v1).
158e5779998SDaniel Mack  *
159e5779998SDaniel Mack  * @dev: usb device
160e5779998SDaniel Mack  * @fp: audioformat record
161e5779998SDaniel Mack  * @fmt: the format descriptor
162e5779998SDaniel Mack  * @offset: the start offset of descriptor pointing the rate type
163e5779998SDaniel Mack  *          (7 for type I and II, 8 for type II)
164e5779998SDaniel Mack  */
165e5779998SDaniel Mack static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
166e5779998SDaniel Mack 				       unsigned char *fmt, int offset)
167e5779998SDaniel Mack {
168e5779998SDaniel Mack 	int nr_rates = fmt[offset];
169e5779998SDaniel Mack 
170e5779998SDaniel Mack 	if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
1710ba41d91STakashi Iwai 		usb_audio_err(chip,
1720ba41d91STakashi Iwai 			"%u:%d : invalid UAC_FORMAT_TYPE desc\n",
1730ba41d91STakashi Iwai 			fp->iface, fp->altsetting);
1748ad10dc6SSachin Kamat 		return -EINVAL;
175e5779998SDaniel Mack 	}
176e5779998SDaniel Mack 
177e5779998SDaniel Mack 	if (nr_rates) {
178e5779998SDaniel Mack 		/*
179e5779998SDaniel Mack 		 * build the rate table and bitmap flags
180e5779998SDaniel Mack 		 */
181e5779998SDaniel Mack 		int r, idx;
182e5779998SDaniel Mack 
1836da2ec56SKees Cook 		fp->rate_table = kmalloc_array(nr_rates, sizeof(int),
1846da2ec56SKees Cook 					       GFP_KERNEL);
1851bc00f32SShawn Lin 		if (fp->rate_table == NULL)
1868ad10dc6SSachin Kamat 			return -ENOMEM;
187e5779998SDaniel Mack 
188e5779998SDaniel Mack 		fp->nr_rates = 0;
189e5779998SDaniel Mack 		fp->rate_min = fp->rate_max = 0;
190e5779998SDaniel Mack 		for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
191e5779998SDaniel Mack 			unsigned int rate = combine_triple(&fmt[idx]);
192e5779998SDaniel Mack 			if (!rate)
193e5779998SDaniel Mack 				continue;
194e5779998SDaniel Mack 			/* C-Media CM6501 mislabels its 96 kHz altsetting */
1958129e79eSWolfgang Breyha 			/* Terratec Aureon 7.1 USB C-Media 6206, too */
196e5779998SDaniel Mack 			if (rate == 48000 && nr_rates == 1 &&
197e5779998SDaniel Mack 			    (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
1988129e79eSWolfgang Breyha 			     chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
1998129e79eSWolfgang Breyha 			     chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
200e5779998SDaniel Mack 			    fp->altsetting == 5 && fp->maxpacksize == 392)
201e5779998SDaniel Mack 				rate = 96000;
2028c4b79cfSPavel Hofman 			/* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
2038c4b79cfSPavel Hofman 			if (rate == 16000 &&
2048c4b79cfSPavel Hofman 			    (chip->usb_id == USB_ID(0x041e, 0x4064) ||
2058c4b79cfSPavel Hofman 			     chip->usb_id == USB_ID(0x041e, 0x4068)))
206e5779998SDaniel Mack 				rate = 8000;
207e5779998SDaniel Mack 
208e5779998SDaniel Mack 			fp->rate_table[fp->nr_rates] = rate;
209e5779998SDaniel Mack 			if (!fp->rate_min || rate < fp->rate_min)
210e5779998SDaniel Mack 				fp->rate_min = rate;
211e5779998SDaniel Mack 			if (!fp->rate_max || rate > fp->rate_max)
212e5779998SDaniel Mack 				fp->rate_max = rate;
213e5779998SDaniel Mack 			fp->rates |= snd_pcm_rate_to_rate_bit(rate);
214e5779998SDaniel Mack 			fp->nr_rates++;
215e5779998SDaniel Mack 		}
216e5779998SDaniel Mack 		if (!fp->nr_rates) {
217e5779998SDaniel Mack 			hwc_debug("All rates were zero. Skipping format!\n");
2188ad10dc6SSachin Kamat 			return -EINVAL;
219e5779998SDaniel Mack 		}
220e5779998SDaniel Mack 	} else {
221e5779998SDaniel Mack 		/* continuous rates */
222e5779998SDaniel Mack 		fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
223e5779998SDaniel Mack 		fp->rate_min = combine_triple(&fmt[offset + 1]);
224e5779998SDaniel Mack 		fp->rate_max = combine_triple(&fmt[offset + 4]);
225e5779998SDaniel Mack 	}
226e5779998SDaniel Mack 	return 0;
227e5779998SDaniel Mack }
228e5779998SDaniel Mack 
229*8dc5efe3SNick Kossifidis 
230*8dc5efe3SNick Kossifidis /*
231*8dc5efe3SNick Kossifidis  * Presonus Studio 1810c supports a limited set of sampling
232*8dc5efe3SNick Kossifidis  * rates per altsetting but reports the full set each time.
233*8dc5efe3SNick Kossifidis  * If we don't filter out the unsupported rates and attempt
234*8dc5efe3SNick Kossifidis  * to configure the card, it will hang refusing to do any
235*8dc5efe3SNick Kossifidis  * further audio I/O until a hard reset is performed.
236*8dc5efe3SNick Kossifidis  *
237*8dc5efe3SNick Kossifidis  * The list of supported rates per altsetting (set of available
238*8dc5efe3SNick Kossifidis  * I/O channels) is described in the owner's manual, section 2.2.
239*8dc5efe3SNick Kossifidis  */
240*8dc5efe3SNick Kossifidis static bool s1810c_valid_sample_rate(struct audioformat *fp,
241*8dc5efe3SNick Kossifidis 				     unsigned int rate)
242*8dc5efe3SNick Kossifidis {
243*8dc5efe3SNick Kossifidis 	switch (fp->altsetting) {
244*8dc5efe3SNick Kossifidis 	case 1:
245*8dc5efe3SNick Kossifidis 		/* All ADAT ports available */
246*8dc5efe3SNick Kossifidis 		return rate <= 48000;
247*8dc5efe3SNick Kossifidis 	case 2:
248*8dc5efe3SNick Kossifidis 		/* Half of ADAT ports available */
249*8dc5efe3SNick Kossifidis 		return (rate == 88200 || rate == 96000);
250*8dc5efe3SNick Kossifidis 	case 3:
251*8dc5efe3SNick Kossifidis 		/* Analog I/O only (no S/PDIF nor ADAT) */
252*8dc5efe3SNick Kossifidis 		return rate >= 176400;
253*8dc5efe3SNick Kossifidis 	default:
254*8dc5efe3SNick Kossifidis 		return false;
255*8dc5efe3SNick Kossifidis 	}
256*8dc5efe3SNick Kossifidis 	return false;
257*8dc5efe3SNick Kossifidis }
258*8dc5efe3SNick Kossifidis 
259e5779998SDaniel Mack /*
26067c10366SDaniel Mack  * Helper function to walk the array of sample rate triplets reported by
26167c10366SDaniel Mack  * the device. The problem is that we need to parse whole array first to
26267c10366SDaniel Mack  * get to know how many sample rates we have to expect.
26367c10366SDaniel Mack  * Then fp->rate_table can be allocated and filled.
26467c10366SDaniel Mack  */
2650ba41d91STakashi Iwai static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
2660ba41d91STakashi Iwai 					struct audioformat *fp, int nr_triplets,
26767c10366SDaniel Mack 					const unsigned char *data)
26867c10366SDaniel Mack {
26967c10366SDaniel Mack 	int i, nr_rates = 0;
27067c10366SDaniel Mack 
27167c10366SDaniel Mack 	fp->rates = fp->rate_min = fp->rate_max = 0;
27267c10366SDaniel Mack 
27367c10366SDaniel Mack 	for (i = 0; i < nr_triplets; i++) {
27467c10366SDaniel Mack 		int min = combine_quad(&data[2 + 12 * i]);
27567c10366SDaniel Mack 		int max = combine_quad(&data[6 + 12 * i]);
27667c10366SDaniel Mack 		int res = combine_quad(&data[10 + 12 * i]);
2774fa0e81bSXi Wang 		unsigned int rate;
27867c10366SDaniel Mack 
27967c10366SDaniel Mack 		if ((max < 0) || (min < 0) || (res < 0) || (max < min))
28067c10366SDaniel Mack 			continue;
28167c10366SDaniel Mack 
28267c10366SDaniel Mack 		/*
28367c10366SDaniel Mack 		 * for ranges with res == 1, we announce a continuous sample
28467c10366SDaniel Mack 		 * rate range, and this function should return 0 for no further
28567c10366SDaniel Mack 		 * parsing.
28667c10366SDaniel Mack 		 */
28767c10366SDaniel Mack 		if (res == 1) {
28867c10366SDaniel Mack 			fp->rate_min = min;
28967c10366SDaniel Mack 			fp->rate_max = max;
29067c10366SDaniel Mack 			fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
29167c10366SDaniel Mack 			return 0;
29267c10366SDaniel Mack 		}
29367c10366SDaniel Mack 
29467c10366SDaniel Mack 		for (rate = min; rate <= max; rate += res) {
295*8dc5efe3SNick Kossifidis 
296*8dc5efe3SNick Kossifidis 			/* Filter out invalid rates on Presonus Studio 1810c */
297*8dc5efe3SNick Kossifidis 			if (chip->usb_id == USB_ID(0x0194f, 0x010c) &&
298*8dc5efe3SNick Kossifidis 			    !s1810c_valid_sample_rate(fp, rate))
299*8dc5efe3SNick Kossifidis 				goto skip_rate;
300*8dc5efe3SNick Kossifidis 
30167c10366SDaniel Mack 			if (fp->rate_table)
30267c10366SDaniel Mack 				fp->rate_table[nr_rates] = rate;
30367c10366SDaniel Mack 			if (!fp->rate_min || rate < fp->rate_min)
30467c10366SDaniel Mack 				fp->rate_min = rate;
30567c10366SDaniel Mack 			if (!fp->rate_max || rate > fp->rate_max)
30667c10366SDaniel Mack 				fp->rate_max = rate;
30767c10366SDaniel Mack 			fp->rates |= snd_pcm_rate_to_rate_bit(rate);
30867c10366SDaniel Mack 
30967c10366SDaniel Mack 			nr_rates++;
3108866f405SXi Wang 			if (nr_rates >= MAX_NR_RATES) {
3110ba41d91STakashi Iwai 				usb_audio_err(chip, "invalid uac2 rates\n");
3124fa0e81bSXi Wang 				break;
3134fa0e81bSXi Wang 			}
31467c10366SDaniel Mack 
315*8dc5efe3SNick Kossifidis skip_rate:
31667c10366SDaniel Mack 			/* avoid endless loop */
31767c10366SDaniel Mack 			if (res == 0)
31867c10366SDaniel Mack 				break;
31967c10366SDaniel Mack 		}
32067c10366SDaniel Mack 	}
32167c10366SDaniel Mack 
32267c10366SDaniel Mack 	return nr_rates;
32367c10366SDaniel Mack }
32467c10366SDaniel Mack 
325d4bd3053SNicola Lunghi /* Line6 Helix series don't support the UAC2_CS_RANGE usb function
326d4bd3053SNicola Lunghi  * call. Return a static table of known clock rates.
327d4bd3053SNicola Lunghi  */
328d4bd3053SNicola Lunghi static int line6_parse_audio_format_rates_quirk(struct snd_usb_audio *chip,
329d4bd3053SNicola Lunghi 						struct audioformat *fp)
330d4bd3053SNicola Lunghi {
331d4bd3053SNicola Lunghi 	switch (chip->usb_id) {
3328be03a71STakashi Iwai 	case USB_ID(0x0e41, 0x4241): /* Line6 Helix */
3338be03a71STakashi Iwai 	case USB_ID(0x0e41, 0x4242): /* Line6 Helix Rack */
3348be03a71STakashi Iwai 	case USB_ID(0x0e41, 0x4244): /* Line6 Helix LT */
3358be03a71STakashi Iwai 	case USB_ID(0x0e41, 0x4246): /* Line6 HX-Stomp */
3369b132f27STakashi Iwai 	case USB_ID(0x0e41, 0x4248): /* Line6 Helix >= fw 2.82 */
3379b132f27STakashi Iwai 	case USB_ID(0x0e41, 0x4249): /* Line6 Helix Rack >= fw 2.82 */
3389b132f27STakashi Iwai 	case USB_ID(0x0e41, 0x424a): /* Line6 Helix LT >= fw 2.82 */
339d4bd3053SNicola Lunghi 		/* supported rates: 48Khz */
340d4bd3053SNicola Lunghi 		kfree(fp->rate_table);
341d4bd3053SNicola Lunghi 		fp->rate_table = kmalloc(sizeof(int), GFP_KERNEL);
342d4bd3053SNicola Lunghi 		if (!fp->rate_table)
343d4bd3053SNicola Lunghi 			return -ENOMEM;
344d4bd3053SNicola Lunghi 		fp->nr_rates = 1;
345d4bd3053SNicola Lunghi 		fp->rate_min = 48000;
346d4bd3053SNicola Lunghi 		fp->rate_max = 48000;
347d4bd3053SNicola Lunghi 		fp->rates = SNDRV_PCM_RATE_48000;
348d4bd3053SNicola Lunghi 		fp->rate_table[0] = 48000;
349d4bd3053SNicola Lunghi 		return 0;
350d4bd3053SNicola Lunghi 	}
351d4bd3053SNicola Lunghi 
352d4bd3053SNicola Lunghi 	return -ENODEV;
353d4bd3053SNicola Lunghi }
354d4bd3053SNicola Lunghi 
35567c10366SDaniel Mack /*
356e5779998SDaniel Mack  * parse the format descriptor and stores the possible sample rates
3579a2fe9b8SRuslan Bilovol  * on the audioformat table (audio class v2 and v3).
358e5779998SDaniel Mack  */
3599a2fe9b8SRuslan Bilovol static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,
3603d8d4dcfSDaniel Mack 				       struct audioformat *fp)
361e5779998SDaniel Mack {
362e5779998SDaniel Mack 	struct usb_device *dev = chip->dev;
363e5779998SDaniel Mack 	unsigned char tmp[2], *data;
364d4bd3053SNicola Lunghi 	int nr_triplets, data_size, ret = 0, ret_l6;
3659a2fe9b8SRuslan Bilovol 	int clock = snd_usb_clock_find_source(chip, fp->protocol,
3669a2fe9b8SRuslan Bilovol 					      fp->clock, false);
367e5779998SDaniel Mack 
368d07140baSDaniel Mack 	if (clock < 0) {
3690ba41d91STakashi Iwai 		dev_err(&dev->dev,
3700ba41d91STakashi Iwai 			"%s(): unable to find clock source (clock %d)\n",
371d07140baSDaniel Mack 				__func__, clock);
372d07140baSDaniel Mack 		goto err;
373d07140baSDaniel Mack 	}
374d07140baSDaniel Mack 
375e5779998SDaniel Mack 	/* get the number of sample rates first by only fetching 2 bytes */
376e5779998SDaniel Mack 	ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
377e5779998SDaniel Mack 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
37811bcbc44SDaniel Mack 			      UAC2_CS_CONTROL_SAM_FREQ << 8,
37911bcbc44SDaniel Mack 			      snd_usb_ctrl_intf(chip) | (clock << 8),
38017d900c4SClemens Ladisch 			      tmp, sizeof(tmp));
381e5779998SDaniel Mack 
382e5779998SDaniel Mack 	if (ret < 0) {
383d4bd3053SNicola Lunghi 		/* line6 helix devices don't support UAC2_CS_CONTROL_SAM_FREQ call */
384d4bd3053SNicola Lunghi 		ret_l6 = line6_parse_audio_format_rates_quirk(chip, fp);
385d4bd3053SNicola Lunghi 		if (ret_l6 == -ENODEV) {
386d4bd3053SNicola Lunghi 			/* no line6 device found continue showing the error */
3870ba41d91STakashi Iwai 			dev_err(&dev->dev,
3880ba41d91STakashi Iwai 				"%s(): unable to retrieve number of sample rates (clock %d)\n",
38979f920fbSDaniel Mack 				__func__, clock);
390e5779998SDaniel Mack 			goto err;
391e5779998SDaniel Mack 		}
392d4bd3053SNicola Lunghi 		if (ret_l6 == 0) {
393d4bd3053SNicola Lunghi 			dev_info(&dev->dev,
394d4bd3053SNicola Lunghi 				"%s(): unable to retrieve number of sample rates: set it to a predefined value (clock %d).\n",
395d4bd3053SNicola Lunghi 				__func__, clock);
396d4bd3053SNicola Lunghi 			return 0;
397d4bd3053SNicola Lunghi 		}
398d4bd3053SNicola Lunghi 		ret = ret_l6;
399d4bd3053SNicola Lunghi 		goto err;
400d4bd3053SNicola Lunghi 	}
401e5779998SDaniel Mack 
40267c10366SDaniel Mack 	nr_triplets = (tmp[1] << 8) | tmp[0];
40367c10366SDaniel Mack 	data_size = 2 + 12 * nr_triplets;
404e5779998SDaniel Mack 	data = kzalloc(data_size, GFP_KERNEL);
405e5779998SDaniel Mack 	if (!data) {
406e5779998SDaniel Mack 		ret = -ENOMEM;
407e5779998SDaniel Mack 		goto err;
408e5779998SDaniel Mack 	}
409e5779998SDaniel Mack 
410e5779998SDaniel Mack 	/* now get the full information */
411e5779998SDaniel Mack 	ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
412e5779998SDaniel Mack 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
41311bcbc44SDaniel Mack 			      UAC2_CS_CONTROL_SAM_FREQ << 8,
41411bcbc44SDaniel Mack 			      snd_usb_ctrl_intf(chip) | (clock << 8),
41517d900c4SClemens Ladisch 			      data, data_size);
416e5779998SDaniel Mack 
417e5779998SDaniel Mack 	if (ret < 0) {
4180ba41d91STakashi Iwai 		dev_err(&dev->dev,
4190ba41d91STakashi Iwai 			"%s(): unable to retrieve sample rate range (clock %d)\n",
42079f920fbSDaniel Mack 				__func__, clock);
421e5779998SDaniel Mack 		ret = -EINVAL;
422e5779998SDaniel Mack 		goto err_free;
423e5779998SDaniel Mack 	}
424e5779998SDaniel Mack 
42567c10366SDaniel Mack 	/* Call the triplet parser, and make sure fp->rate_table is NULL.
42667c10366SDaniel Mack 	 * We just use the return value to know how many sample rates we
42767c10366SDaniel Mack 	 * will have to deal with. */
42867c10366SDaniel Mack 	kfree(fp->rate_table);
42967c10366SDaniel Mack 	fp->rate_table = NULL;
4300ba41d91STakashi Iwai 	fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
43167c10366SDaniel Mack 
43267c10366SDaniel Mack 	if (fp->nr_rates == 0) {
43367c10366SDaniel Mack 		/* SNDRV_PCM_RATE_CONTINUOUS */
43467c10366SDaniel Mack 		ret = 0;
43567c10366SDaniel Mack 		goto err_free;
43667c10366SDaniel Mack 	}
43767c10366SDaniel Mack 
4386da2ec56SKees Cook 	fp->rate_table = kmalloc_array(fp->nr_rates, sizeof(int), GFP_KERNEL);
439e5779998SDaniel Mack 	if (!fp->rate_table) {
440e5779998SDaniel Mack 		ret = -ENOMEM;
441e5779998SDaniel Mack 		goto err_free;
442e5779998SDaniel Mack 	}
443e5779998SDaniel Mack 
44467c10366SDaniel Mack 	/* Call the triplet parser again, but this time, fp->rate_table is
44567c10366SDaniel Mack 	 * allocated, so the rates will be stored */
4460ba41d91STakashi Iwai 	parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
447e5779998SDaniel Mack 
448e5779998SDaniel Mack err_free:
449e5779998SDaniel Mack 	kfree(data);
450e5779998SDaniel Mack err:
451e5779998SDaniel Mack 	return ret;
452e5779998SDaniel Mack }
453e5779998SDaniel Mack 
454e5779998SDaniel Mack /*
455e5779998SDaniel Mack  * parse the format type I and III descriptors
456e5779998SDaniel Mack  */
457e5779998SDaniel Mack static int parse_audio_format_i(struct snd_usb_audio *chip,
4589a2fe9b8SRuslan Bilovol 				struct audioformat *fp, u64 format,
4599a2fe9b8SRuslan Bilovol 				void *_fmt)
460e5779998SDaniel Mack {
46174c34ca1SEldad Zack 	snd_pcm_format_t pcm_format;
4629a2fe9b8SRuslan Bilovol 	unsigned int fmt_type;
46374c34ca1SEldad Zack 	int ret;
464e5779998SDaniel Mack 
4659a2fe9b8SRuslan Bilovol 	switch (fp->protocol) {
4669a2fe9b8SRuslan Bilovol 	default:
4679a2fe9b8SRuslan Bilovol 	case UAC_VERSION_1:
4689a2fe9b8SRuslan Bilovol 	case UAC_VERSION_2: {
4699a2fe9b8SRuslan Bilovol 		struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
4709a2fe9b8SRuslan Bilovol 
4719a2fe9b8SRuslan Bilovol 		fmt_type = fmt->bFormatType;
4729a2fe9b8SRuslan Bilovol 		break;
4739a2fe9b8SRuslan Bilovol 	}
4749a2fe9b8SRuslan Bilovol 	case UAC_VERSION_3: {
4759a2fe9b8SRuslan Bilovol 		/* fp->fmt_type is already set in this case */
4769a2fe9b8SRuslan Bilovol 		fmt_type = fp->fmt_type;
4779a2fe9b8SRuslan Bilovol 		break;
4789a2fe9b8SRuslan Bilovol 	}
4799a2fe9b8SRuslan Bilovol 	}
4809a2fe9b8SRuslan Bilovol 
4819a2fe9b8SRuslan Bilovol 	if (fmt_type == UAC_FORMAT_TYPE_III) {
482e5779998SDaniel Mack 		/* FIXME: the format type is really IECxxx
483e5779998SDaniel Mack 		 *        but we give normal PCM format to get the existing
484e5779998SDaniel Mack 		 *        apps working...
485e5779998SDaniel Mack 		 */
486e5779998SDaniel Mack 		switch (chip->usb_id) {
487e5779998SDaniel Mack 
488e5779998SDaniel Mack 		case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
489e5779998SDaniel Mack 			if (chip->setup == 0x00 &&
490e5779998SDaniel Mack 			    fp->altsetting == 6)
491e5779998SDaniel Mack 				pcm_format = SNDRV_PCM_FORMAT_S16_BE;
492e5779998SDaniel Mack 			else
493e5779998SDaniel Mack 				pcm_format = SNDRV_PCM_FORMAT_S16_LE;
494e5779998SDaniel Mack 			break;
495e5779998SDaniel Mack 		default:
496e5779998SDaniel Mack 			pcm_format = SNDRV_PCM_FORMAT_S16_LE;
497e5779998SDaniel Mack 		}
49874c34ca1SEldad Zack 		fp->formats = pcm_format_to_bits(pcm_format);
499e5779998SDaniel Mack 	} else {
5009a2fe9b8SRuslan Bilovol 		fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt);
50129088fefSClemens Ladisch 		if (!fp->formats)
5028ad10dc6SSachin Kamat 			return -EINVAL;
503e5779998SDaniel Mack 	}
504e5779998SDaniel Mack 
505e5779998SDaniel Mack 	/* gather possible sample rates */
506e5779998SDaniel Mack 	/* audio class v1 reports possible sample rates as part of the
507e5779998SDaniel Mack 	 * proprietary class specific descriptor.
508e5779998SDaniel Mack 	 * audio class v2 uses class specific EP0 range requests for that.
509e5779998SDaniel Mack 	 */
5108f898e92SClemens Ladisch 	switch (fp->protocol) {
511a2acad82SClemens Ladisch 	default:
5129a2fe9b8SRuslan Bilovol 	case UAC_VERSION_1: {
5139a2fe9b8SRuslan Bilovol 		struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
5149a2fe9b8SRuslan Bilovol 
515e5779998SDaniel Mack 		fp->channels = fmt->bNrChannels;
51674754f97SDaniel Mack 		ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
517e5779998SDaniel Mack 		break;
5189a2fe9b8SRuslan Bilovol 	}
519e5779998SDaniel Mack 	case UAC_VERSION_2:
5209a2fe9b8SRuslan Bilovol 	case UAC_VERSION_3: {
521e5779998SDaniel Mack 		/* fp->channels is already set in this case */
5229a2fe9b8SRuslan Bilovol 		ret = parse_audio_format_rates_v2v3(chip, fp);
523e5779998SDaniel Mack 		break;
524e5779998SDaniel Mack 	}
5259a2fe9b8SRuslan Bilovol 	}
526e5779998SDaniel Mack 
527e5779998SDaniel Mack 	if (fp->channels < 1) {
5280ba41d91STakashi Iwai 		usb_audio_err(chip,
5290ba41d91STakashi Iwai 			"%u:%d : invalid channels %d\n",
5300ba41d91STakashi Iwai 			fp->iface, fp->altsetting, fp->channels);
5318ad10dc6SSachin Kamat 		return -EINVAL;
532e5779998SDaniel Mack 	}
533e5779998SDaniel Mack 
534e5779998SDaniel Mack 	return ret;
535e5779998SDaniel Mack }
536e5779998SDaniel Mack 
537e5779998SDaniel Mack /*
538e5779998SDaniel Mack  * parse the format type II descriptor
539e5779998SDaniel Mack  */
540e5779998SDaniel Mack static int parse_audio_format_ii(struct snd_usb_audio *chip,
541e5779998SDaniel Mack 				 struct audioformat *fp,
5429a2fe9b8SRuslan Bilovol 				 u64 format, void *_fmt)
543e5779998SDaniel Mack {
544e5779998SDaniel Mack 	int brate, framesize, ret;
545e5779998SDaniel Mack 
546e5779998SDaniel Mack 	switch (format) {
547e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_II_AC3:
548e5779998SDaniel Mack 		/* FIXME: there is no AC3 format defined yet */
549015eb0b0SClemens Ladisch 		// fp->formats = SNDRV_PCM_FMTBIT_AC3;
550015eb0b0SClemens Ladisch 		fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
551e5779998SDaniel Mack 		break;
552e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_II_MPEG:
553015eb0b0SClemens Ladisch 		fp->formats = SNDRV_PCM_FMTBIT_MPEG;
554e5779998SDaniel Mack 		break;
555e5779998SDaniel Mack 	default:
5560ba41d91STakashi Iwai 		usb_audio_info(chip,
5579a2fe9b8SRuslan Bilovol 			 "%u:%d : unknown format tag %#llx is detected.  processed as MPEG.\n",
5580ba41d91STakashi Iwai 			 fp->iface, fp->altsetting, format);
559015eb0b0SClemens Ladisch 		fp->formats = SNDRV_PCM_FMTBIT_MPEG;
560e5779998SDaniel Mack 		break;
561e5779998SDaniel Mack 	}
562e5779998SDaniel Mack 
563e5779998SDaniel Mack 	fp->channels = 1;
564e5779998SDaniel Mack 
5658f898e92SClemens Ladisch 	switch (fp->protocol) {
566a2acad82SClemens Ladisch 	default:
567e5779998SDaniel Mack 	case UAC_VERSION_1: {
568e5779998SDaniel Mack 		struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
569e5779998SDaniel Mack 		brate = le16_to_cpu(fmt->wMaxBitRate);
570e5779998SDaniel Mack 		framesize = le16_to_cpu(fmt->wSamplesPerFrame);
5710ba41d91STakashi Iwai 		usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
572e5779998SDaniel Mack 		fp->frame_size = framesize;
573e5779998SDaniel Mack 		ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
574e5779998SDaniel Mack 		break;
575e5779998SDaniel Mack 	}
576e5779998SDaniel Mack 	case UAC_VERSION_2: {
577e5779998SDaniel Mack 		struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
578e5779998SDaniel Mack 		brate = le16_to_cpu(fmt->wMaxBitRate);
579e5779998SDaniel Mack 		framesize = le16_to_cpu(fmt->wSamplesPerFrame);
5800ba41d91STakashi Iwai 		usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
581e5779998SDaniel Mack 		fp->frame_size = framesize;
5829a2fe9b8SRuslan Bilovol 		ret = parse_audio_format_rates_v2v3(chip, fp);
583e5779998SDaniel Mack 		break;
584e5779998SDaniel Mack 	}
585e5779998SDaniel Mack 	}
586e5779998SDaniel Mack 
587e5779998SDaniel Mack 	return ret;
588e5779998SDaniel Mack }
589e5779998SDaniel Mack 
5902fcdb06dSDaniel Mack int snd_usb_parse_audio_format(struct snd_usb_audio *chip,
5919a2fe9b8SRuslan Bilovol 			       struct audioformat *fp, u64 format,
5922fcdb06dSDaniel Mack 			       struct uac_format_type_i_continuous_descriptor *fmt,
5938f898e92SClemens Ladisch 			       int stream)
594e5779998SDaniel Mack {
595e5779998SDaniel Mack 	int err;
596e5779998SDaniel Mack 
59774754f97SDaniel Mack 	switch (fmt->bFormatType) {
598e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_I:
599e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_III:
6008f898e92SClemens Ladisch 		err = parse_audio_format_i(chip, fp, format, fmt);
601e5779998SDaniel Mack 		break;
602e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_II:
6038f898e92SClemens Ladisch 		err = parse_audio_format_ii(chip, fp, format, fmt);
604e5779998SDaniel Mack 		break;
605e5779998SDaniel Mack 	default:
6060ba41d91STakashi Iwai 		usb_audio_info(chip,
6070ba41d91STakashi Iwai 			 "%u:%d : format type %d is not supported yet\n",
6080ba41d91STakashi Iwai 			 fp->iface, fp->altsetting,
60974754f97SDaniel Mack 			 fmt->bFormatType);
6108d091242SDaniel Mack 		return -ENOTSUPP;
611e5779998SDaniel Mack 	}
61274754f97SDaniel Mack 	fp->fmt_type = fmt->bFormatType;
613e5779998SDaniel Mack 	if (err < 0)
614e5779998SDaniel Mack 		return err;
615e5779998SDaniel Mack #if 1
616e5779998SDaniel Mack 	/* FIXME: temporary hack for extigy/audigy 2 nx/zs */
617e5779998SDaniel Mack 	/* extigy apparently supports sample rates other than 48k
618e5779998SDaniel Mack 	 * but not in ordinary way.  so we enable only 48k atm.
619e5779998SDaniel Mack 	 */
620e5779998SDaniel Mack 	if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
621e5779998SDaniel Mack 	    chip->usb_id == USB_ID(0x041e, 0x3020) ||
622e5779998SDaniel Mack 	    chip->usb_id == USB_ID(0x041e, 0x3061)) {
62374754f97SDaniel Mack 		if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
624e5779998SDaniel Mack 		    fp->rates != SNDRV_PCM_RATE_48000 &&
625e5779998SDaniel Mack 		    fp->rates != SNDRV_PCM_RATE_96000)
6268d091242SDaniel Mack 			return -ENOTSUPP;
627e5779998SDaniel Mack 	}
628e5779998SDaniel Mack #endif
629e5779998SDaniel Mack 	return 0;
630e5779998SDaniel Mack }
631e5779998SDaniel Mack 
6329a2fe9b8SRuslan Bilovol int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip,
6339a2fe9b8SRuslan Bilovol 			       struct audioformat *fp,
6349a2fe9b8SRuslan Bilovol 			       struct uac3_as_header_descriptor *as,
6359a2fe9b8SRuslan Bilovol 			       int stream)
6369a2fe9b8SRuslan Bilovol {
6379a2fe9b8SRuslan Bilovol 	u64 format = le64_to_cpu(as->bmFormats);
6389a2fe9b8SRuslan Bilovol 	int err;
6399a2fe9b8SRuslan Bilovol 
6409a2fe9b8SRuslan Bilovol 	/*
6419a2fe9b8SRuslan Bilovol 	 * Type I format bits are D0..D6
6429a2fe9b8SRuslan Bilovol 	 * This test works because type IV is not supported
6439a2fe9b8SRuslan Bilovol 	 */
6449a2fe9b8SRuslan Bilovol 	if (format & 0x7f)
6459a2fe9b8SRuslan Bilovol 		fp->fmt_type = UAC_FORMAT_TYPE_I;
6469a2fe9b8SRuslan Bilovol 	else
6479a2fe9b8SRuslan Bilovol 		fp->fmt_type = UAC_FORMAT_TYPE_III;
6489a2fe9b8SRuslan Bilovol 
6499a2fe9b8SRuslan Bilovol 	err = parse_audio_format_i(chip, fp, format, as);
6509a2fe9b8SRuslan Bilovol 	if (err < 0)
6519a2fe9b8SRuslan Bilovol 		return err;
6529a2fe9b8SRuslan Bilovol 
6539a2fe9b8SRuslan Bilovol 	return 0;
6549a2fe9b8SRuslan Bilovol }
655