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