xref: /openbmc/linux/sound/usb/format.c (revision 1a59d1b8e05ea6ab45f7e18897de1ef0e6bc3da6)
1*1a59d1b8SThomas 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 
229e5779998SDaniel Mack /*
23067c10366SDaniel Mack  * Helper function to walk the array of sample rate triplets reported by
23167c10366SDaniel Mack  * the device. The problem is that we need to parse whole array first to
23267c10366SDaniel Mack  * get to know how many sample rates we have to expect.
23367c10366SDaniel Mack  * Then fp->rate_table can be allocated and filled.
23467c10366SDaniel Mack  */
2350ba41d91STakashi Iwai static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
2360ba41d91STakashi Iwai 					struct audioformat *fp, int nr_triplets,
23767c10366SDaniel Mack 					const unsigned char *data)
23867c10366SDaniel Mack {
23967c10366SDaniel Mack 	int i, nr_rates = 0;
24067c10366SDaniel Mack 
24167c10366SDaniel Mack 	fp->rates = fp->rate_min = fp->rate_max = 0;
24267c10366SDaniel Mack 
24367c10366SDaniel Mack 	for (i = 0; i < nr_triplets; i++) {
24467c10366SDaniel Mack 		int min = combine_quad(&data[2 + 12 * i]);
24567c10366SDaniel Mack 		int max = combine_quad(&data[6 + 12 * i]);
24667c10366SDaniel Mack 		int res = combine_quad(&data[10 + 12 * i]);
2474fa0e81bSXi Wang 		unsigned int rate;
24867c10366SDaniel Mack 
24967c10366SDaniel Mack 		if ((max < 0) || (min < 0) || (res < 0) || (max < min))
25067c10366SDaniel Mack 			continue;
25167c10366SDaniel Mack 
25267c10366SDaniel Mack 		/*
25367c10366SDaniel Mack 		 * for ranges with res == 1, we announce a continuous sample
25467c10366SDaniel Mack 		 * rate range, and this function should return 0 for no further
25567c10366SDaniel Mack 		 * parsing.
25667c10366SDaniel Mack 		 */
25767c10366SDaniel Mack 		if (res == 1) {
25867c10366SDaniel Mack 			fp->rate_min = min;
25967c10366SDaniel Mack 			fp->rate_max = max;
26067c10366SDaniel Mack 			fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
26167c10366SDaniel Mack 			return 0;
26267c10366SDaniel Mack 		}
26367c10366SDaniel Mack 
26467c10366SDaniel Mack 		for (rate = min; rate <= max; rate += res) {
26567c10366SDaniel Mack 			if (fp->rate_table)
26667c10366SDaniel Mack 				fp->rate_table[nr_rates] = rate;
26767c10366SDaniel Mack 			if (!fp->rate_min || rate < fp->rate_min)
26867c10366SDaniel Mack 				fp->rate_min = rate;
26967c10366SDaniel Mack 			if (!fp->rate_max || rate > fp->rate_max)
27067c10366SDaniel Mack 				fp->rate_max = rate;
27167c10366SDaniel Mack 			fp->rates |= snd_pcm_rate_to_rate_bit(rate);
27267c10366SDaniel Mack 
27367c10366SDaniel Mack 			nr_rates++;
2748866f405SXi Wang 			if (nr_rates >= MAX_NR_RATES) {
2750ba41d91STakashi Iwai 				usb_audio_err(chip, "invalid uac2 rates\n");
2764fa0e81bSXi Wang 				break;
2774fa0e81bSXi Wang 			}
27867c10366SDaniel Mack 
27967c10366SDaniel Mack 			/* avoid endless loop */
28067c10366SDaniel Mack 			if (res == 0)
28167c10366SDaniel Mack 				break;
28267c10366SDaniel Mack 		}
28367c10366SDaniel Mack 	}
28467c10366SDaniel Mack 
28567c10366SDaniel Mack 	return nr_rates;
28667c10366SDaniel Mack }
28767c10366SDaniel Mack 
28867c10366SDaniel Mack /*
289e5779998SDaniel Mack  * parse the format descriptor and stores the possible sample rates
2909a2fe9b8SRuslan Bilovol  * on the audioformat table (audio class v2 and v3).
291e5779998SDaniel Mack  */
2929a2fe9b8SRuslan Bilovol static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,
2933d8d4dcfSDaniel Mack 				       struct audioformat *fp)
294e5779998SDaniel Mack {
295e5779998SDaniel Mack 	struct usb_device *dev = chip->dev;
296e5779998SDaniel Mack 	unsigned char tmp[2], *data;
29767c10366SDaniel Mack 	int nr_triplets, data_size, ret = 0;
2989a2fe9b8SRuslan Bilovol 	int clock = snd_usb_clock_find_source(chip, fp->protocol,
2999a2fe9b8SRuslan Bilovol 					      fp->clock, false);
300e5779998SDaniel Mack 
301d07140baSDaniel Mack 	if (clock < 0) {
3020ba41d91STakashi Iwai 		dev_err(&dev->dev,
3030ba41d91STakashi Iwai 			"%s(): unable to find clock source (clock %d)\n",
304d07140baSDaniel Mack 				__func__, clock);
305d07140baSDaniel Mack 		goto err;
306d07140baSDaniel Mack 	}
307d07140baSDaniel Mack 
308e5779998SDaniel Mack 	/* get the number of sample rates first by only fetching 2 bytes */
309e5779998SDaniel Mack 	ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
310e5779998SDaniel Mack 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
31111bcbc44SDaniel Mack 			      UAC2_CS_CONTROL_SAM_FREQ << 8,
31211bcbc44SDaniel Mack 			      snd_usb_ctrl_intf(chip) | (clock << 8),
31317d900c4SClemens Ladisch 			      tmp, sizeof(tmp));
314e5779998SDaniel Mack 
315e5779998SDaniel Mack 	if (ret < 0) {
3160ba41d91STakashi Iwai 		dev_err(&dev->dev,
3170ba41d91STakashi Iwai 			"%s(): unable to retrieve number of sample rates (clock %d)\n",
31879f920fbSDaniel Mack 				__func__, clock);
319e5779998SDaniel Mack 		goto err;
320e5779998SDaniel Mack 	}
321e5779998SDaniel Mack 
32267c10366SDaniel Mack 	nr_triplets = (tmp[1] << 8) | tmp[0];
32367c10366SDaniel Mack 	data_size = 2 + 12 * nr_triplets;
324e5779998SDaniel Mack 	data = kzalloc(data_size, GFP_KERNEL);
325e5779998SDaniel Mack 	if (!data) {
326e5779998SDaniel Mack 		ret = -ENOMEM;
327e5779998SDaniel Mack 		goto err;
328e5779998SDaniel Mack 	}
329e5779998SDaniel Mack 
330e5779998SDaniel Mack 	/* now get the full information */
331e5779998SDaniel Mack 	ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
332e5779998SDaniel Mack 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
33311bcbc44SDaniel Mack 			      UAC2_CS_CONTROL_SAM_FREQ << 8,
33411bcbc44SDaniel Mack 			      snd_usb_ctrl_intf(chip) | (clock << 8),
33517d900c4SClemens Ladisch 			      data, data_size);
336e5779998SDaniel Mack 
337e5779998SDaniel Mack 	if (ret < 0) {
3380ba41d91STakashi Iwai 		dev_err(&dev->dev,
3390ba41d91STakashi Iwai 			"%s(): unable to retrieve sample rate range (clock %d)\n",
34079f920fbSDaniel Mack 				__func__, clock);
341e5779998SDaniel Mack 		ret = -EINVAL;
342e5779998SDaniel Mack 		goto err_free;
343e5779998SDaniel Mack 	}
344e5779998SDaniel Mack 
34567c10366SDaniel Mack 	/* Call the triplet parser, and make sure fp->rate_table is NULL.
34667c10366SDaniel Mack 	 * We just use the return value to know how many sample rates we
34767c10366SDaniel Mack 	 * will have to deal with. */
34867c10366SDaniel Mack 	kfree(fp->rate_table);
34967c10366SDaniel Mack 	fp->rate_table = NULL;
3500ba41d91STakashi Iwai 	fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
35167c10366SDaniel Mack 
35267c10366SDaniel Mack 	if (fp->nr_rates == 0) {
35367c10366SDaniel Mack 		/* SNDRV_PCM_RATE_CONTINUOUS */
35467c10366SDaniel Mack 		ret = 0;
35567c10366SDaniel Mack 		goto err_free;
35667c10366SDaniel Mack 	}
35767c10366SDaniel Mack 
3586da2ec56SKees Cook 	fp->rate_table = kmalloc_array(fp->nr_rates, sizeof(int), GFP_KERNEL);
359e5779998SDaniel Mack 	if (!fp->rate_table) {
360e5779998SDaniel Mack 		ret = -ENOMEM;
361e5779998SDaniel Mack 		goto err_free;
362e5779998SDaniel Mack 	}
363e5779998SDaniel Mack 
36467c10366SDaniel Mack 	/* Call the triplet parser again, but this time, fp->rate_table is
36567c10366SDaniel Mack 	 * allocated, so the rates will be stored */
3660ba41d91STakashi Iwai 	parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
367e5779998SDaniel Mack 
368e5779998SDaniel Mack err_free:
369e5779998SDaniel Mack 	kfree(data);
370e5779998SDaniel Mack err:
371e5779998SDaniel Mack 	return ret;
372e5779998SDaniel Mack }
373e5779998SDaniel Mack 
374e5779998SDaniel Mack /*
375e5779998SDaniel Mack  * parse the format type I and III descriptors
376e5779998SDaniel Mack  */
377e5779998SDaniel Mack static int parse_audio_format_i(struct snd_usb_audio *chip,
3789a2fe9b8SRuslan Bilovol 				struct audioformat *fp, u64 format,
3799a2fe9b8SRuslan Bilovol 				void *_fmt)
380e5779998SDaniel Mack {
38174c34ca1SEldad Zack 	snd_pcm_format_t pcm_format;
3829a2fe9b8SRuslan Bilovol 	unsigned int fmt_type;
38374c34ca1SEldad Zack 	int ret;
384e5779998SDaniel Mack 
3859a2fe9b8SRuslan Bilovol 	switch (fp->protocol) {
3869a2fe9b8SRuslan Bilovol 	default:
3879a2fe9b8SRuslan Bilovol 	case UAC_VERSION_1:
3889a2fe9b8SRuslan Bilovol 	case UAC_VERSION_2: {
3899a2fe9b8SRuslan Bilovol 		struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
3909a2fe9b8SRuslan Bilovol 
3919a2fe9b8SRuslan Bilovol 		fmt_type = fmt->bFormatType;
3929a2fe9b8SRuslan Bilovol 		break;
3939a2fe9b8SRuslan Bilovol 	}
3949a2fe9b8SRuslan Bilovol 	case UAC_VERSION_3: {
3959a2fe9b8SRuslan Bilovol 		/* fp->fmt_type is already set in this case */
3969a2fe9b8SRuslan Bilovol 		fmt_type = fp->fmt_type;
3979a2fe9b8SRuslan Bilovol 		break;
3989a2fe9b8SRuslan Bilovol 	}
3999a2fe9b8SRuslan Bilovol 	}
4009a2fe9b8SRuslan Bilovol 
4019a2fe9b8SRuslan Bilovol 	if (fmt_type == UAC_FORMAT_TYPE_III) {
402e5779998SDaniel Mack 		/* FIXME: the format type is really IECxxx
403e5779998SDaniel Mack 		 *        but we give normal PCM format to get the existing
404e5779998SDaniel Mack 		 *        apps working...
405e5779998SDaniel Mack 		 */
406e5779998SDaniel Mack 		switch (chip->usb_id) {
407e5779998SDaniel Mack 
408e5779998SDaniel Mack 		case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
409e5779998SDaniel Mack 			if (chip->setup == 0x00 &&
410e5779998SDaniel Mack 			    fp->altsetting == 6)
411e5779998SDaniel Mack 				pcm_format = SNDRV_PCM_FORMAT_S16_BE;
412e5779998SDaniel Mack 			else
413e5779998SDaniel Mack 				pcm_format = SNDRV_PCM_FORMAT_S16_LE;
414e5779998SDaniel Mack 			break;
415e5779998SDaniel Mack 		default:
416e5779998SDaniel Mack 			pcm_format = SNDRV_PCM_FORMAT_S16_LE;
417e5779998SDaniel Mack 		}
41874c34ca1SEldad Zack 		fp->formats = pcm_format_to_bits(pcm_format);
419e5779998SDaniel Mack 	} else {
4209a2fe9b8SRuslan Bilovol 		fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt);
42129088fefSClemens Ladisch 		if (!fp->formats)
4228ad10dc6SSachin Kamat 			return -EINVAL;
423e5779998SDaniel Mack 	}
424e5779998SDaniel Mack 
425e5779998SDaniel Mack 	/* gather possible sample rates */
426e5779998SDaniel Mack 	/* audio class v1 reports possible sample rates as part of the
427e5779998SDaniel Mack 	 * proprietary class specific descriptor.
428e5779998SDaniel Mack 	 * audio class v2 uses class specific EP0 range requests for that.
429e5779998SDaniel Mack 	 */
4308f898e92SClemens Ladisch 	switch (fp->protocol) {
431a2acad82SClemens Ladisch 	default:
4329a2fe9b8SRuslan Bilovol 	case UAC_VERSION_1: {
4339a2fe9b8SRuslan Bilovol 		struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
4349a2fe9b8SRuslan Bilovol 
435e5779998SDaniel Mack 		fp->channels = fmt->bNrChannels;
43674754f97SDaniel Mack 		ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
437e5779998SDaniel Mack 		break;
4389a2fe9b8SRuslan Bilovol 	}
439e5779998SDaniel Mack 	case UAC_VERSION_2:
4409a2fe9b8SRuslan Bilovol 	case UAC_VERSION_3: {
441e5779998SDaniel Mack 		/* fp->channels is already set in this case */
4429a2fe9b8SRuslan Bilovol 		ret = parse_audio_format_rates_v2v3(chip, fp);
443e5779998SDaniel Mack 		break;
444e5779998SDaniel Mack 	}
4459a2fe9b8SRuslan Bilovol 	}
446e5779998SDaniel Mack 
447e5779998SDaniel Mack 	if (fp->channels < 1) {
4480ba41d91STakashi Iwai 		usb_audio_err(chip,
4490ba41d91STakashi Iwai 			"%u:%d : invalid channels %d\n",
4500ba41d91STakashi Iwai 			fp->iface, fp->altsetting, fp->channels);
4518ad10dc6SSachin Kamat 		return -EINVAL;
452e5779998SDaniel Mack 	}
453e5779998SDaniel Mack 
454e5779998SDaniel Mack 	return ret;
455e5779998SDaniel Mack }
456e5779998SDaniel Mack 
457e5779998SDaniel Mack /*
458e5779998SDaniel Mack  * parse the format type II descriptor
459e5779998SDaniel Mack  */
460e5779998SDaniel Mack static int parse_audio_format_ii(struct snd_usb_audio *chip,
461e5779998SDaniel Mack 				 struct audioformat *fp,
4629a2fe9b8SRuslan Bilovol 				 u64 format, void *_fmt)
463e5779998SDaniel Mack {
464e5779998SDaniel Mack 	int brate, framesize, ret;
465e5779998SDaniel Mack 
466e5779998SDaniel Mack 	switch (format) {
467e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_II_AC3:
468e5779998SDaniel Mack 		/* FIXME: there is no AC3 format defined yet */
469015eb0b0SClemens Ladisch 		// fp->formats = SNDRV_PCM_FMTBIT_AC3;
470015eb0b0SClemens Ladisch 		fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
471e5779998SDaniel Mack 		break;
472e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_II_MPEG:
473015eb0b0SClemens Ladisch 		fp->formats = SNDRV_PCM_FMTBIT_MPEG;
474e5779998SDaniel Mack 		break;
475e5779998SDaniel Mack 	default:
4760ba41d91STakashi Iwai 		usb_audio_info(chip,
4779a2fe9b8SRuslan Bilovol 			 "%u:%d : unknown format tag %#llx is detected.  processed as MPEG.\n",
4780ba41d91STakashi Iwai 			 fp->iface, fp->altsetting, format);
479015eb0b0SClemens Ladisch 		fp->formats = SNDRV_PCM_FMTBIT_MPEG;
480e5779998SDaniel Mack 		break;
481e5779998SDaniel Mack 	}
482e5779998SDaniel Mack 
483e5779998SDaniel Mack 	fp->channels = 1;
484e5779998SDaniel Mack 
4858f898e92SClemens Ladisch 	switch (fp->protocol) {
486a2acad82SClemens Ladisch 	default:
487e5779998SDaniel Mack 	case UAC_VERSION_1: {
488e5779998SDaniel Mack 		struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
489e5779998SDaniel Mack 		brate = le16_to_cpu(fmt->wMaxBitRate);
490e5779998SDaniel Mack 		framesize = le16_to_cpu(fmt->wSamplesPerFrame);
4910ba41d91STakashi Iwai 		usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
492e5779998SDaniel Mack 		fp->frame_size = framesize;
493e5779998SDaniel Mack 		ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
494e5779998SDaniel Mack 		break;
495e5779998SDaniel Mack 	}
496e5779998SDaniel Mack 	case UAC_VERSION_2: {
497e5779998SDaniel Mack 		struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
498e5779998SDaniel Mack 		brate = le16_to_cpu(fmt->wMaxBitRate);
499e5779998SDaniel Mack 		framesize = le16_to_cpu(fmt->wSamplesPerFrame);
5000ba41d91STakashi Iwai 		usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
501e5779998SDaniel Mack 		fp->frame_size = framesize;
5029a2fe9b8SRuslan Bilovol 		ret = parse_audio_format_rates_v2v3(chip, fp);
503e5779998SDaniel Mack 		break;
504e5779998SDaniel Mack 	}
505e5779998SDaniel Mack 	}
506e5779998SDaniel Mack 
507e5779998SDaniel Mack 	return ret;
508e5779998SDaniel Mack }
509e5779998SDaniel Mack 
5102fcdb06dSDaniel Mack int snd_usb_parse_audio_format(struct snd_usb_audio *chip,
5119a2fe9b8SRuslan Bilovol 			       struct audioformat *fp, u64 format,
5122fcdb06dSDaniel Mack 			       struct uac_format_type_i_continuous_descriptor *fmt,
5138f898e92SClemens Ladisch 			       int stream)
514e5779998SDaniel Mack {
515e5779998SDaniel Mack 	int err;
516e5779998SDaniel Mack 
51774754f97SDaniel Mack 	switch (fmt->bFormatType) {
518e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_I:
519e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_III:
5208f898e92SClemens Ladisch 		err = parse_audio_format_i(chip, fp, format, fmt);
521e5779998SDaniel Mack 		break;
522e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_II:
5238f898e92SClemens Ladisch 		err = parse_audio_format_ii(chip, fp, format, fmt);
524e5779998SDaniel Mack 		break;
525e5779998SDaniel Mack 	default:
5260ba41d91STakashi Iwai 		usb_audio_info(chip,
5270ba41d91STakashi Iwai 			 "%u:%d : format type %d is not supported yet\n",
5280ba41d91STakashi Iwai 			 fp->iface, fp->altsetting,
52974754f97SDaniel Mack 			 fmt->bFormatType);
5308d091242SDaniel Mack 		return -ENOTSUPP;
531e5779998SDaniel Mack 	}
53274754f97SDaniel Mack 	fp->fmt_type = fmt->bFormatType;
533e5779998SDaniel Mack 	if (err < 0)
534e5779998SDaniel Mack 		return err;
535e5779998SDaniel Mack #if 1
536e5779998SDaniel Mack 	/* FIXME: temporary hack for extigy/audigy 2 nx/zs */
537e5779998SDaniel Mack 	/* extigy apparently supports sample rates other than 48k
538e5779998SDaniel Mack 	 * but not in ordinary way.  so we enable only 48k atm.
539e5779998SDaniel Mack 	 */
540e5779998SDaniel Mack 	if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
541e5779998SDaniel Mack 	    chip->usb_id == USB_ID(0x041e, 0x3020) ||
542e5779998SDaniel Mack 	    chip->usb_id == USB_ID(0x041e, 0x3061)) {
54374754f97SDaniel Mack 		if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
544e5779998SDaniel Mack 		    fp->rates != SNDRV_PCM_RATE_48000 &&
545e5779998SDaniel Mack 		    fp->rates != SNDRV_PCM_RATE_96000)
5468d091242SDaniel Mack 			return -ENOTSUPP;
547e5779998SDaniel Mack 	}
548e5779998SDaniel Mack #endif
549e5779998SDaniel Mack 	return 0;
550e5779998SDaniel Mack }
551e5779998SDaniel Mack 
5529a2fe9b8SRuslan Bilovol int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip,
5539a2fe9b8SRuslan Bilovol 			       struct audioformat *fp,
5549a2fe9b8SRuslan Bilovol 			       struct uac3_as_header_descriptor *as,
5559a2fe9b8SRuslan Bilovol 			       int stream)
5569a2fe9b8SRuslan Bilovol {
5579a2fe9b8SRuslan Bilovol 	u64 format = le64_to_cpu(as->bmFormats);
5589a2fe9b8SRuslan Bilovol 	int err;
5599a2fe9b8SRuslan Bilovol 
5609a2fe9b8SRuslan Bilovol 	/*
5619a2fe9b8SRuslan Bilovol 	 * Type I format bits are D0..D6
5629a2fe9b8SRuslan Bilovol 	 * This test works because type IV is not supported
5639a2fe9b8SRuslan Bilovol 	 */
5649a2fe9b8SRuslan Bilovol 	if (format & 0x7f)
5659a2fe9b8SRuslan Bilovol 		fp->fmt_type = UAC_FORMAT_TYPE_I;
5669a2fe9b8SRuslan Bilovol 	else
5679a2fe9b8SRuslan Bilovol 		fp->fmt_type = UAC_FORMAT_TYPE_III;
5689a2fe9b8SRuslan Bilovol 
5699a2fe9b8SRuslan Bilovol 	err = parse_audio_format_i(chip, fp, format, as);
5709a2fe9b8SRuslan Bilovol 	if (err < 0)
5719a2fe9b8SRuslan Bilovol 		return err;
5729a2fe9b8SRuslan Bilovol 
5739a2fe9b8SRuslan Bilovol 	return 0;
5749a2fe9b8SRuslan Bilovol }
575