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