quirks.c 48.6 KB
Newer Older
Daniel Mack's avatar
Daniel Mack committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <linux/init.h>
18
#include <linux/slab.h>
Daniel Mack's avatar
Daniel Mack committed
19 20
#include <linux/usb.h>
#include <linux/usb/audio.h>
21
#include <linux/usb/midi.h>
22
#include <linux/bits.h>
Daniel Mack's avatar
Daniel Mack committed
23

24
#include <sound/control.h>
Daniel Mack's avatar
Daniel Mack committed
25 26 27 28 29 30
#include <sound/core.h>
#include <sound/info.h>
#include <sound/pcm.h>

#include "usbaudio.h"
#include "card.h"
31
#include "mixer.h"
32
#include "mixer_quirks.h"
Daniel Mack's avatar
Daniel Mack committed
33 34 35 36 37
#include "midi.h"
#include "quirks.h"
#include "helper.h"
#include "endpoint.h"
#include "pcm.h"
38
#include "clock.h"
Daniel Mack's avatar
Daniel Mack committed
39
#include "stream.h"
Daniel Mack's avatar
Daniel Mack committed
40 41 42 43 44 45 46

/*
 * handle the quirks for the contained interfaces
 */
static int create_composite_quirk(struct snd_usb_audio *chip,
				  struct usb_interface *iface,
				  struct usb_driver *driver,
47
				  const struct snd_usb_audio_quirk *quirk_comp)
Daniel Mack's avatar
Daniel Mack committed
48 49
{
	int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
50
	const struct snd_usb_audio_quirk *quirk;
Daniel Mack's avatar
Daniel Mack committed
51 52
	int err;

53
	for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
Daniel Mack's avatar
Daniel Mack committed
54 55 56 57 58 59 60 61 62
		iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
		if (!iface)
			continue;
		if (quirk->ifnum != probed_ifnum &&
		    usb_interface_claimed(iface))
			continue;
		err = snd_usb_create_quirk(chip, iface, driver, quirk);
		if (err < 0)
			return err;
63 64
	}

65
	for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
66 67 68 69 70
		iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
		if (!iface)
			continue;
		if (quirk->ifnum != probed_ifnum &&
		    !usb_interface_claimed(iface))
Daniel Mack's avatar
Daniel Mack committed
71 72
			usb_driver_claim_interface(driver, iface, (void *)-1L);
	}
73

Daniel Mack's avatar
Daniel Mack committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	return 0;
}

static int ignore_interface_quirk(struct snd_usb_audio *chip,
				  struct usb_interface *iface,
				  struct usb_driver *driver,
				  const struct snd_usb_audio_quirk *quirk)
{
	return 0;
}


/*
 * Allow alignment on audio sub-slot (channel samples) rather than
 * on audio slots (audio frames)
 */
static int create_align_transfer_quirk(struct snd_usb_audio *chip,
				       struct usb_interface *iface,
				       struct usb_driver *driver,
				       const struct snd_usb_audio_quirk *quirk)
{
	chip->txfr_quirk = 1;
	return 1;	/* Continue with creating streams and mixer */
}

static int create_any_midi_quirk(struct snd_usb_audio *chip,
				 struct usb_interface *intf,
				 struct usb_driver *driver,
				 const struct snd_usb_audio_quirk *quirk)
{
	return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
}

/*
 * create a stream for an interface with proper descriptors
 */
static int create_standard_audio_quirk(struct snd_usb_audio *chip,
				       struct usb_interface *iface,
				       struct usb_driver *driver,
				       const struct snd_usb_audio_quirk *quirk)
{
	struct usb_host_interface *alts;
	struct usb_interface_descriptor *altsd;
	int err;

119 120 121
	if (chip->usb_id == USB_ID(0x1686, 0x00dd)) /* Zoom R16/24 */
		chip->tx_length_quirk = 1;

Daniel Mack's avatar
Daniel Mack committed
122 123
	alts = &iface->altsetting[0];
	altsd = get_iface_desc(alts);
Daniel Mack's avatar
Daniel Mack committed
124
	err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
Daniel Mack's avatar
Daniel Mack committed
125
	if (err < 0) {
126
		usb_audio_err(chip, "cannot setup if %d: error %d\n",
Daniel Mack's avatar
Daniel Mack committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
			   altsd->bInterfaceNumber, err);
		return err;
	}
	/* reset the current interface */
	usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
	return 0;
}

/*
 * create a stream for an endpoint/altsetting without proper descriptors
 */
static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
				     struct usb_interface *iface,
				     struct usb_driver *driver,
				     const struct snd_usb_audio_quirk *quirk)
{
	struct audioformat *fp;
	struct usb_host_interface *alts;
145
	struct usb_interface_descriptor *altsd;
Daniel Mack's avatar
Daniel Mack committed
146 147 148 149
	int stream, err;
	unsigned *rate_table = NULL;

	fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
150
	if (!fp)
Daniel Mack's avatar
Daniel Mack committed
151
		return -ENOMEM;
152

153
	INIT_LIST_HEAD(&fp->list);
154 155 156 157
	if (fp->nr_rates > MAX_NR_RATES) {
		kfree(fp);
		return -EINVAL;
	}
Daniel Mack's avatar
Daniel Mack committed
158
	if (fp->nr_rates > 0) {
159 160
		rate_table = kmemdup(fp->rate_table,
				     sizeof(int) * fp->nr_rates, GFP_KERNEL);
Daniel Mack's avatar
Daniel Mack committed
161 162 163 164 165 166 167 168 169
		if (!rate_table) {
			kfree(fp);
			return -ENOMEM;
		}
		fp->rate_table = rate_table;
	}

	stream = (fp->endpoint & USB_DIR_IN)
		? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Daniel Mack's avatar
Daniel Mack committed
170
	err = snd_usb_add_audio_stream(chip, stream, fp);
171 172
	if (err < 0)
		goto error;
Daniel Mack's avatar
Daniel Mack committed
173 174
	if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
	    fp->altset_idx >= iface->num_altsetting) {
175 176
		err = -EINVAL;
		goto error;
Daniel Mack's avatar
Daniel Mack committed
177 178
	}
	alts = &iface->altsetting[fp->altset_idx];
179
	altsd = get_iface_desc(alts);
180
	if (altsd->bNumEndpoints < 1) {
181 182
		err = -EINVAL;
		goto error;
183 184
	}

185 186
	fp->protocol = altsd->bInterfaceProtocol;

187 188 189 190
	if (fp->datainterval == 0)
		fp->datainterval = snd_usb_parse_datainterval(chip, alts);
	if (fp->maxpacksize == 0)
		fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Daniel Mack's avatar
Daniel Mack committed
191
	usb_set_interface(chip->dev, fp->iface, 0);
192 193
	snd_usb_init_pitch(chip, fp->iface, alts, fp);
	snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
Daniel Mack's avatar
Daniel Mack committed
194
	return 0;
195 196

 error:
197
	list_del(&fp->list); /* unlink for avoiding double-free */
198 199 200
	kfree(fp);
	kfree(rate_table);
	return err;
Daniel Mack's avatar
Daniel Mack committed
201 202
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static int create_auto_pcm_quirk(struct snd_usb_audio *chip,
				 struct usb_interface *iface,
				 struct usb_driver *driver)
{
	struct usb_host_interface *alts;
	struct usb_interface_descriptor *altsd;
	struct usb_endpoint_descriptor *epd;
	struct uac1_as_header_descriptor *ashd;
	struct uac_format_type_i_discrete_descriptor *fmtd;

	/*
	 * Most Roland/Yamaha audio streaming interfaces have more or less
	 * standard descriptors, but older devices might lack descriptors, and
	 * future ones might change, so ensure that we fail silently if the
	 * interface doesn't look exactly right.
	 */

	/* must have a non-zero altsetting for streaming */
	if (iface->num_altsetting < 2)
		return -ENODEV;
	alts = &iface->altsetting[1];
	altsd = get_iface_desc(alts);

	/* must have an isochronous endpoint for streaming */
	if (altsd->bNumEndpoints < 1)
		return -ENODEV;
	epd = get_endpoint(alts, 0);
	if (!usb_endpoint_xfer_isoc(epd))
		return -ENODEV;

	/* must have format descriptors */
	ashd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
				       UAC_AS_GENERAL);
	fmtd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
				       UAC_FORMAT_TYPE);
	if (!ashd || ashd->bLength < 7 ||
	    !fmtd || fmtd->bLength < 8)
		return -ENODEV;

	return create_standard_audio_quirk(chip, iface, driver, NULL);
}

static int create_yamaha_midi_quirk(struct snd_usb_audio *chip,
				    struct usb_interface *iface,
				    struct usb_driver *driver,
				    struct usb_host_interface *alts)
{
	static const struct snd_usb_audio_quirk yamaha_midi_quirk = {
		.type = QUIRK_MIDI_YAMAHA
	};
	struct usb_midi_in_jack_descriptor *injd;
	struct usb_midi_out_jack_descriptor *outjd;

	/* must have some valid jack descriptors */
	injd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
				       NULL, USB_MS_MIDI_IN_JACK);
	outjd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
					NULL, USB_MS_MIDI_OUT_JACK);
	if (!injd && !outjd)
		return -ENODEV;
	if (injd && (injd->bLength < 5 ||
		     (injd->bJackType != USB_MS_EMBEDDED &&
		      injd->bJackType != USB_MS_EXTERNAL)))
		return -ENODEV;
	if (outjd && (outjd->bLength < 6 ||
		      (outjd->bJackType != USB_MS_EMBEDDED &&
		       outjd->bJackType != USB_MS_EXTERNAL)))
		return -ENODEV;
	return create_any_midi_quirk(chip, iface, driver, &yamaha_midi_quirk);
}

static int create_roland_midi_quirk(struct snd_usb_audio *chip,
				    struct usb_interface *iface,
				    struct usb_driver *driver,
				    struct usb_host_interface *alts)
{
	static const struct snd_usb_audio_quirk roland_midi_quirk = {
		.type = QUIRK_MIDI_ROLAND
	};
	u8 *roland_desc = NULL;

	/* might have a vendor-specific descriptor <06 24 F1 02 ...> */
	for (;;) {
		roland_desc = snd_usb_find_csint_desc(alts->extra,
						      alts->extralen,
						      roland_desc, 0xf1);
		if (!roland_desc)
			return -ENODEV;
		if (roland_desc[0] < 6 || roland_desc[3] != 2)
			continue;
		return create_any_midi_quirk(chip, iface, driver,
					     &roland_midi_quirk);
	}
}

static int create_std_midi_quirk(struct snd_usb_audio *chip,
				 struct usb_interface *iface,
				 struct usb_driver *driver,
				 struct usb_host_interface *alts)
{
	struct usb_ms_header_descriptor *mshd;
	struct usb_ms_endpoint_descriptor *msepd;

	/* must have the MIDIStreaming interface header descriptor*/
	mshd = (struct usb_ms_header_descriptor *)alts->extra;
	if (alts->extralen < 7 ||
	    mshd->bLength < 7 ||
	    mshd->bDescriptorType != USB_DT_CS_INTERFACE ||
	    mshd->bDescriptorSubtype != USB_MS_HEADER)
		return -ENODEV;
	/* must have the MIDIStreaming endpoint descriptor*/
	msepd = (struct usb_ms_endpoint_descriptor *)alts->endpoint[0].extra;
	if (alts->endpoint[0].extralen < 4 ||
	    msepd->bLength < 4 ||
	    msepd->bDescriptorType != USB_DT_CS_ENDPOINT ||
	    msepd->bDescriptorSubtype != UAC_MS_GENERAL ||
	    msepd->bNumEmbMIDIJack < 1 ||
	    msepd->bNumEmbMIDIJack > 16)
		return -ENODEV;

	return create_any_midi_quirk(chip, iface, driver, NULL);
}

static int create_auto_midi_quirk(struct snd_usb_audio *chip,
				  struct usb_interface *iface,
				  struct usb_driver *driver)
{
	struct usb_host_interface *alts;
	struct usb_interface_descriptor *altsd;
	struct usb_endpoint_descriptor *epd;
	int err;

	alts = &iface->altsetting[0];
	altsd = get_iface_desc(alts);

	/* must have at least one bulk/interrupt endpoint for streaming */
	if (altsd->bNumEndpoints < 1)
		return -ENODEV;
	epd = get_endpoint(alts, 0);
342
	if (!usb_endpoint_xfer_bulk(epd) &&
343 344 345 346 347 348
	    !usb_endpoint_xfer_int(epd))
		return -ENODEV;

	switch (USB_ID_VENDOR(chip->usb_id)) {
	case 0x0499: /* Yamaha */
		err = create_yamaha_midi_quirk(chip, iface, driver, alts);
349
		if (err != -ENODEV)
350 351 352 353
			return err;
		break;
	case 0x0582: /* Roland */
		err = create_roland_midi_quirk(chip, iface, driver, alts);
354
		if (err != -ENODEV)
355 356 357 358 359 360 361 362 363
			return err;
		break;
	}

	return create_std_midi_quirk(chip, iface, driver, alts);
}

static int create_autodetect_quirk(struct snd_usb_audio *chip,
				   struct usb_interface *iface,
364
				   struct usb_driver *driver)
365 366 367 368 369 370 371 372 373
{
	int err;

	err = create_auto_pcm_quirk(chip, iface, driver);
	if (err == -ENODEV)
		err = create_auto_midi_quirk(chip, iface, driver);
	return err;
}

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
static int create_autodetect_quirks(struct snd_usb_audio *chip,
				    struct usb_interface *iface,
				    struct usb_driver *driver,
				    const struct snd_usb_audio_quirk *quirk)
{
	int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
	int ifcount, ifnum, err;

	err = create_autodetect_quirk(chip, iface, driver);
	if (err < 0)
		return err;

	/*
	 * ALSA PCM playback/capture devices cannot be registered in two steps,
	 * so we have to claim the other corresponding interface here.
	 */
	ifcount = chip->dev->actconfig->desc.bNumInterfaces;
	for (ifnum = 0; ifnum < ifcount; ifnum++) {
		if (ifnum == probed_ifnum || quirk->ifnum >= 0)
			continue;
		iface = usb_ifnum_to_if(chip->dev, ifnum);
		if (!iface ||
		    usb_interface_claimed(iface) ||
		    get_iface_desc(iface->altsetting)->bInterfaceClass !=
							USB_CLASS_VENDOR_SPEC)
			continue;

		err = create_autodetect_quirk(chip, iface, driver);
		if (err >= 0)
			usb_driver_claim_interface(driver, iface, (void *)-1L);
	}

	return 0;
}

Daniel Mack's avatar
Daniel Mack committed
409 410 411 412 413 414 415 416 417 418
/*
 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.  
 * The only way to detect the sample rate is by looking at wMaxPacketSize.
 */
static int create_uaxx_quirk(struct snd_usb_audio *chip,
			     struct usb_interface *iface,
			     struct usb_driver *driver,
			     const struct snd_usb_audio_quirk *quirk)
{
	static const struct audioformat ua_format = {
419
		.formats = SNDRV_PCM_FMTBIT_S24_3LE,
Daniel Mack's avatar
Daniel Mack committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
		.channels = 2,
		.fmt_type = UAC_FORMAT_TYPE_I,
		.altsetting = 1,
		.altset_idx = 1,
		.rates = SNDRV_PCM_RATE_CONTINUOUS,
	};
	struct usb_host_interface *alts;
	struct usb_interface_descriptor *altsd;
	struct audioformat *fp;
	int stream, err;

	/* both PCM and MIDI interfaces have 2 or more altsettings */
	if (iface->num_altsetting < 2)
		return -ENXIO;
	alts = &iface->altsetting[1];
	altsd = get_iface_desc(alts);

	if (altsd->bNumEndpoints == 2) {
		static const struct snd_usb_midi_endpoint_info ua700_ep = {
			.out_cables = 0x0003,
			.in_cables  = 0x0003
		};
		static const struct snd_usb_audio_quirk ua700_quirk = {
			.type = QUIRK_MIDI_FIXED_ENDPOINT,
			.data = &ua700_ep
		};
		static const struct snd_usb_midi_endpoint_info uaxx_ep = {
			.out_cables = 0x0001,
			.in_cables  = 0x0001
		};
		static const struct snd_usb_audio_quirk uaxx_quirk = {
			.type = QUIRK_MIDI_FIXED_ENDPOINT,
			.data = &uaxx_ep
		};
		const struct snd_usb_audio_quirk *quirk =
			chip->usb_id == USB_ID(0x0582, 0x002b)
			? &ua700_quirk : &uaxx_quirk;
457 458 459
		return __snd_usbmidi_create(chip->card, iface,
					  &chip->midi_list, quirk,
					  chip->usb_id);
Daniel Mack's avatar
Daniel Mack committed
460 461 462 463 464
	}

	if (altsd->bNumEndpoints != 1)
		return -ENXIO;

465
	fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
Daniel Mack's avatar
Daniel Mack committed
466 467 468 469 470 471 472 473
	if (!fp)
		return -ENOMEM;

	fp->iface = altsd->bInterfaceNumber;
	fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
	fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
	fp->datainterval = 0;
	fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
474
	INIT_LIST_HEAD(&fp->list);
Daniel Mack's avatar
Daniel Mack committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488

	switch (fp->maxpacksize) {
	case 0x120:
		fp->rate_max = fp->rate_min = 44100;
		break;
	case 0x138:
	case 0x140:
		fp->rate_max = fp->rate_min = 48000;
		break;
	case 0x258:
	case 0x260:
		fp->rate_max = fp->rate_min = 96000;
		break;
	default:
489
		usb_audio_err(chip, "unknown sample rate\n");
Daniel Mack's avatar
Daniel Mack committed
490 491 492 493 494 495
		kfree(fp);
		return -ENXIO;
	}

	stream = (fp->endpoint & USB_DIR_IN)
		? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Daniel Mack's avatar
Daniel Mack committed
496
	err = snd_usb_add_audio_stream(chip, stream, fp);
Daniel Mack's avatar
Daniel Mack committed
497
	if (err < 0) {
498
		list_del(&fp->list); /* unlink for avoiding double-free */
Daniel Mack's avatar
Daniel Mack committed
499 500 501 502 503 504 505
		kfree(fp);
		return err;
	}
	usb_set_interface(chip->dev, fp->iface, 0);
	return 0;
}

506 507 508 509 510 511 512 513 514 515 516 517 518 519
/*
 * Create a standard mixer for the specified interface.
 */
static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
				       struct usb_interface *iface,
				       struct usb_driver *driver,
				       const struct snd_usb_audio_quirk *quirk)
{
	if (quirk->ifnum < 0)
		return 0;

	return snd_usb_create_mixer(chip, quirk->ifnum, 0);
}

Daniel Mack's avatar
Daniel Mack committed
520 521 522 523
/*
 * audio-interface quirks
 *
 * returns zero if no standard audio/MIDI parsing is needed.
Lucas De Marchi's avatar
Lucas De Marchi committed
524
 * returns a positive value if standard audio/midi interfaces are parsed
Daniel Mack's avatar
Daniel Mack committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
 * after this.
 * returns a negative value at error.
 */
int snd_usb_create_quirk(struct snd_usb_audio *chip,
			 struct usb_interface *iface,
			 struct usb_driver *driver,
			 const struct snd_usb_audio_quirk *quirk)
{
	typedef int (*quirk_func_t)(struct snd_usb_audio *,
				    struct usb_interface *,
				    struct usb_driver *,
				    const struct snd_usb_audio_quirk *);
	static const quirk_func_t quirk_funcs[] = {
		[QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
		[QUIRK_COMPOSITE] = create_composite_quirk,
540
		[QUIRK_AUTODETECT] = create_autodetect_quirks,
Daniel Mack's avatar
Daniel Mack committed
541 542 543
		[QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
		[QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
		[QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
544
		[QUIRK_MIDI_ROLAND] = create_any_midi_quirk,
Daniel Mack's avatar
Daniel Mack committed
545 546
		[QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
		[QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
547
		[QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
Daniel Mack's avatar
Daniel Mack committed
548 549
		[QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
		[QUIRK_MIDI_CME] = create_any_midi_quirk,
550
		[QUIRK_MIDI_AKAI] = create_any_midi_quirk,
551
		[QUIRK_MIDI_FTDI] = create_any_midi_quirk,
552
		[QUIRK_MIDI_CH345] = create_any_midi_quirk,
Daniel Mack's avatar
Daniel Mack committed
553 554 555
		[QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
		[QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
		[QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
556 557
		[QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
		[QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
Daniel Mack's avatar
Daniel Mack committed
558 559 560 561 562
	};

	if (quirk->type < QUIRK_TYPE_COUNT) {
		return quirk_funcs[quirk->type](chip, iface, driver, quirk);
	} else {
563
		usb_audio_err(chip, "invalid quirk type %d\n", quirk->type);
Daniel Mack's avatar
Daniel Mack committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
		return -ENXIO;
	}
}

/*
 * boot quirks
 */

#define EXTIGY_FIRMWARE_SIZE_OLD 794
#define EXTIGY_FIRMWARE_SIZE_NEW 483

static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
{
	struct usb_host_config *config = dev->actconfig;
	int err;

	if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
	    le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
582
		dev_dbg(&dev->dev, "sending Extigy boot sequence...\n");
Daniel Mack's avatar
Daniel Mack committed
583 584
		/* Send message to force it to reconnect with full interface. */
		err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
585
				      0x10, 0x43, 0x0001, 0x000a, NULL, 0);
586 587
		if (err < 0)
			dev_dbg(&dev->dev, "error sending boot message: %d\n", err);
Daniel Mack's avatar
Daniel Mack committed
588 589 590
		err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
				&dev->descriptor, sizeof(dev->descriptor));
		config = dev->actconfig;
591 592
		if (err < 0)
			dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
Daniel Mack's avatar
Daniel Mack committed
593
		err = usb_reset_configuration(dev);
594 595 596
		if (err < 0)
			dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
		dev_dbg(&dev->dev, "extigy_boot: new boot length = %d\n",
Daniel Mack's avatar
Daniel Mack committed
597 598 599 600 601 602 603 604 605 606 607 608
			    le16_to_cpu(get_cfg_desc(config)->wTotalLength));
		return -ENODEV; /* quit this anyway */
	}
	return 0;
}

static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
{
	u8 buf = 1;

	snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
609
			0, 0, &buf, 1);
Daniel Mack's avatar
Daniel Mack committed
610 611 612
	if (buf == 0) {
		snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
				USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
613
				1, 2000, NULL, 0);
Daniel Mack's avatar
Daniel Mack committed
614 615 616 617 618
		return -ENODEV;
	}
	return 0;
}

619 620 621 622 623
static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
{
	int err;

	if (dev->actconfig->desc.bConfigurationValue == 1) {
624
		dev_info(&dev->dev,
625 626 627 628 629 630 631
			   "Fast Track Pro switching to config #2\n");
		/* This function has to be available by the usb core module.
		 * if it is not avialable the boot quirk has to be left out
		 * and the configuration has to be set by udev or hotplug
		 * rules
		 */
		err = usb_driver_set_configuration(dev, 2);
632
		if (err < 0)
633 634 635
			dev_dbg(&dev->dev,
				"error usb_driver_set_configuration: %d\n",
				err);
636 637 638 639
		/* Always return an error, so that we stop creating a device
		   that will just be destroyed and recreated with a new
		   configuration */
		return -ENODEV;
640
	} else
641
		dev_info(&dev->dev, "Fast Track Pro config OK\n");
642 643 644 645

	return 0;
}

Daniel Mack's avatar
Daniel Mack committed
646 647 648 649 650 651 652 653 654 655 656 657 658
/*
 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
 * documented in the device's data sheet.
 */
static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
{
	u8 buf[4];
	buf[0] = 0x20;
	buf[1] = value & 0xff;
	buf[2] = (value >> 8) & 0xff;
	buf[3] = reg;
	return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
			       USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
659
			       0, 0, &buf, 4);
Daniel Mack's avatar
Daniel Mack committed
660 661 662 663 664 665 666 667 668 669 670 671
}

static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
{
	/*
	 * Enable line-out driver mode, set headphone source to front
	 * channels, enable stereo mic.
	 */
	return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
}

/*
672
 * CM6206 registers from the CM6206 datasheet rev 2.1
Daniel Mack's avatar
Daniel Mack committed
673
 */
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
#define CM6206_REG0_DMA_MASTER BIT(15)
#define CM6206_REG0_SPDIFO_RATE_48K (2 << 12)
#define CM6206_REG0_SPDIFO_RATE_96K (7 << 12)
/* Bit 4 thru 11 is the S/PDIF category code */
#define CM6206_REG0_SPDIFO_CAT_CODE_GENERAL (0 << 4)
#define CM6206_REG0_SPDIFO_EMPHASIS_CD BIT(3)
#define CM6206_REG0_SPDIFO_COPYRIGHT_NA BIT(2)
#define CM6206_REG0_SPDIFO_NON_AUDIO BIT(1)
#define CM6206_REG0_SPDIFO_PRO_FORMAT BIT(0)

#define CM6206_REG1_TEST_SEL_CLK BIT(14)
#define CM6206_REG1_PLLBIN_EN BIT(13)
#define CM6206_REG1_SOFT_MUTE_EN BIT(12)
#define CM6206_REG1_GPIO4_OUT BIT(11)
#define CM6206_REG1_GPIO4_OE BIT(10)
#define CM6206_REG1_GPIO3_OUT BIT(9)
#define CM6206_REG1_GPIO3_OE BIT(8)
#define CM6206_REG1_GPIO2_OUT BIT(7)
#define CM6206_REG1_GPIO2_OE BIT(6)
#define CM6206_REG1_GPIO1_OUT BIT(5)
#define CM6206_REG1_GPIO1_OE BIT(4)
#define CM6206_REG1_SPDIFO_INVALID BIT(3)
#define CM6206_REG1_SPDIF_LOOP_EN BIT(2)
#define CM6206_REG1_SPDIFO_DIS BIT(1)
#define CM6206_REG1_SPDIFI_MIX BIT(0)

#define CM6206_REG2_DRIVER_ON BIT(15)
#define CM6206_REG2_HEADP_SEL_SIDE_CHANNELS (0 << 13)
#define CM6206_REG2_HEADP_SEL_SURROUND_CHANNELS (1 << 13)
#define CM6206_REG2_HEADP_SEL_CENTER_SUBW (2 << 13)
#define CM6206_REG2_HEADP_SEL_FRONT_CHANNELS (3 << 13)
#define CM6206_REG2_MUTE_HEADPHONE_RIGHT BIT(12)
#define CM6206_REG2_MUTE_HEADPHONE_LEFT BIT(11)
#define CM6206_REG2_MUTE_REAR_SURROUND_RIGHT BIT(10)
#define CM6206_REG2_MUTE_REAR_SURROUND_LEFT BIT(9)
#define CM6206_REG2_MUTE_SIDE_SURROUND_RIGHT BIT(8)
#define CM6206_REG2_MUTE_SIDE_SURROUND_LEFT BIT(7)
#define CM6206_REG2_MUTE_SUBWOOFER BIT(6)
#define CM6206_REG2_MUTE_CENTER BIT(5)
#define CM6206_REG2_MUTE_RIGHT_FRONT BIT(3)
#define CM6206_REG2_MUTE_LEFT_FRONT BIT(3)
#define CM6206_REG2_EN_BTL BIT(2)
#define CM6206_REG2_MCUCLKSEL_1_5_MHZ (0)
#define CM6206_REG2_MCUCLKSEL_3_MHZ (1)
#define CM6206_REG2_MCUCLKSEL_6_MHZ (2)
#define CM6206_REG2_MCUCLKSEL_12_MHZ (3)

/* Bit 11..13 sets the sensitivity to FLY tuner volume control VP/VD signal */
#define CM6206_REG3_FLYSPEED_DEFAULT (2 << 11)
#define CM6206_REG3_VRAP25EN BIT(10)
#define CM6206_REG3_MSEL1 BIT(9)
#define CM6206_REG3_SPDIFI_RATE_44_1K BIT(0 << 7)
#define CM6206_REG3_SPDIFI_RATE_48K BIT(2 << 7)
#define CM6206_REG3_SPDIFI_RATE_32K BIT(3 << 7)
#define CM6206_REG3_PINSEL BIT(6)
#define CM6206_REG3_FOE BIT(5)
#define CM6206_REG3_ROE BIT(4)
#define CM6206_REG3_CBOE BIT(3)
#define CM6206_REG3_LOSE BIT(2)
#define CM6206_REG3_HPOE BIT(1)
#define CM6206_REG3_SPDIFI_CANREC BIT(0)

#define CM6206_REG5_DA_RSTN BIT(13)
#define CM6206_REG5_AD_RSTN BIT(12)
#define CM6206_REG5_SPDIFO_AD2SPDO BIT(12)
#define CM6206_REG5_SPDIFO_SEL_FRONT (0 << 9)
#define CM6206_REG5_SPDIFO_SEL_SIDE_SUR (1 << 9)
#define CM6206_REG5_SPDIFO_SEL_CEN_LFE (2 << 9)
#define CM6206_REG5_SPDIFO_SEL_REAR_SUR (3 << 9)
#define CM6206_REG5_CODECM BIT(8)
#define CM6206_REG5_EN_HPF BIT(7)
#define CM6206_REG5_T_SEL_DSDA4 BIT(6)
#define CM6206_REG5_T_SEL_DSDA3 BIT(5)
#define CM6206_REG5_T_SEL_DSDA2 BIT(4)
#define CM6206_REG5_T_SEL_DSDA1 BIT(3)
#define CM6206_REG5_T_SEL_DSDAD_NORMAL 0
#define CM6206_REG5_T_SEL_DSDAD_FRONT 4
#define CM6206_REG5_T_SEL_DSDAD_S_SURROUND 5
#define CM6206_REG5_T_SEL_DSDAD_CEN_LFE 6
#define CM6206_REG5_T_SEL_DSDAD_R_SURROUND 7

Daniel Mack's avatar
Daniel Mack committed
755 756
static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
{
757
	int err  = 0, reg;
758 759 760 761 762 763 764 765 766 767 768 769 770
	int val[] = {
		/*
		 * Values here are chosen based on sniffing USB traffic
		 * under Windows.
		 *
		 * REG0: DAC is master, sample rate 48kHz, no copyright
		 */
		CM6206_REG0_SPDIFO_RATE_48K |
		CM6206_REG0_SPDIFO_COPYRIGHT_NA,
		/*
		 * REG1: PLL binary search enable, soft mute enable.
		 */
		CM6206_REG1_PLLBIN_EN |
771
		CM6206_REG1_SOFT_MUTE_EN,
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
		/*
		 * REG2: enable output drivers,
		 * select front channels to the headphone output,
		 * then mute the headphone channels, run the MCU
		 * at 1.5 MHz.
		 */
		CM6206_REG2_DRIVER_ON |
		CM6206_REG2_HEADP_SEL_FRONT_CHANNELS |
		CM6206_REG2_MUTE_HEADPHONE_RIGHT |
		CM6206_REG2_MUTE_HEADPHONE_LEFT,
		/*
		 * REG3: default flyspeed, set 2.5V mic bias
		 * enable all line out ports and enable SPDIF
		 */
		CM6206_REG3_FLYSPEED_DEFAULT |
		CM6206_REG3_VRAP25EN |
		CM6206_REG3_FOE |
		CM6206_REG3_ROE |
		CM6206_REG3_CBOE |
		CM6206_REG3_LOSE |
		CM6206_REG3_HPOE |
		CM6206_REG3_SPDIFI_CANREC,
		/* REG4 is just a bunch of GPIO lines */
		0x0000,
		/* REG5: de-assert AD/DA reset signals */
		CM6206_REG5_DA_RSTN |
		CM6206_REG5_AD_RSTN };
Daniel Mack's avatar
Daniel Mack committed
799 800 801 802 803 804 805 806 807 808

	for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
		err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
		if (err < 0)
			return err;
	}

	return err;
}

809 810 811 812 813 814
/* quirk for Plantronics GameCom 780 with CM6302 chip */
static int snd_usb_gamecon780_boot_quirk(struct usb_device *dev)
{
	/* set the initial volume and don't change; other values are either
	 * too loud or silent due to firmware bug (bko#65251)
	 */
815
	u8 buf[2] = { 0x74, 0xe3 };
816 817 818 819 820
	return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
			UAC_FU_VOLUME << 8, 9 << 8, buf, 2);
}

821 822
/*
 * Novation Twitch DJ controller
823
 * Focusrite Novation Saffire 6 USB audio card
824
 */
825
static int snd_usb_novation_boot_quirk(struct usb_device *dev)
826 827 828 829 830 831 832
{
	/* preemptively set up the device because otherwise the
	 * raw MIDI endpoints are not active */
	usb_set_interface(dev, 0, 1);
	return 0;
}

Daniel Mack's avatar
Daniel Mack committed
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
/*
 * This call will put the synth in "USB send" mode, i.e it will send MIDI
 * messages through USB (this is disabled at startup). The synth will
 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
 * sign on its LCD. Values here are chosen based on sniffing USB traffic
 * under Windows.
 */
static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
{
	int err, actual_length;

	/* "midi send" enable */
	static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };

	void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
	if (!buf)
		return -ENOMEM;
	err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
			ARRAY_SIZE(seq), &actual_length, 1000);
	kfree(buf);
	if (err < 0)
		return err;

	return 0;
}

859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
/*
 * Some sound cards from Native Instruments are in fact compliant to the USB
 * audio standard of version 2 and other approved USB standards, even though
 * they come up as vendor-specific device when first connected.
 *
 * However, they can be told to come up with a new set of descriptors
 * upon their next enumeration, and the interfaces announced by the new
 * descriptors will then be handled by the kernel's class drivers. As the
 * product ID will also change, no further checks are required.
 */

static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
{
	int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
				  0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
874
				  1, 0, NULL, 0, 1000);
875 876 877 878 879 880 881 882 883 884 885 886

	if (ret < 0)
		return ret;

	usb_reset_device(dev);

	/* return -EAGAIN, so the creation of an audio interface for this
	 * temporary device is aborted. The device will reconnect with a
	 * new product ID */
	return -EAGAIN;
}

887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
static void mbox2_setup_48_24_magic(struct usb_device *dev)
{
	u8 srate[3];
	u8 temp[12];

	/* Choose 48000Hz permanently */
	srate[0] = 0x80;
	srate[1] = 0xbb;
	srate[2] = 0x00;

	/* Send the magic! */
	snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
		0x01, 0x22, 0x0100, 0x0085, &temp, 0x0003);
	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
		0x81, 0xa2, 0x0100, 0x0085, &srate, 0x0003);
	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
		0x81, 0xa2, 0x0100, 0x0086, &srate, 0x0003);
	snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
		0x81, 0xa2, 0x0100, 0x0003, &srate, 0x0003);
	return;
}

/* Digidesign Mbox 2 needs to load firmware onboard
 * and driver must wait a few seconds for initialisation.
 */

#define MBOX2_FIRMWARE_SIZE    646
#define MBOX2_BOOT_LOADING     0x01 /* Hard coded into the device */
#define MBOX2_BOOT_READY       0x02 /* Hard coded into the device */

917
static int snd_usb_mbox2_boot_quirk(struct usb_device *dev)
918 919 920
{
	struct usb_host_config *config = dev->actconfig;
	int err;
921
	u8 bootresponse[0x12];
922 923 924 925 926 927
	int fwsize;
	int count;

	fwsize = le16_to_cpu(get_cfg_desc(config)->wTotalLength);

	if (fwsize != MBOX2_FIRMWARE_SIZE) {
928
		dev_err(&dev->dev, "Invalid firmware size=%d.\n", fwsize);
929 930 931
		return -ENODEV;
	}

932
	dev_dbg(&dev->dev, "Sending Digidesign Mbox 2 boot sequence...\n");
933 934

	count = 0;
935 936
	bootresponse[0] = MBOX2_BOOT_LOADING;
	while ((bootresponse[0] == MBOX2_BOOT_LOADING) && (count < 10)) {
937 938 939 940
		msleep(500); /* 0.5 second delay */
		snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
			/* Control magic - load onboard firmware */
			0x85, 0xc0, 0x0001, 0x0000, &bootresponse, 0x0012);
941
		if (bootresponse[0] == MBOX2_BOOT_READY)
942
			break;
943
		dev_dbg(&dev->dev, "device not ready, resending boot sequence...\n");
944 945 946
		count++;
	}

947
	if (bootresponse[0] != MBOX2_BOOT_READY) {
948
		dev_err(&dev->dev, "Unknown bootresponse=%d, or timed out, ignoring device.\n", bootresponse[0]);
949 950 951
		return -ENODEV;
	}

952
	dev_dbg(&dev->dev, "device initialised!\n");
953 954 955 956 957

	err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
		&dev->descriptor, sizeof(dev->descriptor));
	config = dev->actconfig;
	if (err < 0)
958
		dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
959 960 961

	err = usb_reset_configuration(dev);
	if (err < 0)
962 963
		dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
	dev_dbg(&dev->dev, "mbox2_boot: new boot length = %d\n",
964 965 966 967
		le16_to_cpu(get_cfg_desc(config)->wTotalLength));

	mbox2_setup_48_24_magic(dev);

968
	dev_info(&dev->dev, "Digidesign Mbox 2: 24bit 48kHz");
969 970 971 972

	return 0; /* Successful boot */
}

973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
static int snd_usb_axefx3_boot_quirk(struct usb_device *dev)
{
	int err;

	dev_dbg(&dev->dev, "Waiting for Axe-Fx III to boot up...\n");

	/* If the Axe-Fx III has not fully booted, it will timeout when trying
	 * to enable the audio streaming interface. A more generous timeout is
	 * used here to detect when the Axe-Fx III has finished booting as the
	 * set interface message will be acked once it has
	 */
	err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
				USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
				1, 1, NULL, 0, 120000);
	if (err < 0) {
		dev_err(&dev->dev,
			"failed waiting for Axe-Fx III to boot: %d\n", err);
		return err;
	}

	dev_dbg(&dev->dev, "Axe-Fx III is now ready\n");

	err = usb_set_interface(dev, 1, 0);
	if (err < 0)
		dev_dbg(&dev->dev,
			"error stopping Axe-Fx III interface: %d\n", err);

	return 0;
}

Daniel Mack's avatar
Daniel Mack committed
1003 1004 1005
/*
 * Setup quirks
 */
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
#define MAUDIO_SET		0x01 /* parse device_setup */
#define MAUDIO_SET_COMPATIBLE	0x80 /* use only "win-compatible" interfaces */
#define MAUDIO_SET_DTS		0x02 /* enable DTS Digital Output */
#define MAUDIO_SET_96K		0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
#define MAUDIO_SET_24B		0x08 /* 24bits sample if set, 16bits otherwise */
#define MAUDIO_SET_DI		0x10 /* enable Digital Input */
#define MAUDIO_SET_MASK		0x1f /* bit mask for setup value */
#define MAUDIO_SET_24B_48K_DI	 0x19 /* 24bits+48KHz+Digital Input */
#define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
#define MAUDIO_SET_16B_48K_DI	 0x11 /* 16bits+48KHz+Digital Input */
#define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */

static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
				      int iface, int altno)
{
	/* Reset ALL ifaces to 0 altsetting.
	 * Call it for every possible altsetting of every interface.
	 */
	usb_set_interface(chip->dev, iface, 0);
	if (chip->setup & MAUDIO_SET) {
		if (chip->setup & MAUDIO_SET_COMPATIBLE) {
			if (iface != 1 && iface != 2)
				return 1; /* skip all interfaces but 1 and 2 */
		} else {
			unsigned int mask;
			if (iface == 1 || iface == 2)
				return 1; /* skip interfaces 1 and 2 */
			if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
				return 1; /* skip this altsetting */
			mask = chip->setup & MAUDIO_SET_MASK;
			if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
				return 1; /* skip this altsetting */
			if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
				return 1; /* skip this altsetting */
			if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
				return 1; /* skip this altsetting */
		}
	}
1044
	usb_audio_dbg(chip,
1045 1046 1047 1048
		    "using altsetting %d for interface %d config %d\n",
		    altno, iface, chip->setup);
	return 0; /* keep this altsetting */
}
Daniel Mack's avatar
Daniel Mack committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058

static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
					 int iface,
					 int altno)
{
	/* Reset ALL ifaces to 0 altsetting.
	 * Call it for every possible altsetting of every interface.
	 */
	usb_set_interface(chip->dev, iface, 0);

1059 1060 1061
	if (chip->setup & MAUDIO_SET) {
		unsigned int mask;
		if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
Daniel Mack's avatar
Daniel Mack committed
1062
			return 1; /* skip this altsetting */
1063
		if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
Daniel Mack's avatar
Daniel Mack committed
1064
			return 1; /* skip this altsetting */
1065 1066
		mask = chip->setup & MAUDIO_SET_MASK;
		if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
Daniel Mack's avatar
Daniel Mack committed
1067
			return 1; /* skip this altsetting */
1068
		if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
Daniel Mack's avatar
Daniel Mack committed
1069
			return 1; /* skip this altsetting */
1070
		if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
Daniel Mack's avatar
Daniel Mack committed
1071
			return 1; /* skip this altsetting */
1072
		if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
Daniel Mack's avatar
Daniel Mack committed
1073 1074 1075 1076 1077 1078
			return 1; /* skip this altsetting */
	}

	return 0; /* keep this altsetting */
}

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
					   int iface, int altno)
{
	/* Reset ALL ifaces to 0 altsetting.
	 * Call it for every possible altsetting of every interface.
	 */
	usb_set_interface(chip->dev, iface, 0);

	/* possible configuration where both inputs and only one output is
	 *used is not supported by the current setup
	 */
	if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
		if (chip->setup & MAUDIO_SET_96K) {
			if (altno != 3 && altno != 6)
				return 1;
		} else if (chip->setup & MAUDIO_SET_DI) {
			if (iface == 4)
				return 1; /* no analog input */
			if (altno != 2 && altno != 5)
				return 1; /* enable only altsets 2 and 5 */
		} else {
			if (iface == 5)
				return 1; /* disable digialt input */
			if (altno != 2 && altno != 5)
				return 1; /* enalbe only altsets 2 and 5 */
		}
	} else {
		/* keep only 16-Bit mode */
		if (altno != 1)
			return 1;
	}

1111
	usb_audio_dbg(chip,
1112 1113 1114 1115 1116
		    "using altsetting %d for interface %d config %d\n",
		    altno, iface, chip->setup);
	return 0; /* keep this altsetting */
}

Daniel Mack's avatar
Daniel Mack committed
1117 1118 1119 1120 1121 1122 1123
int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
				  int iface,
				  int altno)
{
	/* audiophile usb: skip altsets incompatible with device_setup */
	if (chip->usb_id == USB_ID(0x0763, 0x2003))
		return audiophile_skip_setting_quirk(chip, iface, altno);
1124 1125 1126 1127 1128 1129
	/* quattro usb: skip altsets incompatible with device_setup */
	if (chip->usb_id == USB_ID(0x0763, 0x2001))
		return quattro_skip_setting_quirk(chip, iface, altno);
	/* fasttrackpro usb: skip altsets incompatible with device_setup */
	if (chip->usb_id == USB_ID(0x0763, 0x2012))
		return fasttrackpro_skip_setting_quirk(chip, iface, altno);
Daniel Mack's avatar
Daniel Mack committed
1130 1131 1132 1133 1134 1135

	return 0;
}

int snd_usb_apply_boot_quirk(struct usb_device *dev,
			     struct usb_interface *intf,
1136 1137
			     const struct snd_usb_audio_quirk *quirk,
			     unsigned int id)
Daniel Mack's avatar
Daniel Mack committed
1138
{
1139 1140 1141 1142
	switch (id) {
	case USB_ID(0x041e, 0x3000):
		/* SB Extigy needs special boot-up sequence */
		/* if more models come, this will go to the quirk list. */
Daniel Mack's avatar
Daniel Mack committed
1143 1144
		return snd_usb_extigy_boot_quirk(dev, intf);

1145 1146
	case USB_ID(0x041e, 0x3020):
		/* SB Audigy 2 NX needs its own boot-up magic, too */
Daniel Mack's avatar
Daniel Mack committed
1147 1148
		return snd_usb_audigy2nx_boot_quirk(dev);

1149 1150
	case USB_ID(0x10f5, 0x0200):
		/* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
Daniel Mack's avatar
Daniel Mack committed
1151 1152
		return snd_usb_cm106_boot_quirk(dev);

1153 1154
	case USB_ID(0x0d8c, 0x0102):
		/* C-Media CM6206 / CM106-Like Sound Device */
1155
	case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
Daniel Mack's avatar
Daniel Mack committed
1156 1157
		return snd_usb_cm6206_boot_quirk(dev);

1158 1159 1160 1161
	case USB_ID(0x0dba, 0x3000):
		/* Digidesign Mbox 2 */
		return snd_usb_mbox2_boot_quirk(dev);

1162 1163 1164
	case USB_ID(0x1235, 0x0010): /* Focusrite Novation Saffire 6 USB */
	case USB_ID(0x1235, 0x0018): /* Focusrite Novation Twitch */
		return snd_usb_novation_boot_quirk(dev);
1165

1166 1167
	case USB_ID(0x133e, 0x0815):
		/* Access Music VirusTI Desktop */
Daniel Mack's avatar
Daniel Mack committed
1168 1169
		return snd_usb_accessmusic_boot_quirk(dev);

1170
	case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
1171 1172
	case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
	case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
1173
		return snd_usb_nativeinstruments_boot_quirk(dev);
1174 1175
	case USB_ID(0x0763, 0x2012):  /* M-Audio Fast Track Pro USB */
		return snd_usb_fasttrackpro_boot_quirk(dev);
1176 1177
	case USB_ID(0x047f, 0xc010): /* Plantronics Gamecom 780 */
		return snd_usb_gamecon780_boot_quirk(dev);
1178 1179
	case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx 3 */
		return snd_usb_axefx3_boot_quirk(dev);
1180
	}
1181

Daniel Mack's avatar
Daniel Mack committed
1182 1183 1184 1185 1186 1187 1188 1189
	return 0;
}

/*
 * check if the device uses big-endian samples
 */
int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
{
1190
	/* it depends on altsetting whether the device is big-endian or not */
Daniel Mack's avatar
Daniel Mack committed
1191 1192
	switch (chip->usb_id) {
	case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
1193 1194
		if (fp->altsetting == 2 || fp->altsetting == 3 ||
			fp->altsetting == 5 || fp->altsetting == 6)
Daniel Mack's avatar
Daniel Mack committed
1195 1196 1197 1198
			return 1;
		break;
	case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
		if (chip->setup == 0x00 ||
1199 1200 1201 1202 1203 1204 1205
			fp->altsetting == 1 || fp->altsetting == 2 ||
			fp->altsetting == 3)
			return 1;
		break;
	case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
		if (fp->altsetting == 2 || fp->altsetting == 3 ||
			fp->altsetting == 5 || fp->altsetting == 6)
Daniel Mack's avatar
Daniel Mack committed
1206
			return 1;
1207
		break;
Daniel Mack's avatar
Daniel Mack committed
1208 1209 1210 1211 1212
	}
	return 0;
}

/*
1213
 * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
Daniel Mack's avatar
Daniel Mack committed
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
 * not for interface.
 */

enum {
	EMU_QUIRK_SR_44100HZ = 0,
	EMU_QUIRK_SR_48000HZ,
	EMU_QUIRK_SR_88200HZ,
	EMU_QUIRK_SR_96000HZ,
	EMU_QUIRK_SR_176400HZ,
	EMU_QUIRK_SR_192000HZ
};

static void set_format_emu_quirk(struct snd_usb_substream *subs,
				 struct audioformat *fmt)
{
	unsigned char emu_samplerate_id = 0;

	/* When capture is active
	 * sample rate shouldn't be changed
	 * by playback substream
	 */
	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
		if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
			return;
	}

	switch (fmt->rate_min) {
	case 48000:
		emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
		break;
	case 88200:
		emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
		break;
	case 96000:
		emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
		break;
	case 176400:
		emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
		break;
	case 192000:
		emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
		break;
	default:
		emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
		break;
	}
	snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
1261
	subs->pkt_offset_adj = (emu_samplerate_id >= EMU_QUIRK_SR_176400HZ) ? 4 : 0;
Daniel Mack's avatar
Daniel Mack committed
1262 1263 1264 1265 1266 1267 1268 1269 1270
}

void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
			      struct audioformat *fmt)
{
	switch (subs->stream->chip->usb_id) {
	case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
	case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
	case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
1271
	case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
Daniel Mack's avatar
Daniel Mack committed
1272 1273 1274 1275 1276
		set_format_emu_quirk(subs, fmt);
		break;
	}
}

1277 1278
bool snd_usb_get_sample_rate_quirk(struct snd_usb_audio *chip)
{
1279 1280
	/* devices which do not support reading the sample rate. */
	switch (chip->usb_id) {
1281
	case USB_ID(0x041E, 0x4080): /* Creative Live Cam VF0610 */
1282
	case USB_ID(0x04D8, 0xFEEA): /* Benchmark DAC1 Pre */
1283
	case USB_ID(0x0556, 0x0014): /* Phoenix Audio TMX320VC */
1284
	case USB_ID(0x05A3, 0x9420): /* ELP HD USB Camera */
1285
	case USB_ID(0x074D, 0x3553): /* Outlaw RR2150 (Micronas UAC3553B) */
1286
	case USB_ID(0x1395, 0x740a): /* Sennheiser DECT */
1287
	case USB_ID(0x1901, 0x0191): /* GE B850V3 CP2114 audio interface */
1288
	case USB_ID(0x21B4, 0x0081): /* AudioQuest DragonFly */
1289 1290
		return true;
	}
1291 1292 1293 1294 1295 1296 1297 1298 1299

	/* devices of these vendors don't support reading rate, either */
	switch (USB_ID_VENDOR(chip->usb_id)) {
	case 0x045E: /* MS Lifecam */
	case 0x047F: /* Plantronics */
	case 0x1de7: /* Phoenix Audio */
		return true;
	}

1300
	return false;
1301
}
1302

1303
/* ITF-USB DSD based DACs need a vendor cmd to switch
1304 1305
 * between PCM and native DSD mode
 */
1306
static bool is_itf_usb_dsd_dac(unsigned int id)
1307
{
1308
	switch (id) {
1309 1310 1311
	case USB_ID(0x154e, 0x1003): /* Denon DA-300USB */
	case USB_ID(0x154e, 0x3005): /* Marantz HD-DAC1 */
	case USB_ID(0x154e, 0x3006): /* Marantz SA-14S1 */
1312
	case USB_ID(0x1852, 0x5065): /* Luxman DA-06 */
1313
	case USB_ID(0x0644, 0x8043): /* TEAC UD-501/UD-501V2/UD-503/NT-503 */
1314
	case USB_ID(0x0644, 0x8044): /* Esoteric D-05X */
1315
	case USB_ID(0x0644, 0x804a): /* TEAC UD-301 */
1316 1317 1318 1319 1320
		return true;
	}
	return false;
}

1321 1322 1323 1324 1325 1326
int snd_usb_select_mode_quirk(struct snd_usb_substream *subs,
			      struct audioformat *fmt)
{
	struct usb_device *dev = subs->dev;
	int err;

1327
	if (is_itf_usb_dsd_dac(subs->stream->chip->usb_id)) {
1328 1329 1330 1331 1332 1333 1334
		/* First switch to alt set 0, otherwise the mode switch cmd
		 * will not be accepted by the DAC
		 */
		err = usb_set_interface(dev, fmt->iface, 0);
		if (err < 0)
			return err;

1335
		msleep(20); /* Delay needed after setting the interface */
1336

1337
		/* Vendor mode switch cmd is required. */
1338 1339
		if (fmt->formats & SNDRV_PCM_FMTBIT_DSD_U32_BE) {
			/* DSD mode (DSD_U32) requested */
1340 1341 1342 1343 1344 1345
			err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
					      USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
					      1, 1, NULL, 0);
			if (err < 0)
				return err;

1346 1347 1348
		} else {
			/* PCM or DOP mode (S32) requested */
			/* PCM mode (S16) requested */
1349 1350 1351 1352 1353
			err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
					      USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
					      0, 1, NULL, 0);
			if (err < 0)
				return err;
1354

1355
		}
1356
		msleep(20);
1357 1358 1359 1360
	}
	return 0;
}

1361 1362 1363 1364 1365 1366
void snd_usb_endpoint_start_quirk(struct snd_usb_endpoint *ep)
{
	/*
	 * "Playback Design" products send bogus feedback data at the start
	 * of the stream. Ignore them.
	 */
1367
	if (USB_ID_VENDOR(ep->chip->usb_id) == 0x23ba &&
1368 1369
	    ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
		ep->skip_packets = 4;
1370 1371

	/*
1372 1373 1374 1375
	 * M-Audio Fast Track C400/C600 - when packets are not skipped, real
	 * world latency varies by approx. +/- 50 frames (at 96KHz) each time
	 * the stream is (re)started. When skipping packets 16 at endpoint
	 * start up, the real world latency is stable within +/- 1 frame (also
1376 1377
	 * across power cycles).
	 */
1378 1379
	if ((ep->chip->usb_id == USB_ID(0x0763, 0x2030) ||
	     ep->chip->usb_id == USB_ID(0x0763, 0x2031)) &&
1380 1381
	    ep->type == SND_USB_ENDPOINT_TYPE_DATA)
		ep->skip_packets = 16;
1382 1383

	/* Work around devices that report unreasonable feedback data */
1384 1385
	if ((ep->chip->usb_id == USB_ID(0x0644, 0x8038) ||  /* TEAC UD-H01 */
	     ep->chip->usb_id == USB_ID(0x1852, 0x5034)) && /* T+A Dac8 */
1386
	    ep->syncmaxsize == 4)
1387
		ep->tenor_fb_quirk = 1;
1388 1389
}

1390 1391
void snd_usb_set_interface_quirk(struct usb_device *dev)
{
1392 1393 1394 1395
	struct snd_usb_audio *chip = dev_get_drvdata(&dev->dev);

	if (!chip)
		return;
1396 1397 1398 1399
	/*
	 * "Playback Design" products need a 50ms delay after setting the
	 * USB interface.
	 */
1400
	switch (USB_ID_VENDOR(chip->usb_id)) {
1401 1402
	case 0x23ba: /* Playback Design */
	case 0x0644: /* TEAC Corp. */
1403
		msleep(50);
1404 1405
		break;
	}
1406 1407
}

1408
/* quirk applied after snd_usb_ctl_msg(); not applied during boot quirks */
1409 1410 1411 1412
void snd_usb_ctl_msg_quirk(struct usb_device *dev, unsigned int pipe,
			   __u8 request, __u8 requesttype, __u16 value,
			   __u16 index, void *data, __u16 size)
{
1413 1414 1415 1416
	struct snd_usb_audio *chip = dev_get_drvdata(&dev->dev);

	if (!chip)
		return;
1417 1418 1419 1420
	/*
	 * "Playback Design" products need a 20ms delay after each
	 * class compliant request
	 */
1421
	if (USB_ID_VENDOR(chip->usb_id) == 0x23ba &&
1422
	    (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1423
		msleep(20);
1424

1425 1426 1427 1428
	/*
	 * "TEAC Corp." products need a 20ms delay after each
	 * class compliant request
	 */
1429
	if (USB_ID_VENDOR(chip->usb_id) == 0x0644 &&
1430
	    (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1431
		msleep(20);
1432

1433
	/* ITF-USB DSD based DACs functionality need a delay
1434 1435
	 * after each class compliant request
	 */
1436
	if (is_itf_usb_dsd_dac(chip->usb_id)
1437
	    && (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1438
		msleep(20);
1439

1440 1441 1442
	/* Zoom R16/24, Logitech H650e, Jabra 550a needs a tiny delay here,
	 * otherwise requests like get/set frequency return as failed despite
	 * actually succeeding.
1443
	 */
1444 1445 1446
	if ((chip->usb_id == USB_ID(0x1686, 0x00dd) ||
	     chip->usb_id == USB_ID(0x046d, 0x0a46) ||
	     chip->usb_id == USB_ID(0x0b0e, 0x0349)) &&
1447
	    (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1448
		usleep_range(1000, 2000);
1449 1450
}

1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
/*
 * snd_usb_interface_dsd_format_quirks() is called from format.c to
 * augment the PCM format bit-field for DSD types. The UAC standards
 * don't have a designated bit field to denote DSD-capable interfaces,
 * hence all hardware that is known to support this format has to be
 * listed here.
 */
u64 snd_usb_interface_dsd_format_quirks(struct snd_usb_audio *chip,
					struct audioformat *fp,
					unsigned int sample_bytes)
{
1462 1463
	struct usb_interface *iface;

1464
	/* Playback Designs */
1465
	if (USB_ID_VENDOR(chip->usb_id) == 0x23ba) {
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
		switch (fp->altsetting) {
		case 1:
			fp->dsd_dop = true;
			return SNDRV_PCM_FMTBIT_DSD_U16_LE;
		case 2:
			fp->dsd_bitrev = true;
			return SNDRV_PCM_FMTBIT_DSD_U8;
		case 3:
			fp->dsd_bitrev = true;
			return SNDRV_PCM_FMTBIT_DSD_U16_LE;
		}
	}

1479 1480
	/* XMOS based USB DACs */
	switch (chip->usb_id) {
1481 1482 1483
	case USB_ID(0x1511, 0x0037): /* AURALiC VEGA */
	case USB_ID(0x20b1, 0x0002): /* Wyred 4 Sound DAC-2 DSD */
	case USB_ID(0x20b1, 0x2004): /* Matrix Audio X-SPDIF 2 */
1484 1485
	case USB_ID(0x20b1, 0x2008): /* Matrix Audio X-Sabre */
	case USB_ID(0x20b1, 0x300a): /* Matrix Audio Mini-i Pro */
1486
	case USB_ID(0x22d9, 0x0416): /* OPPO HA-1 */
1487 1488 1489
	case USB_ID(0x22d9, 0x0436): /* OPPO Sonica */
	case USB_ID(0x22d9, 0x0461): /* OPPO UDP-205 */
	case USB_ID(0x2522, 0x0012): /* LH Labs VI DAC Infinity */
1490
	case USB_ID(0x2772, 0x0230): /* Pro-Ject Pre Box S2 Digital */
1491
		if (fp->altsetting == 2)
1492
			return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1493
		break;
1494

1495
	case USB_ID(0x10cb, 0x0103): /* The Bit Opus #3; with fp->dsd_raw */
1496
	case USB_ID(0x152a, 0x85de): /* SMSL D1 DAC */
1497
	case USB_ID(0x16d0, 0x09dd): /* Encore mDSD */
1498 1499 1500 1501 1502
	case USB_ID(0x0d8c, 0x0316): /* Hegel HD12 DSD */
	case USB_ID(0x16b0, 0x06b2): /* NuPrime DAC-10 */
	case USB_ID(0x16d0, 0x0733): /* Furutech ADL Stratos */
	case USB_ID(0x16d0, 0x09db): /* NuPrime Audio DAC-9 */
	case USB_ID(0x1db5, 0x0003): /* Bryston BDA3 */
1503
	case USB_ID(0x20b1, 0x000a): /* Gustard DAC-X20U */
1504
	case USB_ID(0x20b1, 0x2005): /* Denafrips Ares DAC */
1505 1506
	case USB_ID(0x20b1, 0x2009): /* DIYINHK DSD DXD 384kHz USB to I2S/DSD */
	case USB_ID(0x20b1, 0x2023): /* JLsounds I2SoverUSB */
1507
	case USB_ID(0x20b1, 0x3021): /* Eastern El. MiniMax Tube DAC Supreme */
1508
	case USB_ID(0x20b1, 0x3023): /* Aune X1S 32BIT/384 DSD DAC */
1509 1510 1511 1512 1513 1514
	case USB_ID(0x20b1, 0x302d): /* Unison Research Unico CD Due */
	case USB_ID(0x20b1, 0x307b): /* CH Precision C1 DAC */
	case USB_ID(0x20b1, 0x3086): /* Singxer F-1 converter board */
	case USB_ID(0x22d9, 0x0426): /* OPPO HA-2 */
	case USB_ID(0x22e1, 0xca01): /* HDTA Serenade DSD */
	case USB_ID(0x249c, 0x9326): /* M2Tech Young MkIII */
1515
	case USB_ID(0x2616, 0x0106): /* PS Audio NuWave DAC */
1516 1517 1518 1519
	case USB_ID(0x2622, 0x0041): /* Audiolab M-DAC+ */
	case USB_ID(0x27f7, 0x3002): /* W4S DAC-2v2SE */
	case USB_ID(0x29a2, 0x0086): /* Mutec MC3+ USB */
	case USB_ID(0x6b42, 0x0042): /* MSB Technology */
1520
		if (fp->altsetting == 3)
1521
			return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1522
		break;
1523

1524 1525 1526 1527 1528
	/* Amanero Combo384 USB based DACs with native DSD support */
	case USB_ID(0x16d0, 0x071a):  /* Amanero - Combo384 */
	case USB_ID(0x2ab6, 0x0004):  /* T+A DAC8DSD-V2.0, MP1000E-V2.0, MP2000R-V2.0, MP2500R-V2.0, MP3100HV-V2.0 */
	case USB_ID(0x2ab6, 0x0005):  /* T+A USB HD Audio 1 */
	case USB_ID(0x2ab6, 0x0006):  /* T+A USB HD Audio 2 */
1529
		if (fp->altsetting == 2) {
1530
			switch (le16_to_cpu(chip->dev->descriptor.bcdDevice)) {
1531 1532 1533
			case 0x199:
				return SNDRV_PCM_FMTBIT_DSD_U32_LE;
			case 0x19b:
1534
			case 0x203:
1535 1536 1537 1538 1539 1540
				return SNDRV_PCM_FMTBIT_DSD_U32_BE;
			default:
				break;
			}
		}
		break;
1541 1542 1543 1544
	case USB_ID(0x16d0, 0x0a23):
		if (fp->altsetting == 2)
			return SNDRV_PCM_FMTBIT_DSD_U32_BE;
		break;
1545

1546 1547 1548 1549
	default:
		break;
	}

1550 1551 1552
	/* ITF-USB DSD based DACs */
	if (is_itf_usb_dsd_dac(chip->usb_id)) {
		iface = usb_ifnum_to_if(chip->dev, fp->iface);
1553

1554 1555 1556 1557 1558 1559
		/* Altsetting 2 support native DSD if the num of altsets is
		 * three (0-2),
		 * Altsetting 3 support native DSD if the num of altsets is
		 * four (0-3).
		 */
		if (fp->altsetting == iface->num_altsetting - 1)
1560 1561 1562
			return SNDRV_PCM_FMTBIT_DSD_U32_BE;
	}

1563 1564 1565 1566
	/* Mostly generic method to detect many DSD-capable implementations -
	 * from XMOS/Thesycon
	 */
	switch (USB_ID_VENDOR(chip->usb_id)) {
1567
	case 0x20b1:  /* XMOS based devices */
1568
	case 0x152a:  /* Thesycon devices */
1569 1570 1571 1572 1573 1574 1575 1576 1577
	case 0x25ce:  /* Mytek devices */
		if (fp->dsd_raw)
			return SNDRV_PCM_FMTBIT_DSD_U32_BE;
		break;
	default:
		break;

	}

1578 1579
	return 0;
}
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613

void snd_usb_audioformat_attributes_quirk(struct snd_usb_audio *chip,
					  struct audioformat *fp,
					  int stream)
{
	switch (chip->usb_id) {
	case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
		/* Optoplay sets the sample rate attribute although
		 * it seems not supporting it in fact.
		 */
		fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
		break;
	case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
	case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
		/* doesn't set the sample rate attribute, but supports it */
		fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
		break;
	case USB_ID(0x0763, 0x2001):  /* M-Audio Quattro USB */
	case USB_ID(0x0763, 0x2012):  /* M-Audio Fast Track Pro USB */
	case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
	case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
					an older model 77d:223) */
	/*
	 * plantronics headset and Griffin iMic have set adaptive-in
	 * although it's really not...
	 */
		fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
		if (stream == SNDRV_PCM_STREAM_PLAYBACK)
			fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
		else
			fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
		break;
	}
}