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