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