GNU Linux-libre 4.14.290-gnu1
[releases.git] / sound / usb / quirks.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 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/usb.h>
20 #include <linux/usb/audio.h>
21 #include <linux/usb/midi.h>
22
23 #include <sound/control.h>
24 #include <sound/core.h>
25 #include <sound/info.h>
26 #include <sound/pcm.h>
27
28 #include "usbaudio.h"
29 #include "card.h"
30 #include "mixer.h"
31 #include "mixer_quirks.h"
32 #include "midi.h"
33 #include "quirks.h"
34 #include "helper.h"
35 #include "endpoint.h"
36 #include "pcm.h"
37 #include "clock.h"
38 #include "stream.h"
39
40 /*
41  * handle the quirks for the contained interfaces
42  */
43 static int create_composite_quirk(struct snd_usb_audio *chip,
44                                   struct usb_interface *iface,
45                                   struct usb_driver *driver,
46                                   const struct snd_usb_audio_quirk *quirk_comp)
47 {
48         int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
49         const struct snd_usb_audio_quirk *quirk;
50         int err;
51
52         for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
53                 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
54                 if (!iface)
55                         continue;
56                 if (quirk->ifnum != probed_ifnum &&
57                     usb_interface_claimed(iface))
58                         continue;
59                 err = snd_usb_create_quirk(chip, iface, driver, quirk);
60                 if (err < 0)
61                         return err;
62         }
63
64         for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
65                 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
66                 if (!iface)
67                         continue;
68                 if (quirk->ifnum != probed_ifnum &&
69                     !usb_interface_claimed(iface)) {
70                         err = usb_driver_claim_interface(driver, iface,
71                                                          USB_AUDIO_IFACE_UNUSED);
72                         if (err < 0)
73                                 return err;
74                 }
75         }
76
77         return 0;
78 }
79
80 static int ignore_interface_quirk(struct snd_usb_audio *chip,
81                                   struct usb_interface *iface,
82                                   struct usb_driver *driver,
83                                   const struct snd_usb_audio_quirk *quirk)
84 {
85         return 0;
86 }
87
88
89 /*
90  * Allow alignment on audio sub-slot (channel samples) rather than
91  * on audio slots (audio frames)
92  */
93 static int create_align_transfer_quirk(struct snd_usb_audio *chip,
94                                        struct usb_interface *iface,
95                                        struct usb_driver *driver,
96                                        const struct snd_usb_audio_quirk *quirk)
97 {
98         chip->txfr_quirk = 1;
99         return 1;       /* Continue with creating streams and mixer */
100 }
101
102 static int create_any_midi_quirk(struct snd_usb_audio *chip,
103                                  struct usb_interface *intf,
104                                  struct usb_driver *driver,
105                                  const struct snd_usb_audio_quirk *quirk)
106 {
107         return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
108 }
109
110 /*
111  * create a stream for an interface with proper descriptors
112  */
113 static int create_standard_audio_quirk(struct snd_usb_audio *chip,
114                                        struct usb_interface *iface,
115                                        struct usb_driver *driver,
116                                        const struct snd_usb_audio_quirk *quirk)
117 {
118         struct usb_host_interface *alts;
119         struct usb_interface_descriptor *altsd;
120         int err;
121
122         if (chip->usb_id == USB_ID(0x1686, 0x00dd)) /* Zoom R16/24 */
123                 chip->tx_length_quirk = 1;
124
125         alts = &iface->altsetting[0];
126         altsd = get_iface_desc(alts);
127         err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
128         if (err < 0) {
129                 usb_audio_err(chip, "cannot setup if %d: error %d\n",
130                            altsd->bInterfaceNumber, err);
131                 return err;
132         }
133         /* reset the current interface */
134         usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
135         return 0;
136 }
137
138 /*
139  * create a stream for an endpoint/altsetting without proper descriptors
140  */
141 static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
142                                      struct usb_interface *iface,
143                                      struct usb_driver *driver,
144                                      const struct snd_usb_audio_quirk *quirk)
145 {
146         struct audioformat *fp;
147         struct usb_host_interface *alts;
148         struct usb_interface_descriptor *altsd;
149         int stream, err;
150         unsigned *rate_table = NULL;
151
152         fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
153         if (!fp)
154                 return -ENOMEM;
155
156         INIT_LIST_HEAD(&fp->list);
157         if (fp->nr_rates > MAX_NR_RATES) {
158                 kfree(fp);
159                 return -EINVAL;
160         }
161         if (fp->nr_rates > 0) {
162                 rate_table = kmemdup(fp->rate_table,
163                                      sizeof(int) * fp->nr_rates, GFP_KERNEL);
164                 if (!rate_table) {
165                         kfree(fp);
166                         return -ENOMEM;
167                 }
168                 fp->rate_table = rate_table;
169         }
170
171         stream = (fp->endpoint & USB_DIR_IN)
172                 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
173         err = snd_usb_add_audio_stream(chip, stream, fp);
174         if (err < 0)
175                 goto error;
176         if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
177             fp->altset_idx >= iface->num_altsetting) {
178                 err = -EINVAL;
179                 goto error;
180         }
181         alts = &iface->altsetting[fp->altset_idx];
182         altsd = get_iface_desc(alts);
183         if (altsd->bNumEndpoints < 1) {
184                 err = -EINVAL;
185                 goto error;
186         }
187
188         fp->protocol = altsd->bInterfaceProtocol;
189
190         if (fp->datainterval == 0)
191                 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
192         if (fp->maxpacksize == 0)
193                 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
194         usb_set_interface(chip->dev, fp->iface, 0);
195         snd_usb_init_pitch(chip, fp->iface, alts, fp);
196         snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
197         return 0;
198
199  error:
200         list_del(&fp->list); /* unlink for avoiding double-free */
201         kfree(fp);
202         kfree(rate_table);
203         return err;
204 }
205
206 static int create_auto_pcm_quirk(struct snd_usb_audio *chip,
207                                  struct usb_interface *iface,
208                                  struct usb_driver *driver)
209 {
210         struct usb_host_interface *alts;
211         struct usb_interface_descriptor *altsd;
212         struct usb_endpoint_descriptor *epd;
213         struct uac1_as_header_descriptor *ashd;
214         struct uac_format_type_i_discrete_descriptor *fmtd;
215
216         /*
217          * Most Roland/Yamaha audio streaming interfaces have more or less
218          * standard descriptors, but older devices might lack descriptors, and
219          * future ones might change, so ensure that we fail silently if the
220          * interface doesn't look exactly right.
221          */
222
223         /* must have a non-zero altsetting for streaming */
224         if (iface->num_altsetting < 2)
225                 return -ENODEV;
226         alts = &iface->altsetting[1];
227         altsd = get_iface_desc(alts);
228
229         /* must have an isochronous endpoint for streaming */
230         if (altsd->bNumEndpoints < 1)
231                 return -ENODEV;
232         epd = get_endpoint(alts, 0);
233         if (!usb_endpoint_xfer_isoc(epd))
234                 return -ENODEV;
235
236         /* must have format descriptors */
237         ashd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
238                                        UAC_AS_GENERAL);
239         fmtd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
240                                        UAC_FORMAT_TYPE);
241         if (!ashd || ashd->bLength < 7 ||
242             !fmtd || fmtd->bLength < 8)
243                 return -ENODEV;
244
245         return create_standard_audio_quirk(chip, iface, driver, NULL);
246 }
247
248 static int create_yamaha_midi_quirk(struct snd_usb_audio *chip,
249                                     struct usb_interface *iface,
250                                     struct usb_driver *driver,
251                                     struct usb_host_interface *alts)
252 {
253         static const struct snd_usb_audio_quirk yamaha_midi_quirk = {
254                 .type = QUIRK_MIDI_YAMAHA
255         };
256         struct usb_midi_in_jack_descriptor *injd;
257         struct usb_midi_out_jack_descriptor *outjd;
258
259         /* must have some valid jack descriptors */
260         injd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
261                                        NULL, USB_MS_MIDI_IN_JACK);
262         outjd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
263                                         NULL, USB_MS_MIDI_OUT_JACK);
264         if (!injd && !outjd)
265                 return -ENODEV;
266         if (injd && (injd->bLength < 5 ||
267                      (injd->bJackType != USB_MS_EMBEDDED &&
268                       injd->bJackType != USB_MS_EXTERNAL)))
269                 return -ENODEV;
270         if (outjd && (outjd->bLength < 6 ||
271                       (outjd->bJackType != USB_MS_EMBEDDED &&
272                        outjd->bJackType != USB_MS_EXTERNAL)))
273                 return -ENODEV;
274         return create_any_midi_quirk(chip, iface, driver, &yamaha_midi_quirk);
275 }
276
277 static int create_roland_midi_quirk(struct snd_usb_audio *chip,
278                                     struct usb_interface *iface,
279                                     struct usb_driver *driver,
280                                     struct usb_host_interface *alts)
281 {
282         static const struct snd_usb_audio_quirk roland_midi_quirk = {
283                 .type = QUIRK_MIDI_ROLAND
284         };
285         u8 *roland_desc = NULL;
286
287         /* might have a vendor-specific descriptor <06 24 F1 02 ...> */
288         for (;;) {
289                 roland_desc = snd_usb_find_csint_desc(alts->extra,
290                                                       alts->extralen,
291                                                       roland_desc, 0xf1);
292                 if (!roland_desc)
293                         return -ENODEV;
294                 if (roland_desc[0] < 6 || roland_desc[3] != 2)
295                         continue;
296                 return create_any_midi_quirk(chip, iface, driver,
297                                              &roland_midi_quirk);
298         }
299 }
300
301 static int create_std_midi_quirk(struct snd_usb_audio *chip,
302                                  struct usb_interface *iface,
303                                  struct usb_driver *driver,
304                                  struct usb_host_interface *alts)
305 {
306         struct usb_ms_header_descriptor *mshd;
307         struct usb_ms_endpoint_descriptor *msepd;
308
309         /* must have the MIDIStreaming interface header descriptor*/
310         mshd = (struct usb_ms_header_descriptor *)alts->extra;
311         if (alts->extralen < 7 ||
312             mshd->bLength < 7 ||
313             mshd->bDescriptorType != USB_DT_CS_INTERFACE ||
314             mshd->bDescriptorSubtype != USB_MS_HEADER)
315                 return -ENODEV;
316         /* must have the MIDIStreaming endpoint descriptor*/
317         msepd = (struct usb_ms_endpoint_descriptor *)alts->endpoint[0].extra;
318         if (alts->endpoint[0].extralen < 4 ||
319             msepd->bLength < 4 ||
320             msepd->bDescriptorType != USB_DT_CS_ENDPOINT ||
321             msepd->bDescriptorSubtype != UAC_MS_GENERAL ||
322             msepd->bNumEmbMIDIJack < 1 ||
323             msepd->bNumEmbMIDIJack > 16)
324                 return -ENODEV;
325
326         return create_any_midi_quirk(chip, iface, driver, NULL);
327 }
328
329 static int create_auto_midi_quirk(struct snd_usb_audio *chip,
330                                   struct usb_interface *iface,
331                                   struct usb_driver *driver)
332 {
333         struct usb_host_interface *alts;
334         struct usb_interface_descriptor *altsd;
335         struct usb_endpoint_descriptor *epd;
336         int err;
337
338         alts = &iface->altsetting[0];
339         altsd = get_iface_desc(alts);
340
341         /* must have at least one bulk/interrupt endpoint for streaming */
342         if (altsd->bNumEndpoints < 1)
343                 return -ENODEV;
344         epd = get_endpoint(alts, 0);
345         if (!usb_endpoint_xfer_bulk(epd) &&
346             !usb_endpoint_xfer_int(epd))
347                 return -ENODEV;
348
349         switch (USB_ID_VENDOR(chip->usb_id)) {
350         case 0x0499: /* Yamaha */
351                 err = create_yamaha_midi_quirk(chip, iface, driver, alts);
352                 if (err != -ENODEV)
353                         return err;
354                 break;
355         case 0x0582: /* Roland */
356                 err = create_roland_midi_quirk(chip, iface, driver, alts);
357                 if (err != -ENODEV)
358                         return err;
359                 break;
360         }
361
362         return create_std_midi_quirk(chip, iface, driver, alts);
363 }
364
365 static int create_autodetect_quirk(struct snd_usb_audio *chip,
366                                    struct usb_interface *iface,
367                                    struct usb_driver *driver)
368 {
369         int err;
370
371         err = create_auto_pcm_quirk(chip, iface, driver);
372         if (err == -ENODEV)
373                 err = create_auto_midi_quirk(chip, iface, driver);
374         return err;
375 }
376
377 static int create_autodetect_quirks(struct snd_usb_audio *chip,
378                                     struct usb_interface *iface,
379                                     struct usb_driver *driver,
380                                     const struct snd_usb_audio_quirk *quirk)
381 {
382         int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
383         int ifcount, ifnum, err;
384
385         err = create_autodetect_quirk(chip, iface, driver);
386         if (err < 0)
387                 return err;
388
389         /*
390          * ALSA PCM playback/capture devices cannot be registered in two steps,
391          * so we have to claim the other corresponding interface here.
392          */
393         ifcount = chip->dev->actconfig->desc.bNumInterfaces;
394         for (ifnum = 0; ifnum < ifcount; ifnum++) {
395                 if (ifnum == probed_ifnum || quirk->ifnum >= 0)
396                         continue;
397                 iface = usb_ifnum_to_if(chip->dev, ifnum);
398                 if (!iface ||
399                     usb_interface_claimed(iface) ||
400                     get_iface_desc(iface->altsetting)->bInterfaceClass !=
401                                                         USB_CLASS_VENDOR_SPEC)
402                         continue;
403
404                 err = create_autodetect_quirk(chip, iface, driver);
405                 if (err >= 0) {
406                         err = usb_driver_claim_interface(driver, iface,
407                                                          USB_AUDIO_IFACE_UNUSED);
408                         if (err < 0)
409                                 return err;
410                 }
411         }
412
413         return 0;
414 }
415
416 /*
417  * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.  
418  * The only way to detect the sample rate is by looking at wMaxPacketSize.
419  */
420 static int create_uaxx_quirk(struct snd_usb_audio *chip,
421                              struct usb_interface *iface,
422                              struct usb_driver *driver,
423                              const struct snd_usb_audio_quirk *quirk)
424 {
425         static const struct audioformat ua_format = {
426                 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
427                 .channels = 2,
428                 .fmt_type = UAC_FORMAT_TYPE_I,
429                 .altsetting = 1,
430                 .altset_idx = 1,
431                 .rates = SNDRV_PCM_RATE_CONTINUOUS,
432         };
433         struct usb_host_interface *alts;
434         struct usb_interface_descriptor *altsd;
435         struct audioformat *fp;
436         int stream, err;
437
438         /* both PCM and MIDI interfaces have 2 or more altsettings */
439         if (iface->num_altsetting < 2)
440                 return -ENXIO;
441         alts = &iface->altsetting[1];
442         altsd = get_iface_desc(alts);
443
444         if (altsd->bNumEndpoints == 2) {
445                 static const struct snd_usb_midi_endpoint_info ua700_ep = {
446                         .out_cables = 0x0003,
447                         .in_cables  = 0x0003
448                 };
449                 static const struct snd_usb_audio_quirk ua700_quirk = {
450                         .type = QUIRK_MIDI_FIXED_ENDPOINT,
451                         .data = &ua700_ep
452                 };
453                 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
454                         .out_cables = 0x0001,
455                         .in_cables  = 0x0001
456                 };
457                 static const struct snd_usb_audio_quirk uaxx_quirk = {
458                         .type = QUIRK_MIDI_FIXED_ENDPOINT,
459                         .data = &uaxx_ep
460                 };
461                 const struct snd_usb_audio_quirk *quirk =
462                         chip->usb_id == USB_ID(0x0582, 0x002b)
463                         ? &ua700_quirk : &uaxx_quirk;
464                 return __snd_usbmidi_create(chip->card, iface,
465                                           &chip->midi_list, quirk,
466                                           chip->usb_id);
467         }
468
469         if (altsd->bNumEndpoints != 1)
470                 return -ENXIO;
471
472         fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
473         if (!fp)
474                 return -ENOMEM;
475
476         fp->iface = altsd->bInterfaceNumber;
477         fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
478         fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
479         fp->datainterval = 0;
480         fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
481         INIT_LIST_HEAD(&fp->list);
482
483         switch (fp->maxpacksize) {
484         case 0x120:
485                 fp->rate_max = fp->rate_min = 44100;
486                 break;
487         case 0x138:
488         case 0x140:
489                 fp->rate_max = fp->rate_min = 48000;
490                 break;
491         case 0x258:
492         case 0x260:
493                 fp->rate_max = fp->rate_min = 96000;
494                 break;
495         default:
496                 usb_audio_err(chip, "unknown sample rate\n");
497                 kfree(fp);
498                 return -ENXIO;
499         }
500
501         stream = (fp->endpoint & USB_DIR_IN)
502                 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
503         err = snd_usb_add_audio_stream(chip, stream, fp);
504         if (err < 0) {
505                 list_del(&fp->list); /* unlink for avoiding double-free */
506                 kfree(fp);
507                 return err;
508         }
509         usb_set_interface(chip->dev, fp->iface, 0);
510         return 0;
511 }
512
513 /*
514  * Create a standard mixer for the specified interface.
515  */
516 static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
517                                        struct usb_interface *iface,
518                                        struct usb_driver *driver,
519                                        const struct snd_usb_audio_quirk *quirk)
520 {
521         if (quirk->ifnum < 0)
522                 return 0;
523
524         return snd_usb_create_mixer(chip, quirk->ifnum, 0);
525 }
526
527 /*
528  * audio-interface quirks
529  *
530  * returns zero if no standard audio/MIDI parsing is needed.
531  * returns a positive value if standard audio/midi interfaces are parsed
532  * after this.
533  * returns a negative value at error.
534  */
535 int snd_usb_create_quirk(struct snd_usb_audio *chip,
536                          struct usb_interface *iface,
537                          struct usb_driver *driver,
538                          const struct snd_usb_audio_quirk *quirk)
539 {
540         typedef int (*quirk_func_t)(struct snd_usb_audio *,
541                                     struct usb_interface *,
542                                     struct usb_driver *,
543                                     const struct snd_usb_audio_quirk *);
544         static const quirk_func_t quirk_funcs[] = {
545                 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
546                 [QUIRK_COMPOSITE] = create_composite_quirk,
547                 [QUIRK_AUTODETECT] = create_autodetect_quirks,
548                 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
549                 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
550                 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
551                 [QUIRK_MIDI_ROLAND] = create_any_midi_quirk,
552                 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
553                 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
554                 [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
555                 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
556                 [QUIRK_MIDI_CME] = create_any_midi_quirk,
557                 [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
558                 [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
559                 [QUIRK_MIDI_CH345] = create_any_midi_quirk,
560                 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
561                 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
562                 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
563                 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
564                 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
565         };
566
567         if (quirk->type < QUIRK_TYPE_COUNT) {
568                 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
569         } else {
570                 usb_audio_err(chip, "invalid quirk type %d\n", quirk->type);
571                 return -ENXIO;
572         }
573 }
574
575 /*
576  * boot quirks
577  */
578
579 #define EXTIGY_FIRMWARE_SIZE_OLD 794
580 #define EXTIGY_FIRMWARE_SIZE_NEW 483
581
582 static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
583 {
584         struct usb_host_config *config = dev->actconfig;
585         int err;
586
587         if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
588             le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
589                 dev_dbg(&dev->dev, "sending Extigy boot sequence...\n");
590                 /* Send message to force it to reconnect with full interface. */
591                 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
592                                       0x10, 0x43, 0x0001, 0x000a, NULL, 0);
593                 if (err < 0)
594                         dev_dbg(&dev->dev, "error sending boot message: %d\n", err);
595                 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
596                                 &dev->descriptor, sizeof(dev->descriptor));
597                 config = dev->actconfig;
598                 if (err < 0)
599                         dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
600                 err = usb_reset_configuration(dev);
601                 if (err < 0)
602                         dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
603                 dev_dbg(&dev->dev, "extigy_boot: new boot length = %d\n",
604                             le16_to_cpu(get_cfg_desc(config)->wTotalLength));
605                 return -ENODEV; /* quit this anyway */
606         }
607         return 0;
608 }
609
610 static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
611 {
612         u8 buf = 1;
613
614         snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
615                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
616                         0, 0, &buf, 1);
617         if (buf == 0) {
618                 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
619                                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
620                                 1, 2000, NULL, 0);
621                 return -ENODEV;
622         }
623         return 0;
624 }
625
626 static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
627 {
628         int err;
629
630         if (dev->actconfig->desc.bConfigurationValue == 1) {
631                 dev_info(&dev->dev,
632                            "Fast Track Pro switching to config #2\n");
633                 /* This function has to be available by the usb core module.
634                  * if it is not avialable the boot quirk has to be left out
635                  * and the configuration has to be set by udev or hotplug
636                  * rules
637                  */
638                 err = usb_driver_set_configuration(dev, 2);
639                 if (err < 0)
640                         dev_dbg(&dev->dev,
641                                 "error usb_driver_set_configuration: %d\n",
642                                 err);
643                 /* Always return an error, so that we stop creating a device
644                    that will just be destroyed and recreated with a new
645                    configuration */
646                 return -ENODEV;
647         } else
648                 dev_info(&dev->dev, "Fast Track Pro config OK\n");
649
650         return 0;
651 }
652
653 /*
654  * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
655  * documented in the device's data sheet.
656  */
657 static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
658 {
659         u8 buf[4];
660         buf[0] = 0x20;
661         buf[1] = value & 0xff;
662         buf[2] = (value >> 8) & 0xff;
663         buf[3] = reg;
664         return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
665                                USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
666                                0, 0, &buf, 4);
667 }
668
669 static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
670 {
671         /*
672          * Enable line-out driver mode, set headphone source to front
673          * channels, enable stereo mic.
674          */
675         return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
676 }
677
678 /*
679  * C-Media CM6206 is based on CM106 with two additional
680  * registers that are not documented in the data sheet.
681  * Values here are chosen based on sniffing USB traffic
682  * under Windows.
683  */
684 static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
685 {
686         int err  = 0, reg;
687         int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
688
689         for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
690                 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
691                 if (err < 0)
692                         return err;
693         }
694
695         return err;
696 }
697
698 /* quirk for Plantronics GameCom 780 with CM6302 chip */
699 static int snd_usb_gamecon780_boot_quirk(struct usb_device *dev)
700 {
701         /* set the initial volume and don't change; other values are either
702          * too loud or silent due to firmware bug (bko#65251)
703          */
704         u8 buf[2] = { 0x74, 0xe3 };
705         return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
706                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
707                         UAC_FU_VOLUME << 8, 9 << 8, buf, 2);
708 }
709
710 /*
711  * Novation Twitch DJ controller
712  * Focusrite Novation Saffire 6 USB audio card
713  */
714 static int snd_usb_novation_boot_quirk(struct usb_device *dev)
715 {
716         /* preemptively set up the device because otherwise the
717          * raw MIDI endpoints are not active */
718         usb_set_interface(dev, 0, 1);
719         return 0;
720 }
721
722 /*
723  * This call will put the synth in "USB send" mode, i.e it will send MIDI
724  * messages through USB (this is disabled at startup). The synth will
725  * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
726  * sign on its LCD. Values here are chosen based on sniffing USB traffic
727  * under Windows.
728  */
729 static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
730 {
731         int err, actual_length;
732
733         /* "midi send" enable */
734         static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
735
736         void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
737         if (!buf)
738                 return -ENOMEM;
739         err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
740                         ARRAY_SIZE(seq), &actual_length, 1000);
741         kfree(buf);
742         if (err < 0)
743                 return err;
744
745         return 0;
746 }
747
748 /*
749  * Some sound cards from Native Instruments are in fact compliant to the USB
750  * audio standard of version 2 and other approved USB standards, even though
751  * they come up as vendor-specific device when first connected.
752  *
753  * However, they can be told to come up with a new set of descriptors
754  * upon their next enumeration, and the interfaces announced by the new
755  * descriptors will then be handled by the kernel's class drivers. As the
756  * product ID will also change, no further checks are required.
757  */
758
759 static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
760 {
761         int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
762                                   0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
763                                   1, 0, NULL, 0, 1000);
764
765         if (ret < 0)
766                 return ret;
767
768         usb_reset_device(dev);
769
770         /* return -EAGAIN, so the creation of an audio interface for this
771          * temporary device is aborted. The device will reconnect with a
772          * new product ID */
773         return -EAGAIN;
774 }
775
776 static void mbox2_setup_48_24_magic(struct usb_device *dev)
777 {
778         u8 srate[3];
779         u8 temp[12];
780
781         /* Choose 48000Hz permanently */
782         srate[0] = 0x80;
783         srate[1] = 0xbb;
784         srate[2] = 0x00;
785
786         /* Send the magic! */
787         snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
788                 0x01, 0x22, 0x0100, 0x0085, &temp, 0x0003);
789         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
790                 0x81, 0xa2, 0x0100, 0x0085, &srate, 0x0003);
791         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
792                 0x81, 0xa2, 0x0100, 0x0086, &srate, 0x0003);
793         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
794                 0x81, 0xa2, 0x0100, 0x0003, &srate, 0x0003);
795         return;
796 }
797
798 /* Digidesign Mbox 2 needs to load firmware onboard
799  * and driver must wait a few seconds for initialisation.
800  */
801
802 #define MBOX2_FIRMWARE_SIZE    646
803 #define MBOX2_BOOT_LOADING     0x01 /* Hard coded into the device */
804 #define MBOX2_BOOT_READY       0x02 /* Hard coded into the device */
805
806 static int snd_usb_mbox2_boot_quirk(struct usb_device *dev)
807 {
808         struct usb_host_config *config = dev->actconfig;
809         int err;
810         u8 bootresponse[0x12];
811         int fwsize;
812         int count;
813
814         fwsize = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
815
816         if (fwsize != MBOX2_FIRMWARE_SIZE) {
817                 dev_err(&dev->dev, "Invalid firmware size=%d.\n", fwsize);
818                 return -ENODEV;
819         }
820
821         dev_dbg(&dev->dev, "Sending Digidesign Mbox 2 boot sequence...\n");
822
823         count = 0;
824         bootresponse[0] = MBOX2_BOOT_LOADING;
825         while ((bootresponse[0] == MBOX2_BOOT_LOADING) && (count < 10)) {
826                 msleep(500); /* 0.5 second delay */
827                 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
828                         /* Control magic - load onboard firmware */
829                         0x85, 0xc0, 0x0001, 0x0000, &bootresponse, 0x0012);
830                 if (bootresponse[0] == MBOX2_BOOT_READY)
831                         break;
832                 dev_dbg(&dev->dev, "device not ready, resending boot sequence...\n");
833                 count++;
834         }
835
836         if (bootresponse[0] != MBOX2_BOOT_READY) {
837                 dev_err(&dev->dev, "Unknown bootresponse=%d, or timed out, ignoring device.\n", bootresponse[0]);
838                 return -ENODEV;
839         }
840
841         dev_dbg(&dev->dev, "device initialised!\n");
842
843         err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
844                 &dev->descriptor, sizeof(dev->descriptor));
845         config = dev->actconfig;
846         if (err < 0)
847                 dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
848
849         err = usb_reset_configuration(dev);
850         if (err < 0)
851                 dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
852         dev_dbg(&dev->dev, "mbox2_boot: new boot length = %d\n",
853                 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
854
855         mbox2_setup_48_24_magic(dev);
856
857         dev_info(&dev->dev, "Digidesign Mbox 2: 24bit 48kHz");
858
859         return 0; /* Successful boot */
860 }
861
862 /*
863  * Setup quirks
864  */
865 #define MAUDIO_SET              0x01 /* parse device_setup */
866 #define MAUDIO_SET_COMPATIBLE   0x80 /* use only "win-compatible" interfaces */
867 #define MAUDIO_SET_DTS          0x02 /* enable DTS Digital Output */
868 #define MAUDIO_SET_96K          0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
869 #define MAUDIO_SET_24B          0x08 /* 24bits sample if set, 16bits otherwise */
870 #define MAUDIO_SET_DI           0x10 /* enable Digital Input */
871 #define MAUDIO_SET_MASK         0x1f /* bit mask for setup value */
872 #define MAUDIO_SET_24B_48K_DI    0x19 /* 24bits+48KHz+Digital Input */
873 #define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
874 #define MAUDIO_SET_16B_48K_DI    0x11 /* 16bits+48KHz+Digital Input */
875 #define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
876
877 static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
878                                       int iface, int altno)
879 {
880         /* Reset ALL ifaces to 0 altsetting.
881          * Call it for every possible altsetting of every interface.
882          */
883         usb_set_interface(chip->dev, iface, 0);
884         if (chip->setup & MAUDIO_SET) {
885                 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
886                         if (iface != 1 && iface != 2)
887                                 return 1; /* skip all interfaces but 1 and 2 */
888                 } else {
889                         unsigned int mask;
890                         if (iface == 1 || iface == 2)
891                                 return 1; /* skip interfaces 1 and 2 */
892                         if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
893                                 return 1; /* skip this altsetting */
894                         mask = chip->setup & MAUDIO_SET_MASK;
895                         if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
896                                 return 1; /* skip this altsetting */
897                         if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
898                                 return 1; /* skip this altsetting */
899                         if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
900                                 return 1; /* skip this altsetting */
901                 }
902         }
903         usb_audio_dbg(chip,
904                     "using altsetting %d for interface %d config %d\n",
905                     altno, iface, chip->setup);
906         return 0; /* keep this altsetting */
907 }
908
909 static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
910                                          int iface,
911                                          int altno)
912 {
913         /* Reset ALL ifaces to 0 altsetting.
914          * Call it for every possible altsetting of every interface.
915          */
916         usb_set_interface(chip->dev, iface, 0);
917
918         if (chip->setup & MAUDIO_SET) {
919                 unsigned int mask;
920                 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
921                         return 1; /* skip this altsetting */
922                 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
923                         return 1; /* skip this altsetting */
924                 mask = chip->setup & MAUDIO_SET_MASK;
925                 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
926                         return 1; /* skip this altsetting */
927                 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
928                         return 1; /* skip this altsetting */
929                 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
930                         return 1; /* skip this altsetting */
931                 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
932                         return 1; /* skip this altsetting */
933         }
934
935         return 0; /* keep this altsetting */
936 }
937
938 static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
939                                            int iface, int altno)
940 {
941         /* Reset ALL ifaces to 0 altsetting.
942          * Call it for every possible altsetting of every interface.
943          */
944         usb_set_interface(chip->dev, iface, 0);
945
946         /* possible configuration where both inputs and only one output is
947          *used is not supported by the current setup
948          */
949         if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
950                 if (chip->setup & MAUDIO_SET_96K) {
951                         if (altno != 3 && altno != 6)
952                                 return 1;
953                 } else if (chip->setup & MAUDIO_SET_DI) {
954                         if (iface == 4)
955                                 return 1; /* no analog input */
956                         if (altno != 2 && altno != 5)
957                                 return 1; /* enable only altsets 2 and 5 */
958                 } else {
959                         if (iface == 5)
960                                 return 1; /* disable digialt input */
961                         if (altno != 2 && altno != 5)
962                                 return 1; /* enalbe only altsets 2 and 5 */
963                 }
964         } else {
965                 /* keep only 16-Bit mode */
966                 if (altno != 1)
967                         return 1;
968         }
969
970         usb_audio_dbg(chip,
971                     "using altsetting %d for interface %d config %d\n",
972                     altno, iface, chip->setup);
973         return 0; /* keep this altsetting */
974 }
975
976 int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
977                                   int iface,
978                                   int altno)
979 {
980         /* audiophile usb: skip altsets incompatible with device_setup */
981         if (chip->usb_id == USB_ID(0x0763, 0x2003))
982                 return audiophile_skip_setting_quirk(chip, iface, altno);
983         /* quattro usb: skip altsets incompatible with device_setup */
984         if (chip->usb_id == USB_ID(0x0763, 0x2001))
985                 return quattro_skip_setting_quirk(chip, iface, altno);
986         /* fasttrackpro usb: skip altsets incompatible with device_setup */
987         if (chip->usb_id == USB_ID(0x0763, 0x2012))
988                 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
989
990         return 0;
991 }
992
993 int snd_usb_apply_boot_quirk(struct usb_device *dev,
994                              struct usb_interface *intf,
995                              const struct snd_usb_audio_quirk *quirk,
996                              unsigned int id)
997 {
998         switch (id) {
999         case USB_ID(0x041e, 0x3000):
1000                 /* SB Extigy needs special boot-up sequence */
1001                 /* if more models come, this will go to the quirk list. */
1002                 return snd_usb_extigy_boot_quirk(dev, intf);
1003
1004         case USB_ID(0x041e, 0x3020):
1005                 /* SB Audigy 2 NX needs its own boot-up magic, too */
1006                 return snd_usb_audigy2nx_boot_quirk(dev);
1007
1008         case USB_ID(0x10f5, 0x0200):
1009                 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
1010                 return snd_usb_cm106_boot_quirk(dev);
1011
1012         case USB_ID(0x0d8c, 0x0102):
1013                 /* C-Media CM6206 / CM106-Like Sound Device */
1014         case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
1015                 return snd_usb_cm6206_boot_quirk(dev);
1016
1017         case USB_ID(0x0dba, 0x3000):
1018                 /* Digidesign Mbox 2 */
1019                 return snd_usb_mbox2_boot_quirk(dev);
1020
1021         case USB_ID(0x1235, 0x0010): /* Focusrite Novation Saffire 6 USB */
1022         case USB_ID(0x1235, 0x0018): /* Focusrite Novation Twitch */
1023                 return snd_usb_novation_boot_quirk(dev);
1024
1025         case USB_ID(0x133e, 0x0815):
1026                 /* Access Music VirusTI Desktop */
1027                 return snd_usb_accessmusic_boot_quirk(dev);
1028
1029         case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
1030         case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
1031         case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
1032                 return snd_usb_nativeinstruments_boot_quirk(dev);
1033         case USB_ID(0x0763, 0x2012):  /* M-Audio Fast Track Pro USB */
1034                 return snd_usb_fasttrackpro_boot_quirk(dev);
1035         case USB_ID(0x047f, 0xc010): /* Plantronics Gamecom 780 */
1036                 return snd_usb_gamecon780_boot_quirk(dev);
1037         }
1038
1039         return 0;
1040 }
1041
1042 /*
1043  * check if the device uses big-endian samples
1044  */
1045 int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
1046 {
1047         /* it depends on altsetting whether the device is big-endian or not */
1048         switch (chip->usb_id) {
1049         case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
1050                 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1051                         fp->altsetting == 5 || fp->altsetting == 6)
1052                         return 1;
1053                 break;
1054         case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
1055                 if (chip->setup == 0x00 ||
1056                         fp->altsetting == 1 || fp->altsetting == 2 ||
1057                         fp->altsetting == 3)
1058                         return 1;
1059                 break;
1060         case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
1061                 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1062                         fp->altsetting == 5 || fp->altsetting == 6)
1063                         return 1;
1064                 break;
1065         }
1066         return 0;
1067 }
1068
1069 /*
1070  * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
1071  * not for interface.
1072  */
1073
1074 enum {
1075         EMU_QUIRK_SR_44100HZ = 0,
1076         EMU_QUIRK_SR_48000HZ,
1077         EMU_QUIRK_SR_88200HZ,
1078         EMU_QUIRK_SR_96000HZ,
1079         EMU_QUIRK_SR_176400HZ,
1080         EMU_QUIRK_SR_192000HZ
1081 };
1082
1083 static void set_format_emu_quirk(struct snd_usb_substream *subs,
1084                                  struct audioformat *fmt)
1085 {
1086         unsigned char emu_samplerate_id = 0;
1087
1088         /* When capture is active
1089          * sample rate shouldn't be changed
1090          * by playback substream
1091          */
1092         if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1093                 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
1094                         return;
1095         }
1096
1097         switch (fmt->rate_min) {
1098         case 48000:
1099                 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
1100                 break;
1101         case 88200:
1102                 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
1103                 break;
1104         case 96000:
1105                 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
1106                 break;
1107         case 176400:
1108                 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
1109                 break;
1110         case 192000:
1111                 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
1112                 break;
1113         default:
1114                 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
1115                 break;
1116         }
1117         snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
1118         subs->pkt_offset_adj = (emu_samplerate_id >= EMU_QUIRK_SR_176400HZ) ? 4 : 0;
1119 }
1120
1121 void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
1122                               struct audioformat *fmt)
1123 {
1124         switch (subs->stream->chip->usb_id) {
1125         case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
1126         case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
1127         case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
1128         case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
1129                 set_format_emu_quirk(subs, fmt);
1130                 break;
1131         case USB_ID(0x534d, 0x2109): /* MacroSilicon MS2109 */
1132                 subs->stream_offset_adj = 2;
1133                 break;
1134         }
1135 }
1136
1137 bool snd_usb_get_sample_rate_quirk(struct snd_usb_audio *chip)
1138 {
1139         /* devices which do not support reading the sample rate. */
1140         switch (chip->usb_id) {
1141         case USB_ID(0x041E, 0x4080): /* Creative Live Cam VF0610 */
1142         case USB_ID(0x045E, 0x075D): /* MS Lifecam Cinema  */
1143         case USB_ID(0x045E, 0x076D): /* MS Lifecam HD-5000 */
1144         case USB_ID(0x045E, 0x076E): /* MS Lifecam HD-5001 */
1145         case USB_ID(0x045E, 0x076F): /* MS Lifecam HD-6000 */
1146         case USB_ID(0x045E, 0x0772): /* MS Lifecam Studio */
1147         case USB_ID(0x045E, 0x0779): /* MS Lifecam HD-3000 */
1148         case USB_ID(0x047F, 0x02F7): /* Plantronics BT-600 */
1149         case USB_ID(0x047F, 0x0415): /* Plantronics BT-300 */
1150         case USB_ID(0x047F, 0xAA05): /* Plantronics DA45 */
1151         case USB_ID(0x047F, 0xC022): /* Plantronics C310 */
1152         case USB_ID(0x047F, 0xC02F): /* Plantronics P610 */
1153         case USB_ID(0x047F, 0xC036): /* Plantronics C520-M */
1154         case USB_ID(0x04D8, 0xFEEA): /* Benchmark DAC1 Pre */
1155         case USB_ID(0x0556, 0x0014): /* Phoenix Audio TMX320VC */
1156         case USB_ID(0x05A3, 0x9420): /* ELP HD USB Camera */
1157         case USB_ID(0x05a7, 0x1020): /* Bose Companion 5 */
1158         case USB_ID(0x074D, 0x3553): /* Outlaw RR2150 (Micronas UAC3553B) */
1159         case USB_ID(0x1395, 0x740a): /* Sennheiser DECT */
1160         case USB_ID(0x1901, 0x0191): /* GE B850V3 CP2114 audio interface */
1161         case USB_ID(0x1de7, 0x0013): /* Phoenix Audio MT202exe */
1162         case USB_ID(0x1de7, 0x0014): /* Phoenix Audio TMX320 */
1163         case USB_ID(0x1de7, 0x0114): /* Phoenix Audio MT202pcs */
1164         case USB_ID(0x21B4, 0x0081): /* AudioQuest DragonFly */
1165         case USB_ID(0x2912, 0x30c8): /* Audioengine D1 */
1166         case USB_ID(0x413c, 0xa506): /* Dell AE515 sound bar */
1167         case USB_ID(0x046d, 0x084c): /* Logitech ConferenceCam Connect */
1168                 return true;
1169         }
1170         return false;
1171 }
1172
1173 /* ITF-USB DSD based DACs need a vendor cmd to switch
1174  * between PCM and native DSD mode
1175  * (2 altsets version)
1176  */
1177 static bool is_itf_usb_dsd_2alts_dac(unsigned int id)
1178 {
1179         switch (id) {
1180         case USB_ID(0x154e, 0x1002): /* Denon DCD-1500RE */
1181         case USB_ID(0x154e, 0x1003): /* Denon DA-300USB */
1182         case USB_ID(0x154e, 0x3005): /* Marantz HD-DAC1 */
1183         case USB_ID(0x154e, 0x3006): /* Marantz SA-14S1 */
1184         case USB_ID(0x1852, 0x5065): /* Luxman DA-06 */
1185                 return true;
1186         }
1187         return false;
1188 }
1189
1190 /* ITF-USB DSD based DACs need a vendor cmd to switch
1191  * between PCM and native DSD mode
1192  * (3 altsets version)
1193  */
1194 static bool is_itf_usb_dsd_3alts_dac(unsigned int id)
1195 {
1196         switch (id) {
1197         case USB_ID(0x0644, 0x8043): /* TEAC UD-501/UD-503/NT-503 */
1198         case USB_ID(0x0644, 0x8044): /* Esoteric D-05X */
1199         case USB_ID(0x0644, 0x804a): /* TEAC UD-301 */
1200                 return true;
1201         }
1202         return false;
1203 }
1204
1205 int snd_usb_select_mode_quirk(struct snd_usb_substream *subs,
1206                               struct audioformat *fmt)
1207 {
1208         struct usb_device *dev = subs->dev;
1209         int err;
1210
1211         if (is_itf_usb_dsd_2alts_dac(subs->stream->chip->usb_id)) {
1212                 /* First switch to alt set 0, otherwise the mode switch cmd
1213                  * will not be accepted by the DAC
1214                  */
1215                 err = usb_set_interface(dev, fmt->iface, 0);
1216                 if (err < 0)
1217                         return err;
1218
1219                 mdelay(20); /* Delay needed after setting the interface */
1220
1221                 switch (fmt->altsetting) {
1222                 case 2: /* DSD mode requested */
1223                 case 1: /* PCM mode requested */
1224                         err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1225                                               USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1226                                               fmt->altsetting - 1, 1, NULL, 0);
1227                         if (err < 0)
1228                                 return err;
1229                         break;
1230                 }
1231                 mdelay(20);
1232         } else if (is_itf_usb_dsd_3alts_dac(subs->stream->chip->usb_id)) {
1233                 /* Vendor mode switch cmd is required. */
1234                 switch (fmt->altsetting) {
1235                 case 3: /* DSD mode (DSD_U32) requested */
1236                         err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1237                                               USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1238                                               1, 1, NULL, 0);
1239                         if (err < 0)
1240                                 return err;
1241                         break;
1242
1243                 case 2: /* PCM or DOP mode (S32) requested */
1244                 case 1: /* PCM mode (S16) requested */
1245                         err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1246                                               USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1247                                               0, 1, NULL, 0);
1248                         if (err < 0)
1249                                 return err;
1250                         break;
1251                 }
1252         }
1253         return 0;
1254 }
1255
1256 void snd_usb_endpoint_start_quirk(struct snd_usb_endpoint *ep)
1257 {
1258         /*
1259          * "Playback Design" products send bogus feedback data at the start
1260          * of the stream. Ignore them.
1261          */
1262         if (USB_ID_VENDOR(ep->chip->usb_id) == 0x23ba &&
1263             ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
1264                 ep->skip_packets = 4;
1265
1266         /*
1267          * M-Audio Fast Track C400/C600 - when packets are not skipped, real
1268          * world latency varies by approx. +/- 50 frames (at 96KHz) each time
1269          * the stream is (re)started. When skipping packets 16 at endpoint
1270          * start up, the real world latency is stable within +/- 1 frame (also
1271          * across power cycles).
1272          */
1273         if ((ep->chip->usb_id == USB_ID(0x0763, 0x2030) ||
1274              ep->chip->usb_id == USB_ID(0x0763, 0x2031)) &&
1275             ep->type == SND_USB_ENDPOINT_TYPE_DATA)
1276                 ep->skip_packets = 16;
1277
1278         /* Work around devices that report unreasonable feedback data */
1279         if ((ep->chip->usb_id == USB_ID(0x0644, 0x8038) ||  /* TEAC UD-H01 */
1280              ep->chip->usb_id == USB_ID(0x1852, 0x5034)) && /* T+A Dac8 */
1281             ep->syncmaxsize == 4)
1282                 ep->tenor_fb_quirk = 1;
1283 }
1284
1285 void snd_usb_set_interface_quirk(struct usb_device *dev)
1286 {
1287         struct snd_usb_audio *chip = dev_get_drvdata(&dev->dev);
1288
1289         if (!chip)
1290                 return;
1291         /*
1292          * "Playback Design" products need a 50ms delay after setting the
1293          * USB interface.
1294          */
1295         switch (USB_ID_VENDOR(chip->usb_id)) {
1296         case 0x23ba: /* Playback Design */
1297         case 0x0644: /* TEAC Corp. */
1298                 mdelay(50);
1299                 break;
1300         }
1301 }
1302
1303 /* quirk applied after snd_usb_ctl_msg(); not applied during boot quirks */
1304 void snd_usb_ctl_msg_quirk(struct usb_device *dev, unsigned int pipe,
1305                            __u8 request, __u8 requesttype, __u16 value,
1306                            __u16 index, void *data, __u16 size)
1307 {
1308         struct snd_usb_audio *chip = dev_get_drvdata(&dev->dev);
1309
1310         if (!chip)
1311                 return;
1312         /*
1313          * "Playback Design" products need a 20ms delay after each
1314          * class compliant request
1315          */
1316         if (USB_ID_VENDOR(chip->usb_id) == 0x23ba &&
1317             (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1318                 mdelay(20);
1319
1320         /*
1321          * "TEAC Corp." products need a 20ms delay after each
1322          * class compliant request
1323          */
1324         if (USB_ID_VENDOR(chip->usb_id) == 0x0644 &&
1325             (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1326                 mdelay(20);
1327
1328         /* ITF-USB DSD based DACs functionality need a delay
1329          * after each class compliant request
1330          */
1331         if (is_itf_usb_dsd_2alts_dac(chip->usb_id)
1332             && (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1333                 mdelay(20);
1334
1335         /* Zoom R16/24, Logitech H650e/H570e, Jabra 550a, Kingston HyperX
1336          *  needs a tiny delay here, otherwise requests like get/set
1337          *  frequency return as failed despite actually succeeding.
1338          */
1339         if ((chip->usb_id == USB_ID(0x1686, 0x00dd) ||
1340              chip->usb_id == USB_ID(0x046d, 0x0a46) ||
1341              chip->usb_id == USB_ID(0x046d, 0x0a56) ||
1342              chip->usb_id == USB_ID(0x0b0e, 0x0349) ||
1343              chip->usb_id == USB_ID(0x0951, 0x16ad)) &&
1344             (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1345                 mdelay(1);
1346 }
1347
1348 /*
1349  * snd_usb_interface_dsd_format_quirks() is called from format.c to
1350  * augment the PCM format bit-field for DSD types. The UAC standards
1351  * don't have a designated bit field to denote DSD-capable interfaces,
1352  * hence all hardware that is known to support this format has to be
1353  * listed here.
1354  */
1355 u64 snd_usb_interface_dsd_format_quirks(struct snd_usb_audio *chip,
1356                                         struct audioformat *fp,
1357                                         unsigned int sample_bytes)
1358 {
1359         /* Playback Designs */
1360         if (USB_ID_VENDOR(chip->usb_id) == 0x23ba) {
1361                 switch (fp->altsetting) {
1362                 case 1:
1363                         fp->dsd_dop = true;
1364                         return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1365                 case 2:
1366                         fp->dsd_bitrev = true;
1367                         return SNDRV_PCM_FMTBIT_DSD_U8;
1368                 case 3:
1369                         fp->dsd_bitrev = true;
1370                         return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1371                 }
1372         }
1373
1374         /* XMOS based USB DACs */
1375         switch (chip->usb_id) {
1376         case USB_ID(0x20b1, 0x3008): /* iFi Audio micro/nano iDSD */
1377         case USB_ID(0x20b1, 0x2008): /* Matrix Audio X-Sabre */
1378         case USB_ID(0x20b1, 0x300a): /* Matrix Audio Mini-i Pro */
1379         case USB_ID(0x22d9, 0x0416): /* OPPO HA-1 */
1380         case USB_ID(0x2772, 0x0230): /* Pro-Ject Pre Box S2 Digital */
1381                 if (fp->altsetting == 2)
1382                         return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1383                 break;
1384
1385         case USB_ID(0x20b1, 0x000a): /* Gustard DAC-X20U */
1386         case USB_ID(0x20b1, 0x2009): /* DIYINHK DSD DXD 384kHz USB to I2S/DSD */
1387         case USB_ID(0x20b1, 0x2023): /* JLsounds I2SoverUSB */
1388         case USB_ID(0x20b1, 0x3023): /* Aune X1S 32BIT/384 DSD DAC */
1389         case USB_ID(0x2616, 0x0106): /* PS Audio NuWave DAC */
1390                 if (fp->altsetting == 3)
1391                         return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1392                 break;
1393
1394         /* Amanero Combo384 USB based DACs with native DSD support */
1395         case USB_ID(0x16d0, 0x071a):  /* Amanero - Combo384 */
1396         case USB_ID(0x2ab6, 0x0004):  /* T+A DAC8DSD-V2.0, MP1000E-V2.0, MP2000R-V2.0, MP2500R-V2.0, MP3100HV-V2.0 */
1397         case USB_ID(0x2ab6, 0x0005):  /* T+A USB HD Audio 1 */
1398         case USB_ID(0x2ab6, 0x0006):  /* T+A USB HD Audio 2 */
1399                 if (fp->altsetting == 2) {
1400                         switch (le16_to_cpu(chip->dev->descriptor.bcdDevice)) {
1401                         case 0x199:
1402                                 return SNDRV_PCM_FMTBIT_DSD_U32_LE;
1403                         case 0x19b:
1404                         case 0x203:
1405                                 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1406                         default:
1407                                 break;
1408                         }
1409                 }
1410                 break;
1411         case USB_ID(0x16d0, 0x0a23):
1412                 if (fp->altsetting == 2)
1413                         return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1414                 break;
1415
1416         default:
1417                 break;
1418         }
1419
1420         /* ITF-USB DSD based DACs (2 altsets version) */
1421         if (is_itf_usb_dsd_2alts_dac(chip->usb_id)) {
1422                 if (fp->altsetting == 2)
1423                         return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1424         }
1425
1426         /* ITF-USB DSD based DACs (3 altsets version) */
1427         if (is_itf_usb_dsd_3alts_dac(chip->usb_id)) {
1428                 if (fp->altsetting == 3)
1429                         return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1430         }
1431
1432         return 0;
1433 }