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