blob: c89ced72b8bdab8e08ef01206ab1e16343d26974 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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 */
32static 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
69static 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 */
82static 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
91static 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 */
102static 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 */
130static 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
195static 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
237static 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
269static 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
293static 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
321static 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
357static 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
369static 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 */
412static 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 */
508static 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
520static 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 */
537int 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
585static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
586{
587 struct usb_host_config *config = dev->actconfig;
588 struct usb_device_descriptor *new_device_descriptor = NULL;
589 int err;
590
591 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
592 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
593 dev_dbg(&dev->dev, "sending Extigy boot sequence...\n");
594 /* Send message to force it to reconnect with full interface. */
595 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
596 0x10, 0x43, 0x0001, 0x000a, NULL, 0);
597 if (err < 0)
598 dev_dbg(&dev->dev, "error sending boot message: %d\n", err);
599
600 new_device_descriptor = kmalloc(sizeof(*new_device_descriptor), GFP_KERNEL);
601 if (!new_device_descriptor)
602 return -ENOMEM;
603 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
604 new_device_descriptor, sizeof(*new_device_descriptor));
605 if (err < 0)
606 dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
607 if (new_device_descriptor->bNumConfigurations > dev->descriptor.bNumConfigurations)
608 dev_dbg(&dev->dev, "error too large bNumConfigurations: %d\n",
609 new_device_descriptor->bNumConfigurations);
610 else
611 memcpy(&dev->descriptor, new_device_descriptor, sizeof(dev->descriptor));
612 kfree(new_device_descriptor);
613 err = usb_reset_configuration(dev);
614 if (err < 0)
615 dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
616 dev_dbg(&dev->dev, "extigy_boot: new boot length = %d\n",
617 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
618 return -ENODEV; /* quit this anyway */
619 }
620 return 0;
621}
622
623static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
624{
625 u8 buf = 1;
626
627 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
628 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
629 0, 0, &buf, 1);
630 if (buf == 0) {
631 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
632 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
633 1, 2000, NULL, 0);
634 return -ENODEV;
635 }
636 return 0;
637}
638
639static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
640{
641 int err;
642
643 if (dev->actconfig->desc.bConfigurationValue == 1) {
644 dev_info(&dev->dev,
645 "Fast Track Pro switching to config #2\n");
646 /* This function has to be available by the usb core module.
647 * if it is not avialable the boot quirk has to be left out
648 * and the configuration has to be set by udev or hotplug
649 * rules
650 */
651 err = usb_driver_set_configuration(dev, 2);
652 if (err < 0)
653 dev_dbg(&dev->dev,
654 "error usb_driver_set_configuration: %d\n",
655 err);
656 /* Always return an error, so that we stop creating a device
657 that will just be destroyed and recreated with a new
658 configuration */
659 return -ENODEV;
660 } else
661 dev_info(&dev->dev, "Fast Track Pro config OK\n");
662
663 return 0;
664}
665
666/*
667 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
668 * documented in the device's data sheet.
669 */
670static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
671{
672 u8 buf[4];
673 buf[0] = 0x20;
674 buf[1] = value & 0xff;
675 buf[2] = (value >> 8) & 0xff;
676 buf[3] = reg;
677 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
678 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
679 0, 0, &buf, 4);
680}
681
682static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
683{
684 /*
685 * Enable line-out driver mode, set headphone source to front
686 * channels, enable stereo mic.
687 */
688 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
689}
690
691/*
692 * CM6206 registers from the CM6206 datasheet rev 2.1
693 */
694#define CM6206_REG0_DMA_MASTER BIT(15)
695#define CM6206_REG0_SPDIFO_RATE_48K (2 << 12)
696#define CM6206_REG0_SPDIFO_RATE_96K (7 << 12)
697/* Bit 4 thru 11 is the S/PDIF category code */
698#define CM6206_REG0_SPDIFO_CAT_CODE_GENERAL (0 << 4)
699#define CM6206_REG0_SPDIFO_EMPHASIS_CD BIT(3)
700#define CM6206_REG0_SPDIFO_COPYRIGHT_NA BIT(2)
701#define CM6206_REG0_SPDIFO_NON_AUDIO BIT(1)
702#define CM6206_REG0_SPDIFO_PRO_FORMAT BIT(0)
703
704#define CM6206_REG1_TEST_SEL_CLK BIT(14)
705#define CM6206_REG1_PLLBIN_EN BIT(13)
706#define CM6206_REG1_SOFT_MUTE_EN BIT(12)
707#define CM6206_REG1_GPIO4_OUT BIT(11)
708#define CM6206_REG1_GPIO4_OE BIT(10)
709#define CM6206_REG1_GPIO3_OUT BIT(9)
710#define CM6206_REG1_GPIO3_OE BIT(8)
711#define CM6206_REG1_GPIO2_OUT BIT(7)
712#define CM6206_REG1_GPIO2_OE BIT(6)
713#define CM6206_REG1_GPIO1_OUT BIT(5)
714#define CM6206_REG1_GPIO1_OE BIT(4)
715#define CM6206_REG1_SPDIFO_INVALID BIT(3)
716#define CM6206_REG1_SPDIF_LOOP_EN BIT(2)
717#define CM6206_REG1_SPDIFO_DIS BIT(1)
718#define CM6206_REG1_SPDIFI_MIX BIT(0)
719
720#define CM6206_REG2_DRIVER_ON BIT(15)
721#define CM6206_REG2_HEADP_SEL_SIDE_CHANNELS (0 << 13)
722#define CM6206_REG2_HEADP_SEL_SURROUND_CHANNELS (1 << 13)
723#define CM6206_REG2_HEADP_SEL_CENTER_SUBW (2 << 13)
724#define CM6206_REG2_HEADP_SEL_FRONT_CHANNELS (3 << 13)
725#define CM6206_REG2_MUTE_HEADPHONE_RIGHT BIT(12)
726#define CM6206_REG2_MUTE_HEADPHONE_LEFT BIT(11)
727#define CM6206_REG2_MUTE_REAR_SURROUND_RIGHT BIT(10)
728#define CM6206_REG2_MUTE_REAR_SURROUND_LEFT BIT(9)
729#define CM6206_REG2_MUTE_SIDE_SURROUND_RIGHT BIT(8)
730#define CM6206_REG2_MUTE_SIDE_SURROUND_LEFT BIT(7)
731#define CM6206_REG2_MUTE_SUBWOOFER BIT(6)
732#define CM6206_REG2_MUTE_CENTER BIT(5)
733#define CM6206_REG2_MUTE_RIGHT_FRONT BIT(3)
734#define CM6206_REG2_MUTE_LEFT_FRONT BIT(3)
735#define CM6206_REG2_EN_BTL BIT(2)
736#define CM6206_REG2_MCUCLKSEL_1_5_MHZ (0)
737#define CM6206_REG2_MCUCLKSEL_3_MHZ (1)
738#define CM6206_REG2_MCUCLKSEL_6_MHZ (2)
739#define CM6206_REG2_MCUCLKSEL_12_MHZ (3)
740
741/* Bit 11..13 sets the sensitivity to FLY tuner volume control VP/VD signal */
742#define CM6206_REG3_FLYSPEED_DEFAULT (2 << 11)
743#define CM6206_REG3_VRAP25EN BIT(10)
744#define CM6206_REG3_MSEL1 BIT(9)
745#define CM6206_REG3_SPDIFI_RATE_44_1K BIT(0 << 7)
746#define CM6206_REG3_SPDIFI_RATE_48K BIT(2 << 7)
747#define CM6206_REG3_SPDIFI_RATE_32K BIT(3 << 7)
748#define CM6206_REG3_PINSEL BIT(6)
749#define CM6206_REG3_FOE BIT(5)
750#define CM6206_REG3_ROE BIT(4)
751#define CM6206_REG3_CBOE BIT(3)
752#define CM6206_REG3_LOSE BIT(2)
753#define CM6206_REG3_HPOE BIT(1)
754#define CM6206_REG3_SPDIFI_CANREC BIT(0)
755
756#define CM6206_REG5_DA_RSTN BIT(13)
757#define CM6206_REG5_AD_RSTN BIT(12)
758#define CM6206_REG5_SPDIFO_AD2SPDO BIT(12)
759#define CM6206_REG5_SPDIFO_SEL_FRONT (0 << 9)
760#define CM6206_REG5_SPDIFO_SEL_SIDE_SUR (1 << 9)
761#define CM6206_REG5_SPDIFO_SEL_CEN_LFE (2 << 9)
762#define CM6206_REG5_SPDIFO_SEL_REAR_SUR (3 << 9)
763#define CM6206_REG5_CODECM BIT(8)
764#define CM6206_REG5_EN_HPF BIT(7)
765#define CM6206_REG5_T_SEL_DSDA4 BIT(6)
766#define CM6206_REG5_T_SEL_DSDA3 BIT(5)
767#define CM6206_REG5_T_SEL_DSDA2 BIT(4)
768#define CM6206_REG5_T_SEL_DSDA1 BIT(3)
769#define CM6206_REG5_T_SEL_DSDAD_NORMAL 0
770#define CM6206_REG5_T_SEL_DSDAD_FRONT 4
771#define CM6206_REG5_T_SEL_DSDAD_S_SURROUND 5
772#define CM6206_REG5_T_SEL_DSDAD_CEN_LFE 6
773#define CM6206_REG5_T_SEL_DSDAD_R_SURROUND 7
774
775static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
776{
777 int err = 0, reg;
778 int val[] = {
779 /*
780 * Values here are chosen based on sniffing USB traffic
781 * under Windows.
782 *
783 * REG0: DAC is master, sample rate 48kHz, no copyright
784 */
785 CM6206_REG0_SPDIFO_RATE_48K |
786 CM6206_REG0_SPDIFO_COPYRIGHT_NA,
787 /*
788 * REG1: PLL binary search enable, soft mute enable.
789 */
790 CM6206_REG1_PLLBIN_EN |
791 CM6206_REG1_SOFT_MUTE_EN,
792 /*
793 * REG2: enable output drivers,
794 * select front channels to the headphone output,
795 * then mute the headphone channels, run the MCU
796 * at 1.5 MHz.
797 */
798 CM6206_REG2_DRIVER_ON |
799 CM6206_REG2_HEADP_SEL_FRONT_CHANNELS |
800 CM6206_REG2_MUTE_HEADPHONE_RIGHT |
801 CM6206_REG2_MUTE_HEADPHONE_LEFT,
802 /*
803 * REG3: default flyspeed, set 2.5V mic bias
804 * enable all line out ports and enable SPDIF
805 */
806 CM6206_REG3_FLYSPEED_DEFAULT |
807 CM6206_REG3_VRAP25EN |
808 CM6206_REG3_FOE |
809 CM6206_REG3_ROE |
810 CM6206_REG3_CBOE |
811 CM6206_REG3_LOSE |
812 CM6206_REG3_HPOE |
813 CM6206_REG3_SPDIFI_CANREC,
814 /* REG4 is just a bunch of GPIO lines */
815 0x0000,
816 /* REG5: de-assert AD/DA reset signals */
817 CM6206_REG5_DA_RSTN |
818 CM6206_REG5_AD_RSTN };
819
820 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
821 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
822 if (err < 0)
823 return err;
824 }
825
826 return err;
827}
828
829/* quirk for Plantronics GameCom 780 with CM6302 chip */
830static int snd_usb_gamecon780_boot_quirk(struct usb_device *dev)
831{
832 /* set the initial volume and don't change; other values are either
833 * too loud or silent due to firmware bug (bko#65251)
834 */
835 u8 buf[2] = { 0x74, 0xe3 };
836 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
837 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
838 UAC_FU_VOLUME << 8, 9 << 8, buf, 2);
839}
840
841/*
842 * Novation Twitch DJ controller
843 * Focusrite Novation Saffire 6 USB audio card
844 */
845static int snd_usb_novation_boot_quirk(struct usb_device *dev)
846{
847 /* preemptively set up the device because otherwise the
848 * raw MIDI endpoints are not active */
849 usb_set_interface(dev, 0, 1);
850 return 0;
851}
852
853/*
854 * This call will put the synth in "USB send" mode, i.e it will send MIDI
855 * messages through USB (this is disabled at startup). The synth will
856 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
857 * sign on its LCD. Values here are chosen based on sniffing USB traffic
858 * under Windows.
859 */
860static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
861{
862 int err, actual_length;
863 /* "midi send" enable */
864 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
865 void *buf;
866
867 if (usb_pipe_type_check(dev, usb_sndintpipe(dev, 0x05)))
868 return -EINVAL;
869 buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
870 if (!buf)
871 return -ENOMEM;
872 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
873 ARRAY_SIZE(seq), &actual_length, 1000);
874 kfree(buf);
875 if (err < 0)
876 return err;
877
878 return 0;
879}
880
881/*
882 * Some sound cards from Native Instruments are in fact compliant to the USB
883 * audio standard of version 2 and other approved USB standards, even though
884 * they come up as vendor-specific device when first connected.
885 *
886 * However, they can be told to come up with a new set of descriptors
887 * upon their next enumeration, and the interfaces announced by the new
888 * descriptors will then be handled by the kernel's class drivers. As the
889 * product ID will also change, no further checks are required.
890 */
891
892static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
893{
894 int ret;
895
896 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
897 return -EINVAL;
898 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
899 0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
900 1, 0, NULL, 0, 1000);
901
902 if (ret < 0)
903 return ret;
904
905 usb_reset_device(dev);
906
907 /* return -EAGAIN, so the creation of an audio interface for this
908 * temporary device is aborted. The device will reconnect with a
909 * new product ID */
910 return -EAGAIN;
911}
912
913static void mbox2_setup_48_24_magic(struct usb_device *dev)
914{
915 u8 srate[3];
916 u8 temp[12];
917
918 /* Choose 48000Hz permanently */
919 srate[0] = 0x80;
920 srate[1] = 0xbb;
921 srate[2] = 0x00;
922
923 /* Send the magic! */
924 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
925 0x01, 0x22, 0x0100, 0x0085, &temp, 0x0003);
926 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
927 0x81, 0xa2, 0x0100, 0x0085, &srate, 0x0003);
928 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
929 0x81, 0xa2, 0x0100, 0x0086, &srate, 0x0003);
930 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
931 0x81, 0xa2, 0x0100, 0x0003, &srate, 0x0003);
932 return;
933}
934
935/* Digidesign Mbox 2 needs to load firmware onboard
936 * and driver must wait a few seconds for initialisation.
937 */
938
939#define MBOX2_FIRMWARE_SIZE 646
940#define MBOX2_BOOT_LOADING 0x01 /* Hard coded into the device */
941#define MBOX2_BOOT_READY 0x02 /* Hard coded into the device */
942
943static int snd_usb_mbox2_boot_quirk(struct usb_device *dev)
944{
945 struct usb_host_config *config = dev->actconfig;
946 struct usb_device_descriptor *new_device_descriptor = NULL;
947 int err;
948 u8 bootresponse[0x12];
949 int fwsize;
950 int count;
951
952 fwsize = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
953
954 if (fwsize != MBOX2_FIRMWARE_SIZE) {
955 dev_err(&dev->dev, "Invalid firmware size=%d.\n", fwsize);
956 return -ENODEV;
957 }
958
959 dev_dbg(&dev->dev, "Sending Digidesign Mbox 2 boot sequence...\n");
960
961 count = 0;
962 bootresponse[0] = MBOX2_BOOT_LOADING;
963 while ((bootresponse[0] == MBOX2_BOOT_LOADING) && (count < 10)) {
964 msleep(500); /* 0.5 second delay */
965 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
966 /* Control magic - load onboard firmware */
967 0x85, 0xc0, 0x0001, 0x0000, &bootresponse, 0x0012);
968 if (bootresponse[0] == MBOX2_BOOT_READY)
969 break;
970 dev_dbg(&dev->dev, "device not ready, resending boot sequence...\n");
971 count++;
972 }
973
974 if (bootresponse[0] != MBOX2_BOOT_READY) {
975 dev_err(&dev->dev, "Unknown bootresponse=%d, or timed out, ignoring device.\n", bootresponse[0]);
976 return -ENODEV;
977 }
978
979 dev_dbg(&dev->dev, "device initialised!\n");
980
981 new_device_descriptor = kmalloc(sizeof(*new_device_descriptor), GFP_KERNEL);
982 if (!new_device_descriptor)
983 return -ENOMEM;
984
985 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
986 new_device_descriptor, sizeof(*new_device_descriptor));
987 if (err < 0)
988 dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
989 if (new_device_descriptor->bNumConfigurations > dev->descriptor.bNumConfigurations)
990 dev_dbg(&dev->dev, "error too large bNumConfigurations: %d\n",
991 new_device_descriptor->bNumConfigurations);
992 else
993 memcpy(&dev->descriptor, new_device_descriptor, sizeof(dev->descriptor));
994
995 kfree(new_device_descriptor);
996
997 err = usb_reset_configuration(dev);
998 if (err < 0)
999 dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
1000 dev_dbg(&dev->dev, "mbox2_boot: new boot length = %d\n",
1001 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
1002
1003 mbox2_setup_48_24_magic(dev);
1004
1005 dev_info(&dev->dev, "Digidesign Mbox 2: 24bit 48kHz");
1006
1007 return 0; /* Successful boot */
1008}
1009
1010static int snd_usb_axefx3_boot_quirk(struct usb_device *dev)
1011{
1012 int err;
1013
1014 dev_dbg(&dev->dev, "Waiting for Axe-Fx III to boot up...\n");
1015
1016 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
1017 return -EINVAL;
1018 /* If the Axe-Fx III has not fully booted, it will timeout when trying
1019 * to enable the audio streaming interface. A more generous timeout is
1020 * used here to detect when the Axe-Fx III has finished booting as the
1021 * set interface message will be acked once it has
1022 */
1023 err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1024 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1025 1, 1, NULL, 0, 120000);
1026 if (err < 0) {
1027 dev_err(&dev->dev,
1028 "failed waiting for Axe-Fx III to boot: %d\n", err);
1029 return err;
1030 }
1031
1032 dev_dbg(&dev->dev, "Axe-Fx III is now ready\n");
1033
1034 err = usb_set_interface(dev, 1, 0);
1035 if (err < 0)
1036 dev_dbg(&dev->dev,
1037 "error stopping Axe-Fx III interface: %d\n", err);
1038
1039 return 0;
1040}
1041
1042#define MICROBOOK_BUF_SIZE 128
1043
1044static int snd_usb_motu_microbookii_communicate(struct usb_device *dev, u8 *buf,
1045 int buf_size, int *length)
1046{
1047 int err, actual_length;
1048
1049 if (usb_pipe_type_check(dev, usb_sndintpipe(dev, 0x01)))
1050 return -EINVAL;
1051 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x01), buf, *length,
1052 &actual_length, 1000);
1053 if (err < 0)
1054 return err;
1055
1056 print_hex_dump(KERN_DEBUG, "MicroBookII snd: ", DUMP_PREFIX_NONE, 16, 1,
1057 buf, actual_length, false);
1058
1059 memset(buf, 0, buf_size);
1060
1061 if (usb_pipe_type_check(dev, usb_rcvintpipe(dev, 0x82)))
1062 return -EINVAL;
1063 err = usb_interrupt_msg(dev, usb_rcvintpipe(dev, 0x82), buf, buf_size,
1064 &actual_length, 1000);
1065 if (err < 0)
1066 return err;
1067
1068 print_hex_dump(KERN_DEBUG, "MicroBookII rcv: ", DUMP_PREFIX_NONE, 16, 1,
1069 buf, actual_length, false);
1070
1071 *length = actual_length;
1072 return 0;
1073}
1074
1075static int snd_usb_motu_microbookii_boot_quirk(struct usb_device *dev)
1076{
1077 int err, actual_length, poll_attempts = 0;
1078 static const u8 set_samplerate_seq[] = { 0x00, 0x00, 0x00, 0x00,
1079 0x00, 0x00, 0x0b, 0x14,
1080 0x00, 0x00, 0x00, 0x01 };
1081 static const u8 poll_ready_seq[] = { 0x00, 0x04, 0x00, 0x00,
1082 0x00, 0x00, 0x0b, 0x18 };
1083 u8 *buf = kzalloc(MICROBOOK_BUF_SIZE, GFP_KERNEL);
1084
1085 if (!buf)
1086 return -ENOMEM;
1087
1088 dev_info(&dev->dev, "Waiting for MOTU Microbook II to boot up...\n");
1089
1090 /* First we tell the device which sample rate to use. */
1091 memcpy(buf, set_samplerate_seq, sizeof(set_samplerate_seq));
1092 actual_length = sizeof(set_samplerate_seq);
1093 err = snd_usb_motu_microbookii_communicate(dev, buf, MICROBOOK_BUF_SIZE,
1094 &actual_length);
1095
1096 if (err < 0) {
1097 dev_err(&dev->dev,
1098 "failed setting the sample rate for Motu MicroBook II: %d\n",
1099 err);
1100 goto free_buf;
1101 }
1102
1103 /* Then we poll every 100 ms until the device informs of its readiness. */
1104 while (true) {
1105 if (++poll_attempts > 100) {
1106 dev_err(&dev->dev,
1107 "failed booting Motu MicroBook II: timeout\n");
1108 err = -ENODEV;
1109 goto free_buf;
1110 }
1111
1112 memset(buf, 0, MICROBOOK_BUF_SIZE);
1113 memcpy(buf, poll_ready_seq, sizeof(poll_ready_seq));
1114
1115 actual_length = sizeof(poll_ready_seq);
1116 err = snd_usb_motu_microbookii_communicate(
1117 dev, buf, MICROBOOK_BUF_SIZE, &actual_length);
1118 if (err < 0) {
1119 dev_err(&dev->dev,
1120 "failed booting Motu MicroBook II: communication error %d\n",
1121 err);
1122 goto free_buf;
1123 }
1124
1125 /* the device signals its readiness through a message of the
1126 * form
1127 * XX 06 00 00 00 00 0b 18 00 00 00 01
1128 * If the device is not yet ready to accept audio data, the
1129 * last byte of that sequence is 00.
1130 */
1131 if (actual_length == 12 && buf[actual_length - 1] == 1)
1132 break;
1133
1134 msleep(100);
1135 }
1136
1137 dev_info(&dev->dev, "MOTU MicroBook II ready\n");
1138
1139free_buf:
1140 kfree(buf);
1141 return err;
1142}
1143
1144static int snd_usb_motu_m_series_boot_quirk(struct usb_device *dev)
1145{
1146 int ret;
1147
1148 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
1149 return -EINVAL;
1150 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1151 1, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1152 0x0, 0, NULL, 0, 1000);
1153
1154 if (ret < 0)
1155 return ret;
1156
1157 msleep(2000);
1158
1159 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1160 1, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1161 0x20, 0, NULL, 0, 1000);
1162
1163 if (ret < 0)
1164 return ret;
1165
1166 return 0;
1167}
1168
1169/*
1170 * Setup quirks
1171 */
1172#define MAUDIO_SET 0x01 /* parse device_setup */
1173#define MAUDIO_SET_COMPATIBLE 0x80 /* use only "win-compatible" interfaces */
1174#define MAUDIO_SET_DTS 0x02 /* enable DTS Digital Output */
1175#define MAUDIO_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
1176#define MAUDIO_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
1177#define MAUDIO_SET_DI 0x10 /* enable Digital Input */
1178#define MAUDIO_SET_MASK 0x1f /* bit mask for setup value */
1179#define MAUDIO_SET_24B_48K_DI 0x19 /* 24bits+48KHz+Digital Input */
1180#define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
1181#define MAUDIO_SET_16B_48K_DI 0x11 /* 16bits+48KHz+Digital Input */
1182#define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
1183
1184static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
1185 int iface, int altno)
1186{
1187 /* Reset ALL ifaces to 0 altsetting.
1188 * Call it for every possible altsetting of every interface.
1189 */
1190 usb_set_interface(chip->dev, iface, 0);
1191 if (chip->setup & MAUDIO_SET) {
1192 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
1193 if (iface != 1 && iface != 2)
1194 return 1; /* skip all interfaces but 1 and 2 */
1195 } else {
1196 unsigned int mask;
1197 if (iface == 1 || iface == 2)
1198 return 1; /* skip interfaces 1 and 2 */
1199 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
1200 return 1; /* skip this altsetting */
1201 mask = chip->setup & MAUDIO_SET_MASK;
1202 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
1203 return 1; /* skip this altsetting */
1204 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
1205 return 1; /* skip this altsetting */
1206 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
1207 return 1; /* skip this altsetting */
1208 }
1209 }
1210 usb_audio_dbg(chip,
1211 "using altsetting %d for interface %d config %d\n",
1212 altno, iface, chip->setup);
1213 return 0; /* keep this altsetting */
1214}
1215
1216static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
1217 int iface,
1218 int altno)
1219{
1220 /* Reset ALL ifaces to 0 altsetting.
1221 * Call it for every possible altsetting of every interface.
1222 */
1223 usb_set_interface(chip->dev, iface, 0);
1224
1225 if (chip->setup & MAUDIO_SET) {
1226 unsigned int mask;
1227 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
1228 return 1; /* skip this altsetting */
1229 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
1230 return 1; /* skip this altsetting */
1231 mask = chip->setup & MAUDIO_SET_MASK;
1232 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
1233 return 1; /* skip this altsetting */
1234 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
1235 return 1; /* skip this altsetting */
1236 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
1237 return 1; /* skip this altsetting */
1238 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
1239 return 1; /* skip this altsetting */
1240 }
1241
1242 return 0; /* keep this altsetting */
1243}
1244
1245static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
1246 int iface, int altno)
1247{
1248 /* Reset ALL ifaces to 0 altsetting.
1249 * Call it for every possible altsetting of every interface.
1250 */
1251 usb_set_interface(chip->dev, iface, 0);
1252
1253 /* possible configuration where both inputs and only one output is
1254 *used is not supported by the current setup
1255 */
1256 if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
1257 if (chip->setup & MAUDIO_SET_96K) {
1258 if (altno != 3 && altno != 6)
1259 return 1;
1260 } else if (chip->setup & MAUDIO_SET_DI) {
1261 if (iface == 4)
1262 return 1; /* no analog input */
1263 if (altno != 2 && altno != 5)
1264 return 1; /* enable only altsets 2 and 5 */
1265 } else {
1266 if (iface == 5)
1267 return 1; /* disable digialt input */
1268 if (altno != 2 && altno != 5)
1269 return 1; /* enalbe only altsets 2 and 5 */
1270 }
1271 } else {
1272 /* keep only 16-Bit mode */
1273 if (altno != 1)
1274 return 1;
1275 }
1276
1277 usb_audio_dbg(chip,
1278 "using altsetting %d for interface %d config %d\n",
1279 altno, iface, chip->setup);
1280 return 0; /* keep this altsetting */
1281}
1282
1283int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
1284 int iface,
1285 int altno)
1286{
1287 /* audiophile usb: skip altsets incompatible with device_setup */
1288 if (chip->usb_id == USB_ID(0x0763, 0x2003))
1289 return audiophile_skip_setting_quirk(chip, iface, altno);
1290 /* quattro usb: skip altsets incompatible with device_setup */
1291 if (chip->usb_id == USB_ID(0x0763, 0x2001))
1292 return quattro_skip_setting_quirk(chip, iface, altno);
1293 /* fasttrackpro usb: skip altsets incompatible with device_setup */
1294 if (chip->usb_id == USB_ID(0x0763, 0x2012))
1295 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
1296
1297 return 0;
1298}
1299
1300int snd_usb_apply_boot_quirk(struct usb_device *dev,
1301 struct usb_interface *intf,
1302 const struct snd_usb_audio_quirk *quirk,
1303 unsigned int id)
1304{
1305 switch (id) {
1306 case USB_ID(0x041e, 0x3000):
1307 /* SB Extigy needs special boot-up sequence */
1308 /* if more models come, this will go to the quirk list. */
1309 return snd_usb_extigy_boot_quirk(dev, intf);
1310
1311 case USB_ID(0x041e, 0x3020):
1312 /* SB Audigy 2 NX needs its own boot-up magic, too */
1313 return snd_usb_audigy2nx_boot_quirk(dev);
1314
1315 case USB_ID(0x10f5, 0x0200):
1316 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
1317 return snd_usb_cm106_boot_quirk(dev);
1318
1319 case USB_ID(0x0d8c, 0x0102):
1320 /* C-Media CM6206 / CM106-Like Sound Device */
1321 case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
1322 return snd_usb_cm6206_boot_quirk(dev);
1323
1324 case USB_ID(0x0dba, 0x3000):
1325 /* Digidesign Mbox 2 */
1326 return snd_usb_mbox2_boot_quirk(dev);
1327
1328 case USB_ID(0x1235, 0x0010): /* Focusrite Novation Saffire 6 USB */
1329 case USB_ID(0x1235, 0x0018): /* Focusrite Novation Twitch */
1330 return snd_usb_novation_boot_quirk(dev);
1331
1332 case USB_ID(0x133e, 0x0815):
1333 /* Access Music VirusTI Desktop */
1334 return snd_usb_accessmusic_boot_quirk(dev);
1335
1336 case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
1337 case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
1338 case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
1339 return snd_usb_nativeinstruments_boot_quirk(dev);
1340 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
1341 return snd_usb_fasttrackpro_boot_quirk(dev);
1342 case USB_ID(0x047f, 0xc010): /* Plantronics Gamecom 780 */
1343 return snd_usb_gamecon780_boot_quirk(dev);
1344 case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx 3 */
1345 return snd_usb_axefx3_boot_quirk(dev);
1346 case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II */
1347 /*
1348 * For some reason interface 3 with vendor-spec class is
1349 * detected on MicroBook IIc.
1350 */
1351 if (get_iface_desc(intf->altsetting)->bInterfaceClass ==
1352 USB_CLASS_VENDOR_SPEC &&
1353 get_iface_desc(intf->altsetting)->bInterfaceNumber < 3)
1354 return snd_usb_motu_microbookii_boot_quirk(dev);
1355 break;
1356 }
1357
1358 return 0;
1359}
1360
1361int snd_usb_apply_boot_quirk_once(struct usb_device *dev,
1362 struct usb_interface *intf,
1363 const struct snd_usb_audio_quirk *quirk,
1364 unsigned int id)
1365{
1366 switch (id) {
1367 case USB_ID(0x07fd, 0x0008): /* MOTU M Series */
1368 return snd_usb_motu_m_series_boot_quirk(dev);
1369 }
1370
1371 return 0;
1372}
1373
1374/*
1375 * check if the device uses big-endian samples
1376 */
1377int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
1378{
1379 /* it depends on altsetting whether the device is big-endian or not */
1380 switch (chip->usb_id) {
1381 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
1382 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1383 fp->altsetting == 5 || fp->altsetting == 6)
1384 return 1;
1385 break;
1386 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
1387 if (chip->setup == 0x00 ||
1388 fp->altsetting == 1 || fp->altsetting == 2 ||
1389 fp->altsetting == 3)
1390 return 1;
1391 break;
1392 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
1393 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1394 fp->altsetting == 5 || fp->altsetting == 6)
1395 return 1;
1396 break;
1397 }
1398 return 0;
1399}
1400
1401/*
1402 * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
1403 * not for interface.
1404 */
1405
1406enum {
1407 EMU_QUIRK_SR_44100HZ = 0,
1408 EMU_QUIRK_SR_48000HZ,
1409 EMU_QUIRK_SR_88200HZ,
1410 EMU_QUIRK_SR_96000HZ,
1411 EMU_QUIRK_SR_176400HZ,
1412 EMU_QUIRK_SR_192000HZ
1413};
1414
1415static void set_format_emu_quirk(struct snd_usb_substream *subs,
1416 struct audioformat *fmt)
1417{
1418 unsigned char emu_samplerate_id = 0;
1419
1420 /* When capture is active
1421 * sample rate shouldn't be changed
1422 * by playback substream
1423 */
1424 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1425 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
1426 return;
1427 }
1428
1429 switch (fmt->rate_min) {
1430 case 48000:
1431 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
1432 break;
1433 case 88200:
1434 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
1435 break;
1436 case 96000:
1437 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
1438 break;
1439 case 176400:
1440 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
1441 break;
1442 case 192000:
1443 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
1444 break;
1445 default:
1446 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
1447 break;
1448 }
1449 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
1450 subs->pkt_offset_adj = (emu_samplerate_id >= EMU_QUIRK_SR_176400HZ) ? 4 : 0;
1451}
1452
1453void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
1454 struct audioformat *fmt)
1455{
1456 switch (subs->stream->chip->usb_id) {
1457 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
1458 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
1459 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
1460 case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
1461 set_format_emu_quirk(subs, fmt);
1462 break;
1463 case USB_ID(0x534d, 0x0021): /* MacroSilicon MS2100/MS2106 */
1464 case USB_ID(0x534d, 0x2109): /* MacroSilicon MS2109 */
1465 subs->stream_offset_adj = 2;
1466 break;
1467 }
1468}
1469
1470bool snd_usb_get_sample_rate_quirk(struct snd_usb_audio *chip)
1471{
1472 /* devices which do not support reading the sample rate. */
1473 switch (chip->usb_id) {
1474 case USB_ID(0x041E, 0x4080): /* Creative Live Cam VF0610 */
1475 case USB_ID(0x04D8, 0xFEEA): /* Benchmark DAC1 Pre */
1476 case USB_ID(0x0556, 0x0014): /* Phoenix Audio TMX320VC */
1477 case USB_ID(0x05A3, 0x9420): /* ELP HD USB Camera */
1478 case USB_ID(0x05a7, 0x1020): /* Bose Companion 5 */
1479 case USB_ID(0x074D, 0x3553): /* Outlaw RR2150 (Micronas UAC3553B) */
1480 case USB_ID(0x1395, 0x740a): /* Sennheiser DECT */
1481 case USB_ID(0x1901, 0x0191): /* GE B850V3 CP2114 audio interface */
1482 case USB_ID(0x21B4, 0x0081): /* AudioQuest DragonFly */
1483 case USB_ID(0x2912, 0x30c8): /* Audioengine D1 */
1484 case USB_ID(0x413c, 0xa506): /* Dell AE515 sound bar */
1485 case USB_ID(0x046d, 0x084c): /* Logitech ConferenceCam Connect */
1486 return true;
1487 }
1488
1489 /* devices of these vendors don't support reading rate, either */
1490 switch (USB_ID_VENDOR(chip->usb_id)) {
1491 case 0x045E: /* MS Lifecam */
1492 case 0x047F: /* Plantronics */
1493 case 0x1de7: /* Phoenix Audio */
1494 return true;
1495 }
1496
1497 return false;
1498}
1499
1500/* ITF-USB DSD based DACs need a vendor cmd to switch
1501 * between PCM and native DSD mode
1502 */
1503static bool is_itf_usb_dsd_dac(unsigned int id)
1504{
1505 switch (id) {
1506 case USB_ID(0x154e, 0x1002): /* Denon DCD-1500RE */
1507 case USB_ID(0x154e, 0x1003): /* Denon DA-300USB */
1508 case USB_ID(0x154e, 0x3005): /* Marantz HD-DAC1 */
1509 case USB_ID(0x154e, 0x3006): /* Marantz SA-14S1 */
1510 case USB_ID(0x1852, 0x5065): /* Luxman DA-06 */
1511 case USB_ID(0x0644, 0x8043): /* TEAC UD-501/UD-501V2/UD-503/NT-503 */
1512 case USB_ID(0x0644, 0x8044): /* Esoteric D-05X */
1513 case USB_ID(0x0644, 0x804a): /* TEAC UD-301 */
1514 return true;
1515 }
1516 return false;
1517}
1518
1519int snd_usb_select_mode_quirk(struct snd_usb_substream *subs,
1520 struct audioformat *fmt)
1521{
1522 struct usb_device *dev = subs->dev;
1523 int err;
1524
1525 if (is_itf_usb_dsd_dac(subs->stream->chip->usb_id)) {
1526 /* First switch to alt set 0, otherwise the mode switch cmd
1527 * will not be accepted by the DAC
1528 */
1529 err = usb_set_interface(dev, fmt->iface, 0);
1530 if (err < 0)
1531 return err;
1532
1533 msleep(20); /* Delay needed after setting the interface */
1534
1535 /* Vendor mode switch cmd is required. */
1536 if (fmt->formats & SNDRV_PCM_FMTBIT_DSD_U32_BE) {
1537 /* DSD mode (DSD_U32) requested */
1538 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1539 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1540 1, 1, NULL, 0);
1541 if (err < 0)
1542 return err;
1543
1544 } else {
1545 /* PCM or DOP mode (S32) requested */
1546 /* PCM mode (S16) requested */
1547 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1548 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1549 0, 1, NULL, 0);
1550 if (err < 0)
1551 return err;
1552
1553 }
1554 msleep(20);
1555 }
1556 return 0;
1557}
1558
1559void snd_usb_endpoint_start_quirk(struct snd_usb_endpoint *ep)
1560{
1561 /*
1562 * "Playback Design" products send bogus feedback data at the start
1563 * of the stream. Ignore them.
1564 */
1565 if (USB_ID_VENDOR(ep->chip->usb_id) == 0x23ba &&
1566 ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
1567 ep->skip_packets = 4;
1568
1569 /*
1570 * M-Audio Fast Track C400/C600 - when packets are not skipped, real
1571 * world latency varies by approx. +/- 50 frames (at 96KHz) each time
1572 * the stream is (re)started. When skipping packets 16 at endpoint
1573 * start up, the real world latency is stable within +/- 1 frame (also
1574 * across power cycles).
1575 */
1576 if ((ep->chip->usb_id == USB_ID(0x0763, 0x2030) ||
1577 ep->chip->usb_id == USB_ID(0x0763, 0x2031)) &&
1578 ep->type == SND_USB_ENDPOINT_TYPE_DATA)
1579 ep->skip_packets = 16;
1580
1581 /* Work around devices that report unreasonable feedback data */
1582 if ((ep->chip->usb_id == USB_ID(0x0644, 0x8038) || /* TEAC UD-H01 */
1583 ep->chip->usb_id == USB_ID(0x1852, 0x5034)) && /* T+A Dac8 */
1584 ep->syncmaxsize == 4)
1585 ep->tenor_fb_quirk = 1;
1586}
1587
1588void snd_usb_set_interface_quirk(struct usb_device *dev)
1589{
1590 struct snd_usb_audio *chip = dev_get_drvdata(&dev->dev);
1591
1592 if (!chip)
1593 return;
1594 /*
1595 * "Playback Design" products need a 50ms delay after setting the
1596 * USB interface.
1597 */
1598 switch (USB_ID_VENDOR(chip->usb_id)) {
1599 case 0x23ba: /* Playback Design */
1600 case 0x0644: /* TEAC Corp. */
1601 msleep(50);
1602 break;
1603 }
1604}
1605
1606/* quirk applied after snd_usb_ctl_msg(); not applied during boot quirks */
1607void snd_usb_ctl_msg_quirk(struct usb_device *dev, unsigned int pipe,
1608 __u8 request, __u8 requesttype, __u16 value,
1609 __u16 index, void *data, __u16 size)
1610{
1611 struct snd_usb_audio *chip = dev_get_drvdata(&dev->dev);
1612
1613 if (!chip)
1614 return;
1615 /*
1616 * "Playback Design" products need a 20ms delay after each
1617 * class compliant request
1618 */
1619 if (USB_ID_VENDOR(chip->usb_id) == 0x23ba &&
1620 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1621 msleep(20);
1622
1623 /*
1624 * "TEAC Corp." products need a 20ms delay after each
1625 * class compliant request
1626 */
1627 if (USB_ID_VENDOR(chip->usb_id) == 0x0644 &&
1628 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1629 msleep(20);
1630
1631 /* ITF-USB DSD based DACs functionality need a delay
1632 * after each class compliant request
1633 */
1634 if (is_itf_usb_dsd_dac(chip->usb_id)
1635 && (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1636 msleep(20);
1637
1638 /*
1639 * Plantronics headsets (C320, C320-M, etc) need a delay to avoid
1640 * random microhpone failures.
1641 */
1642 if (USB_ID_VENDOR(chip->usb_id) == 0x047f &&
1643 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1644 msleep(20);
1645
1646 /* Zoom R16/24, many Logitech(at least H650e/H570e/BCC950),
1647 * Jabra 550a, Kingston HyperX needs a tiny delay here,
1648 * otherwise requests like get/set frequency return
1649 * as failed despite actually succeeding.
1650 */
1651 if ((chip->usb_id == USB_ID(0x1686, 0x00dd) ||
1652 USB_ID_VENDOR(chip->usb_id) == 0x046d || /* Logitech */
1653 chip->usb_id == USB_ID(0x0b0e, 0x0349) ||
1654 chip->usb_id == USB_ID(0x0951, 0x16ad)) &&
1655 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1656 usleep_range(1000, 2000);
1657
1658 /*
1659 * Samsung USBC Headset (AKG) need a tiny delay after each
1660 * class compliant request. (Model number: AAM625R or AAM627R)
1661 */
1662 if (chip->usb_id == USB_ID(0x04e8, 0xa051) &&
1663 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1664 usleep_range(5000, 6000);
1665}
1666
1667/*
1668 * snd_usb_interface_dsd_format_quirks() is called from format.c to
1669 * augment the PCM format bit-field for DSD types. The UAC standards
1670 * don't have a designated bit field to denote DSD-capable interfaces,
1671 * hence all hardware that is known to support this format has to be
1672 * listed here.
1673 */
1674u64 snd_usb_interface_dsd_format_quirks(struct snd_usb_audio *chip,
1675 struct audioformat *fp,
1676 unsigned int sample_bytes)
1677{
1678 struct usb_interface *iface;
1679
1680 /* Playback Designs */
1681 if (USB_ID_VENDOR(chip->usb_id) == 0x23ba &&
1682 USB_ID_PRODUCT(chip->usb_id) < 0x0110) {
1683 switch (fp->altsetting) {
1684 case 1:
1685 fp->dsd_dop = true;
1686 return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1687 case 2:
1688 fp->dsd_bitrev = true;
1689 return SNDRV_PCM_FMTBIT_DSD_U8;
1690 case 3:
1691 fp->dsd_bitrev = true;
1692 return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1693 }
1694 }
1695
1696 /* XMOS based USB DACs */
1697 switch (chip->usb_id) {
1698 case USB_ID(0x1511, 0x0037): /* AURALiC VEGA */
1699 case USB_ID(0x21ed, 0xd75a): /* Accuphase DAC-60 option card */
1700 case USB_ID(0x2522, 0x0012): /* LH Labs VI DAC Infinity */
1701 case USB_ID(0x2772, 0x0230): /* Pro-Ject Pre Box S2 Digital */
1702 if (fp->altsetting == 2)
1703 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1704 break;
1705
1706 case USB_ID(0x0d8c, 0x0316): /* Hegel HD12 DSD */
1707 case USB_ID(0x10cb, 0x0103): /* The Bit Opus #3; with fp->dsd_raw */
1708 case USB_ID(0x16d0, 0x06b2): /* NuPrime DAC-10 */
1709 case USB_ID(0x16d0, 0x09dd): /* Encore mDSD */
1710 case USB_ID(0x16d0, 0x0733): /* Furutech ADL Stratos */
1711 case USB_ID(0x16d0, 0x09db): /* NuPrime Audio DAC-9 */
1712 case USB_ID(0x1db5, 0x0003): /* Bryston BDA3 */
1713 case USB_ID(0x22e1, 0xca01): /* HDTA Serenade DSD */
1714 case USB_ID(0x249c, 0x9326): /* M2Tech Young MkIII */
1715 case USB_ID(0x2616, 0x0106): /* PS Audio NuWave DAC */
1716 case USB_ID(0x2622, 0x0041): /* Audiolab M-DAC+ */
1717 case USB_ID(0x27f7, 0x3002): /* W4S DAC-2v2SE */
1718 case USB_ID(0x29a2, 0x0086): /* Mutec MC3+ USB */
1719 case USB_ID(0x6b42, 0x0042): /* MSB Technology */
1720 if (fp->altsetting == 3)
1721 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1722 break;
1723
1724 /* Amanero Combo384 USB based DACs with native DSD support */
1725 case USB_ID(0x16d0, 0x071a): /* Amanero - Combo384 */
1726 case USB_ID(0x2ab6, 0x0004): /* T+A DAC8DSD-V2.0, MP1000E-V2.0, MP2000R-V2.0, MP2500R-V2.0, MP3100HV-V2.0 */
1727 case USB_ID(0x2ab6, 0x0005): /* T+A USB HD Audio 1 */
1728 case USB_ID(0x2ab6, 0x0006): /* T+A USB HD Audio 2 */
1729 if (fp->altsetting == 2) {
1730 switch (le16_to_cpu(chip->dev->descriptor.bcdDevice)) {
1731 case 0x199:
1732 return SNDRV_PCM_FMTBIT_DSD_U32_LE;
1733 case 0x19b:
1734 case 0x203:
1735 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1736 default:
1737 break;
1738 }
1739 }
1740 break;
1741 case USB_ID(0x16d0, 0x0a23):
1742 if (fp->altsetting == 2)
1743 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1744 break;
1745
1746 default:
1747 break;
1748 }
1749
1750 /* ITF-USB DSD based DACs */
1751 if (is_itf_usb_dsd_dac(chip->usb_id)) {
1752 iface = usb_ifnum_to_if(chip->dev, fp->iface);
1753
1754 /* Altsetting 2 support native DSD if the num of altsets is
1755 * three (0-2),
1756 * Altsetting 3 support native DSD if the num of altsets is
1757 * four (0-3).
1758 */
1759 if (fp->altsetting == iface->num_altsetting - 1)
1760 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1761 }
1762
1763 /* Mostly generic method to detect many DSD-capable implementations -
1764 * from XMOS/Thesycon
1765 */
1766 switch (USB_ID_VENDOR(chip->usb_id)) {
1767 case 0x152a: /* Thesycon devices */
1768 case 0x20b1: /* XMOS based devices */
1769 case 0x22d9: /* Oppo */
1770 case 0x23ba: /* Playback Designs */
1771 case 0x25ce: /* Mytek devices */
1772 case 0x278b: /* Rotel? */
1773 case 0x292b: /* Gustard/Ess based devices */
1774 case 0x2972: /* FiiO devices */
1775 case 0x2ab6: /* T+A devices */
1776 case 0x3353: /* Khadas devices */
1777 case 0x3842: /* EVGA */
1778 case 0xc502: /* HiBy devices */
1779 if (fp->dsd_raw)
1780 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1781 break;
1782 default:
1783 break;
1784
1785 }
1786
1787 return 0;
1788}
1789
1790void snd_usb_audioformat_attributes_quirk(struct snd_usb_audio *chip,
1791 struct audioformat *fp,
1792 int stream)
1793{
1794 switch (chip->usb_id) {
1795 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
1796 /* Optoplay sets the sample rate attribute although
1797 * it seems not supporting it in fact.
1798 */
1799 fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
1800 break;
1801 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
1802 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
1803 /* doesn't set the sample rate attribute, but supports it */
1804 fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
1805 break;
1806 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro USB */
1807 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
1808 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
1809 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
1810 an older model 77d:223) */
1811 /*
1812 * plantronics headset and Griffin iMic have set adaptive-in
1813 * although it's really not...
1814 */
1815 fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
1816 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1817 fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
1818 else
1819 fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
1820 break;
1821 case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook IIc */
1822 /*
1823 * MaxPacketsOnly attribute is erroneously set in endpoint
1824 * descriptors. As a result this card produces noise with
1825 * all sample rates other than 96 KHz.
1826 */
1827 fp->attributes &= ~UAC_EP_CS_ATTR_FILL_MAX;
1828 break;
1829 case USB_ID(0x1235, 0x8202): /* Focusrite Scarlett 2i2 2nd gen */
1830 case USB_ID(0x1235, 0x8205): /* Focusrite Scarlett Solo 2nd gen */
1831 /*
1832 * Reports that playback should use Synch: Synchronous
1833 * while still providing a feedback endpoint.
1834 * Synchronous causes snapping on some sample rates.
1835 * Force it to use Synch: Asynchronous.
1836 */
1837 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
1838 fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
1839 fp->ep_attr |= USB_ENDPOINT_SYNC_ASYNC;
1840 }
1841 break;
1842 }
1843}
1844
1845/*
1846 * registration quirk:
1847 * the registration is skipped if a device matches with the given ID,
1848 * unless the interface reaches to the defined one. This is for delaying
1849 * the registration until the last known interface, so that the card and
1850 * devices appear at the same time.
1851 */
1852
1853struct registration_quirk {
1854 unsigned int usb_id; /* composed via USB_ID() */
1855 unsigned int interface; /* the interface to trigger register */
1856};
1857
1858#define REG_QUIRK_ENTRY(vendor, product, iface) \
1859 { .usb_id = USB_ID(vendor, product), .interface = (iface) }
1860
1861static const struct registration_quirk registration_quirks[] = {
1862 REG_QUIRK_ENTRY(0x0951, 0x16d8, 2), /* Kingston HyperX AMP */
1863 REG_QUIRK_ENTRY(0x0951, 0x16ed, 2), /* Kingston HyperX Cloud Alpha S */
1864 REG_QUIRK_ENTRY(0x0951, 0x16ea, 2), /* Kingston HyperX Cloud Flight S */
1865 REG_QUIRK_ENTRY(0x0ecb, 0x1f46, 2), /* JBL Quantum 600 */
1866 REG_QUIRK_ENTRY(0x0ecb, 0x1f47, 2), /* JBL Quantum 800 */
1867 REG_QUIRK_ENTRY(0x0ecb, 0x1f4c, 2), /* JBL Quantum 400 */
1868 REG_QUIRK_ENTRY(0x0ecb, 0x2039, 2), /* JBL Quantum 400 */
1869 REG_QUIRK_ENTRY(0x0ecb, 0x203c, 2), /* JBL Quantum 600 */
1870 REG_QUIRK_ENTRY(0x0ecb, 0x203e, 2), /* JBL Quantum 800 */
1871 { 0 } /* terminator */
1872};
1873
1874/* return true if skipping registration */
1875bool snd_usb_registration_quirk(struct snd_usb_audio *chip, int iface)
1876{
1877 const struct registration_quirk *q;
1878
1879 for (q = registration_quirks; q->usb_id; q++)
1880 if (chip->usb_id == q->usb_id)
1881 return iface != q->interface;
1882
1883 /* Register as normal */
1884 return false;
1885}