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