GNU Linux-libre 4.9.318-gnu1
[releases.git] / sound / usb / format.c
1 /*
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the Free Software
14  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15  *
16  */
17
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/usb.h>
21 #include <linux/usb/audio.h>
22 #include <linux/usb/audio-v2.h>
23
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26
27 #include "usbaudio.h"
28 #include "card.h"
29 #include "quirks.h"
30 #include "helper.h"
31 #include "debug.h"
32 #include "clock.h"
33 #include "format.h"
34
35 /*
36  * parse the audio format type I descriptor
37  * and returns the corresponding pcm format
38  *
39  * @dev: usb device
40  * @fp: audioformat record
41  * @format: the format tag (wFormatTag)
42  * @fmt: the format type descriptor
43  */
44 static u64 parse_audio_format_i_type(struct snd_usb_audio *chip,
45                                      struct audioformat *fp,
46                                      unsigned int format, void *_fmt)
47 {
48         int sample_width, sample_bytes;
49         u64 pcm_formats = 0;
50
51         switch (fp->protocol) {
52         case UAC_VERSION_1:
53         default: {
54                 struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
55                 if (format >= 64)
56                         return 0; /* invalid format */
57                 sample_width = fmt->bBitResolution;
58                 sample_bytes = fmt->bSubframeSize;
59                 format = 1 << format;
60                 break;
61         }
62
63         case UAC_VERSION_2: {
64                 struct uac_format_type_i_ext_descriptor *fmt = _fmt;
65                 sample_width = fmt->bBitResolution;
66                 sample_bytes = fmt->bSubslotSize;
67
68                 if (format & UAC2_FORMAT_TYPE_I_RAW_DATA)
69                         pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
70
71                 format <<= 1;
72                 break;
73         }
74         }
75
76         if ((pcm_formats == 0) &&
77             (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED))) {
78                 /* some devices don't define this correctly... */
79                 usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM\n",
80                         fp->iface, fp->altsetting);
81                 format = 1 << UAC_FORMAT_TYPE_I_PCM;
82         }
83         if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) {
84                 if (((chip->usb_id == USB_ID(0x0582, 0x0016)) ||
85                      /* Edirol SD-90 */
86                      (chip->usb_id == USB_ID(0x0582, 0x000c))) &&
87                      /* Roland SC-D70 */
88                     sample_width == 24 && sample_bytes == 2)
89                         sample_bytes = 3;
90                 else if (sample_width > sample_bytes * 8) {
91                         usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d\n",
92                                  fp->iface, fp->altsetting,
93                                  sample_width, sample_bytes);
94                 }
95                 /* check the format byte size */
96                 switch (sample_bytes) {
97                 case 1:
98                         pcm_formats |= SNDRV_PCM_FMTBIT_S8;
99                         break;
100                 case 2:
101                         if (snd_usb_is_big_endian_format(chip, fp))
102                                 pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */
103                         else
104                                 pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE;
105                         break;
106                 case 3:
107                         if (snd_usb_is_big_endian_format(chip, fp))
108                                 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */
109                         else
110                                 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE;
111                         break;
112                 case 4:
113                         pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE;
114                         break;
115                 default:
116                         usb_audio_info(chip,
117                                  "%u:%d : unsupported sample bitwidth %d in %d bytes\n",
118                                  fp->iface, fp->altsetting,
119                                  sample_width, sample_bytes);
120                         break;
121                 }
122         }
123         if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) {
124                 /* Dallas DS4201 workaround: it advertises U8 format, but really
125                    supports S8. */
126                 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
127                         pcm_formats |= SNDRV_PCM_FMTBIT_S8;
128                 else
129                         pcm_formats |= SNDRV_PCM_FMTBIT_U8;
130         }
131         if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) {
132                 pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
133         }
134         if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) {
135                 pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;
136         }
137         if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) {
138                 pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;
139         }
140         if (format & ~0x3f) {
141                 usb_audio_info(chip,
142                          "%u:%d : unsupported format bits %#x\n",
143                          fp->iface, fp->altsetting, format);
144         }
145
146         pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes);
147
148         return pcm_formats;
149 }
150
151
152 /*
153  * parse the format descriptor and stores the possible sample rates
154  * on the audioformat table (audio class v1).
155  *
156  * @dev: usb device
157  * @fp: audioformat record
158  * @fmt: the format descriptor
159  * @offset: the start offset of descriptor pointing the rate type
160  *          (7 for type I and II, 8 for type II)
161  */
162 static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
163                                        unsigned char *fmt, int offset)
164 {
165         int nr_rates = fmt[offset];
166
167         if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
168                 usb_audio_err(chip,
169                         "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
170                         fp->iface, fp->altsetting);
171                 return -EINVAL;
172         }
173
174         if (nr_rates) {
175                 /*
176                  * build the rate table and bitmap flags
177                  */
178                 int r, idx;
179
180                 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
181                 if (fp->rate_table == NULL)
182                         return -ENOMEM;
183
184                 fp->nr_rates = 0;
185                 fp->rate_min = fp->rate_max = 0;
186                 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
187                         unsigned int rate = combine_triple(&fmt[idx]);
188                         if (!rate)
189                                 continue;
190                         /* C-Media CM6501 mislabels its 96 kHz altsetting */
191                         /* Terratec Aureon 7.1 USB C-Media 6206, too */
192                         /* Ozone Z90 USB C-Media, too */
193                         if (rate == 48000 && nr_rates == 1 &&
194                             (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
195                              chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
196                              chip->usb_id == USB_ID(0x0d8c, 0x0078) ||
197                              chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
198                             fp->altsetting == 5 && fp->maxpacksize == 392)
199                                 rate = 96000;
200                         /* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
201                         if (rate == 16000 &&
202                             (chip->usb_id == USB_ID(0x041e, 0x4064) ||
203                              chip->usb_id == USB_ID(0x041e, 0x4068)))
204                                 rate = 8000;
205
206                         fp->rate_table[fp->nr_rates] = rate;
207                         if (!fp->rate_min || rate < fp->rate_min)
208                                 fp->rate_min = rate;
209                         if (!fp->rate_max || rate > fp->rate_max)
210                                 fp->rate_max = rate;
211                         fp->rates |= snd_pcm_rate_to_rate_bit(rate);
212                         fp->nr_rates++;
213                 }
214                 if (!fp->nr_rates) {
215                         hwc_debug("All rates were zero. Skipping format!\n");
216                         return -EINVAL;
217                 }
218         } else {
219                 /* continuous rates */
220                 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
221                 fp->rate_min = combine_triple(&fmt[offset + 1]);
222                 fp->rate_max = combine_triple(&fmt[offset + 4]);
223         }
224         return 0;
225 }
226
227 /*
228  * Many Focusrite devices supports a limited set of sampling rates per
229  * altsetting. Maximum rate is exposed in the last 4 bytes of Format Type
230  * descriptor which has a non-standard bLength = 10.
231  */
232 static bool focusrite_valid_sample_rate(struct snd_usb_audio *chip,
233                                         struct audioformat *fp,
234                                         unsigned int rate)
235 {
236         struct usb_interface *iface;
237         struct usb_host_interface *alts;
238         unsigned char *fmt;
239         unsigned int max_rate;
240
241         iface = usb_ifnum_to_if(chip->dev, fp->iface);
242         if (!iface)
243                 return true;
244
245         alts = &iface->altsetting[fp->altset_idx];
246         fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
247                                       NULL, UAC_FORMAT_TYPE);
248         if (!fmt)
249                 return true;
250
251         if (fmt[0] == 10) { /* bLength */
252                 max_rate = combine_quad(&fmt[6]);
253
254                 /* Validate max rate */
255                 if (max_rate != 48000 &&
256                     max_rate != 96000 &&
257                     max_rate != 192000 &&
258                     max_rate != 384000) {
259
260                         usb_audio_info(chip,
261                                 "%u:%d : unexpected max rate: %u\n",
262                                 fp->iface, fp->altsetting, max_rate);
263
264                         return true;
265                 }
266
267                 return rate <= max_rate;
268         }
269
270         return true;
271 }
272
273 /*
274  * Helper function to walk the array of sample rate triplets reported by
275  * the device. The problem is that we need to parse whole array first to
276  * get to know how many sample rates we have to expect.
277  * Then fp->rate_table can be allocated and filled.
278  */
279 static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
280                                         struct audioformat *fp, int nr_triplets,
281                                         const unsigned char *data)
282 {
283         int i, nr_rates = 0;
284
285         fp->rates = fp->rate_min = fp->rate_max = 0;
286
287         for (i = 0; i < nr_triplets; i++) {
288                 int min = combine_quad(&data[2 + 12 * i]);
289                 int max = combine_quad(&data[6 + 12 * i]);
290                 int res = combine_quad(&data[10 + 12 * i]);
291                 unsigned int rate;
292
293                 if ((max < 0) || (min < 0) || (res < 0) || (max < min))
294                         continue;
295
296                 /*
297                  * for ranges with res == 1, we announce a continuous sample
298                  * rate range, and this function should return 0 for no further
299                  * parsing.
300                  */
301                 if (res == 1) {
302                         fp->rate_min = min;
303                         fp->rate_max = max;
304                         fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
305                         return 0;
306                 }
307
308                 for (rate = min; rate <= max; rate += res) {
309                         /* Filter out invalid rates on Focusrite devices */
310                         if (USB_ID_VENDOR(chip->usb_id) == 0x1235 &&
311                             !focusrite_valid_sample_rate(chip, fp, rate))
312                                 goto skip_rate;
313
314                         if (fp->rate_table)
315                                 fp->rate_table[nr_rates] = rate;
316                         if (!fp->rate_min || rate < fp->rate_min)
317                                 fp->rate_min = rate;
318                         if (!fp->rate_max || rate > fp->rate_max)
319                                 fp->rate_max = rate;
320                         fp->rates |= snd_pcm_rate_to_rate_bit(rate);
321
322                         nr_rates++;
323                         if (nr_rates >= MAX_NR_RATES) {
324                                 usb_audio_err(chip, "invalid uac2 rates\n");
325                                 break;
326                         }
327
328 skip_rate:
329                         /* avoid endless loop */
330                         if (res == 0)
331                                 break;
332                 }
333         }
334
335         return nr_rates;
336 }
337
338 /*
339  * parse the format descriptor and stores the possible sample rates
340  * on the audioformat table (audio class v2).
341  */
342 static int parse_audio_format_rates_v2(struct snd_usb_audio *chip,
343                                        struct audioformat *fp)
344 {
345         struct usb_device *dev = chip->dev;
346         unsigned char tmp[2], *data;
347         int nr_triplets, data_size, ret = 0;
348         int clock = snd_usb_clock_find_source(chip, fp->clock, false);
349
350         if (clock < 0) {
351                 dev_err(&dev->dev,
352                         "%s(): unable to find clock source (clock %d)\n",
353                                 __func__, clock);
354                 goto err;
355         }
356
357         /* get the number of sample rates first by only fetching 2 bytes */
358         ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
359                               USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
360                               UAC2_CS_CONTROL_SAM_FREQ << 8,
361                               snd_usb_ctrl_intf(chip) | (clock << 8),
362                               tmp, sizeof(tmp));
363
364         if (ret < 0) {
365                 dev_err(&dev->dev,
366                         "%s(): unable to retrieve number of sample rates (clock %d)\n",
367                                 __func__, clock);
368                 goto err;
369         }
370
371         nr_triplets = (tmp[1] << 8) | tmp[0];
372         data_size = 2 + 12 * nr_triplets;
373         data = kzalloc(data_size, GFP_KERNEL);
374         if (!data) {
375                 ret = -ENOMEM;
376                 goto err;
377         }
378
379         /* now get the full information */
380         ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
381                               USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
382                               UAC2_CS_CONTROL_SAM_FREQ << 8,
383                               snd_usb_ctrl_intf(chip) | (clock << 8),
384                               data, data_size);
385
386         if (ret < 0) {
387                 dev_err(&dev->dev,
388                         "%s(): unable to retrieve sample rate range (clock %d)\n",
389                                 __func__, clock);
390                 ret = -EINVAL;
391                 goto err_free;
392         }
393
394         /* Call the triplet parser, and make sure fp->rate_table is NULL.
395          * We just use the return value to know how many sample rates we
396          * will have to deal with. */
397         kfree(fp->rate_table);
398         fp->rate_table = NULL;
399         fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
400
401         if (fp->nr_rates == 0) {
402                 /* SNDRV_PCM_RATE_CONTINUOUS */
403                 ret = 0;
404                 goto err_free;
405         }
406
407         fp->rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
408         if (!fp->rate_table) {
409                 ret = -ENOMEM;
410                 goto err_free;
411         }
412
413         /* Call the triplet parser again, but this time, fp->rate_table is
414          * allocated, so the rates will be stored */
415         parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
416
417 err_free:
418         kfree(data);
419 err:
420         return ret;
421 }
422
423 /*
424  * parse the format type I and III descriptors
425  */
426 static int parse_audio_format_i(struct snd_usb_audio *chip,
427                                 struct audioformat *fp, unsigned int format,
428                                 struct uac_format_type_i_continuous_descriptor *fmt)
429 {
430         snd_pcm_format_t pcm_format;
431         int ret;
432
433         if (fmt->bFormatType == UAC_FORMAT_TYPE_III) {
434                 /* FIXME: the format type is really IECxxx
435                  *        but we give normal PCM format to get the existing
436                  *        apps working...
437                  */
438                 switch (chip->usb_id) {
439
440                 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
441                         if (chip->setup == 0x00 && 
442                             fp->altsetting == 6)
443                                 pcm_format = SNDRV_PCM_FORMAT_S16_BE;
444                         else
445                                 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
446                         break;
447                 default:
448                         pcm_format = SNDRV_PCM_FORMAT_S16_LE;
449                 }
450                 fp->formats = pcm_format_to_bits(pcm_format);
451         } else {
452                 fp->formats = parse_audio_format_i_type(chip, fp, format, fmt);
453                 if (!fp->formats)
454                         return -EINVAL;
455         }
456
457         /* gather possible sample rates */
458         /* audio class v1 reports possible sample rates as part of the
459          * proprietary class specific descriptor.
460          * audio class v2 uses class specific EP0 range requests for that.
461          */
462         switch (fp->protocol) {
463         default:
464         case UAC_VERSION_1:
465                 fp->channels = fmt->bNrChannels;
466                 ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
467                 break;
468         case UAC_VERSION_2:
469                 /* fp->channels is already set in this case */
470                 ret = parse_audio_format_rates_v2(chip, fp);
471                 break;
472         }
473
474         if (fp->channels < 1) {
475                 usb_audio_err(chip,
476                         "%u:%d : invalid channels %d\n",
477                         fp->iface, fp->altsetting, fp->channels);
478                 return -EINVAL;
479         }
480
481         return ret;
482 }
483
484 /*
485  * parse the format type II descriptor
486  */
487 static int parse_audio_format_ii(struct snd_usb_audio *chip,
488                                  struct audioformat *fp,
489                                  int format, void *_fmt)
490 {
491         int brate, framesize, ret;
492
493         switch (format) {
494         case UAC_FORMAT_TYPE_II_AC3:
495                 /* FIXME: there is no AC3 format defined yet */
496                 // fp->formats = SNDRV_PCM_FMTBIT_AC3;
497                 fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
498                 break;
499         case UAC_FORMAT_TYPE_II_MPEG:
500                 fp->formats = SNDRV_PCM_FMTBIT_MPEG;
501                 break;
502         default:
503                 usb_audio_info(chip,
504                          "%u:%d : unknown format tag %#x is detected.  processed as MPEG.\n",
505                          fp->iface, fp->altsetting, format);
506                 fp->formats = SNDRV_PCM_FMTBIT_MPEG;
507                 break;
508         }
509
510         fp->channels = 1;
511
512         switch (fp->protocol) {
513         default:
514         case UAC_VERSION_1: {
515                 struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
516                 brate = le16_to_cpu(fmt->wMaxBitRate);
517                 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
518                 usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
519                 fp->frame_size = framesize;
520                 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
521                 break;
522         }
523         case UAC_VERSION_2: {
524                 struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
525                 brate = le16_to_cpu(fmt->wMaxBitRate);
526                 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
527                 usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
528                 fp->frame_size = framesize;
529                 ret = parse_audio_format_rates_v2(chip, fp);
530                 break;
531         }
532         }
533
534         return ret;
535 }
536
537 int snd_usb_parse_audio_format(struct snd_usb_audio *chip,
538                                struct audioformat *fp, unsigned int format,
539                                struct uac_format_type_i_continuous_descriptor *fmt,
540                                int stream)
541 {
542         int err;
543
544         switch (fmt->bFormatType) {
545         case UAC_FORMAT_TYPE_I:
546         case UAC_FORMAT_TYPE_III:
547                 err = parse_audio_format_i(chip, fp, format, fmt);
548                 break;
549         case UAC_FORMAT_TYPE_II:
550                 err = parse_audio_format_ii(chip, fp, format, fmt);
551                 break;
552         default:
553                 usb_audio_info(chip,
554                          "%u:%d : format type %d is not supported yet\n",
555                          fp->iface, fp->altsetting,
556                          fmt->bFormatType);
557                 return -ENOTSUPP;
558         }
559         fp->fmt_type = fmt->bFormatType;
560         if (err < 0)
561                 return err;
562 #if 1
563         /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
564         /* extigy apparently supports sample rates other than 48k
565          * but not in ordinary way.  so we enable only 48k atm.
566          */
567         if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
568             chip->usb_id == USB_ID(0x041e, 0x3020) ||
569             chip->usb_id == USB_ID(0x041e, 0x3061)) {
570                 if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
571                     fp->rates != SNDRV_PCM_RATE_48000 &&
572                     fp->rates != SNDRV_PCM_RATE_96000)
573                         return -ENOTSUPP;
574         }
575 #endif
576         return 0;
577 }
578