ALSA: usb-audio: Manage number of rawmidis globally
[linux-block.git] / sound / usb / quirks.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  */
4
5 #include <linux/init.h>
6 #include <linux/slab.h>
7 #include <linux/usb.h>
8 #include <linux/usb/audio.h>
9 #include <linux/usb/midi.h>
10 #include <linux/bits.h>
11
12 #include <sound/control.h>
13 #include <sound/core.h>
14 #include <sound/info.h>
15 #include <sound/pcm.h>
16
17 #include "usbaudio.h"
18 #include "card.h"
19 #include "mixer.h"
20 #include "mixer_quirks.h"
21 #include "midi.h"
22 #include "quirks.h"
23 #include "helper.h"
24 #include "endpoint.h"
25 #include "pcm.h"
26 #include "clock.h"
27 #include "stream.h"
28
29 /*
30  * handle the quirks for the contained interfaces
31  */
32 static int create_composite_quirk(struct snd_usb_audio *chip,
33                                   struct usb_interface *iface,
34                                   struct usb_driver *driver,
35                                   const struct snd_usb_audio_quirk *quirk_comp)
36 {
37         int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
38         const struct snd_usb_audio_quirk *quirk;
39         int err;
40
41         for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
42                 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
43                 if (!iface)
44                         continue;
45                 if (quirk->ifnum != probed_ifnum &&
46                     usb_interface_claimed(iface))
47                         continue;
48                 err = snd_usb_create_quirk(chip, iface, driver, quirk);
49                 if (err < 0)
50                         return err;
51         }
52
53         for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
54                 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
55                 if (!iface)
56                         continue;
57                 if (quirk->ifnum != probed_ifnum &&
58                     !usb_interface_claimed(iface)) {
59                         err = usb_driver_claim_interface(driver, iface,
60                                                          USB_AUDIO_IFACE_UNUSED);
61                         if (err < 0)
62                                 return err;
63                 }
64         }
65
66         return 0;
67 }
68
69 static int ignore_interface_quirk(struct snd_usb_audio *chip,
70                                   struct usb_interface *iface,
71                                   struct usb_driver *driver,
72                                   const struct snd_usb_audio_quirk *quirk)
73 {
74         return 0;
75 }
76
77
78 static int create_any_midi_quirk(struct snd_usb_audio *chip,
79                                  struct usb_interface *intf,
80                                  struct usb_driver *driver,
81                                  const struct snd_usb_audio_quirk *quirk)
82 {
83         return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
84 }
85
86 /*
87  * create a stream for an interface with proper descriptors
88  */
89 static int create_standard_audio_quirk(struct snd_usb_audio *chip,
90                                        struct usb_interface *iface,
91                                        struct usb_driver *driver,
92                                        const struct snd_usb_audio_quirk *quirk)
93 {
94         struct usb_host_interface *alts;
95         struct usb_interface_descriptor *altsd;
96         int err;
97
98         alts = &iface->altsetting[0];
99         altsd = get_iface_desc(alts);
100         err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
101         if (err < 0) {
102                 usb_audio_err(chip, "cannot setup if %d: error %d\n",
103                            altsd->bInterfaceNumber, err);
104                 return err;
105         }
106         /* reset the current interface */
107         usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
108         return 0;
109 }
110
111 /* create the audio stream and the corresponding endpoints from the fixed
112  * audioformat object; this is used for quirks with the fixed EPs
113  */
114 static int add_audio_stream_from_fixed_fmt(struct snd_usb_audio *chip,
115                                            struct audioformat *fp)
116 {
117         int stream, err;
118
119         stream = (fp->endpoint & USB_DIR_IN) ?
120                 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
121
122         snd_usb_audioformat_set_sync_ep(chip, fp);
123
124         err = snd_usb_add_audio_stream(chip, stream, fp);
125         if (err < 0)
126                 return err;
127
128         err = snd_usb_add_endpoint(chip, fp->endpoint,
129                                    SND_USB_ENDPOINT_TYPE_DATA);
130         if (err < 0)
131                 return err;
132
133         if (fp->sync_ep) {
134                 err = snd_usb_add_endpoint(chip, fp->sync_ep,
135                                            fp->implicit_fb ?
136                                            SND_USB_ENDPOINT_TYPE_DATA :
137                                            SND_USB_ENDPOINT_TYPE_SYNC);
138                 if (err < 0)
139                         return err;
140         }
141
142         return 0;
143 }
144
145 /*
146  * create a stream for an endpoint/altsetting without proper descriptors
147  */
148 static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
149                                      struct usb_interface *iface,
150                                      struct usb_driver *driver,
151                                      const struct snd_usb_audio_quirk *quirk)
152 {
153         struct audioformat *fp;
154         struct usb_host_interface *alts;
155         struct usb_interface_descriptor *altsd;
156         unsigned *rate_table = NULL;
157         int err;
158
159         fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
160         if (!fp)
161                 return -ENOMEM;
162
163         INIT_LIST_HEAD(&fp->list);
164         if (fp->nr_rates > MAX_NR_RATES) {
165                 kfree(fp);
166                 return -EINVAL;
167         }
168         if (fp->nr_rates > 0) {
169                 rate_table = kmemdup(fp->rate_table,
170                                      sizeof(int) * fp->nr_rates, GFP_KERNEL);
171                 if (!rate_table) {
172                         kfree(fp);
173                         return -ENOMEM;
174                 }
175                 fp->rate_table = rate_table;
176         }
177
178         if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
179             fp->altset_idx >= iface->num_altsetting) {
180                 err = -EINVAL;
181                 goto error;
182         }
183         alts = &iface->altsetting[fp->altset_idx];
184         altsd = get_iface_desc(alts);
185         if (altsd->bNumEndpoints <= fp->ep_idx) {
186                 err = -EINVAL;
187                 goto error;
188         }
189
190         fp->protocol = altsd->bInterfaceProtocol;
191
192         if (fp->datainterval == 0)
193                 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
194         if (fp->maxpacksize == 0)
195                 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, fp->ep_idx)->wMaxPacketSize);
196         if (!fp->fmt_type)
197                 fp->fmt_type = UAC_FORMAT_TYPE_I;
198
199         err = add_audio_stream_from_fixed_fmt(chip, fp);
200         if (err < 0)
201                 goto error;
202
203         usb_set_interface(chip->dev, fp->iface, 0);
204         snd_usb_init_pitch(chip, fp);
205         snd_usb_init_sample_rate(chip, fp, fp->rate_max);
206         return 0;
207
208  error:
209         list_del(&fp->list); /* unlink for avoiding double-free */
210         kfree(fp);
211         kfree(rate_table);
212         return err;
213 }
214
215 static int create_auto_pcm_quirk(struct snd_usb_audio *chip,
216                                  struct usb_interface *iface,
217                                  struct usb_driver *driver)
218 {
219         struct usb_host_interface *alts;
220         struct usb_interface_descriptor *altsd;
221         struct usb_endpoint_descriptor *epd;
222         struct uac1_as_header_descriptor *ashd;
223         struct uac_format_type_i_discrete_descriptor *fmtd;
224
225         /*
226          * Most Roland/Yamaha audio streaming interfaces have more or less
227          * standard descriptors, but older devices might lack descriptors, and
228          * future ones might change, so ensure that we fail silently if the
229          * interface doesn't look exactly right.
230          */
231
232         /* must have a non-zero altsetting for streaming */
233         if (iface->num_altsetting < 2)
234                 return -ENODEV;
235         alts = &iface->altsetting[1];
236         altsd = get_iface_desc(alts);
237
238         /* must have an isochronous endpoint for streaming */
239         if (altsd->bNumEndpoints < 1)
240                 return -ENODEV;
241         epd = get_endpoint(alts, 0);
242         if (!usb_endpoint_xfer_isoc(epd))
243                 return -ENODEV;
244
245         /* must have format descriptors */
246         ashd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
247                                        UAC_AS_GENERAL);
248         fmtd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
249                                        UAC_FORMAT_TYPE);
250         if (!ashd || ashd->bLength < 7 ||
251             !fmtd || fmtd->bLength < 8)
252                 return -ENODEV;
253
254         return create_standard_audio_quirk(chip, iface, driver, NULL);
255 }
256
257 static int create_yamaha_midi_quirk(struct snd_usb_audio *chip,
258                                     struct usb_interface *iface,
259                                     struct usb_driver *driver,
260                                     struct usb_host_interface *alts)
261 {
262         static const struct snd_usb_audio_quirk yamaha_midi_quirk = {
263                 .type = QUIRK_MIDI_YAMAHA
264         };
265         struct usb_midi_in_jack_descriptor *injd;
266         struct usb_midi_out_jack_descriptor *outjd;
267
268         /* must have some valid jack descriptors */
269         injd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
270                                        NULL, USB_MS_MIDI_IN_JACK);
271         outjd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
272                                         NULL, USB_MS_MIDI_OUT_JACK);
273         if (!injd && !outjd)
274                 return -ENODEV;
275         if ((injd && !snd_usb_validate_midi_desc(injd)) ||
276             (outjd && !snd_usb_validate_midi_desc(outjd)))
277                 return -ENODEV;
278         if (injd && (injd->bLength < 5 ||
279                      (injd->bJackType != USB_MS_EMBEDDED &&
280                       injd->bJackType != USB_MS_EXTERNAL)))
281                 return -ENODEV;
282         if (outjd && (outjd->bLength < 6 ||
283                       (outjd->bJackType != USB_MS_EMBEDDED &&
284                        outjd->bJackType != USB_MS_EXTERNAL)))
285                 return -ENODEV;
286         return create_any_midi_quirk(chip, iface, driver, &yamaha_midi_quirk);
287 }
288
289 static int create_roland_midi_quirk(struct snd_usb_audio *chip,
290                                     struct usb_interface *iface,
291                                     struct usb_driver *driver,
292                                     struct usb_host_interface *alts)
293 {
294         static const struct snd_usb_audio_quirk roland_midi_quirk = {
295                 .type = QUIRK_MIDI_ROLAND
296         };
297         u8 *roland_desc = NULL;
298
299         /* might have a vendor-specific descriptor <06 24 F1 02 ...> */
300         for (;;) {
301                 roland_desc = snd_usb_find_csint_desc(alts->extra,
302                                                       alts->extralen,
303                                                       roland_desc, 0xf1);
304                 if (!roland_desc)
305                         return -ENODEV;
306                 if (roland_desc[0] < 6 || roland_desc[3] != 2)
307                         continue;
308                 return create_any_midi_quirk(chip, iface, driver,
309                                              &roland_midi_quirk);
310         }
311 }
312
313 static int create_std_midi_quirk(struct snd_usb_audio *chip,
314                                  struct usb_interface *iface,
315                                  struct usb_driver *driver,
316                                  struct usb_host_interface *alts)
317 {
318         struct usb_ms_header_descriptor *mshd;
319         struct usb_ms_endpoint_descriptor *msepd;
320
321         /* must have the MIDIStreaming interface header descriptor*/
322         mshd = (struct usb_ms_header_descriptor *)alts->extra;
323         if (alts->extralen < 7 ||
324             mshd->bLength < 7 ||
325             mshd->bDescriptorType != USB_DT_CS_INTERFACE ||
326             mshd->bDescriptorSubtype != USB_MS_HEADER)
327                 return -ENODEV;
328         /* must have the MIDIStreaming endpoint descriptor*/
329         msepd = (struct usb_ms_endpoint_descriptor *)alts->endpoint[0].extra;
330         if (alts->endpoint[0].extralen < 4 ||
331             msepd->bLength < 4 ||
332             msepd->bDescriptorType != USB_DT_CS_ENDPOINT ||
333             msepd->bDescriptorSubtype != UAC_MS_GENERAL ||
334             msepd->bNumEmbMIDIJack < 1 ||
335             msepd->bNumEmbMIDIJack > 16)
336                 return -ENODEV;
337
338         return create_any_midi_quirk(chip, iface, driver, NULL);
339 }
340
341 static int create_auto_midi_quirk(struct snd_usb_audio *chip,
342                                   struct usb_interface *iface,
343                                   struct usb_driver *driver)
344 {
345         struct usb_host_interface *alts;
346         struct usb_interface_descriptor *altsd;
347         struct usb_endpoint_descriptor *epd;
348         int err;
349
350         alts = &iface->altsetting[0];
351         altsd = get_iface_desc(alts);
352
353         /* must have at least one bulk/interrupt endpoint for streaming */
354         if (altsd->bNumEndpoints < 1)
355                 return -ENODEV;
356         epd = get_endpoint(alts, 0);
357         if (!usb_endpoint_xfer_bulk(epd) &&
358             !usb_endpoint_xfer_int(epd))
359                 return -ENODEV;
360
361         switch (USB_ID_VENDOR(chip->usb_id)) {
362         case 0x0499: /* Yamaha */
363                 err = create_yamaha_midi_quirk(chip, iface, driver, alts);
364                 if (err != -ENODEV)
365                         return err;
366                 break;
367         case 0x0582: /* Roland */
368                 err = create_roland_midi_quirk(chip, iface, driver, alts);
369                 if (err != -ENODEV)
370                         return err;
371                 break;
372         }
373
374         return create_std_midi_quirk(chip, iface, driver, alts);
375 }
376
377 static int create_autodetect_quirk(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 err;
383
384         err = create_auto_pcm_quirk(chip, iface, driver);
385         if (err == -ENODEV)
386                 err = create_auto_midi_quirk(chip, iface, driver);
387         return err;
388 }
389
390 /*
391  * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.  
392  * The only way to detect the sample rate is by looking at wMaxPacketSize.
393  */
394 static int create_uaxx_quirk(struct snd_usb_audio *chip,
395                              struct usb_interface *iface,
396                              struct usb_driver *driver,
397                              const struct snd_usb_audio_quirk *quirk)
398 {
399         static const struct audioformat ua_format = {
400                 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
401                 .channels = 2,
402                 .fmt_type = UAC_FORMAT_TYPE_I,
403                 .altsetting = 1,
404                 .altset_idx = 1,
405                 .rates = SNDRV_PCM_RATE_CONTINUOUS,
406         };
407         struct usb_host_interface *alts;
408         struct usb_interface_descriptor *altsd;
409         struct audioformat *fp;
410         int err;
411
412         /* both PCM and MIDI interfaces have 2 or more altsettings */
413         if (iface->num_altsetting < 2)
414                 return -ENXIO;
415         alts = &iface->altsetting[1];
416         altsd = get_iface_desc(alts);
417
418         if (altsd->bNumEndpoints == 2) {
419                 static const struct snd_usb_midi_endpoint_info ua700_ep = {
420                         .out_cables = 0x0003,
421                         .in_cables  = 0x0003
422                 };
423                 static const struct snd_usb_audio_quirk ua700_quirk = {
424                         .type = QUIRK_MIDI_FIXED_ENDPOINT,
425                         .data = &ua700_ep
426                 };
427                 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
428                         .out_cables = 0x0001,
429                         .in_cables  = 0x0001
430                 };
431                 static const struct snd_usb_audio_quirk uaxx_quirk = {
432                         .type = QUIRK_MIDI_FIXED_ENDPOINT,
433                         .data = &uaxx_ep
434                 };
435                 const struct snd_usb_audio_quirk *quirk =
436                         chip->usb_id == USB_ID(0x0582, 0x002b)
437                         ? &ua700_quirk : &uaxx_quirk;
438                 return __snd_usbmidi_create(chip->card, iface,
439                                             &chip->midi_list, quirk,
440                                             chip->usb_id,
441                                             &chip->num_rawmidis);
442         }
443
444         if (altsd->bNumEndpoints != 1)
445                 return -ENXIO;
446
447         fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
448         if (!fp)
449                 return -ENOMEM;
450
451         fp->iface = altsd->bInterfaceNumber;
452         fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
453         fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
454         fp->datainterval = 0;
455         fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
456         INIT_LIST_HEAD(&fp->list);
457
458         switch (fp->maxpacksize) {
459         case 0x120:
460                 fp->rate_max = fp->rate_min = 44100;
461                 break;
462         case 0x138:
463         case 0x140:
464                 fp->rate_max = fp->rate_min = 48000;
465                 break;
466         case 0x258:
467         case 0x260:
468                 fp->rate_max = fp->rate_min = 96000;
469                 break;
470         default:
471                 usb_audio_err(chip, "unknown sample rate\n");
472                 kfree(fp);
473                 return -ENXIO;
474         }
475
476         err = add_audio_stream_from_fixed_fmt(chip, fp);
477         if (err < 0) {
478                 list_del(&fp->list); /* unlink for avoiding double-free */
479                 kfree(fp);
480                 return err;
481         }
482         usb_set_interface(chip->dev, fp->iface, 0);
483         return 0;
484 }
485
486 /*
487  * Create a standard mixer for the specified interface.
488  */
489 static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
490                                        struct usb_interface *iface,
491                                        struct usb_driver *driver,
492                                        const struct snd_usb_audio_quirk *quirk)
493 {
494         if (quirk->ifnum < 0)
495                 return 0;
496
497         return snd_usb_create_mixer(chip, quirk->ifnum);
498 }
499
500 /*
501  * audio-interface quirks
502  *
503  * returns zero if no standard audio/MIDI parsing is needed.
504  * returns a positive value if standard audio/midi interfaces are parsed
505  * after this.
506  * returns a negative value at error.
507  */
508 int snd_usb_create_quirk(struct snd_usb_audio *chip,
509                          struct usb_interface *iface,
510                          struct usb_driver *driver,
511                          const struct snd_usb_audio_quirk *quirk)
512 {
513         typedef int (*quirk_func_t)(struct snd_usb_audio *,
514                                     struct usb_interface *,
515                                     struct usb_driver *,
516                                     const struct snd_usb_audio_quirk *);
517         static const quirk_func_t quirk_funcs[] = {
518                 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
519                 [QUIRK_COMPOSITE] = create_composite_quirk,
520                 [QUIRK_AUTODETECT] = create_autodetect_quirk,
521                 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
522                 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
523                 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
524                 [QUIRK_MIDI_ROLAND] = create_any_midi_quirk,
525                 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
526                 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
527                 [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
528                 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
529                 [QUIRK_MIDI_CME] = create_any_midi_quirk,
530                 [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
531                 [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
532                 [QUIRK_MIDI_CH345] = create_any_midi_quirk,
533                 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
534                 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
535                 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
536                 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
537         };
538
539         if (quirk->type < QUIRK_TYPE_COUNT) {
540                 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
541         } else {
542                 usb_audio_err(chip, "invalid quirk type %d\n", quirk->type);
543                 return -ENXIO;
544         }
545 }
546
547 /*
548  * boot quirks
549  */
550
551 #define EXTIGY_FIRMWARE_SIZE_OLD 794
552 #define EXTIGY_FIRMWARE_SIZE_NEW 483
553
554 static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
555 {
556         struct usb_host_config *config = dev->actconfig;
557         int err;
558
559         if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
560             le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
561                 dev_dbg(&dev->dev, "sending Extigy boot sequence...\n");
562                 /* Send message to force it to reconnect with full interface. */
563                 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
564                                       0x10, 0x43, 0x0001, 0x000a, NULL, 0);
565                 if (err < 0)
566                         dev_dbg(&dev->dev, "error sending boot message: %d\n", err);
567                 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
568                                 &dev->descriptor, sizeof(dev->descriptor));
569                 config = dev->actconfig;
570                 if (err < 0)
571                         dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
572                 err = usb_reset_configuration(dev);
573                 if (err < 0)
574                         dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
575                 dev_dbg(&dev->dev, "extigy_boot: new boot length = %d\n",
576                             le16_to_cpu(get_cfg_desc(config)->wTotalLength));
577                 return -ENODEV; /* quit this anyway */
578         }
579         return 0;
580 }
581
582 static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
583 {
584         u8 buf = 1;
585
586         snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
587                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
588                         0, 0, &buf, 1);
589         if (buf == 0) {
590                 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
591                                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
592                                 1, 2000, NULL, 0);
593                 return -ENODEV;
594         }
595         return 0;
596 }
597
598 static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
599 {
600         int err;
601
602         if (dev->actconfig->desc.bConfigurationValue == 1) {
603                 dev_info(&dev->dev,
604                            "Fast Track Pro switching to config #2\n");
605                 /* This function has to be available by the usb core module.
606                  * if it is not avialable the boot quirk has to be left out
607                  * and the configuration has to be set by udev or hotplug
608                  * rules
609                  */
610                 err = usb_driver_set_configuration(dev, 2);
611                 if (err < 0)
612                         dev_dbg(&dev->dev,
613                                 "error usb_driver_set_configuration: %d\n",
614                                 err);
615                 /* Always return an error, so that we stop creating a device
616                    that will just be destroyed and recreated with a new
617                    configuration */
618                 return -ENODEV;
619         } else
620                 dev_info(&dev->dev, "Fast Track Pro config OK\n");
621
622         return 0;
623 }
624
625 /*
626  * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
627  * documented in the device's data sheet.
628  */
629 static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
630 {
631         u8 buf[4];
632         buf[0] = 0x20;
633         buf[1] = value & 0xff;
634         buf[2] = (value >> 8) & 0xff;
635         buf[3] = reg;
636         return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
637                                USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
638                                0, 0, &buf, 4);
639 }
640
641 static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
642 {
643         /*
644          * Enable line-out driver mode, set headphone source to front
645          * channels, enable stereo mic.
646          */
647         return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
648 }
649
650 /*
651  * CM6206 registers from the CM6206 datasheet rev 2.1
652  */
653 #define CM6206_REG0_DMA_MASTER BIT(15)
654 #define CM6206_REG0_SPDIFO_RATE_48K (2 << 12)
655 #define CM6206_REG0_SPDIFO_RATE_96K (7 << 12)
656 /* Bit 4 thru 11 is the S/PDIF category code */
657 #define CM6206_REG0_SPDIFO_CAT_CODE_GENERAL (0 << 4)
658 #define CM6206_REG0_SPDIFO_EMPHASIS_CD BIT(3)
659 #define CM6206_REG0_SPDIFO_COPYRIGHT_NA BIT(2)
660 #define CM6206_REG0_SPDIFO_NON_AUDIO BIT(1)
661 #define CM6206_REG0_SPDIFO_PRO_FORMAT BIT(0)
662
663 #define CM6206_REG1_TEST_SEL_CLK BIT(14)
664 #define CM6206_REG1_PLLBIN_EN BIT(13)
665 #define CM6206_REG1_SOFT_MUTE_EN BIT(12)
666 #define CM6206_REG1_GPIO4_OUT BIT(11)
667 #define CM6206_REG1_GPIO4_OE BIT(10)
668 #define CM6206_REG1_GPIO3_OUT BIT(9)
669 #define CM6206_REG1_GPIO3_OE BIT(8)
670 #define CM6206_REG1_GPIO2_OUT BIT(7)
671 #define CM6206_REG1_GPIO2_OE BIT(6)
672 #define CM6206_REG1_GPIO1_OUT BIT(5)
673 #define CM6206_REG1_GPIO1_OE BIT(4)
674 #define CM6206_REG1_SPDIFO_INVALID BIT(3)
675 #define CM6206_REG1_SPDIF_LOOP_EN BIT(2)
676 #define CM6206_REG1_SPDIFO_DIS BIT(1)
677 #define CM6206_REG1_SPDIFI_MIX BIT(0)
678
679 #define CM6206_REG2_DRIVER_ON BIT(15)
680 #define CM6206_REG2_HEADP_SEL_SIDE_CHANNELS (0 << 13)
681 #define CM6206_REG2_HEADP_SEL_SURROUND_CHANNELS (1 << 13)
682 #define CM6206_REG2_HEADP_SEL_CENTER_SUBW (2 << 13)
683 #define CM6206_REG2_HEADP_SEL_FRONT_CHANNELS (3 << 13)
684 #define CM6206_REG2_MUTE_HEADPHONE_RIGHT BIT(12)
685 #define CM6206_REG2_MUTE_HEADPHONE_LEFT BIT(11)
686 #define CM6206_REG2_MUTE_REAR_SURROUND_RIGHT BIT(10)
687 #define CM6206_REG2_MUTE_REAR_SURROUND_LEFT BIT(9)
688 #define CM6206_REG2_MUTE_SIDE_SURROUND_RIGHT BIT(8)
689 #define CM6206_REG2_MUTE_SIDE_SURROUND_LEFT BIT(7)
690 #define CM6206_REG2_MUTE_SUBWOOFER BIT(6)
691 #define CM6206_REG2_MUTE_CENTER BIT(5)
692 #define CM6206_REG2_MUTE_RIGHT_FRONT BIT(3)
693 #define CM6206_REG2_MUTE_LEFT_FRONT BIT(3)
694 #define CM6206_REG2_EN_BTL BIT(2)
695 #define CM6206_REG2_MCUCLKSEL_1_5_MHZ (0)
696 #define CM6206_REG2_MCUCLKSEL_3_MHZ (1)
697 #define CM6206_REG2_MCUCLKSEL_6_MHZ (2)
698 #define CM6206_REG2_MCUCLKSEL_12_MHZ (3)
699
700 /* Bit 11..13 sets the sensitivity to FLY tuner volume control VP/VD signal */
701 #define CM6206_REG3_FLYSPEED_DEFAULT (2 << 11)
702 #define CM6206_REG3_VRAP25EN BIT(10)
703 #define CM6206_REG3_MSEL1 BIT(9)
704 #define CM6206_REG3_SPDIFI_RATE_44_1K BIT(0 << 7)
705 #define CM6206_REG3_SPDIFI_RATE_48K BIT(2 << 7)
706 #define CM6206_REG3_SPDIFI_RATE_32K BIT(3 << 7)
707 #define CM6206_REG3_PINSEL BIT(6)
708 #define CM6206_REG3_FOE BIT(5)
709 #define CM6206_REG3_ROE BIT(4)
710 #define CM6206_REG3_CBOE BIT(3)
711 #define CM6206_REG3_LOSE BIT(2)
712 #define CM6206_REG3_HPOE BIT(1)
713 #define CM6206_REG3_SPDIFI_CANREC BIT(0)
714
715 #define CM6206_REG5_DA_RSTN BIT(13)
716 #define CM6206_REG5_AD_RSTN BIT(12)
717 #define CM6206_REG5_SPDIFO_AD2SPDO BIT(12)
718 #define CM6206_REG5_SPDIFO_SEL_FRONT (0 << 9)
719 #define CM6206_REG5_SPDIFO_SEL_SIDE_SUR (1 << 9)
720 #define CM6206_REG5_SPDIFO_SEL_CEN_LFE (2 << 9)
721 #define CM6206_REG5_SPDIFO_SEL_REAR_SUR (3 << 9)
722 #define CM6206_REG5_CODECM BIT(8)
723 #define CM6206_REG5_EN_HPF BIT(7)
724 #define CM6206_REG5_T_SEL_DSDA4 BIT(6)
725 #define CM6206_REG5_T_SEL_DSDA3 BIT(5)
726 #define CM6206_REG5_T_SEL_DSDA2 BIT(4)
727 #define CM6206_REG5_T_SEL_DSDA1 BIT(3)
728 #define CM6206_REG5_T_SEL_DSDAD_NORMAL 0
729 #define CM6206_REG5_T_SEL_DSDAD_FRONT 4
730 #define CM6206_REG5_T_SEL_DSDAD_S_SURROUND 5
731 #define CM6206_REG5_T_SEL_DSDAD_CEN_LFE 6
732 #define CM6206_REG5_T_SEL_DSDAD_R_SURROUND 7
733
734 static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
735 {
736         int err  = 0, reg;
737         int val[] = {
738                 /*
739                  * Values here are chosen based on sniffing USB traffic
740                  * under Windows.
741                  *
742                  * REG0: DAC is master, sample rate 48kHz, no copyright
743                  */
744                 CM6206_REG0_SPDIFO_RATE_48K |
745                 CM6206_REG0_SPDIFO_COPYRIGHT_NA,
746                 /*
747                  * REG1: PLL binary search enable, soft mute enable.
748                  */
749                 CM6206_REG1_PLLBIN_EN |
750                 CM6206_REG1_SOFT_MUTE_EN,
751                 /*
752                  * REG2: enable output drivers,
753                  * select front channels to the headphone output,
754                  * then mute the headphone channels, run the MCU
755                  * at 1.5 MHz.
756                  */
757                 CM6206_REG2_DRIVER_ON |
758                 CM6206_REG2_HEADP_SEL_FRONT_CHANNELS |
759                 CM6206_REG2_MUTE_HEADPHONE_RIGHT |
760                 CM6206_REG2_MUTE_HEADPHONE_LEFT,
761                 /*
762                  * REG3: default flyspeed, set 2.5V mic bias
763                  * enable all line out ports and enable SPDIF
764                  */
765                 CM6206_REG3_FLYSPEED_DEFAULT |
766                 CM6206_REG3_VRAP25EN |
767                 CM6206_REG3_FOE |
768                 CM6206_REG3_ROE |
769                 CM6206_REG3_CBOE |
770                 CM6206_REG3_LOSE |
771                 CM6206_REG3_HPOE |
772                 CM6206_REG3_SPDIFI_CANREC,
773                 /* REG4 is just a bunch of GPIO lines */
774                 0x0000,
775                 /* REG5: de-assert AD/DA reset signals */
776                 CM6206_REG5_DA_RSTN |
777                 CM6206_REG5_AD_RSTN };
778
779         for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
780                 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
781                 if (err < 0)
782                         return err;
783         }
784
785         return err;
786 }
787
788 /* quirk for Plantronics GameCom 780 with CM6302 chip */
789 static int snd_usb_gamecon780_boot_quirk(struct usb_device *dev)
790 {
791         /* set the initial volume and don't change; other values are either
792          * too loud or silent due to firmware bug (bko#65251)
793          */
794         u8 buf[2] = { 0x74, 0xe3 };
795         return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
796                         USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
797                         UAC_FU_VOLUME << 8, 9 << 8, buf, 2);
798 }
799
800 /*
801  * Novation Twitch DJ controller
802  * Focusrite Novation Saffire 6 USB audio card
803  */
804 static int snd_usb_novation_boot_quirk(struct usb_device *dev)
805 {
806         /* preemptively set up the device because otherwise the
807          * raw MIDI endpoints are not active */
808         usb_set_interface(dev, 0, 1);
809         return 0;
810 }
811
812 /*
813  * This call will put the synth in "USB send" mode, i.e it will send MIDI
814  * messages through USB (this is disabled at startup). The synth will
815  * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
816  * sign on its LCD. Values here are chosen based on sniffing USB traffic
817  * under Windows.
818  */
819 static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
820 {
821         int err, actual_length;
822         /* "midi send" enable */
823         static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
824         void *buf;
825
826         if (usb_pipe_type_check(dev, usb_sndintpipe(dev, 0x05)))
827                 return -EINVAL;
828         buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
829         if (!buf)
830                 return -ENOMEM;
831         err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
832                         ARRAY_SIZE(seq), &actual_length, 1000);
833         kfree(buf);
834         if (err < 0)
835                 return err;
836
837         return 0;
838 }
839
840 /*
841  * Some sound cards from Native Instruments are in fact compliant to the USB
842  * audio standard of version 2 and other approved USB standards, even though
843  * they come up as vendor-specific device when first connected.
844  *
845  * However, they can be told to come up with a new set of descriptors
846  * upon their next enumeration, and the interfaces announced by the new
847  * descriptors will then be handled by the kernel's class drivers. As the
848  * product ID will also change, no further checks are required.
849  */
850
851 static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
852 {
853         int ret;
854
855         ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
856                                   0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
857                                   1, 0, NULL, 0, 1000);
858
859         if (ret < 0)
860                 return ret;
861
862         usb_reset_device(dev);
863
864         /* return -EAGAIN, so the creation of an audio interface for this
865          * temporary device is aborted. The device will reconnect with a
866          * new product ID */
867         return -EAGAIN;
868 }
869
870 static void mbox2_setup_48_24_magic(struct usb_device *dev)
871 {
872         u8 srate[3];
873         u8 temp[12];
874
875         /* Choose 48000Hz permanently */
876         srate[0] = 0x80;
877         srate[1] = 0xbb;
878         srate[2] = 0x00;
879
880         /* Send the magic! */
881         snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
882                 0x01, 0x22, 0x0100, 0x0085, &temp, 0x0003);
883         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
884                 0x81, 0xa2, 0x0100, 0x0085, &srate, 0x0003);
885         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
886                 0x81, 0xa2, 0x0100, 0x0086, &srate, 0x0003);
887         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
888                 0x81, 0xa2, 0x0100, 0x0003, &srate, 0x0003);
889         return;
890 }
891
892 /* Digidesign Mbox 2 needs to load firmware onboard
893  * and driver must wait a few seconds for initialisation.
894  */
895
896 #define MBOX2_FIRMWARE_SIZE    646
897 #define MBOX2_BOOT_LOADING     0x01 /* Hard coded into the device */
898 #define MBOX2_BOOT_READY       0x02 /* Hard coded into the device */
899
900 static int snd_usb_mbox2_boot_quirk(struct usb_device *dev)
901 {
902         struct usb_host_config *config = dev->actconfig;
903         int err;
904         u8 bootresponse[0x12];
905         int fwsize;
906         int count;
907
908         fwsize = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
909
910         if (fwsize != MBOX2_FIRMWARE_SIZE) {
911                 dev_err(&dev->dev, "Invalid firmware size=%d.\n", fwsize);
912                 return -ENODEV;
913         }
914
915         dev_dbg(&dev->dev, "Sending Digidesign Mbox 2 boot sequence...\n");
916
917         count = 0;
918         bootresponse[0] = MBOX2_BOOT_LOADING;
919         while ((bootresponse[0] == MBOX2_BOOT_LOADING) && (count < 10)) {
920                 msleep(500); /* 0.5 second delay */
921                 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
922                         /* Control magic - load onboard firmware */
923                         0x85, 0xc0, 0x0001, 0x0000, &bootresponse, 0x0012);
924                 if (bootresponse[0] == MBOX2_BOOT_READY)
925                         break;
926                 dev_dbg(&dev->dev, "device not ready, resending boot sequence...\n");
927                 count++;
928         }
929
930         if (bootresponse[0] != MBOX2_BOOT_READY) {
931                 dev_err(&dev->dev, "Unknown bootresponse=%d, or timed out, ignoring device.\n", bootresponse[0]);
932                 return -ENODEV;
933         }
934
935         dev_dbg(&dev->dev, "device initialised!\n");
936
937         err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
938                 &dev->descriptor, sizeof(dev->descriptor));
939         config = dev->actconfig;
940         if (err < 0)
941                 dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
942
943         err = usb_reset_configuration(dev);
944         if (err < 0)
945                 dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
946         dev_dbg(&dev->dev, "mbox2_boot: new boot length = %d\n",
947                 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
948
949         mbox2_setup_48_24_magic(dev);
950
951         dev_info(&dev->dev, "Digidesign Mbox 2: 24bit 48kHz");
952
953         return 0; /* Successful boot */
954 }
955
956 static int snd_usb_axefx3_boot_quirk(struct usb_device *dev)
957 {
958         int err;
959
960         dev_dbg(&dev->dev, "Waiting for Axe-Fx III to boot up...\n");
961
962         /* If the Axe-Fx III has not fully booted, it will timeout when trying
963          * to enable the audio streaming interface. A more generous timeout is
964          * used here to detect when the Axe-Fx III has finished booting as the
965          * set interface message will be acked once it has
966          */
967         err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
968                                 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
969                                 1, 1, NULL, 0, 120000);
970         if (err < 0) {
971                 dev_err(&dev->dev,
972                         "failed waiting for Axe-Fx III to boot: %d\n", err);
973                 return err;
974         }
975
976         dev_dbg(&dev->dev, "Axe-Fx III is now ready\n");
977
978         err = usb_set_interface(dev, 1, 0);
979         if (err < 0)
980                 dev_dbg(&dev->dev,
981                         "error stopping Axe-Fx III interface: %d\n", err);
982
983         return 0;
984 }
985
986 static void mbox3_setup_48_24_magic(struct usb_device *dev)
987 {
988         /* The Mbox 3 is "little endian" */
989         /* max volume is: 0x0000. */
990         /* min volume is: 0x0080 (shown in little endian form) */
991
992
993         /* Load 48000Hz rate into buffer */
994         u8 com_buff[4] = {0x80, 0xbb, 0x00, 0x00};
995
996         /* Set 48000Hz sample rate */
997         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
998                         0x01, 0x21, 0x0100, 0x0001, &com_buff, 4);  //Is this really needed?
999         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1000                         0x01, 0x21, 0x0100, 0x8101, &com_buff, 4);
1001
1002         /* Deactivate Tuner */
1003         /* on  = 0x01*/
1004         /* off = 0x00*/
1005         com_buff[0] = 0x00;
1006         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1007                 0x01, 0x21, 0x0003, 0x2001, &com_buff, 1);
1008
1009         /* Set clock source to Internal (as opposed to S/PDIF) */
1010         com_buff[0] = 0x01;
1011         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1012                         1, 0x21, 0x0100, 0x8001, &com_buff, 1);
1013
1014         /* Mute the hardware loopbacks to start the device in a known state. */
1015         com_buff[0] = 0x00;
1016         com_buff[1] = 0x80;
1017         /* Analogue input 1 left channel: */
1018         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1019                         1, 0x21, 0x0110, 0x4001, &com_buff, 2);
1020         /* Analogue input 1 right channel: */
1021         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1022                         1, 0x21, 0x0111, 0x4001, &com_buff, 2);
1023         /* Analogue input 2 left channel: */
1024         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1025                         1, 0x21, 0x0114, 0x4001, &com_buff, 2);
1026         /* Analogue input 2 right channel: */
1027         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1028                         1, 0x21, 0x0115, 0x4001, &com_buff, 2);
1029         /* Analogue input 3 left channel: */
1030         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1031                         1, 0x21, 0x0118, 0x4001, &com_buff, 2);
1032         /* Analogue input 3 right channel: */
1033         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1034                         1, 0x21, 0x0119, 0x4001, &com_buff, 2);
1035         /* Analogue input 4 left channel: */
1036         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1037                         1, 0x21, 0x011c, 0x4001, &com_buff, 2);
1038         /* Analogue input 4 right channel: */
1039         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1040                         1, 0x21, 0x011d, 0x4001, &com_buff, 2);
1041
1042         /* Set software sends to output */
1043         com_buff[0] = 0x00;
1044         com_buff[1] = 0x00;
1045         /* Analogue software return 1 left channel: */
1046         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1047                         1, 0x21, 0x0100, 0x4001, &com_buff, 2);
1048         com_buff[0] = 0x00;
1049         com_buff[1] = 0x80;
1050         /* Analogue software return 1 right channel: */
1051         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1052                         1, 0x21, 0x0101, 0x4001, &com_buff, 2);
1053         com_buff[0] = 0x00;
1054         com_buff[1] = 0x80;
1055         /* Analogue software return 2 left channel: */
1056         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1057                         1, 0x21, 0x0104, 0x4001, &com_buff, 2);
1058         com_buff[0] = 0x00;
1059         com_buff[1] = 0x00;
1060         /* Analogue software return 2 right channel: */
1061         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1062                         1, 0x21, 0x0105, 0x4001, &com_buff, 2);
1063
1064         com_buff[0] = 0x00;
1065         com_buff[1] = 0x80;
1066         /* Analogue software return 3 left channel: */
1067         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1068                         1, 0x21, 0x0108, 0x4001, &com_buff, 2);
1069         /* Analogue software return 3 right channel: */
1070         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1071                         1, 0x21, 0x0109, 0x4001, &com_buff, 2);
1072         /* Analogue software return 4 left channel: */
1073         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1074                         1, 0x21, 0x010c, 0x4001, &com_buff, 2);
1075         /* Analogue software return 4 right channel: */
1076         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1077                         1, 0x21, 0x010d, 0x4001, &com_buff, 2);
1078
1079         /* Return to muting sends */
1080         com_buff[0] = 0x00;
1081         com_buff[1] = 0x80;
1082         /* Analogue fx return left channel: */
1083         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1084                         1, 0x21, 0x0120, 0x4001, &com_buff, 2);
1085         /* Analogue fx return right channel: */
1086         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1087                         1, 0x21, 0x0121, 0x4001, &com_buff, 2);
1088
1089         /* Analogue software input 1 fx send: */
1090         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1091                         1, 0x21, 0x0100, 0x4201, &com_buff, 2);
1092         /* Analogue software input 2 fx send: */
1093         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1094                         1, 0x21, 0x0101, 0x4201, &com_buff, 2);
1095         /* Analogue software input 3 fx send: */
1096         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1097                         1, 0x21, 0x0102, 0x4201, &com_buff, 2);
1098         /* Analogue software input 4 fx send: */
1099         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1100                         1, 0x21, 0x0103, 0x4201, &com_buff, 2);
1101         /* Analogue input 1 fx send: */
1102         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1103                         1, 0x21, 0x0104, 0x4201, &com_buff, 2);
1104         /* Analogue input 2 fx send: */
1105         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1106                         1, 0x21, 0x0105, 0x4201, &com_buff, 2);
1107         /* Analogue input 3 fx send: */
1108         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1109                         1, 0x21, 0x0106, 0x4201, &com_buff, 2);
1110         /* Analogue input 4 fx send: */
1111         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1112                         1, 0x21, 0x0107, 0x4201, &com_buff, 2);
1113
1114         /* Toggle allowing host control */
1115         com_buff[0] = 0x02;
1116         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1117                         3, 0x21, 0x0000, 0x2001, &com_buff, 1);
1118
1119         /* Do not dim fx returns */
1120         com_buff[0] = 0x00;
1121         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1122                         3, 0x21, 0x0002, 0x2001, &com_buff, 1);
1123
1124         /* Do not set fx returns to mono */
1125         com_buff[0] = 0x00;
1126         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1127                         3, 0x21, 0x0001, 0x2001, &com_buff, 1);
1128
1129         /* Mute the S/PDIF hardware loopback
1130          * same odd volume logic here as above
1131          */
1132         com_buff[0] = 0x00;
1133         com_buff[1] = 0x80;
1134         /* S/PDIF hardware input 1 left channel */
1135         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1136                         1, 0x21, 0x0112, 0x4001, &com_buff, 2);
1137         /* S/PDIF hardware input 1 right channel */
1138         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1139                         1, 0x21, 0x0113, 0x4001, &com_buff, 2);
1140         /* S/PDIF hardware input 2 left channel */
1141         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1142                         1, 0x21, 0x0116, 0x4001, &com_buff, 2);
1143         /* S/PDIF hardware input 2 right channel */
1144         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1145                         1, 0x21, 0x0117, 0x4001, &com_buff, 2);
1146         /* S/PDIF hardware input 3 left channel */
1147         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1148                         1, 0x21, 0x011a, 0x4001, &com_buff, 2);
1149         /* S/PDIF hardware input 3 right channel */
1150         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1151                         1, 0x21, 0x011b, 0x4001, &com_buff, 2);
1152         /* S/PDIF hardware input 4 left channel */
1153         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1154                         1, 0x21, 0x011e, 0x4001, &com_buff, 2);
1155         /* S/PDIF hardware input 4 right channel */
1156         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1157                         1, 0x21, 0x011f, 0x4001, &com_buff, 2);
1158         /* S/PDIF software return 1 left channel */
1159         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1160                         1, 0x21, 0x0102, 0x4001, &com_buff, 2);
1161         /* S/PDIF software return 1 right channel */
1162         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1163                         1, 0x21, 0x0103, 0x4001, &com_buff, 2);
1164         /* S/PDIF software return 2 left channel */
1165         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1166                         1, 0x21, 0x0106, 0x4001, &com_buff, 2);
1167         /* S/PDIF software return 2 right channel */
1168         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1169                         1, 0x21, 0x0107, 0x4001, &com_buff, 2);
1170
1171         com_buff[0] = 0x00;
1172         com_buff[1] = 0x00;
1173         /* S/PDIF software return 3 left channel */
1174         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1175                         1, 0x21, 0x010a, 0x4001, &com_buff, 2);
1176
1177         com_buff[0] = 0x00;
1178         com_buff[1] = 0x80;
1179         /* S/PDIF software return 3 right channel */
1180         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1181                         1, 0x21, 0x010b, 0x4001, &com_buff, 2);
1182         /* S/PDIF software return 4 left channel */
1183         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1184                         1, 0x21, 0x010e, 0x4001, &com_buff, 2);
1185
1186         com_buff[0] = 0x00;
1187         com_buff[1] = 0x00;
1188         /* S/PDIF software return 4 right channel */
1189         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1190                         1, 0x21, 0x010f, 0x4001, &com_buff, 2);
1191
1192         com_buff[0] = 0x00;
1193         com_buff[1] = 0x80;
1194         /* S/PDIF fx returns left channel */
1195         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1196                         1, 0x21, 0x0122, 0x4001, &com_buff, 2);
1197         /* S/PDIF fx returns right channel */
1198         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1199                         1, 0x21, 0x0123, 0x4001, &com_buff, 2);
1200
1201         /* Set the dropdown "Effect" to the first option */
1202         /* Room1  = 0x00 */
1203         /* Room2  = 0x01 */
1204         /* Room3  = 0x02 */
1205         /* Hall 1 = 0x03 */
1206         /* Hall 2 = 0x04 */
1207         /* Plate  = 0x05 */
1208         /* Delay  = 0x06 */
1209         /* Echo   = 0x07 */
1210         com_buff[0] = 0x00;
1211         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1212                         1, 0x21, 0x0200, 0x4301, &com_buff, 1); /* max is 0xff */
1213         /* min is 0x00 */
1214
1215
1216         /* Set the effect duration to 0 */
1217         /* max is 0xffff */
1218         /* min is 0x0000 */
1219         com_buff[0] = 0x00;
1220         com_buff[1] = 0x00;
1221         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1222                         1, 0x21, 0x0400, 0x4301, &com_buff, 2);
1223
1224         /* Set the effect volume and feedback to 0 */
1225         /* max is 0xff */
1226         /* min is 0x00 */
1227         com_buff[0] = 0x00;
1228         /* feedback: */
1229         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1230                         1, 0x21, 0x0500, 0x4301, &com_buff, 1);
1231         /* volume: */
1232         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1233                         1, 0x21, 0x0300, 0x4301, &com_buff, 1);
1234
1235         /* Set soft button hold duration */
1236         /* 0x03 = 250ms */
1237         /* 0x05 = 500ms DEFAULT */
1238         /* 0x08 = 750ms */
1239         /* 0x0a = 1sec */
1240         com_buff[0] = 0x05;
1241         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1242                         3, 0x21, 0x0005, 0x2001, &com_buff, 1);
1243
1244         /* Use dim LEDs for button of state */
1245         com_buff[0] = 0x00;
1246         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1247                         3, 0x21, 0x0004, 0x2001, &com_buff, 1);
1248 }
1249
1250 #define MBOX3_DESCRIPTOR_SIZE   464
1251
1252 static int snd_usb_mbox3_boot_quirk(struct usb_device *dev)
1253 {
1254         struct usb_host_config *config = dev->actconfig;
1255         int err;
1256         int descriptor_size;
1257
1258         descriptor_size = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
1259
1260         if (descriptor_size != MBOX3_DESCRIPTOR_SIZE) {
1261                 dev_err(&dev->dev, "Invalid descriptor size=%d.\n", descriptor_size);
1262                 return -ENODEV;
1263         }
1264
1265         dev_dbg(&dev->dev, "device initialised!\n");
1266
1267         err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
1268                 &dev->descriptor, sizeof(dev->descriptor));
1269         config = dev->actconfig;
1270         if (err < 0)
1271                 dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
1272
1273         err = usb_reset_configuration(dev);
1274         if (err < 0)
1275                 dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
1276         dev_dbg(&dev->dev, "mbox3_boot: new boot length = %d\n",
1277                 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
1278
1279         mbox3_setup_48_24_magic(dev);
1280         dev_info(&dev->dev, "Digidesign Mbox 3: 24bit 48kHz");
1281
1282         return 0; /* Successful boot */
1283 }
1284
1285 #define MICROBOOK_BUF_SIZE 128
1286
1287 static int snd_usb_motu_microbookii_communicate(struct usb_device *dev, u8 *buf,
1288                                                 int buf_size, int *length)
1289 {
1290         int err, actual_length;
1291
1292         if (usb_pipe_type_check(dev, usb_sndintpipe(dev, 0x01)))
1293                 return -EINVAL;
1294         err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x01), buf, *length,
1295                                 &actual_length, 1000);
1296         if (err < 0)
1297                 return err;
1298
1299         print_hex_dump(KERN_DEBUG, "MicroBookII snd: ", DUMP_PREFIX_NONE, 16, 1,
1300                        buf, actual_length, false);
1301
1302         memset(buf, 0, buf_size);
1303
1304         if (usb_pipe_type_check(dev, usb_rcvintpipe(dev, 0x82)))
1305                 return -EINVAL;
1306         err = usb_interrupt_msg(dev, usb_rcvintpipe(dev, 0x82), buf, buf_size,
1307                                 &actual_length, 1000);
1308         if (err < 0)
1309                 return err;
1310
1311         print_hex_dump(KERN_DEBUG, "MicroBookII rcv: ", DUMP_PREFIX_NONE, 16, 1,
1312                        buf, actual_length, false);
1313
1314         *length = actual_length;
1315         return 0;
1316 }
1317
1318 static int snd_usb_motu_microbookii_boot_quirk(struct usb_device *dev)
1319 {
1320         int err, actual_length, poll_attempts = 0;
1321         static const u8 set_samplerate_seq[] = { 0x00, 0x00, 0x00, 0x00,
1322                                                  0x00, 0x00, 0x0b, 0x14,
1323                                                  0x00, 0x00, 0x00, 0x01 };
1324         static const u8 poll_ready_seq[] = { 0x00, 0x04, 0x00, 0x00,
1325                                              0x00, 0x00, 0x0b, 0x18 };
1326         u8 *buf = kzalloc(MICROBOOK_BUF_SIZE, GFP_KERNEL);
1327
1328         if (!buf)
1329                 return -ENOMEM;
1330
1331         dev_info(&dev->dev, "Waiting for MOTU Microbook II to boot up...\n");
1332
1333         /* First we tell the device which sample rate to use. */
1334         memcpy(buf, set_samplerate_seq, sizeof(set_samplerate_seq));
1335         actual_length = sizeof(set_samplerate_seq);
1336         err = snd_usb_motu_microbookii_communicate(dev, buf, MICROBOOK_BUF_SIZE,
1337                                                    &actual_length);
1338
1339         if (err < 0) {
1340                 dev_err(&dev->dev,
1341                         "failed setting the sample rate for Motu MicroBook II: %d\n",
1342                         err);
1343                 goto free_buf;
1344         }
1345
1346         /* Then we poll every 100 ms until the device informs of its readiness. */
1347         while (true) {
1348                 if (++poll_attempts > 100) {
1349                         dev_err(&dev->dev,
1350                                 "failed booting Motu MicroBook II: timeout\n");
1351                         err = -ENODEV;
1352                         goto free_buf;
1353                 }
1354
1355                 memset(buf, 0, MICROBOOK_BUF_SIZE);
1356                 memcpy(buf, poll_ready_seq, sizeof(poll_ready_seq));
1357
1358                 actual_length = sizeof(poll_ready_seq);
1359                 err = snd_usb_motu_microbookii_communicate(
1360                         dev, buf, MICROBOOK_BUF_SIZE, &actual_length);
1361                 if (err < 0) {
1362                         dev_err(&dev->dev,
1363                                 "failed booting Motu MicroBook II: communication error %d\n",
1364                                 err);
1365                         goto free_buf;
1366                 }
1367
1368                 /* the device signals its readiness through a message of the
1369                  * form
1370                  *           XX 06 00 00 00 00 0b 18  00 00 00 01
1371                  * If the device is not yet ready to accept audio data, the
1372                  * last byte of that sequence is 00.
1373                  */
1374                 if (actual_length == 12 && buf[actual_length - 1] == 1)
1375                         break;
1376
1377                 msleep(100);
1378         }
1379
1380         dev_info(&dev->dev, "MOTU MicroBook II ready\n");
1381
1382 free_buf:
1383         kfree(buf);
1384         return err;
1385 }
1386
1387 static int snd_usb_motu_m_series_boot_quirk(struct usb_device *dev)
1388 {
1389         msleep(2000);
1390
1391         return 0;
1392 }
1393
1394 /*
1395  * Setup quirks
1396  */
1397 #define MAUDIO_SET              0x01 /* parse device_setup */
1398 #define MAUDIO_SET_COMPATIBLE   0x80 /* use only "win-compatible" interfaces */
1399 #define MAUDIO_SET_DTS          0x02 /* enable DTS Digital Output */
1400 #define MAUDIO_SET_96K          0x04 /* 48-96kHz rate if set, 8-48kHz otherwise */
1401 #define MAUDIO_SET_24B          0x08 /* 24bits sample if set, 16bits otherwise */
1402 #define MAUDIO_SET_DI           0x10 /* enable Digital Input */
1403 #define MAUDIO_SET_MASK         0x1f /* bit mask for setup value */
1404 #define MAUDIO_SET_24B_48K_DI    0x19 /* 24bits+48kHz+Digital Input */
1405 #define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48kHz+No Digital Input */
1406 #define MAUDIO_SET_16B_48K_DI    0x11 /* 16bits+48kHz+Digital Input */
1407 #define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48kHz+No Digital Input */
1408
1409 static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
1410                                       int iface, int altno)
1411 {
1412         /* Reset ALL ifaces to 0 altsetting.
1413          * Call it for every possible altsetting of every interface.
1414          */
1415         usb_set_interface(chip->dev, iface, 0);
1416         if (chip->setup & MAUDIO_SET) {
1417                 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
1418                         if (iface != 1 && iface != 2)
1419                                 return 1; /* skip all interfaces but 1 and 2 */
1420                 } else {
1421                         unsigned int mask;
1422                         if (iface == 1 || iface == 2)
1423                                 return 1; /* skip interfaces 1 and 2 */
1424                         if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
1425                                 return 1; /* skip this altsetting */
1426                         mask = chip->setup & MAUDIO_SET_MASK;
1427                         if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
1428                                 return 1; /* skip this altsetting */
1429                         if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
1430                                 return 1; /* skip this altsetting */
1431                         if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
1432                                 return 1; /* skip this altsetting */
1433                 }
1434         }
1435         usb_audio_dbg(chip,
1436                     "using altsetting %d for interface %d config %d\n",
1437                     altno, iface, chip->setup);
1438         return 0; /* keep this altsetting */
1439 }
1440
1441 static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
1442                                          int iface,
1443                                          int altno)
1444 {
1445         /* Reset ALL ifaces to 0 altsetting.
1446          * Call it for every possible altsetting of every interface.
1447          */
1448         usb_set_interface(chip->dev, iface, 0);
1449
1450         if (chip->setup & MAUDIO_SET) {
1451                 unsigned int mask;
1452                 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
1453                         return 1; /* skip this altsetting */
1454                 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
1455                         return 1; /* skip this altsetting */
1456                 mask = chip->setup & MAUDIO_SET_MASK;
1457                 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
1458                         return 1; /* skip this altsetting */
1459                 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
1460                         return 1; /* skip this altsetting */
1461                 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
1462                         return 1; /* skip this altsetting */
1463                 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
1464                         return 1; /* skip this altsetting */
1465         }
1466
1467         return 0; /* keep this altsetting */
1468 }
1469
1470 static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
1471                                            int iface, int altno)
1472 {
1473         /* Reset ALL ifaces to 0 altsetting.
1474          * Call it for every possible altsetting of every interface.
1475          */
1476         usb_set_interface(chip->dev, iface, 0);
1477
1478         /* possible configuration where both inputs and only one output is
1479          *used is not supported by the current setup
1480          */
1481         if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
1482                 if (chip->setup & MAUDIO_SET_96K) {
1483                         if (altno != 3 && altno != 6)
1484                                 return 1;
1485                 } else if (chip->setup & MAUDIO_SET_DI) {
1486                         if (iface == 4)
1487                                 return 1; /* no analog input */
1488                         if (altno != 2 && altno != 5)
1489                                 return 1; /* enable only altsets 2 and 5 */
1490                 } else {
1491                         if (iface == 5)
1492                                 return 1; /* disable digialt input */
1493                         if (altno != 2 && altno != 5)
1494                                 return 1; /* enalbe only altsets 2 and 5 */
1495                 }
1496         } else {
1497                 /* keep only 16-Bit mode */
1498                 if (altno != 1)
1499                         return 1;
1500         }
1501
1502         usb_audio_dbg(chip,
1503                     "using altsetting %d for interface %d config %d\n",
1504                     altno, iface, chip->setup);
1505         return 0; /* keep this altsetting */
1506 }
1507
1508 static int s1810c_skip_setting_quirk(struct snd_usb_audio *chip,
1509                                            int iface, int altno)
1510 {
1511         /*
1512          * Altno settings:
1513          *
1514          * Playback (Interface 1):
1515          * 1: 6 Analog + 2 S/PDIF
1516          * 2: 6 Analog + 2 S/PDIF
1517          * 3: 6 Analog
1518          *
1519          * Capture (Interface 2):
1520          * 1: 8 Analog + 2 S/PDIF + 8 ADAT
1521          * 2: 8 Analog + 2 S/PDIF + 4 ADAT
1522          * 3: 8 Analog
1523          */
1524
1525         /*
1526          * I'll leave 2 as the default one and
1527          * use device_setup to switch to the
1528          * other two.
1529          */
1530         if ((chip->setup == 0 || chip->setup > 2) && altno != 2)
1531                 return 1;
1532         else if (chip->setup == 1 && altno != 1)
1533                 return 1;
1534         else if (chip->setup == 2 && altno != 3)
1535                 return 1;
1536
1537         return 0;
1538 }
1539
1540 int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
1541                                   int iface,
1542                                   int altno)
1543 {
1544         /* audiophile usb: skip altsets incompatible with device_setup */
1545         if (chip->usb_id == USB_ID(0x0763, 0x2003))
1546                 return audiophile_skip_setting_quirk(chip, iface, altno);
1547         /* quattro usb: skip altsets incompatible with device_setup */
1548         if (chip->usb_id == USB_ID(0x0763, 0x2001))
1549                 return quattro_skip_setting_quirk(chip, iface, altno);
1550         /* fasttrackpro usb: skip altsets incompatible with device_setup */
1551         if (chip->usb_id == USB_ID(0x0763, 0x2012))
1552                 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
1553         /* presonus studio 1810c: skip altsets incompatible with device_setup */
1554         if (chip->usb_id == USB_ID(0x194f, 0x010c))
1555                 return s1810c_skip_setting_quirk(chip, iface, altno);
1556
1557
1558         return 0;
1559 }
1560
1561 int snd_usb_apply_boot_quirk(struct usb_device *dev,
1562                              struct usb_interface *intf,
1563                              const struct snd_usb_audio_quirk *quirk,
1564                              unsigned int id)
1565 {
1566         switch (id) {
1567         case USB_ID(0x041e, 0x3000):
1568                 /* SB Extigy needs special boot-up sequence */
1569                 /* if more models come, this will go to the quirk list. */
1570                 return snd_usb_extigy_boot_quirk(dev, intf);
1571
1572         case USB_ID(0x041e, 0x3020):
1573                 /* SB Audigy 2 NX needs its own boot-up magic, too */
1574                 return snd_usb_audigy2nx_boot_quirk(dev);
1575
1576         case USB_ID(0x10f5, 0x0200):
1577                 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
1578                 return snd_usb_cm106_boot_quirk(dev);
1579
1580         case USB_ID(0x0d8c, 0x0102):
1581                 /* C-Media CM6206 / CM106-Like Sound Device */
1582         case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
1583                 return snd_usb_cm6206_boot_quirk(dev);
1584
1585         case USB_ID(0x0dba, 0x3000):
1586                 /* Digidesign Mbox 2 */
1587                 return snd_usb_mbox2_boot_quirk(dev);
1588         case USB_ID(0x0dba, 0x5000):
1589                 /* Digidesign Mbox 3 */
1590                 return snd_usb_mbox3_boot_quirk(dev);
1591
1592
1593         case USB_ID(0x1235, 0x0010): /* Focusrite Novation Saffire 6 USB */
1594         case USB_ID(0x1235, 0x0018): /* Focusrite Novation Twitch */
1595                 return snd_usb_novation_boot_quirk(dev);
1596
1597         case USB_ID(0x133e, 0x0815):
1598                 /* Access Music VirusTI Desktop */
1599                 return snd_usb_accessmusic_boot_quirk(dev);
1600
1601         case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
1602         case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
1603         case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
1604                 return snd_usb_nativeinstruments_boot_quirk(dev);
1605         case USB_ID(0x0763, 0x2012):  /* M-Audio Fast Track Pro USB */
1606                 return snd_usb_fasttrackpro_boot_quirk(dev);
1607         case USB_ID(0x047f, 0xc010): /* Plantronics Gamecom 780 */
1608                 return snd_usb_gamecon780_boot_quirk(dev);
1609         case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx 3 */
1610                 return snd_usb_axefx3_boot_quirk(dev);
1611         case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II */
1612                 /*
1613                  * For some reason interface 3 with vendor-spec class is
1614                  * detected on MicroBook IIc.
1615                  */
1616                 if (get_iface_desc(intf->altsetting)->bInterfaceClass ==
1617                     USB_CLASS_VENDOR_SPEC &&
1618                     get_iface_desc(intf->altsetting)->bInterfaceNumber < 3)
1619                         return snd_usb_motu_microbookii_boot_quirk(dev);
1620                 break;
1621         }
1622
1623         return 0;
1624 }
1625
1626 int snd_usb_apply_boot_quirk_once(struct usb_device *dev,
1627                                   struct usb_interface *intf,
1628                                   const struct snd_usb_audio_quirk *quirk,
1629                                   unsigned int id)
1630 {
1631         switch (id) {
1632         case USB_ID(0x07fd, 0x0008): /* MOTU M Series */
1633                 return snd_usb_motu_m_series_boot_quirk(dev);
1634         }
1635
1636         return 0;
1637 }
1638
1639 /*
1640  * check if the device uses big-endian samples
1641  */
1642 int snd_usb_is_big_endian_format(struct snd_usb_audio *chip,
1643                                  const struct audioformat *fp)
1644 {
1645         /* it depends on altsetting whether the device is big-endian or not */
1646         switch (chip->usb_id) {
1647         case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
1648                 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1649                         fp->altsetting == 5 || fp->altsetting == 6)
1650                         return 1;
1651                 break;
1652         case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
1653                 if (chip->setup == 0x00 ||
1654                         fp->altsetting == 1 || fp->altsetting == 2 ||
1655                         fp->altsetting == 3)
1656                         return 1;
1657                 break;
1658         case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
1659                 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1660                         fp->altsetting == 5 || fp->altsetting == 6)
1661                         return 1;
1662                 break;
1663         }
1664         return 0;
1665 }
1666
1667 /*
1668  * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
1669  * not for interface.
1670  */
1671
1672 enum {
1673         EMU_QUIRK_SR_44100HZ = 0,
1674         EMU_QUIRK_SR_48000HZ,
1675         EMU_QUIRK_SR_88200HZ,
1676         EMU_QUIRK_SR_96000HZ,
1677         EMU_QUIRK_SR_176400HZ,
1678         EMU_QUIRK_SR_192000HZ
1679 };
1680
1681 static void set_format_emu_quirk(struct snd_usb_substream *subs,
1682                                  const struct audioformat *fmt)
1683 {
1684         unsigned char emu_samplerate_id = 0;
1685
1686         /* When capture is active
1687          * sample rate shouldn't be changed
1688          * by playback substream
1689          */
1690         if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1691                 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].cur_audiofmt)
1692                         return;
1693         }
1694
1695         switch (fmt->rate_min) {
1696         case 48000:
1697                 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
1698                 break;
1699         case 88200:
1700                 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
1701                 break;
1702         case 96000:
1703                 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
1704                 break;
1705         case 176400:
1706                 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
1707                 break;
1708         case 192000:
1709                 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
1710                 break;
1711         default:
1712                 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
1713                 break;
1714         }
1715         snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
1716         subs->pkt_offset_adj = (emu_samplerate_id >= EMU_QUIRK_SR_176400HZ) ? 4 : 0;
1717 }
1718
1719 static int pioneer_djm_set_format_quirk(struct snd_usb_substream *subs,
1720                                         u16 windex)
1721 {
1722         unsigned int cur_rate = subs->data_endpoint->cur_rate;
1723         u8 sr[3];
1724         // Convert to little endian
1725         sr[0] = cur_rate & 0xff;
1726         sr[1] = (cur_rate >> 8) & 0xff;
1727         sr[2] = (cur_rate >> 16) & 0xff;
1728         usb_set_interface(subs->dev, 0, 1);
1729         // we should derive windex from fmt-sync_ep but it's not set
1730         snd_usb_ctl_msg(subs->stream->chip->dev,
1731                 usb_sndctrlpipe(subs->stream->chip->dev, 0),
1732                 0x01, 0x22, 0x0100, windex, &sr, 0x0003);
1733         return 0;
1734 }
1735
1736 void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
1737                               const struct audioformat *fmt)
1738 {
1739         switch (subs->stream->chip->usb_id) {
1740         case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
1741         case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
1742         case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
1743         case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
1744                 set_format_emu_quirk(subs, fmt);
1745                 break;
1746         case USB_ID(0x534d, 0x0021): /* MacroSilicon MS2100/MS2106 */
1747         case USB_ID(0x534d, 0x2109): /* MacroSilicon MS2109 */
1748                 subs->stream_offset_adj = 2;
1749                 break;
1750         case USB_ID(0x2b73, 0x0013): /* Pioneer DJM-450 */
1751                 pioneer_djm_set_format_quirk(subs, 0x0082);
1752                 break;
1753         case USB_ID(0x08e4, 0x017f): /* Pioneer DJM-750 */
1754         case USB_ID(0x08e4, 0x0163): /* Pioneer DJM-850 */
1755                 pioneer_djm_set_format_quirk(subs, 0x0086);
1756                 break;
1757         }
1758 }
1759
1760 int snd_usb_select_mode_quirk(struct snd_usb_audio *chip,
1761                               const struct audioformat *fmt)
1762 {
1763         struct usb_device *dev = chip->dev;
1764         int err;
1765
1766         if (chip->quirk_flags & QUIRK_FLAG_ITF_USB_DSD_DAC) {
1767                 /* First switch to alt set 0, otherwise the mode switch cmd
1768                  * will not be accepted by the DAC
1769                  */
1770                 err = usb_set_interface(dev, fmt->iface, 0);
1771                 if (err < 0)
1772                         return err;
1773
1774                 msleep(20); /* Delay needed after setting the interface */
1775
1776                 /* Vendor mode switch cmd is required. */
1777                 if (fmt->formats & SNDRV_PCM_FMTBIT_DSD_U32_BE) {
1778                         /* DSD mode (DSD_U32) requested */
1779                         err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1780                                               USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1781                                               1, 1, NULL, 0);
1782                         if (err < 0)
1783                                 return err;
1784
1785                 } else {
1786                         /* PCM or DOP mode (S32) requested */
1787                         /* PCM mode (S16) requested */
1788                         err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1789                                               USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1790                                               0, 1, NULL, 0);
1791                         if (err < 0)
1792                                 return err;
1793
1794                 }
1795                 msleep(20);
1796         }
1797         return 0;
1798 }
1799
1800 void snd_usb_endpoint_start_quirk(struct snd_usb_endpoint *ep)
1801 {
1802         /*
1803          * "Playback Design" products send bogus feedback data at the start
1804          * of the stream. Ignore them.
1805          */
1806         if (USB_ID_VENDOR(ep->chip->usb_id) == 0x23ba &&
1807             ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
1808                 ep->skip_packets = 4;
1809
1810         /*
1811          * M-Audio Fast Track C400/C600 - when packets are not skipped, real
1812          * world latency varies by approx. +/- 50 frames (at 96kHz) each time
1813          * the stream is (re)started. When skipping packets 16 at endpoint
1814          * start up, the real world latency is stable within +/- 1 frame (also
1815          * across power cycles).
1816          */
1817         if ((ep->chip->usb_id == USB_ID(0x0763, 0x2030) ||
1818              ep->chip->usb_id == USB_ID(0x0763, 0x2031)) &&
1819             ep->type == SND_USB_ENDPOINT_TYPE_DATA)
1820                 ep->skip_packets = 16;
1821
1822         /* Work around devices that report unreasonable feedback data */
1823         if ((ep->chip->usb_id == USB_ID(0x0644, 0x8038) ||  /* TEAC UD-H01 */
1824              ep->chip->usb_id == USB_ID(0x1852, 0x5034)) && /* T+A Dac8 */
1825             ep->syncmaxsize == 4)
1826                 ep->tenor_fb_quirk = 1;
1827 }
1828
1829 /* quirk applied after snd_usb_ctl_msg(); not applied during boot quirks */
1830 void snd_usb_ctl_msg_quirk(struct usb_device *dev, unsigned int pipe,
1831                            __u8 request, __u8 requesttype, __u16 value,
1832                            __u16 index, void *data, __u16 size)
1833 {
1834         struct snd_usb_audio *chip = dev_get_drvdata(&dev->dev);
1835
1836         if (!chip || (requesttype & USB_TYPE_MASK) != USB_TYPE_CLASS)
1837                 return;
1838
1839         if (chip->quirk_flags & QUIRK_FLAG_CTL_MSG_DELAY)
1840                 msleep(20);
1841         else if (chip->quirk_flags & QUIRK_FLAG_CTL_MSG_DELAY_1M)
1842                 usleep_range(1000, 2000);
1843         else if (chip->quirk_flags & QUIRK_FLAG_CTL_MSG_DELAY_5M)
1844                 usleep_range(5000, 6000);
1845 }
1846
1847 /*
1848  * snd_usb_interface_dsd_format_quirks() is called from format.c to
1849  * augment the PCM format bit-field for DSD types. The UAC standards
1850  * don't have a designated bit field to denote DSD-capable interfaces,
1851  * hence all hardware that is known to support this format has to be
1852  * listed here.
1853  */
1854 u64 snd_usb_interface_dsd_format_quirks(struct snd_usb_audio *chip,
1855                                         struct audioformat *fp,
1856                                         unsigned int sample_bytes)
1857 {
1858         struct usb_interface *iface;
1859
1860         /* Playback Designs */
1861         if (USB_ID_VENDOR(chip->usb_id) == 0x23ba &&
1862             USB_ID_PRODUCT(chip->usb_id) < 0x0110) {
1863                 switch (fp->altsetting) {
1864                 case 1:
1865                         fp->dsd_dop = true;
1866                         return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1867                 case 2:
1868                         fp->dsd_bitrev = true;
1869                         return SNDRV_PCM_FMTBIT_DSD_U8;
1870                 case 3:
1871                         fp->dsd_bitrev = true;
1872                         return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1873                 }
1874         }
1875
1876         /* XMOS based USB DACs */
1877         switch (chip->usb_id) {
1878         case USB_ID(0x1511, 0x0037): /* AURALiC VEGA */
1879         case USB_ID(0x21ed, 0xd75a): /* Accuphase DAC-60 option card */
1880         case USB_ID(0x2522, 0x0012): /* LH Labs VI DAC Infinity */
1881         case USB_ID(0x2772, 0x0230): /* Pro-Ject Pre Box S2 Digital */
1882                 if (fp->altsetting == 2)
1883                         return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1884                 break;
1885
1886         case USB_ID(0x0d8c, 0x0316): /* Hegel HD12 DSD */
1887         case USB_ID(0x10cb, 0x0103): /* The Bit Opus #3; with fp->dsd_raw */
1888         case USB_ID(0x16d0, 0x06b2): /* NuPrime DAC-10 */
1889         case USB_ID(0x16d0, 0x09dd): /* Encore mDSD */
1890         case USB_ID(0x16d0, 0x0733): /* Furutech ADL Stratos */
1891         case USB_ID(0x16d0, 0x09db): /* NuPrime Audio DAC-9 */
1892         case USB_ID(0x1db5, 0x0003): /* Bryston BDA3 */
1893         case USB_ID(0x22e1, 0xca01): /* HDTA Serenade DSD */
1894         case USB_ID(0x249c, 0x9326): /* M2Tech Young MkIII */
1895         case USB_ID(0x2616, 0x0106): /* PS Audio NuWave DAC */
1896         case USB_ID(0x2622, 0x0041): /* Audiolab M-DAC+ */
1897         case USB_ID(0x27f7, 0x3002): /* W4S DAC-2v2SE */
1898         case USB_ID(0x29a2, 0x0086): /* Mutec MC3+ USB */
1899         case USB_ID(0x6b42, 0x0042): /* MSB Technology */
1900                 if (fp->altsetting == 3)
1901                         return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1902                 break;
1903
1904         /* Amanero Combo384 USB based DACs with native DSD support */
1905         case USB_ID(0x16d0, 0x071a):  /* Amanero - Combo384 */
1906         case USB_ID(0x2ab6, 0x0004):  /* T+A DAC8DSD-V2.0, MP1000E-V2.0, MP2000R-V2.0, MP2500R-V2.0, MP3100HV-V2.0 */
1907         case USB_ID(0x2ab6, 0x0005):  /* T+A USB HD Audio 1 */
1908         case USB_ID(0x2ab6, 0x0006):  /* T+A USB HD Audio 2 */
1909                 if (fp->altsetting == 2) {
1910                         switch (le16_to_cpu(chip->dev->descriptor.bcdDevice)) {
1911                         case 0x199:
1912                                 return SNDRV_PCM_FMTBIT_DSD_U32_LE;
1913                         case 0x19b:
1914                         case 0x203:
1915                                 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1916                         default:
1917                                 break;
1918                         }
1919                 }
1920                 break;
1921         case USB_ID(0x16d0, 0x0a23):
1922                 if (fp->altsetting == 2)
1923                         return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1924                 break;
1925
1926         default:
1927                 break;
1928         }
1929
1930         /* ITF-USB DSD based DACs */
1931         if (chip->quirk_flags & QUIRK_FLAG_ITF_USB_DSD_DAC) {
1932                 iface = usb_ifnum_to_if(chip->dev, fp->iface);
1933
1934                 /* Altsetting 2 support native DSD if the num of altsets is
1935                  * three (0-2),
1936                  * Altsetting 3 support native DSD if the num of altsets is
1937                  * four (0-3).
1938                  */
1939                 if (fp->altsetting == iface->num_altsetting - 1)
1940                         return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1941         }
1942
1943         /* Mostly generic method to detect many DSD-capable implementations */
1944         if ((chip->quirk_flags & QUIRK_FLAG_DSD_RAW) && fp->dsd_raw)
1945                 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1946
1947         return 0;
1948 }
1949
1950 void snd_usb_audioformat_attributes_quirk(struct snd_usb_audio *chip,
1951                                           struct audioformat *fp,
1952                                           int stream)
1953 {
1954         switch (chip->usb_id) {
1955         case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
1956                 /* Optoplay sets the sample rate attribute although
1957                  * it seems not supporting it in fact.
1958                  */
1959                 fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
1960                 break;
1961         case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
1962         case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
1963                 /* doesn't set the sample rate attribute, but supports it */
1964                 fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
1965                 break;
1966         case USB_ID(0x0763, 0x2001):  /* M-Audio Quattro USB */
1967         case USB_ID(0x0763, 0x2012):  /* M-Audio Fast Track Pro USB */
1968         case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
1969         case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
1970                                         an older model 77d:223) */
1971         /*
1972          * plantronics headset and Griffin iMic have set adaptive-in
1973          * although it's really not...
1974          */
1975                 fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
1976                 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1977                         fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
1978                 else
1979                         fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
1980                 break;
1981         case USB_ID(0x07fd, 0x0004):  /* MOTU MicroBook IIc */
1982                 /*
1983                  * MaxPacketsOnly attribute is erroneously set in endpoint
1984                  * descriptors. As a result this card produces noise with
1985                  * all sample rates other than 96 kHz.
1986                  */
1987                 fp->attributes &= ~UAC_EP_CS_ATTR_FILL_MAX;
1988                 break;
1989         case USB_ID(0x1224, 0x2a25):  /* Jieli Technology USB PHY 2.0 */
1990                 /* mic works only when ep packet size is set to wMaxPacketSize */
1991                 fp->attributes |= UAC_EP_CS_ATTR_FILL_MAX;
1992                 break;
1993
1994         }
1995 }
1996
1997 /*
1998  * driver behavior quirk flags
1999  */
2000 struct usb_audio_quirk_flags_table {
2001         u32 id;
2002         u32 flags;
2003 };
2004
2005 #define DEVICE_FLG(vid, pid, _flags) \
2006         { .id = USB_ID(vid, pid), .flags = (_flags) }
2007 #define VENDOR_FLG(vid, _flags) DEVICE_FLG(vid, 0, _flags)
2008
2009 static const struct usb_audio_quirk_flags_table quirk_flags_table[] = {
2010         /* Device matches */
2011         DEVICE_FLG(0x041e, 0x3000, /* Creative SB Extigy */
2012                    QUIRK_FLAG_IGNORE_CTL_ERROR),
2013         DEVICE_FLG(0x041e, 0x4080, /* Creative Live Cam VF0610 */
2014                    QUIRK_FLAG_GET_SAMPLE_RATE),
2015         DEVICE_FLG(0x046d, 0x084c, /* Logitech ConferenceCam Connect */
2016                    QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_CTL_MSG_DELAY_1M),
2017         DEVICE_FLG(0x046d, 0x0991, /* Logitech QuickCam Pro */
2018                    QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_IGNORE_CTL_ERROR),
2019         DEVICE_FLG(0x046d, 0x09a4, /* Logitech QuickCam E 3500 */
2020                    QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_IGNORE_CTL_ERROR),
2021         DEVICE_FLG(0x0499, 0x1509, /* Steinberg UR22 */
2022                    QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2023         DEVICE_FLG(0x04d8, 0xfeea, /* Benchmark DAC1 Pre */
2024                    QUIRK_FLAG_GET_SAMPLE_RATE),
2025         DEVICE_FLG(0x04e8, 0xa051, /* Samsung USBC Headset (AKG) */
2026                    QUIRK_FLAG_SKIP_CLOCK_SELECTOR | QUIRK_FLAG_CTL_MSG_DELAY_5M),
2027         DEVICE_FLG(0x054c, 0x0b8c, /* Sony WALKMAN NW-A45 DAC */
2028                    QUIRK_FLAG_SET_IFACE_FIRST),
2029         DEVICE_FLG(0x0556, 0x0014, /* Phoenix Audio TMX320VC */
2030                    QUIRK_FLAG_GET_SAMPLE_RATE),
2031         DEVICE_FLG(0x05a3, 0x9420, /* ELP HD USB Camera */
2032                    QUIRK_FLAG_GET_SAMPLE_RATE),
2033         DEVICE_FLG(0x05a7, 0x1020, /* Bose Companion 5 */
2034                    QUIRK_FLAG_GET_SAMPLE_RATE),
2035         DEVICE_FLG(0x05e1, 0x0408, /* Syntek STK1160 */
2036                    QUIRK_FLAG_ALIGN_TRANSFER),
2037         DEVICE_FLG(0x05e1, 0x0480, /* Hauppauge Woodbury */
2038                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2039         DEVICE_FLG(0x0644, 0x8043, /* TEAC UD-501/UD-501V2/UD-503/NT-503 */
2040                    QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY |
2041                    QUIRK_FLAG_IFACE_DELAY),
2042         DEVICE_FLG(0x0644, 0x8044, /* Esoteric D-05X */
2043                    QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY |
2044                    QUIRK_FLAG_IFACE_DELAY),
2045         DEVICE_FLG(0x0644, 0x804a, /* TEAC UD-301 */
2046                    QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY |
2047                    QUIRK_FLAG_IFACE_DELAY),
2048         DEVICE_FLG(0x0644, 0x805f, /* TEAC Model 12 */
2049                    QUIRK_FLAG_FORCE_IFACE_RESET),
2050         DEVICE_FLG(0x06f8, 0xb000, /* Hercules DJ Console (Windows Edition) */
2051                    QUIRK_FLAG_IGNORE_CTL_ERROR),
2052         DEVICE_FLG(0x06f8, 0xd002, /* Hercules DJ Console (Macintosh Edition) */
2053                    QUIRK_FLAG_IGNORE_CTL_ERROR),
2054         DEVICE_FLG(0x0711, 0x5800, /* MCT Trigger 5 USB-to-HDMI */
2055                    QUIRK_FLAG_GET_SAMPLE_RATE),
2056         DEVICE_FLG(0x074d, 0x3553, /* Outlaw RR2150 (Micronas UAC3553B) */
2057                    QUIRK_FLAG_GET_SAMPLE_RATE),
2058         DEVICE_FLG(0x0763, 0x2030, /* M-Audio Fast Track C400 */
2059                    QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2060         DEVICE_FLG(0x0763, 0x2031, /* M-Audio Fast Track C600 */
2061                    QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2062         DEVICE_FLG(0x08bb, 0x2702, /* LineX FM Transmitter */
2063                    QUIRK_FLAG_IGNORE_CTL_ERROR),
2064         DEVICE_FLG(0x0951, 0x16ad, /* Kingston HyperX */
2065                    QUIRK_FLAG_CTL_MSG_DELAY_1M),
2066         DEVICE_FLG(0x0b0e, 0x0349, /* Jabra 550a */
2067                    QUIRK_FLAG_CTL_MSG_DELAY_1M),
2068         DEVICE_FLG(0x0fd9, 0x0008, /* Hauppauge HVR-950Q */
2069                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2070         DEVICE_FLG(0x1395, 0x740a, /* Sennheiser DECT */
2071                    QUIRK_FLAG_GET_SAMPLE_RATE),
2072         DEVICE_FLG(0x1397, 0x0507, /* Behringer UMC202HD */
2073                    QUIRK_FLAG_PLAYBACK_FIRST | QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2074         DEVICE_FLG(0x1397, 0x0508, /* Behringer UMC204HD */
2075                    QUIRK_FLAG_PLAYBACK_FIRST | QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2076         DEVICE_FLG(0x1397, 0x0509, /* Behringer UMC404HD */
2077                    QUIRK_FLAG_PLAYBACK_FIRST | QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2078         DEVICE_FLG(0x13e5, 0x0001, /* Serato Phono */
2079                    QUIRK_FLAG_IGNORE_CTL_ERROR),
2080         DEVICE_FLG(0x154e, 0x1002, /* Denon DCD-1500RE */
2081                    QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY),
2082         DEVICE_FLG(0x154e, 0x1003, /* Denon DA-300USB */
2083                    QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY),
2084         DEVICE_FLG(0x154e, 0x3005, /* Marantz HD-DAC1 */
2085                    QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY),
2086         DEVICE_FLG(0x154e, 0x3006, /* Marantz SA-14S1 */
2087                    QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY),
2088         DEVICE_FLG(0x154e, 0x500e, /* Denon DN-X1600 */
2089                    QUIRK_FLAG_IGNORE_CLOCK_SOURCE),
2090         DEVICE_FLG(0x1686, 0x00dd, /* Zoom R16/24 */
2091                    QUIRK_FLAG_TX_LENGTH | QUIRK_FLAG_CTL_MSG_DELAY_1M),
2092         DEVICE_FLG(0x17aa, 0x1046, /* Lenovo ThinkStation P620 Rear Line-in, Line-out and Microphone */
2093                    QUIRK_FLAG_DISABLE_AUTOSUSPEND),
2094         DEVICE_FLG(0x17aa, 0x104d, /* Lenovo ThinkStation P620 Internal Speaker + Front Headset */
2095                    QUIRK_FLAG_DISABLE_AUTOSUSPEND),
2096         DEVICE_FLG(0x1852, 0x5065, /* Luxman DA-06 */
2097                    QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY),
2098         DEVICE_FLG(0x1901, 0x0191, /* GE B850V3 CP2114 audio interface */
2099                    QUIRK_FLAG_GET_SAMPLE_RATE),
2100         DEVICE_FLG(0x2040, 0x7200, /* Hauppauge HVR-950Q */
2101                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2102         DEVICE_FLG(0x2040, 0x7201, /* Hauppauge HVR-950Q-MXL */
2103                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2104         DEVICE_FLG(0x2040, 0x7210, /* Hauppauge HVR-950Q */
2105                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2106         DEVICE_FLG(0x2040, 0x7211, /* Hauppauge HVR-950Q-MXL */
2107                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2108         DEVICE_FLG(0x2040, 0x7213, /* Hauppauge HVR-950Q */
2109                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2110         DEVICE_FLG(0x2040, 0x7217, /* Hauppauge HVR-950Q */
2111                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2112         DEVICE_FLG(0x2040, 0x721b, /* Hauppauge HVR-950Q */
2113                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2114         DEVICE_FLG(0x2040, 0x721e, /* Hauppauge HVR-950Q */
2115                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2116         DEVICE_FLG(0x2040, 0x721f, /* Hauppauge HVR-950Q */
2117                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2118         DEVICE_FLG(0x2040, 0x7240, /* Hauppauge HVR-850 */
2119                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2120         DEVICE_FLG(0x2040, 0x7260, /* Hauppauge HVR-950Q */
2121                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2122         DEVICE_FLG(0x2040, 0x7270, /* Hauppauge HVR-950Q */
2123                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2124         DEVICE_FLG(0x2040, 0x7280, /* Hauppauge HVR-950Q */
2125                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2126         DEVICE_FLG(0x2040, 0x7281, /* Hauppauge HVR-950Q-MXL */
2127                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2128         DEVICE_FLG(0x2040, 0x8200, /* Hauppauge Woodbury */
2129                    QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2130         DEVICE_FLG(0x21b4, 0x0081, /* AudioQuest DragonFly */
2131                    QUIRK_FLAG_GET_SAMPLE_RATE),
2132         DEVICE_FLG(0x2522, 0x0007, /* LH Labs Geek Out HD Audio 1V5 */
2133                    QUIRK_FLAG_SET_IFACE_FIRST),
2134         DEVICE_FLG(0x2708, 0x0002, /* Audient iD14 */
2135                    QUIRK_FLAG_IGNORE_CTL_ERROR),
2136         DEVICE_FLG(0x2912, 0x30c8, /* Audioengine D1 */
2137                    QUIRK_FLAG_GET_SAMPLE_RATE),
2138         DEVICE_FLG(0x30be, 0x0101, /* Schiit Hel */
2139                    QUIRK_FLAG_IGNORE_CTL_ERROR),
2140         DEVICE_FLG(0x413c, 0xa506, /* Dell AE515 sound bar */
2141                    QUIRK_FLAG_GET_SAMPLE_RATE),
2142         DEVICE_FLG(0x534d, 0x0021, /* MacroSilicon MS2100/MS2106 */
2143                    QUIRK_FLAG_ALIGN_TRANSFER),
2144         DEVICE_FLG(0x534d, 0x2109, /* MacroSilicon MS2109 */
2145                    QUIRK_FLAG_ALIGN_TRANSFER),
2146         DEVICE_FLG(0x1224, 0x2a25, /* Jieli Technology USB PHY 2.0 */
2147                    QUIRK_FLAG_GET_SAMPLE_RATE),
2148         DEVICE_FLG(0x2b53, 0x0023, /* Fiero SC-01 (firmware v1.0.0 @ 48 kHz) */
2149                    QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2150         DEVICE_FLG(0x2b53, 0x0024, /* Fiero SC-01 (firmware v1.0.0 @ 96 kHz) */
2151                    QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2152         DEVICE_FLG(0x2b53, 0x0031, /* Fiero SC-01 (firmware v1.1.0) */
2153                    QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2154         DEVICE_FLG(0x0525, 0xa4ad, /* Hamedal C20 usb camero */
2155                    QUIRK_FLAG_IFACE_SKIP_CLOSE),
2156         DEVICE_FLG(0x0ecb, 0x205c, /* JBL Quantum610 Wireless */
2157                    QUIRK_FLAG_FIXED_RATE),
2158         DEVICE_FLG(0x0ecb, 0x2069, /* JBL Quantum810 Wireless */
2159                    QUIRK_FLAG_FIXED_RATE),
2160
2161         /* Vendor matches */
2162         VENDOR_FLG(0x045e, /* MS Lifecam */
2163                    QUIRK_FLAG_GET_SAMPLE_RATE),
2164         VENDOR_FLG(0x046d, /* Logitech */
2165                    QUIRK_FLAG_CTL_MSG_DELAY_1M),
2166         VENDOR_FLG(0x047f, /* Plantronics */
2167                    QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_CTL_MSG_DELAY),
2168         VENDOR_FLG(0x0644, /* TEAC Corp. */
2169                    QUIRK_FLAG_CTL_MSG_DELAY | QUIRK_FLAG_IFACE_DELAY),
2170         VENDOR_FLG(0x07fd, /* MOTU */
2171                    QUIRK_FLAG_VALIDATE_RATES),
2172         VENDOR_FLG(0x1235, /* Focusrite Novation */
2173                    QUIRK_FLAG_VALIDATE_RATES),
2174         VENDOR_FLG(0x152a, /* Thesycon devices */
2175                    QUIRK_FLAG_DSD_RAW),
2176         VENDOR_FLG(0x1de7, /* Phoenix Audio */
2177                    QUIRK_FLAG_GET_SAMPLE_RATE),
2178         VENDOR_FLG(0x20b1, /* XMOS based devices */
2179                    QUIRK_FLAG_DSD_RAW),
2180         VENDOR_FLG(0x22d9, /* Oppo */
2181                    QUIRK_FLAG_DSD_RAW),
2182         VENDOR_FLG(0x23ba, /* Playback Design */
2183                    QUIRK_FLAG_CTL_MSG_DELAY | QUIRK_FLAG_IFACE_DELAY |
2184                    QUIRK_FLAG_DSD_RAW),
2185         VENDOR_FLG(0x25ce, /* Mytek devices */
2186                    QUIRK_FLAG_DSD_RAW),
2187         VENDOR_FLG(0x278b, /* Rotel? */
2188                    QUIRK_FLAG_DSD_RAW),
2189         VENDOR_FLG(0x292b, /* Gustard/Ess based devices */
2190                    QUIRK_FLAG_DSD_RAW),
2191         VENDOR_FLG(0x2972, /* FiiO devices */
2192                    QUIRK_FLAG_DSD_RAW),
2193         VENDOR_FLG(0x2ab6, /* T+A devices */
2194                    QUIRK_FLAG_DSD_RAW),
2195         VENDOR_FLG(0x3353, /* Khadas devices */
2196                    QUIRK_FLAG_DSD_RAW),
2197         VENDOR_FLG(0x3842, /* EVGA */
2198                    QUIRK_FLAG_DSD_RAW),
2199         VENDOR_FLG(0xc502, /* HiBy devices */
2200                    QUIRK_FLAG_DSD_RAW),
2201
2202         {} /* terminator */
2203 };
2204
2205 void snd_usb_init_quirk_flags(struct snd_usb_audio *chip)
2206 {
2207         const struct usb_audio_quirk_flags_table *p;
2208
2209         for (p = quirk_flags_table; p->id; p++) {
2210                 if (chip->usb_id == p->id ||
2211                     (!USB_ID_PRODUCT(p->id) &&
2212                      USB_ID_VENDOR(chip->usb_id) == USB_ID_VENDOR(p->id))) {
2213                         usb_audio_dbg(chip,
2214                                       "Set quirk_flags 0x%x for device %04x:%04x\n",
2215                                       p->flags, USB_ID_VENDOR(chip->usb_id),
2216                                       USB_ID_PRODUCT(chip->usb_id));
2217                         chip->quirk_flags |= p->flags;
2218                         return;
2219                 }
2220         }
2221 }