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