Blame view

sound/usb/format.c 19 KB
1a59d1b8e   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
e5779998b   Daniel Mack   ALSA: usb-audio: ...
2
  /*
e5779998b   Daniel Mack   ALSA: usb-audio: ...
3
4
5
   */
  
  #include <linux/init.h>
36db04565   Stephen Rothwell   ALSA: usb - use o...
6
  #include <linux/slab.h>
e5779998b   Daniel Mack   ALSA: usb-audio: ...
7
8
  #include <linux/usb.h>
  #include <linux/usb/audio.h>
7e8478940   Daniel Mack   linux/usb/audio.h...
9
  #include <linux/usb/audio-v2.h>
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
10
  #include <linux/usb/audio-v3.h>
e5779998b   Daniel Mack   ALSA: usb-audio: ...
11
12
13
14
15
16
17
18
19
  
  #include <sound/core.h>
  #include <sound/pcm.h>
  
  #include "usbaudio.h"
  #include "card.h"
  #include "quirks.h"
  #include "helper.h"
  #include "debug.h"
79f920fbf   Daniel Mack   ALSA: usb-audio: ...
20
  #include "clock.h"
ee95cb612   Daniel Mack   ALSA: usb-audio: ...
21
  #include "format.h"
e5779998b   Daniel Mack   ALSA: usb-audio: ...
22
23
24
25
26
27
28
29
  
  /*
   * parse the audio format type I descriptor
   * and returns the corresponding pcm format
   *
   * @dev: usb device
   * @fp: audioformat record
   * @format: the format tag (wFormatTag)
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
30
   * @fmt: the format type descriptor (v1/v2) or AudioStreaming descriptor (v3)
e5779998b   Daniel Mack   ALSA: usb-audio: ...
31
   */
29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
32
  static u64 parse_audio_format_i_type(struct snd_usb_audio *chip,
e5779998b   Daniel Mack   ALSA: usb-audio: ...
33
  				     struct audioformat *fp,
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
34
  				     u64 format, void *_fmt)
e5779998b   Daniel Mack   ALSA: usb-audio: ...
35
  {
e5779998b   Daniel Mack   ALSA: usb-audio: ...
36
  	int sample_width, sample_bytes;
717bfb5f4   Daniel Mack   ALSA: snd-usb: ha...
37
  	u64 pcm_formats = 0;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
38

8f898e92a   Clemens Ladisch   ALSA: usb-audio: ...
39
  	switch (fp->protocol) {
a2acad829   Clemens Ladisch   ALSA: usb-audio: ...
40
41
  	case UAC_VERSION_1:
  	default: {
e5779998b   Daniel Mack   ALSA: usb-audio: ...
42
  		struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
cc3edd81e   Takashi Iwai   ALSA: usb-audio: ...
43
44
  		if (format >= 64)
  			return 0; /* invalid format */
e5779998b   Daniel Mack   ALSA: usb-audio: ...
45
46
  		sample_width = fmt->bBitResolution;
  		sample_bytes = fmt->bSubframeSize;
b44d419b9   Dan Carpenter   ALSA: usb-audio: ...
47
  		format = 1ULL << format;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
48
49
50
51
52
53
54
  		break;
  	}
  
  	case UAC_VERSION_2: {
  		struct uac_format_type_i_ext_descriptor *fmt = _fmt;
  		sample_width = fmt->bBitResolution;
  		sample_bytes = fmt->bSubslotSize;
717bfb5f4   Daniel Mack   ALSA: snd-usb: ha...
55

3a572d94b   Jussi Laako   ALSA: usb-audio: ...
56
  		if (format & UAC2_FORMAT_TYPE_I_RAW_DATA) {
717bfb5f4   Daniel Mack   ALSA: snd-usb: ha...
57
  			pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
3a572d94b   Jussi Laako   ALSA: usb-audio: ...
58
59
60
  			/* flag potentially raw DSD capable altsettings */
  			fp->dsd_raw = true;
  		}
717bfb5f4   Daniel Mack   ALSA: snd-usb: ha...
61

29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
62
  		format <<= 1;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
63
64
  		break;
  	}
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
65
66
67
68
69
70
71
72
73
74
75
76
  	case UAC_VERSION_3: {
  		struct uac3_as_header_descriptor *as = _fmt;
  
  		sample_width = as->bBitResolution;
  		sample_bytes = as->bSubslotSize;
  
  		if (format & UAC3_FORMAT_TYPE_I_RAW_DATA)
  			pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
  
  		format <<= 1;
  		break;
  	}
e5779998b   Daniel Mack   ALSA: usb-audio: ...
77
  	}
c7a132649   Jussi Laako   ALSA: usb-audio: ...
78
  	fp->fmt_bits = sample_width;
717bfb5f4   Daniel Mack   ALSA: snd-usb: ha...
79
80
  	if ((pcm_formats == 0) &&
  	    (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED))) {
29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
81
  		/* some devices don't define this correctly... */
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
82
83
84
  		usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM
  ",
  			fp->iface, fp->altsetting);
29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
85
86
87
  		format = 1 << UAC_FORMAT_TYPE_I_PCM;
  	}
  	if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) {
6d1f2f605   Takamichi Horikawa   ALSA: usb-audio: ...
88
89
90
91
  		if (((chip->usb_id == USB_ID(0x0582, 0x0016)) ||
  		     /* Edirol SD-90 */
  		     (chip->usb_id == USB_ID(0x0582, 0x000c))) &&
  		     /* Roland SC-D70 */
061b869ec   Clemens Ladisch   ALSA: usb-audio: ...
92
93
94
  		    sample_width == 24 && sample_bytes == 2)
  			sample_bytes = 3;
  		else if (sample_width > sample_bytes * 8) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
95
96
97
98
  			usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d
  ",
  				 fp->iface, fp->altsetting,
  				 sample_width, sample_bytes);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
99
100
101
102
  		}
  		/* check the format byte size */
  		switch (sample_bytes) {
  		case 1:
29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
103
  			pcm_formats |= SNDRV_PCM_FMTBIT_S8;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
104
105
106
  			break;
  		case 2:
  			if (snd_usb_is_big_endian_format(chip, fp))
29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
107
  				pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */
e5779998b   Daniel Mack   ALSA: usb-audio: ...
108
  			else
29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
109
  				pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
110
111
112
  			break;
  		case 3:
  			if (snd_usb_is_big_endian_format(chip, fp))
29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
113
  				pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */
e5779998b   Daniel Mack   ALSA: usb-audio: ...
114
  			else
29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
115
  				pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
116
117
  			break;
  		case 4:
29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
118
  			pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
119
120
  			break;
  		default:
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
121
122
123
124
125
  			usb_audio_info(chip,
  				 "%u:%d : unsupported sample bitwidth %d in %d bytes
  ",
  				 fp->iface, fp->altsetting,
  				 sample_width, sample_bytes);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
126
127
  			break;
  		}
29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
128
129
  	}
  	if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) {
e5779998b   Daniel Mack   ALSA: usb-audio: ...
130
131
132
  		/* Dallas DS4201 workaround: it advertises U8 format, but really
  		   supports S8. */
  		if (chip->usb_id == USB_ID(0x04fa, 0x4201))
29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  			pcm_formats |= SNDRV_PCM_FMTBIT_S8;
  		else
  			pcm_formats |= SNDRV_PCM_FMTBIT_U8;
  	}
  	if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) {
  		pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  	}
  	if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) {
  		pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;
  	}
  	if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) {
  		pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;
  	}
  	if (format & ~0x3f) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
147
  		usb_audio_info(chip,
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
148
149
  			 "%u:%d : unsupported format bits %#llx
  ",
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
150
  			 fp->iface, fp->altsetting, format);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
151
  	}
126825e7e   Daniel Mack   ALSA: snd-usb: ad...
152
153
  
  	pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes);
29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
154
  	return pcm_formats;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
155
  }
74f73476c   Takashi Iwai   ALSA: usb-audio: ...
156
157
158
159
160
161
162
163
164
165
166
167
168
  static int set_fixed_rate(struct audioformat *fp, int rate, int rate_bits)
  {
  	kfree(fp->rate_table);
  	fp->rate_table = kmalloc(sizeof(int), GFP_KERNEL);
  	if (!fp->rate_table)
  		return -ENOMEM;
  	fp->nr_rates = 1;
  	fp->rate_min = rate;
  	fp->rate_max = rate;
  	fp->rates = rate_bits;
  	fp->rate_table[0] = rate;
  	return 0;
  }
e5779998b   Daniel Mack   ALSA: usb-audio: ...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  
  /*
   * parse the format descriptor and stores the possible sample rates
   * on the audioformat table (audio class v1).
   *
   * @dev: usb device
   * @fp: audioformat record
   * @fmt: the format descriptor
   * @offset: the start offset of descriptor pointing the rate type
   *          (7 for type I and II, 8 for type II)
   */
  static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
  				       unsigned char *fmt, int offset)
  {
  	int nr_rates = fmt[offset];
  
  	if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
186
187
188
189
  		usb_audio_err(chip,
  			"%u:%d : invalid UAC_FORMAT_TYPE desc
  ",
  			fp->iface, fp->altsetting);
8ad10dc6d   Sachin Kamat   ALSA: usb-audio: ...
190
  		return -EINVAL;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
191
192
193
194
195
196
197
  	}
  
  	if (nr_rates) {
  		/*
  		 * build the rate table and bitmap flags
  		 */
  		int r, idx;
6da2ec560   Kees Cook   treewide: kmalloc...
198
199
  		fp->rate_table = kmalloc_array(nr_rates, sizeof(int),
  					       GFP_KERNEL);
1bc00f32b   Shawn Lin   ALSA: usb-audio: ...
200
  		if (fp->rate_table == NULL)
8ad10dc6d   Sachin Kamat   ALSA: usb-audio: ...
201
  			return -ENOMEM;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
202
203
204
205
206
207
208
209
  
  		fp->nr_rates = 0;
  		fp->rate_min = fp->rate_max = 0;
  		for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
  			unsigned int rate = combine_triple(&fmt[idx]);
  			if (!rate)
  				continue;
  			/* C-Media CM6501 mislabels its 96 kHz altsetting */
8129e79ed   Wolfgang Breyha   ALSA: usb-audio -...
210
  			/* Terratec Aureon 7.1 USB C-Media 6206, too */
e5779998b   Daniel Mack   ALSA: usb-audio: ...
211
212
  			if (rate == 48000 && nr_rates == 1 &&
  			    (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
8129e79ed   Wolfgang Breyha   ALSA: usb-audio -...
213
214
  			     chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
  			     chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
e5779998b   Daniel Mack   ALSA: usb-audio: ...
215
216
  			    fp->altsetting == 5 && fp->maxpacksize == 392)
  				rate = 96000;
8c4b79cf2   Pavel Hofman   ALSA: usb-audio: ...
217
218
219
220
  			/* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
  			if (rate == 16000 &&
  			    (chip->usb_id == USB_ID(0x041e, 0x4064) ||
  			     chip->usb_id == USB_ID(0x041e, 0x4068)))
e5779998b   Daniel Mack   ALSA: usb-audio: ...
221
222
223
224
225
226
227
228
229
230
231
232
233
  				rate = 8000;
  
  			fp->rate_table[fp->nr_rates] = rate;
  			if (!fp->rate_min || rate < fp->rate_min)
  				fp->rate_min = rate;
  			if (!fp->rate_max || rate > fp->rate_max)
  				fp->rate_max = rate;
  			fp->rates |= snd_pcm_rate_to_rate_bit(rate);
  			fp->nr_rates++;
  		}
  		if (!fp->nr_rates) {
  			hwc_debug("All rates were zero. Skipping format!
  ");
8ad10dc6d   Sachin Kamat   ALSA: usb-audio: ...
234
  			return -EINVAL;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
235
236
237
238
239
240
241
  		}
  	} else {
  		/* continuous rates */
  		fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  		fp->rate_min = combine_triple(&fmt[offset + 1]);
  		fp->rate_max = combine_triple(&fmt[offset + 4]);
  	}
74f73476c   Takashi Iwai   ALSA: usb-audio: ...
242
243
244
245
246
247
248
  
  	/* Jabra Evolve 65 headset */
  	if (chip->usb_id == USB_ID(0x0b0e, 0x030b)) {
  		/* only 48kHz for playback while keeping 16kHz for capture */
  		if (fp->nr_rates != 1)
  			return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000);
  	}
e5779998b   Daniel Mack   ALSA: usb-audio: ...
249
250
  	return 0;
  }
8dc5efe3d   Nick Kossifidis   ALSA: usb-audio: ...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  
  /*
   * Presonus Studio 1810c supports a limited set of sampling
   * rates per altsetting but reports the full set each time.
   * If we don't filter out the unsupported rates and attempt
   * to configure the card, it will hang refusing to do any
   * further audio I/O until a hard reset is performed.
   *
   * The list of supported rates per altsetting (set of available
   * I/O channels) is described in the owner's manual, section 2.2.
   */
  static bool s1810c_valid_sample_rate(struct audioformat *fp,
  				     unsigned int rate)
  {
  	switch (fp->altsetting) {
  	case 1:
  		/* All ADAT ports available */
  		return rate <= 48000;
  	case 2:
  		/* Half of ADAT ports available */
  		return (rate == 88200 || rate == 96000);
  	case 3:
  		/* Analog I/O only (no S/PDIF nor ADAT) */
  		return rate >= 176400;
  	default:
  		return false;
  	}
  	return false;
  }
e5779998b   Daniel Mack   ALSA: usb-audio: ...
280
  /*
1c8267925   Alexander Tsoy   ALSA: usb-audio: ...
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
   * Many Focusrite devices supports a limited set of sampling rates per
   * altsetting. Maximum rate is exposed in the last 4 bytes of Format Type
   * descriptor which has a non-standard bLength = 10.
   */
  static bool focusrite_valid_sample_rate(struct snd_usb_audio *chip,
  					struct audioformat *fp,
  					unsigned int rate)
  {
  	struct usb_interface *iface;
  	struct usb_host_interface *alts;
  	unsigned char *fmt;
  	unsigned int max_rate;
  
  	iface = usb_ifnum_to_if(chip->dev, fp->iface);
  	if (!iface)
  		return true;
  
  	alts = &iface->altsetting[fp->altset_idx];
  	fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
  				      NULL, UAC_FORMAT_TYPE);
  	if (!fmt)
  		return true;
  
  	if (fmt[0] == 10) { /* bLength */
  		max_rate = combine_quad(&fmt[6]);
  
  		/* Validate max rate */
  		if (max_rate != 48000 &&
  		    max_rate != 96000 &&
  		    max_rate != 192000 &&
  		    max_rate != 384000) {
  
  			usb_audio_info(chip,
  				"%u:%d : unexpected max rate: %u
  ",
  				fp->iface, fp->altsetting, max_rate);
  
  			return true;
  		}
  
  		return rate <= max_rate;
  	}
  
  	return true;
  }
  
  /*
67c103664   Daniel Mack   ALSA: usb-audio: ...
328
329
330
331
332
   * Helper function to walk the array of sample rate triplets reported by
   * the device. The problem is that we need to parse whole array first to
   * get to know how many sample rates we have to expect.
   * Then fp->rate_table can be allocated and filled.
   */
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
333
334
  static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
  					struct audioformat *fp, int nr_triplets,
67c103664   Daniel Mack   ALSA: usb-audio: ...
335
336
337
338
339
340
341
342
343
344
  					const unsigned char *data)
  {
  	int i, nr_rates = 0;
  
  	fp->rates = fp->rate_min = fp->rate_max = 0;
  
  	for (i = 0; i < nr_triplets; i++) {
  		int min = combine_quad(&data[2 + 12 * i]);
  		int max = combine_quad(&data[6 + 12 * i]);
  		int res = combine_quad(&data[10 + 12 * i]);
4fa0e81b8   Xi Wang   ALSA: usb-audio: ...
345
  		unsigned int rate;
67c103664   Daniel Mack   ALSA: usb-audio: ...
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
  
  		if ((max < 0) || (min < 0) || (res < 0) || (max < min))
  			continue;
  
  		/*
  		 * for ranges with res == 1, we announce a continuous sample
  		 * rate range, and this function should return 0 for no further
  		 * parsing.
  		 */
  		if (res == 1) {
  			fp->rate_min = min;
  			fp->rate_max = max;
  			fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  			return 0;
  		}
  
  		for (rate = min; rate <= max; rate += res) {
8dc5efe3d   Nick Kossifidis   ALSA: usb-audio: ...
363
364
365
366
367
  
  			/* Filter out invalid rates on Presonus Studio 1810c */
  			if (chip->usb_id == USB_ID(0x0194f, 0x010c) &&
  			    !s1810c_valid_sample_rate(fp, rate))
  				goto skip_rate;
1c8267925   Alexander Tsoy   ALSA: usb-audio: ...
368
369
370
371
  			/* Filter out invalid rates on Focusrite devices */
  			if (USB_ID_VENDOR(chip->usb_id) == 0x1235 &&
  			    !focusrite_valid_sample_rate(chip, fp, rate))
  				goto skip_rate;
67c103664   Daniel Mack   ALSA: usb-audio: ...
372
373
374
375
376
377
378
379
380
  			if (fp->rate_table)
  				fp->rate_table[nr_rates] = rate;
  			if (!fp->rate_min || rate < fp->rate_min)
  				fp->rate_min = rate;
  			if (!fp->rate_max || rate > fp->rate_max)
  				fp->rate_max = rate;
  			fp->rates |= snd_pcm_rate_to_rate_bit(rate);
  
  			nr_rates++;
8866f405e   Xi Wang   ALSA: usb-audio: ...
381
  			if (nr_rates >= MAX_NR_RATES) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
382
383
  				usb_audio_err(chip, "invalid uac2 rates
  ");
4fa0e81b8   Xi Wang   ALSA: usb-audio: ...
384
385
  				break;
  			}
67c103664   Daniel Mack   ALSA: usb-audio: ...
386

8dc5efe3d   Nick Kossifidis   ALSA: usb-audio: ...
387
  skip_rate:
67c103664   Daniel Mack   ALSA: usb-audio: ...
388
389
390
391
392
393
394
395
  			/* avoid endless loop */
  			if (res == 0)
  				break;
  		}
  	}
  
  	return nr_rates;
  }
8abf41dcd   Christopher Swenson   ALSA: usb-audio: ...
396
397
398
  /* Line6 Helix series and the Rode Rodecaster Pro don't support the
   * UAC2_CS_RANGE usb function call. Return a static table of known
   * clock rates.
d4bd30532   Nicola Lunghi   ALSA: usb-audio: ...
399
400
401
402
403
   */
  static int line6_parse_audio_format_rates_quirk(struct snd_usb_audio *chip,
  						struct audioformat *fp)
  {
  	switch (chip->usb_id) {
8be03a717   Takashi Iwai   ALSA: usb-audio: ...
404
405
406
407
  	case USB_ID(0x0e41, 0x4241): /* Line6 Helix */
  	case USB_ID(0x0e41, 0x4242): /* Line6 Helix Rack */
  	case USB_ID(0x0e41, 0x4244): /* Line6 Helix LT */
  	case USB_ID(0x0e41, 0x4246): /* Line6 HX-Stomp */
7da4c510a   Lukasz Halman   ALSA: usb-audio: ...
408
  	case USB_ID(0x0e41, 0x4247): /* Line6 Pod Go */
9b132f276   Takashi Iwai   Merge branch 'for...
409
410
411
  	case USB_ID(0x0e41, 0x4248): /* Line6 Helix >= fw 2.82 */
  	case USB_ID(0x0e41, 0x4249): /* Line6 Helix Rack >= fw 2.82 */
  	case USB_ID(0x0e41, 0x424a): /* Line6 Helix LT >= fw 2.82 */
8abf41dcd   Christopher Swenson   ALSA: usb-audio: ...
412
  	case USB_ID(0x19f7, 0x0011): /* Rode Rodecaster Pro */
74f73476c   Takashi Iwai   ALSA: usb-audio: ...
413
  		return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000);
d4bd30532   Nicola Lunghi   ALSA: usb-audio: ...
414
415
416
417
  	}
  
  	return -ENODEV;
  }
67c103664   Daniel Mack   ALSA: usb-audio: ...
418
  /*
e5779998b   Daniel Mack   ALSA: usb-audio: ...
419
   * parse the format descriptor and stores the possible sample rates
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
420
   * on the audioformat table (audio class v2 and v3).
e5779998b   Daniel Mack   ALSA: usb-audio: ...
421
   */
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
422
  static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,
3d8d4dcfd   Daniel Mack   ALSA: usb-audio: ...
423
  				       struct audioformat *fp)
e5779998b   Daniel Mack   ALSA: usb-audio: ...
424
425
426
  {
  	struct usb_device *dev = chip->dev;
  	unsigned char tmp[2], *data;
d4bd30532   Nicola Lunghi   ALSA: usb-audio: ...
427
  	int nr_triplets, data_size, ret = 0, ret_l6;
9f35a3128   Alexander Tsoy   ALSA: usb-audio: ...
428
  	int clock = snd_usb_clock_find_source(chip, fp, false);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
429

d07140ba7   Daniel Mack   ALSA: usb-audio: ...
430
  	if (clock < 0) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
431
432
433
  		dev_err(&dev->dev,
  			"%s(): unable to find clock source (clock %d)
  ",
d07140ba7   Daniel Mack   ALSA: usb-audio: ...
434
435
436
  				__func__, clock);
  		goto err;
  	}
e5779998b   Daniel Mack   ALSA: usb-audio: ...
437
438
439
  	/* get the number of sample rates first by only fetching 2 bytes */
  	ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
  			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
11bcbc443   Daniel Mack   ALSA: usb-audio: ...
440
441
  			      UAC2_CS_CONTROL_SAM_FREQ << 8,
  			      snd_usb_ctrl_intf(chip) | (clock << 8),
17d900c4a   Clemens Ladisch   ALSA: usb-audio: ...
442
  			      tmp, sizeof(tmp));
e5779998b   Daniel Mack   ALSA: usb-audio: ...
443
444
  
  	if (ret < 0) {
d4bd30532   Nicola Lunghi   ALSA: usb-audio: ...
445
446
447
448
449
450
451
452
453
454
455
456
457
458
  		/* line6 helix devices don't support UAC2_CS_CONTROL_SAM_FREQ call */
  		ret_l6 = line6_parse_audio_format_rates_quirk(chip, fp);
  		if (ret_l6 == -ENODEV) {
  			/* no line6 device found continue showing the error */
  			dev_err(&dev->dev,
  				"%s(): unable to retrieve number of sample rates (clock %d)
  ",
  				__func__, clock);
  			goto err;
  		}
  		if (ret_l6 == 0) {
  			dev_info(&dev->dev,
  				"%s(): unable to retrieve number of sample rates: set it to a predefined value (clock %d).
  ",
79f920fbf   Daniel Mack   ALSA: usb-audio: ...
459
  				__func__, clock);
d4bd30532   Nicola Lunghi   ALSA: usb-audio: ...
460
461
462
  			return 0;
  		}
  		ret = ret_l6;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
463
464
  		goto err;
  	}
67c103664   Daniel Mack   ALSA: usb-audio: ...
465
466
  	nr_triplets = (tmp[1] << 8) | tmp[0];
  	data_size = 2 + 12 * nr_triplets;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
467
468
469
470
471
472
473
474
  	data = kzalloc(data_size, GFP_KERNEL);
  	if (!data) {
  		ret = -ENOMEM;
  		goto err;
  	}
  
  	/* now get the full information */
  	ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
79f920fbf   Daniel Mack   ALSA: usb-audio: ...
475
  			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
11bcbc443   Daniel Mack   ALSA: usb-audio: ...
476
477
  			      UAC2_CS_CONTROL_SAM_FREQ << 8,
  			      snd_usb_ctrl_intf(chip) | (clock << 8),
17d900c4a   Clemens Ladisch   ALSA: usb-audio: ...
478
  			      data, data_size);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
479
480
  
  	if (ret < 0) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
481
482
483
  		dev_err(&dev->dev,
  			"%s(): unable to retrieve sample rate range (clock %d)
  ",
79f920fbf   Daniel Mack   ALSA: usb-audio: ...
484
  				__func__, clock);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
485
486
487
  		ret = -EINVAL;
  		goto err_free;
  	}
67c103664   Daniel Mack   ALSA: usb-audio: ...
488
489
490
491
492
  	/* Call the triplet parser, and make sure fp->rate_table is NULL.
  	 * We just use the return value to know how many sample rates we
  	 * will have to deal with. */
  	kfree(fp->rate_table);
  	fp->rate_table = NULL;
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
493
  	fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
67c103664   Daniel Mack   ALSA: usb-audio: ...
494
495
496
497
498
499
  
  	if (fp->nr_rates == 0) {
  		/* SNDRV_PCM_RATE_CONTINUOUS */
  		ret = 0;
  		goto err_free;
  	}
6da2ec560   Kees Cook   treewide: kmalloc...
500
  	fp->rate_table = kmalloc_array(fp->nr_rates, sizeof(int), GFP_KERNEL);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
501
502
503
504
  	if (!fp->rate_table) {
  		ret = -ENOMEM;
  		goto err_free;
  	}
67c103664   Daniel Mack   ALSA: usb-audio: ...
505
506
  	/* Call the triplet parser again, but this time, fp->rate_table is
  	 * allocated, so the rates will be stored */
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
507
  	parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
508
509
510
511
512
513
514
515
516
517
518
  
  err_free:
  	kfree(data);
  err:
  	return ret;
  }
  
  /*
   * parse the format type I and III descriptors
   */
  static int parse_audio_format_i(struct snd_usb_audio *chip,
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
519
520
  				struct audioformat *fp, u64 format,
  				void *_fmt)
e5779998b   Daniel Mack   ALSA: usb-audio: ...
521
  {
74c34ca1c   Eldad Zack   ALSA: pcm_format_...
522
  	snd_pcm_format_t pcm_format;
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
523
  	unsigned int fmt_type;
74c34ca1c   Eldad Zack   ALSA: pcm_format_...
524
  	int ret;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
525

9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
  	switch (fp->protocol) {
  	default:
  	case UAC_VERSION_1:
  	case UAC_VERSION_2: {
  		struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
  
  		fmt_type = fmt->bFormatType;
  		break;
  	}
  	case UAC_VERSION_3: {
  		/* fp->fmt_type is already set in this case */
  		fmt_type = fp->fmt_type;
  		break;
  	}
  	}
  
  	if (fmt_type == UAC_FORMAT_TYPE_III) {
e5779998b   Daniel Mack   ALSA: usb-audio: ...
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  		/* FIXME: the format type is really IECxxx
  		 *        but we give normal PCM format to get the existing
  		 *        apps working...
  		 */
  		switch (chip->usb_id) {
  
  		case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  			if (chip->setup == 0x00 && 
  			    fp->altsetting == 6)
  				pcm_format = SNDRV_PCM_FORMAT_S16_BE;
  			else
  				pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  			break;
  		default:
  			pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  		}
74c34ca1c   Eldad Zack   ALSA: pcm_format_...
559
  		fp->formats = pcm_format_to_bits(pcm_format);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
560
  	} else {
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
561
  		fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt);
29088fef3   Clemens Ladisch   ALSA: usb-audio: ...
562
  		if (!fp->formats)
8ad10dc6d   Sachin Kamat   ALSA: usb-audio: ...
563
  			return -EINVAL;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
564
  	}
e5779998b   Daniel Mack   ALSA: usb-audio: ...
565
566
567
568
569
  	/* gather possible sample rates */
  	/* audio class v1 reports possible sample rates as part of the
  	 * proprietary class specific descriptor.
  	 * audio class v2 uses class specific EP0 range requests for that.
  	 */
8f898e92a   Clemens Ladisch   ALSA: usb-audio: ...
570
  	switch (fp->protocol) {
a2acad829   Clemens Ladisch   ALSA: usb-audio: ...
571
  	default:
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
572
573
  	case UAC_VERSION_1: {
  		struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
574
  		fp->channels = fmt->bNrChannels;
74754f974   Daniel Mack   ALSA: usb-audio: ...
575
  		ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
576
  		break;
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
577
  	}
e5779998b   Daniel Mack   ALSA: usb-audio: ...
578
  	case UAC_VERSION_2:
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
579
  	case UAC_VERSION_3: {
e5779998b   Daniel Mack   ALSA: usb-audio: ...
580
  		/* fp->channels is already set in this case */
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
581
  		ret = parse_audio_format_rates_v2v3(chip, fp);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
582
583
  		break;
  	}
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
584
  	}
e5779998b   Daniel Mack   ALSA: usb-audio: ...
585
586
  
  	if (fp->channels < 1) {
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
587
588
589
590
  		usb_audio_err(chip,
  			"%u:%d : invalid channels %d
  ",
  			fp->iface, fp->altsetting, fp->channels);
8ad10dc6d   Sachin Kamat   ALSA: usb-audio: ...
591
  		return -EINVAL;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
592
593
594
595
596
597
598
599
600
601
  	}
  
  	return ret;
  }
  
  /*
   * parse the format type II descriptor
   */
  static int parse_audio_format_ii(struct snd_usb_audio *chip,
  				 struct audioformat *fp,
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
602
  				 u64 format, void *_fmt)
e5779998b   Daniel Mack   ALSA: usb-audio: ...
603
604
  {
  	int brate, framesize, ret;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
605
606
607
608
  
  	switch (format) {
  	case UAC_FORMAT_TYPE_II_AC3:
  		/* FIXME: there is no AC3 format defined yet */
015eb0b08   Clemens Ladisch   ALSA: usb-audio: ...
609
610
  		// fp->formats = SNDRV_PCM_FMTBIT_AC3;
  		fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
e5779998b   Daniel Mack   ALSA: usb-audio: ...
611
612
  		break;
  	case UAC_FORMAT_TYPE_II_MPEG:
015eb0b08   Clemens Ladisch   ALSA: usb-audio: ...
613
  		fp->formats = SNDRV_PCM_FMTBIT_MPEG;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
614
615
  		break;
  	default:
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
616
  		usb_audio_info(chip,
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
617
618
  			 "%u:%d : unknown format tag %#llx is detected.  processed as MPEG.
  ",
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
619
  			 fp->iface, fp->altsetting, format);
015eb0b08   Clemens Ladisch   ALSA: usb-audio: ...
620
  		fp->formats = SNDRV_PCM_FMTBIT_MPEG;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
621
622
623
624
  		break;
  	}
  
  	fp->channels = 1;
8f898e92a   Clemens Ladisch   ALSA: usb-audio: ...
625
  	switch (fp->protocol) {
a2acad829   Clemens Ladisch   ALSA: usb-audio: ...
626
  	default:
e5779998b   Daniel Mack   ALSA: usb-audio: ...
627
628
629
630
  	case UAC_VERSION_1: {
  		struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
  		brate = le16_to_cpu(fmt->wMaxBitRate);
  		framesize = le16_to_cpu(fmt->wSamplesPerFrame);
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
631
632
  		usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d
  ", brate, framesize);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
633
634
635
636
637
638
639
640
  		fp->frame_size = framesize;
  		ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
  		break;
  	}
  	case UAC_VERSION_2: {
  		struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
  		brate = le16_to_cpu(fmt->wMaxBitRate);
  		framesize = le16_to_cpu(fmt->wSamplesPerFrame);
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
641
642
  		usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d
  ", brate, framesize);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
643
  		fp->frame_size = framesize;
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
644
  		ret = parse_audio_format_rates_v2v3(chip, fp);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
645
646
647
648
649
650
  		break;
  	}
  	}
  
  	return ret;
  }
2fcdb06d4   Daniel Mack   ALSA: snd-usb: ha...
651
  int snd_usb_parse_audio_format(struct snd_usb_audio *chip,
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
652
  			       struct audioformat *fp, u64 format,
2fcdb06d4   Daniel Mack   ALSA: snd-usb: ha...
653
  			       struct uac_format_type_i_continuous_descriptor *fmt,
8f898e92a   Clemens Ladisch   ALSA: usb-audio: ...
654
  			       int stream)
e5779998b   Daniel Mack   ALSA: usb-audio: ...
655
656
  {
  	int err;
74754f974   Daniel Mack   ALSA: usb-audio: ...
657
  	switch (fmt->bFormatType) {
e5779998b   Daniel Mack   ALSA: usb-audio: ...
658
659
  	case UAC_FORMAT_TYPE_I:
  	case UAC_FORMAT_TYPE_III:
8f898e92a   Clemens Ladisch   ALSA: usb-audio: ...
660
  		err = parse_audio_format_i(chip, fp, format, fmt);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
661
662
  		break;
  	case UAC_FORMAT_TYPE_II:
8f898e92a   Clemens Ladisch   ALSA: usb-audio: ...
663
  		err = parse_audio_format_ii(chip, fp, format, fmt);
e5779998b   Daniel Mack   ALSA: usb-audio: ...
664
665
  		break;
  	default:
0ba41d917   Takashi Iwai   ALSA: usb-audio: ...
666
667
668
669
670
  		usb_audio_info(chip,
  			 "%u:%d : format type %d is not supported yet
  ",
  			 fp->iface, fp->altsetting,
  			 fmt->bFormatType);
8d0912427   Daniel Mack   ALSA: usb-audio: ...
671
  		return -ENOTSUPP;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
672
  	}
74754f974   Daniel Mack   ALSA: usb-audio: ...
673
  	fp->fmt_type = fmt->bFormatType;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
674
675
676
677
678
679
680
681
682
683
  	if (err < 0)
  		return err;
  #if 1
  	/* FIXME: temporary hack for extigy/audigy 2 nx/zs */
  	/* extigy apparently supports sample rates other than 48k
  	 * but not in ordinary way.  so we enable only 48k atm.
  	 */
  	if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
  	    chip->usb_id == USB_ID(0x041e, 0x3020) ||
  	    chip->usb_id == USB_ID(0x041e, 0x3061)) {
74754f974   Daniel Mack   ALSA: usb-audio: ...
684
  		if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
e5779998b   Daniel Mack   ALSA: usb-audio: ...
685
686
  		    fp->rates != SNDRV_PCM_RATE_48000 &&
  		    fp->rates != SNDRV_PCM_RATE_96000)
8d0912427   Daniel Mack   ALSA: usb-audio: ...
687
  			return -ENOTSUPP;
e5779998b   Daniel Mack   ALSA: usb-audio: ...
688
689
690
691
  	}
  #endif
  	return 0;
  }
9a2fe9b80   Ruslan Bilovol   ALSA: usb: initia...
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
  int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip,
  			       struct audioformat *fp,
  			       struct uac3_as_header_descriptor *as,
  			       int stream)
  {
  	u64 format = le64_to_cpu(as->bmFormats);
  	int err;
  
  	/*
  	 * Type I format bits are D0..D6
  	 * This test works because type IV is not supported
  	 */
  	if (format & 0x7f)
  		fp->fmt_type = UAC_FORMAT_TYPE_I;
  	else
  		fp->fmt_type = UAC_FORMAT_TYPE_III;
  
  	err = parse_audio_format_i(chip, fp, format, as);
  	if (err < 0)
  		return err;
  
  	return 0;
  }