xref: /openbmc/linux/sound/usb/format.c (revision 6da2ec56059c3c7a7e5f729e6349e74ace1e5c57)
1e5779998SDaniel Mack /*
2e5779998SDaniel Mack  *   This program is free software; you can redistribute it and/or modify
3e5779998SDaniel Mack  *   it under the terms of the GNU General Public License as published by
4e5779998SDaniel Mack  *   the Free Software Foundation; either version 2 of the License, or
5e5779998SDaniel Mack  *   (at your option) any later version.
6e5779998SDaniel Mack  *
7e5779998SDaniel Mack  *   This program is distributed in the hope that it will be useful,
8e5779998SDaniel Mack  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9e5779998SDaniel Mack  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10e5779998SDaniel Mack  *   GNU General Public License for more details.
11e5779998SDaniel Mack  *
12e5779998SDaniel Mack  *   You should have received a copy of the GNU General Public License
13e5779998SDaniel Mack  *   along with this program; if not, write to the Free Software
14e5779998SDaniel Mack  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15e5779998SDaniel Mack  *
16e5779998SDaniel Mack  */
17e5779998SDaniel Mack 
18e5779998SDaniel Mack #include <linux/init.h>
1936db0456SStephen Rothwell #include <linux/slab.h>
20e5779998SDaniel Mack #include <linux/usb.h>
21e5779998SDaniel Mack #include <linux/usb/audio.h>
227e847894SDaniel Mack #include <linux/usb/audio-v2.h>
239a2fe9b8SRuslan Bilovol #include <linux/usb/audio-v3.h>
24e5779998SDaniel Mack 
25e5779998SDaniel Mack #include <sound/core.h>
26e5779998SDaniel Mack #include <sound/pcm.h>
27e5779998SDaniel Mack 
28e5779998SDaniel Mack #include "usbaudio.h"
29e5779998SDaniel Mack #include "card.h"
30e5779998SDaniel Mack #include "quirks.h"
31e5779998SDaniel Mack #include "helper.h"
32e5779998SDaniel Mack #include "debug.h"
3379f920fbSDaniel Mack #include "clock.h"
34ee95cb61SDaniel Mack #include "format.h"
35e5779998SDaniel Mack 
36e5779998SDaniel Mack /*
37e5779998SDaniel Mack  * parse the audio format type I descriptor
38e5779998SDaniel Mack  * and returns the corresponding pcm format
39e5779998SDaniel Mack  *
40e5779998SDaniel Mack  * @dev: usb device
41e5779998SDaniel Mack  * @fp: audioformat record
42e5779998SDaniel Mack  * @format: the format tag (wFormatTag)
439a2fe9b8SRuslan Bilovol  * @fmt: the format type descriptor (v1/v2) or AudioStreaming descriptor (v3)
44e5779998SDaniel Mack  */
4529088fefSClemens Ladisch static u64 parse_audio_format_i_type(struct snd_usb_audio *chip,
46e5779998SDaniel Mack 				     struct audioformat *fp,
479a2fe9b8SRuslan Bilovol 				     u64 format, void *_fmt)
48e5779998SDaniel Mack {
49e5779998SDaniel Mack 	int sample_width, sample_bytes;
50717bfb5fSDaniel Mack 	u64 pcm_formats = 0;
51e5779998SDaniel Mack 
528f898e92SClemens Ladisch 	switch (fp->protocol) {
53a2acad82SClemens Ladisch 	case UAC_VERSION_1:
54a2acad82SClemens Ladisch 	default: {
55e5779998SDaniel Mack 		struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
56e5779998SDaniel Mack 		sample_width = fmt->bBitResolution;
57e5779998SDaniel Mack 		sample_bytes = fmt->bSubframeSize;
58b44d419bSDan Carpenter 		format = 1ULL << format;
59e5779998SDaniel Mack 		break;
60e5779998SDaniel Mack 	}
61e5779998SDaniel Mack 
62e5779998SDaniel Mack 	case UAC_VERSION_2: {
63e5779998SDaniel Mack 		struct uac_format_type_i_ext_descriptor *fmt = _fmt;
64e5779998SDaniel Mack 		sample_width = fmt->bBitResolution;
65e5779998SDaniel Mack 		sample_bytes = fmt->bSubslotSize;
66717bfb5fSDaniel Mack 
67717bfb5fSDaniel Mack 		if (format & UAC2_FORMAT_TYPE_I_RAW_DATA)
68717bfb5fSDaniel Mack 			pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
69717bfb5fSDaniel Mack 
7029088fefSClemens Ladisch 		format <<= 1;
71e5779998SDaniel Mack 		break;
72e5779998SDaniel Mack 	}
739a2fe9b8SRuslan Bilovol 	case UAC_VERSION_3: {
749a2fe9b8SRuslan Bilovol 		struct uac3_as_header_descriptor *as = _fmt;
759a2fe9b8SRuslan Bilovol 
769a2fe9b8SRuslan Bilovol 		sample_width = as->bBitResolution;
779a2fe9b8SRuslan Bilovol 		sample_bytes = as->bSubslotSize;
789a2fe9b8SRuslan Bilovol 
799a2fe9b8SRuslan Bilovol 		if (format & UAC3_FORMAT_TYPE_I_RAW_DATA)
809a2fe9b8SRuslan Bilovol 			pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
819a2fe9b8SRuslan Bilovol 
829a2fe9b8SRuslan Bilovol 		format <<= 1;
839a2fe9b8SRuslan Bilovol 		break;
849a2fe9b8SRuslan Bilovol 	}
85e5779998SDaniel Mack 	}
86e5779998SDaniel Mack 
87717bfb5fSDaniel Mack 	if ((pcm_formats == 0) &&
88717bfb5fSDaniel Mack 	    (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED))) {
8929088fefSClemens Ladisch 		/* some devices don't define this correctly... */
900ba41d91STakashi Iwai 		usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM\n",
910ba41d91STakashi Iwai 			fp->iface, fp->altsetting);
9229088fefSClemens Ladisch 		format = 1 << UAC_FORMAT_TYPE_I_PCM;
9329088fefSClemens Ladisch 	}
9429088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) {
956d1f2f60STakamichi Horikawa 		if (((chip->usb_id == USB_ID(0x0582, 0x0016)) ||
966d1f2f60STakamichi Horikawa 		     /* Edirol SD-90 */
976d1f2f60STakamichi Horikawa 		     (chip->usb_id == USB_ID(0x0582, 0x000c))) &&
986d1f2f60STakamichi Horikawa 		     /* Roland SC-D70 */
99061b869eSClemens Ladisch 		    sample_width == 24 && sample_bytes == 2)
100061b869eSClemens Ladisch 			sample_bytes = 3;
101061b869eSClemens Ladisch 		else if (sample_width > sample_bytes * 8) {
1020ba41d91STakashi Iwai 			usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d\n",
1030ba41d91STakashi Iwai 				 fp->iface, fp->altsetting,
104e5779998SDaniel Mack 				 sample_width, sample_bytes);
105e5779998SDaniel Mack 		}
106e5779998SDaniel Mack 		/* check the format byte size */
107e5779998SDaniel Mack 		switch (sample_bytes) {
108e5779998SDaniel Mack 		case 1:
10929088fefSClemens Ladisch 			pcm_formats |= SNDRV_PCM_FMTBIT_S8;
110e5779998SDaniel Mack 			break;
111e5779998SDaniel Mack 		case 2:
112e5779998SDaniel Mack 			if (snd_usb_is_big_endian_format(chip, fp))
11329088fefSClemens Ladisch 				pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */
114e5779998SDaniel Mack 			else
11529088fefSClemens Ladisch 				pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE;
116e5779998SDaniel Mack 			break;
117e5779998SDaniel Mack 		case 3:
118e5779998SDaniel Mack 			if (snd_usb_is_big_endian_format(chip, fp))
11929088fefSClemens Ladisch 				pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */
120e5779998SDaniel Mack 			else
12129088fefSClemens Ladisch 				pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE;
122e5779998SDaniel Mack 			break;
123e5779998SDaniel Mack 		case 4:
12429088fefSClemens Ladisch 			pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE;
125e5779998SDaniel Mack 			break;
126e5779998SDaniel Mack 		default:
1270ba41d91STakashi Iwai 			usb_audio_info(chip,
1280ba41d91STakashi Iwai 				 "%u:%d : unsupported sample bitwidth %d in %d bytes\n",
1290ba41d91STakashi Iwai 				 fp->iface, fp->altsetting,
130e5779998SDaniel Mack 				 sample_width, sample_bytes);
131e5779998SDaniel Mack 			break;
132e5779998SDaniel Mack 		}
13329088fefSClemens Ladisch 	}
13429088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) {
135e5779998SDaniel Mack 		/* Dallas DS4201 workaround: it advertises U8 format, but really
136e5779998SDaniel Mack 		   supports S8. */
137e5779998SDaniel Mack 		if (chip->usb_id == USB_ID(0x04fa, 0x4201))
13829088fefSClemens Ladisch 			pcm_formats |= SNDRV_PCM_FMTBIT_S8;
13929088fefSClemens Ladisch 		else
14029088fefSClemens Ladisch 			pcm_formats |= SNDRV_PCM_FMTBIT_U8;
141e5779998SDaniel Mack 	}
14229088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) {
14329088fefSClemens Ladisch 		pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
14429088fefSClemens Ladisch 	}
14529088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) {
14629088fefSClemens Ladisch 		pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;
14729088fefSClemens Ladisch 	}
14829088fefSClemens Ladisch 	if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) {
14929088fefSClemens Ladisch 		pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;
15029088fefSClemens Ladisch 	}
15129088fefSClemens Ladisch 	if (format & ~0x3f) {
1520ba41d91STakashi Iwai 		usb_audio_info(chip,
1539a2fe9b8SRuslan Bilovol 			 "%u:%d : unsupported format bits %#llx\n",
1540ba41d91STakashi Iwai 			 fp->iface, fp->altsetting, format);
15529088fefSClemens Ladisch 	}
156126825e7SDaniel Mack 
157126825e7SDaniel Mack 	pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes);
158126825e7SDaniel Mack 
15929088fefSClemens Ladisch 	return pcm_formats;
160e5779998SDaniel Mack }
161e5779998SDaniel Mack 
162e5779998SDaniel Mack 
163e5779998SDaniel Mack /*
164e5779998SDaniel Mack  * parse the format descriptor and stores the possible sample rates
165e5779998SDaniel Mack  * on the audioformat table (audio class v1).
166e5779998SDaniel Mack  *
167e5779998SDaniel Mack  * @dev: usb device
168e5779998SDaniel Mack  * @fp: audioformat record
169e5779998SDaniel Mack  * @fmt: the format descriptor
170e5779998SDaniel Mack  * @offset: the start offset of descriptor pointing the rate type
171e5779998SDaniel Mack  *          (7 for type I and II, 8 for type II)
172e5779998SDaniel Mack  */
173e5779998SDaniel Mack static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
174e5779998SDaniel Mack 				       unsigned char *fmt, int offset)
175e5779998SDaniel Mack {
176e5779998SDaniel Mack 	int nr_rates = fmt[offset];
177e5779998SDaniel Mack 
178e5779998SDaniel Mack 	if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
1790ba41d91STakashi Iwai 		usb_audio_err(chip,
1800ba41d91STakashi Iwai 			"%u:%d : invalid UAC_FORMAT_TYPE desc\n",
1810ba41d91STakashi Iwai 			fp->iface, fp->altsetting);
1828ad10dc6SSachin Kamat 		return -EINVAL;
183e5779998SDaniel Mack 	}
184e5779998SDaniel Mack 
185e5779998SDaniel Mack 	if (nr_rates) {
186e5779998SDaniel Mack 		/*
187e5779998SDaniel Mack 		 * build the rate table and bitmap flags
188e5779998SDaniel Mack 		 */
189e5779998SDaniel Mack 		int r, idx;
190e5779998SDaniel Mack 
191*6da2ec56SKees Cook 		fp->rate_table = kmalloc_array(nr_rates, sizeof(int),
192*6da2ec56SKees Cook 					       GFP_KERNEL);
1931bc00f32SShawn Lin 		if (fp->rate_table == NULL)
1948ad10dc6SSachin Kamat 			return -ENOMEM;
195e5779998SDaniel Mack 
196e5779998SDaniel Mack 		fp->nr_rates = 0;
197e5779998SDaniel Mack 		fp->rate_min = fp->rate_max = 0;
198e5779998SDaniel Mack 		for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
199e5779998SDaniel Mack 			unsigned int rate = combine_triple(&fmt[idx]);
200e5779998SDaniel Mack 			if (!rate)
201e5779998SDaniel Mack 				continue;
202e5779998SDaniel Mack 			/* C-Media CM6501 mislabels its 96 kHz altsetting */
2038129e79eSWolfgang Breyha 			/* Terratec Aureon 7.1 USB C-Media 6206, too */
204e5779998SDaniel Mack 			if (rate == 48000 && nr_rates == 1 &&
205e5779998SDaniel Mack 			    (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
2068129e79eSWolfgang Breyha 			     chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
2078129e79eSWolfgang Breyha 			     chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
208e5779998SDaniel Mack 			    fp->altsetting == 5 && fp->maxpacksize == 392)
209e5779998SDaniel Mack 				rate = 96000;
2108c4b79cfSPavel Hofman 			/* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
2118c4b79cfSPavel Hofman 			if (rate == 16000 &&
2128c4b79cfSPavel Hofman 			    (chip->usb_id == USB_ID(0x041e, 0x4064) ||
2138c4b79cfSPavel Hofman 			     chip->usb_id == USB_ID(0x041e, 0x4068)))
214e5779998SDaniel Mack 				rate = 8000;
215e5779998SDaniel Mack 
216e5779998SDaniel Mack 			fp->rate_table[fp->nr_rates] = rate;
217e5779998SDaniel Mack 			if (!fp->rate_min || rate < fp->rate_min)
218e5779998SDaniel Mack 				fp->rate_min = rate;
219e5779998SDaniel Mack 			if (!fp->rate_max || rate > fp->rate_max)
220e5779998SDaniel Mack 				fp->rate_max = rate;
221e5779998SDaniel Mack 			fp->rates |= snd_pcm_rate_to_rate_bit(rate);
222e5779998SDaniel Mack 			fp->nr_rates++;
223e5779998SDaniel Mack 		}
224e5779998SDaniel Mack 		if (!fp->nr_rates) {
225e5779998SDaniel Mack 			hwc_debug("All rates were zero. Skipping format!\n");
2268ad10dc6SSachin Kamat 			return -EINVAL;
227e5779998SDaniel Mack 		}
228e5779998SDaniel Mack 	} else {
229e5779998SDaniel Mack 		/* continuous rates */
230e5779998SDaniel Mack 		fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
231e5779998SDaniel Mack 		fp->rate_min = combine_triple(&fmt[offset + 1]);
232e5779998SDaniel Mack 		fp->rate_max = combine_triple(&fmt[offset + 4]);
233e5779998SDaniel Mack 	}
234e5779998SDaniel Mack 	return 0;
235e5779998SDaniel Mack }
236e5779998SDaniel Mack 
237e5779998SDaniel Mack /*
23867c10366SDaniel Mack  * Helper function to walk the array of sample rate triplets reported by
23967c10366SDaniel Mack  * the device. The problem is that we need to parse whole array first to
24067c10366SDaniel Mack  * get to know how many sample rates we have to expect.
24167c10366SDaniel Mack  * Then fp->rate_table can be allocated and filled.
24267c10366SDaniel Mack  */
2430ba41d91STakashi Iwai static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
2440ba41d91STakashi Iwai 					struct audioformat *fp, int nr_triplets,
24567c10366SDaniel Mack 					const unsigned char *data)
24667c10366SDaniel Mack {
24767c10366SDaniel Mack 	int i, nr_rates = 0;
24867c10366SDaniel Mack 
24967c10366SDaniel Mack 	fp->rates = fp->rate_min = fp->rate_max = 0;
25067c10366SDaniel Mack 
25167c10366SDaniel Mack 	for (i = 0; i < nr_triplets; i++) {
25267c10366SDaniel Mack 		int min = combine_quad(&data[2 + 12 * i]);
25367c10366SDaniel Mack 		int max = combine_quad(&data[6 + 12 * i]);
25467c10366SDaniel Mack 		int res = combine_quad(&data[10 + 12 * i]);
2554fa0e81bSXi Wang 		unsigned int rate;
25667c10366SDaniel Mack 
25767c10366SDaniel Mack 		if ((max < 0) || (min < 0) || (res < 0) || (max < min))
25867c10366SDaniel Mack 			continue;
25967c10366SDaniel Mack 
26067c10366SDaniel Mack 		/*
26167c10366SDaniel Mack 		 * for ranges with res == 1, we announce a continuous sample
26267c10366SDaniel Mack 		 * rate range, and this function should return 0 for no further
26367c10366SDaniel Mack 		 * parsing.
26467c10366SDaniel Mack 		 */
26567c10366SDaniel Mack 		if (res == 1) {
26667c10366SDaniel Mack 			fp->rate_min = min;
26767c10366SDaniel Mack 			fp->rate_max = max;
26867c10366SDaniel Mack 			fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
26967c10366SDaniel Mack 			return 0;
27067c10366SDaniel Mack 		}
27167c10366SDaniel Mack 
27267c10366SDaniel Mack 		for (rate = min; rate <= max; rate += res) {
27367c10366SDaniel Mack 			if (fp->rate_table)
27467c10366SDaniel Mack 				fp->rate_table[nr_rates] = rate;
27567c10366SDaniel Mack 			if (!fp->rate_min || rate < fp->rate_min)
27667c10366SDaniel Mack 				fp->rate_min = rate;
27767c10366SDaniel Mack 			if (!fp->rate_max || rate > fp->rate_max)
27867c10366SDaniel Mack 				fp->rate_max = rate;
27967c10366SDaniel Mack 			fp->rates |= snd_pcm_rate_to_rate_bit(rate);
28067c10366SDaniel Mack 
28167c10366SDaniel Mack 			nr_rates++;
2828866f405SXi Wang 			if (nr_rates >= MAX_NR_RATES) {
2830ba41d91STakashi Iwai 				usb_audio_err(chip, "invalid uac2 rates\n");
2844fa0e81bSXi Wang 				break;
2854fa0e81bSXi Wang 			}
28667c10366SDaniel Mack 
28767c10366SDaniel Mack 			/* avoid endless loop */
28867c10366SDaniel Mack 			if (res == 0)
28967c10366SDaniel Mack 				break;
29067c10366SDaniel Mack 		}
29167c10366SDaniel Mack 	}
29267c10366SDaniel Mack 
29367c10366SDaniel Mack 	return nr_rates;
29467c10366SDaniel Mack }
29567c10366SDaniel Mack 
29667c10366SDaniel Mack /*
297e5779998SDaniel Mack  * parse the format descriptor and stores the possible sample rates
2989a2fe9b8SRuslan Bilovol  * on the audioformat table (audio class v2 and v3).
299e5779998SDaniel Mack  */
3009a2fe9b8SRuslan Bilovol static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,
3013d8d4dcfSDaniel Mack 				       struct audioformat *fp)
302e5779998SDaniel Mack {
303e5779998SDaniel Mack 	struct usb_device *dev = chip->dev;
304e5779998SDaniel Mack 	unsigned char tmp[2], *data;
30567c10366SDaniel Mack 	int nr_triplets, data_size, ret = 0;
3069a2fe9b8SRuslan Bilovol 	int clock = snd_usb_clock_find_source(chip, fp->protocol,
3079a2fe9b8SRuslan Bilovol 					      fp->clock, false);
308e5779998SDaniel Mack 
309d07140baSDaniel Mack 	if (clock < 0) {
3100ba41d91STakashi Iwai 		dev_err(&dev->dev,
3110ba41d91STakashi Iwai 			"%s(): unable to find clock source (clock %d)\n",
312d07140baSDaniel Mack 				__func__, clock);
313d07140baSDaniel Mack 		goto err;
314d07140baSDaniel Mack 	}
315d07140baSDaniel Mack 
316e5779998SDaniel Mack 	/* get the number of sample rates first by only fetching 2 bytes */
317e5779998SDaniel Mack 	ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
318e5779998SDaniel Mack 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
31911bcbc44SDaniel Mack 			      UAC2_CS_CONTROL_SAM_FREQ << 8,
32011bcbc44SDaniel Mack 			      snd_usb_ctrl_intf(chip) | (clock << 8),
32117d900c4SClemens Ladisch 			      tmp, sizeof(tmp));
322e5779998SDaniel Mack 
323e5779998SDaniel Mack 	if (ret < 0) {
3240ba41d91STakashi Iwai 		dev_err(&dev->dev,
3250ba41d91STakashi Iwai 			"%s(): unable to retrieve number of sample rates (clock %d)\n",
32679f920fbSDaniel Mack 				__func__, clock);
327e5779998SDaniel Mack 		goto err;
328e5779998SDaniel Mack 	}
329e5779998SDaniel Mack 
33067c10366SDaniel Mack 	nr_triplets = (tmp[1] << 8) | tmp[0];
33167c10366SDaniel Mack 	data_size = 2 + 12 * nr_triplets;
332e5779998SDaniel Mack 	data = kzalloc(data_size, GFP_KERNEL);
333e5779998SDaniel Mack 	if (!data) {
334e5779998SDaniel Mack 		ret = -ENOMEM;
335e5779998SDaniel Mack 		goto err;
336e5779998SDaniel Mack 	}
337e5779998SDaniel Mack 
338e5779998SDaniel Mack 	/* now get the full information */
339e5779998SDaniel Mack 	ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
340e5779998SDaniel Mack 			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
34111bcbc44SDaniel Mack 			      UAC2_CS_CONTROL_SAM_FREQ << 8,
34211bcbc44SDaniel Mack 			      snd_usb_ctrl_intf(chip) | (clock << 8),
34317d900c4SClemens Ladisch 			      data, data_size);
344e5779998SDaniel Mack 
345e5779998SDaniel Mack 	if (ret < 0) {
3460ba41d91STakashi Iwai 		dev_err(&dev->dev,
3470ba41d91STakashi Iwai 			"%s(): unable to retrieve sample rate range (clock %d)\n",
34879f920fbSDaniel Mack 				__func__, clock);
349e5779998SDaniel Mack 		ret = -EINVAL;
350e5779998SDaniel Mack 		goto err_free;
351e5779998SDaniel Mack 	}
352e5779998SDaniel Mack 
35367c10366SDaniel Mack 	/* Call the triplet parser, and make sure fp->rate_table is NULL.
35467c10366SDaniel Mack 	 * We just use the return value to know how many sample rates we
35567c10366SDaniel Mack 	 * will have to deal with. */
35667c10366SDaniel Mack 	kfree(fp->rate_table);
35767c10366SDaniel Mack 	fp->rate_table = NULL;
3580ba41d91STakashi Iwai 	fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
35967c10366SDaniel Mack 
36067c10366SDaniel Mack 	if (fp->nr_rates == 0) {
36167c10366SDaniel Mack 		/* SNDRV_PCM_RATE_CONTINUOUS */
36267c10366SDaniel Mack 		ret = 0;
36367c10366SDaniel Mack 		goto err_free;
36467c10366SDaniel Mack 	}
36567c10366SDaniel Mack 
366*6da2ec56SKees Cook 	fp->rate_table = kmalloc_array(fp->nr_rates, sizeof(int), GFP_KERNEL);
367e5779998SDaniel Mack 	if (!fp->rate_table) {
368e5779998SDaniel Mack 		ret = -ENOMEM;
369e5779998SDaniel Mack 		goto err_free;
370e5779998SDaniel Mack 	}
371e5779998SDaniel Mack 
37267c10366SDaniel Mack 	/* Call the triplet parser again, but this time, fp->rate_table is
37367c10366SDaniel Mack 	 * allocated, so the rates will be stored */
3740ba41d91STakashi Iwai 	parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
375e5779998SDaniel Mack 
376e5779998SDaniel Mack err_free:
377e5779998SDaniel Mack 	kfree(data);
378e5779998SDaniel Mack err:
379e5779998SDaniel Mack 	return ret;
380e5779998SDaniel Mack }
381e5779998SDaniel Mack 
382e5779998SDaniel Mack /*
383e5779998SDaniel Mack  * parse the format type I and III descriptors
384e5779998SDaniel Mack  */
385e5779998SDaniel Mack static int parse_audio_format_i(struct snd_usb_audio *chip,
3869a2fe9b8SRuslan Bilovol 				struct audioformat *fp, u64 format,
3879a2fe9b8SRuslan Bilovol 				void *_fmt)
388e5779998SDaniel Mack {
38974c34ca1SEldad Zack 	snd_pcm_format_t pcm_format;
3909a2fe9b8SRuslan Bilovol 	unsigned int fmt_type;
39174c34ca1SEldad Zack 	int ret;
392e5779998SDaniel Mack 
3939a2fe9b8SRuslan Bilovol 	switch (fp->protocol) {
3949a2fe9b8SRuslan Bilovol 	default:
3959a2fe9b8SRuslan Bilovol 	case UAC_VERSION_1:
3969a2fe9b8SRuslan Bilovol 	case UAC_VERSION_2: {
3979a2fe9b8SRuslan Bilovol 		struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
3989a2fe9b8SRuslan Bilovol 
3999a2fe9b8SRuslan Bilovol 		fmt_type = fmt->bFormatType;
4009a2fe9b8SRuslan Bilovol 		break;
4019a2fe9b8SRuslan Bilovol 	}
4029a2fe9b8SRuslan Bilovol 	case UAC_VERSION_3: {
4039a2fe9b8SRuslan Bilovol 		/* fp->fmt_type is already set in this case */
4049a2fe9b8SRuslan Bilovol 		fmt_type = fp->fmt_type;
4059a2fe9b8SRuslan Bilovol 		break;
4069a2fe9b8SRuslan Bilovol 	}
4079a2fe9b8SRuslan Bilovol 	}
4089a2fe9b8SRuslan Bilovol 
4099a2fe9b8SRuslan Bilovol 	if (fmt_type == UAC_FORMAT_TYPE_III) {
410e5779998SDaniel Mack 		/* FIXME: the format type is really IECxxx
411e5779998SDaniel Mack 		 *        but we give normal PCM format to get the existing
412e5779998SDaniel Mack 		 *        apps working...
413e5779998SDaniel Mack 		 */
414e5779998SDaniel Mack 		switch (chip->usb_id) {
415e5779998SDaniel Mack 
416e5779998SDaniel Mack 		case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
417e5779998SDaniel Mack 			if (chip->setup == 0x00 &&
418e5779998SDaniel Mack 			    fp->altsetting == 6)
419e5779998SDaniel Mack 				pcm_format = SNDRV_PCM_FORMAT_S16_BE;
420e5779998SDaniel Mack 			else
421e5779998SDaniel Mack 				pcm_format = SNDRV_PCM_FORMAT_S16_LE;
422e5779998SDaniel Mack 			break;
423e5779998SDaniel Mack 		default:
424e5779998SDaniel Mack 			pcm_format = SNDRV_PCM_FORMAT_S16_LE;
425e5779998SDaniel Mack 		}
42674c34ca1SEldad Zack 		fp->formats = pcm_format_to_bits(pcm_format);
427e5779998SDaniel Mack 	} else {
4289a2fe9b8SRuslan Bilovol 		fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt);
42929088fefSClemens Ladisch 		if (!fp->formats)
4308ad10dc6SSachin Kamat 			return -EINVAL;
431e5779998SDaniel Mack 	}
432e5779998SDaniel Mack 
433e5779998SDaniel Mack 	/* gather possible sample rates */
434e5779998SDaniel Mack 	/* audio class v1 reports possible sample rates as part of the
435e5779998SDaniel Mack 	 * proprietary class specific descriptor.
436e5779998SDaniel Mack 	 * audio class v2 uses class specific EP0 range requests for that.
437e5779998SDaniel Mack 	 */
4388f898e92SClemens Ladisch 	switch (fp->protocol) {
439a2acad82SClemens Ladisch 	default:
4409a2fe9b8SRuslan Bilovol 	case UAC_VERSION_1: {
4419a2fe9b8SRuslan Bilovol 		struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
4429a2fe9b8SRuslan Bilovol 
443e5779998SDaniel Mack 		fp->channels = fmt->bNrChannels;
44474754f97SDaniel Mack 		ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
445e5779998SDaniel Mack 		break;
4469a2fe9b8SRuslan Bilovol 	}
447e5779998SDaniel Mack 	case UAC_VERSION_2:
4489a2fe9b8SRuslan Bilovol 	case UAC_VERSION_3: {
449e5779998SDaniel Mack 		/* fp->channels is already set in this case */
4509a2fe9b8SRuslan Bilovol 		ret = parse_audio_format_rates_v2v3(chip, fp);
451e5779998SDaniel Mack 		break;
452e5779998SDaniel Mack 	}
4539a2fe9b8SRuslan Bilovol 	}
454e5779998SDaniel Mack 
455e5779998SDaniel Mack 	if (fp->channels < 1) {
4560ba41d91STakashi Iwai 		usb_audio_err(chip,
4570ba41d91STakashi Iwai 			"%u:%d : invalid channels %d\n",
4580ba41d91STakashi Iwai 			fp->iface, fp->altsetting, fp->channels);
4598ad10dc6SSachin Kamat 		return -EINVAL;
460e5779998SDaniel Mack 	}
461e5779998SDaniel Mack 
462e5779998SDaniel Mack 	return ret;
463e5779998SDaniel Mack }
464e5779998SDaniel Mack 
465e5779998SDaniel Mack /*
466e5779998SDaniel Mack  * parse the format type II descriptor
467e5779998SDaniel Mack  */
468e5779998SDaniel Mack static int parse_audio_format_ii(struct snd_usb_audio *chip,
469e5779998SDaniel Mack 				 struct audioformat *fp,
4709a2fe9b8SRuslan Bilovol 				 u64 format, void *_fmt)
471e5779998SDaniel Mack {
472e5779998SDaniel Mack 	int brate, framesize, ret;
473e5779998SDaniel Mack 
474e5779998SDaniel Mack 	switch (format) {
475e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_II_AC3:
476e5779998SDaniel Mack 		/* FIXME: there is no AC3 format defined yet */
477015eb0b0SClemens Ladisch 		// fp->formats = SNDRV_PCM_FMTBIT_AC3;
478015eb0b0SClemens Ladisch 		fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
479e5779998SDaniel Mack 		break;
480e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_II_MPEG:
481015eb0b0SClemens Ladisch 		fp->formats = SNDRV_PCM_FMTBIT_MPEG;
482e5779998SDaniel Mack 		break;
483e5779998SDaniel Mack 	default:
4840ba41d91STakashi Iwai 		usb_audio_info(chip,
4859a2fe9b8SRuslan Bilovol 			 "%u:%d : unknown format tag %#llx is detected.  processed as MPEG.\n",
4860ba41d91STakashi Iwai 			 fp->iface, fp->altsetting, format);
487015eb0b0SClemens Ladisch 		fp->formats = SNDRV_PCM_FMTBIT_MPEG;
488e5779998SDaniel Mack 		break;
489e5779998SDaniel Mack 	}
490e5779998SDaniel Mack 
491e5779998SDaniel Mack 	fp->channels = 1;
492e5779998SDaniel Mack 
4938f898e92SClemens Ladisch 	switch (fp->protocol) {
494a2acad82SClemens Ladisch 	default:
495e5779998SDaniel Mack 	case UAC_VERSION_1: {
496e5779998SDaniel Mack 		struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
497e5779998SDaniel Mack 		brate = le16_to_cpu(fmt->wMaxBitRate);
498e5779998SDaniel Mack 		framesize = le16_to_cpu(fmt->wSamplesPerFrame);
4990ba41d91STakashi Iwai 		usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
500e5779998SDaniel Mack 		fp->frame_size = framesize;
501e5779998SDaniel Mack 		ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
502e5779998SDaniel Mack 		break;
503e5779998SDaniel Mack 	}
504e5779998SDaniel Mack 	case UAC_VERSION_2: {
505e5779998SDaniel Mack 		struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
506e5779998SDaniel Mack 		brate = le16_to_cpu(fmt->wMaxBitRate);
507e5779998SDaniel Mack 		framesize = le16_to_cpu(fmt->wSamplesPerFrame);
5080ba41d91STakashi Iwai 		usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
509e5779998SDaniel Mack 		fp->frame_size = framesize;
5109a2fe9b8SRuslan Bilovol 		ret = parse_audio_format_rates_v2v3(chip, fp);
511e5779998SDaniel Mack 		break;
512e5779998SDaniel Mack 	}
513e5779998SDaniel Mack 	}
514e5779998SDaniel Mack 
515e5779998SDaniel Mack 	return ret;
516e5779998SDaniel Mack }
517e5779998SDaniel Mack 
5182fcdb06dSDaniel Mack int snd_usb_parse_audio_format(struct snd_usb_audio *chip,
5199a2fe9b8SRuslan Bilovol 			       struct audioformat *fp, u64 format,
5202fcdb06dSDaniel Mack 			       struct uac_format_type_i_continuous_descriptor *fmt,
5218f898e92SClemens Ladisch 			       int stream)
522e5779998SDaniel Mack {
523e5779998SDaniel Mack 	int err;
524e5779998SDaniel Mack 
52574754f97SDaniel Mack 	switch (fmt->bFormatType) {
526e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_I:
527e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_III:
5288f898e92SClemens Ladisch 		err = parse_audio_format_i(chip, fp, format, fmt);
529e5779998SDaniel Mack 		break;
530e5779998SDaniel Mack 	case UAC_FORMAT_TYPE_II:
5318f898e92SClemens Ladisch 		err = parse_audio_format_ii(chip, fp, format, fmt);
532e5779998SDaniel Mack 		break;
533e5779998SDaniel Mack 	default:
5340ba41d91STakashi Iwai 		usb_audio_info(chip,
5350ba41d91STakashi Iwai 			 "%u:%d : format type %d is not supported yet\n",
5360ba41d91STakashi Iwai 			 fp->iface, fp->altsetting,
53774754f97SDaniel Mack 			 fmt->bFormatType);
5388d091242SDaniel Mack 		return -ENOTSUPP;
539e5779998SDaniel Mack 	}
54074754f97SDaniel Mack 	fp->fmt_type = fmt->bFormatType;
541e5779998SDaniel Mack 	if (err < 0)
542e5779998SDaniel Mack 		return err;
543e5779998SDaniel Mack #if 1
544e5779998SDaniel Mack 	/* FIXME: temporary hack for extigy/audigy 2 nx/zs */
545e5779998SDaniel Mack 	/* extigy apparently supports sample rates other than 48k
546e5779998SDaniel Mack 	 * but not in ordinary way.  so we enable only 48k atm.
547e5779998SDaniel Mack 	 */
548e5779998SDaniel Mack 	if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
549e5779998SDaniel Mack 	    chip->usb_id == USB_ID(0x041e, 0x3020) ||
550e5779998SDaniel Mack 	    chip->usb_id == USB_ID(0x041e, 0x3061)) {
55174754f97SDaniel Mack 		if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
552e5779998SDaniel Mack 		    fp->rates != SNDRV_PCM_RATE_48000 &&
553e5779998SDaniel Mack 		    fp->rates != SNDRV_PCM_RATE_96000)
5548d091242SDaniel Mack 			return -ENOTSUPP;
555e5779998SDaniel Mack 	}
556e5779998SDaniel Mack #endif
557e5779998SDaniel Mack 	return 0;
558e5779998SDaniel Mack }
559e5779998SDaniel Mack 
5609a2fe9b8SRuslan Bilovol int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip,
5619a2fe9b8SRuslan Bilovol 			       struct audioformat *fp,
5629a2fe9b8SRuslan Bilovol 			       struct uac3_as_header_descriptor *as,
5639a2fe9b8SRuslan Bilovol 			       int stream)
5649a2fe9b8SRuslan Bilovol {
5659a2fe9b8SRuslan Bilovol 	u64 format = le64_to_cpu(as->bmFormats);
5669a2fe9b8SRuslan Bilovol 	int err;
5679a2fe9b8SRuslan Bilovol 
5689a2fe9b8SRuslan Bilovol 	/*
5699a2fe9b8SRuslan Bilovol 	 * Type I format bits are D0..D6
5709a2fe9b8SRuslan Bilovol 	 * This test works because type IV is not supported
5719a2fe9b8SRuslan Bilovol 	 */
5729a2fe9b8SRuslan Bilovol 	if (format & 0x7f)
5739a2fe9b8SRuslan Bilovol 		fp->fmt_type = UAC_FORMAT_TYPE_I;
5749a2fe9b8SRuslan Bilovol 	else
5759a2fe9b8SRuslan Bilovol 		fp->fmt_type = UAC_FORMAT_TYPE_III;
5769a2fe9b8SRuslan Bilovol 
5779a2fe9b8SRuslan Bilovol 	err = parse_audio_format_i(chip, fp, format, as);
5789a2fe9b8SRuslan Bilovol 	if (err < 0)
5799a2fe9b8SRuslan Bilovol 		return err;
5809a2fe9b8SRuslan Bilovol 
5819a2fe9b8SRuslan Bilovol 	return 0;
5829a2fe9b8SRuslan Bilovol }
583