GNU Linux-libre 4.4.282-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                         usb_audio_err(chip, "cannot malloc\n");
183                         return -ENOMEM;
184                 }
185
186                 fp->nr_rates = 0;
187                 fp->rate_min = fp->rate_max = 0;
188                 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
189                         unsigned int rate = combine_triple(&fmt[idx]);
190                         if (!rate)
191                                 continue;
192                         /* C-Media CM6501 mislabels its 96 kHz altsetting */
193                         /* Terratec Aureon 7.1 USB C-Media 6206, too */
194                         /* Ozone Z90 USB C-Media, too */
195                         if (rate == 48000 && nr_rates == 1 &&
196                             (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
197                              chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
198                              chip->usb_id == USB_ID(0x0d8c, 0x0078) ||
199                              chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
200                             fp->altsetting == 5 && fp->maxpacksize == 392)
201                                 rate = 96000;
202                         /* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
203                         if (rate == 16000 &&
204                             (chip->usb_id == USB_ID(0x041e, 0x4064) ||
205                              chip->usb_id == USB_ID(0x041e, 0x4068)))
206                                 rate = 8000;
207
208                         fp->rate_table[fp->nr_rates] = rate;
209                         if (!fp->rate_min || rate < fp->rate_min)
210                                 fp->rate_min = rate;
211                         if (!fp->rate_max || rate > fp->rate_max)
212                                 fp->rate_max = rate;
213                         fp->rates |= snd_pcm_rate_to_rate_bit(rate);
214                         fp->nr_rates++;
215                 }
216                 if (!fp->nr_rates) {
217                         hwc_debug("All rates were zero. Skipping format!\n");
218                         return -EINVAL;
219                 }
220         } else {
221                 /* continuous rates */
222                 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
223                 fp->rate_min = combine_triple(&fmt[offset + 1]);
224                 fp->rate_max = combine_triple(&fmt[offset + 4]);
225         }
226         return 0;
227 }
228
229 /*
230  * Many Focusrite devices supports a limited set of sampling rates per
231  * altsetting. Maximum rate is exposed in the last 4 bytes of Format Type
232  * descriptor which has a non-standard bLength = 10.
233  */
234 static bool focusrite_valid_sample_rate(struct snd_usb_audio *chip,
235                                         struct audioformat *fp,
236                                         unsigned int rate)
237 {
238         struct usb_interface *iface;
239         struct usb_host_interface *alts;
240         unsigned char *fmt;
241         unsigned int max_rate;
242
243         iface = usb_ifnum_to_if(chip->dev, fp->iface);
244         if (!iface)
245                 return true;
246
247         alts = &iface->altsetting[fp->altset_idx];
248         fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
249                                       NULL, UAC_FORMAT_TYPE);
250         if (!fmt)
251                 return true;
252
253         if (fmt[0] == 10) { /* bLength */
254                 max_rate = combine_quad(&fmt[6]);
255
256                 /* Validate max rate */
257                 if (max_rate != 48000 &&
258                     max_rate != 96000 &&
259                     max_rate != 192000 &&
260                     max_rate != 384000) {
261
262                         usb_audio_info(chip,
263                                 "%u:%d : unexpected max rate: %u\n",
264                                 fp->iface, fp->altsetting, max_rate);
265
266                         return true;
267                 }
268
269                 return rate <= max_rate;
270         }
271
272         return true;
273 }
274
275 /*
276  * Helper function to walk the array of sample rate triplets reported by
277  * the device. The problem is that we need to parse whole array first to
278  * get to know how many sample rates we have to expect.
279  * Then fp->rate_table can be allocated and filled.
280  */
281 static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
282                                         struct audioformat *fp, int nr_triplets,
283                                         const unsigned char *data)
284 {
285         int i, nr_rates = 0;
286
287         fp->rates = fp->rate_min = fp->rate_max = 0;
288
289         for (i = 0; i < nr_triplets; i++) {
290                 int min = combine_quad(&data[2 + 12 * i]);
291                 int max = combine_quad(&data[6 + 12 * i]);
292                 int res = combine_quad(&data[10 + 12 * i]);
293                 unsigned int rate;
294
295                 if ((max < 0) || (min < 0) || (res < 0) || (max < min))
296                         continue;
297
298                 /*
299                  * for ranges with res == 1, we announce a continuous sample
300                  * rate range, and this function should return 0 for no further
301                  * parsing.
302                  */
303                 if (res == 1) {
304                         fp->rate_min = min;
305                         fp->rate_max = max;
306                         fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
307                         return 0;
308                 }
309
310                 for (rate = min; rate <= max; rate += res) {
311                         /* Filter out invalid rates on Focusrite devices */
312                         if (USB_ID_VENDOR(chip->usb_id) == 0x1235 &&
313                             !focusrite_valid_sample_rate(chip, fp, rate))
314                                 goto skip_rate;
315
316                         if (fp->rate_table)
317                                 fp->rate_table[nr_rates] = rate;
318                         if (!fp->rate_min || rate < fp->rate_min)
319                                 fp->rate_min = rate;
320                         if (!fp->rate_max || rate > fp->rate_max)
321                                 fp->rate_max = rate;
322                         fp->rates |= snd_pcm_rate_to_rate_bit(rate);
323
324                         nr_rates++;
325                         if (nr_rates >= MAX_NR_RATES) {
326                                 usb_audio_err(chip, "invalid uac2 rates\n");
327                                 break;
328                         }
329
330 skip_rate:
331                         /* avoid endless loop */
332                         if (res == 0)
333                                 break;
334                 }
335         }
336
337         return nr_rates;
338 }
339
340 /*
341  * parse the format descriptor and stores the possible sample rates
342  * on the audioformat table (audio class v2).
343  */
344 static int parse_audio_format_rates_v2(struct snd_usb_audio *chip,
345                                        struct audioformat *fp)
346 {
347         struct usb_device *dev = chip->dev;
348         unsigned char tmp[2], *data;
349         int nr_triplets, data_size, ret = 0;
350         int clock = snd_usb_clock_find_source(chip, fp->clock, false);
351
352         if (clock < 0) {
353                 dev_err(&dev->dev,
354                         "%s(): unable to find clock source (clock %d)\n",
355                                 __func__, clock);
356                 goto err;
357         }
358
359         /* get the number of sample rates first by only fetching 2 bytes */
360         ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
361                               USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
362                               UAC2_CS_CONTROL_SAM_FREQ << 8,
363                               snd_usb_ctrl_intf(chip) | (clock << 8),
364                               tmp, sizeof(tmp));
365
366         if (ret < 0) {
367                 dev_err(&dev->dev,
368                         "%s(): unable to retrieve number of sample rates (clock %d)\n",
369                                 __func__, clock);
370                 goto err;
371         }
372
373         nr_triplets = (tmp[1] << 8) | tmp[0];
374         data_size = 2 + 12 * nr_triplets;
375         data = kzalloc(data_size, GFP_KERNEL);
376         if (!data) {
377                 ret = -ENOMEM;
378                 goto err;
379         }
380
381         /* now get the full information */
382         ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
383                               USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
384                               UAC2_CS_CONTROL_SAM_FREQ << 8,
385                               snd_usb_ctrl_intf(chip) | (clock << 8),
386                               data, data_size);
387
388         if (ret < 0) {
389                 dev_err(&dev->dev,
390                         "%s(): unable to retrieve sample rate range (clock %d)\n",
391                                 __func__, clock);
392                 ret = -EINVAL;
393                 goto err_free;
394         }
395
396         /* Call the triplet parser, and make sure fp->rate_table is NULL.
397          * We just use the return value to know how many sample rates we
398          * will have to deal with. */
399         kfree(fp->rate_table);
400         fp->rate_table = NULL;
401         fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
402
403         if (fp->nr_rates == 0) {
404                 /* SNDRV_PCM_RATE_CONTINUOUS */
405                 ret = 0;
406                 goto err_free;
407         }
408
409         fp->rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
410         if (!fp->rate_table) {
411                 ret = -ENOMEM;
412                 goto err_free;
413         }
414
415         /* Call the triplet parser again, but this time, fp->rate_table is
416          * allocated, so the rates will be stored */
417         parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
418
419 err_free:
420         kfree(data);
421 err:
422         return ret;
423 }
424
425 /*
426  * parse the format type I and III descriptors
427  */
428 static int parse_audio_format_i(struct snd_usb_audio *chip,
429                                 struct audioformat *fp, unsigned int format,
430                                 struct uac_format_type_i_continuous_descriptor *fmt)
431 {
432         snd_pcm_format_t pcm_format;
433         int ret;
434
435         if (fmt->bFormatType == UAC_FORMAT_TYPE_III) {
436                 /* FIXME: the format type is really IECxxx
437                  *        but we give normal PCM format to get the existing
438                  *        apps working...
439                  */
440                 switch (chip->usb_id) {
441
442                 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
443                         if (chip->setup == 0x00 && 
444                             fp->altsetting == 6)
445                                 pcm_format = SNDRV_PCM_FORMAT_S16_BE;
446                         else
447                                 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
448                         break;
449                 default:
450                         pcm_format = SNDRV_PCM_FORMAT_S16_LE;
451                 }
452                 fp->formats = pcm_format_to_bits(pcm_format);
453         } else {
454                 fp->formats = parse_audio_format_i_type(chip, fp, format, fmt);
455                 if (!fp->formats)
456                         return -EINVAL;
457         }
458
459         /* gather possible sample rates */
460         /* audio class v1 reports possible sample rates as part of the
461          * proprietary class specific descriptor.
462          * audio class v2 uses class specific EP0 range requests for that.
463          */
464         switch (fp->protocol) {
465         default:
466         case UAC_VERSION_1:
467                 fp->channels = fmt->bNrChannels;
468                 ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
469                 break;
470         case UAC_VERSION_2:
471                 /* fp->channels is already set in this case */
472                 ret = parse_audio_format_rates_v2(chip, fp);
473                 break;
474         }
475
476         if (fp->channels < 1) {
477                 usb_audio_err(chip,
478                         "%u:%d : invalid channels %d\n",
479                         fp->iface, fp->altsetting, fp->channels);
480                 return -EINVAL;
481         }
482
483         return ret;
484 }
485
486 /*
487  * parse the format type II descriptor
488  */
489 static int parse_audio_format_ii(struct snd_usb_audio *chip,
490                                  struct audioformat *fp,
491                                  int format, void *_fmt)
492 {
493         int brate, framesize, ret;
494
495         switch (format) {
496         case UAC_FORMAT_TYPE_II_AC3:
497                 /* FIXME: there is no AC3 format defined yet */
498                 // fp->formats = SNDRV_PCM_FMTBIT_AC3;
499                 fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
500                 break;
501         case UAC_FORMAT_TYPE_II_MPEG:
502                 fp->formats = SNDRV_PCM_FMTBIT_MPEG;
503                 break;
504         default:
505                 usb_audio_info(chip,
506                          "%u:%d : unknown format tag %#x is detected.  processed as MPEG.\n",
507                          fp->iface, fp->altsetting, format);
508                 fp->formats = SNDRV_PCM_FMTBIT_MPEG;
509                 break;
510         }
511
512         fp->channels = 1;
513
514         switch (fp->protocol) {
515         default:
516         case UAC_VERSION_1: {
517                 struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
518                 brate = le16_to_cpu(fmt->wMaxBitRate);
519                 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
520                 usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
521                 fp->frame_size = framesize;
522                 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
523                 break;
524         }
525         case UAC_VERSION_2: {
526                 struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
527                 brate = le16_to_cpu(fmt->wMaxBitRate);
528                 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
529                 usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
530                 fp->frame_size = framesize;
531                 ret = parse_audio_format_rates_v2(chip, fp);
532                 break;
533         }
534         }
535
536         return ret;
537 }
538
539 int snd_usb_parse_audio_format(struct snd_usb_audio *chip,
540                                struct audioformat *fp, unsigned int format,
541                                struct uac_format_type_i_continuous_descriptor *fmt,
542                                int stream)
543 {
544         int err;
545
546         switch (fmt->bFormatType) {
547         case UAC_FORMAT_TYPE_I:
548         case UAC_FORMAT_TYPE_III:
549                 err = parse_audio_format_i(chip, fp, format, fmt);
550                 break;
551         case UAC_FORMAT_TYPE_II:
552                 err = parse_audio_format_ii(chip, fp, format, fmt);
553                 break;
554         default:
555                 usb_audio_info(chip,
556                          "%u:%d : format type %d is not supported yet\n",
557                          fp->iface, fp->altsetting,
558                          fmt->bFormatType);
559                 return -ENOTSUPP;
560         }
561         fp->fmt_type = fmt->bFormatType;
562         if (err < 0)
563                 return err;
564 #if 1
565         /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
566         /* extigy apparently supports sample rates other than 48k
567          * but not in ordinary way.  so we enable only 48k atm.
568          */
569         if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
570             chip->usb_id == USB_ID(0x041e, 0x3020) ||
571             chip->usb_id == USB_ID(0x041e, 0x3061)) {
572                 if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
573                     fp->rates != SNDRV_PCM_RATE_48000 &&
574                     fp->rates != SNDRV_PCM_RATE_96000)
575                         return -ENOTSUPP;
576         }
577 #endif
578         return 0;
579 }
580