blob: a0986e079a66bf455812c89080533cd095ff8f33 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Daniel Macke5779992010-03-04 19:46:13 +01002/*
Daniel Macke5779992010-03-04 19:46:13 +01003 */
4
5#include <linux/init.h>
Stephen Rothwell36db0452010-03-29 16:02:50 +11006#include <linux/slab.h>
Daniel Macke5779992010-03-04 19:46:13 +01007#include <linux/usb.h>
8#include <linux/usb/audio.h>
Clemens Ladischaafe77c2013-03-31 23:43:12 +02009#include <linux/usb/midi.h>
Linus Walleijad43d5282018-11-02 22:11:33 +010010#include <linux/bits.h>
Daniel Macke5779992010-03-04 19:46:13 +010011
Daniel Mack9e386582011-05-25 09:09:01 +020012#include <sound/control.h>
Daniel Macke5779992010-03-04 19:46:13 +010013#include <sound/core.h>
14#include <sound/info.h>
15#include <sound/pcm.h>
16
17#include "usbaudio.h"
18#include "card.h"
Daniel Mackf0b5e632010-03-11 21:13:23 +010019#include "mixer.h"
Daniel Mack7b1eda22010-03-11 21:13:22 +010020#include "mixer_quirks.h"
Daniel Macke5779992010-03-04 19:46:13 +010021#include "midi.h"
22#include "quirks.h"
23#include "helper.h"
24#include "endpoint.h"
25#include "pcm.h"
Daniel Mack3d8d4dc2010-06-16 17:57:31 +020026#include "clock.h"
Daniel Macke8e8bab2011-09-12 18:54:12 +020027#include "stream.h"
Daniel Macke5779992010-03-04 19:46:13 +010028
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,
Takashi Iwai85a81812014-11-10 07:41:59 +010035 const struct snd_usb_audio_quirk *quirk_comp)
Daniel Macke5779992010-03-04 19:46:13 +010036{
37 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
Takashi Iwai85a81812014-11-10 07:41:59 +010038 const struct snd_usb_audio_quirk *quirk;
Daniel Macke5779992010-03-04 19:46:13 +010039 int err;
40
Takashi Iwai85a81812014-11-10 07:41:59 +010041 for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
Daniel Macke5779992010-03-04 19:46:13 +010042 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;
Takashi Iwaid4b8fc62014-11-09 18:21:23 +010051 }
52
Takashi Iwai85a81812014-11-10 07:41:59 +010053 for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
Takashi Iwaid4b8fc62014-11-09 18:21:23 +010054 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
55 if (!iface)
56 continue;
57 if (quirk->ifnum != probed_ifnum &&
Takashi Iwai5fb45412021-04-06 13:35:34 +020058 !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 }
Daniel Macke5779992010-03-04 19:46:13 +010064 }
Takashi Iwaid4b8fc62014-11-09 18:21:23 +010065
Daniel Macke5779992010-03-04 19:46:13 +010066 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
Daniel Macke5779992010-03-04 19:46:13 +010078static int create_any_midi_quirk(struct snd_usb_audio *chip,
79 struct usb_interface *intf,
80 struct usb_driver *driver,
81 const struct snd_usb_audio_quirk *quirk)
82{
83 return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
84}
85
86/*
87 * create a stream for an interface with proper descriptors
88 */
89static int create_standard_audio_quirk(struct snd_usb_audio *chip,
90 struct usb_interface *iface,
91 struct usb_driver *driver,
92 const struct snd_usb_audio_quirk *quirk)
93{
94 struct usb_host_interface *alts;
95 struct usb_interface_descriptor *altsd;
96 int err;
97
98 alts = &iface->altsetting[0];
99 altsd = get_iface_desc(alts);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200100 err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
Daniel Macke5779992010-03-04 19:46:13 +0100101 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100102 usb_audio_err(chip, "cannot setup if %d: error %d\n",
Daniel Macke5779992010-03-04 19:46:13 +0100103 altsd->bInterfaceNumber, err);
104 return err;
105 }
106 /* reset the current interface */
107 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
108 return 0;
109}
110
Takashi Iwaib2345a82021-01-08 08:52:15 +0100111/* create the audio stream and the corresponding endpoints from the fixed
112 * audioformat object; this is used for quirks with the fixed EPs
113 */
114static int add_audio_stream_from_fixed_fmt(struct snd_usb_audio *chip,
115 struct audioformat *fp)
116{
117 int stream, err;
118
119 stream = (fp->endpoint & USB_DIR_IN) ?
120 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
121
122 snd_usb_audioformat_set_sync_ep(chip, fp);
123
124 err = snd_usb_add_audio_stream(chip, stream, fp);
125 if (err < 0)
126 return err;
127
128 err = snd_usb_add_endpoint(chip, fp->endpoint,
129 SND_USB_ENDPOINT_TYPE_DATA);
130 if (err < 0)
131 return err;
132
133 if (fp->sync_ep) {
134 err = snd_usb_add_endpoint(chip, fp->sync_ep,
135 fp->implicit_fb ?
136 SND_USB_ENDPOINT_TYPE_DATA :
137 SND_USB_ENDPOINT_TYPE_SYNC);
138 if (err < 0)
139 return err;
140 }
141
142 return 0;
143}
144
Daniel Macke5779992010-03-04 19:46:13 +0100145/*
146 * create a stream for an endpoint/altsetting without proper descriptors
147 */
148static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
149 struct usb_interface *iface,
150 struct usb_driver *driver,
151 const struct snd_usb_audio_quirk *quirk)
152{
153 struct audioformat *fp;
154 struct usb_host_interface *alts;
Eldad Zack42d4ab82013-07-09 20:36:15 +0200155 struct usb_interface_descriptor *altsd;
Daniel Macke5779992010-03-04 19:46:13 +0100156 unsigned *rate_table = NULL;
Takashi Iwaib2345a82021-01-08 08:52:15 +0100157 int err;
Daniel Macke5779992010-03-04 19:46:13 +0100158
159 fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
Markus Elfring9ecb2402017-08-11 18:38:25 +0200160 if (!fp)
Daniel Macke5779992010-03-04 19:46:13 +0100161 return -ENOMEM;
Markus Elfring9ecb2402017-08-11 18:38:25 +0200162
Vladis Dronov836b34a2016-03-31 12:05:43 -0400163 INIT_LIST_HEAD(&fp->list);
Xi Wang8866f402012-02-14 05:18:48 -0500164 if (fp->nr_rates > MAX_NR_RATES) {
165 kfree(fp);
166 return -EINVAL;
167 }
Daniel Macke5779992010-03-04 19:46:13 +0100168 if (fp->nr_rates > 0) {
Thomas Meyer43df2a52011-11-10 19:38:43 +0100169 rate_table = kmemdup(fp->rate_table,
170 sizeof(int) * fp->nr_rates, GFP_KERNEL);
Daniel Macke5779992010-03-04 19:46:13 +0100171 if (!rate_table) {
172 kfree(fp);
173 return -ENOMEM;
174 }
Daniel Macke5779992010-03-04 19:46:13 +0100175 fp->rate_table = rate_table;
176 }
177
Daniel Macke5779992010-03-04 19:46:13 +0100178 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
179 fp->altset_idx >= iface->num_altsetting) {
Takashi Iwai902eb7f2016-03-15 12:14:49 +0100180 err = -EINVAL;
181 goto error;
Daniel Macke5779992010-03-04 19:46:13 +0100182 }
183 alts = &iface->altsetting[fp->altset_idx];
Eldad Zack42d4ab82013-07-09 20:36:15 +0200184 altsd = get_iface_desc(alts);
Takashi Iwaieae4d052021-01-08 08:52:18 +0100185 if (altsd->bNumEndpoints <= fp->ep_idx) {
Takashi Iwai902eb7f2016-03-15 12:14:49 +0100186 err = -EINVAL;
187 goto error;
Takashi Iwai0f886ca2016-03-15 12:09:10 +0100188 }
189
Eldad Zack42d4ab82013-07-09 20:36:15 +0200190 fp->protocol = altsd->bInterfaceProtocol;
191
Mark Hills59ea5862013-03-17 11:07:54 +0000192 if (fp->datainterval == 0)
193 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
194 if (fp->maxpacksize == 0)
Takashi Iwaieae4d052021-01-08 08:52:18 +0100195 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, fp->ep_idx)->wMaxPacketSize);
Takashi Iwaib2345a82021-01-08 08:52:15 +0100196 if (!fp->fmt_type)
197 fp->fmt_type = UAC_FORMAT_TYPE_I;
198
199 err = add_audio_stream_from_fixed_fmt(chip, fp);
200 if (err < 0)
201 goto error;
202
Daniel Macke5779992010-03-04 19:46:13 +0100203 usb_set_interface(chip->dev, fp->iface, 0);
Takashi Iwai73037c82020-11-23 09:53:26 +0100204 snd_usb_init_pitch(chip, fp);
Takashi Iwai953a4462020-11-23 09:53:25 +0100205 snd_usb_init_sample_rate(chip, fp, fp->rate_max);
Daniel Macke5779992010-03-04 19:46:13 +0100206 return 0;
Takashi Iwai902eb7f2016-03-15 12:14:49 +0100207
208 error:
Vladis Dronov836b34a2016-03-31 12:05:43 -0400209 list_del(&fp->list); /* unlink for avoiding double-free */
Takashi Iwai902eb7f2016-03-15 12:14:49 +0100210 kfree(fp);
211 kfree(rate_table);
212 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100213}
214
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200215static int create_auto_pcm_quirk(struct snd_usb_audio *chip,
216 struct usb_interface *iface,
217 struct usb_driver *driver)
218{
219 struct usb_host_interface *alts;
220 struct usb_interface_descriptor *altsd;
221 struct usb_endpoint_descriptor *epd;
222 struct uac1_as_header_descriptor *ashd;
223 struct uac_format_type_i_discrete_descriptor *fmtd;
224
225 /*
226 * Most Roland/Yamaha audio streaming interfaces have more or less
227 * standard descriptors, but older devices might lack descriptors, and
228 * future ones might change, so ensure that we fail silently if the
229 * interface doesn't look exactly right.
230 */
231
232 /* must have a non-zero altsetting for streaming */
233 if (iface->num_altsetting < 2)
234 return -ENODEV;
235 alts = &iface->altsetting[1];
236 altsd = get_iface_desc(alts);
237
238 /* must have an isochronous endpoint for streaming */
239 if (altsd->bNumEndpoints < 1)
240 return -ENODEV;
241 epd = get_endpoint(alts, 0);
242 if (!usb_endpoint_xfer_isoc(epd))
243 return -ENODEV;
244
245 /* must have format descriptors */
246 ashd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
247 UAC_AS_GENERAL);
248 fmtd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
249 UAC_FORMAT_TYPE);
250 if (!ashd || ashd->bLength < 7 ||
251 !fmtd || fmtd->bLength < 8)
252 return -ENODEV;
253
254 return create_standard_audio_quirk(chip, iface, driver, NULL);
255}
256
257static int create_yamaha_midi_quirk(struct snd_usb_audio *chip,
258 struct usb_interface *iface,
259 struct usb_driver *driver,
260 struct usb_host_interface *alts)
261{
262 static const struct snd_usb_audio_quirk yamaha_midi_quirk = {
263 .type = QUIRK_MIDI_YAMAHA
264 };
265 struct usb_midi_in_jack_descriptor *injd;
266 struct usb_midi_out_jack_descriptor *outjd;
267
268 /* must have some valid jack descriptors */
269 injd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
270 NULL, USB_MS_MIDI_IN_JACK);
271 outjd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
272 NULL, USB_MS_MIDI_OUT_JACK);
273 if (!injd && !outjd)
274 return -ENODEV;
Takashi Iwaicc9dbfa2019-11-13 12:12:59 +0100275 if ((injd && !snd_usb_validate_midi_desc(injd)) ||
276 (outjd && !snd_usb_validate_midi_desc(outjd)))
Takashi Iwai57f87702019-08-20 17:17:09 +0200277 return -ENODEV;
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200278 if (injd && (injd->bLength < 5 ||
279 (injd->bJackType != USB_MS_EMBEDDED &&
280 injd->bJackType != USB_MS_EXTERNAL)))
281 return -ENODEV;
282 if (outjd && (outjd->bLength < 6 ||
283 (outjd->bJackType != USB_MS_EMBEDDED &&
284 outjd->bJackType != USB_MS_EXTERNAL)))
285 return -ENODEV;
286 return create_any_midi_quirk(chip, iface, driver, &yamaha_midi_quirk);
287}
288
289static int create_roland_midi_quirk(struct snd_usb_audio *chip,
290 struct usb_interface *iface,
291 struct usb_driver *driver,
292 struct usb_host_interface *alts)
293{
294 static const struct snd_usb_audio_quirk roland_midi_quirk = {
295 .type = QUIRK_MIDI_ROLAND
296 };
297 u8 *roland_desc = NULL;
298
299 /* might have a vendor-specific descriptor <06 24 F1 02 ...> */
300 for (;;) {
301 roland_desc = snd_usb_find_csint_desc(alts->extra,
302 alts->extralen,
303 roland_desc, 0xf1);
304 if (!roland_desc)
305 return -ENODEV;
306 if (roland_desc[0] < 6 || roland_desc[3] != 2)
307 continue;
308 return create_any_midi_quirk(chip, iface, driver,
309 &roland_midi_quirk);
310 }
311}
312
313static int create_std_midi_quirk(struct snd_usb_audio *chip,
314 struct usb_interface *iface,
315 struct usb_driver *driver,
316 struct usb_host_interface *alts)
317{
318 struct usb_ms_header_descriptor *mshd;
319 struct usb_ms_endpoint_descriptor *msepd;
320
321 /* must have the MIDIStreaming interface header descriptor*/
322 mshd = (struct usb_ms_header_descriptor *)alts->extra;
323 if (alts->extralen < 7 ||
324 mshd->bLength < 7 ||
325 mshd->bDescriptorType != USB_DT_CS_INTERFACE ||
326 mshd->bDescriptorSubtype != USB_MS_HEADER)
327 return -ENODEV;
328 /* must have the MIDIStreaming endpoint descriptor*/
329 msepd = (struct usb_ms_endpoint_descriptor *)alts->endpoint[0].extra;
330 if (alts->endpoint[0].extralen < 4 ||
331 msepd->bLength < 4 ||
332 msepd->bDescriptorType != USB_DT_CS_ENDPOINT ||
333 msepd->bDescriptorSubtype != UAC_MS_GENERAL ||
334 msepd->bNumEmbMIDIJack < 1 ||
335 msepd->bNumEmbMIDIJack > 16)
336 return -ENODEV;
337
338 return create_any_midi_quirk(chip, iface, driver, NULL);
339}
340
341static int create_auto_midi_quirk(struct snd_usb_audio *chip,
342 struct usb_interface *iface,
343 struct usb_driver *driver)
344{
345 struct usb_host_interface *alts;
346 struct usb_interface_descriptor *altsd;
347 struct usb_endpoint_descriptor *epd;
348 int err;
349
350 alts = &iface->altsetting[0];
351 altsd = get_iface_desc(alts);
352
353 /* must have at least one bulk/interrupt endpoint for streaming */
354 if (altsd->bNumEndpoints < 1)
355 return -ENODEV;
356 epd = get_endpoint(alts, 0);
Clemens Ladischaa773bf2013-08-11 14:13:13 +0200357 if (!usb_endpoint_xfer_bulk(epd) &&
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200358 !usb_endpoint_xfer_int(epd))
359 return -ENODEV;
360
361 switch (USB_ID_VENDOR(chip->usb_id)) {
362 case 0x0499: /* Yamaha */
363 err = create_yamaha_midi_quirk(chip, iface, driver, alts);
Clemens Ladischaa773bf2013-08-11 14:13:13 +0200364 if (err != -ENODEV)
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200365 return err;
366 break;
367 case 0x0582: /* Roland */
368 err = create_roland_midi_quirk(chip, iface, driver, alts);
Clemens Ladischaa773bf2013-08-11 14:13:13 +0200369 if (err != -ENODEV)
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200370 return err;
371 break;
372 }
373
374 return create_std_midi_quirk(chip, iface, driver, alts);
375}
376
377static int create_autodetect_quirk(struct snd_usb_audio *chip,
378 struct usb_interface *iface,
Clemens Ladischb1ce7ba2013-04-04 21:43:57 +0200379 struct usb_driver *driver)
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200380{
381 int err;
382
383 err = create_auto_pcm_quirk(chip, iface, driver);
384 if (err == -ENODEV)
385 err = create_auto_midi_quirk(chip, iface, driver);
386 return err;
387}
388
Clemens Ladischb1ce7ba2013-04-04 21:43:57 +0200389static int create_autodetect_quirks(struct snd_usb_audio *chip,
390 struct usb_interface *iface,
391 struct usb_driver *driver,
392 const struct snd_usb_audio_quirk *quirk)
393{
394 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
395 int ifcount, ifnum, err;
396
397 err = create_autodetect_quirk(chip, iface, driver);
398 if (err < 0)
399 return err;
400
401 /*
402 * ALSA PCM playback/capture devices cannot be registered in two steps,
403 * so we have to claim the other corresponding interface here.
404 */
405 ifcount = chip->dev->actconfig->desc.bNumInterfaces;
406 for (ifnum = 0; ifnum < ifcount; ifnum++) {
407 if (ifnum == probed_ifnum || quirk->ifnum >= 0)
408 continue;
409 iface = usb_ifnum_to_if(chip->dev, ifnum);
410 if (!iface ||
411 usb_interface_claimed(iface) ||
412 get_iface_desc(iface->altsetting)->bInterfaceClass !=
413 USB_CLASS_VENDOR_SPEC)
414 continue;
415
416 err = create_autodetect_quirk(chip, iface, driver);
Takashi Iwai5fb45412021-04-06 13:35:34 +0200417 if (err >= 0) {
418 err = usb_driver_claim_interface(driver, iface,
419 USB_AUDIO_IFACE_UNUSED);
420 if (err < 0)
421 return err;
422 }
Clemens Ladischb1ce7ba2013-04-04 21:43:57 +0200423 }
424
425 return 0;
426}
427
Daniel Macke5779992010-03-04 19:46:13 +0100428/*
429 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
430 * The only way to detect the sample rate is by looking at wMaxPacketSize.
431 */
432static int create_uaxx_quirk(struct snd_usb_audio *chip,
433 struct usb_interface *iface,
434 struct usb_driver *driver,
435 const struct snd_usb_audio_quirk *quirk)
436{
437 static const struct audioformat ua_format = {
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100438 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
Daniel Macke5779992010-03-04 19:46:13 +0100439 .channels = 2,
440 .fmt_type = UAC_FORMAT_TYPE_I,
441 .altsetting = 1,
442 .altset_idx = 1,
443 .rates = SNDRV_PCM_RATE_CONTINUOUS,
444 };
445 struct usb_host_interface *alts;
446 struct usb_interface_descriptor *altsd;
447 struct audioformat *fp;
Takashi Iwaib2345a82021-01-08 08:52:15 +0100448 int err;
Daniel Macke5779992010-03-04 19:46:13 +0100449
450 /* both PCM and MIDI interfaces have 2 or more altsettings */
451 if (iface->num_altsetting < 2)
452 return -ENXIO;
453 alts = &iface->altsetting[1];
454 altsd = get_iface_desc(alts);
455
456 if (altsd->bNumEndpoints == 2) {
457 static const struct snd_usb_midi_endpoint_info ua700_ep = {
458 .out_cables = 0x0003,
459 .in_cables = 0x0003
460 };
461 static const struct snd_usb_audio_quirk ua700_quirk = {
462 .type = QUIRK_MIDI_FIXED_ENDPOINT,
463 .data = &ua700_ep
464 };
465 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
466 .out_cables = 0x0001,
467 .in_cables = 0x0001
468 };
469 static const struct snd_usb_audio_quirk uaxx_quirk = {
470 .type = QUIRK_MIDI_FIXED_ENDPOINT,
471 .data = &uaxx_ep
472 };
473 const struct snd_usb_audio_quirk *quirk =
474 chip->usb_id == USB_ID(0x0582, 0x002b)
475 ? &ua700_quirk : &uaxx_quirk;
Takashi Iwai79289e22016-01-11 11:33:34 +0100476 return __snd_usbmidi_create(chip->card, iface,
477 &chip->midi_list, quirk,
478 chip->usb_id);
Daniel Macke5779992010-03-04 19:46:13 +0100479 }
480
481 if (altsd->bNumEndpoints != 1)
482 return -ENXIO;
483
Thomas Meyer43df2a52011-11-10 19:38:43 +0100484 fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
Daniel Macke5779992010-03-04 19:46:13 +0100485 if (!fp)
486 return -ENOMEM;
Daniel Macke5779992010-03-04 19:46:13 +0100487
488 fp->iface = altsd->bInterfaceNumber;
489 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
490 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
491 fp->datainterval = 0;
492 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Vladis Dronov836b34a2016-03-31 12:05:43 -0400493 INIT_LIST_HEAD(&fp->list);
Daniel Macke5779992010-03-04 19:46:13 +0100494
495 switch (fp->maxpacksize) {
496 case 0x120:
497 fp->rate_max = fp->rate_min = 44100;
498 break;
499 case 0x138:
500 case 0x140:
501 fp->rate_max = fp->rate_min = 48000;
502 break;
503 case 0x258:
504 case 0x260:
505 fp->rate_max = fp->rate_min = 96000;
506 break;
507 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100508 usb_audio_err(chip, "unknown sample rate\n");
Daniel Macke5779992010-03-04 19:46:13 +0100509 kfree(fp);
510 return -ENXIO;
511 }
512
Takashi Iwaib2345a82021-01-08 08:52:15 +0100513 err = add_audio_stream_from_fixed_fmt(chip, fp);
Daniel Macke5779992010-03-04 19:46:13 +0100514 if (err < 0) {
Vladis Dronov836b34a2016-03-31 12:05:43 -0400515 list_del(&fp->list); /* unlink for avoiding double-free */
Daniel Macke5779992010-03-04 19:46:13 +0100516 kfree(fp);
517 return err;
518 }
519 usb_set_interface(chip->dev, fp->iface, 0);
520 return 0;
521}
522
523/*
Daniel Mack014950b2011-05-25 09:09:02 +0200524 * Create a standard mixer for the specified interface.
525 */
526static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
527 struct usb_interface *iface,
528 struct usb_driver *driver,
529 const struct snd_usb_audio_quirk *quirk)
530{
531 if (quirk->ifnum < 0)
532 return 0;
533
534 return snd_usb_create_mixer(chip, quirk->ifnum, 0);
535}
536
Kai-Heng Feng1965c432020-08-23 18:58:50 +0800537static int setup_disable_autosuspend(struct snd_usb_audio *chip,
538 struct usb_interface *iface,
539 struct usb_driver *driver,
540 const struct snd_usb_audio_quirk *quirk)
541{
Kai-Heng Feng97991102021-03-04 12:34:16 +0800542 usb_disable_autosuspend(interface_to_usbdev(iface));
Kai-Heng Feng1965c432020-08-23 18:58:50 +0800543 return 1; /* Continue with creating streams and mixer */
544}
545
Daniel Mack014950b2011-05-25 09:09:02 +0200546/*
Daniel Macke5779992010-03-04 19:46:13 +0100547 * audio-interface quirks
548 *
549 * returns zero if no standard audio/MIDI parsing is needed.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300550 * returns a positive value if standard audio/midi interfaces are parsed
Daniel Macke5779992010-03-04 19:46:13 +0100551 * after this.
552 * returns a negative value at error.
553 */
554int snd_usb_create_quirk(struct snd_usb_audio *chip,
555 struct usb_interface *iface,
556 struct usb_driver *driver,
557 const struct snd_usb_audio_quirk *quirk)
558{
559 typedef int (*quirk_func_t)(struct snd_usb_audio *,
560 struct usb_interface *,
561 struct usb_driver *,
562 const struct snd_usb_audio_quirk *);
563 static const quirk_func_t quirk_funcs[] = {
564 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
565 [QUIRK_COMPOSITE] = create_composite_quirk,
Clemens Ladischb1ce7ba2013-04-04 21:43:57 +0200566 [QUIRK_AUTODETECT] = create_autodetect_quirks,
Daniel Macke5779992010-03-04 19:46:13 +0100567 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
568 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
569 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200570 [QUIRK_MIDI_ROLAND] = create_any_midi_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100571 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
572 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
Clemens Ladischc7f57212010-10-22 18:20:48 +0200573 [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100574 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
575 [QUIRK_MIDI_CME] = create_any_midi_quirk,
Krzysztof Foltman4434ade2010-05-20 20:31:10 +0100576 [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
Kristian Amlie1ef0e0a2011-08-26 13:19:49 +0200577 [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
Clemens Ladisch1ca8b202015-11-15 22:38:29 +0100578 [QUIRK_MIDI_CH345] = create_any_midi_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100579 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
580 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
581 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
Daniel Mack014950b2011-05-25 09:09:02 +0200582 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
Kai-Heng Feng1965c432020-08-23 18:58:50 +0800583 [QUIRK_SETUP_DISABLE_AUTOSUSPEND] = setup_disable_autosuspend,
Daniel Macke5779992010-03-04 19:46:13 +0100584 };
585
586 if (quirk->type < QUIRK_TYPE_COUNT) {
587 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
588 } else {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100589 usb_audio_err(chip, "invalid quirk type %d\n", quirk->type);
Daniel Macke5779992010-03-04 19:46:13 +0100590 return -ENXIO;
591 }
592}
593
594/*
595 * boot quirks
596 */
597
598#define EXTIGY_FIRMWARE_SIZE_OLD 794
599#define EXTIGY_FIRMWARE_SIZE_NEW 483
600
601static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
602{
603 struct usb_host_config *config = dev->actconfig;
604 int err;
605
606 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
607 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100608 dev_dbg(&dev->dev, "sending Extigy boot sequence...\n");
Daniel Macke5779992010-03-04 19:46:13 +0100609 /* Send message to force it to reconnect with full interface. */
610 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200611 0x10, 0x43, 0x0001, 0x000a, NULL, 0);
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100612 if (err < 0)
613 dev_dbg(&dev->dev, "error sending boot message: %d\n", err);
Daniel Macke5779992010-03-04 19:46:13 +0100614 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
615 &dev->descriptor, sizeof(dev->descriptor));
616 config = dev->actconfig;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100617 if (err < 0)
618 dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
Daniel Macke5779992010-03-04 19:46:13 +0100619 err = usb_reset_configuration(dev);
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100620 if (err < 0)
621 dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
622 dev_dbg(&dev->dev, "extigy_boot: new boot length = %d\n",
Daniel Macke5779992010-03-04 19:46:13 +0100623 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
624 return -ENODEV; /* quit this anyway */
625 }
626 return 0;
627}
628
629static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
630{
631 u8 buf = 1;
632
633 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
634 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200635 0, 0, &buf, 1);
Daniel Macke5779992010-03-04 19:46:13 +0100636 if (buf == 0) {
637 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
638 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200639 1, 2000, NULL, 0);
Daniel Macke5779992010-03-04 19:46:13 +0100640 return -ENODEV;
641 }
642 return 0;
643}
644
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200645static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
646{
647 int err;
648
649 if (dev->actconfig->desc.bConfigurationValue == 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100650 dev_info(&dev->dev,
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200651 "Fast Track Pro switching to config #2\n");
652 /* This function has to be available by the usb core module.
653 * if it is not avialable the boot quirk has to be left out
654 * and the configuration has to be set by udev or hotplug
655 * rules
656 */
657 err = usb_driver_set_configuration(dev, 2);
David Henningssonb98ae272013-01-04 17:02:18 +0100658 if (err < 0)
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100659 dev_dbg(&dev->dev,
660 "error usb_driver_set_configuration: %d\n",
661 err);
David Henningssonb98ae272013-01-04 17:02:18 +0100662 /* Always return an error, so that we stop creating a device
663 that will just be destroyed and recreated with a new
664 configuration */
665 return -ENODEV;
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200666 } else
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100667 dev_info(&dev->dev, "Fast Track Pro config OK\n");
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200668
669 return 0;
670}
671
Daniel Macke5779992010-03-04 19:46:13 +0100672/*
673 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
674 * documented in the device's data sheet.
675 */
676static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
677{
678 u8 buf[4];
679 buf[0] = 0x20;
680 buf[1] = value & 0xff;
681 buf[2] = (value >> 8) & 0xff;
682 buf[3] = reg;
683 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
684 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200685 0, 0, &buf, 4);
Daniel Macke5779992010-03-04 19:46:13 +0100686}
687
688static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
689{
690 /*
691 * Enable line-out driver mode, set headphone source to front
692 * channels, enable stereo mic.
693 */
694 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
695}
696
697/*
Linus Walleijad43d5282018-11-02 22:11:33 +0100698 * CM6206 registers from the CM6206 datasheet rev 2.1
Daniel Macke5779992010-03-04 19:46:13 +0100699 */
Linus Walleijad43d5282018-11-02 22:11:33 +0100700#define CM6206_REG0_DMA_MASTER BIT(15)
701#define CM6206_REG0_SPDIFO_RATE_48K (2 << 12)
702#define CM6206_REG0_SPDIFO_RATE_96K (7 << 12)
703/* Bit 4 thru 11 is the S/PDIF category code */
704#define CM6206_REG0_SPDIFO_CAT_CODE_GENERAL (0 << 4)
705#define CM6206_REG0_SPDIFO_EMPHASIS_CD BIT(3)
706#define CM6206_REG0_SPDIFO_COPYRIGHT_NA BIT(2)
707#define CM6206_REG0_SPDIFO_NON_AUDIO BIT(1)
708#define CM6206_REG0_SPDIFO_PRO_FORMAT BIT(0)
709
710#define CM6206_REG1_TEST_SEL_CLK BIT(14)
711#define CM6206_REG1_PLLBIN_EN BIT(13)
712#define CM6206_REG1_SOFT_MUTE_EN BIT(12)
713#define CM6206_REG1_GPIO4_OUT BIT(11)
714#define CM6206_REG1_GPIO4_OE BIT(10)
715#define CM6206_REG1_GPIO3_OUT BIT(9)
716#define CM6206_REG1_GPIO3_OE BIT(8)
717#define CM6206_REG1_GPIO2_OUT BIT(7)
718#define CM6206_REG1_GPIO2_OE BIT(6)
719#define CM6206_REG1_GPIO1_OUT BIT(5)
720#define CM6206_REG1_GPIO1_OE BIT(4)
721#define CM6206_REG1_SPDIFO_INVALID BIT(3)
722#define CM6206_REG1_SPDIF_LOOP_EN BIT(2)
723#define CM6206_REG1_SPDIFO_DIS BIT(1)
724#define CM6206_REG1_SPDIFI_MIX BIT(0)
725
726#define CM6206_REG2_DRIVER_ON BIT(15)
727#define CM6206_REG2_HEADP_SEL_SIDE_CHANNELS (0 << 13)
728#define CM6206_REG2_HEADP_SEL_SURROUND_CHANNELS (1 << 13)
729#define CM6206_REG2_HEADP_SEL_CENTER_SUBW (2 << 13)
730#define CM6206_REG2_HEADP_SEL_FRONT_CHANNELS (3 << 13)
731#define CM6206_REG2_MUTE_HEADPHONE_RIGHT BIT(12)
732#define CM6206_REG2_MUTE_HEADPHONE_LEFT BIT(11)
733#define CM6206_REG2_MUTE_REAR_SURROUND_RIGHT BIT(10)
734#define CM6206_REG2_MUTE_REAR_SURROUND_LEFT BIT(9)
735#define CM6206_REG2_MUTE_SIDE_SURROUND_RIGHT BIT(8)
736#define CM6206_REG2_MUTE_SIDE_SURROUND_LEFT BIT(7)
737#define CM6206_REG2_MUTE_SUBWOOFER BIT(6)
738#define CM6206_REG2_MUTE_CENTER BIT(5)
739#define CM6206_REG2_MUTE_RIGHT_FRONT BIT(3)
740#define CM6206_REG2_MUTE_LEFT_FRONT BIT(3)
741#define CM6206_REG2_EN_BTL BIT(2)
742#define CM6206_REG2_MCUCLKSEL_1_5_MHZ (0)
743#define CM6206_REG2_MCUCLKSEL_3_MHZ (1)
744#define CM6206_REG2_MCUCLKSEL_6_MHZ (2)
745#define CM6206_REG2_MCUCLKSEL_12_MHZ (3)
746
747/* Bit 11..13 sets the sensitivity to FLY tuner volume control VP/VD signal */
748#define CM6206_REG3_FLYSPEED_DEFAULT (2 << 11)
749#define CM6206_REG3_VRAP25EN BIT(10)
750#define CM6206_REG3_MSEL1 BIT(9)
751#define CM6206_REG3_SPDIFI_RATE_44_1K BIT(0 << 7)
752#define CM6206_REG3_SPDIFI_RATE_48K BIT(2 << 7)
753#define CM6206_REG3_SPDIFI_RATE_32K BIT(3 << 7)
754#define CM6206_REG3_PINSEL BIT(6)
755#define CM6206_REG3_FOE BIT(5)
756#define CM6206_REG3_ROE BIT(4)
757#define CM6206_REG3_CBOE BIT(3)
758#define CM6206_REG3_LOSE BIT(2)
759#define CM6206_REG3_HPOE BIT(1)
760#define CM6206_REG3_SPDIFI_CANREC BIT(0)
761
762#define CM6206_REG5_DA_RSTN BIT(13)
763#define CM6206_REG5_AD_RSTN BIT(12)
764#define CM6206_REG5_SPDIFO_AD2SPDO BIT(12)
765#define CM6206_REG5_SPDIFO_SEL_FRONT (0 << 9)
766#define CM6206_REG5_SPDIFO_SEL_SIDE_SUR (1 << 9)
767#define CM6206_REG5_SPDIFO_SEL_CEN_LFE (2 << 9)
768#define CM6206_REG5_SPDIFO_SEL_REAR_SUR (3 << 9)
769#define CM6206_REG5_CODECM BIT(8)
770#define CM6206_REG5_EN_HPF BIT(7)
771#define CM6206_REG5_T_SEL_DSDA4 BIT(6)
772#define CM6206_REG5_T_SEL_DSDA3 BIT(5)
773#define CM6206_REG5_T_SEL_DSDA2 BIT(4)
774#define CM6206_REG5_T_SEL_DSDA1 BIT(3)
775#define CM6206_REG5_T_SEL_DSDAD_NORMAL 0
776#define CM6206_REG5_T_SEL_DSDAD_FRONT 4
777#define CM6206_REG5_T_SEL_DSDAD_S_SURROUND 5
778#define CM6206_REG5_T_SEL_DSDAD_CEN_LFE 6
779#define CM6206_REG5_T_SEL_DSDAD_R_SURROUND 7
780
Daniel Macke5779992010-03-04 19:46:13 +0100781static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
782{
Daniel Mackdac8f842011-08-06 00:23:18 +0200783 int err = 0, reg;
Linus Walleijad43d5282018-11-02 22:11:33 +0100784 int val[] = {
785 /*
786 * Values here are chosen based on sniffing USB traffic
787 * under Windows.
788 *
789 * REG0: DAC is master, sample rate 48kHz, no copyright
790 */
791 CM6206_REG0_SPDIFO_RATE_48K |
792 CM6206_REG0_SPDIFO_COPYRIGHT_NA,
793 /*
794 * REG1: PLL binary search enable, soft mute enable.
795 */
796 CM6206_REG1_PLLBIN_EN |
Amadeusz Sławińskif5c95712019-01-08 21:03:11 +0100797 CM6206_REG1_SOFT_MUTE_EN,
Linus Walleijad43d5282018-11-02 22:11:33 +0100798 /*
799 * REG2: enable output drivers,
800 * select front channels to the headphone output,
801 * then mute the headphone channels, run the MCU
802 * at 1.5 MHz.
803 */
804 CM6206_REG2_DRIVER_ON |
805 CM6206_REG2_HEADP_SEL_FRONT_CHANNELS |
806 CM6206_REG2_MUTE_HEADPHONE_RIGHT |
807 CM6206_REG2_MUTE_HEADPHONE_LEFT,
808 /*
809 * REG3: default flyspeed, set 2.5V mic bias
810 * enable all line out ports and enable SPDIF
811 */
812 CM6206_REG3_FLYSPEED_DEFAULT |
813 CM6206_REG3_VRAP25EN |
814 CM6206_REG3_FOE |
815 CM6206_REG3_ROE |
816 CM6206_REG3_CBOE |
817 CM6206_REG3_LOSE |
818 CM6206_REG3_HPOE |
819 CM6206_REG3_SPDIFI_CANREC,
820 /* REG4 is just a bunch of GPIO lines */
821 0x0000,
822 /* REG5: de-assert AD/DA reset signals */
823 CM6206_REG5_DA_RSTN |
824 CM6206_REG5_AD_RSTN };
Daniel Macke5779992010-03-04 19:46:13 +0100825
826 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
827 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
828 if (err < 0)
829 return err;
830 }
831
832 return err;
833}
834
Takashi Iwai19570d72013-12-19 14:32:53 +0100835/* quirk for Plantronics GameCom 780 with CM6302 chip */
836static int snd_usb_gamecon780_boot_quirk(struct usb_device *dev)
837{
838 /* set the initial volume and don't change; other values are either
839 * too loud or silent due to firmware bug (bko#65251)
840 */
Paul S McSpadden542baf92014-08-03 17:47:36 -0500841 u8 buf[2] = { 0x74, 0xe3 };
Takashi Iwai19570d72013-12-19 14:32:53 +0100842 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
843 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
844 UAC_FU_VOLUME << 8, 9 << 8, buf, 2);
845}
846
Daniel Macke5779992010-03-04 19:46:13 +0100847/*
Mark Hills5e212332013-03-17 11:07:53 +0000848 * Novation Twitch DJ controller
Eduard Gilmutdinov11e424e2013-12-20 14:06:58 +0600849 * Focusrite Novation Saffire 6 USB audio card
Mark Hills5e212332013-03-17 11:07:53 +0000850 */
Eduard Gilmutdinov11e424e2013-12-20 14:06:58 +0600851static int snd_usb_novation_boot_quirk(struct usb_device *dev)
Mark Hills5e212332013-03-17 11:07:53 +0000852{
853 /* preemptively set up the device because otherwise the
854 * raw MIDI endpoints are not active */
855 usb_set_interface(dev, 0, 1);
856 return 0;
857}
858
859/*
Daniel Macke5779992010-03-04 19:46:13 +0100860 * This call will put the synth in "USB send" mode, i.e it will send MIDI
861 * messages through USB (this is disabled at startup). The synth will
862 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
863 * sign on its LCD. Values here are chosen based on sniffing USB traffic
864 * under Windows.
865 */
866static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
867{
868 int err, actual_length;
Daniel Macke5779992010-03-04 19:46:13 +0100869 /* "midi send" enable */
870 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
Takashi Iwai801ebf12019-06-24 15:08:28 +0200871 void *buf;
Daniel Macke5779992010-03-04 19:46:13 +0100872
Greg Kroah-Hartmanfcc2cc12020-09-14 17:37:46 +0200873 if (usb_pipe_type_check(dev, usb_sndintpipe(dev, 0x05)))
Takashi Iwai801ebf12019-06-24 15:08:28 +0200874 return -EINVAL;
875 buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
Daniel Macke5779992010-03-04 19:46:13 +0100876 if (!buf)
877 return -ENOMEM;
878 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
879 ARRAY_SIZE(seq), &actual_length, 1000);
880 kfree(buf);
881 if (err < 0)
882 return err;
883
884 return 0;
885}
886
887/*
Daniel Mack54a8c502011-02-11 11:08:06 +0000888 * Some sound cards from Native Instruments are in fact compliant to the USB
889 * audio standard of version 2 and other approved USB standards, even though
890 * they come up as vendor-specific device when first connected.
891 *
892 * However, they can be told to come up with a new set of descriptors
893 * upon their next enumeration, and the interfaces announced by the new
894 * descriptors will then be handled by the kernel's class drivers. As the
895 * product ID will also change, no further checks are required.
896 */
897
898static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
899{
Takashi Iwai801ebf12019-06-24 15:08:28 +0200900 int ret;
901
Takashi Iwai801ebf12019-06-24 15:08:28 +0200902 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
Daniel Mack54a8c502011-02-11 11:08:06 +0000903 0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Eldad Zack889d6682013-04-05 20:49:46 +0200904 1, 0, NULL, 0, 1000);
Daniel Mack54a8c502011-02-11 11:08:06 +0000905
906 if (ret < 0)
907 return ret;
908
909 usb_reset_device(dev);
910
911 /* return -EAGAIN, so the creation of an audio interface for this
912 * temporary device is aborted. The device will reconnect with a
913 * new product ID */
914 return -EAGAIN;
915}
916
Damien Zammitcb998642012-12-19 11:27:22 +0100917static void mbox2_setup_48_24_magic(struct usb_device *dev)
918{
919 u8 srate[3];
920 u8 temp[12];
921
922 /* Choose 48000Hz permanently */
923 srate[0] = 0x80;
924 srate[1] = 0xbb;
925 srate[2] = 0x00;
926
927 /* Send the magic! */
928 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
929 0x01, 0x22, 0x0100, 0x0085, &temp, 0x0003);
930 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
931 0x81, 0xa2, 0x0100, 0x0085, &srate, 0x0003);
932 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
933 0x81, 0xa2, 0x0100, 0x0086, &srate, 0x0003);
934 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
935 0x81, 0xa2, 0x0100, 0x0003, &srate, 0x0003);
936 return;
937}
938
939/* Digidesign Mbox 2 needs to load firmware onboard
940 * and driver must wait a few seconds for initialisation.
941 */
942
943#define MBOX2_FIRMWARE_SIZE 646
944#define MBOX2_BOOT_LOADING 0x01 /* Hard coded into the device */
945#define MBOX2_BOOT_READY 0x02 /* Hard coded into the device */
946
Damien Zammitb7b435e2013-01-04 09:51:44 +0100947static int snd_usb_mbox2_boot_quirk(struct usb_device *dev)
Damien Zammitcb998642012-12-19 11:27:22 +0100948{
949 struct usb_host_config *config = dev->actconfig;
950 int err;
Jiri Slaby4909a0c2013-02-17 14:33:04 +0100951 u8 bootresponse[0x12];
Damien Zammitcb998642012-12-19 11:27:22 +0100952 int fwsize;
953 int count;
954
955 fwsize = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
956
957 if (fwsize != MBOX2_FIRMWARE_SIZE) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100958 dev_err(&dev->dev, "Invalid firmware size=%d.\n", fwsize);
Damien Zammitcb998642012-12-19 11:27:22 +0100959 return -ENODEV;
960 }
961
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100962 dev_dbg(&dev->dev, "Sending Digidesign Mbox 2 boot sequence...\n");
Damien Zammitcb998642012-12-19 11:27:22 +0100963
964 count = 0;
Damien Zammitb7b435e2013-01-04 09:51:44 +0100965 bootresponse[0] = MBOX2_BOOT_LOADING;
966 while ((bootresponse[0] == MBOX2_BOOT_LOADING) && (count < 10)) {
Damien Zammitcb998642012-12-19 11:27:22 +0100967 msleep(500); /* 0.5 second delay */
968 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
969 /* Control magic - load onboard firmware */
970 0x85, 0xc0, 0x0001, 0x0000, &bootresponse, 0x0012);
Damien Zammitb7b435e2013-01-04 09:51:44 +0100971 if (bootresponse[0] == MBOX2_BOOT_READY)
Damien Zammitcb998642012-12-19 11:27:22 +0100972 break;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100973 dev_dbg(&dev->dev, "device not ready, resending boot sequence...\n");
Damien Zammitcb998642012-12-19 11:27:22 +0100974 count++;
975 }
976
Damien Zammitb7b435e2013-01-04 09:51:44 +0100977 if (bootresponse[0] != MBOX2_BOOT_READY) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100978 dev_err(&dev->dev, "Unknown bootresponse=%d, or timed out, ignoring device.\n", bootresponse[0]);
Damien Zammitcb998642012-12-19 11:27:22 +0100979 return -ENODEV;
980 }
981
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100982 dev_dbg(&dev->dev, "device initialised!\n");
Damien Zammitcb998642012-12-19 11:27:22 +0100983
984 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
985 &dev->descriptor, sizeof(dev->descriptor));
986 config = dev->actconfig;
987 if (err < 0)
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100988 dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
Damien Zammitcb998642012-12-19 11:27:22 +0100989
990 err = usb_reset_configuration(dev);
991 if (err < 0)
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100992 dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
993 dev_dbg(&dev->dev, "mbox2_boot: new boot length = %d\n",
Damien Zammitcb998642012-12-19 11:27:22 +0100994 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
995
996 mbox2_setup_48_24_magic(dev);
997
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100998 dev_info(&dev->dev, "Digidesign Mbox 2: 24bit 48kHz");
Damien Zammitcb998642012-12-19 11:27:22 +0100999
1000 return 0; /* Successful boot */
1001}
1002
Alberto Aguirre16bafa72018-05-08 17:14:13 -05001003static int snd_usb_axefx3_boot_quirk(struct usb_device *dev)
1004{
1005 int err;
1006
1007 dev_dbg(&dev->dev, "Waiting for Axe-Fx III to boot up...\n");
1008
1009 /* If the Axe-Fx III has not fully booted, it will timeout when trying
1010 * to enable the audio streaming interface. A more generous timeout is
1011 * used here to detect when the Axe-Fx III has finished booting as the
1012 * set interface message will be acked once it has
1013 */
1014 err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1015 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1016 1, 1, NULL, 0, 120000);
1017 if (err < 0) {
1018 dev_err(&dev->dev,
1019 "failed waiting for Axe-Fx III to boot: %d\n", err);
1020 return err;
1021 }
1022
1023 dev_dbg(&dev->dev, "Axe-Fx III is now ready\n");
1024
1025 err = usb_set_interface(dev, 1, 0);
1026 if (err < 0)
1027 dev_dbg(&dev->dev,
1028 "error stopping Axe-Fx III interface: %d\n", err);
1029
1030 return 0;
1031}
1032
Manuel Reinhardta6340902019-02-28 20:34:04 +01001033
1034#define MICROBOOK_BUF_SIZE 128
1035
1036static int snd_usb_motu_microbookii_communicate(struct usb_device *dev, u8 *buf,
1037 int buf_size, int *length)
1038{
1039 int err, actual_length;
1040
Greg Kroah-Hartmanfcc2cc12020-09-14 17:37:46 +02001041 if (usb_pipe_type_check(dev, usb_sndintpipe(dev, 0x01)))
Takashi Iwai801ebf12019-06-24 15:08:28 +02001042 return -EINVAL;
Manuel Reinhardta6340902019-02-28 20:34:04 +01001043 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x01), buf, *length,
1044 &actual_length, 1000);
1045 if (err < 0)
1046 return err;
1047
1048 print_hex_dump(KERN_DEBUG, "MicroBookII snd: ", DUMP_PREFIX_NONE, 16, 1,
1049 buf, actual_length, false);
1050
1051 memset(buf, 0, buf_size);
1052
Greg Kroah-Hartmanfcc2cc12020-09-14 17:37:46 +02001053 if (usb_pipe_type_check(dev, usb_rcvintpipe(dev, 0x82)))
Takashi Iwai801ebf12019-06-24 15:08:28 +02001054 return -EINVAL;
Manuel Reinhardta6340902019-02-28 20:34:04 +01001055 err = usb_interrupt_msg(dev, usb_rcvintpipe(dev, 0x82), buf, buf_size,
1056 &actual_length, 1000);
1057 if (err < 0)
1058 return err;
1059
1060 print_hex_dump(KERN_DEBUG, "MicroBookII rcv: ", DUMP_PREFIX_NONE, 16, 1,
1061 buf, actual_length, false);
1062
1063 *length = actual_length;
1064 return 0;
1065}
1066
1067static int snd_usb_motu_microbookii_boot_quirk(struct usb_device *dev)
1068{
1069 int err, actual_length, poll_attempts = 0;
1070 static const u8 set_samplerate_seq[] = { 0x00, 0x00, 0x00, 0x00,
1071 0x00, 0x00, 0x0b, 0x14,
1072 0x00, 0x00, 0x00, 0x01 };
1073 static const u8 poll_ready_seq[] = { 0x00, 0x04, 0x00, 0x00,
1074 0x00, 0x00, 0x0b, 0x18 };
1075 u8 *buf = kzalloc(MICROBOOK_BUF_SIZE, GFP_KERNEL);
1076
1077 if (!buf)
1078 return -ENOMEM;
1079
1080 dev_info(&dev->dev, "Waiting for MOTU Microbook II to boot up...\n");
1081
1082 /* First we tell the device which sample rate to use. */
1083 memcpy(buf, set_samplerate_seq, sizeof(set_samplerate_seq));
1084 actual_length = sizeof(set_samplerate_seq);
1085 err = snd_usb_motu_microbookii_communicate(dev, buf, MICROBOOK_BUF_SIZE,
1086 &actual_length);
1087
1088 if (err < 0) {
1089 dev_err(&dev->dev,
1090 "failed setting the sample rate for Motu MicroBook II: %d\n",
1091 err);
1092 goto free_buf;
1093 }
1094
1095 /* Then we poll every 100 ms until the device informs of its readiness. */
1096 while (true) {
1097 if (++poll_attempts > 100) {
1098 dev_err(&dev->dev,
1099 "failed booting Motu MicroBook II: timeout\n");
1100 err = -ENODEV;
1101 goto free_buf;
1102 }
1103
1104 memset(buf, 0, MICROBOOK_BUF_SIZE);
1105 memcpy(buf, poll_ready_seq, sizeof(poll_ready_seq));
1106
1107 actual_length = sizeof(poll_ready_seq);
1108 err = snd_usb_motu_microbookii_communicate(
1109 dev, buf, MICROBOOK_BUF_SIZE, &actual_length);
1110 if (err < 0) {
1111 dev_err(&dev->dev,
1112 "failed booting Motu MicroBook II: communication error %d\n",
1113 err);
1114 goto free_buf;
1115 }
1116
1117 /* the device signals its readiness through a message of the
1118 * form
1119 * XX 06 00 00 00 00 0b 18 00 00 00 01
1120 * If the device is not yet ready to accept audio data, the
1121 * last byte of that sequence is 00.
1122 */
1123 if (actual_length == 12 && buf[actual_length - 1] == 1)
1124 break;
1125
1126 msleep(100);
1127 }
1128
1129 dev_info(&dev->dev, "MOTU MicroBook II ready\n");
1130
1131free_buf:
1132 kfree(buf);
1133 return err;
1134}
1135
Alexander Tsoy73ac9f52020-01-12 13:23:58 +03001136static int snd_usb_motu_m_series_boot_quirk(struct usb_device *dev)
1137{
Alexander Tsoy73ac9f52020-01-12 13:23:58 +03001138 msleep(2000);
1139
Alexander Tsoy73ac9f52020-01-12 13:23:58 +03001140 return 0;
1141}
1142
Daniel Mack54a8c502011-02-11 11:08:06 +00001143/*
Daniel Macke5779992010-03-04 19:46:13 +01001144 * Setup quirks
1145 */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001146#define MAUDIO_SET 0x01 /* parse device_setup */
1147#define MAUDIO_SET_COMPATIBLE 0x80 /* use only "win-compatible" interfaces */
1148#define MAUDIO_SET_DTS 0x02 /* enable DTS Digital Output */
Alexander Tsoy5ff40e62020-06-29 06:26:07 +03001149#define MAUDIO_SET_96K 0x04 /* 48-96kHz rate if set, 8-48kHz otherwise */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001150#define MAUDIO_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
1151#define MAUDIO_SET_DI 0x10 /* enable Digital Input */
1152#define MAUDIO_SET_MASK 0x1f /* bit mask for setup value */
Alexander Tsoy5ff40e62020-06-29 06:26:07 +03001153#define MAUDIO_SET_24B_48K_DI 0x19 /* 24bits+48kHz+Digital Input */
1154#define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48kHz+No Digital Input */
1155#define MAUDIO_SET_16B_48K_DI 0x11 /* 16bits+48kHz+Digital Input */
1156#define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48kHz+No Digital Input */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001157
1158static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
1159 int iface, int altno)
1160{
1161 /* Reset ALL ifaces to 0 altsetting.
1162 * Call it for every possible altsetting of every interface.
1163 */
1164 usb_set_interface(chip->dev, iface, 0);
1165 if (chip->setup & MAUDIO_SET) {
1166 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
1167 if (iface != 1 && iface != 2)
1168 return 1; /* skip all interfaces but 1 and 2 */
1169 } else {
1170 unsigned int mask;
1171 if (iface == 1 || iface == 2)
1172 return 1; /* skip interfaces 1 and 2 */
1173 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
1174 return 1; /* skip this altsetting */
1175 mask = chip->setup & MAUDIO_SET_MASK;
1176 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
1177 return 1; /* skip this altsetting */
1178 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
1179 return 1; /* skip this altsetting */
1180 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
1181 return 1; /* skip this altsetting */
1182 }
1183 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001184 usb_audio_dbg(chip,
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001185 "using altsetting %d for interface %d config %d\n",
1186 altno, iface, chip->setup);
1187 return 0; /* keep this altsetting */
1188}
Daniel Macke5779992010-03-04 19:46:13 +01001189
1190static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
1191 int iface,
1192 int altno)
1193{
1194 /* Reset ALL ifaces to 0 altsetting.
1195 * Call it for every possible altsetting of every interface.
1196 */
1197 usb_set_interface(chip->dev, iface, 0);
1198
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001199 if (chip->setup & MAUDIO_SET) {
1200 unsigned int mask;
1201 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
Daniel Macke5779992010-03-04 19:46:13 +01001202 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001203 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
Daniel Macke5779992010-03-04 19:46:13 +01001204 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001205 mask = chip->setup & MAUDIO_SET_MASK;
1206 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
Daniel Macke5779992010-03-04 19:46:13 +01001207 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001208 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
Daniel Macke5779992010-03-04 19:46:13 +01001209 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001210 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
Daniel Macke5779992010-03-04 19:46:13 +01001211 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001212 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
Daniel Macke5779992010-03-04 19:46:13 +01001213 return 1; /* skip this altsetting */
1214 }
1215
1216 return 0; /* keep this altsetting */
1217}
1218
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001219static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
1220 int iface, int altno)
1221{
1222 /* Reset ALL ifaces to 0 altsetting.
1223 * Call it for every possible altsetting of every interface.
1224 */
1225 usb_set_interface(chip->dev, iface, 0);
1226
1227 /* possible configuration where both inputs and only one output is
1228 *used is not supported by the current setup
1229 */
1230 if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
1231 if (chip->setup & MAUDIO_SET_96K) {
1232 if (altno != 3 && altno != 6)
1233 return 1;
1234 } else if (chip->setup & MAUDIO_SET_DI) {
1235 if (iface == 4)
1236 return 1; /* no analog input */
1237 if (altno != 2 && altno != 5)
1238 return 1; /* enable only altsets 2 and 5 */
1239 } else {
1240 if (iface == 5)
1241 return 1; /* disable digialt input */
1242 if (altno != 2 && altno != 5)
1243 return 1; /* enalbe only altsets 2 and 5 */
1244 }
1245 } else {
1246 /* keep only 16-Bit mode */
1247 if (altno != 1)
1248 return 1;
1249 }
1250
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001251 usb_audio_dbg(chip,
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001252 "using altsetting %d for interface %d config %d\n",
1253 altno, iface, chip->setup);
1254 return 0; /* keep this altsetting */
1255}
1256
Nick Kossifidis8dc5efe2020-02-15 03:23:35 +02001257static int s1810c_skip_setting_quirk(struct snd_usb_audio *chip,
1258 int iface, int altno)
1259{
1260 /*
1261 * Altno settings:
1262 *
1263 * Playback (Interface 1):
1264 * 1: 6 Analog + 2 S/PDIF
1265 * 2: 6 Analog + 2 S/PDIF
1266 * 3: 6 Analog
1267 *
1268 * Capture (Interface 2):
1269 * 1: 8 Analog + 2 S/PDIF + 8 ADAT
1270 * 2: 8 Analog + 2 S/PDIF + 4 ADAT
1271 * 3: 8 Analog
1272 */
1273
1274 /*
1275 * I'll leave 2 as the default one and
1276 * use device_setup to switch to the
1277 * other two.
1278 */
1279 if ((chip->setup == 0 || chip->setup > 2) && altno != 2)
1280 return 1;
1281 else if (chip->setup == 1 && altno != 1)
1282 return 1;
1283 else if (chip->setup == 2 && altno != 3)
1284 return 1;
1285
1286 return 0;
1287}
1288
Daniel Macke5779992010-03-04 19:46:13 +01001289int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
1290 int iface,
1291 int altno)
1292{
1293 /* audiophile usb: skip altsets incompatible with device_setup */
1294 if (chip->usb_id == USB_ID(0x0763, 0x2003))
1295 return audiophile_skip_setting_quirk(chip, iface, altno);
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001296 /* quattro usb: skip altsets incompatible with device_setup */
1297 if (chip->usb_id == USB_ID(0x0763, 0x2001))
1298 return quattro_skip_setting_quirk(chip, iface, altno);
1299 /* fasttrackpro usb: skip altsets incompatible with device_setup */
1300 if (chip->usb_id == USB_ID(0x0763, 0x2012))
1301 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
Nick Kossifidis8dc5efe2020-02-15 03:23:35 +02001302 /* presonus studio 1810c: skip altsets incompatible with device_setup */
1303 if (chip->usb_id == USB_ID(0x0194f, 0x010c))
1304 return s1810c_skip_setting_quirk(chip, iface, altno);
1305
Daniel Macke5779992010-03-04 19:46:13 +01001306
1307 return 0;
1308}
1309
1310int snd_usb_apply_boot_quirk(struct usb_device *dev,
1311 struct usb_interface *intf,
Takashi Iwai79289e22016-01-11 11:33:34 +01001312 const struct snd_usb_audio_quirk *quirk,
1313 unsigned int id)
Daniel Macke5779992010-03-04 19:46:13 +01001314{
Daniel Mack3347b262011-02-11 11:34:12 +00001315 switch (id) {
1316 case USB_ID(0x041e, 0x3000):
1317 /* SB Extigy needs special boot-up sequence */
1318 /* if more models come, this will go to the quirk list. */
Daniel Macke5779992010-03-04 19:46:13 +01001319 return snd_usb_extigy_boot_quirk(dev, intf);
1320
Daniel Mack3347b262011-02-11 11:34:12 +00001321 case USB_ID(0x041e, 0x3020):
1322 /* SB Audigy 2 NX needs its own boot-up magic, too */
Daniel Macke5779992010-03-04 19:46:13 +01001323 return snd_usb_audigy2nx_boot_quirk(dev);
1324
Daniel Mack3347b262011-02-11 11:34:12 +00001325 case USB_ID(0x10f5, 0x0200):
1326 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
Daniel Macke5779992010-03-04 19:46:13 +01001327 return snd_usb_cm106_boot_quirk(dev);
1328
Daniel Mack3347b262011-02-11 11:34:12 +00001329 case USB_ID(0x0d8c, 0x0102):
1330 /* C-Media CM6206 / CM106-Like Sound Device */
Wolfgang Breyha8129e792011-04-28 16:18:40 +02001331 case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
Daniel Macke5779992010-03-04 19:46:13 +01001332 return snd_usb_cm6206_boot_quirk(dev);
1333
Damien Zammitcb998642012-12-19 11:27:22 +01001334 case USB_ID(0x0dba, 0x3000):
1335 /* Digidesign Mbox 2 */
1336 return snd_usb_mbox2_boot_quirk(dev);
1337
Eduard Gilmutdinov11e424e2013-12-20 14:06:58 +06001338 case USB_ID(0x1235, 0x0010): /* Focusrite Novation Saffire 6 USB */
1339 case USB_ID(0x1235, 0x0018): /* Focusrite Novation Twitch */
1340 return snd_usb_novation_boot_quirk(dev);
Mark Hills5e212332013-03-17 11:07:53 +00001341
Daniel Mack3347b262011-02-11 11:34:12 +00001342 case USB_ID(0x133e, 0x0815):
1343 /* Access Music VirusTI Desktop */
Daniel Macke5779992010-03-04 19:46:13 +01001344 return snd_usb_accessmusic_boot_quirk(dev);
1345
Daniel Mack759e890f2011-05-18 11:28:41 +02001346 case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
Daniel Mack3347b262011-02-11 11:34:12 +00001347 case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
1348 case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
Daniel Mack54a8c502011-02-11 11:08:06 +00001349 return snd_usb_nativeinstruments_boot_quirk(dev);
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001350 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
1351 return snd_usb_fasttrackpro_boot_quirk(dev);
Takashi Iwai19570d72013-12-19 14:32:53 +01001352 case USB_ID(0x047f, 0xc010): /* Plantronics Gamecom 780 */
1353 return snd_usb_gamecon780_boot_quirk(dev);
Alberto Aguirre16bafa72018-05-08 17:14:13 -05001354 case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx 3 */
1355 return snd_usb_axefx3_boot_quirk(dev);
Manuel Reinhardta6340902019-02-28 20:34:04 +01001356 case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II */
Alexander Tsoy2edb84e2020-02-29 18:18:15 +03001357 /*
1358 * For some reason interface 3 with vendor-spec class is
1359 * detected on MicroBook IIc.
1360 */
1361 if (get_iface_desc(intf->altsetting)->bInterfaceClass ==
1362 USB_CLASS_VENDOR_SPEC &&
1363 get_iface_desc(intf->altsetting)->bInterfaceNumber < 3)
1364 return snd_usb_motu_microbookii_boot_quirk(dev);
1365 break;
Daniel Mack3347b262011-02-11 11:34:12 +00001366 }
Daniel Mack54a8c502011-02-11 11:08:06 +00001367
Daniel Macke5779992010-03-04 19:46:13 +01001368 return 0;
1369}
1370
Alexander Tsoy73ac9f52020-01-12 13:23:58 +03001371int snd_usb_apply_boot_quirk_once(struct usb_device *dev,
1372 struct usb_interface *intf,
1373 const struct snd_usb_audio_quirk *quirk,
1374 unsigned int id)
1375{
1376 switch (id) {
1377 case USB_ID(0x07fd, 0x0008): /* MOTU M Series */
1378 return snd_usb_motu_m_series_boot_quirk(dev);
1379 }
1380
1381 return 0;
1382}
1383
Daniel Macke5779992010-03-04 19:46:13 +01001384/*
1385 * check if the device uses big-endian samples
1386 */
Takashi Iwaicab941b2020-11-23 09:53:33 +01001387int snd_usb_is_big_endian_format(struct snd_usb_audio *chip,
1388 const struct audioformat *fp)
Daniel Macke5779992010-03-04 19:46:13 +01001389{
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04001390 /* it depends on altsetting whether the device is big-endian or not */
Daniel Macke5779992010-03-04 19:46:13 +01001391 switch (chip->usb_id) {
1392 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001393 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1394 fp->altsetting == 5 || fp->altsetting == 6)
Daniel Macke5779992010-03-04 19:46:13 +01001395 return 1;
1396 break;
1397 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
1398 if (chip->setup == 0x00 ||
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001399 fp->altsetting == 1 || fp->altsetting == 2 ||
1400 fp->altsetting == 3)
Daniel Macke5779992010-03-04 19:46:13 +01001401 return 1;
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001402 break;
1403 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
1404 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1405 fp->altsetting == 5 || fp->altsetting == 6)
1406 return 1;
1407 break;
Daniel Macke5779992010-03-04 19:46:13 +01001408 }
1409 return 0;
1410}
1411
1412/*
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -05001413 * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
Daniel Macke5779992010-03-04 19:46:13 +01001414 * not for interface.
1415 */
1416
1417enum {
1418 EMU_QUIRK_SR_44100HZ = 0,
1419 EMU_QUIRK_SR_48000HZ,
1420 EMU_QUIRK_SR_88200HZ,
1421 EMU_QUIRK_SR_96000HZ,
1422 EMU_QUIRK_SR_176400HZ,
1423 EMU_QUIRK_SR_192000HZ
1424};
1425
1426static void set_format_emu_quirk(struct snd_usb_substream *subs,
Takashi Iwaicab941b2020-11-23 09:53:33 +01001427 const struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +01001428{
1429 unsigned char emu_samplerate_id = 0;
1430
1431 /* When capture is active
1432 * sample rate shouldn't be changed
1433 * by playback substream
1434 */
1435 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
Takashi Iwai6aa719d2020-11-23 09:53:36 +01001436 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].cur_audiofmt)
Daniel Macke5779992010-03-04 19:46:13 +01001437 return;
1438 }
1439
1440 switch (fmt->rate_min) {
1441 case 48000:
1442 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
1443 break;
1444 case 88200:
1445 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
1446 break;
1447 case 96000:
1448 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
1449 break;
1450 case 176400:
1451 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
1452 break;
1453 case 192000:
1454 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
1455 break;
1456 default:
1457 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
1458 break;
1459 }
1460 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
Calvin Owens1539d4f2013-04-12 22:33:59 -05001461 subs->pkt_offset_adj = (emu_samplerate_id >= EMU_QUIRK_SR_176400HZ) ? 4 : 0;
Daniel Macke5779992010-03-04 19:46:13 +01001462}
1463
Olivia Mackintosh3b85f5f2021-02-02 13:42:26 +00001464static int pioneer_djm_set_format_quirk(struct snd_usb_substream *subs,
1465 u16 windex)
1466{
1467 unsigned int cur_rate = subs->data_endpoint->cur_rate;
1468 u8 sr[3];
1469 // Convert to little endian
1470 sr[0] = cur_rate & 0xff;
1471 sr[1] = (cur_rate >> 8) & 0xff;
1472 sr[2] = (cur_rate >> 16) & 0xff;
1473 usb_set_interface(subs->dev, 0, 1);
1474 // we should derive windex from fmt-sync_ep but it's not set
1475 snd_usb_ctl_msg(subs->stream->chip->dev,
Nicolas MURE2c911902021-03-01 15:29:27 +01001476 usb_sndctrlpipe(subs->stream->chip->dev, 0),
Olivia Mackintosh3b85f5f2021-02-02 13:42:26 +00001477 0x01, 0x22, 0x0100, windex, &sr, 0x0003);
1478 return 0;
1479}
1480
Daniel Macke5779992010-03-04 19:46:13 +01001481void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
Takashi Iwaicab941b2020-11-23 09:53:33 +01001482 const struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +01001483{
1484 switch (subs->stream->chip->usb_id) {
1485 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
1486 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
1487 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -05001488 case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
Daniel Macke5779992010-03-04 19:46:13 +01001489 set_format_emu_quirk(subs, fmt);
1490 break;
Hector Martin1b7ecc22020-08-10 17:24:00 +09001491 case USB_ID(0x534d, 0x2109): /* MacroSilicon MS2109 */
1492 subs->stream_offset_adj = 2;
1493 break;
Olivia Mackintosh3b85f5f2021-02-02 13:42:26 +00001494 case USB_ID(0x2b73, 0x0013): /* Pioneer DJM-450 */
1495 pioneer_djm_set_format_quirk(subs, 0x0082);
1496 break;
Olivia Mackintoshe7df7df2021-04-18 17:59:01 +01001497 case USB_ID(0x08e4, 0x017f): /* Pioneer DJM-750 */
Nicolas MURE1a2a94a2021-03-01 16:27:28 +01001498 case USB_ID(0x08e4, 0x0163): /* Pioneer DJM-850 */
1499 pioneer_djm_set_format_quirk(subs, 0x0086);
1500 break;
Daniel Macke5779992010-03-04 19:46:13 +01001501 }
1502}
1503
Takashi Iwaid767aba2020-11-23 09:53:24 +01001504int snd_usb_select_mode_quirk(struct snd_usb_audio *chip,
Takashi Iwaicab941b2020-11-23 09:53:33 +01001505 const struct audioformat *fmt)
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001506{
Takashi Iwaid767aba2020-11-23 09:53:24 +01001507 struct usb_device *dev = chip->dev;
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001508 int err;
1509
Takashi Iwai2de00d52021-07-29 09:38:53 +02001510 if (chip->quirk_flags & QUIRK_FLAG_ITF_USB_DSD_DAC) {
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001511 /* First switch to alt set 0, otherwise the mode switch cmd
1512 * will not be accepted by the DAC
1513 */
1514 err = usb_set_interface(dev, fmt->iface, 0);
1515 if (err < 0)
1516 return err;
1517
Jia-Ju Baidf3f0342018-07-27 16:55:28 +08001518 msleep(20); /* Delay needed after setting the interface */
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001519
Nobutaka Okabe7f38ca02016-12-13 01:24:08 +09001520 /* Vendor mode switch cmd is required. */
Nobutaka Okabef3b906d2018-03-23 19:21:13 +09001521 if (fmt->formats & SNDRV_PCM_FMTBIT_DSD_U32_BE) {
1522 /* DSD mode (DSD_U32) requested */
Nobutaka Okabe7f38ca02016-12-13 01:24:08 +09001523 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1524 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1525 1, 1, NULL, 0);
1526 if (err < 0)
1527 return err;
Nobutaka Okabe7f38ca02016-12-13 01:24:08 +09001528
Nobutaka Okabef3b906d2018-03-23 19:21:13 +09001529 } else {
1530 /* PCM or DOP mode (S32) requested */
1531 /* PCM mode (S16) requested */
Nobutaka Okabe7f38ca02016-12-13 01:24:08 +09001532 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1533 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1534 0, 1, NULL, 0);
1535 if (err < 0)
1536 return err;
Nobutaka Okabef3b906d2018-03-23 19:21:13 +09001537
Nobutaka Okabe7f38ca02016-12-13 01:24:08 +09001538 }
Jia-Ju Baidf3f0342018-07-27 16:55:28 +08001539 msleep(20);
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001540 }
1541 return 0;
1542}
1543
Daniel Mack2b58fd52012-09-04 10:23:07 +02001544void snd_usb_endpoint_start_quirk(struct snd_usb_endpoint *ep)
1545{
1546 /*
1547 * "Playback Design" products send bogus feedback data at the start
1548 * of the stream. Ignore them.
1549 */
Takashi Iwai79289e22016-01-11 11:33:34 +01001550 if (USB_ID_VENDOR(ep->chip->usb_id) == 0x23ba &&
Daniel Mack2b58fd52012-09-04 10:23:07 +02001551 ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
1552 ep->skip_packets = 4;
Eldad Zack83e3acd2013-01-13 23:02:03 +01001553
1554 /*
Matt Gruskine9a25e02013-02-09 12:56:35 -05001555 * M-Audio Fast Track C400/C600 - when packets are not skipped, real
Alexander Tsoy5ff40e62020-06-29 06:26:07 +03001556 * world latency varies by approx. +/- 50 frames (at 96kHz) each time
Matt Gruskine9a25e02013-02-09 12:56:35 -05001557 * the stream is (re)started. When skipping packets 16 at endpoint
1558 * start up, the real world latency is stable within +/- 1 frame (also
Eldad Zack83e3acd2013-01-13 23:02:03 +01001559 * across power cycles).
1560 */
Matt Gruskine9a25e02013-02-09 12:56:35 -05001561 if ((ep->chip->usb_id == USB_ID(0x0763, 0x2030) ||
1562 ep->chip->usb_id == USB_ID(0x0763, 0x2031)) &&
Eldad Zack83e3acd2013-01-13 23:02:03 +01001563 ep->type == SND_USB_ENDPOINT_TYPE_DATA)
1564 ep->skip_packets = 16;
Daniel Mack9abc1342016-08-22 08:53:36 +02001565
1566 /* Work around devices that report unreasonable feedback data */
Daniel Mackca0dd272016-08-22 08:53:37 +02001567 if ((ep->chip->usb_id == USB_ID(0x0644, 0x8038) || /* TEAC UD-H01 */
1568 ep->chip->usb_id == USB_ID(0x1852, 0x5034)) && /* T+A Dac8 */
Daniel Mack9abc1342016-08-22 08:53:36 +02001569 ep->syncmaxsize == 4)
Daniel Mackca0dd272016-08-22 08:53:37 +02001570 ep->tenor_fb_quirk = 1;
Daniel Mack2b58fd52012-09-04 10:23:07 +02001571}
1572
Takashi Iwaid767aba2020-11-23 09:53:24 +01001573void snd_usb_set_interface_quirk(struct snd_usb_audio *chip)
Daniel Mack21bb5aa2013-04-10 00:56:03 +08001574{
Takashi Iwai79289e22016-01-11 11:33:34 +01001575 if (!chip)
1576 return;
Daniel Mack21bb5aa2013-04-10 00:56:03 +08001577 /*
1578 * "Playback Design" products need a 50ms delay after setting the
1579 * USB interface.
1580 */
Takashi Iwai79289e22016-01-11 11:33:34 +01001581 switch (USB_ID_VENDOR(chip->usb_id)) {
Guillaume Fougnies5a4ff9e2016-01-26 00:28:27 +01001582 case 0x23ba: /* Playback Design */
1583 case 0x0644: /* TEAC Corp. */
Jia-Ju Baidf3f0342018-07-27 16:55:28 +08001584 msleep(50);
Guillaume Fougnies5a4ff9e2016-01-26 00:28:27 +01001585 break;
1586 }
Daniel Mack21bb5aa2013-04-10 00:56:03 +08001587}
1588
Takashi Iwai79289e22016-01-11 11:33:34 +01001589/* quirk applied after snd_usb_ctl_msg(); not applied during boot quirks */
Daniel Mack2b58fd52012-09-04 10:23:07 +02001590void snd_usb_ctl_msg_quirk(struct usb_device *dev, unsigned int pipe,
1591 __u8 request, __u8 requesttype, __u16 value,
1592 __u16 index, void *data, __u16 size)
1593{
Takashi Iwai79289e22016-01-11 11:33:34 +01001594 struct snd_usb_audio *chip = dev_get_drvdata(&dev->dev);
1595
1596 if (!chip)
1597 return;
Daniel Mack2b58fd52012-09-04 10:23:07 +02001598 /*
1599 * "Playback Design" products need a 20ms delay after each
1600 * class compliant request
1601 */
Takashi Iwai79289e22016-01-11 11:33:34 +01001602 if (USB_ID_VENDOR(chip->usb_id) == 0x23ba &&
Daniel Mack2b58fd52012-09-04 10:23:07 +02001603 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
Jia-Ju Baidf3f0342018-07-27 16:55:28 +08001604 msleep(20);
Jurgen Kramer6e84a8d2014-11-15 14:01:21 +01001605
Guillaume Fougnies5a4ff9e2016-01-26 00:28:27 +01001606 /*
1607 * "TEAC Corp." products need a 20ms delay after each
1608 * class compliant request
1609 */
Takashi Iwai79289e22016-01-11 11:33:34 +01001610 if (USB_ID_VENDOR(chip->usb_id) == 0x0644 &&
Guillaume Fougnies5a4ff9e2016-01-26 00:28:27 +01001611 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
Jia-Ju Baidf3f0342018-07-27 16:55:28 +08001612 msleep(20);
Guillaume Fougnies5a4ff9e2016-01-26 00:28:27 +01001613
Nobutaka Okabe71426532018-03-23 19:18:22 +09001614 /* ITF-USB DSD based DACs functionality need a delay
Jurgen Kramer6e84a8d2014-11-15 14:01:21 +01001615 * after each class compliant request
1616 */
Takashi Iwai2de00d52021-07-29 09:38:53 +02001617 if ((chip->quirk_flags & QUIRK_FLAG_ITF_USB_DSD_DAC)
Takashi Iwai8b28c932015-03-04 15:37:01 +01001618 && (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
Jia-Ju Baidf3f0342018-07-27 16:55:28 +08001619 msleep(20);
Panu Matilainendacacb02014-11-30 18:45:40 +02001620
John Ernbergfc7c5c22021-03-03 18:14:39 +00001621 /*
Takashi Iwai06abcb12021-03-04 09:50:09 +01001622 * Plantronics headsets (C320, C320-M, etc) need a delay to avoid
1623 * random microhpone failures.
John Ernbergfc7c5c22021-03-03 18:14:39 +00001624 */
Takashi Iwai06abcb12021-03-04 09:50:09 +01001625 if (USB_ID_VENDOR(chip->usb_id) == 0x047f &&
John Ernbergfc7c5c22021-03-03 18:14:39 +00001626 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1627 msleep(20);
1628
Joakim Tjernlund54a2a382020-11-17 13:28:03 +01001629 /* Zoom R16/24, many Logitech(at least H650e/H570e/BCC950),
1630 * Jabra 550a, Kingston HyperX needs a tiny delay here,
1631 * otherwise requests like get/set frequency return
1632 * as failed despite actually succeeding.
Panu Matilainendacacb02014-11-30 18:45:40 +02001633 */
Joakim Tjernlund07b3b5e92017-08-22 08:33:53 +02001634 if ((chip->usb_id == USB_ID(0x1686, 0x00dd) ||
Joakim Tjernlund54a2a382020-11-17 13:28:03 +01001635 USB_ID_VENDOR(chip->usb_id) == 0x046d || /* Logitech */
Jesus Ramos073919e2020-04-27 06:21:39 -07001636 chip->usb_id == USB_ID(0x0b0e, 0x0349) ||
1637 chip->usb_id == USB_ID(0x0951, 0x16ad)) &&
Panu Matilainendacacb02014-11-30 18:45:40 +02001638 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
Jia-Ju Baidf3f0342018-07-27 16:55:28 +08001639 usleep_range(1000, 2000);
Macpaul Lina32a1fc2020-06-23 19:03:23 +08001640
1641 /*
1642 * Samsung USBC Headset (AKG) need a tiny delay after each
1643 * class compliant request. (Model number: AAM625R or AAM627R)
1644 */
1645 if (chip->usb_id == USB_ID(0x04e8, 0xa051) &&
1646 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1647 usleep_range(5000, 6000);
Daniel Mack2b58fd52012-09-04 10:23:07 +02001648}
1649
Daniel Mack126825e2013-04-17 00:01:40 +08001650/*
1651 * snd_usb_interface_dsd_format_quirks() is called from format.c to
1652 * augment the PCM format bit-field for DSD types. The UAC standards
1653 * don't have a designated bit field to denote DSD-capable interfaces,
1654 * hence all hardware that is known to support this format has to be
1655 * listed here.
1656 */
1657u64 snd_usb_interface_dsd_format_quirks(struct snd_usb_audio *chip,
1658 struct audioformat *fp,
1659 unsigned int sample_bytes)
1660{
Nobutaka Okabef3b906d2018-03-23 19:21:13 +09001661 struct usb_interface *iface;
1662
Daniel Mack126825e2013-04-17 00:01:40 +08001663 /* Playback Designs */
Jussi Laakoeb7505d2019-08-28 00:08:46 +03001664 if (USB_ID_VENDOR(chip->usb_id) == 0x23ba &&
1665 USB_ID_PRODUCT(chip->usb_id) < 0x0110) {
Daniel Mack126825e2013-04-17 00:01:40 +08001666 switch (fp->altsetting) {
1667 case 1:
1668 fp->dsd_dop = true;
1669 return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1670 case 2:
1671 fp->dsd_bitrev = true;
1672 return SNDRV_PCM_FMTBIT_DSD_U8;
1673 case 3:
1674 fp->dsd_bitrev = true;
1675 return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1676 }
1677 }
1678
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001679 /* XMOS based USB DACs */
1680 switch (chip->usb_id) {
Daniel Mackf6568912018-04-20 22:29:41 +02001681 case USB_ID(0x1511, 0x0037): /* AURALiC VEGA */
Daniel Mackf6568912018-04-20 22:29:41 +02001682 case USB_ID(0x2522, 0x0012): /* LH Labs VI DAC Infinity */
Jussi Laako9bb201a2017-10-15 12:41:32 +03001683 case USB_ID(0x2772, 0x0230): /* Pro-Ject Pre Box S2 Digital */
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001684 if (fp->altsetting == 2)
Jussi Laakod42472e2014-11-21 16:04:46 +02001685 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001686 break;
Jurgen Kramer3b7e5c72015-06-05 09:42:49 +02001687
Daniel Mackf6568912018-04-20 22:29:41 +02001688 case USB_ID(0x0d8c, 0x0316): /* Hegel HD12 DSD */
Jussi Laako202e69e2019-01-29 00:47:01 +02001689 case USB_ID(0x10cb, 0x0103): /* The Bit Opus #3; with fp->dsd_raw */
Takashi Iwai547d2c92020-04-30 14:47:55 +02001690 case USB_ID(0x16d0, 0x06b2): /* NuPrime DAC-10 */
Jussi Laako202e69e2019-01-29 00:47:01 +02001691 case USB_ID(0x16d0, 0x09dd): /* Encore mDSD */
Daniel Mackf6568912018-04-20 22:29:41 +02001692 case USB_ID(0x16d0, 0x0733): /* Furutech ADL Stratos */
1693 case USB_ID(0x16d0, 0x09db): /* NuPrime Audio DAC-9 */
1694 case USB_ID(0x1db5, 0x0003): /* Bryston BDA3 */
Daniel Mackf6568912018-04-20 22:29:41 +02001695 case USB_ID(0x22e1, 0xca01): /* HDTA Serenade DSD */
1696 case USB_ID(0x249c, 0x9326): /* M2Tech Young MkIII */
Jurgen Kramerad678b42016-01-29 14:59:25 +01001697 case USB_ID(0x2616, 0x0106): /* PS Audio NuWave DAC */
Daniel Mackf6568912018-04-20 22:29:41 +02001698 case USB_ID(0x2622, 0x0041): /* Audiolab M-DAC+ */
1699 case USB_ID(0x27f7, 0x3002): /* W4S DAC-2v2SE */
1700 case USB_ID(0x29a2, 0x0086): /* Mutec MC3+ USB */
1701 case USB_ID(0x6b42, 0x0042): /* MSB Technology */
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001702 if (fp->altsetting == 3)
Jussi Laakod42472e2014-11-21 16:04:46 +02001703 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001704 break;
Jussi Laako3eff6822016-12-17 23:51:41 +02001705
Daniel Mack7c748662018-02-11 09:50:27 -04001706 /* Amanero Combo384 USB based DACs with native DSD support */
1707 case USB_ID(0x16d0, 0x071a): /* Amanero - Combo384 */
1708 case USB_ID(0x2ab6, 0x0004): /* T+A DAC8DSD-V2.0, MP1000E-V2.0, MP2000R-V2.0, MP2500R-V2.0, MP3100HV-V2.0 */
1709 case USB_ID(0x2ab6, 0x0005): /* T+A USB HD Audio 1 */
1710 case USB_ID(0x2ab6, 0x0006): /* T+A USB HD Audio 2 */
Jussi Laako3eff6822016-12-17 23:51:41 +02001711 if (fp->altsetting == 2) {
Johan Hovoldf83914f2017-05-12 14:34:37 +02001712 switch (le16_to_cpu(chip->dev->descriptor.bcdDevice)) {
Jussi Laako3eff6822016-12-17 23:51:41 +02001713 case 0x199:
1714 return SNDRV_PCM_FMTBIT_DSD_U32_LE;
1715 case 0x19b:
Jussi Laakof5ce8172017-11-01 23:32:33 +02001716 case 0x203:
Jussi Laako3eff6822016-12-17 23:51:41 +02001717 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1718 default:
1719 break;
1720 }
1721 }
1722 break;
Jussi Laakoed993c62017-08-18 10:42:14 +03001723 case USB_ID(0x16d0, 0x0a23):
1724 if (fp->altsetting == 2)
1725 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1726 break;
Jussi Laako3eff6822016-12-17 23:51:41 +02001727
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001728 default:
1729 break;
1730 }
1731
Nobutaka Okabef3b906d2018-03-23 19:21:13 +09001732 /* ITF-USB DSD based DACs */
Takashi Iwai2de00d52021-07-29 09:38:53 +02001733 if (chip->quirk_flags & QUIRK_FLAG_ITF_USB_DSD_DAC) {
Nobutaka Okabef3b906d2018-03-23 19:21:13 +09001734 iface = usb_ifnum_to_if(chip->dev, fp->iface);
Jurgen Kramer7a2e9dd2014-11-28 17:32:53 +01001735
Nobutaka Okabef3b906d2018-03-23 19:21:13 +09001736 /* Altsetting 2 support native DSD if the num of altsets is
1737 * three (0-2),
1738 * Altsetting 3 support native DSD if the num of altsets is
1739 * four (0-3).
1740 */
1741 if (fp->altsetting == iface->num_altsetting - 1)
Nobutaka Okabe7f38ca02016-12-13 01:24:08 +09001742 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1743 }
1744
Jussi Laako3a572d92018-06-13 01:43:01 +03001745 /* Mostly generic method to detect many DSD-capable implementations -
1746 * from XMOS/Thesycon
1747 */
1748 switch (USB_ID_VENDOR(chip->usb_id)) {
Yue Wang1ea03582018-07-23 01:56:46 -07001749 case 0x152a: /* Thesycon devices */
Jussi Laakoeb7505d2019-08-28 00:08:46 +03001750 case 0x20b1: /* XMOS based devices */
Jussi Laako0067e152019-08-28 00:08:47 +03001751 case 0x22d9: /* Oppo */
Jussi Laakoeb7505d2019-08-28 00:08:46 +03001752 case 0x23ba: /* Playback Designs */
Jussi Laako3a572d92018-06-13 01:43:01 +03001753 case 0x25ce: /* Mytek devices */
Jussi Laako0067e152019-08-28 00:08:47 +03001754 case 0x278b: /* Rotel? */
Justin Songe2995b92019-10-24 12:27:14 +02001755 case 0x292b: /* Gustard/Ess based devices */
Amadej Kastelic725124d2020-12-15 19:09:05 +01001756 case 0x2972: /* FiiO devices */
Udo Eberhardt3bff2402019-02-05 17:20:47 +01001757 case 0x2ab6: /* T+A devices */
Artem Lapkin07815a22020-11-03 18:08:09 +08001758 case 0x3353: /* Khadas devices */
Jussi Laakof41f9002019-09-24 10:11:43 +03001759 case 0x3842: /* EVGA */
Ilya Pshonkin029d2c02019-09-17 10:49:34 +03001760 case 0xc502: /* HiBy devices */
Jussi Laako3a572d92018-06-13 01:43:01 +03001761 if (fp->dsd_raw)
1762 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
1763 break;
1764 default:
1765 break;
1766
1767 }
1768
Daniel Mack126825e2013-04-17 00:01:40 +08001769 return 0;
1770}
Ruslan Bilovolceb18f52018-03-19 03:46:02 +02001771
1772void snd_usb_audioformat_attributes_quirk(struct snd_usb_audio *chip,
1773 struct audioformat *fp,
1774 int stream)
1775{
1776 switch (chip->usb_id) {
1777 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
1778 /* Optoplay sets the sample rate attribute although
1779 * it seems not supporting it in fact.
1780 */
1781 fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
1782 break;
1783 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
1784 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
1785 /* doesn't set the sample rate attribute, but supports it */
1786 fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
1787 break;
1788 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro USB */
1789 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
1790 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
1791 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
1792 an older model 77d:223) */
1793 /*
1794 * plantronics headset and Griffin iMic have set adaptive-in
1795 * although it's really not...
1796 */
1797 fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
1798 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
1799 fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
1800 else
1801 fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
1802 break;
Alexander Tsoy2edb84e2020-02-29 18:18:15 +03001803 case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook IIc */
1804 /*
1805 * MaxPacketsOnly attribute is erroneously set in endpoint
1806 * descriptors. As a result this card produces noise with
Alexander Tsoy5ff40e62020-06-29 06:26:07 +03001807 * all sample rates other than 96 kHz.
Alexander Tsoy2edb84e2020-02-29 18:18:15 +03001808 */
1809 fp->attributes &= ~UAC_EP_CS_ATTR_FILL_MAX;
1810 break;
Ruslan Bilovolceb18f52018-03-19 03:46:02 +02001811 }
1812}
Chris Wulff55f73262020-03-14 12:54:49 -04001813
Takashi Iwaid8695bc2020-03-25 11:33:19 +01001814/*
1815 * registration quirk:
1816 * the registration is skipped if a device matches with the given ID,
1817 * unless the interface reaches to the defined one. This is for delaying
1818 * the registration until the last known interface, so that the card and
1819 * devices appear at the same time.
1820 */
1821
1822struct registration_quirk {
1823 unsigned int usb_id; /* composed via USB_ID() */
1824 unsigned int interface; /* the interface to trigger register */
1825};
1826
1827#define REG_QUIRK_ENTRY(vendor, product, iface) \
1828 { .usb_id = USB_ID(vendor, product), .interface = (iface) }
1829
1830static const struct registration_quirk registration_quirks[] = {
1831 REG_QUIRK_ENTRY(0x0951, 0x16d8, 2), /* Kingston HyperX AMP */
Emmanuel Pescostafd60e062020-04-04 17:38:43 +02001832 REG_QUIRK_ENTRY(0x0951, 0x16ed, 2), /* Kingston HyperX Cloud Alpha S */
Christoffer Nielsen73094602020-06-19 13:48:22 +02001833 REG_QUIRK_ENTRY(0x0951, 0x16ea, 2), /* Kingston HyperX Cloud Flight S */
Alexander Tsoyb0084af2021-07-22 02:56:05 +03001834 REG_QUIRK_ENTRY(0x0ecb, 0x1f46, 2), /* JBL Quantum 600 */
1835 REG_QUIRK_ENTRY(0x0ecb, 0x2039, 2), /* JBL Quantum 400 */
Alexander Tsoy4b0556b2021-07-27 12:33:26 +03001836 REG_QUIRK_ENTRY(0x0ecb, 0x203c, 2), /* JBL Quantum 600 */
Alexander Tsoyb0084af2021-07-22 02:56:05 +03001837 REG_QUIRK_ENTRY(0x0ecb, 0x203e, 2), /* JBL Quantum 800 */
Takashi Iwaid8695bc2020-03-25 11:33:19 +01001838 { 0 } /* terminator */
1839};
1840
1841/* return true if skipping registration */
1842bool snd_usb_registration_quirk(struct snd_usb_audio *chip, int iface)
Chris Wulff55f73262020-03-14 12:54:49 -04001843{
Takashi Iwaid8695bc2020-03-25 11:33:19 +01001844 const struct registration_quirk *q;
1845
1846 for (q = registration_quirks; q->usb_id; q++)
1847 if (chip->usb_id == q->usb_id)
1848 return iface != q->interface;
1849
Chris Wulff55f73262020-03-14 12:54:49 -04001850 /* Register as normal */
Takashi Iwaid8695bc2020-03-25 11:33:19 +01001851 return false;
Chris Wulff55f73262020-03-14 12:54:49 -04001852}
Takashi Iwai4d4dee02021-07-29 09:38:47 +02001853
1854/*
1855 * driver behavior quirk flags
1856 */
1857struct usb_audio_quirk_flags_table {
1858 u32 id;
1859 u32 flags;
1860};
1861
1862#define DEVICE_FLG(vid, pid, _flags) \
1863 { .id = USB_ID(vid, pid), .flags = (_flags) }
1864#define VENDOR_FLG(vid, _flags) DEVICE_FLG(vid, 0, _flags)
1865
1866static const struct usb_audio_quirk_flags_table quirk_flags_table[] = {
1867 /* Device matches */
1868 DEVICE_FLG(0x041e, 0x4080, /* Creative Live Cam VF0610 */
1869 QUIRK_FLAG_GET_SAMPLE_RATE),
1870 DEVICE_FLG(0x046d, 0x084c, /* Logitech ConferenceCam Connect */
1871 QUIRK_FLAG_GET_SAMPLE_RATE),
1872 DEVICE_FLG(0x04d8, 0xfeea, /* Benchmark DAC1 Pre */
1873 QUIRK_FLAG_GET_SAMPLE_RATE),
Takashi Iwaif21dca82021-07-29 09:38:52 +02001874 DEVICE_FLG(0x04e8, 0xa051, /* Samsung USBC Headset (AKG) */
1875 QUIRK_FLAG_SKIP_CLOCK_SELECTOR),
Takashi Iwai4d4dee02021-07-29 09:38:47 +02001876 DEVICE_FLG(0x0556, 0x0014, /* Phoenix Audio TMX320VC */
1877 QUIRK_FLAG_GET_SAMPLE_RATE),
1878 DEVICE_FLG(0x05a3, 0x9420, /* ELP HD USB Camera */
1879 QUIRK_FLAG_GET_SAMPLE_RATE),
1880 DEVICE_FLG(0x05a7, 0x1020, /* Bose Companion 5 */
1881 QUIRK_FLAG_GET_SAMPLE_RATE),
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001882 DEVICE_FLG(0x05e1, 0x0408, /* Syntek STK1160 */
1883 QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001884 DEVICE_FLG(0x05e1, 0x0480, /* Hauppauge Woodbury */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001885 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwai2de00d52021-07-29 09:38:53 +02001886 DEVICE_FLG(0x0644, 0x8043, /* TEAC UD-501/UD-501V2/UD-503/NT-503 */
1887 QUIRK_FLAG_ITF_USB_DSD_DAC),
1888 DEVICE_FLG(0x0644, 0x8044, /* Esoteric D-05X */
1889 QUIRK_FLAG_ITF_USB_DSD_DAC),
1890 DEVICE_FLG(0x0644, 0x804a, /* TEAC UD-301 */
1891 QUIRK_FLAG_ITF_USB_DSD_DAC),
Takashi Iwai4d4dee02021-07-29 09:38:47 +02001892 DEVICE_FLG(0x074d, 0x3553, /* Outlaw RR2150 (Micronas UAC3553B) */
1893 QUIRK_FLAG_GET_SAMPLE_RATE),
Takashi Iwaice47d472021-07-29 09:38:48 +02001894 DEVICE_FLG(0x0fd9, 0x0008, /* Hauppauge HVR-950Q */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001895 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwai4d4dee02021-07-29 09:38:47 +02001896 DEVICE_FLG(0x1395, 0x740a, /* Sennheiser DECT */
1897 QUIRK_FLAG_GET_SAMPLE_RATE),
Takashi Iwai2de00d52021-07-29 09:38:53 +02001898 DEVICE_FLG(0x154e, 0x1002, /* Denon DCD-1500RE */
1899 QUIRK_FLAG_ITF_USB_DSD_DAC),
1900 DEVICE_FLG(0x154e, 0x1003, /* Denon DA-300USB */
1901 QUIRK_FLAG_ITF_USB_DSD_DAC),
1902 DEVICE_FLG(0x154e, 0x3005, /* Marantz HD-DAC1 */
1903 QUIRK_FLAG_ITF_USB_DSD_DAC),
1904 DEVICE_FLG(0x154e, 0x3006, /* Marantz SA-14S1 */
1905 QUIRK_FLAG_ITF_USB_DSD_DAC),
Takashi Iwaif21dca82021-07-29 09:38:52 +02001906 DEVICE_FLG(0x154e, 0x500e, /* Denon DN-X1600 */
1907 QUIRK_FLAG_IGNORE_CLOCK_SOURCE),
Takashi Iwaic1b034a2021-07-29 09:38:50 +02001908 DEVICE_FLG(0x1686, 0x00dd, /* Zoom R16/24 */
1909 QUIRK_FLAG_TX_LENGTH),
Takashi Iwai2de00d52021-07-29 09:38:53 +02001910 DEVICE_FLG(0x1852, 0x5065, /* Luxman DA-06 */
1911 QUIRK_FLAG_ITF_USB_DSD_DAC),
Takashi Iwai4d4dee02021-07-29 09:38:47 +02001912 DEVICE_FLG(0x1901, 0x0191, /* GE B850V3 CP2114 audio interface */
1913 QUIRK_FLAG_GET_SAMPLE_RATE),
Takashi Iwaice47d472021-07-29 09:38:48 +02001914 DEVICE_FLG(0x2040, 0x7200, /* Hauppauge HVR-950Q */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001915 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001916 DEVICE_FLG(0x2040, 0x7201, /* Hauppauge HVR-950Q-MXL */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001917 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001918 DEVICE_FLG(0x2040, 0x7210, /* Hauppauge HVR-950Q */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001919 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001920 DEVICE_FLG(0x2040, 0x7211, /* Hauppauge HVR-950Q-MXL */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001921 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001922 DEVICE_FLG(0x2040, 0x7213, /* Hauppauge HVR-950Q */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001923 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001924 DEVICE_FLG(0x2040, 0x7217, /* Hauppauge HVR-950Q */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001925 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001926 DEVICE_FLG(0x2040, 0x721b, /* Hauppauge HVR-950Q */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001927 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001928 DEVICE_FLG(0x2040, 0x721e, /* Hauppauge HVR-950Q */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001929 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001930 DEVICE_FLG(0x2040, 0x721f, /* Hauppauge HVR-950Q */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001931 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001932 DEVICE_FLG(0x2040, 0x7240, /* Hauppauge HVR-850 */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001933 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001934 DEVICE_FLG(0x2040, 0x7260, /* Hauppauge HVR-950Q */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001935 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001936 DEVICE_FLG(0x2040, 0x7270, /* Hauppauge HVR-950Q */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001937 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001938 DEVICE_FLG(0x2040, 0x7280, /* Hauppauge HVR-950Q */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001939 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001940 DEVICE_FLG(0x2040, 0x7281, /* Hauppauge HVR-950Q-MXL */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001941 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwaice47d472021-07-29 09:38:48 +02001942 DEVICE_FLG(0x2040, 0x8200, /* Hauppauge Woodbury */
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001943 QUIRK_FLAG_SHARE_MEDIA_DEVICE | QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwai4d4dee02021-07-29 09:38:47 +02001944 DEVICE_FLG(0x21b4, 0x0081, /* AudioQuest DragonFly */
1945 QUIRK_FLAG_GET_SAMPLE_RATE),
1946 DEVICE_FLG(0x2912, 0x30c8, /* Audioengine D1 */
1947 QUIRK_FLAG_GET_SAMPLE_RATE),
1948 DEVICE_FLG(0x413c, 0xa506, /* Dell AE515 sound bar */
1949 QUIRK_FLAG_GET_SAMPLE_RATE),
Takashi Iwaiaf158a72021-07-29 09:38:49 +02001950 DEVICE_FLG(0x534d, 0x2109, /* MacroSilicon MS2109 */
1951 QUIRK_FLAG_ALIGN_TRANSFER),
Takashi Iwai4d4dee02021-07-29 09:38:47 +02001952
1953 /* Vendor matches */
1954 VENDOR_FLG(0x045e, /* MS Lifecam */
1955 QUIRK_FLAG_GET_SAMPLE_RATE),
1956 VENDOR_FLG(0x047f, /* Plantronics */
1957 QUIRK_FLAG_GET_SAMPLE_RATE),
1958 VENDOR_FLG(0x1de7, /* Phoenix Audio */
1959 QUIRK_FLAG_GET_SAMPLE_RATE),
1960
1961 {} /* terminator */
1962};
1963
1964void snd_usb_init_quirk_flags(struct snd_usb_audio *chip)
1965{
1966 const struct usb_audio_quirk_flags_table *p;
1967
1968 for (p = quirk_flags_table; p->id; p++) {
1969 if (chip->usb_id == p->id ||
1970 (!USB_ID_PRODUCT(p->id) &&
1971 USB_ID_VENDOR(chip->usb_id) == USB_ID_VENDOR(p->id))) {
1972 usb_audio_dbg(chip,
1973 "Set quirk_flags 0x%x for device %04x:%04x\n",
1974 p->flags, USB_ID_VENDOR(chip->usb_id),
1975 USB_ID_PRODUCT(chip->usb_id));
1976 chip->quirk_flags |= p->flags;
1977 return;
1978 }
1979 }
1980}