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