xref: /openbmc/linux/sound/usb/format.c (revision 96d3c5a7d20ec546e44695983fe0508c6f904248)
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"
1979f920fbSDaniel Mack #include "clock.h"
20ee95cb61SDaniel Mack #include "format.h"
21e5779998SDaniel Mack 
22e5779998SDaniel Mack /*
23e5779998SDaniel Mack  * parse the audio format type I descriptor
24e5779998SDaniel Mack  * and returns the corresponding pcm format
25e5779998SDaniel Mack  *
26e5779998SDaniel Mack  * @dev: usb device
27e5779998SDaniel Mack  * @fp: audioformat record
28e5779998SDaniel Mack  * @format: the format tag (wFormatTag)
299a2fe9b8SRuslan Bilovol  * @fmt: the format type descriptor (v1/v2) or AudioStreaming descriptor (v3)
30e5779998SDaniel Mack  */
parse_audio_format_i_type(struct snd_usb_audio * chip,struct audioformat * fp,u64 format,void * _fmt)3129088fefSClemens Ladisch static u64 parse_audio_format_i_type(struct snd_usb_audio *chip,
32e5779998SDaniel Mack 				     struct audioformat *fp,
339a2fe9b8SRuslan Bilovol 				     u64 format, void *_fmt)
34e5779998SDaniel Mack {
35e5779998SDaniel Mack 	int sample_width, sample_bytes;
36717bfb5fSDaniel Mack 	u64 pcm_formats = 0;
37e5779998SDaniel Mack 
388f898e92SClemens Ladisch 	switch (fp->protocol) {
39a2acad82SClemens Ladisch 	case UAC_VERSION_1:
40a2acad82SClemens Ladisch 	default: {
41e5779998SDaniel Mack 		struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
42fa4e7a6fSTakashi Iwai 		if (format >= 64) {
43fa4e7a6fSTakashi Iwai 			usb_audio_info(chip,
44fa4e7a6fSTakashi Iwai 				       "%u:%d: invalid format type 0x%llx is detected, processed as PCM\n",
45fa4e7a6fSTakashi Iwai 				       fp->iface, fp->altsetting, format);
46fa4e7a6fSTakashi Iwai 			format = UAC_FORMAT_TYPE_I_PCM;
47fa4e7a6fSTakashi Iwai 		}
48e5779998SDaniel Mack 		sample_width = fmt->bBitResolution;
49e5779998SDaniel Mack 		sample_bytes = fmt->bSubframeSize;
50b44d419bSDan Carpenter 		format = 1ULL << format;
51e5779998SDaniel Mack 		break;
52e5779998SDaniel Mack 	}
53e5779998SDaniel Mack 
54e5779998SDaniel Mack 	case UAC_VERSION_2: {
55e5779998SDaniel Mack 		struct uac_format_type_i_ext_descriptor *fmt = _fmt;
56e5779998SDaniel Mack 		sample_width = fmt->bBitResolution;
57e5779998SDaniel Mack 		sample_bytes = fmt->bSubslotSize;
58717bfb5fSDaniel Mack 
593a572d94SJussi Laako 		if (format & UAC2_FORMAT_TYPE_I_RAW_DATA) {
60717bfb5fSDaniel Mack 			pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
613a572d94SJussi Laako 			/* flag potentially raw DSD capable altsettings */
623a572d94SJussi Laako 			fp->dsd_raw = true;
633a572d94SJussi Laako 		}
64717bfb5fSDaniel Mack 
6529088fefSClemens Ladisch 		format <<= 1;
66e5779998SDaniel Mack 		break;
67e5779998SDaniel Mack 	}
689a2fe9b8SRuslan Bilovol 	case UAC_VERSION_3: {
699a2fe9b8SRuslan Bilovol 		struct uac3_as_header_descriptor *as = _fmt;
709a2fe9b8SRuslan Bilovol 
719a2fe9b8SRuslan Bilovol 		sample_width = as->bBitResolution;
729a2fe9b8SRuslan Bilovol 		sample_bytes = as->bSubslotSize;
739a2fe9b8SRuslan Bilovol 
749a2fe9b8SRuslan Bilovol 		if (format & UAC3_FORMAT_TYPE_I_RAW_DATA)
759a2fe9b8SRuslan Bilovol 			pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
769a2fe9b8SRuslan Bilovol 
779a2fe9b8SRuslan Bilovol 		format <<= 1;
789a2fe9b8SRuslan Bilovol 		break;
799a2fe9b8SRuslan Bilovol 	}
80e5779998SDaniel Mack 	}
81e5779998SDaniel Mack 
82c7a13264SJussi Laako 	fp->fmt_bits = sample_width;
83c7a13264SJussi Laako 
84717bfb5fSDaniel Mack 	if ((pcm_formats == 0) &&
85717bfb5fSDaniel Mack 	    (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED))) {
8629088fefSClemens Ladisch 		/* some devices don't define this correctly... */
870ba41d91STakashi Iwai 		usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM\n",
880ba41d91STakashi Iwai 			fp->iface, fp->altsetting);
8929088fefSClemens Ladisch 		format = 1 << UAC_FORMAT_TYPE_I_PCM;
9029088fefSClemens Ladisch 	}
9129088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) {
926d1f2f60STakamichi Horikawa 		if (((chip->usb_id == USB_ID(0x0582, 0x0016)) ||
936d1f2f60STakamichi Horikawa 		     /* Edirol SD-90 */
946d1f2f60STakamichi Horikawa 		     (chip->usb_id == USB_ID(0x0582, 0x000c))) &&
956d1f2f60STakamichi Horikawa 		     /* Roland SC-D70 */
96061b869eSClemens Ladisch 		    sample_width == 24 && sample_bytes == 2)
97061b869eSClemens Ladisch 			sample_bytes = 3;
98061b869eSClemens Ladisch 		else if (sample_width > sample_bytes * 8) {
990ba41d91STakashi Iwai 			usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d\n",
1000ba41d91STakashi Iwai 				 fp->iface, fp->altsetting,
101e5779998SDaniel Mack 				 sample_width, sample_bytes);
102e5779998SDaniel Mack 		}
103e5779998SDaniel Mack 		/* check the format byte size */
104e5779998SDaniel Mack 		switch (sample_bytes) {
105e5779998SDaniel Mack 		case 1:
10629088fefSClemens Ladisch 			pcm_formats |= SNDRV_PCM_FMTBIT_S8;
107e5779998SDaniel Mack 			break;
108e5779998SDaniel Mack 		case 2:
109e5779998SDaniel Mack 			if (snd_usb_is_big_endian_format(chip, fp))
11029088fefSClemens Ladisch 				pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */
111e5779998SDaniel Mack 			else
11229088fefSClemens Ladisch 				pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE;
113e5779998SDaniel Mack 			break;
114e5779998SDaniel Mack 		case 3:
115e5779998SDaniel Mack 			if (snd_usb_is_big_endian_format(chip, fp))
11629088fefSClemens Ladisch 				pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */
117e5779998SDaniel Mack 			else
11829088fefSClemens Ladisch 				pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE;
119e5779998SDaniel Mack 			break;
120e5779998SDaniel Mack 		case 4:
12129088fefSClemens Ladisch 			pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE;
122e5779998SDaniel Mack 			break;
123e5779998SDaniel Mack 		default:
1240ba41d91STakashi Iwai 			usb_audio_info(chip,
1250ba41d91STakashi Iwai 				 "%u:%d : unsupported sample bitwidth %d in %d bytes\n",
1260ba41d91STakashi Iwai 				 fp->iface, fp->altsetting,
127e5779998SDaniel Mack 				 sample_width, sample_bytes);
128e5779998SDaniel Mack 			break;
129e5779998SDaniel Mack 		}
13029088fefSClemens Ladisch 	}
13129088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) {
132e5779998SDaniel Mack 		/* Dallas DS4201 workaround: it advertises U8 format, but really
133e5779998SDaniel Mack 		   supports S8. */
134e5779998SDaniel Mack 		if (chip->usb_id == USB_ID(0x04fa, 0x4201))
13529088fefSClemens Ladisch 			pcm_formats |= SNDRV_PCM_FMTBIT_S8;
13629088fefSClemens Ladisch 		else
13729088fefSClemens Ladisch 			pcm_formats |= SNDRV_PCM_FMTBIT_U8;
138e5779998SDaniel Mack 	}
13929088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) {
14029088fefSClemens Ladisch 		pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
14129088fefSClemens Ladisch 	}
14229088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) {
14329088fefSClemens Ladisch 		pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;
14429088fefSClemens Ladisch 	}
14529088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) {
14629088fefSClemens Ladisch 		pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;
14729088fefSClemens Ladisch 	}
14829088fefSClemens Ladisch 	if (format & ~0x3f) {
1490ba41d91STakashi Iwai 		usb_audio_info(chip,
1509a2fe9b8SRuslan Bilovol 			 "%u:%d : unsupported format bits %#llx\n",
1510ba41d91STakashi Iwai 			 fp->iface, fp->altsetting, format);
15229088fefSClemens Ladisch 	}
153126825e7SDaniel Mack 
154126825e7SDaniel Mack 	pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes);
155126825e7SDaniel Mack 
15629088fefSClemens Ladisch 	return pcm_formats;
157e5779998SDaniel Mack }
158e5779998SDaniel Mack 
set_fixed_rate(struct audioformat * fp,int rate,int rate_bits)15974f73476STakashi Iwai static int set_fixed_rate(struct audioformat *fp, int rate, int rate_bits)
16074f73476STakashi Iwai {
16174f73476STakashi Iwai 	kfree(fp->rate_table);
16274f73476STakashi Iwai 	fp->rate_table = kmalloc(sizeof(int), GFP_KERNEL);
16374f73476STakashi Iwai 	if (!fp->rate_table)
16474f73476STakashi Iwai 		return -ENOMEM;
16574f73476STakashi Iwai 	fp->nr_rates = 1;
16674f73476STakashi Iwai 	fp->rate_min = rate;
16774f73476STakashi Iwai 	fp->rate_max = rate;
16874f73476STakashi Iwai 	fp->rates = rate_bits;
16974f73476STakashi Iwai 	fp->rate_table[0] = rate;
17074f73476STakashi Iwai 	return 0;
17174f73476STakashi Iwai }
172e5779998SDaniel Mack 
17313ee0336STakashi Iwai /* set up rate_min, rate_max and rates from the rate table */
set_rate_table_min_max(struct audioformat * fp)17413ee0336STakashi Iwai static void set_rate_table_min_max(struct audioformat *fp)
17513ee0336STakashi Iwai {
17613ee0336STakashi Iwai 	unsigned int rate;
17713ee0336STakashi Iwai 	int i;
17813ee0336STakashi Iwai 
17913ee0336STakashi Iwai 	fp->rate_min = INT_MAX;
18013ee0336STakashi Iwai 	fp->rate_max = 0;
18113ee0336STakashi Iwai 	fp->rates = 0;
18213ee0336STakashi Iwai 	for (i = 0; i < fp->nr_rates; i++) {
18313ee0336STakashi Iwai 		rate = fp->rate_table[i];
18413ee0336STakashi Iwai 		fp->rate_min = min(fp->rate_min, rate);
18513ee0336STakashi Iwai 		fp->rate_max = max(fp->rate_max, rate);
18613ee0336STakashi Iwai 		fp->rates |= snd_pcm_rate_to_rate_bit(rate);
18713ee0336STakashi Iwai 	}
18813ee0336STakashi Iwai }
18913ee0336STakashi Iwai 
190e5779998SDaniel Mack /*
191e5779998SDaniel Mack  * parse the format descriptor and stores the possible sample rates
192e5779998SDaniel Mack  * on the audioformat table (audio class v1).
193e5779998SDaniel Mack  *
194e5779998SDaniel Mack  * @dev: usb device
195e5779998SDaniel Mack  * @fp: audioformat record
196e5779998SDaniel Mack  * @fmt: the format descriptor
197e5779998SDaniel Mack  * @offset: the start offset of descriptor pointing the rate type
198e5779998SDaniel Mack  *          (7 for type I and II, 8 for type II)
199e5779998SDaniel Mack  */
parse_audio_format_rates_v1(struct snd_usb_audio * chip,struct audioformat * fp,unsigned char * fmt,int offset)200e5779998SDaniel Mack static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
201e5779998SDaniel Mack 				       unsigned char *fmt, int offset)
202e5779998SDaniel Mack {
203e5779998SDaniel Mack 	int nr_rates = fmt[offset];
204e5779998SDaniel Mack 
205e5779998SDaniel Mack 	if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
2060ba41d91STakashi Iwai 		usb_audio_err(chip,
2070ba41d91STakashi Iwai 			"%u:%d : invalid UAC_FORMAT_TYPE desc\n",
2080ba41d91STakashi Iwai 			fp->iface, fp->altsetting);
2098ad10dc6SSachin Kamat 		return -EINVAL;
210e5779998SDaniel Mack 	}
211e5779998SDaniel Mack 
212e5779998SDaniel Mack 	if (nr_rates) {
213e5779998SDaniel Mack 		/*
214e5779998SDaniel Mack 		 * build the rate table and bitmap flags
215e5779998SDaniel Mack 		 */
216e5779998SDaniel Mack 		int r, idx;
217e5779998SDaniel Mack 
2186da2ec56SKees Cook 		fp->rate_table = kmalloc_array(nr_rates, sizeof(int),
2196da2ec56SKees Cook 					       GFP_KERNEL);
2201bc00f32SShawn Lin 		if (fp->rate_table == NULL)
2218ad10dc6SSachin Kamat 			return -ENOMEM;
222e5779998SDaniel Mack 
223e5779998SDaniel Mack 		fp->nr_rates = 0;
224e5779998SDaniel Mack 		for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
225e5779998SDaniel Mack 			unsigned int rate = combine_triple(&fmt[idx]);
226e5779998SDaniel Mack 			if (!rate)
227e5779998SDaniel Mack 				continue;
228e5779998SDaniel Mack 			/* C-Media CM6501 mislabels its 96 kHz altsetting */
2298129e79eSWolfgang Breyha 			/* Terratec Aureon 7.1 USB C-Media 6206, too */
230aecc19ecSDaehwan Jung 			/* Ozone Z90 USB C-Media, too */
231e5779998SDaniel Mack 			if (rate == 48000 && nr_rates == 1 &&
232e5779998SDaniel Mack 			    (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
2338129e79eSWolfgang Breyha 			     chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
234aecc19ecSDaehwan Jung 			     chip->usb_id == USB_ID(0x0d8c, 0x0078) ||
2358129e79eSWolfgang Breyha 			     chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
236e5779998SDaniel Mack 			    fp->altsetting == 5 && fp->maxpacksize == 392)
237e5779998SDaniel Mack 				rate = 96000;
2388c4b79cfSPavel Hofman 			/* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
2398c4b79cfSPavel Hofman 			if (rate == 16000 &&
2408c4b79cfSPavel Hofman 			    (chip->usb_id == USB_ID(0x041e, 0x4064) ||
2418c4b79cfSPavel Hofman 			     chip->usb_id == USB_ID(0x041e, 0x4068)))
242e5779998SDaniel Mack 				rate = 8000;
243e5779998SDaniel Mack 
24413ee0336STakashi Iwai 			fp->rate_table[fp->nr_rates++] = rate;
245e5779998SDaniel Mack 		}
246e5779998SDaniel Mack 		if (!fp->nr_rates) {
2477ec827b9STakashi Iwai 			usb_audio_info(chip,
2487ec827b9STakashi Iwai 				       "%u:%d: All rates were zero\n",
2497ec827b9STakashi Iwai 				       fp->iface, fp->altsetting);
2508ad10dc6SSachin Kamat 			return -EINVAL;
251e5779998SDaniel Mack 		}
25213ee0336STakashi Iwai 		set_rate_table_min_max(fp);
253e5779998SDaniel Mack 	} else {
254e5779998SDaniel Mack 		/* continuous rates */
255e5779998SDaniel Mack 		fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
256e5779998SDaniel Mack 		fp->rate_min = combine_triple(&fmt[offset + 1]);
257e5779998SDaniel Mack 		fp->rate_max = combine_triple(&fmt[offset + 4]);
258e5779998SDaniel Mack 	}
25974f73476STakashi Iwai 
26074f73476STakashi Iwai 	/* Jabra Evolve 65 headset */
26174f73476STakashi Iwai 	if (chip->usb_id == USB_ID(0x0b0e, 0x030b)) {
26274f73476STakashi Iwai 		/* only 48kHz for playback while keeping 16kHz for capture */
26374f73476STakashi Iwai 		if (fp->nr_rates != 1)
26474f73476STakashi Iwai 			return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000);
26574f73476STakashi Iwai 	}
26674f73476STakashi Iwai 
267e5779998SDaniel Mack 	return 0;
268e5779998SDaniel Mack }
269e5779998SDaniel Mack 
2708dc5efe3SNick Kossifidis 
2718dc5efe3SNick Kossifidis /*
2728dc5efe3SNick Kossifidis  * Presonus Studio 1810c supports a limited set of sampling
2738dc5efe3SNick Kossifidis  * rates per altsetting but reports the full set each time.
2748dc5efe3SNick Kossifidis  * If we don't filter out the unsupported rates and attempt
2758dc5efe3SNick Kossifidis  * to configure the card, it will hang refusing to do any
2768dc5efe3SNick Kossifidis  * further audio I/O until a hard reset is performed.
2778dc5efe3SNick Kossifidis  *
2788dc5efe3SNick Kossifidis  * The list of supported rates per altsetting (set of available
2798dc5efe3SNick Kossifidis  * I/O channels) is described in the owner's manual, section 2.2.
2808dc5efe3SNick Kossifidis  */
s1810c_valid_sample_rate(struct audioformat * fp,unsigned int rate)2818dc5efe3SNick Kossifidis static bool s1810c_valid_sample_rate(struct audioformat *fp,
2828dc5efe3SNick Kossifidis 				     unsigned int rate)
2838dc5efe3SNick Kossifidis {
2848dc5efe3SNick Kossifidis 	switch (fp->altsetting) {
2858dc5efe3SNick Kossifidis 	case 1:
2868dc5efe3SNick Kossifidis 		/* All ADAT ports available */
2878dc5efe3SNick Kossifidis 		return rate <= 48000;
2888dc5efe3SNick Kossifidis 	case 2:
2898dc5efe3SNick Kossifidis 		/* Half of ADAT ports available */
2908dc5efe3SNick Kossifidis 		return (rate == 88200 || rate == 96000);
2918dc5efe3SNick Kossifidis 	case 3:
2928dc5efe3SNick Kossifidis 		/* Analog I/O only (no S/PDIF nor ADAT) */
2938dc5efe3SNick Kossifidis 		return rate >= 176400;
2948dc5efe3SNick Kossifidis 	default:
2958dc5efe3SNick Kossifidis 		return false;
2968dc5efe3SNick Kossifidis 	}
2978dc5efe3SNick Kossifidis 	return false;
2988dc5efe3SNick Kossifidis }
2998dc5efe3SNick Kossifidis 
300e5779998SDaniel Mack /*
3011c826792SAlexander Tsoy  * Many Focusrite devices supports a limited set of sampling rates per
3021c826792SAlexander Tsoy  * altsetting. Maximum rate is exposed in the last 4 bytes of Format Type
3031c826792SAlexander Tsoy  * descriptor which has a non-standard bLength = 10.
3041c826792SAlexander Tsoy  */
focusrite_valid_sample_rate(struct snd_usb_audio * chip,struct audioformat * fp,unsigned int rate)3051c826792SAlexander Tsoy static bool focusrite_valid_sample_rate(struct snd_usb_audio *chip,
3061c826792SAlexander Tsoy 					struct audioformat *fp,
3071c826792SAlexander Tsoy 					unsigned int rate)
3081c826792SAlexander Tsoy {
3091c826792SAlexander Tsoy 	struct usb_interface *iface;
3101c826792SAlexander Tsoy 	struct usb_host_interface *alts;
3111c826792SAlexander Tsoy 	unsigned char *fmt;
3121c826792SAlexander Tsoy 	unsigned int max_rate;
3131c826792SAlexander Tsoy 
3141c826792SAlexander Tsoy 	iface = usb_ifnum_to_if(chip->dev, fp->iface);
3151c826792SAlexander Tsoy 	if (!iface)
3161c826792SAlexander Tsoy 		return true;
3171c826792SAlexander Tsoy 
3181c826792SAlexander Tsoy 	alts = &iface->altsetting[fp->altset_idx];
3191c826792SAlexander Tsoy 	fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
3201c826792SAlexander Tsoy 				      NULL, UAC_FORMAT_TYPE);
3211c826792SAlexander Tsoy 	if (!fmt)
3221c826792SAlexander Tsoy 		return true;
3231c826792SAlexander Tsoy 
3241c826792SAlexander Tsoy 	if (fmt[0] == 10) { /* bLength */
3251c826792SAlexander Tsoy 		max_rate = combine_quad(&fmt[6]);
3261c826792SAlexander Tsoy 
3271c826792SAlexander Tsoy 		/* Validate max rate */
3281c826792SAlexander Tsoy 		if (max_rate != 48000 &&
3291c826792SAlexander Tsoy 		    max_rate != 96000 &&
3301c826792SAlexander Tsoy 		    max_rate != 192000 &&
3311c826792SAlexander Tsoy 		    max_rate != 384000) {
3321c826792SAlexander Tsoy 
3331c826792SAlexander Tsoy 			usb_audio_info(chip,
3341c826792SAlexander Tsoy 				"%u:%d : unexpected max rate: %u\n",
3351c826792SAlexander Tsoy 				fp->iface, fp->altsetting, max_rate);
3361c826792SAlexander Tsoy 
3371c826792SAlexander Tsoy 			return true;
3381c826792SAlexander Tsoy 		}
3391c826792SAlexander Tsoy 
3401c826792SAlexander Tsoy 		return rate <= max_rate;
3411c826792SAlexander Tsoy 	}
3421c826792SAlexander Tsoy 
3431c826792SAlexander Tsoy 	return true;
3441c826792SAlexander Tsoy }
3451c826792SAlexander Tsoy 
3461c826792SAlexander Tsoy /*
34767c10366SDaniel Mack  * Helper function to walk the array of sample rate triplets reported by
34867c10366SDaniel Mack  * the device. The problem is that we need to parse whole array first to
34967c10366SDaniel Mack  * get to know how many sample rates we have to expect.
35067c10366SDaniel Mack  * Then fp->rate_table can be allocated and filled.
35167c10366SDaniel Mack  */
parse_uac2_sample_rate_range(struct snd_usb_audio * chip,struct audioformat * fp,int nr_triplets,const unsigned char * data)3520ba41d91STakashi Iwai static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
3530ba41d91STakashi Iwai 					struct audioformat *fp, int nr_triplets,
35467c10366SDaniel Mack 					const unsigned char *data)
35567c10366SDaniel Mack {
35667c10366SDaniel Mack 	int i, nr_rates = 0;
35767c10366SDaniel Mack 
35867c10366SDaniel Mack 	for (i = 0; i < nr_triplets; i++) {
35967c10366SDaniel Mack 		int min = combine_quad(&data[2 + 12 * i]);
36067c10366SDaniel Mack 		int max = combine_quad(&data[6 + 12 * i]);
36167c10366SDaniel Mack 		int res = combine_quad(&data[10 + 12 * i]);
3624fa0e81bSXi Wang 		unsigned int rate;
36367c10366SDaniel Mack 
36467c10366SDaniel Mack 		if ((max < 0) || (min < 0) || (res < 0) || (max < min))
36567c10366SDaniel Mack 			continue;
36667c10366SDaniel Mack 
36767c10366SDaniel Mack 		/*
36867c10366SDaniel Mack 		 * for ranges with res == 1, we announce a continuous sample
36967c10366SDaniel Mack 		 * rate range, and this function should return 0 for no further
37067c10366SDaniel Mack 		 * parsing.
37167c10366SDaniel Mack 		 */
37267c10366SDaniel Mack 		if (res == 1) {
37367c10366SDaniel Mack 			fp->rate_min = min;
37467c10366SDaniel Mack 			fp->rate_max = max;
37567c10366SDaniel Mack 			fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
37667c10366SDaniel Mack 			return 0;
37767c10366SDaniel Mack 		}
37867c10366SDaniel Mack 
37967c10366SDaniel Mack 		for (rate = min; rate <= max; rate += res) {
3808dc5efe3SNick Kossifidis 
3818dc5efe3SNick Kossifidis 			/* Filter out invalid rates on Presonus Studio 1810c */
3821e583aefSTakashi Iwai 			if (chip->usb_id == USB_ID(0x194f, 0x010c) &&
3838dc5efe3SNick Kossifidis 			    !s1810c_valid_sample_rate(fp, rate))
3848dc5efe3SNick Kossifidis 				goto skip_rate;
3858dc5efe3SNick Kossifidis 
3861c826792SAlexander Tsoy 			/* Filter out invalid rates on Focusrite devices */
3871c826792SAlexander Tsoy 			if (USB_ID_VENDOR(chip->usb_id) == 0x1235 &&
3881c826792SAlexander Tsoy 			    !focusrite_valid_sample_rate(chip, fp, rate))
3891c826792SAlexander Tsoy 				goto skip_rate;
3901c826792SAlexander Tsoy 
39167c10366SDaniel Mack 			if (fp->rate_table)
39267c10366SDaniel Mack 				fp->rate_table[nr_rates] = rate;
39367c10366SDaniel Mack 			nr_rates++;
3948866f405SXi Wang 			if (nr_rates >= MAX_NR_RATES) {
3950ba41d91STakashi Iwai 				usb_audio_err(chip, "invalid uac2 rates\n");
3964fa0e81bSXi Wang 				break;
3974fa0e81bSXi Wang 			}
39867c10366SDaniel Mack 
3998dc5efe3SNick Kossifidis skip_rate:
40067c10366SDaniel Mack 			/* avoid endless loop */
40167c10366SDaniel Mack 			if (res == 0)
40267c10366SDaniel Mack 				break;
40367c10366SDaniel Mack 		}
40467c10366SDaniel Mack 	}
40567c10366SDaniel Mack 
40667c10366SDaniel Mack 	return nr_rates;
40767c10366SDaniel Mack }
40867c10366SDaniel Mack 
4098abf41dcSChristopher Swenson /* Line6 Helix series and the Rode Rodecaster Pro don't support the
4108abf41dcSChristopher Swenson  * UAC2_CS_RANGE usb function call. Return a static table of known
4118abf41dcSChristopher Swenson  * clock rates.
412d4bd3053SNicola Lunghi  */
line6_parse_audio_format_rates_quirk(struct snd_usb_audio * chip,struct audioformat * fp)413d4bd3053SNicola Lunghi static int line6_parse_audio_format_rates_quirk(struct snd_usb_audio *chip,
414d4bd3053SNicola Lunghi 						struct audioformat *fp)
415d4bd3053SNicola Lunghi {
416d4bd3053SNicola Lunghi 	switch (chip->usb_id) {
4178be03a71STakashi Iwai 	case USB_ID(0x0e41, 0x4241): /* Line6 Helix */
4188be03a71STakashi Iwai 	case USB_ID(0x0e41, 0x4242): /* Line6 Helix Rack */
4198be03a71STakashi Iwai 	case USB_ID(0x0e41, 0x4244): /* Line6 Helix LT */
4208be03a71STakashi Iwai 	case USB_ID(0x0e41, 0x4246): /* Line6 HX-Stomp */
4218f27b689SJason Ormes 	case USB_ID(0x0e41, 0x4253): /* Line6 HX-Stomp XL */
4227da4c510SLukasz Halman 	case USB_ID(0x0e41, 0x4247): /* Line6 Pod Go */
4239b132f27STakashi Iwai 	case USB_ID(0x0e41, 0x4248): /* Line6 Helix >= fw 2.82 */
4249b132f27STakashi Iwai 	case USB_ID(0x0e41, 0x4249): /* Line6 Helix Rack >= fw 2.82 */
4259b132f27STakashi Iwai 	case USB_ID(0x0e41, 0x424a): /* Line6 Helix LT >= fw 2.82 */
426359b4315STakashi Iwai 	case USB_ID(0x0e41, 0x424b): /* Line6 Pod Go */
4278abf41dcSChristopher Swenson 	case USB_ID(0x19f7, 0x0011): /* Rode Rodecaster Pro */
42874f73476STakashi Iwai 		return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000);
429d4bd3053SNicola Lunghi 	}
430d4bd3053SNicola Lunghi 
431d4bd3053SNicola Lunghi 	return -ENODEV;
432d4bd3053SNicola Lunghi }
433d4bd3053SNicola Lunghi 
43493db51d0STakashi Iwai /* 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)43593db51d0STakashi Iwai static bool check_valid_altsetting_v2v3(struct snd_usb_audio *chip, int iface,
43693db51d0STakashi Iwai 					int altsetting)
43793db51d0STakashi Iwai {
43893db51d0STakashi Iwai 	struct usb_device *dev = chip->dev;
43993db51d0STakashi Iwai 	__le64 raw_data = 0;
44093db51d0STakashi Iwai 	u64 data;
44193db51d0STakashi Iwai 	int err;
44293db51d0STakashi Iwai 
44393db51d0STakashi Iwai 	/* we assume 64bit is enough for any altsettings */
44493db51d0STakashi Iwai 	if (snd_BUG_ON(altsetting >= 64 - 8))
44593db51d0STakashi Iwai 		return false;
44693db51d0STakashi Iwai 
44751cb8e20SJohan Hovold 	err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
44893db51d0STakashi Iwai 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
44993db51d0STakashi Iwai 			      UAC2_AS_VAL_ALT_SETTINGS << 8,
45093db51d0STakashi Iwai 			      iface, &raw_data, sizeof(raw_data));
45193db51d0STakashi Iwai 	if (err < 0)
45293db51d0STakashi Iwai 		return false;
45393db51d0STakashi Iwai 
45493db51d0STakashi Iwai 	data = le64_to_cpu(raw_data);
45593db51d0STakashi Iwai 	/* first byte contains the bitmap size */
45693db51d0STakashi Iwai 	if ((data & 0xff) * 8 < altsetting)
45793db51d0STakashi Iwai 		return false;
45893db51d0STakashi Iwai 	if (data & (1ULL << (altsetting + 8)))
45993db51d0STakashi Iwai 		return true;
46093db51d0STakashi Iwai 
46193db51d0STakashi Iwai 	return false;
46293db51d0STakashi Iwai }
46393db51d0STakashi Iwai 
46493db51d0STakashi Iwai /*
46593db51d0STakashi Iwai  * Validate each sample rate with the altsetting
46693db51d0STakashi Iwai  * Rebuild the rate table if only partial values are valid
46793db51d0STakashi Iwai  */
validate_sample_rate_table_v2v3(struct snd_usb_audio * chip,struct audioformat * fp,int clock)46893db51d0STakashi Iwai static int validate_sample_rate_table_v2v3(struct snd_usb_audio *chip,
46993db51d0STakashi Iwai 					   struct audioformat *fp,
47093db51d0STakashi Iwai 					   int clock)
47193db51d0STakashi Iwai {
47293db51d0STakashi Iwai 	struct usb_device *dev = chip->dev;
473*ee277704SAlexander Tsoy 	struct usb_host_interface *alts;
47493db51d0STakashi Iwai 	unsigned int *table;
47593db51d0STakashi Iwai 	unsigned int nr_rates;
47693db51d0STakashi Iwai 	int i, err;
477*ee277704SAlexander Tsoy 	u32 bmControls;
47893db51d0STakashi Iwai 
479fe773b87STakashi Iwai 	/* performing the rate verification may lead to unexpected USB bus
480fe773b87STakashi Iwai 	 * behavior afterwards by some unknown reason.  Do this only for the
481fe773b87STakashi Iwai 	 * known devices.
482fe773b87STakashi Iwai 	 */
4838bfe17adSTakashi Iwai 	if (!(chip->quirk_flags & QUIRK_FLAG_VALIDATE_RATES))
484fe773b87STakashi Iwai 		return 0; /* don't perform the validation as default */
485fe773b87STakashi Iwai 
486*ee277704SAlexander Tsoy 	alts = snd_usb_get_host_interface(chip, fp->iface, fp->altsetting);
487*ee277704SAlexander Tsoy 	if (!alts)
488*ee277704SAlexander Tsoy 		return 0;
489*ee277704SAlexander Tsoy 
490*ee277704SAlexander Tsoy 	if (fp->protocol == UAC_VERSION_3) {
491*ee277704SAlexander Tsoy 		struct uac3_as_header_descriptor *as = snd_usb_find_csint_desc(
492*ee277704SAlexander Tsoy 				alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
493*ee277704SAlexander Tsoy 		bmControls = le32_to_cpu(as->bmControls);
494*ee277704SAlexander Tsoy 	} else {
495*ee277704SAlexander Tsoy 		struct uac2_as_header_descriptor *as = snd_usb_find_csint_desc(
496*ee277704SAlexander Tsoy 				alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
497*ee277704SAlexander Tsoy 		bmControls = as->bmControls;
498*ee277704SAlexander Tsoy 	}
499*ee277704SAlexander Tsoy 
500*ee277704SAlexander Tsoy 	if (!uac_v2v3_control_is_readable(bmControls,
501*ee277704SAlexander Tsoy 				UAC2_AS_VAL_ALT_SETTINGS))
502*ee277704SAlexander Tsoy 		return 0;
503*ee277704SAlexander Tsoy 
50493db51d0STakashi Iwai 	table = kcalloc(fp->nr_rates, sizeof(*table), GFP_KERNEL);
50593db51d0STakashi Iwai 	if (!table)
50693db51d0STakashi Iwai 		return -ENOMEM;
50793db51d0STakashi Iwai 
50893db51d0STakashi Iwai 	/* clear the interface altsetting at first */
50993db51d0STakashi Iwai 	usb_set_interface(dev, fp->iface, 0);
51093db51d0STakashi Iwai 
51193db51d0STakashi Iwai 	nr_rates = 0;
51293db51d0STakashi Iwai 	for (i = 0; i < fp->nr_rates; i++) {
51393db51d0STakashi Iwai 		err = snd_usb_set_sample_rate_v2v3(chip, fp, clock,
51493db51d0STakashi Iwai 						   fp->rate_table[i]);
51593db51d0STakashi Iwai 		if (err < 0)
51693db51d0STakashi Iwai 			continue;
51793db51d0STakashi Iwai 
51813ee0336STakashi Iwai 		if (check_valid_altsetting_v2v3(chip, fp->iface, fp->altsetting))
51993db51d0STakashi Iwai 			table[nr_rates++] = fp->rate_table[i];
52093db51d0STakashi Iwai 	}
52193db51d0STakashi Iwai 
52293db51d0STakashi Iwai 	if (!nr_rates) {
52393db51d0STakashi Iwai 		usb_audio_dbg(chip,
52493db51d0STakashi Iwai 			      "No valid sample rate available for %d:%d, assuming a firmware bug\n",
52593db51d0STakashi Iwai 			      fp->iface, fp->altsetting);
52693db51d0STakashi Iwai 		nr_rates = fp->nr_rates; /* continue as is */
52793db51d0STakashi Iwai 	}
52893db51d0STakashi Iwai 
52993db51d0STakashi Iwai 	if (fp->nr_rates == nr_rates) {
53093db51d0STakashi Iwai 		kfree(table);
53193db51d0STakashi Iwai 		return 0;
53293db51d0STakashi Iwai 	}
53393db51d0STakashi Iwai 
53493db51d0STakashi Iwai 	kfree(fp->rate_table);
53593db51d0STakashi Iwai 	fp->rate_table = table;
53693db51d0STakashi Iwai 	fp->nr_rates = nr_rates;
53793db51d0STakashi Iwai 	return 0;
53893db51d0STakashi Iwai }
53993db51d0STakashi Iwai 
54067c10366SDaniel Mack /*
541e5779998SDaniel Mack  * parse the format descriptor and stores the possible sample rates
5429a2fe9b8SRuslan Bilovol  * on the audioformat table (audio class v2 and v3).
543e5779998SDaniel Mack  */
parse_audio_format_rates_v2v3(struct snd_usb_audio * chip,struct audioformat * fp)5449a2fe9b8SRuslan Bilovol static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,
5453d8d4dcfSDaniel Mack 				       struct audioformat *fp)
546e5779998SDaniel Mack {
547e5779998SDaniel Mack 	struct usb_device *dev = chip->dev;
548e5779998SDaniel Mack 	unsigned char tmp[2], *data;
549d4bd3053SNicola Lunghi 	int nr_triplets, data_size, ret = 0, ret_l6;
5509f35a312SAlexander Tsoy 	int clock = snd_usb_clock_find_source(chip, fp, false);
551e5779998SDaniel Mack 
552d07140baSDaniel Mack 	if (clock < 0) {
5530ba41d91STakashi Iwai 		dev_err(&dev->dev,
5540ba41d91STakashi Iwai 			"%s(): unable to find clock source (clock %d)\n",
555d07140baSDaniel Mack 				__func__, clock);
556d07140baSDaniel Mack 		goto err;
557d07140baSDaniel Mack 	}
558d07140baSDaniel Mack 
559e5779998SDaniel Mack 	/* get the number of sample rates first by only fetching 2 bytes */
560e5779998SDaniel Mack 	ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
561e5779998SDaniel Mack 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
56211bcbc44SDaniel Mack 			      UAC2_CS_CONTROL_SAM_FREQ << 8,
56311bcbc44SDaniel Mack 			      snd_usb_ctrl_intf(chip) | (clock << 8),
56417d900c4SClemens Ladisch 			      tmp, sizeof(tmp));
565e5779998SDaniel Mack 
566e5779998SDaniel Mack 	if (ret < 0) {
567d4bd3053SNicola Lunghi 		/* line6 helix devices don't support UAC2_CS_CONTROL_SAM_FREQ call */
568d4bd3053SNicola Lunghi 		ret_l6 = line6_parse_audio_format_rates_quirk(chip, fp);
569d4bd3053SNicola Lunghi 		if (ret_l6 == -ENODEV) {
570d4bd3053SNicola Lunghi 			/* no line6 device found continue showing the error */
5710ba41d91STakashi Iwai 			dev_err(&dev->dev,
5720ba41d91STakashi Iwai 				"%s(): unable to retrieve number of sample rates (clock %d)\n",
57379f920fbSDaniel Mack 				__func__, clock);
574e5779998SDaniel Mack 			goto err;
575e5779998SDaniel Mack 		}
576d4bd3053SNicola Lunghi 		if (ret_l6 == 0) {
577d4bd3053SNicola Lunghi 			dev_info(&dev->dev,
578d4bd3053SNicola Lunghi 				"%s(): unable to retrieve number of sample rates: set it to a predefined value (clock %d).\n",
579d4bd3053SNicola Lunghi 				__func__, clock);
580d4bd3053SNicola Lunghi 			return 0;
581d4bd3053SNicola Lunghi 		}
582d4bd3053SNicola Lunghi 		ret = ret_l6;
583d4bd3053SNicola Lunghi 		goto err;
584d4bd3053SNicola Lunghi 	}
585e5779998SDaniel Mack 
58667c10366SDaniel Mack 	nr_triplets = (tmp[1] << 8) | tmp[0];
58767c10366SDaniel Mack 	data_size = 2 + 12 * nr_triplets;
588e5779998SDaniel Mack 	data = kzalloc(data_size, GFP_KERNEL);
589e5779998SDaniel Mack 	if (!data) {
590e5779998SDaniel Mack 		ret = -ENOMEM;
591e5779998SDaniel Mack 		goto err;
592e5779998SDaniel Mack 	}
593e5779998SDaniel Mack 
594e5779998SDaniel Mack 	/* now get the full information */
595e5779998SDaniel Mack 	ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
596e5779998SDaniel Mack 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
59711bcbc44SDaniel Mack 			      UAC2_CS_CONTROL_SAM_FREQ << 8,
59811bcbc44SDaniel Mack 			      snd_usb_ctrl_intf(chip) | (clock << 8),
59917d900c4SClemens Ladisch 			      data, data_size);
600e5779998SDaniel Mack 
601e5779998SDaniel Mack 	if (ret < 0) {
6020ba41d91STakashi Iwai 		dev_err(&dev->dev,
6030ba41d91STakashi Iwai 			"%s(): unable to retrieve sample rate range (clock %d)\n",
60479f920fbSDaniel Mack 				__func__, clock);
605e5779998SDaniel Mack 		ret = -EINVAL;
606e5779998SDaniel Mack 		goto err_free;
607e5779998SDaniel Mack 	}
608e5779998SDaniel Mack 
60967c10366SDaniel Mack 	/* Call the triplet parser, and make sure fp->rate_table is NULL.
61067c10366SDaniel Mack 	 * We just use the return value to know how many sample rates we
61167c10366SDaniel Mack 	 * will have to deal with. */
61267c10366SDaniel Mack 	kfree(fp->rate_table);
61367c10366SDaniel Mack 	fp->rate_table = NULL;
6140ba41d91STakashi Iwai 	fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
61567c10366SDaniel Mack 
61667c10366SDaniel Mack 	if (fp->nr_rates == 0) {
61767c10366SDaniel Mack 		/* SNDRV_PCM_RATE_CONTINUOUS */
61867c10366SDaniel Mack 		ret = 0;
61967c10366SDaniel Mack 		goto err_free;
62067c10366SDaniel Mack 	}
62167c10366SDaniel Mack 
6226da2ec56SKees Cook 	fp->rate_table = kmalloc_array(fp->nr_rates, sizeof(int), GFP_KERNEL);
623e5779998SDaniel Mack 	if (!fp->rate_table) {
624e5779998SDaniel Mack 		ret = -ENOMEM;
625e5779998SDaniel Mack 		goto err_free;
626e5779998SDaniel Mack 	}
627e5779998SDaniel Mack 
62867c10366SDaniel Mack 	/* Call the triplet parser again, but this time, fp->rate_table is
62967c10366SDaniel Mack 	 * allocated, so the rates will be stored */
6300ba41d91STakashi Iwai 	parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
631e5779998SDaniel Mack 
63293db51d0STakashi Iwai 	ret = validate_sample_rate_table_v2v3(chip, fp, clock);
63313ee0336STakashi Iwai 	if (ret < 0)
63413ee0336STakashi Iwai 		goto err_free;
63513ee0336STakashi Iwai 
63613ee0336STakashi Iwai 	set_rate_table_min_max(fp);
63793db51d0STakashi Iwai 
638e5779998SDaniel Mack err_free:
639e5779998SDaniel Mack 	kfree(data);
640e5779998SDaniel Mack err:
641e5779998SDaniel Mack 	return ret;
642e5779998SDaniel Mack }
643e5779998SDaniel Mack 
644e5779998SDaniel Mack /*
645e5779998SDaniel Mack  * parse the format type I and III descriptors
646e5779998SDaniel Mack  */
parse_audio_format_i(struct snd_usb_audio * chip,struct audioformat * fp,u64 format,void * _fmt)647e5779998SDaniel Mack static int parse_audio_format_i(struct snd_usb_audio *chip,
6489a2fe9b8SRuslan Bilovol 				struct audioformat *fp, u64 format,
6499a2fe9b8SRuslan Bilovol 				void *_fmt)
650e5779998SDaniel Mack {
65174c34ca1SEldad Zack 	snd_pcm_format_t pcm_format;
6529a2fe9b8SRuslan Bilovol 	unsigned int fmt_type;
65374c34ca1SEldad Zack 	int ret;
654e5779998SDaniel Mack 
6559a2fe9b8SRuslan Bilovol 	switch (fp->protocol) {
6569a2fe9b8SRuslan Bilovol 	default:
6579a2fe9b8SRuslan Bilovol 	case UAC_VERSION_1:
6589a2fe9b8SRuslan Bilovol 	case UAC_VERSION_2: {
6599a2fe9b8SRuslan Bilovol 		struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
6609a2fe9b8SRuslan Bilovol 
6619a2fe9b8SRuslan Bilovol 		fmt_type = fmt->bFormatType;
6629a2fe9b8SRuslan Bilovol 		break;
6639a2fe9b8SRuslan Bilovol 	}
6649a2fe9b8SRuslan Bilovol 	case UAC_VERSION_3: {
6659a2fe9b8SRuslan Bilovol 		/* fp->fmt_type is already set in this case */
6669a2fe9b8SRuslan Bilovol 		fmt_type = fp->fmt_type;
6679a2fe9b8SRuslan Bilovol 		break;
6689a2fe9b8SRuslan Bilovol 	}
6699a2fe9b8SRuslan Bilovol 	}
6709a2fe9b8SRuslan Bilovol 
6719a2fe9b8SRuslan Bilovol 	if (fmt_type == UAC_FORMAT_TYPE_III) {
672e5779998SDaniel Mack 		/* FIXME: the format type is really IECxxx
673e5779998SDaniel Mack 		 *        but we give normal PCM format to get the existing
674e5779998SDaniel Mack 		 *        apps working...
675e5779998SDaniel Mack 		 */
676e5779998SDaniel Mack 		switch (chip->usb_id) {
677e5779998SDaniel Mack 
678e5779998SDaniel Mack 		case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
679e5779998SDaniel Mack 			if (chip->setup == 0x00 &&
680e5779998SDaniel Mack 			    fp->altsetting == 6)
681e5779998SDaniel Mack 				pcm_format = SNDRV_PCM_FORMAT_S16_BE;
682e5779998SDaniel Mack 			else
683e5779998SDaniel Mack 				pcm_format = SNDRV_PCM_FORMAT_S16_LE;
684e5779998SDaniel Mack 			break;
685e5779998SDaniel Mack 		default:
686e5779998SDaniel Mack 			pcm_format = SNDRV_PCM_FORMAT_S16_LE;
687e5779998SDaniel Mack 		}
68874c34ca1SEldad Zack 		fp->formats = pcm_format_to_bits(pcm_format);
689e5779998SDaniel Mack 	} else {
6909a2fe9b8SRuslan Bilovol 		fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt);
69129088fefSClemens Ladisch 		if (!fp->formats)
6928ad10dc6SSachin Kamat 			return -EINVAL;
693e5779998SDaniel Mack 	}
694e5779998SDaniel Mack 
695e5779998SDaniel Mack 	/* gather possible sample rates */
696e5779998SDaniel Mack 	/* audio class v1 reports possible sample rates as part of the
697e5779998SDaniel Mack 	 * proprietary class specific descriptor.
698e5779998SDaniel Mack 	 * audio class v2 uses class specific EP0 range requests for that.
699e5779998SDaniel Mack 	 */
7008f898e92SClemens Ladisch 	switch (fp->protocol) {
701a2acad82SClemens Ladisch 	default:
7029a2fe9b8SRuslan Bilovol 	case UAC_VERSION_1: {
7039a2fe9b8SRuslan Bilovol 		struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
7049a2fe9b8SRuslan Bilovol 
705e5779998SDaniel Mack 		fp->channels = fmt->bNrChannels;
70674754f97SDaniel Mack 		ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
707e5779998SDaniel Mack 		break;
7089a2fe9b8SRuslan Bilovol 	}
709e5779998SDaniel Mack 	case UAC_VERSION_2:
7109a2fe9b8SRuslan Bilovol 	case UAC_VERSION_3: {
711e5779998SDaniel Mack 		/* fp->channels is already set in this case */
7129a2fe9b8SRuslan Bilovol 		ret = parse_audio_format_rates_v2v3(chip, fp);
713e5779998SDaniel Mack 		break;
714e5779998SDaniel Mack 	}
7159a2fe9b8SRuslan Bilovol 	}
716e5779998SDaniel Mack 
717e5779998SDaniel Mack 	if (fp->channels < 1) {
7180ba41d91STakashi Iwai 		usb_audio_err(chip,
7190ba41d91STakashi Iwai 			"%u:%d : invalid channels %d\n",
7200ba41d91STakashi Iwai 			fp->iface, fp->altsetting, fp->channels);
7218ad10dc6SSachin Kamat 		return -EINVAL;
722e5779998SDaniel Mack 	}
723e5779998SDaniel Mack 
724e5779998SDaniel Mack 	return ret;
725e5779998SDaniel Mack }
726e5779998SDaniel Mack 
727e5779998SDaniel Mack /*
728e5779998SDaniel Mack  * parse the format type II descriptor
729e5779998SDaniel Mack  */
parse_audio_format_ii(struct snd_usb_audio * chip,struct audioformat * fp,u64 format,void * _fmt)730e5779998SDaniel Mack static int parse_audio_format_ii(struct snd_usb_audio *chip,
731e5779998SDaniel Mack 				 struct audioformat *fp,
7329a2fe9b8SRuslan Bilovol 				 u64 format, void *_fmt)
733e5779998SDaniel Mack {
734e5779998SDaniel Mack 	int brate, framesize, ret;
735e5779998SDaniel Mack 
736e5779998SDaniel Mack 	switch (format) {
737e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_II_AC3:
738e5779998SDaniel Mack 		/* FIXME: there is no AC3 format defined yet */
739015eb0b0SClemens Ladisch 		// fp->formats = SNDRV_PCM_FMTBIT_AC3;
740015eb0b0SClemens Ladisch 		fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
741e5779998SDaniel Mack 		break;
742e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_II_MPEG:
743015eb0b0SClemens Ladisch 		fp->formats = SNDRV_PCM_FMTBIT_MPEG;
744e5779998SDaniel Mack 		break;
745e5779998SDaniel Mack 	default:
7460ba41d91STakashi Iwai 		usb_audio_info(chip,
7479a2fe9b8SRuslan Bilovol 			 "%u:%d : unknown format tag %#llx is detected.  processed as MPEG.\n",
7480ba41d91STakashi Iwai 			 fp->iface, fp->altsetting, format);
749015eb0b0SClemens Ladisch 		fp->formats = SNDRV_PCM_FMTBIT_MPEG;
750e5779998SDaniel Mack 		break;
751e5779998SDaniel Mack 	}
752e5779998SDaniel Mack 
753e5779998SDaniel Mack 	fp->channels = 1;
754e5779998SDaniel Mack 
7558f898e92SClemens Ladisch 	switch (fp->protocol) {
756a2acad82SClemens Ladisch 	default:
757e5779998SDaniel Mack 	case UAC_VERSION_1: {
758e5779998SDaniel Mack 		struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
759e5779998SDaniel Mack 		brate = le16_to_cpu(fmt->wMaxBitRate);
760e5779998SDaniel Mack 		framesize = le16_to_cpu(fmt->wSamplesPerFrame);
7610ba41d91STakashi Iwai 		usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
762e5779998SDaniel Mack 		fp->frame_size = framesize;
763e5779998SDaniel Mack 		ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
764e5779998SDaniel Mack 		break;
765e5779998SDaniel Mack 	}
766e5779998SDaniel Mack 	case UAC_VERSION_2: {
767e5779998SDaniel Mack 		struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
768e5779998SDaniel Mack 		brate = le16_to_cpu(fmt->wMaxBitRate);
769e5779998SDaniel Mack 		framesize = le16_to_cpu(fmt->wSamplesPerFrame);
7700ba41d91STakashi Iwai 		usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
771e5779998SDaniel Mack 		fp->frame_size = framesize;
7729a2fe9b8SRuslan Bilovol 		ret = parse_audio_format_rates_v2v3(chip, fp);
773e5779998SDaniel Mack 		break;
774e5779998SDaniel Mack 	}
775e5779998SDaniel Mack 	}
776e5779998SDaniel Mack 
777e5779998SDaniel Mack 	return ret;
778e5779998SDaniel Mack }
779e5779998SDaniel Mack 
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)7802fcdb06dSDaniel Mack int snd_usb_parse_audio_format(struct snd_usb_audio *chip,
7819a2fe9b8SRuslan Bilovol 			       struct audioformat *fp, u64 format,
7822fcdb06dSDaniel Mack 			       struct uac_format_type_i_continuous_descriptor *fmt,
7838f898e92SClemens Ladisch 			       int stream)
784e5779998SDaniel Mack {
785e5779998SDaniel Mack 	int err;
786e5779998SDaniel Mack 
78774754f97SDaniel Mack 	switch (fmt->bFormatType) {
788e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_I:
789e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_III:
7908f898e92SClemens Ladisch 		err = parse_audio_format_i(chip, fp, format, fmt);
791e5779998SDaniel Mack 		break;
792e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_II:
7938f898e92SClemens Ladisch 		err = parse_audio_format_ii(chip, fp, format, fmt);
794e5779998SDaniel Mack 		break;
795e5779998SDaniel Mack 	default:
7960ba41d91STakashi Iwai 		usb_audio_info(chip,
7970ba41d91STakashi Iwai 			 "%u:%d : format type %d is not supported yet\n",
7980ba41d91STakashi Iwai 			 fp->iface, fp->altsetting,
79974754f97SDaniel Mack 			 fmt->bFormatType);
8008d091242SDaniel Mack 		return -ENOTSUPP;
801e5779998SDaniel Mack 	}
80274754f97SDaniel Mack 	fp->fmt_type = fmt->bFormatType;
803e5779998SDaniel Mack 	if (err < 0)
804e5779998SDaniel Mack 		return err;
805e5779998SDaniel Mack #if 1
806e5779998SDaniel Mack 	/* FIXME: temporary hack for extigy/audigy 2 nx/zs */
807e5779998SDaniel Mack 	/* extigy apparently supports sample rates other than 48k
808e5779998SDaniel Mack 	 * but not in ordinary way.  so we enable only 48k atm.
809e5779998SDaniel Mack 	 */
810e5779998SDaniel Mack 	if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
811e5779998SDaniel Mack 	    chip->usb_id == USB_ID(0x041e, 0x3020) ||
812e5779998SDaniel Mack 	    chip->usb_id == USB_ID(0x041e, 0x3061)) {
81374754f97SDaniel Mack 		if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
814e5779998SDaniel Mack 		    fp->rates != SNDRV_PCM_RATE_48000 &&
815e5779998SDaniel Mack 		    fp->rates != SNDRV_PCM_RATE_96000)
8168d091242SDaniel Mack 			return -ENOTSUPP;
817e5779998SDaniel Mack 	}
818e5779998SDaniel Mack #endif
819e5779998SDaniel Mack 	return 0;
820e5779998SDaniel Mack }
821e5779998SDaniel Mack 
snd_usb_parse_audio_format_v3(struct snd_usb_audio * chip,struct audioformat * fp,struct uac3_as_header_descriptor * as,int stream)8229a2fe9b8SRuslan Bilovol int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip,
8239a2fe9b8SRuslan Bilovol 			       struct audioformat *fp,
8249a2fe9b8SRuslan Bilovol 			       struct uac3_as_header_descriptor *as,
8259a2fe9b8SRuslan Bilovol 			       int stream)
8269a2fe9b8SRuslan Bilovol {
8279a2fe9b8SRuslan Bilovol 	u64 format = le64_to_cpu(as->bmFormats);
8289a2fe9b8SRuslan Bilovol 	int err;
8299a2fe9b8SRuslan Bilovol 
8309a2fe9b8SRuslan Bilovol 	/*
8319a2fe9b8SRuslan Bilovol 	 * Type I format bits are D0..D6
8329a2fe9b8SRuslan Bilovol 	 * This test works because type IV is not supported
8339a2fe9b8SRuslan Bilovol 	 */
8349a2fe9b8SRuslan Bilovol 	if (format & 0x7f)
8359a2fe9b8SRuslan Bilovol 		fp->fmt_type = UAC_FORMAT_TYPE_I;
8369a2fe9b8SRuslan Bilovol 	else
8379a2fe9b8SRuslan Bilovol 		fp->fmt_type = UAC_FORMAT_TYPE_III;
8389a2fe9b8SRuslan Bilovol 
8399a2fe9b8SRuslan Bilovol 	err = parse_audio_format_i(chip, fp, format, as);
8409a2fe9b8SRuslan Bilovol 	if (err < 0)
8419a2fe9b8SRuslan Bilovol 		return err;
8429a2fe9b8SRuslan Bilovol 
8439a2fe9b8SRuslan Bilovol 	return 0;
8449a2fe9b8SRuslan Bilovol }
845