xref: /linux/sound/usb/quirks.c (revision 666f12ae9f504625e5a93581d8fbcba3dd60c294)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  */
4 
5 #include <linux/cleanup.h>
6 #include <linux/err.h>
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/string.h>
10 #include <linux/usb.h>
11 #include <linux/usb/audio.h>
12 #include <linux/usb/midi.h>
13 #include <linux/bits.h>
14 
15 #include <sound/control.h>
16 #include <sound/core.h>
17 #include <sound/info.h>
18 #include <sound/pcm.h>
19 
20 #include "usbaudio.h"
21 #include "card.h"
22 #include "mixer.h"
23 #include "mixer_quirks.h"
24 #include "midi.h"
25 #include "midi2.h"
26 #include "quirks.h"
27 #include "helper.h"
28 #include "endpoint.h"
29 #include "pcm.h"
30 #include "clock.h"
31 #include "stream.h"
32 
33 /*
34  * handle the quirks for the contained interfaces
35  */
create_composite_quirk(struct snd_usb_audio * chip,struct usb_interface * iface,struct usb_driver * driver,const struct snd_usb_audio_quirk * quirk_comp)36 static int create_composite_quirk(struct snd_usb_audio *chip,
37 				  struct usb_interface *iface,
38 				  struct usb_driver *driver,
39 				  const struct snd_usb_audio_quirk *quirk_comp)
40 {
41 	int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
42 	const struct snd_usb_audio_quirk *quirk;
43 	int err;
44 
45 	for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
46 		iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
47 		if (!iface)
48 			continue;
49 		if (quirk->ifnum != probed_ifnum &&
50 		    usb_interface_claimed(iface))
51 			continue;
52 		err = snd_usb_create_quirk(chip, iface, driver, quirk);
53 		if (err < 0)
54 			return err;
55 	}
56 
57 	for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
58 		iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
59 		if (!iface)
60 			continue;
61 		if (quirk->ifnum != probed_ifnum &&
62 		    !usb_interface_claimed(iface)) {
63 			err = usb_driver_claim_interface(driver, iface,
64 							 USB_AUDIO_IFACE_UNUSED);
65 			if (err < 0)
66 				return err;
67 		}
68 	}
69 
70 	return 0;
71 }
72 
ignore_interface_quirk(struct snd_usb_audio * chip,struct usb_interface * iface,struct usb_driver * driver,const struct snd_usb_audio_quirk * quirk)73 static int ignore_interface_quirk(struct snd_usb_audio *chip,
74 				  struct usb_interface *iface,
75 				  struct usb_driver *driver,
76 				  const struct snd_usb_audio_quirk *quirk)
77 {
78 	return 0;
79 }
80 
81 
create_any_midi_quirk(struct snd_usb_audio * chip,struct usb_interface * intf,struct usb_driver * driver,const struct snd_usb_audio_quirk * quirk)82 static int create_any_midi_quirk(struct snd_usb_audio *chip,
83 				 struct usb_interface *intf,
84 				 struct usb_driver *driver,
85 				 const struct snd_usb_audio_quirk *quirk)
86 {
87 	return snd_usb_midi_v2_create(chip, intf, quirk, 0);
88 }
89 
90 /*
91  * create a stream for an interface with proper descriptors
92  */
create_standard_audio_quirk(struct snd_usb_audio * chip,struct usb_interface * iface,struct usb_driver * driver,const struct snd_usb_audio_quirk * quirk)93 static int create_standard_audio_quirk(struct snd_usb_audio *chip,
94 				       struct usb_interface *iface,
95 				       struct usb_driver *driver,
96 				       const struct snd_usb_audio_quirk *quirk)
97 {
98 	struct usb_host_interface *alts;
99 	struct usb_interface_descriptor *altsd;
100 	int err;
101 
102 	alts = &iface->altsetting[0];
103 	altsd = get_iface_desc(alts);
104 	err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
105 	if (err < 0) {
106 		usb_audio_err(chip, "cannot setup if %d: error %d\n",
107 			   altsd->bInterfaceNumber, err);
108 		return err;
109 	}
110 	/* reset the current interface */
111 	usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
112 	return 0;
113 }
114 
115 /* create the audio stream and the corresponding endpoints from the fixed
116  * audioformat object; this is used for quirks with the fixed EPs
117  */
add_audio_stream_from_fixed_fmt(struct snd_usb_audio * chip,struct audioformat * fp)118 static int add_audio_stream_from_fixed_fmt(struct snd_usb_audio *chip,
119 					   struct audioformat *fp)
120 {
121 	int stream, err;
122 
123 	stream = (fp->endpoint & USB_DIR_IN) ?
124 		SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
125 
126 	snd_usb_audioformat_set_sync_ep(chip, fp);
127 
128 	err = snd_usb_add_audio_stream(chip, stream, fp, NULL);
129 	if (err < 0)
130 		return err;
131 
132 	err = snd_usb_add_endpoint(chip, fp->endpoint,
133 				   SND_USB_ENDPOINT_TYPE_DATA);
134 	if (err < 0)
135 		return err;
136 
137 	if (fp->sync_ep) {
138 		err = snd_usb_add_endpoint(chip, fp->sync_ep,
139 					   fp->implicit_fb ?
140 					   SND_USB_ENDPOINT_TYPE_DATA :
141 					   SND_USB_ENDPOINT_TYPE_SYNC);
142 		if (err < 0)
143 			return err;
144 	}
145 
146 	return 0;
147 }
148 
149 /*
150  * create a stream for an endpoint/altsetting without proper descriptors
151  */
create_fixed_stream_quirk(struct snd_usb_audio * chip,struct usb_interface * iface,struct usb_driver * driver,const struct snd_usb_audio_quirk * quirk)152 static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
153 				     struct usb_interface *iface,
154 				     struct usb_driver *driver,
155 				     const struct snd_usb_audio_quirk *quirk)
156 {
157 	struct audioformat *fp;
158 	struct usb_host_interface *alts;
159 	struct usb_interface_descriptor *altsd;
160 	unsigned *rate_table = NULL;
161 	int err;
162 
163 	fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
164 	if (!fp)
165 		return -ENOMEM;
166 
167 	INIT_LIST_HEAD(&fp->list);
168 	if (fp->nr_rates > MAX_NR_RATES) {
169 		kfree(fp);
170 		return -EINVAL;
171 	}
172 	if (fp->nr_rates > 0) {
173 		rate_table = kmemdup_array(fp->rate_table, fp->nr_rates, sizeof(int),
174 					   GFP_KERNEL);
175 		if (!rate_table) {
176 			kfree(fp);
177 			return -ENOMEM;
178 		}
179 		fp->rate_table = rate_table;
180 	}
181 
182 	if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
183 	    fp->altset_idx >= iface->num_altsetting) {
184 		err = -EINVAL;
185 		goto error;
186 	}
187 	alts = &iface->altsetting[fp->altset_idx];
188 	altsd = get_iface_desc(alts);
189 	if (altsd->bNumEndpoints <= fp->ep_idx) {
190 		err = -EINVAL;
191 		goto error;
192 	}
193 
194 	fp->protocol = altsd->bInterfaceProtocol;
195 
196 	if (fp->datainterval == 0)
197 		fp->datainterval = snd_usb_parse_datainterval(chip, alts);
198 	if (fp->maxpacksize == 0)
199 		fp->maxpacksize = le16_to_cpu(get_endpoint(alts, fp->ep_idx)->wMaxPacketSize);
200 	if (!fp->fmt_type)
201 		fp->fmt_type = UAC_FORMAT_TYPE_I;
202 
203 	err = add_audio_stream_from_fixed_fmt(chip, fp);
204 	if (err < 0)
205 		goto error;
206 
207 	usb_set_interface(chip->dev, fp->iface, 0);
208 	snd_usb_init_pitch(chip, fp);
209 	snd_usb_init_sample_rate(chip, fp, fp->rate_max);
210 	return 0;
211 
212  error:
213 	list_del(&fp->list); /* unlink for avoiding double-free */
214 	kfree(fp);
215 	kfree(rate_table);
216 	return err;
217 }
218 
create_auto_pcm_quirk(struct snd_usb_audio * chip,struct usb_interface * iface,struct usb_driver * driver)219 static int create_auto_pcm_quirk(struct snd_usb_audio *chip,
220 				 struct usb_interface *iface,
221 				 struct usb_driver *driver)
222 {
223 	struct usb_host_interface *alts;
224 	struct usb_interface_descriptor *altsd;
225 	struct usb_endpoint_descriptor *epd;
226 	struct uac1_as_header_descriptor *ashd;
227 	struct uac_format_type_i_discrete_descriptor *fmtd;
228 
229 	/*
230 	 * Most Roland/Yamaha audio streaming interfaces have more or less
231 	 * standard descriptors, but older devices might lack descriptors, and
232 	 * future ones might change, so ensure that we fail silently if the
233 	 * interface doesn't look exactly right.
234 	 */
235 
236 	/* must have a non-zero altsetting for streaming */
237 	if (iface->num_altsetting < 2)
238 		return -ENODEV;
239 	alts = &iface->altsetting[1];
240 	altsd = get_iface_desc(alts);
241 
242 	/* must have an isochronous endpoint for streaming */
243 	if (altsd->bNumEndpoints < 1)
244 		return -ENODEV;
245 	epd = get_endpoint(alts, 0);
246 	if (!usb_endpoint_xfer_isoc(epd))
247 		return -ENODEV;
248 
249 	/* must have format descriptors */
250 	ashd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
251 				       UAC_AS_GENERAL);
252 	fmtd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
253 				       UAC_FORMAT_TYPE);
254 	if (!ashd || ashd->bLength < 7 ||
255 	    !fmtd || fmtd->bLength < 8)
256 		return -ENODEV;
257 
258 	return create_standard_audio_quirk(chip, iface, driver, NULL);
259 }
260 
create_yamaha_midi_quirk(struct snd_usb_audio * chip,struct usb_interface * iface,struct usb_driver * driver,struct usb_host_interface * alts)261 static int create_yamaha_midi_quirk(struct snd_usb_audio *chip,
262 				    struct usb_interface *iface,
263 				    struct usb_driver *driver,
264 				    struct usb_host_interface *alts)
265 {
266 	static const struct snd_usb_audio_quirk yamaha_midi_quirk = {
267 		.type = QUIRK_MIDI_YAMAHA
268 	};
269 	struct usb_midi_in_jack_descriptor *injd;
270 	struct usb_midi_out_jack_descriptor *outjd;
271 
272 	/* must have some valid jack descriptors */
273 	injd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
274 				       NULL, USB_MS_MIDI_IN_JACK);
275 	outjd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
276 					NULL, USB_MS_MIDI_OUT_JACK);
277 	if (!injd && !outjd)
278 		return -ENODEV;
279 	if ((injd && !snd_usb_validate_midi_desc(injd)) ||
280 	    (outjd && !snd_usb_validate_midi_desc(outjd)))
281 		return -ENODEV;
282 	if (injd && (injd->bLength < 5 ||
283 		     (injd->bJackType != USB_MS_EMBEDDED &&
284 		      injd->bJackType != USB_MS_EXTERNAL)))
285 		return -ENODEV;
286 	if (outjd && (outjd->bLength < 6 ||
287 		      (outjd->bJackType != USB_MS_EMBEDDED &&
288 		       outjd->bJackType != USB_MS_EXTERNAL)))
289 		return -ENODEV;
290 	return create_any_midi_quirk(chip, iface, driver, &yamaha_midi_quirk);
291 }
292 
create_roland_midi_quirk(struct snd_usb_audio * chip,struct usb_interface * iface,struct usb_driver * driver,struct usb_host_interface * alts)293 static int create_roland_midi_quirk(struct snd_usb_audio *chip,
294 				    struct usb_interface *iface,
295 				    struct usb_driver *driver,
296 				    struct usb_host_interface *alts)
297 {
298 	static const struct snd_usb_audio_quirk roland_midi_quirk = {
299 		.type = QUIRK_MIDI_ROLAND
300 	};
301 	u8 *roland_desc = NULL;
302 
303 	/* might have a vendor-specific descriptor <06 24 F1 02 ...> */
304 	for (;;) {
305 		roland_desc = snd_usb_find_csint_desc(alts->extra,
306 						      alts->extralen,
307 						      roland_desc, 0xf1);
308 		if (!roland_desc)
309 			return -ENODEV;
310 		if (roland_desc[0] < 6 || roland_desc[3] != 2)
311 			continue;
312 		return create_any_midi_quirk(chip, iface, driver,
313 					     &roland_midi_quirk);
314 	}
315 }
316 
create_std_midi_quirk(struct snd_usb_audio * chip,struct usb_interface * iface,struct usb_driver * driver,struct usb_host_interface * alts)317 static int create_std_midi_quirk(struct snd_usb_audio *chip,
318 				 struct usb_interface *iface,
319 				 struct usb_driver *driver,
320 				 struct usb_host_interface *alts)
321 {
322 	struct usb_ms_header_descriptor *mshd;
323 	struct usb_ms_endpoint_descriptor *msepd;
324 
325 	/* must have the MIDIStreaming interface header descriptor*/
326 	mshd = (struct usb_ms_header_descriptor *)alts->extra;
327 	if (alts->extralen < 7 ||
328 	    mshd->bLength < 7 ||
329 	    mshd->bDescriptorType != USB_DT_CS_INTERFACE ||
330 	    mshd->bDescriptorSubtype != USB_MS_HEADER)
331 		return -ENODEV;
332 	/* must have the MIDIStreaming endpoint descriptor*/
333 	msepd = (struct usb_ms_endpoint_descriptor *)alts->endpoint[0].extra;
334 	if (alts->endpoint[0].extralen < 4 ||
335 	    msepd->bLength < 4 ||
336 	    msepd->bDescriptorType != USB_DT_CS_ENDPOINT ||
337 	    msepd->bDescriptorSubtype != UAC_MS_GENERAL ||
338 	    msepd->bNumEmbMIDIJack < 1 ||
339 	    msepd->bNumEmbMIDIJack > 16)
340 		return -ENODEV;
341 
342 	return create_any_midi_quirk(chip, iface, driver, NULL);
343 }
344 
create_auto_midi_quirk(struct snd_usb_audio * chip,struct usb_interface * iface,struct usb_driver * driver)345 static int create_auto_midi_quirk(struct snd_usb_audio *chip,
346 				  struct usb_interface *iface,
347 				  struct usb_driver *driver)
348 {
349 	struct usb_host_interface *alts;
350 	struct usb_interface_descriptor *altsd;
351 	struct usb_endpoint_descriptor *epd;
352 	int err;
353 
354 	alts = &iface->altsetting[0];
355 	altsd = get_iface_desc(alts);
356 
357 	/* must have at least one bulk/interrupt endpoint for streaming */
358 	if (altsd->bNumEndpoints < 1)
359 		return -ENODEV;
360 	epd = get_endpoint(alts, 0);
361 	if (!usb_endpoint_xfer_bulk(epd) &&
362 	    !usb_endpoint_xfer_int(epd))
363 		return -ENODEV;
364 
365 	switch (USB_ID_VENDOR(chip->usb_id)) {
366 	case 0x0499: /* Yamaha */
367 		err = create_yamaha_midi_quirk(chip, iface, driver, alts);
368 		if (err != -ENODEV)
369 			return err;
370 		break;
371 	case 0x0582: /* Roland */
372 		err = create_roland_midi_quirk(chip, iface, driver, alts);
373 		if (err != -ENODEV)
374 			return err;
375 		break;
376 	}
377 
378 	return create_std_midi_quirk(chip, iface, driver, alts);
379 }
380 
create_autodetect_quirk(struct snd_usb_audio * chip,struct usb_interface * iface,struct usb_driver * driver,const struct snd_usb_audio_quirk * quirk)381 static int create_autodetect_quirk(struct snd_usb_audio *chip,
382 				   struct usb_interface *iface,
383 				   struct usb_driver *driver,
384 				   const struct snd_usb_audio_quirk *quirk)
385 {
386 	int err;
387 
388 	err = create_auto_pcm_quirk(chip, iface, driver);
389 	if (err == -ENODEV)
390 		err = create_auto_midi_quirk(chip, iface, driver);
391 	return err;
392 }
393 
394 /*
395  * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
396  * The only way to detect the sample rate is by looking at wMaxPacketSize.
397  */
create_uaxx_quirk(struct snd_usb_audio * chip,struct usb_interface * iface,struct usb_driver * driver,const struct snd_usb_audio_quirk * quirk)398 static int create_uaxx_quirk(struct snd_usb_audio *chip,
399 			     struct usb_interface *iface,
400 			     struct usb_driver *driver,
401 			     const struct snd_usb_audio_quirk *quirk)
402 {
403 	static const struct audioformat ua_format = {
404 		.formats = SNDRV_PCM_FMTBIT_S24_3LE,
405 		.channels = 2,
406 		.fmt_type = UAC_FORMAT_TYPE_I,
407 		.altsetting = 1,
408 		.altset_idx = 1,
409 		.rates = SNDRV_PCM_RATE_CONTINUOUS,
410 	};
411 	struct usb_host_interface *alts;
412 	struct usb_interface_descriptor *altsd;
413 	struct audioformat *fp;
414 	int err;
415 
416 	/* both PCM and MIDI interfaces have 2 or more altsettings */
417 	if (iface->num_altsetting < 2)
418 		return -ENXIO;
419 	alts = &iface->altsetting[1];
420 	altsd = get_iface_desc(alts);
421 
422 	if (altsd->bNumEndpoints == 2) {
423 		static const struct snd_usb_midi_endpoint_info ua700_ep = {
424 			.out_cables = 0x0003,
425 			.in_cables  = 0x0003
426 		};
427 		static const struct snd_usb_audio_quirk ua700_quirk = {
428 			.type = QUIRK_MIDI_FIXED_ENDPOINT,
429 			.data = &ua700_ep
430 		};
431 		static const struct snd_usb_midi_endpoint_info uaxx_ep = {
432 			.out_cables = 0x0001,
433 			.in_cables  = 0x0001
434 		};
435 		static const struct snd_usb_audio_quirk uaxx_quirk = {
436 			.type = QUIRK_MIDI_FIXED_ENDPOINT,
437 			.data = &uaxx_ep
438 		};
439 		const struct snd_usb_audio_quirk *quirk =
440 			chip->usb_id == USB_ID(0x0582, 0x002b)
441 			? &ua700_quirk : &uaxx_quirk;
442 		return __snd_usbmidi_create(chip->card, iface,
443 					    &chip->midi_list, quirk,
444 					    chip->usb_id,
445 					    &chip->num_rawmidis);
446 	}
447 
448 	if (altsd->bNumEndpoints != 1)
449 		return -ENXIO;
450 
451 	fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
452 	if (!fp)
453 		return -ENOMEM;
454 
455 	fp->iface = altsd->bInterfaceNumber;
456 	fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
457 	fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
458 	fp->datainterval = 0;
459 	fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
460 	INIT_LIST_HEAD(&fp->list);
461 
462 	switch (fp->maxpacksize) {
463 	case 0x120:
464 		fp->rate_max = fp->rate_min = 44100;
465 		break;
466 	case 0x138:
467 	case 0x140:
468 		fp->rate_max = fp->rate_min = 48000;
469 		break;
470 	case 0x258:
471 	case 0x260:
472 		fp->rate_max = fp->rate_min = 96000;
473 		break;
474 	default:
475 		usb_audio_err(chip, "unknown sample rate\n");
476 		kfree(fp);
477 		return -ENXIO;
478 	}
479 
480 	err = add_audio_stream_from_fixed_fmt(chip, fp);
481 	if (err < 0) {
482 		list_del(&fp->list); /* unlink for avoiding double-free */
483 		kfree(fp);
484 		return err;
485 	}
486 	usb_set_interface(chip->dev, fp->iface, 0);
487 	return 0;
488 }
489 
490 /*
491  * Create a standard mixer for the specified interface.
492  */
create_standard_mixer_quirk(struct snd_usb_audio * chip,struct usb_interface * iface,struct usb_driver * driver,const struct snd_usb_audio_quirk * quirk)493 static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
494 				       struct usb_interface *iface,
495 				       struct usb_driver *driver,
496 				       const struct snd_usb_audio_quirk *quirk)
497 {
498 	if (quirk->ifnum < 0)
499 		return 0;
500 
501 	return snd_usb_create_mixer(chip, quirk->ifnum);
502 }
503 
504 /*
505  * audio-interface quirks
506  *
507  * returns zero if no standard audio/MIDI parsing is needed.
508  * returns a positive value if standard audio/midi interfaces are parsed
509  * after this.
510  * returns a negative value at error.
511  */
snd_usb_create_quirk(struct snd_usb_audio * chip,struct usb_interface * iface,struct usb_driver * driver,const struct snd_usb_audio_quirk * quirk)512 int snd_usb_create_quirk(struct snd_usb_audio *chip,
513 			 struct usb_interface *iface,
514 			 struct usb_driver *driver,
515 			 const struct snd_usb_audio_quirk *quirk)
516 {
517 	typedef int (*quirk_func_t)(struct snd_usb_audio *,
518 				    struct usb_interface *,
519 				    struct usb_driver *,
520 				    const struct snd_usb_audio_quirk *);
521 	static const quirk_func_t quirk_funcs[] = {
522 		[QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
523 		[QUIRK_COMPOSITE] = create_composite_quirk,
524 		[QUIRK_AUTODETECT] = create_autodetect_quirk,
525 		[QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
526 		[QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
527 		[QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
528 		[QUIRK_MIDI_ROLAND] = create_any_midi_quirk,
529 		[QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
530 		[QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
531 		[QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
532 		[QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
533 		[QUIRK_MIDI_CME] = create_any_midi_quirk,
534 		[QUIRK_MIDI_AKAI] = create_any_midi_quirk,
535 		[QUIRK_MIDI_FTDI] = create_any_midi_quirk,
536 		[QUIRK_MIDI_CH345] = create_any_midi_quirk,
537 		[QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
538 		[QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
539 		[QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
540 		[QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
541 	};
542 
543 	if (quirk->type < QUIRK_TYPE_COUNT) {
544 		return quirk_funcs[quirk->type](chip, iface, driver, quirk);
545 	} else {
546 		usb_audio_err(chip, "invalid quirk type %d\n", quirk->type);
547 		return -ENXIO;
548 	}
549 }
550 
551 /*
552  * boot quirks
553  */
554 
555 #define EXTIGY_FIRMWARE_SIZE_OLD 794
556 #define EXTIGY_FIRMWARE_SIZE_NEW 483
557 
snd_usb_extigy_boot_quirk(struct usb_device * dev,struct usb_interface * intf)558 static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
559 {
560 	struct usb_host_config *config = dev->actconfig;
561 	int err;
562 
563 	if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
564 	    le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
565 		dev_dbg(&dev->dev, "sending Extigy boot sequence...\n");
566 		/* Send message to force it to reconnect with full interface. */
567 		err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
568 				      0x10, 0x43, 0x0001, 0x000a, NULL, 0);
569 		if (err < 0)
570 			dev_dbg(&dev->dev, "error sending boot message: %d\n", err);
571 		struct usb_device_descriptor *new_device_descriptor __free(kfree) =
572 			kmalloc_obj(*new_device_descriptor);
573 		if (!new_device_descriptor)
574 			return -ENOMEM;
575 		err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
576 				new_device_descriptor, sizeof(*new_device_descriptor));
577 		if (err < 0)
578 			dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
579 		if (new_device_descriptor->bNumConfigurations > dev->descriptor.bNumConfigurations)
580 			dev_dbg(&dev->dev, "error too large bNumConfigurations: %d\n",
581 				new_device_descriptor->bNumConfigurations);
582 		else
583 			memcpy(&dev->descriptor, new_device_descriptor, sizeof(dev->descriptor));
584 		err = usb_reset_configuration(dev);
585 		if (err < 0)
586 			dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
587 		dev_dbg(&dev->dev, "extigy_boot: new boot length = %d\n",
588 			    le16_to_cpu(get_cfg_desc(config)->wTotalLength));
589 		return -ENODEV; /* quit this anyway */
590 	}
591 	return 0;
592 }
593 
snd_usb_audigy2nx_boot_quirk(struct usb_device * dev)594 static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
595 {
596 	u8 buf = 1;
597 
598 	snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
599 			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
600 			0, 0, &buf, 1);
601 	if (buf == 0) {
602 		snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
603 				USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
604 				1, 2000, NULL, 0);
605 		return -ENODEV;
606 	}
607 	return 0;
608 }
609 
snd_usb_fasttrackpro_boot_quirk(struct usb_device * dev)610 static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
611 {
612 	int err;
613 
614 	if (dev->actconfig->desc.bConfigurationValue == 1) {
615 		dev_info(&dev->dev,
616 			   "Fast Track Pro switching to config #2\n");
617 		/* This function has to be available by the usb core module.
618 		 * if it is not avialable the boot quirk has to be left out
619 		 * and the configuration has to be set by udev or hotplug
620 		 * rules
621 		 */
622 		err = usb_driver_set_configuration(dev, 2);
623 		if (err < 0)
624 			dev_dbg(&dev->dev,
625 				"error usb_driver_set_configuration: %d\n",
626 				err);
627 		/* Always return an error, so that we stop creating a device
628 		   that will just be destroyed and recreated with a new
629 		   configuration */
630 		return -ENODEV;
631 	} else
632 		dev_info(&dev->dev, "Fast Track Pro config OK\n");
633 
634 	return 0;
635 }
636 
637 /*
638  * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
639  * documented in the device's data sheet.
640  */
snd_usb_cm106_write_int_reg(struct usb_device * dev,int reg,u16 value)641 static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
642 {
643 	u8 buf[4];
644 	buf[0] = 0x20;
645 	buf[1] = value & 0xff;
646 	buf[2] = (value >> 8) & 0xff;
647 	buf[3] = reg;
648 	return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
649 			       USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
650 			       0, 0, &buf, 4);
651 }
652 
snd_usb_cm106_boot_quirk(struct usb_device * dev)653 static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
654 {
655 	/*
656 	 * Enable line-out driver mode, set headphone source to front
657 	 * channels, enable stereo mic.
658 	 */
659 	return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
660 }
661 
662 /*
663  * CM6206 registers from the CM6206 datasheet rev 2.1
664  */
665 #define CM6206_REG0_DMA_MASTER BIT(15)
666 #define CM6206_REG0_SPDIFO_RATE_48K (2 << 12)
667 #define CM6206_REG0_SPDIFO_RATE_96K (7 << 12)
668 /* Bit 4 thru 11 is the S/PDIF category code */
669 #define CM6206_REG0_SPDIFO_CAT_CODE_GENERAL (0 << 4)
670 #define CM6206_REG0_SPDIFO_EMPHASIS_CD BIT(3)
671 #define CM6206_REG0_SPDIFO_COPYRIGHT_NA BIT(2)
672 #define CM6206_REG0_SPDIFO_NON_AUDIO BIT(1)
673 #define CM6206_REG0_SPDIFO_PRO_FORMAT BIT(0)
674 
675 #define CM6206_REG1_TEST_SEL_CLK BIT(14)
676 #define CM6206_REG1_PLLBIN_EN BIT(13)
677 #define CM6206_REG1_SOFT_MUTE_EN BIT(12)
678 #define CM6206_REG1_GPIO4_OUT BIT(11)
679 #define CM6206_REG1_GPIO4_OE BIT(10)
680 #define CM6206_REG1_GPIO3_OUT BIT(9)
681 #define CM6206_REG1_GPIO3_OE BIT(8)
682 #define CM6206_REG1_GPIO2_OUT BIT(7)
683 #define CM6206_REG1_GPIO2_OE BIT(6)
684 #define CM6206_REG1_GPIO1_OUT BIT(5)
685 #define CM6206_REG1_GPIO1_OE BIT(4)
686 #define CM6206_REG1_SPDIFO_INVALID BIT(3)
687 #define CM6206_REG1_SPDIF_LOOP_EN BIT(2)
688 #define CM6206_REG1_SPDIFO_DIS BIT(1)
689 #define CM6206_REG1_SPDIFI_MIX BIT(0)
690 
691 #define CM6206_REG2_DRIVER_ON BIT(15)
692 #define CM6206_REG2_HEADP_SEL_SIDE_CHANNELS (0 << 13)
693 #define CM6206_REG2_HEADP_SEL_SURROUND_CHANNELS (1 << 13)
694 #define CM6206_REG2_HEADP_SEL_CENTER_SUBW (2 << 13)
695 #define CM6206_REG2_HEADP_SEL_FRONT_CHANNELS (3 << 13)
696 #define CM6206_REG2_MUTE_HEADPHONE_RIGHT BIT(12)
697 #define CM6206_REG2_MUTE_HEADPHONE_LEFT BIT(11)
698 #define CM6206_REG2_MUTE_REAR_SURROUND_RIGHT BIT(10)
699 #define CM6206_REG2_MUTE_REAR_SURROUND_LEFT BIT(9)
700 #define CM6206_REG2_MUTE_SIDE_SURROUND_RIGHT BIT(8)
701 #define CM6206_REG2_MUTE_SIDE_SURROUND_LEFT BIT(7)
702 #define CM6206_REG2_MUTE_SUBWOOFER BIT(6)
703 #define CM6206_REG2_MUTE_CENTER BIT(5)
704 #define CM6206_REG2_MUTE_RIGHT_FRONT BIT(3)
705 #define CM6206_REG2_MUTE_LEFT_FRONT BIT(3)
706 #define CM6206_REG2_EN_BTL BIT(2)
707 #define CM6206_REG2_MCUCLKSEL_1_5_MHZ (0)
708 #define CM6206_REG2_MCUCLKSEL_3_MHZ (1)
709 #define CM6206_REG2_MCUCLKSEL_6_MHZ (2)
710 #define CM6206_REG2_MCUCLKSEL_12_MHZ (3)
711 
712 /* Bit 11..13 sets the sensitivity to FLY tuner volume control VP/VD signal */
713 #define CM6206_REG3_FLYSPEED_DEFAULT (2 << 11)
714 #define CM6206_REG3_VRAP25EN BIT(10)
715 #define CM6206_REG3_MSEL1 BIT(9)
716 #define CM6206_REG3_SPDIFI_RATE_44_1K BIT(0 << 7)
717 #define CM6206_REG3_SPDIFI_RATE_48K BIT(2 << 7)
718 #define CM6206_REG3_SPDIFI_RATE_32K BIT(3 << 7)
719 #define CM6206_REG3_PINSEL BIT(6)
720 #define CM6206_REG3_FOE BIT(5)
721 #define CM6206_REG3_ROE BIT(4)
722 #define CM6206_REG3_CBOE BIT(3)
723 #define CM6206_REG3_LOSE BIT(2)
724 #define CM6206_REG3_HPOE BIT(1)
725 #define CM6206_REG3_SPDIFI_CANREC BIT(0)
726 
727 #define CM6206_REG5_DA_RSTN BIT(13)
728 #define CM6206_REG5_AD_RSTN BIT(12)
729 #define CM6206_REG5_SPDIFO_AD2SPDO BIT(12)
730 #define CM6206_REG5_SPDIFO_SEL_FRONT (0 << 9)
731 #define CM6206_REG5_SPDIFO_SEL_SIDE_SUR (1 << 9)
732 #define CM6206_REG5_SPDIFO_SEL_CEN_LFE (2 << 9)
733 #define CM6206_REG5_SPDIFO_SEL_REAR_SUR (3 << 9)
734 #define CM6206_REG5_CODECM BIT(8)
735 #define CM6206_REG5_EN_HPF BIT(7)
736 #define CM6206_REG5_T_SEL_DSDA4 BIT(6)
737 #define CM6206_REG5_T_SEL_DSDA3 BIT(5)
738 #define CM6206_REG5_T_SEL_DSDA2 BIT(4)
739 #define CM6206_REG5_T_SEL_DSDA1 BIT(3)
740 #define CM6206_REG5_T_SEL_DSDAD_NORMAL 0
741 #define CM6206_REG5_T_SEL_DSDAD_FRONT 4
742 #define CM6206_REG5_T_SEL_DSDAD_S_SURROUND 5
743 #define CM6206_REG5_T_SEL_DSDAD_CEN_LFE 6
744 #define CM6206_REG5_T_SEL_DSDAD_R_SURROUND 7
745 
snd_usb_cm6206_boot_quirk(struct usb_device * dev)746 static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
747 {
748 	int err  = 0, reg;
749 	int val[] = {
750 		/*
751 		 * Values here are chosen based on sniffing USB traffic
752 		 * under Windows.
753 		 *
754 		 * REG0: DAC is master, sample rate 48kHz, no copyright
755 		 */
756 		CM6206_REG0_SPDIFO_RATE_48K |
757 		CM6206_REG0_SPDIFO_COPYRIGHT_NA,
758 		/*
759 		 * REG1: PLL binary search enable, soft mute enable.
760 		 */
761 		CM6206_REG1_PLLBIN_EN |
762 		CM6206_REG1_SOFT_MUTE_EN,
763 		/*
764 		 * REG2: enable output drivers,
765 		 * select front channels to the headphone output,
766 		 * then mute the headphone channels, run the MCU
767 		 * at 1.5 MHz.
768 		 */
769 		CM6206_REG2_DRIVER_ON |
770 		CM6206_REG2_HEADP_SEL_FRONT_CHANNELS |
771 		CM6206_REG2_MUTE_HEADPHONE_RIGHT |
772 		CM6206_REG2_MUTE_HEADPHONE_LEFT,
773 		/*
774 		 * REG3: default flyspeed, set 2.5V mic bias
775 		 * enable all line out ports and enable SPDIF
776 		 */
777 		CM6206_REG3_FLYSPEED_DEFAULT |
778 		CM6206_REG3_VRAP25EN |
779 		CM6206_REG3_FOE |
780 		CM6206_REG3_ROE |
781 		CM6206_REG3_CBOE |
782 		CM6206_REG3_LOSE |
783 		CM6206_REG3_HPOE |
784 		CM6206_REG3_SPDIFI_CANREC,
785 		/* REG4 is just a bunch of GPIO lines */
786 		0x0000,
787 		/* REG5: de-assert AD/DA reset signals */
788 		CM6206_REG5_DA_RSTN |
789 		CM6206_REG5_AD_RSTN };
790 
791 	for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
792 		err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
793 		if (err < 0)
794 			return err;
795 	}
796 
797 	return err;
798 }
799 
800 /* quirk for Plantronics GameCom 780 with CM6302 chip */
snd_usb_gamecon780_boot_quirk(struct usb_device * dev)801 static int snd_usb_gamecon780_boot_quirk(struct usb_device *dev)
802 {
803 	/* set the initial volume and don't change; other values are either
804 	 * too loud or silent due to firmware bug (bko#65251)
805 	 */
806 	u8 buf[2] = { 0x74, 0xe3 };
807 	return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
808 			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
809 			UAC_FU_VOLUME << 8, 9 << 8, buf, 2);
810 }
811 
812 /*
813  * Novation Twitch DJ controller
814  * Focusrite Novation Saffire 6 USB audio card
815  */
snd_usb_novation_boot_quirk(struct usb_device * dev)816 static int snd_usb_novation_boot_quirk(struct usb_device *dev)
817 {
818 	/* preemptively set up the device because otherwise the
819 	 * raw MIDI endpoints are not active */
820 	usb_set_interface(dev, 0, 1);
821 	return 0;
822 }
823 
824 /*
825  * This call will put the synth in "USB send" mode, i.e it will send MIDI
826  * messages through USB (this is disabled at startup). The synth will
827  * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
828  * sign on its LCD. Values here are chosen based on sniffing USB traffic
829  * under Windows.
830  */
snd_usb_accessmusic_boot_quirk(struct usb_device * dev)831 static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
832 {
833 	int err, actual_length;
834 	/* "midi send" enable */
835 	static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
836 	void *buf;
837 
838 	if (usb_pipe_type_check(dev, usb_sndintpipe(dev, 0x05)))
839 		return -EINVAL;
840 	buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
841 	if (!buf)
842 		return -ENOMEM;
843 	err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
844 			ARRAY_SIZE(seq), &actual_length, 1000);
845 	kfree(buf);
846 	if (err < 0)
847 		return err;
848 
849 	return 0;
850 }
851 
852 /*
853  * A post configuration device descriptor read is needed to make the CM1A
854  * operational after reenumeration.
855  */
snd_usb_cm1a_boot_quirk(struct usb_device * dev)856 static int snd_usb_cm1a_boot_quirk(struct usb_device *dev)
857 {
858 	struct usb_device_descriptor *desc __free(kfree) = kmalloc_obj(*desc);
859 	int err;
860 
861 	if (!desc)
862 		return -ENOMEM;
863 
864 	err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, sizeof(*desc));
865 	if (err < 0) {
866 		dev_err(&dev->dev, "failed to read device descriptor: %d\n", err);
867 		return err;
868 	}
869 
870 	return 0;
871 }
872 
873 /*
874  * Some sound cards from Native Instruments are in fact compliant to the USB
875  * audio standard of version 2 and other approved USB standards, even though
876  * they come up as vendor-specific device when first connected.
877  *
878  * However, they can be told to come up with a new set of descriptors
879  * upon their next enumeration, and the interfaces announced by the new
880  * descriptors will then be handled by the kernel's class drivers. As the
881  * product ID will also change, no further checks are required.
882  */
883 
snd_usb_nativeinstruments_boot_quirk(struct usb_device * dev)884 static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
885 {
886 	int ret;
887 
888 	ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
889 				  0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
890 				  1, 0, NULL, 0, 1000);
891 
892 	if (ret < 0)
893 		return ret;
894 
895 	usb_reset_device(dev);
896 
897 	/* return -EAGAIN, so the creation of an audio interface for this
898 	 * temporary device is aborted. The device will reconnect with a
899 	 * new product ID */
900 	return -EAGAIN;
901 }
902 
mbox2_setup_48_24_magic(struct usb_device * dev)903 static void mbox2_setup_48_24_magic(struct usb_device *dev)
904 {
905 	u8 srate[3];
906 	u8 temp[12];
907 
908 	/* Choose 48000Hz permanently */
909 	srate[0] = 0x80;
910 	srate[1] = 0xbb;
911 	srate[2] = 0x00;
912 
913 	/* Send the magic! */
914 	snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
915 		0x01, 0x22, 0x0100, 0x0085, &temp, 0x0003);
916 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
917 		0x81, 0xa2, 0x0100, 0x0085, &srate, 0x0003);
918 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
919 		0x81, 0xa2, 0x0100, 0x0086, &srate, 0x0003);
920 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
921 		0x81, 0xa2, 0x0100, 0x0003, &srate, 0x0003);
922 	return;
923 }
924 
925 /* Digidesign Mbox 2 needs to load firmware onboard
926  * and driver must wait a few seconds for initialisation.
927  */
928 
929 #define MBOX2_FIRMWARE_SIZE    646
930 #define MBOX2_BOOT_LOADING     0x01 /* Hard coded into the device */
931 #define MBOX2_BOOT_READY       0x02 /* Hard coded into the device */
932 
snd_usb_mbox2_boot_quirk(struct usb_device * dev)933 static int snd_usb_mbox2_boot_quirk(struct usb_device *dev)
934 {
935 	struct usb_host_config *config = dev->actconfig;
936 	int err;
937 	u8 bootresponse[0x12];
938 	int fwsize;
939 	int count;
940 
941 	fwsize = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
942 
943 	if (fwsize != MBOX2_FIRMWARE_SIZE) {
944 		dev_err(&dev->dev, "Invalid firmware size=%d.\n", fwsize);
945 		return -ENODEV;
946 	}
947 
948 	dev_dbg(&dev->dev, "Sending Digidesign Mbox 2 boot sequence...\n");
949 
950 	count = 0;
951 	bootresponse[0] = MBOX2_BOOT_LOADING;
952 	while ((bootresponse[0] == MBOX2_BOOT_LOADING) && (count < 10)) {
953 		msleep(500); /* 0.5 second delay */
954 		snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
955 			/* Control magic - load onboard firmware */
956 			0x85, 0xc0, 0x0001, 0x0000, &bootresponse, 0x0012);
957 		if (bootresponse[0] == MBOX2_BOOT_READY)
958 			break;
959 		dev_dbg(&dev->dev, "device not ready, resending boot sequence...\n");
960 		count++;
961 	}
962 
963 	if (bootresponse[0] != MBOX2_BOOT_READY) {
964 		dev_err(&dev->dev, "Unknown bootresponse=%d, or timed out, ignoring device.\n", bootresponse[0]);
965 		return -ENODEV;
966 	}
967 
968 	dev_dbg(&dev->dev, "device initialised!\n");
969 
970 	struct usb_device_descriptor *new_device_descriptor __free(kfree) =
971 		kmalloc_obj(*new_device_descriptor);
972 	if (!new_device_descriptor)
973 		return -ENOMEM;
974 
975 	err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
976 		new_device_descriptor, sizeof(*new_device_descriptor));
977 	if (err < 0)
978 		dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
979 	if (new_device_descriptor->bNumConfigurations > dev->descriptor.bNumConfigurations)
980 		dev_dbg(&dev->dev, "error too large bNumConfigurations: %d\n",
981 			new_device_descriptor->bNumConfigurations);
982 	else
983 		memcpy(&dev->descriptor, new_device_descriptor, sizeof(dev->descriptor));
984 
985 	err = usb_reset_configuration(dev);
986 	if (err < 0)
987 		dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
988 	dev_dbg(&dev->dev, "mbox2_boot: new boot length = %d\n",
989 		le16_to_cpu(get_cfg_desc(config)->wTotalLength));
990 
991 	mbox2_setup_48_24_magic(dev);
992 
993 	dev_info(&dev->dev, "Digidesign Mbox 2: 24bit 48kHz");
994 
995 	return 0; /* Successful boot */
996 }
997 
snd_usb_axefx3_boot_quirk(struct usb_device * dev)998 static int snd_usb_axefx3_boot_quirk(struct usb_device *dev)
999 {
1000 	int err;
1001 
1002 	dev_dbg(&dev->dev, "Waiting for Axe-Fx III to boot up...\n");
1003 
1004 	/* If the Axe-Fx III has not fully booted, it will timeout when trying
1005 	 * to enable the audio streaming interface. A more generous timeout is
1006 	 * used here to detect when the Axe-Fx III has finished booting as the
1007 	 * set interface message will be acked once it has
1008 	 */
1009 	err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1010 				USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1011 				1, 1, NULL, 0, 120000);
1012 	if (err < 0) {
1013 		dev_err(&dev->dev,
1014 			"failed waiting for Axe-Fx III to boot: %d\n", err);
1015 		return err;
1016 	}
1017 
1018 	dev_dbg(&dev->dev, "Axe-Fx III is now ready\n");
1019 
1020 	err = usb_set_interface(dev, 1, 0);
1021 	if (err < 0)
1022 		dev_dbg(&dev->dev,
1023 			"error stopping Axe-Fx III interface: %d\n", err);
1024 
1025 	return 0;
1026 }
1027 
mbox3_setup_defaults(struct usb_device * dev)1028 static void mbox3_setup_defaults(struct usb_device *dev)
1029 {
1030 	/* The Mbox 3 is "little endian" */
1031 	/* max volume is: 0x0000. */
1032 	/* min volume is: 0x0080 (shown in little endian form) */
1033 
1034 	u8 com_buff[2];
1035 
1036 	/* Deactivate Tuner */
1037 	/* on  = 0x01*/
1038 	/* off = 0x00*/
1039 	com_buff[0] = 0x00;
1040 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1041 		0x01, 0x21, 0x0003, 0x2001, &com_buff, 1);
1042 
1043 	/* Set clock source to Internal (as opposed to S/PDIF) */
1044 	/* Internal  = 0x01*/
1045 	/* S/PDIF    = 0x02*/
1046 	com_buff[0] = 0x01;
1047 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1048 			1, 0x21, 0x0100, 0x8001, &com_buff, 1);
1049 
1050 	/* Mute the hardware loopbacks to start the device in a known state. */
1051 	com_buff[0] = 0x00;
1052 	com_buff[1] = 0x80;
1053 	/* Analogue input 1 left channel: */
1054 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1055 			1, 0x21, 0x0110, 0x4001, &com_buff, 2);
1056 	/* Analogue input 1 right channel: */
1057 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1058 			1, 0x21, 0x0111, 0x4001, &com_buff, 2);
1059 	/* Analogue input 2 left channel: */
1060 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1061 			1, 0x21, 0x0114, 0x4001, &com_buff, 2);
1062 	/* Analogue input 2 right channel: */
1063 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1064 			1, 0x21, 0x0115, 0x4001, &com_buff, 2);
1065 	/* Analogue input 3 left channel: */
1066 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1067 			1, 0x21, 0x0118, 0x4001, &com_buff, 2);
1068 	/* Analogue input 3 right channel: */
1069 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1070 			1, 0x21, 0x0119, 0x4001, &com_buff, 2);
1071 	/* Analogue input 4 left channel: */
1072 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1073 			1, 0x21, 0x011c, 0x4001, &com_buff, 2);
1074 	/* Analogue input 4 right channel: */
1075 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1076 			1, 0x21, 0x011d, 0x4001, &com_buff, 2);
1077 
1078 	/* Set software sends to output */
1079 	com_buff[0] = 0x00;
1080 	com_buff[1] = 0x00;
1081 	/* Analogue software return 1 left channel: */
1082 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1083 			1, 0x21, 0x0100, 0x4001, &com_buff, 2);
1084 	com_buff[0] = 0x00;
1085 	com_buff[1] = 0x80;
1086 	/* Analogue software return 1 right channel: */
1087 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1088 			1, 0x21, 0x0101, 0x4001, &com_buff, 2);
1089 	com_buff[0] = 0x00;
1090 	com_buff[1] = 0x80;
1091 	/* Analogue software return 2 left channel: */
1092 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1093 			1, 0x21, 0x0104, 0x4001, &com_buff, 2);
1094 	com_buff[0] = 0x00;
1095 	com_buff[1] = 0x00;
1096 	/* Analogue software return 2 right channel: */
1097 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1098 			1, 0x21, 0x0105, 0x4001, &com_buff, 2);
1099 
1100 	com_buff[0] = 0x00;
1101 	com_buff[1] = 0x80;
1102 	/* Analogue software return 3 left channel: */
1103 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1104 			1, 0x21, 0x0108, 0x4001, &com_buff, 2);
1105 	/* Analogue software return 3 right channel: */
1106 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1107 			1, 0x21, 0x0109, 0x4001, &com_buff, 2);
1108 	/* Analogue software return 4 left channel: */
1109 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1110 			1, 0x21, 0x010c, 0x4001, &com_buff, 2);
1111 	/* Analogue software return 4 right channel: */
1112 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1113 			1, 0x21, 0x010d, 0x4001, &com_buff, 2);
1114 
1115 	/* Return to muting sends */
1116 	com_buff[0] = 0x00;
1117 	com_buff[1] = 0x80;
1118 	/* Analogue fx return left channel: */
1119 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1120 			1, 0x21, 0x0120, 0x4001, &com_buff, 2);
1121 	/* Analogue fx return right channel: */
1122 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1123 			1, 0x21, 0x0121, 0x4001, &com_buff, 2);
1124 
1125 	/* Analogue software input 1 fx send: */
1126 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1127 			1, 0x21, 0x0100, 0x4201, &com_buff, 2);
1128 	/* Analogue software input 2 fx send: */
1129 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1130 			1, 0x21, 0x0101, 0x4201, &com_buff, 2);
1131 	/* Analogue software input 3 fx send: */
1132 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1133 			1, 0x21, 0x0102, 0x4201, &com_buff, 2);
1134 	/* Analogue software input 4 fx send: */
1135 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1136 			1, 0x21, 0x0103, 0x4201, &com_buff, 2);
1137 	/* Analogue input 1 fx send: */
1138 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1139 			1, 0x21, 0x0104, 0x4201, &com_buff, 2);
1140 	/* Analogue input 2 fx send: */
1141 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1142 			1, 0x21, 0x0105, 0x4201, &com_buff, 2);
1143 	/* Analogue input 3 fx send: */
1144 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1145 			1, 0x21, 0x0106, 0x4201, &com_buff, 2);
1146 	/* Analogue input 4 fx send: */
1147 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1148 			1, 0x21, 0x0107, 0x4201, &com_buff, 2);
1149 
1150 	/* Toggle allowing host control */
1151 	/* Not needed
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 
1157 	/* Do not dim fx returns */
1158 	com_buff[0] = 0x00;
1159 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1160 			3, 0x21, 0x0002, 0x2001, &com_buff, 1);
1161 
1162 	/* Do not set fx returns to mono */
1163 	com_buff[0] = 0x00;
1164 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1165 			3, 0x21, 0x0001, 0x2001, &com_buff, 1);
1166 
1167 	/* Mute the S/PDIF hardware loopback
1168 	 * same odd volume logic here as above
1169 	 */
1170 	com_buff[0] = 0x00;
1171 	com_buff[1] = 0x80;
1172 	/* S/PDIF hardware input 1 left channel */
1173 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1174 			1, 0x21, 0x0112, 0x4001, &com_buff, 2);
1175 	/* S/PDIF hardware input 1 right channel */
1176 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1177 			1, 0x21, 0x0113, 0x4001, &com_buff, 2);
1178 	/* S/PDIF hardware input 2 left channel */
1179 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1180 			1, 0x21, 0x0116, 0x4001, &com_buff, 2);
1181 	/* S/PDIF hardware input 2 right channel */
1182 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1183 			1, 0x21, 0x0117, 0x4001, &com_buff, 2);
1184 	/* S/PDIF hardware input 3 left channel */
1185 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1186 			1, 0x21, 0x011a, 0x4001, &com_buff, 2);
1187 	/* S/PDIF hardware input 3 right channel */
1188 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1189 			1, 0x21, 0x011b, 0x4001, &com_buff, 2);
1190 	/* S/PDIF hardware input 4 left channel */
1191 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1192 			1, 0x21, 0x011e, 0x4001, &com_buff, 2);
1193 	/* S/PDIF hardware input 4 right channel */
1194 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1195 			1, 0x21, 0x011f, 0x4001, &com_buff, 2);
1196 	/* S/PDIF software return 1 left channel */
1197 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1198 			1, 0x21, 0x0102, 0x4001, &com_buff, 2);
1199 	/* S/PDIF software return 1 right channel */
1200 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1201 			1, 0x21, 0x0103, 0x4001, &com_buff, 2);
1202 	/* S/PDIF software return 2 left channel */
1203 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1204 			1, 0x21, 0x0106, 0x4001, &com_buff, 2);
1205 	/* S/PDIF software return 2 right channel */
1206 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1207 			1, 0x21, 0x0107, 0x4001, &com_buff, 2);
1208 
1209 	com_buff[0] = 0x00;
1210 	com_buff[1] = 0x00;
1211 	/* S/PDIF software return 3 left channel */
1212 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1213 			1, 0x21, 0x010a, 0x4001, &com_buff, 2);
1214 
1215 	com_buff[0] = 0x00;
1216 	com_buff[1] = 0x80;
1217 	/* S/PDIF software return 3 right channel */
1218 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1219 			1, 0x21, 0x010b, 0x4001, &com_buff, 2);
1220 	/* S/PDIF software return 4 left channel */
1221 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1222 			1, 0x21, 0x010e, 0x4001, &com_buff, 2);
1223 
1224 	com_buff[0] = 0x00;
1225 	com_buff[1] = 0x00;
1226 	/* S/PDIF software return 4 right channel */
1227 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1228 			1, 0x21, 0x010f, 0x4001, &com_buff, 2);
1229 
1230 	com_buff[0] = 0x00;
1231 	com_buff[1] = 0x80;
1232 	/* S/PDIF fx returns left channel */
1233 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1234 			1, 0x21, 0x0122, 0x4001, &com_buff, 2);
1235 	/* S/PDIF fx returns right channel */
1236 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1237 			1, 0x21, 0x0123, 0x4001, &com_buff, 2);
1238 
1239 	/* Set the dropdown "Effect" to the first option */
1240 	/* Room1  = 0x00 */
1241 	/* Room2  = 0x01 */
1242 	/* Room3  = 0x02 */
1243 	/* Hall 1 = 0x03 */
1244 	/* Hall 2 = 0x04 */
1245 	/* Plate  = 0x05 */
1246 	/* Delay  = 0x06 */
1247 	/* Echo   = 0x07 */
1248 	com_buff[0] = 0x00;
1249 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1250 			1, 0x21, 0x0200, 0x4301, &com_buff, 1);	/* max is 0xff */
1251 	/* min is 0x00 */
1252 
1253 
1254 	/* Set the effect duration to 0 */
1255 	/* max is 0xffff */
1256 	/* min is 0x0000 */
1257 	com_buff[0] = 0x00;
1258 	com_buff[1] = 0x00;
1259 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1260 			1, 0x21, 0x0400, 0x4301, &com_buff, 2);
1261 
1262 	/* Set the effect volume and feedback to 0 */
1263 	/* max is 0xff */
1264 	/* min is 0x00 */
1265 	com_buff[0] = 0x00;
1266 	/* feedback: */
1267 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1268 			1, 0x21, 0x0500, 0x4301, &com_buff, 1);
1269 	/* volume: */
1270 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1271 			1, 0x21, 0x0300, 0x4301, &com_buff, 1);
1272 
1273 	/* Set soft button hold duration */
1274 	/* 0x03 = 250ms */
1275 	/* 0x05 = 500ms DEFAULT */
1276 	/* 0x08 = 750ms */
1277 	/* 0x0a = 1sec */
1278 	com_buff[0] = 0x05;
1279 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1280 			3, 0x21, 0x0005, 0x2001, &com_buff, 1);
1281 
1282 	/* Use dim LEDs for button of state */
1283 	com_buff[0] = 0x00;
1284 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1285 			3, 0x21, 0x0004, 0x2001, &com_buff, 1);
1286 }
1287 
1288 #define MBOX3_DESCRIPTOR_SIZE	464
1289 
snd_usb_mbox3_boot_quirk(struct usb_device * dev)1290 static int snd_usb_mbox3_boot_quirk(struct usb_device *dev)
1291 {
1292 	struct usb_host_config *config = dev->actconfig;
1293 	int err;
1294 	int descriptor_size;
1295 
1296 	descriptor_size = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
1297 
1298 	if (descriptor_size != MBOX3_DESCRIPTOR_SIZE) {
1299 		dev_err(&dev->dev, "MBOX3: Invalid descriptor size=%d.\n", descriptor_size);
1300 		return -ENODEV;
1301 	}
1302 
1303 	dev_dbg(&dev->dev, "MBOX3: device initialised!\n");
1304 
1305 	struct usb_device_descriptor *new_device_descriptor __free(kfree) =
1306 		kmalloc_obj(*new_device_descriptor);
1307 	if (!new_device_descriptor)
1308 		return -ENOMEM;
1309 
1310 	err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
1311 		new_device_descriptor, sizeof(*new_device_descriptor));
1312 	if (err < 0)
1313 		dev_dbg(&dev->dev, "MBOX3: error usb_get_descriptor: %d\n", err);
1314 	if (new_device_descriptor->bNumConfigurations > dev->descriptor.bNumConfigurations)
1315 		dev_dbg(&dev->dev, "MBOX3: error too large bNumConfigurations: %d\n",
1316 			new_device_descriptor->bNumConfigurations);
1317 	else
1318 		memcpy(&dev->descriptor, new_device_descriptor, sizeof(dev->descriptor));
1319 
1320 	err = usb_reset_configuration(dev);
1321 	if (err < 0)
1322 		dev_dbg(&dev->dev, "MBOX3: error usb_reset_configuration: %d\n", err);
1323 
1324 	dev_dbg(&dev->dev, "MBOX3: new boot length = %d\n",
1325 		le16_to_cpu(get_cfg_desc(config)->wTotalLength));
1326 
1327 	mbox3_setup_defaults(dev);
1328 	dev_info(&dev->dev, "MBOX3: Initialized.");
1329 
1330 	return 0; /* Successful boot */
1331 }
1332 
1333 #define MICROBOOK_BUF_SIZE 128
1334 
snd_usb_motu_microbookii_communicate(struct usb_device * dev,u8 * buf,int buf_size,int * length)1335 static int snd_usb_motu_microbookii_communicate(struct usb_device *dev, u8 *buf,
1336 						int buf_size, int *length)
1337 {
1338 	int err, actual_length;
1339 
1340 	if (usb_pipe_type_check(dev, usb_sndintpipe(dev, 0x01)))
1341 		return -EINVAL;
1342 	err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x01), buf, *length,
1343 				&actual_length, 1000);
1344 	if (err < 0)
1345 		return err;
1346 
1347 	print_hex_dump(KERN_DEBUG, "MicroBookII snd: ", DUMP_PREFIX_NONE, 16, 1,
1348 		       buf, actual_length, false);
1349 
1350 	memset(buf, 0, buf_size);
1351 
1352 	if (usb_pipe_type_check(dev, usb_rcvintpipe(dev, 0x82)))
1353 		return -EINVAL;
1354 	err = usb_interrupt_msg(dev, usb_rcvintpipe(dev, 0x82), buf, buf_size,
1355 				&actual_length, 1000);
1356 	if (err < 0)
1357 		return err;
1358 
1359 	print_hex_dump(KERN_DEBUG, "MicroBookII rcv: ", DUMP_PREFIX_NONE, 16, 1,
1360 		       buf, actual_length, false);
1361 
1362 	*length = actual_length;
1363 	return 0;
1364 }
1365 
snd_usb_motu_microbookii_boot_quirk(struct usb_device * dev)1366 static int snd_usb_motu_microbookii_boot_quirk(struct usb_device *dev)
1367 {
1368 	int err, actual_length, poll_attempts = 0;
1369 	static const u8 set_samplerate_seq[] = { 0x00, 0x00, 0x00, 0x00,
1370 						 0x00, 0x00, 0x0b, 0x14,
1371 						 0x00, 0x00, 0x00, 0x01 };
1372 	static const u8 poll_ready_seq[] = { 0x00, 0x04, 0x00, 0x00,
1373 					     0x00, 0x00, 0x0b, 0x18 };
1374 	u8 *buf = kzalloc(MICROBOOK_BUF_SIZE, GFP_KERNEL);
1375 
1376 	if (!buf)
1377 		return -ENOMEM;
1378 
1379 	dev_info(&dev->dev, "Waiting for MOTU Microbook II to boot up...\n");
1380 
1381 	/* First we tell the device which sample rate to use. */
1382 	memcpy(buf, set_samplerate_seq, sizeof(set_samplerate_seq));
1383 	actual_length = sizeof(set_samplerate_seq);
1384 	err = snd_usb_motu_microbookii_communicate(dev, buf, MICROBOOK_BUF_SIZE,
1385 						   &actual_length);
1386 
1387 	if (err < 0) {
1388 		dev_err(&dev->dev,
1389 			"failed setting the sample rate for Motu MicroBook II: %d\n",
1390 			err);
1391 		goto free_buf;
1392 	}
1393 
1394 	/* Then we poll every 100 ms until the device informs of its readiness. */
1395 	while (true) {
1396 		if (++poll_attempts > 100) {
1397 			dev_err(&dev->dev,
1398 				"failed booting Motu MicroBook II: timeout\n");
1399 			err = -ENODEV;
1400 			goto free_buf;
1401 		}
1402 
1403 		memset(buf, 0, MICROBOOK_BUF_SIZE);
1404 		memcpy(buf, poll_ready_seq, sizeof(poll_ready_seq));
1405 
1406 		actual_length = sizeof(poll_ready_seq);
1407 		err = snd_usb_motu_microbookii_communicate(
1408 			dev, buf, MICROBOOK_BUF_SIZE, &actual_length);
1409 		if (err < 0) {
1410 			dev_err(&dev->dev,
1411 				"failed booting Motu MicroBook II: communication error %d\n",
1412 				err);
1413 			goto free_buf;
1414 		}
1415 
1416 		/* the device signals its readiness through a message of the
1417 		 * form
1418 		 *           XX 06 00 00 00 00 0b 18  00 00 00 01
1419 		 * If the device is not yet ready to accept audio data, the
1420 		 * last byte of that sequence is 00.
1421 		 */
1422 		if (actual_length == 12 && buf[actual_length - 1] == 1)
1423 			break;
1424 
1425 		msleep(100);
1426 	}
1427 
1428 	dev_info(&dev->dev, "MOTU MicroBook II ready\n");
1429 
1430 free_buf:
1431 	kfree(buf);
1432 	return err;
1433 }
1434 
snd_usb_motu_m_series_boot_quirk(struct usb_device * dev)1435 static int snd_usb_motu_m_series_boot_quirk(struct usb_device *dev)
1436 {
1437 	msleep(4000);
1438 
1439 	return 0;
1440 }
1441 
snd_usb_rme_digiface_boot_quirk(struct usb_device * dev)1442 static int snd_usb_rme_digiface_boot_quirk(struct usb_device *dev)
1443 {
1444 	/* Disable mixer, internal clock, all outputs ADAT, 48kHz, TMS off */
1445 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1446 			16, 0x40, 0x2410, 0x7fff, NULL, 0);
1447 	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1448 			18, 0x40, 0x0104, 0xffff, NULL, 0);
1449 
1450 	/* Disable loopback for all inputs */
1451 	for (int ch = 0; ch < 32; ch++)
1452 		snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1453 				22, 0x40, 0x400, ch, NULL, 0);
1454 
1455 	/* Unity gain for all outputs */
1456 	for (int ch = 0; ch < 34; ch++)
1457 		snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1458 				21, 0x40, 0x9000, 0x100 + ch, NULL, 0);
1459 
1460 	return 0;
1461 }
1462 
1463 /*
1464  * Setup quirks
1465  */
1466 #define MAUDIO_SET		0x01 /* parse device_setup */
1467 #define MAUDIO_SET_COMPATIBLE	0x80 /* use only "win-compatible" interfaces */
1468 #define MAUDIO_SET_DTS		0x02 /* enable DTS Digital Output */
1469 #define MAUDIO_SET_96K		0x04 /* 48-96kHz rate if set, 8-48kHz otherwise */
1470 #define MAUDIO_SET_24B		0x08 /* 24bits sample if set, 16bits otherwise */
1471 #define MAUDIO_SET_DI		0x10 /* enable Digital Input */
1472 #define MAUDIO_SET_MASK		0x1f /* bit mask for setup value */
1473 #define MAUDIO_SET_24B_48K_DI	 0x19 /* 24bits+48kHz+Digital Input */
1474 #define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48kHz+No Digital Input */
1475 #define MAUDIO_SET_16B_48K_DI	 0x11 /* 16bits+48kHz+Digital Input */
1476 #define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48kHz+No Digital Input */
1477 
quattro_skip_setting_quirk(struct snd_usb_audio * chip,int iface,int altno)1478 static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
1479 				      int iface, int altno)
1480 {
1481 	/* Reset ALL ifaces to 0 altsetting.
1482 	 * Call it for every possible altsetting of every interface.
1483 	 */
1484 	usb_set_interface(chip->dev, iface, 0);
1485 	if (chip->setup & MAUDIO_SET) {
1486 		if (chip->setup & MAUDIO_SET_COMPATIBLE) {
1487 			if (iface != 1 && iface != 2)
1488 				return 1; /* skip all interfaces but 1 and 2 */
1489 		} else {
1490 			unsigned int mask;
1491 			if (iface == 1 || iface == 2)
1492 				return 1; /* skip interfaces 1 and 2 */
1493 			if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
1494 				return 1; /* skip this altsetting */
1495 			mask = chip->setup & MAUDIO_SET_MASK;
1496 			if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
1497 				return 1; /* skip this altsetting */
1498 			if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
1499 				return 1; /* skip this altsetting */
1500 			if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
1501 				return 1; /* skip this altsetting */
1502 		}
1503 	}
1504 	usb_audio_dbg(chip,
1505 		    "using altsetting %d for interface %d config %d\n",
1506 		    altno, iface, chip->setup);
1507 	return 0; /* keep this altsetting */
1508 }
1509 
audiophile_skip_setting_quirk(struct snd_usb_audio * chip,int iface,int altno)1510 static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
1511 					 int iface,
1512 					 int altno)
1513 {
1514 	/* Reset ALL ifaces to 0 altsetting.
1515 	 * Call it for every possible altsetting of every interface.
1516 	 */
1517 	usb_set_interface(chip->dev, iface, 0);
1518 
1519 	if (chip->setup & MAUDIO_SET) {
1520 		unsigned int mask;
1521 		if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
1522 			return 1; /* skip this altsetting */
1523 		if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
1524 			return 1; /* skip this altsetting */
1525 		mask = chip->setup & MAUDIO_SET_MASK;
1526 		if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
1527 			return 1; /* skip this altsetting */
1528 		if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
1529 			return 1; /* skip this altsetting */
1530 		if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
1531 			return 1; /* skip this altsetting */
1532 		if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
1533 			return 1; /* skip this altsetting */
1534 	}
1535 
1536 	return 0; /* keep this altsetting */
1537 }
1538 
fasttrackpro_skip_setting_quirk(struct snd_usb_audio * chip,int iface,int altno)1539 static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
1540 					   int iface, int altno)
1541 {
1542 	/* Reset ALL ifaces to 0 altsetting.
1543 	 * Call it for every possible altsetting of every interface.
1544 	 */
1545 	usb_set_interface(chip->dev, iface, 0);
1546 
1547 	/* possible configuration where both inputs and only one output is
1548 	 *used is not supported by the current setup
1549 	 */
1550 	if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
1551 		if (chip->setup & MAUDIO_SET_96K) {
1552 			if (altno != 3 && altno != 6)
1553 				return 1;
1554 		} else if (chip->setup & MAUDIO_SET_DI) {
1555 			if (iface == 4)
1556 				return 1; /* no analog input */
1557 			if (altno != 2 && altno != 5)
1558 				return 1; /* enable only altsets 2 and 5 */
1559 		} else {
1560 			if (iface == 5)
1561 				return 1; /* disable digialt input */
1562 			if (altno != 2 && altno != 5)
1563 				return 1; /* enalbe only altsets 2 and 5 */
1564 		}
1565 	} else {
1566 		/* keep only 16-Bit mode */
1567 		if (altno != 1)
1568 			return 1;
1569 	}
1570 
1571 	usb_audio_dbg(chip,
1572 		    "using altsetting %d for interface %d config %d\n",
1573 		    altno, iface, chip->setup);
1574 	return 0; /* keep this altsetting */
1575 }
1576 
s1810c_skip_setting_quirk(struct snd_usb_audio * chip,int iface,int altno)1577 static int s1810c_skip_setting_quirk(struct snd_usb_audio *chip,
1578 					   int iface, int altno)
1579 {
1580 	/*
1581 	 * Altno settings:
1582 	 *
1583 	 * Playback (Interface 1):
1584 	 * 1: 6 Analog + 2 S/PDIF
1585 	 * 2: 6 Analog + 2 S/PDIF
1586 	 * 3: 6 Analog
1587 	 *
1588 	 * Capture (Interface 2):
1589 	 * 1: 8 Analog + 2 S/PDIF + 8 ADAT
1590 	 * 2: 8 Analog + 2 S/PDIF + 4 ADAT
1591 	 * 3: 8 Analog
1592 	 */
1593 
1594 	/*
1595 	 * I'll leave 2 as the default one and
1596 	 * use device_setup to switch to the
1597 	 * other two.
1598 	 */
1599 	if ((chip->setup == 0 || chip->setup > 2) && altno != 2)
1600 		return 1;
1601 	else if (chip->setup == 1 && altno != 1)
1602 		return 1;
1603 	else if (chip->setup == 2 && altno != 3)
1604 		return 1;
1605 
1606 	return 0;
1607 }
1608 
snd_usb_apply_interface_quirk(struct snd_usb_audio * chip,int iface,int altno)1609 int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
1610 				  int iface,
1611 				  int altno)
1612 {
1613 	/* audiophile usb: skip altsets incompatible with device_setup */
1614 	if (chip->usb_id == USB_ID(0x0763, 0x2003))
1615 		return audiophile_skip_setting_quirk(chip, iface, altno);
1616 	/* quattro usb: skip altsets incompatible with device_setup */
1617 	if (chip->usb_id == USB_ID(0x0763, 0x2001))
1618 		return quattro_skip_setting_quirk(chip, iface, altno);
1619 	/* fasttrackpro usb: skip altsets incompatible with device_setup */
1620 	if (chip->usb_id == USB_ID(0x0763, 0x2012))
1621 		return fasttrackpro_skip_setting_quirk(chip, iface, altno);
1622 	/* presonus studio 1810c: skip altsets incompatible with device_setup */
1623 	if (chip->usb_id == USB_ID(0x194f, 0x010c))
1624 		return s1810c_skip_setting_quirk(chip, iface, altno);
1625 
1626 	return 0;
1627 }
1628 
snd_usb_apply_boot_quirk(struct usb_device * dev,struct usb_interface * intf,const struct snd_usb_audio_quirk * quirk,unsigned int id)1629 int snd_usb_apply_boot_quirk(struct usb_device *dev,
1630 			     struct usb_interface *intf,
1631 			     const struct snd_usb_audio_quirk *quirk,
1632 			     unsigned int id)
1633 {
1634 	switch (id) {
1635 	case USB_ID(0x041e, 0x3000):
1636 		/* SB Extigy needs special boot-up sequence */
1637 		/* if more models come, this will go to the quirk list. */
1638 		return snd_usb_extigy_boot_quirk(dev, intf);
1639 
1640 	case USB_ID(0x041e, 0x3020):
1641 		/* SB Audigy 2 NX needs its own boot-up magic, too */
1642 		return snd_usb_audigy2nx_boot_quirk(dev);
1643 
1644 	case USB_ID(0x10f5, 0x0200):
1645 		/* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
1646 		return snd_usb_cm106_boot_quirk(dev);
1647 
1648 	case USB_ID(0x0d8c, 0x0102):
1649 		/* C-Media CM6206 / CM106-Like Sound Device */
1650 	case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
1651 		return snd_usb_cm6206_boot_quirk(dev);
1652 
1653 	case USB_ID(0x0dba, 0x3000):
1654 		/* Digidesign Mbox 2 */
1655 		return snd_usb_mbox2_boot_quirk(dev);
1656 	case USB_ID(0x0dba, 0x5000):
1657 		/* Digidesign Mbox 3 */
1658 		return snd_usb_mbox3_boot_quirk(dev);
1659 
1660 
1661 	case USB_ID(0x1235, 0x0010): /* Focusrite Novation Saffire 6 USB */
1662 	case USB_ID(0x1235, 0x0018): /* Focusrite Novation Twitch */
1663 		return snd_usb_novation_boot_quirk(dev);
1664 
1665 	case USB_ID(0x133e, 0x0815):
1666 		/* Access Music VirusTI Desktop */
1667 		return snd_usb_accessmusic_boot_quirk(dev);
1668 
1669 	case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
1670 	case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
1671 	case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
1672 		return snd_usb_nativeinstruments_boot_quirk(dev);
1673 	case USB_ID(0x0763, 0x2012):  /* M-Audio Fast Track Pro USB */
1674 		return snd_usb_fasttrackpro_boot_quirk(dev);
1675 	case USB_ID(0x047f, 0xc010): /* Plantronics Gamecom 780 */
1676 		return snd_usb_gamecon780_boot_quirk(dev);
1677 	case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx 3 */
1678 		return snd_usb_axefx3_boot_quirk(dev);
1679 	case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II */
1680 		/*
1681 		 * For some reason interface 3 with vendor-spec class is
1682 		 * detected on MicroBook IIc.
1683 		 */
1684 		if (get_iface_desc(intf->altsetting)->bInterfaceClass ==
1685 		    USB_CLASS_VENDOR_SPEC &&
1686 		    get_iface_desc(intf->altsetting)->bInterfaceNumber < 3)
1687 			return snd_usb_motu_microbookii_boot_quirk(dev);
1688 		break;
1689 	case USB_ID(0x2a39, 0x3f8c): /* RME Digiface USB */
1690 	case USB_ID(0x2a39, 0x3fa0): /* RME Digiface USB (alternate) */
1691 		return snd_usb_rme_digiface_boot_quirk(dev);
1692 	}
1693 
1694 	return 0;
1695 }
1696 
snd_usb_apply_boot_quirk_once(struct usb_device * dev,struct usb_interface * intf,const struct snd_usb_audio_quirk * quirk,unsigned int id)1697 int snd_usb_apply_boot_quirk_once(struct usb_device *dev,
1698 				  struct usb_interface *intf,
1699 				  const struct snd_usb_audio_quirk *quirk,
1700 				  unsigned int id)
1701 {
1702 	switch (id) {
1703 	case USB_ID(0x07fd, 0x0008): /* MOTU M Series, 1st hardware version */
1704 		return snd_usb_motu_m_series_boot_quirk(dev);
1705 	case USB_ID(0x1397, 0x1234): /* Behringer CM1A */
1706 		return snd_usb_cm1a_boot_quirk(dev);
1707 	}
1708 
1709 	return 0;
1710 }
1711 
1712 /*
1713  * check if the device uses big-endian samples
1714  */
snd_usb_is_big_endian_format(struct snd_usb_audio * chip,const struct audioformat * fp)1715 int snd_usb_is_big_endian_format(struct snd_usb_audio *chip,
1716 				 const struct audioformat *fp)
1717 {
1718 	/* it depends on altsetting whether the device is big-endian or not */
1719 	switch (chip->usb_id) {
1720 	case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
1721 		if (fp->altsetting == 2 || fp->altsetting == 3 ||
1722 			fp->altsetting == 5 || fp->altsetting == 6)
1723 			return 1;
1724 		break;
1725 	case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
1726 		if (chip->setup == 0x00 ||
1727 			fp->altsetting == 1 || fp->altsetting == 2 ||
1728 			fp->altsetting == 3)
1729 			return 1;
1730 		break;
1731 	case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
1732 		if (fp->altsetting == 2 || fp->altsetting == 3 ||
1733 			fp->altsetting == 5 || fp->altsetting == 6)
1734 			return 1;
1735 		break;
1736 	}
1737 	return 0;
1738 }
1739 
1740 /*
1741  * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
1742  * not for interface.
1743  */
1744 
1745 enum {
1746 	EMU_QUIRK_SR_44100HZ = 0,
1747 	EMU_QUIRK_SR_48000HZ,
1748 	EMU_QUIRK_SR_88200HZ,
1749 	EMU_QUIRK_SR_96000HZ,
1750 	EMU_QUIRK_SR_176400HZ,
1751 	EMU_QUIRK_SR_192000HZ
1752 };
1753 
set_format_emu_quirk(struct snd_usb_substream * subs,const struct audioformat * fmt)1754 static void set_format_emu_quirk(struct snd_usb_substream *subs,
1755 				 const struct audioformat *fmt)
1756 {
1757 	unsigned char emu_samplerate_id = 0;
1758 
1759 	/* When capture is active
1760 	 * sample rate shouldn't be changed
1761 	 * by playback substream
1762 	 */
1763 	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1764 		if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].cur_audiofmt)
1765 			return;
1766 	}
1767 
1768 	switch (fmt->rate_min) {
1769 	case 48000:
1770 		emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
1771 		break;
1772 	case 88200:
1773 		emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
1774 		break;
1775 	case 96000:
1776 		emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
1777 		break;
1778 	case 176400:
1779 		emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
1780 		break;
1781 	case 192000:
1782 		emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
1783 		break;
1784 	default:
1785 		emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
1786 		break;
1787 	}
1788 	snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
1789 	subs->pkt_offset_adj = (emu_samplerate_id >= EMU_QUIRK_SR_176400HZ) ? 4 : 0;
1790 }
1791 
pioneer_djm_set_format_quirk(struct snd_usb_substream * subs,u16 windex)1792 static int pioneer_djm_set_format_quirk(struct snd_usb_substream *subs,
1793 					u16 windex)
1794 {
1795 	unsigned int cur_rate = subs->data_endpoint->cur_rate;
1796 	u8 sr[3];
1797 	// Convert to little endian
1798 	sr[0] = cur_rate & 0xff;
1799 	sr[1] = (cur_rate >> 8) & 0xff;
1800 	sr[2] = (cur_rate >> 16) & 0xff;
1801 	usb_set_interface(subs->dev, 0, 1);
1802 	// we should derive windex from fmt-sync_ep but it's not set
1803 	snd_usb_ctl_msg(subs->stream->chip->dev,
1804 		usb_sndctrlpipe(subs->stream->chip->dev, 0),
1805 		0x01, 0x22, 0x0100, windex, &sr, 0x0003);
1806 	return 0;
1807 }
1808 
mbox3_set_format_quirk(struct snd_usb_substream * subs,const struct audioformat * fmt)1809 static void mbox3_set_format_quirk(struct snd_usb_substream *subs,
1810 				const struct audioformat *fmt)
1811 {
1812 	__le32 buff4 = 0;
1813 	u8 buff1 = 0x01;
1814 	u32 new_rate = subs->data_endpoint->cur_rate;
1815 	u32 current_rate;
1816 
1817 	// Get current rate from card and check if changing it is needed
1818 	snd_usb_ctl_msg(subs->dev, usb_rcvctrlpipe(subs->dev, 0),
1819 					0x01, 0x21 | USB_DIR_IN, 0x0100, 0x8101, &buff4, 4);
1820 	current_rate = le32_to_cpu(buff4);
1821 	dev_dbg(&subs->dev->dev,
1822 			 "MBOX3: Current configured sample rate: %d", current_rate);
1823 	if (current_rate == new_rate) {
1824 		dev_dbg(&subs->dev->dev,
1825 			"MBOX3: No change needed (current rate:%d == new rate:%d)",
1826 			current_rate, new_rate);
1827 		return;
1828 	}
1829 
1830 	// Set new rate
1831 	dev_info(&subs->dev->dev,
1832 			 "MBOX3: Changing sample rate to: %d", new_rate);
1833 	buff4 = cpu_to_le32(new_rate);
1834 	snd_usb_ctl_msg(subs->dev, usb_sndctrlpipe(subs->dev, 0),
1835 					0x01, 0x21, 0x0100, 0x8101, &buff4, 4);
1836 
1837 	// Set clock source to Internal
1838 	snd_usb_ctl_msg(subs->dev, usb_sndctrlpipe(subs->dev, 0),
1839 					0x01, 0x21, 0x0100, 0x8001, &buff1, 1);
1840 
1841 	// Check whether the change was successful
1842 	buff4 = 0;
1843 	snd_usb_ctl_msg(subs->dev, usb_rcvctrlpipe(subs->dev, 0),
1844 					0x01, 0x21 | USB_DIR_IN, 0x0100, 0x8101, &buff4, 4);
1845 	if (new_rate != le32_to_cpu(buff4))
1846 		dev_warn(&subs->dev->dev, "MBOX3: Couldn't set the sample rate");
1847 }
1848 
1849 static const int rme_digiface_rate_table[] = {
1850 	32000, 44100, 48000, 0,
1851 	64000, 88200, 96000, 0,
1852 	128000, 176400, 192000, 0,
1853 };
1854 
rme_digiface_set_format_quirk(struct snd_usb_substream * subs)1855 static int rme_digiface_set_format_quirk(struct snd_usb_substream *subs)
1856 {
1857 	unsigned int cur_rate = subs->data_endpoint->cur_rate;
1858 	u16 val;
1859 	int speed_mode;
1860 	int id;
1861 
1862 	for (id = 0; id < ARRAY_SIZE(rme_digiface_rate_table); id++) {
1863 		if (rme_digiface_rate_table[id] == cur_rate)
1864 			break;
1865 	}
1866 
1867 	if (id >= ARRAY_SIZE(rme_digiface_rate_table))
1868 		return -EINVAL;
1869 
1870 	/* 2, 3, 4 for 1x, 2x, 4x */
1871 	speed_mode = (id >> 2) + 2;
1872 	val = (id << 3) | (speed_mode << 12);
1873 
1874 	/* Set the sample rate */
1875 	snd_usb_ctl_msg(subs->stream->chip->dev,
1876 		usb_sndctrlpipe(subs->stream->chip->dev, 0),
1877 		16, 0x40, val, 0x7078, NULL, 0);
1878 	return 0;
1879 }
1880 
snd_usb_set_format_quirk(struct snd_usb_substream * subs,const struct audioformat * fmt)1881 void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
1882 			      const struct audioformat *fmt)
1883 {
1884 	switch (subs->stream->chip->usb_id) {
1885 	case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
1886 	case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
1887 	case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
1888 	case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
1889 		set_format_emu_quirk(subs, fmt);
1890 		break;
1891 	case USB_ID(0x534d, 0x0021): /* MacroSilicon MS2100/MS2106 */
1892 	case USB_ID(0x534d, 0x2109): /* MacroSilicon MS2109 */
1893 		subs->stream_offset_adj = 2;
1894 		break;
1895 	case USB_ID(0x2b73, 0x000a): /* Pioneer DJM-900NXS2 */
1896 	case USB_ID(0x2b73, 0x0013): /* Pioneer DJM-450 */
1897 	case USB_ID(0x2b73, 0x0034): /* Pioneer DJM-V10 */
1898 		pioneer_djm_set_format_quirk(subs, 0x0082);
1899 		break;
1900 	case USB_ID(0x08e4, 0x017f): /* Pioneer DJM-750 */
1901 	case USB_ID(0x08e4, 0x0163): /* Pioneer DJM-850 */
1902 		pioneer_djm_set_format_quirk(subs, 0x0086);
1903 		break;
1904 	case USB_ID(0x0dba, 0x5000):
1905 		mbox3_set_format_quirk(subs, fmt); /* Digidesign Mbox 3 */
1906 		break;
1907 	case USB_ID(0x2a39, 0x3f8c): /* RME Digiface USB */
1908 	case USB_ID(0x2a39, 0x3fa0): /* RME Digiface USB (alternate) */
1909 		rme_digiface_set_format_quirk(subs);
1910 		break;
1911 	}
1912 }
1913 
snd_usb_select_mode_quirk(struct snd_usb_audio * chip,const struct audioformat * fmt)1914 int snd_usb_select_mode_quirk(struct snd_usb_audio *chip,
1915 			      const struct audioformat *fmt)
1916 {
1917 	struct usb_device *dev = chip->dev;
1918 	int err;
1919 
1920 	if (chip->quirk_flags & QUIRK_FLAG_ITF_USB_DSD_DAC) {
1921 		/* First switch to alt set 0, otherwise the mode switch cmd
1922 		 * will not be accepted by the DAC
1923 		 */
1924 		err = usb_set_interface(dev, fmt->iface, 0);
1925 		if (err < 0)
1926 			return err;
1927 
1928 		msleep(20); /* Delay needed after setting the interface */
1929 
1930 		/* Vendor mode switch cmd is required. */
1931 		if (fmt->formats & SNDRV_PCM_FMTBIT_DSD_U32_BE) {
1932 			/* DSD mode (DSD_U32) requested */
1933 			err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1934 					      USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1935 					      1, 1, NULL, 0);
1936 			if (err < 0)
1937 				return err;
1938 
1939 		} else {
1940 			/* PCM or DOP mode (S32) requested */
1941 			/* PCM mode (S16) requested */
1942 			err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1943 					      USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1944 					      0, 1, NULL, 0);
1945 			if (err < 0)
1946 				return err;
1947 
1948 		}
1949 		msleep(20);
1950 	}
1951 	return 0;
1952 }
1953 
snd_usb_endpoint_start_quirk(struct snd_usb_endpoint * ep)1954 void snd_usb_endpoint_start_quirk(struct snd_usb_endpoint *ep)
1955 {
1956 	/*
1957 	 * "Playback Design" products send bogus feedback data at the start
1958 	 * of the stream. Ignore them.
1959 	 */
1960 	if (USB_ID_VENDOR(ep->chip->usb_id) == 0x23ba &&
1961 	    ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
1962 		ep->skip_packets = 4;
1963 
1964 	/*
1965 	 * M-Audio Fast Track C400/C600 - when packets are not skipped, real
1966 	 * world latency varies by approx. +/- 50 frames (at 96kHz) each time
1967 	 * the stream is (re)started. When skipping packets 16 at endpoint
1968 	 * start up, the real world latency is stable within +/- 1 frame (also
1969 	 * across power cycles).
1970 	 */
1971 	if ((ep->chip->usb_id == USB_ID(0x0763, 0x2030) ||
1972 	     ep->chip->usb_id == USB_ID(0x0763, 0x2031)) &&
1973 	    ep->type == SND_USB_ENDPOINT_TYPE_DATA)
1974 		ep->skip_packets = 16;
1975 
1976 	/* Work around devices that report unreasonable feedback data */
1977 	if ((ep->chip->usb_id == USB_ID(0x0644, 0x8038) ||  /* TEAC UD-H01 */
1978 	     ep->chip->usb_id == USB_ID(0x1852, 0x5034)) && /* T+A Dac8 */
1979 	    ep->syncmaxsize == 4)
1980 		ep->tenor_fb_quirk = 1;
1981 }
1982 
1983 /* quirk applied after snd_usb_ctl_msg(); not applied during boot quirks */
snd_usb_ctl_msg_quirk(struct usb_device * dev,unsigned int pipe,__u8 request,__u8 requesttype,__u16 value,__u16 index,void * data,__u16 size)1984 void snd_usb_ctl_msg_quirk(struct usb_device *dev, unsigned int pipe,
1985 			   __u8 request, __u8 requesttype, __u16 value,
1986 			   __u16 index, void *data, __u16 size)
1987 {
1988 	struct snd_usb_audio *chip = dev_get_drvdata(&dev->dev);
1989 
1990 	if (!chip || (requesttype & USB_TYPE_MASK) != USB_TYPE_CLASS)
1991 		return;
1992 
1993 	if (chip->quirk_flags & QUIRK_FLAG_CTL_MSG_DELAY)
1994 		msleep(20);
1995 	else if (chip->quirk_flags & QUIRK_FLAG_CTL_MSG_DELAY_1M)
1996 		usleep_range(1000, 2000);
1997 	else if (chip->quirk_flags & QUIRK_FLAG_CTL_MSG_DELAY_5M)
1998 		usleep_range(5000, 6000);
1999 }
2000 
2001 /*
2002  * snd_usb_interface_dsd_format_quirks() is called from format.c to
2003  * augment the PCM format bit-field for DSD types. The UAC standards
2004  * don't have a designated bit field to denote DSD-capable interfaces,
2005  * hence all hardware that is known to support this format has to be
2006  * listed here.
2007  */
snd_usb_interface_dsd_format_quirks(struct snd_usb_audio * chip,struct audioformat * fp,unsigned int sample_bytes)2008 u64 snd_usb_interface_dsd_format_quirks(struct snd_usb_audio *chip,
2009 					struct audioformat *fp,
2010 					unsigned int sample_bytes)
2011 {
2012 	struct usb_interface *iface;
2013 
2014 	/* Playback Designs */
2015 	if (USB_ID_VENDOR(chip->usb_id) == 0x23ba &&
2016 	    USB_ID_PRODUCT(chip->usb_id) < 0x0110) {
2017 		switch (fp->altsetting) {
2018 		case 1:
2019 			fp->dsd_dop = true;
2020 			return SNDRV_PCM_FMTBIT_DSD_U16_LE;
2021 		case 2:
2022 			fp->dsd_bitrev = true;
2023 			return SNDRV_PCM_FMTBIT_DSD_U8;
2024 		case 3:
2025 			fp->dsd_bitrev = true;
2026 			return SNDRV_PCM_FMTBIT_DSD_U16_LE;
2027 		}
2028 	}
2029 
2030 	/* XMOS based USB DACs */
2031 	switch (chip->usb_id) {
2032 	case USB_ID(0x139f, 0x5504): /* Nagra DAC */
2033 	case USB_ID(0x20b1, 0x3089): /* Mola-Mola DAC */
2034 	case USB_ID(0x2522, 0x0007): /* LH Labs Geek Out 1V5 */
2035 	case USB_ID(0x2522, 0x0009): /* LH Labs Geek Pulse X Inifinity 2V0 */
2036 	case USB_ID(0x2522, 0x0012): /* LH Labs VI DAC Infinity */
2037 	case USB_ID(0x2772, 0x0230): /* Pro-Ject Pre Box S2 Digital */
2038 		if (fp->altsetting == 2)
2039 			return SNDRV_PCM_FMTBIT_DSD_U32_BE;
2040 		break;
2041 
2042 	case USB_ID(0x0d8c, 0x0316): /* Hegel HD12 DSD */
2043 	case USB_ID(0x10cb, 0x0103): /* The Bit Opus #3; with fp->dsd_raw */
2044 	case USB_ID(0x16d0, 0x06b2): /* NuPrime DAC-10 */
2045 	case USB_ID(0x16d0, 0x06b4): /* NuPrime Audio HD-AVP/AVA */
2046 	case USB_ID(0x16d0, 0x0733): /* Furutech ADL Stratos */
2047 	case USB_ID(0x16d0, 0x09d8): /* NuPrime IDA-8 */
2048 	case USB_ID(0x16d0, 0x09db): /* NuPrime Audio DAC-9 */
2049 	case USB_ID(0x16d0, 0x09dd): /* Encore mDSD */
2050 	case USB_ID(0x16d0, 0x0ab1): /* PureAudio APA DAC */
2051 	case USB_ID(0x16d0, 0xeca1): /* PureAudio Lotus DAC5, DAC5 SE, DAC5 Pro */
2052 	case USB_ID(0x1db5, 0x0003): /* Bryston BDA3 */
2053 	case USB_ID(0x20a0, 0x4143): /* WaveIO USB Audio 2.0 */
2054 	case USB_ID(0x22e1, 0xca01): /* HDTA Serenade DSD */
2055 	case USB_ID(0x249c, 0x9326): /* M2Tech Young MkIII */
2056 	case USB_ID(0x2616, 0x0106): /* PS Audio NuWave DAC */
2057 	case USB_ID(0x2622, 0x0041): /* Audiolab M-DAC+ */
2058 	case USB_ID(0x2622, 0x0061): /* LEAK Stereo 230 */
2059 	case USB_ID(0x278b, 0x5100): /* Rotel RC-1590 */
2060 	case USB_ID(0x27f7, 0x3002): /* W4S DAC-2v2SE */
2061 	case USB_ID(0x29a2, 0x0086): /* Mutec MC3+ USB */
2062 	case USB_ID(0x6b42, 0x0042): /* MSB Technology */
2063 		if (fp->altsetting == 3)
2064 			return SNDRV_PCM_FMTBIT_DSD_U32_BE;
2065 		break;
2066 
2067 	/* Amanero Combo384 USB based DACs with native DSD support */
2068 	case USB_ID(0x16d0, 0x071a):  /* Amanero - Combo384 */
2069 		if (fp->altsetting == 2) {
2070 			switch (le16_to_cpu(chip->dev->descriptor.bcdDevice)) {
2071 			case 0x199:
2072 				return SNDRV_PCM_FMTBIT_DSD_U32_LE;
2073 			case 0x19b:
2074 			case 0x203:
2075 				return SNDRV_PCM_FMTBIT_DSD_U32_BE;
2076 			default:
2077 				break;
2078 			}
2079 		}
2080 		break;
2081 	case USB_ID(0x16d0, 0x0a23):
2082 		if (fp->altsetting == 2)
2083 			return SNDRV_PCM_FMTBIT_DSD_U32_BE;
2084 		break;
2085 
2086 	default:
2087 		break;
2088 	}
2089 
2090 	/* ITF-USB DSD based DACs */
2091 	if (chip->quirk_flags & QUIRK_FLAG_ITF_USB_DSD_DAC) {
2092 		iface = usb_ifnum_to_if(chip->dev, fp->iface);
2093 
2094 		/* Altsetting 2 support native DSD if the num of altsets is
2095 		 * three (0-2),
2096 		 * Altsetting 3 support native DSD if the num of altsets is
2097 		 * four (0-3).
2098 		 */
2099 		if (fp->altsetting == iface->num_altsetting - 1)
2100 			return SNDRV_PCM_FMTBIT_DSD_U32_BE;
2101 	}
2102 
2103 	/* Mostly generic method to detect many DSD-capable implementations */
2104 	if ((chip->quirk_flags & QUIRK_FLAG_DSD_RAW) && fp->dsd_raw)
2105 		return SNDRV_PCM_FMTBIT_DSD_U32_BE;
2106 
2107 	return 0;
2108 }
2109 
snd_usb_audioformat_attributes_quirk(struct snd_usb_audio * chip,struct audioformat * fp,int stream)2110 void snd_usb_audioformat_attributes_quirk(struct snd_usb_audio *chip,
2111 					  struct audioformat *fp,
2112 					  int stream)
2113 {
2114 	switch (chip->usb_id) {
2115 	case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
2116 		/* Optoplay sets the sample rate attribute although
2117 		 * it seems not supporting it in fact.
2118 		 */
2119 		fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
2120 		break;
2121 	case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
2122 	case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
2123 		/* doesn't set the sample rate attribute, but supports it */
2124 		fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
2125 		break;
2126 	case USB_ID(0x0763, 0x2001):  /* M-Audio Quattro USB */
2127 	case USB_ID(0x0763, 0x2012):  /* M-Audio Fast Track Pro USB */
2128 	case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
2129 	case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
2130 					an older model 77d:223) */
2131 	/*
2132 	 * plantronics headset and Griffin iMic have set adaptive-in
2133 	 * although it's really not...
2134 	 */
2135 		fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
2136 		if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2137 			fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
2138 		else
2139 			fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
2140 		break;
2141 	case USB_ID(0x07fd, 0x0004):  /* MOTU MicroBook IIc */
2142 		/*
2143 		 * MaxPacketsOnly attribute is erroneously set in endpoint
2144 		 * descriptors. As a result this card produces noise with
2145 		 * all sample rates other than 96 kHz.
2146 		 */
2147 		fp->attributes &= ~UAC_EP_CS_ATTR_FILL_MAX;
2148 		break;
2149 	case USB_ID(0x1224, 0x2a25):  /* Jieli Technology USB PHY 2.0 */
2150 		/* mic works only when ep packet size is set to wMaxPacketSize */
2151 		fp->attributes |= UAC_EP_CS_ATTR_FILL_MAX;
2152 		break;
2153 	case USB_ID(0x3511, 0x2b1e): /* Opencomm2 UC USB Bluetooth dongle */
2154 		/* mic works only when ep pitch control is not set */
2155 		if (stream == SNDRV_PCM_STREAM_CAPTURE)
2156 			fp->attributes &= ~UAC_EP_CS_ATTR_PITCH_CONTROL;
2157 		break;
2158 	}
2159 }
2160 
2161 /*
2162  * driver behavior quirk flags
2163  */
2164 struct usb_string_match {
2165 	const char *manufacturer;
2166 	const char *product;
2167 };
2168 
2169 struct usb_audio_quirk_flags_table {
2170 	u32 id;
2171 	u64 flags;
2172 	const struct usb_string_match *usb_string_match;
2173 };
2174 
2175 #define DEVICE_FLG(vid, pid, _flags) \
2176 	{ .id = USB_ID(vid, pid), .flags = (_flags) }
2177 #define VENDOR_FLG(vid, _flags) DEVICE_FLG(vid, 0, _flags)
2178 
2179 /*
2180  * Use as a last resort if using DEVICE_FLG() is prone to VID/PID conflicts.
2181  *
2182  * Usage:
2183  *   // match vid, pid, "manufacturer", and "product"
2184  *   DEVICE_STRING_FLG(vid, pid, "manufacturer", "product", flags)
2185  *
2186  *   // match vid, pid, "manufacturer", and any product string
2187  *   DEVICE_STRING_FLG(vid, pid, "manufacturer", NULL,      flags)
2188  *
2189  *   // match vid, pid, "manufacturer", and device must have no product string
2190  *   DEVICE_STRING_FLG(vid, pid, "manufacturer", "",        flags)
2191  *
2192  *   // match vid, pid, any manufacturer string, and "product"
2193  *   DEVICE_STRING_FLG(vid, pid, NULL,           "product", flags)
2194  *
2195  *   // match vid, pid, no manufacturer string, and "product"
2196  *   DEVICE_STRING_FLG(vid, pid, "",             "product", flags)
2197  *
2198  *   // match vid, pid, no manufacturer string, and no product string
2199  *   DEVICE_STRING_FLG(vid, pid, "",             "",        flags)
2200  */
2201 #define DEVICE_STRING_FLG(vid, pid, _manufacturer, _product, _flags)	\
2202 {									\
2203 	.id = USB_ID(vid, pid),						\
2204 	.usb_string_match = &(const struct usb_string_match) {		\
2205 		.manufacturer = _manufacturer,				\
2206 		.product = _product,					\
2207 	},								\
2208 	.flags = (_flags),						\
2209 }
2210 
2211 /*
2212  * Use as a last resort if using VENDOR_FLG() is prone to VID conflicts.
2213  *
2214  * Usage:
2215  *   // match vid, and "manufacturer"
2216  *   VENDOR_STRING_FLG(vid, "manufacturer", flags)
2217  *
2218  *   // match vid, and device must have no manufacturer string
2219  *   VENDOR_STRING_FLG(vid, "",             flags)
2220  */
2221 #define VENDOR_STRING_FLG(vid, _manufacturer, _flags)			\
2222 	DEVICE_STRING_FLG(vid, 0, _manufacturer, NULL, _flags)
2223 
2224 static const struct usb_audio_quirk_flags_table quirk_flags_table[] = {
2225 	/* Device and string descriptor matches */
2226 	DEVICE_STRING_FLG(0x05ac, 0x110b, /* VID conflicts with Apple */
2227 			  "JKY Technology", NULL, /* Q2A */
2228 			  QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY),
2229 
2230 	/* Device matches */
2231 	DEVICE_FLG(0x001f, 0x0b21, /* AB13X USB Audio */
2232 		   QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY),
2233 	DEVICE_FLG(0x001f, 0x0b23, /* AB17X USB Audio */
2234 		   QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY),
2235 	DEVICE_FLG(0x0020, 0x0b21, /* GHW-123P */
2236 		   QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY),
2237 	DEVICE_FLG(0x0124, 0x0c21, /* Generic USB Headphone */
2238 		   QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY),
2239 	DEVICE_FLG(0x03f0, 0x654a, /* HP 320 FHD Webcam */
2240 		   QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_MIC_RES_16),
2241 	DEVICE_FLG(0x041e, 0x3000, /* Creative SB Extigy */
2242 		   QUIRK_FLAG_IGNORE_CTL_ERROR),
2243 	DEVICE_FLG(0x041e, 0x324d, /* Creative Sound Blaster Play! 3 */
2244 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2245 	DEVICE_FLG(0x041e, 0x4080, /* Creative Live Cam VF0610 */
2246 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2247 	DEVICE_FLG(0x045e, 0x083c, /* MS USB Link headset */
2248 		   QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_CTL_MSG_DELAY |
2249 		   QUIRK_FLAG_DISABLE_AUTOSUSPEND),
2250 	DEVICE_FLG(0x045e, 0x070f, /* MS LifeChat LX-3000 Headset */
2251 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2252 	DEVICE_FLG(0x046d, 0x0807, /* Logitech Webcam C500 */
2253 		   QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_MIC_RES_384),
2254 	DEVICE_FLG(0x046d, 0x0808, /* Logitech Webcam C600 */
2255 		   QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_MIC_RES_384),
2256 	DEVICE_FLG(0x046d, 0x0809,
2257 		   QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_MIC_RES_384),
2258 	DEVICE_FLG(0x046d, 0x0819, /* Logitech Webcam C210 */
2259 		   QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_MIC_RES_384),
2260 	DEVICE_FLG(0x046d, 0x081b, /* HD Webcam c310 */
2261 		   QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_MIC_RES_384),
2262 	DEVICE_FLG(0x046d, 0x081d, /* HD Webcam c510 */
2263 		   QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_MIC_RES_384),
2264 	DEVICE_FLG(0x046d, 0x0825, /* HD Webcam c270 */
2265 		   QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_MIC_RES_384),
2266 	DEVICE_FLG(0x046d, 0x0826, /* HD Webcam c525 */
2267 		   QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_MIC_RES_384),
2268 	DEVICE_FLG(0x046d, 0x084c, /* Logitech ConferenceCam Connect */
2269 		   QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_CTL_MSG_DELAY_1M),
2270 	DEVICE_FLG(0x046d, 0x08ca, /* Logitech Quickcam Fusion */
2271 		   QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_MIC_RES_384),
2272 	DEVICE_FLG(0x046d, 0x0991, /* Logitech QuickCam Pro */
2273 		   QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_IGNORE_CTL_ERROR |
2274 		   QUIRK_FLAG_MIC_RES_384),
2275 	DEVICE_FLG(0x046d, 0x09a2, /* QuickCam Communicate Deluxe/S7500 */
2276 		   QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_MIC_RES_384),
2277 	DEVICE_FLG(0x046d, 0x09a4, /* Logitech QuickCam E 3500 */
2278 		   QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_IGNORE_CTL_ERROR),
2279 	DEVICE_FLG(0x046d, 0x0a8f, /* Logitech H390 headset */
2280 		   QUIRK_FLAG_CTL_MSG_DELAY_1M |
2281 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2282 	DEVICE_FLG(0x046d, 0x0aba, /* Logitech PRO X Wireless */
2283 		   QUIRK_FLAG_MIXER_GET_CUR_OK |
2284 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2285 	DEVICE_FLG(0x0499, 0x1506, /* Yamaha THR5 */
2286 		   QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2287 	DEVICE_FLG(0x0499, 0x1509, /* Steinberg UR22 */
2288 		   QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2289 	DEVICE_FLG(0x0499, 0x3108, /* Yamaha YIT-W12TX */
2290 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2291 	DEVICE_FLG(0x04d8, 0xfeea, /* Benchmark DAC1 Pre */
2292 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2293 	DEVICE_FLG(0x04e8, 0xa051, /* Samsung USBC Headset (AKG) */
2294 		   QUIRK_FLAG_SKIP_CLOCK_SELECTOR | QUIRK_FLAG_CTL_MSG_DELAY_5M),
2295 	DEVICE_FLG(0x0525, 0xa4ad, /* Hamedal C20 usb camero */
2296 		   QUIRK_FLAG_IFACE_SKIP_CLOSE),
2297 	DEVICE_FLG(0x054c, 0x0b8c, /* Sony WALKMAN NW-A45 DAC */
2298 		   QUIRK_FLAG_SET_IFACE_FIRST),
2299 	DEVICE_FLG(0x0556, 0x0014, /* Phoenix Audio TMX320VC */
2300 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2301 	DEVICE_FLG(0x0572, 0x1b08, /* Conexant Systems (Rockwell), Inc. */
2302 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2303 	DEVICE_FLG(0x0572, 0x1b09, /* Conexant Systems (Rockwell), Inc. */
2304 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2305 	DEVICE_FLG(0x05a3, 0x9420, /* ELP HD USB Camera */
2306 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2307 	DEVICE_FLG(0x05a7, 0x1020, /* Bose Companion 5 */
2308 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2309 	DEVICE_FLG(0x05e1, 0x0408, /* Syntek STK1160 */
2310 		   QUIRK_FLAG_ALIGN_TRANSFER),
2311 	DEVICE_FLG(0x05e1, 0x0480, /* Hauppauge Woodbury */
2312 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2313 	DEVICE_FLG(0x05fc, 0x0231, /* JBL Pebbles */
2314 		   QUIRK_FLAG_MIXER_PLAYBACK_LINEAR_VOL | QUIRK_FLAG_MIXER_CAPTURE_LINEAR_VOL |
2315 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2316 	DEVICE_FLG(0x0624, 0x3d3f, /* AB13X USB Audio */
2317 		   QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY),
2318 	DEVICE_FLG(0x0644, 0x8043, /* TEAC UD-501/UD-501V2/UD-503/NT-503 */
2319 		   QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY |
2320 		   QUIRK_FLAG_IFACE_DELAY),
2321 	DEVICE_FLG(0x0644, 0x8044, /* Esoteric D-05X */
2322 		   QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY |
2323 		   QUIRK_FLAG_IFACE_DELAY | QUIRK_FLAG_FORCE_IFACE_RESET),
2324 	DEVICE_FLG(0x0644, 0x804a, /* TEAC UD-301 */
2325 		   QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY |
2326 		   QUIRK_FLAG_IFACE_DELAY),
2327 	DEVICE_FLG(0x0644, 0x805f, /* TEAC Model 12 */
2328 		   QUIRK_FLAG_FORCE_IFACE_RESET),
2329 	DEVICE_FLG(0x0644, 0x806b, /* TEAC UD-701 */
2330 		   QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY |
2331 		   QUIRK_FLAG_IFACE_DELAY | QUIRK_FLAG_FORCE_IFACE_RESET),
2332 	DEVICE_FLG(0x0644, 0x807d, /* TEAC UD-507 */
2333 		   QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY |
2334 		   QUIRK_FLAG_IFACE_DELAY | QUIRK_FLAG_FORCE_IFACE_RESET),
2335 	DEVICE_FLG(0x0644, 0x806c, /* Esoteric XD */
2336 		   QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY |
2337 		   QUIRK_FLAG_IFACE_DELAY | QUIRK_FLAG_FORCE_IFACE_RESET),
2338 	DEVICE_FLG(0x0661, 0x0883, /* iBasso DC04 Ultra */
2339 		   QUIRK_FLAG_DSD_RAW),
2340 	DEVICE_FLG(0x0666, 0x0880, /* SPACETOUCH USB Audio */
2341 		   QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY |
2342 		   QUIRK_FLAG_CTL_MSG_DELAY_5M),
2343 	DEVICE_FLG(0x06f8, 0xb000, /* Hercules DJ Console (Windows Edition) */
2344 		   QUIRK_FLAG_IGNORE_CTL_ERROR),
2345 	DEVICE_FLG(0x06f8, 0xd002, /* Hercules DJ Console (Macintosh Edition) */
2346 		   QUIRK_FLAG_IGNORE_CTL_ERROR),
2347 	DEVICE_FLG(0x0711, 0x5800, /* MCT Trigger 5 USB-to-HDMI */
2348 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2349 	DEVICE_FLG(0x074d, 0x3553, /* Outlaw RR2150 (Micronas UAC3553B) */
2350 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2351 	DEVICE_FLG(0x0763, 0x2030, /* M-Audio Fast Track C400 */
2352 		   QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2353 	DEVICE_FLG(0x0763, 0x2031, /* M-Audio Fast Track C600 */
2354 		   QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2355 	DEVICE_FLG(0x0763, 0x2080, /* M-Audio Fast Track Ultra */
2356 		   QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2357 	DEVICE_FLG(0x0763, 0x2081, /* M-Audio Fast Track Ultra */
2358 		   QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2359 	DEVICE_FLG(0x0763, 0x2084, /* M-Audio Venom */
2360 		   QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_DISABLE_AUTOSUSPEND),
2361 	DEVICE_FLG(0x07fd, 0x000b, /* MOTU M Series 2nd hardware revision */
2362 		   QUIRK_FLAG_CTL_MSG_DELAY_1M),
2363 	DEVICE_FLG(0x08bb, 0x2702, /* LineX FM Transmitter */
2364 		   QUIRK_FLAG_IGNORE_CTL_ERROR),
2365 	DEVICE_FLG(0x0951, 0x16ad, /* Kingston HyperX */
2366 		   QUIRK_FLAG_CTL_MSG_DELAY_1M),
2367 	DEVICE_FLG(0x0a73, 0x003a, /* Mackie DLZ Creator XS */
2368 		   QUIRK_FLAG_ALWAYS_SET_RATE),
2369 	DEVICE_FLG(0x0b05, 0x18a6, /* ASUSTek Computer, Inc. */
2370 		   QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE),
2371 	DEVICE_FLG(0x0b0e, 0x0349, /* Jabra 550a */
2372 		   QUIRK_FLAG_CTL_MSG_DELAY_1M),
2373 	DEVICE_FLG(0x0bda, 0x498a, /* Realtek Semiconductor Corp. */
2374 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE | QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE),
2375 	DEVICE_FLG(0x0c45, 0x6340, /* Sonix HD USB Camera */
2376 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2377 	DEVICE_FLG(0x0c45, 0x636b, /* Microdia JP001 USB Camera */
2378 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2379 	DEVICE_FLG(0x0d8c, 0x000c, /* C-Media */
2380 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2381 	DEVICE_FLG(0x0d8c, 0x0012, /* C-Media */
2382 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2383 	DEVICE_FLG(0x0d8c, 0x0014, /* C-Media */
2384 		   QUIRK_FLAG_CTL_MSG_DELAY_1M | QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2385 	DEVICE_FLG(0x0d8c, 0x0102, /* C-Media CM6206 */
2386 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2387 	DEVICE_FLG(0x0e0b, 0xfa01, /* Feaulle Rainbow */
2388 		   QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2389 	DEVICE_FLG(0x0ecb, 0x205c, /* JBL Quantum610 Wireless */
2390 		   QUIRK_FLAG_FIXED_RATE),
2391 	DEVICE_FLG(0x0ecb, 0x2125, /* JBL Quantum650 Wireless */
2392 		   QUIRK_FLAG_FIXED_RATE),
2393 	DEVICE_FLG(0x0ecb, 0x2069, /* JBL Quantum810 Wireless */
2394 		   QUIRK_FLAG_FIXED_RATE),
2395 	DEVICE_FLG(0x0fd9, 0x0008, /* Hauppauge HVR-950Q */
2396 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2397 	DEVICE_FLG(0x1038, 0x1294, /* SteelSeries Arctis Pro Wireless */
2398 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2399 	DEVICE_FLG(0x1101, 0x0003, /* Audioengine D1 */
2400 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2401 	DEVICE_FLG(0x12d1, 0x3a07, /* HUAWEI USB-C HEADSET */
2402 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE | QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE |
2403 		   QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY),
2404 	DEVICE_FLG(0x1224, 0x2a25, /* Jieli Technology USB PHY 2.0 */
2405 		   QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_MIC_RES_16),
2406 	DEVICE_FLG(0x1395, 0x740a, /* Sennheiser DECT */
2407 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2408 	DEVICE_FLG(0x1397, 0x0507, /* Behringer UMC202HD */
2409 		   QUIRK_FLAG_PLAYBACK_FIRST | QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2410 	DEVICE_FLG(0x1397, 0x0508, /* Behringer UMC204HD */
2411 		   QUIRK_FLAG_PLAYBACK_FIRST | QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2412 	DEVICE_FLG(0x1397, 0x0509, /* Behringer UMC404HD */
2413 		   QUIRK_FLAG_PLAYBACK_FIRST | QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2414 	DEVICE_FLG(0x1397, 0x050c, /* Behringer Flow 8 */
2415 		   QUIRK_FLAG_IFB_SILENCE_ON_EMPTY),
2416 	DEVICE_FLG(0x1397, 0x0510, /* Behringer UV1 */
2417 		   QUIRK_FLAG_PLAYBACK_FIRST | QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2418 	DEVICE_FLG(0x13e5, 0x0001, /* Serato Phono */
2419 		   QUIRK_FLAG_IGNORE_CTL_ERROR),
2420 	DEVICE_FLG(0x152a, 0x85dd, /* SMSL USB DAC */
2421 		   QUIRK_FLAG_DSD_RAW | QUIRK_FLAG_DISABLE_AUTOSUSPEND |
2422 		   QUIRK_FLAG_SKIP_IFACE_SETUP),
2423 	DEVICE_FLG(0x152a, 0x880a, /* NeuralDSP Quad Cortex */
2424 		   0), /* Doesn't have the vendor quirk which would otherwise apply */
2425 	DEVICE_FLG(0x1532, 0x055e, /* Razer Nommo V2 X */
2426 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2427 	DEVICE_FLG(0x154e, 0x1002, /* Denon DCD-1500RE */
2428 		   QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY),
2429 	DEVICE_FLG(0x154e, 0x1003, /* Denon DA-300USB */
2430 		   QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY),
2431 	DEVICE_FLG(0x154e, 0x3005, /* Marantz HD-DAC1 */
2432 		   QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY),
2433 	DEVICE_FLG(0x154e, 0x3006, /* Marantz SA-14S1 */
2434 		   QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY),
2435 	DEVICE_FLG(0x154e, 0x300b, /* Marantz SA-KI RUBY / SA-12 */
2436 		   QUIRK_FLAG_DSD_RAW),
2437 	DEVICE_FLG(0x154e, 0x500e, /* Denon DN-X1600 */
2438 		   QUIRK_FLAG_IGNORE_CLOCK_SOURCE),
2439 	DEVICE_FLG(0x1686, 0x00dd, /* Zoom R16/24 */
2440 		   QUIRK_FLAG_TX_LENGTH | QUIRK_FLAG_CTL_MSG_DELAY_1M),
2441 	DEVICE_FLG(0x16d0, 0x0ab1, /* PureAudio APA DAC */
2442 		   QUIRK_FLAG_DSD_RAW),
2443 	DEVICE_FLG(0x16d0, 0xeca1, /* PureAudio Lotus DAC5, DAC5 SE and DAC5 Pro */
2444 		   QUIRK_FLAG_DSD_RAW),
2445 	DEVICE_FLG(0x17aa, 0x1046, /* Lenovo ThinkStation P620 Rear Line-in, Line-out and Microphone */
2446 		   QUIRK_FLAG_DISABLE_AUTOSUSPEND),
2447 	DEVICE_FLG(0x17aa, 0x104d, /* Lenovo ThinkStation P620 Internal Speaker + Front Headset */
2448 		   QUIRK_FLAG_DISABLE_AUTOSUSPEND),
2449 	DEVICE_FLG(0x17ef, 0x3083, /* Lenovo TBT3 dock */
2450 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2451 	DEVICE_FLG(0x1852, 0x5062, /* Luxman D-08u */
2452 		   QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY),
2453 	DEVICE_FLG(0x1852, 0x5065, /* Luxman DA-06 */
2454 		   QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY),
2455 	DEVICE_FLG(0x1901, 0x0191, /* GE B850V3 CP2114 audio interface */
2456 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2457 	DEVICE_FLG(0x19f7, 0x0003, /* RODE NT-USB */
2458 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2459 	DEVICE_FLG(0x19f7, 0x0035, /* RODE NT-USB+ */
2460 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2461 	DEVICE_FLG(0x1bcf, 0x2281, /* HD Webcam */
2462 		   QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_MIC_RES_16),
2463 	DEVICE_FLG(0x1bcf, 0x2283, /* NexiGo N930AF FHD Webcam */
2464 		   QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_MIC_RES_16),
2465 	DEVICE_FLG(0x1e0b, 0xd01e, /* Generic USB Audio Device */
2466 		   QUIRK_FLAG_PLAYBACK_URB_FIXUP),
2467 	DEVICE_FLG(0x1ff7, 0x0f81, /* SC13A Webcam */
2468 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2469 	DEVICE_FLG(0x2040, 0x7200, /* Hauppauge HVR-950Q */
2470 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2471 	DEVICE_FLG(0x2040, 0x7201, /* Hauppauge HVR-950Q-MXL */
2472 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2473 	DEVICE_FLG(0x2040, 0x7210, /* Hauppauge HVR-950Q */
2474 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2475 	DEVICE_FLG(0x2040, 0x7211, /* Hauppauge HVR-950Q-MXL */
2476 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2477 	DEVICE_FLG(0x2040, 0x7213, /* Hauppauge HVR-950Q */
2478 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2479 	DEVICE_FLG(0x2040, 0x7217, /* Hauppauge HVR-950Q */
2480 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2481 	DEVICE_FLG(0x2040, 0x721b, /* Hauppauge HVR-950Q */
2482 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2483 	DEVICE_FLG(0x2040, 0x721e, /* Hauppauge HVR-950Q */
2484 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2485 	DEVICE_FLG(0x2040, 0x721f, /* Hauppauge HVR-950Q */
2486 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2487 	DEVICE_FLG(0x2040, 0x7240, /* Hauppauge HVR-850 */
2488 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2489 	DEVICE_FLG(0x2040, 0x7260, /* Hauppauge HVR-950Q */
2490 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2491 	DEVICE_FLG(0x2040, 0x7270, /* Hauppauge HVR-950Q */
2492 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2493 	DEVICE_FLG(0x2040, 0x7280, /* Hauppauge HVR-950Q */
2494 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2495 	DEVICE_FLG(0x2040, 0x7281, /* Hauppauge HVR-950Q-MXL */
2496 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2497 	DEVICE_FLG(0x20b1, 0x2009, /* XMOS Ltd DIYINHK USB Audio 2.0 */
2498 		   QUIRK_FLAG_SKIP_IMPLICIT_FB | QUIRK_FLAG_DSD_RAW),
2499 	DEVICE_FLG(0x2040, 0x8200, /* Hauppauge Woodbury */
2500 		   QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
2501 	DEVICE_FLG(0x21b4, 0x0081, /* AudioQuest DragonFly */
2502 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2503 	DEVICE_FLG(0x21b4, 0x0230, /* Ayre QB-9 Twenty */
2504 		   QUIRK_FLAG_DSD_RAW),
2505 	DEVICE_FLG(0x21b4, 0x0232, /* Ayre QX-5 Twenty */
2506 		   QUIRK_FLAG_DSD_RAW),
2507 	DEVICE_FLG(0x2522, 0x0007, /* LH Labs Geek Out HD Audio 1V5 */
2508 		   QUIRK_FLAG_SET_IFACE_FIRST),
2509 	DEVICE_FLG(0x25aa, 0x600b, /* TAE1159 */
2510 		   QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY),
2511 	DEVICE_FLG(0x262a, 0x9302, /* ddHiFi TC44C */
2512 		   QUIRK_FLAG_DSD_RAW),
2513 	DEVICE_FLG(0x2708, 0x0002, /* Audient iD14 */
2514 		   QUIRK_FLAG_IGNORE_CTL_ERROR),
2515 	DEVICE_FLG(0x2772, 0x0502, /* Musical Fidelity M6s DAC */
2516 		   0), /* for avoiding QUIRK_FLAG_DSD_RAW with vendor match */
2517 	DEVICE_FLG(0x2912, 0x30c8, /* Audioengine D1 */
2518 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2519 	DEVICE_FLG(0x2a70, 0x1881, /* OnePlus Technology (Shenzhen) Co., Ltd. BE02T */
2520 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE | QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE),
2521 	DEVICE_FLG(0x2b53, 0x0023, /* Fiero SC-01 (firmware v1.0.0 @ 48 kHz) */
2522 		   QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2523 	DEVICE_FLG(0x2b53, 0x0024, /* Fiero SC-01 (firmware v1.0.0 @ 96 kHz) */
2524 		   QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2525 	DEVICE_FLG(0x2b53, 0x0031, /* Fiero SC-01 (firmware v1.1.0) */
2526 		   QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2527 	DEVICE_FLG(0x2b73, 0x0047, /* AlphaTheta EUPHONIA */
2528 		   QUIRK_FLAG_PLAYBACK_FIRST | QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2529 	DEVICE_FLG(0x2d95, 0x8011, /* VIVO USB-C HEADSET */
2530 		   QUIRK_FLAG_CTL_MSG_DELAY_1M),
2531 	DEVICE_FLG(0x2d95, 0x8021, /* VIVO USB-C-XE710 HEADSET */
2532 		   QUIRK_FLAG_CTL_MSG_DELAY_1M),
2533 	DEVICE_FLG(0x2d99, 0x0026, /* HECATE G2 GAMING HEADSET */
2534 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2535 	DEVICE_FLG(0x2fc6, 0xf06b, /* MOONDROP Moonriver2 Ti */
2536 		   QUIRK_FLAG_CTL_MSG_DELAY),
2537 	DEVICE_FLG(0x2fc6, 0xf0b5, /* iBasso DC-Elite */
2538 		   QUIRK_FLAG_CTL_MSG_DELAY_1M),
2539 	DEVICE_FLG(0x2fc6, 0xf0b7, /* iBasso DC07 Pro */
2540 		   QUIRK_FLAG_CTL_MSG_DELAY_1M),
2541 	DEVICE_FLG(0x30be, 0x0101, /* Schiit Hel */
2542 		   QUIRK_FLAG_IGNORE_CTL_ERROR),
2543 	DEVICE_FLG(0x3255, 0x0000, /* Luxman D-10X */
2544 		   QUIRK_FLAG_ITF_USB_DSD_DAC | QUIRK_FLAG_CTL_MSG_DELAY),
2545 	DEVICE_FLG(0x3302, 0x17c2, /* TTGK Technology USB-C Audio */
2546 		   QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY),
2547 	DEVICE_FLG(0x339b, 0x3a07, /* Synaptics HONOR USB-C HEADSET */
2548 		   QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE),
2549 	DEVICE_FLG(0x3443, 0x930d, /* NexiGo N930W 60fps Webcam */
2550 		   QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_MIC_RES_16),
2551 	DEVICE_FLG(0x36f9, 0xc009, /* XIBERIA K03S */
2552 		   QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY),
2553 	DEVICE_FLG(0x3c20, 0x3d21, /* AB13X USB Audio */
2554 		   QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY),
2555 	DEVICE_FLG(0x413c, 0xa506, /* Dell AE515 sound bar */
2556 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2557 	DEVICE_FLG(0x534d, 0x0021, /* MacroSilicon MS2100/MS2106 */
2558 		   QUIRK_FLAG_ALIGN_TRANSFER),
2559 	DEVICE_FLG(0x534d, 0x2109, /* MacroSilicon MS2109 */
2560 		   QUIRK_FLAG_ALIGN_TRANSFER),
2561 	DEVICE_FLG(0x84ef, 0x002a, /* Valeton GP-200 */
2562 		   QUIRK_FLAG_GENERIC_IMPLICIT_FB),
2563 	DEVICE_FLG(0x84ef, 0x0082, /* Hotone Audio Pulze Mini */
2564 		   QUIRK_FLAG_MIXER_PLAYBACK_LINEAR_VOL | QUIRK_FLAG_MIXER_CAPTURE_LINEAR_VOL),
2565 
2566 	/* Vendor and string descriptor matches */
2567 	VENDOR_STRING_FLG(0x1235, /* Conflict with Focusrite Novation */
2568 			  "MV-SILICON",
2569 			  0), /* Stop matching */
2570 
2571 	/* Vendor matches */
2572 	VENDOR_FLG(0x045e, /* MS Lifecam */
2573 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2574 	VENDOR_FLG(0x046d, /* Logitech */
2575 		   QUIRK_FLAG_CTL_MSG_DELAY_1M),
2576 	VENDOR_FLG(0x047f, /* Plantronics */
2577 		   QUIRK_FLAG_GET_SAMPLE_RATE | QUIRK_FLAG_CTL_MSG_DELAY),
2578 	VENDOR_FLG(0x0644, /* TEAC Corp. */
2579 		   QUIRK_FLAG_CTL_MSG_DELAY | QUIRK_FLAG_IFACE_DELAY),
2580 	VENDOR_FLG(0x07fd, /* MOTU */
2581 		   QUIRK_FLAG_VALIDATE_RATES),
2582 	DEVICE_FLG(0x1235, 0x8006, 0), /* Focusrite Scarlett 2i2 1st Gen */
2583 	DEVICE_FLG(0x1235, 0x800a, 0), /* Focusrite Scarlett 2i4 1st Gen */
2584 	DEVICE_FLG(0x1235, 0x800c, 0), /* Focusrite Scarlett 18i20 1st Gen */
2585 	DEVICE_FLG(0x1235, 0x8016, 0), /* Focusrite Scarlett 2i2 1st Gen */
2586 	DEVICE_FLG(0x1235, 0x801c, 0), /* Focusrite Scarlett Solo 1st Gen */
2587 	VENDOR_FLG(0x1235, /* Focusrite Novation */
2588 		   QUIRK_FLAG_SKIP_CLOCK_SELECTOR |
2589 		   QUIRK_FLAG_SKIP_IFACE_SETUP),
2590 	VENDOR_FLG(0x1511, /* AURALiC */
2591 		   QUIRK_FLAG_DSD_RAW),
2592 	VENDOR_FLG(0x152a, /* Thesycon devices */
2593 		   QUIRK_FLAG_DSD_RAW),
2594 	VENDOR_FLG(0x18d1, /* iBasso devices */
2595 		   QUIRK_FLAG_DSD_RAW),
2596 	VENDOR_FLG(0x1de7, /* Phoenix Audio */
2597 		   QUIRK_FLAG_GET_SAMPLE_RATE),
2598 	VENDOR_FLG(0x20b1, /* XMOS based devices */
2599 		   QUIRK_FLAG_DSD_RAW),
2600 	VENDOR_FLG(0x21ed, /* Accuphase Laboratory */
2601 		   QUIRK_FLAG_DSD_RAW),
2602 	VENDOR_FLG(0x22d9, /* Oppo */
2603 		   QUIRK_FLAG_DSD_RAW),
2604 	VENDOR_FLG(0x23ba, /* Playback Design */
2605 		   QUIRK_FLAG_CTL_MSG_DELAY | QUIRK_FLAG_IFACE_DELAY |
2606 		   QUIRK_FLAG_DSD_RAW),
2607 	VENDOR_FLG(0x25ce, /* Mytek devices */
2608 		   QUIRK_FLAG_DSD_RAW),
2609 	VENDOR_FLG(0x2622, /* IAG Limited devices */
2610 		   QUIRK_FLAG_DSD_RAW),
2611 	VENDOR_FLG(0x2772, /* Musical Fidelity devices */
2612 		   QUIRK_FLAG_DSD_RAW),
2613 	VENDOR_FLG(0x278b, /* Rotel? */
2614 		   QUIRK_FLAG_DSD_RAW),
2615 	VENDOR_FLG(0x292b, /* Gustard/Ess based devices */
2616 		   QUIRK_FLAG_DSD_RAW),
2617 	VENDOR_FLG(0x2972, /* FiiO devices */
2618 		   QUIRK_FLAG_DSD_RAW),
2619 	VENDOR_FLG(0x2ab6, /* T+A devices */
2620 		   QUIRK_FLAG_DSD_RAW),
2621 	VENDOR_FLG(0x2afd, /* McIntosh Laboratory, Inc. */
2622 		   QUIRK_FLAG_DSD_RAW),
2623 	VENDOR_FLG(0x2d87, /* Cayin device */
2624 		   QUIRK_FLAG_DSD_RAW),
2625 	VENDOR_FLG(0x2fc6, /* Comture-inc devices */
2626 		   QUIRK_FLAG_DSD_RAW),
2627 	VENDOR_FLG(0x3336, /* HEM devices */
2628 		   QUIRK_FLAG_DSD_RAW),
2629 	VENDOR_FLG(0x3353, /* Khadas devices */
2630 		   QUIRK_FLAG_DSD_RAW),
2631 	VENDOR_FLG(0x35f4, /* MSB Technology */
2632 		   QUIRK_FLAG_DSD_RAW),
2633 	VENDOR_FLG(0x3842, /* EVGA */
2634 		   QUIRK_FLAG_DSD_RAW),
2635 	VENDOR_FLG(0xc502, /* HiBy devices */
2636 		   QUIRK_FLAG_DSD_RAW),
2637 
2638 	{} /* terminator */
2639 };
2640 
2641 #define QUIRK_STRING_ENTRY(x) \
2642 	[QUIRK_TYPE_ ## x] = __stringify(x)
2643 
2644 static const char *const snd_usb_audio_quirk_flag_names[] = {
2645 	QUIRK_STRING_ENTRY(GET_SAMPLE_RATE),
2646 	QUIRK_STRING_ENTRY(SHARE_MEDIA_DEVICE),
2647 	QUIRK_STRING_ENTRY(ALIGN_TRANSFER),
2648 	QUIRK_STRING_ENTRY(TX_LENGTH),
2649 	QUIRK_STRING_ENTRY(PLAYBACK_FIRST),
2650 	QUIRK_STRING_ENTRY(SKIP_CLOCK_SELECTOR),
2651 	QUIRK_STRING_ENTRY(IGNORE_CLOCK_SOURCE),
2652 	QUIRK_STRING_ENTRY(ITF_USB_DSD_DAC),
2653 	QUIRK_STRING_ENTRY(CTL_MSG_DELAY),
2654 	QUIRK_STRING_ENTRY(CTL_MSG_DELAY_1M),
2655 	QUIRK_STRING_ENTRY(CTL_MSG_DELAY_5M),
2656 	QUIRK_STRING_ENTRY(IFACE_DELAY),
2657 	QUIRK_STRING_ENTRY(VALIDATE_RATES),
2658 	QUIRK_STRING_ENTRY(DISABLE_AUTOSUSPEND),
2659 	QUIRK_STRING_ENTRY(IGNORE_CTL_ERROR),
2660 	QUIRK_STRING_ENTRY(DSD_RAW),
2661 	QUIRK_STRING_ENTRY(SET_IFACE_FIRST),
2662 	QUIRK_STRING_ENTRY(GENERIC_IMPLICIT_FB),
2663 	QUIRK_STRING_ENTRY(SKIP_IMPLICIT_FB),
2664 	QUIRK_STRING_ENTRY(IFACE_SKIP_CLOSE),
2665 	QUIRK_STRING_ENTRY(FORCE_IFACE_RESET),
2666 	QUIRK_STRING_ENTRY(FIXED_RATE),
2667 	QUIRK_STRING_ENTRY(MIC_RES_16),
2668 	QUIRK_STRING_ENTRY(MIC_RES_384),
2669 	QUIRK_STRING_ENTRY(MIXER_PLAYBACK_MIN_MUTE),
2670 	QUIRK_STRING_ENTRY(MIXER_CAPTURE_MIN_MUTE),
2671 	QUIRK_STRING_ENTRY(SKIP_IFACE_SETUP),
2672 	QUIRK_STRING_ENTRY(MIXER_PLAYBACK_LINEAR_VOL),
2673 	QUIRK_STRING_ENTRY(MIXER_CAPTURE_LINEAR_VOL),
2674 	QUIRK_STRING_ENTRY(IFB_SILENCE_ON_EMPTY),
2675 	QUIRK_STRING_ENTRY(MIXER_GET_CUR_OK),
2676 	QUIRK_STRING_ENTRY(PLAYBACK_URB_FIXUP),
2677 	QUIRK_STRING_ENTRY(ALWAYS_SET_RATE),
2678 	NULL
2679 };
2680 
snd_usb_quirk_flags_from_name(const char * name)2681 static u64 snd_usb_quirk_flags_from_name(const char *name)
2682 {
2683 	int i;
2684 
2685 	if (!name || !*name)
2686 		return 0;
2687 
2688 	for (i = 0; snd_usb_audio_quirk_flag_names[i]; i++) {
2689 		if (strcasecmp(name, snd_usb_audio_quirk_flag_names[i]) == 0)
2690 			return BIT_U64(i);
2691 	}
2692 
2693 	return 0;
2694 }
2695 
snd_usb_apply_flag_dbg(const char * reason,struct snd_usb_audio * chip,unsigned long flag)2696 void snd_usb_apply_flag_dbg(const char *reason,
2697 			    struct snd_usb_audio *chip,
2698 			    unsigned long flag)
2699 {
2700 	unsigned long bit;
2701 
2702 	for_each_set_bit(bit, &flag, BYTES_TO_BITS(sizeof(flag))) {
2703 		const char *name = snd_usb_audio_quirk_flag_names[bit];
2704 
2705 		if (name)
2706 			usb_audio_dbg(chip,
2707 				      "From %s apply quirk flag %s for device %04x:%04x\n",
2708 				      reason, name, USB_ID_VENDOR(chip->usb_id),
2709 				      USB_ID_PRODUCT(chip->usb_id));
2710 		else
2711 			usb_audio_warn(chip,
2712 				       "From %s apply unknown quirk flag 0x%lx for device %04x:%04x\n",
2713 				       reason, bit, USB_ID_VENDOR(chip->usb_id),
2714 				       USB_ID_PRODUCT(chip->usb_id));
2715 	}
2716 }
2717 
snd_usb_init_quirk_flags_table(struct snd_usb_audio * chip)2718 void snd_usb_init_quirk_flags_table(struct snd_usb_audio *chip)
2719 {
2720 	const struct usb_audio_quirk_flags_table *p;
2721 
2722 	for (p = quirk_flags_table; p->id; p++) {
2723 		if (chip->usb_id == p->id ||
2724 		    (!USB_ID_PRODUCT(p->id) &&
2725 		     USB_ID_VENDOR(chip->usb_id) == USB_ID_VENDOR(p->id))) {
2726 			/* Handle DEVICE_STRING_FLG/VENDOR_STRING_FLG. */
2727 			if (p->usb_string_match && p->usb_string_match->manufacturer &&
2728 			    strcmp(p->usb_string_match->manufacturer,
2729 				   chip->dev->manufacturer ? chip->dev->manufacturer : ""))
2730 				continue;
2731 			if (p->usb_string_match && p->usb_string_match->product &&
2732 			    strcmp(p->usb_string_match->product,
2733 				   chip->dev->product ? chip->dev->product : ""))
2734 				continue;
2735 
2736 			snd_usb_apply_flag_dbg("builtin table", chip, p->flags);
2737 			chip->quirk_flags |= p->flags;
2738 			return;
2739 		}
2740 	}
2741 }
2742 
snd_usb_init_quirk_flags_parse_string(struct snd_usb_audio * chip,const char * str)2743 void snd_usb_init_quirk_flags_parse_string(struct snd_usb_audio *chip,
2744 					   const char *str)
2745 {
2746 	u16 chip_vid = USB_ID_VENDOR(chip->usb_id);
2747 	u16 chip_pid = USB_ID_PRODUCT(chip->usb_id);
2748 	u64 mask_flags, unmask_flags, bit;
2749 	char *p, *field, *flag;
2750 	bool is_unmask;
2751 	u16 vid, pid;
2752 
2753 	char *val __free(kfree) = kstrdup(str, GFP_KERNEL);
2754 
2755 	if (!val)
2756 		return;
2757 
2758 	for (p = val; p && *p;) {
2759 		/* Each entry consists of VID:PID:flags */
2760 		field = strsep(&p, ":");
2761 		if (!field)
2762 			break;
2763 
2764 		if (strcmp(field, "*") == 0)
2765 			vid = 0;
2766 		else if (kstrtou16(field, 16, &vid))
2767 			break;
2768 
2769 		field = strsep(&p, ":");
2770 		if (!field)
2771 			break;
2772 
2773 		if (strcmp(field, "*") == 0)
2774 			pid = 0;
2775 		else if (kstrtou16(field, 16, &pid))
2776 			break;
2777 
2778 		field = strsep(&p, ";");
2779 		if (!field || !*field)
2780 			break;
2781 
2782 		if ((vid != 0 && vid != chip_vid) ||
2783 		    (pid != 0 && pid != chip_pid))
2784 			continue;
2785 
2786 		/* Collect the flags */
2787 		mask_flags = 0;
2788 		unmask_flags = 0;
2789 		while (field && *field) {
2790 			flag = strsep(&field, "|");
2791 
2792 			if (!flag)
2793 				break;
2794 
2795 			if (*flag == '!') {
2796 				is_unmask = true;
2797 				flag++;
2798 			} else {
2799 				is_unmask = false;
2800 			}
2801 
2802 			if (!kstrtou64(flag, 16, &bit)) {
2803 				if (is_unmask)
2804 					unmask_flags |= bit;
2805 				else
2806 					mask_flags |= bit;
2807 
2808 				break;
2809 			}
2810 
2811 			bit = snd_usb_quirk_flags_from_name(flag);
2812 
2813 			if (bit) {
2814 				if (is_unmask)
2815 					unmask_flags |= bit;
2816 				else
2817 					mask_flags |= bit;
2818 			} else {
2819 				pr_warn("snd_usb_audio: unknown flag %s while parsing param quirk_flags\n",
2820 					flag);
2821 			}
2822 		}
2823 
2824 		chip->quirk_flags &= ~unmask_flags;
2825 		chip->quirk_flags |= mask_flags;
2826 		snd_usb_apply_flag_dbg("module param", chip,
2827 				       chip->quirk_flags);
2828 	}
2829 }
2830