blob: 5585cfd7f63e393a1571495e5c301848d16ec9ee [file] [log] [blame]
Daniel Macke5779992010-03-04 19:46:13 +01001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17#include <linux/init.h>
Stephen Rothwell36db0452010-03-29 16:02:50 +110018#include <linux/slab.h>
Daniel Macke5779992010-03-04 19:46:13 +010019#include <linux/usb.h>
20#include <linux/usb/audio.h>
Clemens Ladischaafe77c2013-03-31 23:43:12 +020021#include <linux/usb/midi.h>
Daniel Macke5779992010-03-04 19:46:13 +010022
Daniel Mack9e386582011-05-25 09:09:01 +020023#include <sound/control.h>
Daniel Macke5779992010-03-04 19:46:13 +010024#include <sound/core.h>
25#include <sound/info.h>
26#include <sound/pcm.h>
27
28#include "usbaudio.h"
29#include "card.h"
Daniel Mackf0b5e632010-03-11 21:13:23 +010030#include "mixer.h"
Daniel Mack7b1eda22010-03-11 21:13:22 +010031#include "mixer_quirks.h"
Daniel Macke5779992010-03-04 19:46:13 +010032#include "midi.h"
33#include "quirks.h"
34#include "helper.h"
35#include "endpoint.h"
36#include "pcm.h"
Daniel Mack3d8d4dc2010-06-16 17:57:31 +020037#include "clock.h"
Daniel Macke8e8bab2011-09-12 18:54:12 +020038#include "stream.h"
Daniel Macke5779992010-03-04 19:46:13 +010039
40/*
41 * handle the quirks for the contained interfaces
42 */
43static int create_composite_quirk(struct snd_usb_audio *chip,
44 struct usb_interface *iface,
45 struct usb_driver *driver,
Takashi Iwai85a81812014-11-10 07:41:59 +010046 const struct snd_usb_audio_quirk *quirk_comp)
Daniel Macke5779992010-03-04 19:46:13 +010047{
48 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
Takashi Iwai85a81812014-11-10 07:41:59 +010049 const struct snd_usb_audio_quirk *quirk;
Daniel Macke5779992010-03-04 19:46:13 +010050 int err;
51
Takashi Iwai85a81812014-11-10 07:41:59 +010052 for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
Daniel Macke5779992010-03-04 19:46:13 +010053 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
54 if (!iface)
55 continue;
56 if (quirk->ifnum != probed_ifnum &&
57 usb_interface_claimed(iface))
58 continue;
59 err = snd_usb_create_quirk(chip, iface, driver, quirk);
60 if (err < 0)
61 return err;
Takashi Iwaid4b8fc62014-11-09 18:21:23 +010062 }
63
Takashi Iwai85a81812014-11-10 07:41:59 +010064 for (quirk = quirk_comp->data; quirk->ifnum >= 0; ++quirk) {
Takashi Iwaid4b8fc62014-11-09 18:21:23 +010065 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
66 if (!iface)
67 continue;
68 if (quirk->ifnum != probed_ifnum &&
69 !usb_interface_claimed(iface))
Daniel Macke5779992010-03-04 19:46:13 +010070 usb_driver_claim_interface(driver, iface, (void *)-1L);
71 }
Takashi Iwaid4b8fc62014-11-09 18:21:23 +010072
Daniel Macke5779992010-03-04 19:46:13 +010073 return 0;
74}
75
76static int ignore_interface_quirk(struct snd_usb_audio *chip,
77 struct usb_interface *iface,
78 struct usb_driver *driver,
79 const struct snd_usb_audio_quirk *quirk)
80{
81 return 0;
82}
83
84
85/*
86 * Allow alignment on audio sub-slot (channel samples) rather than
87 * on audio slots (audio frames)
88 */
89static int create_align_transfer_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 chip->txfr_quirk = 1;
95 return 1; /* Continue with creating streams and mixer */
96}
97
98static int create_any_midi_quirk(struct snd_usb_audio *chip,
99 struct usb_interface *intf,
100 struct usb_driver *driver,
101 const struct snd_usb_audio_quirk *quirk)
102{
103 return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
104}
105
106/*
107 * create a stream for an interface with proper descriptors
108 */
109static int create_standard_audio_quirk(struct snd_usb_audio *chip,
110 struct usb_interface *iface,
111 struct usb_driver *driver,
112 const struct snd_usb_audio_quirk *quirk)
113{
114 struct usb_host_interface *alts;
115 struct usb_interface_descriptor *altsd;
116 int err;
117
118 alts = &iface->altsetting[0];
119 altsd = get_iface_desc(alts);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200120 err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
Daniel Macke5779992010-03-04 19:46:13 +0100121 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100122 usb_audio_err(chip, "cannot setup if %d: error %d\n",
Daniel Macke5779992010-03-04 19:46:13 +0100123 altsd->bInterfaceNumber, err);
124 return err;
125 }
126 /* reset the current interface */
127 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
128 return 0;
129}
130
131/*
132 * create a stream for an endpoint/altsetting without proper descriptors
133 */
134static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
135 struct usb_interface *iface,
136 struct usb_driver *driver,
137 const struct snd_usb_audio_quirk *quirk)
138{
139 struct audioformat *fp;
140 struct usb_host_interface *alts;
Eldad Zack42d4ab82013-07-09 20:36:15 +0200141 struct usb_interface_descriptor *altsd;
Daniel Macke5779992010-03-04 19:46:13 +0100142 int stream, err;
143 unsigned *rate_table = NULL;
144
145 fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
Xi Wang8866f402012-02-14 05:18:48 -0500146 if (!fp) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100147 usb_audio_err(chip, "cannot memdup\n");
Daniel Macke5779992010-03-04 19:46:13 +0100148 return -ENOMEM;
149 }
Xi Wang8866f402012-02-14 05:18:48 -0500150 if (fp->nr_rates > MAX_NR_RATES) {
151 kfree(fp);
152 return -EINVAL;
153 }
Daniel Macke5779992010-03-04 19:46:13 +0100154 if (fp->nr_rates > 0) {
Thomas Meyer43df2a52011-11-10 19:38:43 +0100155 rate_table = kmemdup(fp->rate_table,
156 sizeof(int) * fp->nr_rates, GFP_KERNEL);
Daniel Macke5779992010-03-04 19:46:13 +0100157 if (!rate_table) {
158 kfree(fp);
159 return -ENOMEM;
160 }
Daniel Macke5779992010-03-04 19:46:13 +0100161 fp->rate_table = rate_table;
162 }
163
164 stream = (fp->endpoint & USB_DIR_IN)
165 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200166 err = snd_usb_add_audio_stream(chip, stream, fp);
Daniel Macke5779992010-03-04 19:46:13 +0100167 if (err < 0) {
168 kfree(fp);
169 kfree(rate_table);
170 return err;
171 }
172 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
173 fp->altset_idx >= iface->num_altsetting) {
174 kfree(fp);
175 kfree(rate_table);
176 return -EINVAL;
177 }
178 alts = &iface->altsetting[fp->altset_idx];
Eldad Zack42d4ab82013-07-09 20:36:15 +0200179 altsd = get_iface_desc(alts);
180 fp->protocol = altsd->bInterfaceProtocol;
181
Mark Hills59ea5862013-03-17 11:07:54 +0000182 if (fp->datainterval == 0)
183 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
184 if (fp->maxpacksize == 0)
185 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Daniel Macke5779992010-03-04 19:46:13 +0100186 usb_set_interface(chip->dev, fp->iface, 0);
Daniel Mack767d75a2010-03-04 19:46:17 +0100187 snd_usb_init_pitch(chip, fp->iface, alts, fp);
188 snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
Daniel Macke5779992010-03-04 19:46:13 +0100189 return 0;
190}
191
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200192static int create_auto_pcm_quirk(struct snd_usb_audio *chip,
193 struct usb_interface *iface,
194 struct usb_driver *driver)
195{
196 struct usb_host_interface *alts;
197 struct usb_interface_descriptor *altsd;
198 struct usb_endpoint_descriptor *epd;
199 struct uac1_as_header_descriptor *ashd;
200 struct uac_format_type_i_discrete_descriptor *fmtd;
201
202 /*
203 * Most Roland/Yamaha audio streaming interfaces have more or less
204 * standard descriptors, but older devices might lack descriptors, and
205 * future ones might change, so ensure that we fail silently if the
206 * interface doesn't look exactly right.
207 */
208
209 /* must have a non-zero altsetting for streaming */
210 if (iface->num_altsetting < 2)
211 return -ENODEV;
212 alts = &iface->altsetting[1];
213 altsd = get_iface_desc(alts);
214
215 /* must have an isochronous endpoint for streaming */
216 if (altsd->bNumEndpoints < 1)
217 return -ENODEV;
218 epd = get_endpoint(alts, 0);
219 if (!usb_endpoint_xfer_isoc(epd))
220 return -ENODEV;
221
222 /* must have format descriptors */
223 ashd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
224 UAC_AS_GENERAL);
225 fmtd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
226 UAC_FORMAT_TYPE);
227 if (!ashd || ashd->bLength < 7 ||
228 !fmtd || fmtd->bLength < 8)
229 return -ENODEV;
230
231 return create_standard_audio_quirk(chip, iface, driver, NULL);
232}
233
234static int create_yamaha_midi_quirk(struct snd_usb_audio *chip,
235 struct usb_interface *iface,
236 struct usb_driver *driver,
237 struct usb_host_interface *alts)
238{
239 static const struct snd_usb_audio_quirk yamaha_midi_quirk = {
240 .type = QUIRK_MIDI_YAMAHA
241 };
242 struct usb_midi_in_jack_descriptor *injd;
243 struct usb_midi_out_jack_descriptor *outjd;
244
245 /* must have some valid jack descriptors */
246 injd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
247 NULL, USB_MS_MIDI_IN_JACK);
248 outjd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
249 NULL, USB_MS_MIDI_OUT_JACK);
250 if (!injd && !outjd)
251 return -ENODEV;
252 if (injd && (injd->bLength < 5 ||
253 (injd->bJackType != USB_MS_EMBEDDED &&
254 injd->bJackType != USB_MS_EXTERNAL)))
255 return -ENODEV;
256 if (outjd && (outjd->bLength < 6 ||
257 (outjd->bJackType != USB_MS_EMBEDDED &&
258 outjd->bJackType != USB_MS_EXTERNAL)))
259 return -ENODEV;
260 return create_any_midi_quirk(chip, iface, driver, &yamaha_midi_quirk);
261}
262
263static int create_roland_midi_quirk(struct snd_usb_audio *chip,
264 struct usb_interface *iface,
265 struct usb_driver *driver,
266 struct usb_host_interface *alts)
267{
268 static const struct snd_usb_audio_quirk roland_midi_quirk = {
269 .type = QUIRK_MIDI_ROLAND
270 };
271 u8 *roland_desc = NULL;
272
273 /* might have a vendor-specific descriptor <06 24 F1 02 ...> */
274 for (;;) {
275 roland_desc = snd_usb_find_csint_desc(alts->extra,
276 alts->extralen,
277 roland_desc, 0xf1);
278 if (!roland_desc)
279 return -ENODEV;
280 if (roland_desc[0] < 6 || roland_desc[3] != 2)
281 continue;
282 return create_any_midi_quirk(chip, iface, driver,
283 &roland_midi_quirk);
284 }
285}
286
287static int create_std_midi_quirk(struct snd_usb_audio *chip,
288 struct usb_interface *iface,
289 struct usb_driver *driver,
290 struct usb_host_interface *alts)
291{
292 struct usb_ms_header_descriptor *mshd;
293 struct usb_ms_endpoint_descriptor *msepd;
294
295 /* must have the MIDIStreaming interface header descriptor*/
296 mshd = (struct usb_ms_header_descriptor *)alts->extra;
297 if (alts->extralen < 7 ||
298 mshd->bLength < 7 ||
299 mshd->bDescriptorType != USB_DT_CS_INTERFACE ||
300 mshd->bDescriptorSubtype != USB_MS_HEADER)
301 return -ENODEV;
302 /* must have the MIDIStreaming endpoint descriptor*/
303 msepd = (struct usb_ms_endpoint_descriptor *)alts->endpoint[0].extra;
304 if (alts->endpoint[0].extralen < 4 ||
305 msepd->bLength < 4 ||
306 msepd->bDescriptorType != USB_DT_CS_ENDPOINT ||
307 msepd->bDescriptorSubtype != UAC_MS_GENERAL ||
308 msepd->bNumEmbMIDIJack < 1 ||
309 msepd->bNumEmbMIDIJack > 16)
310 return -ENODEV;
311
312 return create_any_midi_quirk(chip, iface, driver, NULL);
313}
314
315static int create_auto_midi_quirk(struct snd_usb_audio *chip,
316 struct usb_interface *iface,
317 struct usb_driver *driver)
318{
319 struct usb_host_interface *alts;
320 struct usb_interface_descriptor *altsd;
321 struct usb_endpoint_descriptor *epd;
322 int err;
323
324 alts = &iface->altsetting[0];
325 altsd = get_iface_desc(alts);
326
327 /* must have at least one bulk/interrupt endpoint for streaming */
328 if (altsd->bNumEndpoints < 1)
329 return -ENODEV;
330 epd = get_endpoint(alts, 0);
Clemens Ladischaa773bf2013-08-11 14:13:13 +0200331 if (!usb_endpoint_xfer_bulk(epd) &&
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200332 !usb_endpoint_xfer_int(epd))
333 return -ENODEV;
334
335 switch (USB_ID_VENDOR(chip->usb_id)) {
336 case 0x0499: /* Yamaha */
337 err = create_yamaha_midi_quirk(chip, iface, driver, alts);
Clemens Ladischaa773bf2013-08-11 14:13:13 +0200338 if (err != -ENODEV)
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200339 return err;
340 break;
341 case 0x0582: /* Roland */
342 err = create_roland_midi_quirk(chip, iface, driver, alts);
Clemens Ladischaa773bf2013-08-11 14:13:13 +0200343 if (err != -ENODEV)
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200344 return err;
345 break;
346 }
347
348 return create_std_midi_quirk(chip, iface, driver, alts);
349}
350
351static int create_autodetect_quirk(struct snd_usb_audio *chip,
352 struct usb_interface *iface,
Clemens Ladischb1ce7ba2013-04-04 21:43:57 +0200353 struct usb_driver *driver)
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200354{
355 int err;
356
357 err = create_auto_pcm_quirk(chip, iface, driver);
358 if (err == -ENODEV)
359 err = create_auto_midi_quirk(chip, iface, driver);
360 return err;
361}
362
Clemens Ladischb1ce7ba2013-04-04 21:43:57 +0200363static int create_autodetect_quirks(struct snd_usb_audio *chip,
364 struct usb_interface *iface,
365 struct usb_driver *driver,
366 const struct snd_usb_audio_quirk *quirk)
367{
368 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
369 int ifcount, ifnum, err;
370
371 err = create_autodetect_quirk(chip, iface, driver);
372 if (err < 0)
373 return err;
374
375 /*
376 * ALSA PCM playback/capture devices cannot be registered in two steps,
377 * so we have to claim the other corresponding interface here.
378 */
379 ifcount = chip->dev->actconfig->desc.bNumInterfaces;
380 for (ifnum = 0; ifnum < ifcount; ifnum++) {
381 if (ifnum == probed_ifnum || quirk->ifnum >= 0)
382 continue;
383 iface = usb_ifnum_to_if(chip->dev, ifnum);
384 if (!iface ||
385 usb_interface_claimed(iface) ||
386 get_iface_desc(iface->altsetting)->bInterfaceClass !=
387 USB_CLASS_VENDOR_SPEC)
388 continue;
389
390 err = create_autodetect_quirk(chip, iface, driver);
391 if (err >= 0)
392 usb_driver_claim_interface(driver, iface, (void *)-1L);
393 }
394
395 return 0;
396}
397
Daniel Macke5779992010-03-04 19:46:13 +0100398/*
399 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
400 * The only way to detect the sample rate is by looking at wMaxPacketSize.
401 */
402static int create_uaxx_quirk(struct snd_usb_audio *chip,
403 struct usb_interface *iface,
404 struct usb_driver *driver,
405 const struct snd_usb_audio_quirk *quirk)
406{
407 static const struct audioformat ua_format = {
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100408 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
Daniel Macke5779992010-03-04 19:46:13 +0100409 .channels = 2,
410 .fmt_type = UAC_FORMAT_TYPE_I,
411 .altsetting = 1,
412 .altset_idx = 1,
413 .rates = SNDRV_PCM_RATE_CONTINUOUS,
414 };
415 struct usb_host_interface *alts;
416 struct usb_interface_descriptor *altsd;
417 struct audioformat *fp;
418 int stream, err;
419
420 /* both PCM and MIDI interfaces have 2 or more altsettings */
421 if (iface->num_altsetting < 2)
422 return -ENXIO;
423 alts = &iface->altsetting[1];
424 altsd = get_iface_desc(alts);
425
426 if (altsd->bNumEndpoints == 2) {
427 static const struct snd_usb_midi_endpoint_info ua700_ep = {
428 .out_cables = 0x0003,
429 .in_cables = 0x0003
430 };
431 static const struct snd_usb_audio_quirk ua700_quirk = {
432 .type = QUIRK_MIDI_FIXED_ENDPOINT,
433 .data = &ua700_ep
434 };
435 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
436 .out_cables = 0x0001,
437 .in_cables = 0x0001
438 };
439 static const struct snd_usb_audio_quirk uaxx_quirk = {
440 .type = QUIRK_MIDI_FIXED_ENDPOINT,
441 .data = &uaxx_ep
442 };
443 const struct snd_usb_audio_quirk *quirk =
444 chip->usb_id == USB_ID(0x0582, 0x002b)
445 ? &ua700_quirk : &uaxx_quirk;
446 return snd_usbmidi_create(chip->card, iface,
447 &chip->midi_list, quirk);
448 }
449
450 if (altsd->bNumEndpoints != 1)
451 return -ENXIO;
452
Thomas Meyer43df2a52011-11-10 19:38:43 +0100453 fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
Daniel Macke5779992010-03-04 19:46:13 +0100454 if (!fp)
455 return -ENOMEM;
Daniel Macke5779992010-03-04 19:46:13 +0100456
457 fp->iface = altsd->bInterfaceNumber;
458 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
459 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
460 fp->datainterval = 0;
461 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
462
463 switch (fp->maxpacksize) {
464 case 0x120:
465 fp->rate_max = fp->rate_min = 44100;
466 break;
467 case 0x138:
468 case 0x140:
469 fp->rate_max = fp->rate_min = 48000;
470 break;
471 case 0x258:
472 case 0x260:
473 fp->rate_max = fp->rate_min = 96000;
474 break;
475 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100476 usb_audio_err(chip, "unknown sample rate\n");
Daniel Macke5779992010-03-04 19:46:13 +0100477 kfree(fp);
478 return -ENXIO;
479 }
480
481 stream = (fp->endpoint & USB_DIR_IN)
482 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200483 err = snd_usb_add_audio_stream(chip, stream, fp);
Daniel Macke5779992010-03-04 19:46:13 +0100484 if (err < 0) {
485 kfree(fp);
486 return err;
487 }
488 usb_set_interface(chip->dev, fp->iface, 0);
489 return 0;
490}
491
492/*
Daniel Mack014950b2011-05-25 09:09:02 +0200493 * Create a standard mixer for the specified interface.
494 */
495static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
496 struct usb_interface *iface,
497 struct usb_driver *driver,
498 const struct snd_usb_audio_quirk *quirk)
499{
500 if (quirk->ifnum < 0)
501 return 0;
502
503 return snd_usb_create_mixer(chip, quirk->ifnum, 0);
504}
505
506/*
Daniel Macke5779992010-03-04 19:46:13 +0100507 * audio-interface quirks
508 *
509 * returns zero if no standard audio/MIDI parsing is needed.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300510 * returns a positive value if standard audio/midi interfaces are parsed
Daniel Macke5779992010-03-04 19:46:13 +0100511 * after this.
512 * returns a negative value at error.
513 */
514int snd_usb_create_quirk(struct snd_usb_audio *chip,
515 struct usb_interface *iface,
516 struct usb_driver *driver,
517 const struct snd_usb_audio_quirk *quirk)
518{
519 typedef int (*quirk_func_t)(struct snd_usb_audio *,
520 struct usb_interface *,
521 struct usb_driver *,
522 const struct snd_usb_audio_quirk *);
523 static const quirk_func_t quirk_funcs[] = {
524 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
525 [QUIRK_COMPOSITE] = create_composite_quirk,
Clemens Ladischb1ce7ba2013-04-04 21:43:57 +0200526 [QUIRK_AUTODETECT] = create_autodetect_quirks,
Daniel Macke5779992010-03-04 19:46:13 +0100527 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
528 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
529 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200530 [QUIRK_MIDI_ROLAND] = create_any_midi_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100531 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
532 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
Clemens Ladischc7f57212010-10-22 18:20:48 +0200533 [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100534 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
535 [QUIRK_MIDI_CME] = create_any_midi_quirk,
Krzysztof Foltman4434ade2010-05-20 20:31:10 +0100536 [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
Kristian Amlie1ef0e0a2011-08-26 13:19:49 +0200537 [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
Clemens Ladisch96c93462015-11-15 22:38:29 +0100538 [QUIRK_MIDI_CH345] = create_any_midi_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100539 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
540 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
541 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
Daniel Mack014950b2011-05-25 09:09:02 +0200542 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
543 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100544 };
545
546 if (quirk->type < QUIRK_TYPE_COUNT) {
547 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
548 } else {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100549 usb_audio_err(chip, "invalid quirk type %d\n", quirk->type);
Daniel Macke5779992010-03-04 19:46:13 +0100550 return -ENXIO;
551 }
552}
553
554/*
555 * boot quirks
556 */
557
558#define EXTIGY_FIRMWARE_SIZE_OLD 794
559#define EXTIGY_FIRMWARE_SIZE_NEW 483
560
561static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
562{
563 struct usb_host_config *config = dev->actconfig;
564 int err;
565
566 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
567 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100568 dev_dbg(&dev->dev, "sending Extigy boot sequence...\n");
Daniel Macke5779992010-03-04 19:46:13 +0100569 /* Send message to force it to reconnect with full interface. */
570 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200571 0x10, 0x43, 0x0001, 0x000a, NULL, 0);
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100572 if (err < 0)
573 dev_dbg(&dev->dev, "error sending boot message: %d\n", err);
Daniel Macke5779992010-03-04 19:46:13 +0100574 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
575 &dev->descriptor, sizeof(dev->descriptor));
576 config = dev->actconfig;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100577 if (err < 0)
578 dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
Daniel Macke5779992010-03-04 19:46:13 +0100579 err = usb_reset_configuration(dev);
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100580 if (err < 0)
581 dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
582 dev_dbg(&dev->dev, "extigy_boot: new boot length = %d\n",
Daniel Macke5779992010-03-04 19:46:13 +0100583 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
584 return -ENODEV; /* quit this anyway */
585 }
586 return 0;
587}
588
589static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
590{
591 u8 buf = 1;
592
593 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
594 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200595 0, 0, &buf, 1);
Daniel Macke5779992010-03-04 19:46:13 +0100596 if (buf == 0) {
597 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
598 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200599 1, 2000, NULL, 0);
Daniel Macke5779992010-03-04 19:46:13 +0100600 return -ENODEV;
601 }
602 return 0;
603}
604
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200605static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
606{
607 int err;
608
609 if (dev->actconfig->desc.bConfigurationValue == 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100610 dev_info(&dev->dev,
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200611 "Fast Track Pro switching to config #2\n");
612 /* This function has to be available by the usb core module.
613 * if it is not avialable the boot quirk has to be left out
614 * and the configuration has to be set by udev or hotplug
615 * rules
616 */
617 err = usb_driver_set_configuration(dev, 2);
David Henningssonb98ae272013-01-04 17:02:18 +0100618 if (err < 0)
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100619 dev_dbg(&dev->dev,
620 "error usb_driver_set_configuration: %d\n",
621 err);
David Henningssonb98ae272013-01-04 17:02:18 +0100622 /* Always return an error, so that we stop creating a device
623 that will just be destroyed and recreated with a new
624 configuration */
625 return -ENODEV;
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200626 } else
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100627 dev_info(&dev->dev, "Fast Track Pro config OK\n");
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200628
629 return 0;
630}
631
Daniel Macke5779992010-03-04 19:46:13 +0100632/*
633 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
634 * documented in the device's data sheet.
635 */
636static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
637{
638 u8 buf[4];
639 buf[0] = 0x20;
640 buf[1] = value & 0xff;
641 buf[2] = (value >> 8) & 0xff;
642 buf[3] = reg;
643 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
644 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200645 0, 0, &buf, 4);
Daniel Macke5779992010-03-04 19:46:13 +0100646}
647
648static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
649{
650 /*
651 * Enable line-out driver mode, set headphone source to front
652 * channels, enable stereo mic.
653 */
654 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
655}
656
657/*
658 * C-Media CM6206 is based on CM106 with two additional
659 * registers that are not documented in the data sheet.
660 * Values here are chosen based on sniffing USB traffic
661 * under Windows.
662 */
663static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
664{
Daniel Mackdac8f842011-08-06 00:23:18 +0200665 int err = 0, reg;
Eric Lammerts157186b2011-05-27 18:16:52 -0400666 int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
Daniel Macke5779992010-03-04 19:46:13 +0100667
668 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
669 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
670 if (err < 0)
671 return err;
672 }
673
674 return err;
675}
676
Takashi Iwai19570d72013-12-19 14:32:53 +0100677/* quirk for Plantronics GameCom 780 with CM6302 chip */
678static int snd_usb_gamecon780_boot_quirk(struct usb_device *dev)
679{
680 /* set the initial volume and don't change; other values are either
681 * too loud or silent due to firmware bug (bko#65251)
682 */
Paul S McSpadden542baf92014-08-03 17:47:36 -0500683 u8 buf[2] = { 0x74, 0xe3 };
Takashi Iwai19570d72013-12-19 14:32:53 +0100684 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
685 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
686 UAC_FU_VOLUME << 8, 9 << 8, buf, 2);
687}
688
Daniel Macke5779992010-03-04 19:46:13 +0100689/*
Mark Hills5e212332013-03-17 11:07:53 +0000690 * Novation Twitch DJ controller
Eduard Gilmutdinov11e424e2013-12-20 14:06:58 +0600691 * Focusrite Novation Saffire 6 USB audio card
Mark Hills5e212332013-03-17 11:07:53 +0000692 */
Eduard Gilmutdinov11e424e2013-12-20 14:06:58 +0600693static int snd_usb_novation_boot_quirk(struct usb_device *dev)
Mark Hills5e212332013-03-17 11:07:53 +0000694{
695 /* preemptively set up the device because otherwise the
696 * raw MIDI endpoints are not active */
697 usb_set_interface(dev, 0, 1);
698 return 0;
699}
700
701/*
Daniel Macke5779992010-03-04 19:46:13 +0100702 * This call will put the synth in "USB send" mode, i.e it will send MIDI
703 * messages through USB (this is disabled at startup). The synth will
704 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
705 * sign on its LCD. Values here are chosen based on sniffing USB traffic
706 * under Windows.
707 */
708static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
709{
710 int err, actual_length;
711
712 /* "midi send" enable */
713 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
714
715 void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
716 if (!buf)
717 return -ENOMEM;
718 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
719 ARRAY_SIZE(seq), &actual_length, 1000);
720 kfree(buf);
721 if (err < 0)
722 return err;
723
724 return 0;
725}
726
727/*
Daniel Mack54a8c502011-02-11 11:08:06 +0000728 * Some sound cards from Native Instruments are in fact compliant to the USB
729 * audio standard of version 2 and other approved USB standards, even though
730 * they come up as vendor-specific device when first connected.
731 *
732 * However, they can be told to come up with a new set of descriptors
733 * upon their next enumeration, and the interfaces announced by the new
734 * descriptors will then be handled by the kernel's class drivers. As the
735 * product ID will also change, no further checks are required.
736 */
737
738static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
739{
740 int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
741 0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Eldad Zack889d6682013-04-05 20:49:46 +0200742 1, 0, NULL, 0, 1000);
Daniel Mack54a8c502011-02-11 11:08:06 +0000743
744 if (ret < 0)
745 return ret;
746
747 usb_reset_device(dev);
748
749 /* return -EAGAIN, so the creation of an audio interface for this
750 * temporary device is aborted. The device will reconnect with a
751 * new product ID */
752 return -EAGAIN;
753}
754
Damien Zammitcb998642012-12-19 11:27:22 +0100755static void mbox2_setup_48_24_magic(struct usb_device *dev)
756{
757 u8 srate[3];
758 u8 temp[12];
759
760 /* Choose 48000Hz permanently */
761 srate[0] = 0x80;
762 srate[1] = 0xbb;
763 srate[2] = 0x00;
764
765 /* Send the magic! */
766 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
767 0x01, 0x22, 0x0100, 0x0085, &temp, 0x0003);
768 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
769 0x81, 0xa2, 0x0100, 0x0085, &srate, 0x0003);
770 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
771 0x81, 0xa2, 0x0100, 0x0086, &srate, 0x0003);
772 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
773 0x81, 0xa2, 0x0100, 0x0003, &srate, 0x0003);
774 return;
775}
776
777/* Digidesign Mbox 2 needs to load firmware onboard
778 * and driver must wait a few seconds for initialisation.
779 */
780
781#define MBOX2_FIRMWARE_SIZE 646
782#define MBOX2_BOOT_LOADING 0x01 /* Hard coded into the device */
783#define MBOX2_BOOT_READY 0x02 /* Hard coded into the device */
784
Damien Zammitb7b435e2013-01-04 09:51:44 +0100785static int snd_usb_mbox2_boot_quirk(struct usb_device *dev)
Damien Zammitcb998642012-12-19 11:27:22 +0100786{
787 struct usb_host_config *config = dev->actconfig;
788 int err;
Jiri Slaby4909a0c2013-02-17 14:33:04 +0100789 u8 bootresponse[0x12];
Damien Zammitcb998642012-12-19 11:27:22 +0100790 int fwsize;
791 int count;
792
793 fwsize = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
794
795 if (fwsize != MBOX2_FIRMWARE_SIZE) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100796 dev_err(&dev->dev, "Invalid firmware size=%d.\n", fwsize);
Damien Zammitcb998642012-12-19 11:27:22 +0100797 return -ENODEV;
798 }
799
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100800 dev_dbg(&dev->dev, "Sending Digidesign Mbox 2 boot sequence...\n");
Damien Zammitcb998642012-12-19 11:27:22 +0100801
802 count = 0;
Damien Zammitb7b435e2013-01-04 09:51:44 +0100803 bootresponse[0] = MBOX2_BOOT_LOADING;
804 while ((bootresponse[0] == MBOX2_BOOT_LOADING) && (count < 10)) {
Damien Zammitcb998642012-12-19 11:27:22 +0100805 msleep(500); /* 0.5 second delay */
806 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
807 /* Control magic - load onboard firmware */
808 0x85, 0xc0, 0x0001, 0x0000, &bootresponse, 0x0012);
Damien Zammitb7b435e2013-01-04 09:51:44 +0100809 if (bootresponse[0] == MBOX2_BOOT_READY)
Damien Zammitcb998642012-12-19 11:27:22 +0100810 break;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100811 dev_dbg(&dev->dev, "device not ready, resending boot sequence...\n");
Damien Zammitcb998642012-12-19 11:27:22 +0100812 count++;
813 }
814
Damien Zammitb7b435e2013-01-04 09:51:44 +0100815 if (bootresponse[0] != MBOX2_BOOT_READY) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100816 dev_err(&dev->dev, "Unknown bootresponse=%d, or timed out, ignoring device.\n", bootresponse[0]);
Damien Zammitcb998642012-12-19 11:27:22 +0100817 return -ENODEV;
818 }
819
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100820 dev_dbg(&dev->dev, "device initialised!\n");
Damien Zammitcb998642012-12-19 11:27:22 +0100821
822 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
823 &dev->descriptor, sizeof(dev->descriptor));
824 config = dev->actconfig;
825 if (err < 0)
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100826 dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
Damien Zammitcb998642012-12-19 11:27:22 +0100827
828 err = usb_reset_configuration(dev);
829 if (err < 0)
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100830 dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
831 dev_dbg(&dev->dev, "mbox2_boot: new boot length = %d\n",
Damien Zammitcb998642012-12-19 11:27:22 +0100832 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
833
834 mbox2_setup_48_24_magic(dev);
835
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100836 dev_info(&dev->dev, "Digidesign Mbox 2: 24bit 48kHz");
Damien Zammitcb998642012-12-19 11:27:22 +0100837
838 return 0; /* Successful boot */
839}
840
Daniel Mack54a8c502011-02-11 11:08:06 +0000841/*
Daniel Macke5779992010-03-04 19:46:13 +0100842 * Setup quirks
843 */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200844#define MAUDIO_SET 0x01 /* parse device_setup */
845#define MAUDIO_SET_COMPATIBLE 0x80 /* use only "win-compatible" interfaces */
846#define MAUDIO_SET_DTS 0x02 /* enable DTS Digital Output */
847#define MAUDIO_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
848#define MAUDIO_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
849#define MAUDIO_SET_DI 0x10 /* enable Digital Input */
850#define MAUDIO_SET_MASK 0x1f /* bit mask for setup value */
851#define MAUDIO_SET_24B_48K_DI 0x19 /* 24bits+48KHz+Digital Input */
852#define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
853#define MAUDIO_SET_16B_48K_DI 0x11 /* 16bits+48KHz+Digital Input */
854#define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
855
856static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
857 int iface, int altno)
858{
859 /* Reset ALL ifaces to 0 altsetting.
860 * Call it for every possible altsetting of every interface.
861 */
862 usb_set_interface(chip->dev, iface, 0);
863 if (chip->setup & MAUDIO_SET) {
864 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
865 if (iface != 1 && iface != 2)
866 return 1; /* skip all interfaces but 1 and 2 */
867 } else {
868 unsigned int mask;
869 if (iface == 1 || iface == 2)
870 return 1; /* skip interfaces 1 and 2 */
871 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
872 return 1; /* skip this altsetting */
873 mask = chip->setup & MAUDIO_SET_MASK;
874 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
875 return 1; /* skip this altsetting */
876 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
877 return 1; /* skip this altsetting */
878 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
879 return 1; /* skip this altsetting */
880 }
881 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100882 usb_audio_dbg(chip,
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200883 "using altsetting %d for interface %d config %d\n",
884 altno, iface, chip->setup);
885 return 0; /* keep this altsetting */
886}
Daniel Macke5779992010-03-04 19:46:13 +0100887
888static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
889 int iface,
890 int altno)
891{
892 /* Reset ALL ifaces to 0 altsetting.
893 * Call it for every possible altsetting of every interface.
894 */
895 usb_set_interface(chip->dev, iface, 0);
896
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200897 if (chip->setup & MAUDIO_SET) {
898 unsigned int mask;
899 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
Daniel Macke5779992010-03-04 19:46:13 +0100900 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200901 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
Daniel Macke5779992010-03-04 19:46:13 +0100902 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200903 mask = chip->setup & MAUDIO_SET_MASK;
904 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
Daniel Macke5779992010-03-04 19:46:13 +0100905 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200906 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
Daniel Macke5779992010-03-04 19:46:13 +0100907 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200908 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
Daniel Macke5779992010-03-04 19:46:13 +0100909 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200910 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
Daniel Macke5779992010-03-04 19:46:13 +0100911 return 1; /* skip this altsetting */
912 }
913
914 return 0; /* keep this altsetting */
915}
916
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200917static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
918 int iface, int altno)
919{
920 /* Reset ALL ifaces to 0 altsetting.
921 * Call it for every possible altsetting of every interface.
922 */
923 usb_set_interface(chip->dev, iface, 0);
924
925 /* possible configuration where both inputs and only one output is
926 *used is not supported by the current setup
927 */
928 if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
929 if (chip->setup & MAUDIO_SET_96K) {
930 if (altno != 3 && altno != 6)
931 return 1;
932 } else if (chip->setup & MAUDIO_SET_DI) {
933 if (iface == 4)
934 return 1; /* no analog input */
935 if (altno != 2 && altno != 5)
936 return 1; /* enable only altsets 2 and 5 */
937 } else {
938 if (iface == 5)
939 return 1; /* disable digialt input */
940 if (altno != 2 && altno != 5)
941 return 1; /* enalbe only altsets 2 and 5 */
942 }
943 } else {
944 /* keep only 16-Bit mode */
945 if (altno != 1)
946 return 1;
947 }
948
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100949 usb_audio_dbg(chip,
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200950 "using altsetting %d for interface %d config %d\n",
951 altno, iface, chip->setup);
952 return 0; /* keep this altsetting */
953}
954
Daniel Macke5779992010-03-04 19:46:13 +0100955int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
956 int iface,
957 int altno)
958{
959 /* audiophile usb: skip altsets incompatible with device_setup */
960 if (chip->usb_id == USB_ID(0x0763, 0x2003))
961 return audiophile_skip_setting_quirk(chip, iface, altno);
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200962 /* quattro usb: skip altsets incompatible with device_setup */
963 if (chip->usb_id == USB_ID(0x0763, 0x2001))
964 return quattro_skip_setting_quirk(chip, iface, altno);
965 /* fasttrackpro usb: skip altsets incompatible with device_setup */
966 if (chip->usb_id == USB_ID(0x0763, 0x2012))
967 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
Daniel Macke5779992010-03-04 19:46:13 +0100968
969 return 0;
970}
971
972int snd_usb_apply_boot_quirk(struct usb_device *dev,
973 struct usb_interface *intf,
974 const struct snd_usb_audio_quirk *quirk)
975{
976 u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
977 le16_to_cpu(dev->descriptor.idProduct));
978
Daniel Mack3347b262011-02-11 11:34:12 +0000979 switch (id) {
980 case USB_ID(0x041e, 0x3000):
981 /* SB Extigy needs special boot-up sequence */
982 /* if more models come, this will go to the quirk list. */
Daniel Macke5779992010-03-04 19:46:13 +0100983 return snd_usb_extigy_boot_quirk(dev, intf);
984
Daniel Mack3347b262011-02-11 11:34:12 +0000985 case USB_ID(0x041e, 0x3020):
986 /* SB Audigy 2 NX needs its own boot-up magic, too */
Daniel Macke5779992010-03-04 19:46:13 +0100987 return snd_usb_audigy2nx_boot_quirk(dev);
988
Daniel Mack3347b262011-02-11 11:34:12 +0000989 case USB_ID(0x10f5, 0x0200):
990 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
Daniel Macke5779992010-03-04 19:46:13 +0100991 return snd_usb_cm106_boot_quirk(dev);
992
Daniel Mack3347b262011-02-11 11:34:12 +0000993 case USB_ID(0x0d8c, 0x0102):
994 /* C-Media CM6206 / CM106-Like Sound Device */
Wolfgang Breyha8129e792011-04-28 16:18:40 +0200995 case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
Daniel Macke5779992010-03-04 19:46:13 +0100996 return snd_usb_cm6206_boot_quirk(dev);
997
Damien Zammitcb998642012-12-19 11:27:22 +0100998 case USB_ID(0x0dba, 0x3000):
999 /* Digidesign Mbox 2 */
1000 return snd_usb_mbox2_boot_quirk(dev);
1001
Eduard Gilmutdinov11e424e2013-12-20 14:06:58 +06001002 case USB_ID(0x1235, 0x0010): /* Focusrite Novation Saffire 6 USB */
1003 case USB_ID(0x1235, 0x0018): /* Focusrite Novation Twitch */
1004 return snd_usb_novation_boot_quirk(dev);
Mark Hills5e212332013-03-17 11:07:53 +00001005
Daniel Mack3347b262011-02-11 11:34:12 +00001006 case USB_ID(0x133e, 0x0815):
1007 /* Access Music VirusTI Desktop */
Daniel Macke5779992010-03-04 19:46:13 +01001008 return snd_usb_accessmusic_boot_quirk(dev);
1009
Daniel Mack759e890f2011-05-18 11:28:41 +02001010 case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
Daniel Mack3347b262011-02-11 11:34:12 +00001011 case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
1012 case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
Daniel Mack54a8c502011-02-11 11:08:06 +00001013 return snd_usb_nativeinstruments_boot_quirk(dev);
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001014 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
1015 return snd_usb_fasttrackpro_boot_quirk(dev);
Takashi Iwai19570d72013-12-19 14:32:53 +01001016 case USB_ID(0x047f, 0xc010): /* Plantronics Gamecom 780 */
1017 return snd_usb_gamecon780_boot_quirk(dev);
Daniel Mack3347b262011-02-11 11:34:12 +00001018 }
Daniel Mack54a8c502011-02-11 11:08:06 +00001019
Daniel Macke5779992010-03-04 19:46:13 +01001020 return 0;
1021}
1022
1023/*
1024 * check if the device uses big-endian samples
1025 */
1026int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
1027{
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04001028 /* it depends on altsetting whether the device is big-endian or not */
Daniel Macke5779992010-03-04 19:46:13 +01001029 switch (chip->usb_id) {
1030 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001031 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1032 fp->altsetting == 5 || fp->altsetting == 6)
Daniel Macke5779992010-03-04 19:46:13 +01001033 return 1;
1034 break;
1035 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
1036 if (chip->setup == 0x00 ||
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001037 fp->altsetting == 1 || fp->altsetting == 2 ||
1038 fp->altsetting == 3)
Daniel Macke5779992010-03-04 19:46:13 +01001039 return 1;
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001040 break;
1041 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
1042 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1043 fp->altsetting == 5 || fp->altsetting == 6)
1044 return 1;
1045 break;
Daniel Macke5779992010-03-04 19:46:13 +01001046 }
1047 return 0;
1048}
1049
1050/*
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -05001051 * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
Daniel Macke5779992010-03-04 19:46:13 +01001052 * not for interface.
1053 */
1054
1055enum {
1056 EMU_QUIRK_SR_44100HZ = 0,
1057 EMU_QUIRK_SR_48000HZ,
1058 EMU_QUIRK_SR_88200HZ,
1059 EMU_QUIRK_SR_96000HZ,
1060 EMU_QUIRK_SR_176400HZ,
1061 EMU_QUIRK_SR_192000HZ
1062};
1063
1064static void set_format_emu_quirk(struct snd_usb_substream *subs,
1065 struct audioformat *fmt)
1066{
1067 unsigned char emu_samplerate_id = 0;
1068
1069 /* When capture is active
1070 * sample rate shouldn't be changed
1071 * by playback substream
1072 */
1073 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1074 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
1075 return;
1076 }
1077
1078 switch (fmt->rate_min) {
1079 case 48000:
1080 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
1081 break;
1082 case 88200:
1083 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
1084 break;
1085 case 96000:
1086 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
1087 break;
1088 case 176400:
1089 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
1090 break;
1091 case 192000:
1092 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
1093 break;
1094 default:
1095 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
1096 break;
1097 }
1098 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
Calvin Owens1539d4f2013-04-12 22:33:59 -05001099 subs->pkt_offset_adj = (emu_samplerate_id >= EMU_QUIRK_SR_176400HZ) ? 4 : 0;
Daniel Macke5779992010-03-04 19:46:13 +01001100}
1101
1102void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
1103 struct audioformat *fmt)
1104{
1105 switch (subs->stream->chip->usb_id) {
1106 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
1107 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
1108 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -05001109 case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
Daniel Macke5779992010-03-04 19:46:13 +01001110 set_format_emu_quirk(subs, fmt);
1111 break;
1112 }
1113}
1114
Joe Turnerb62b9982015-02-16 20:44:33 +00001115bool snd_usb_get_sample_rate_quirk(struct snd_usb_audio *chip)
1116{
Eric Wong9fc88ad2015-03-31 07:34:05 +00001117 /* devices which do not support reading the sample rate. */
1118 switch (chip->usb_id) {
Adam Honseeef03422015-04-12 01:03:07 -05001119 case USB_ID(0x045E, 0x075D): /* MS Lifecam Cinema */
Eric Wong9fc88ad2015-03-31 07:34:05 +00001120 case USB_ID(0x045E, 0x076D): /* MS Lifecam HD-5000 */
Takashi Iwaifa94b0d2015-05-19 10:46:49 +02001121 case USB_ID(0x045E, 0x0772): /* MS Lifecam Studio */
Vittorio G (VittGam)ae425bb2015-05-22 21:15:19 +02001122 case USB_ID(0x045E, 0x0779): /* MS Lifecam HD-3000 */
Eric Wong9fc88ad2015-03-31 07:34:05 +00001123 case USB_ID(0x04D8, 0xFEEA): /* Benchmark DAC1 Pre */
Eric Wong2f80b292015-05-30 09:15:39 +00001124 case USB_ID(0x074D, 0x3553): /* Outlaw RR2150 (Micronas UAC3553B) */
Anssi Hannula73a5fda2015-12-13 20:49:59 +02001125 case USB_ID(0x21B4, 0x0081): /* AudioQuest DragonFly */
Eric Wong9fc88ad2015-03-31 07:34:05 +00001126 return true;
1127 }
1128 return false;
Joe Turnerb62b9982015-02-16 20:44:33 +00001129}
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001130
1131/* Marantz/Denon USB DACs need a vendor cmd to switch
1132 * between PCM and native DSD mode
1133 */
Takashi Iwai8b28c932015-03-04 15:37:01 +01001134static bool is_marantz_denon_dac(unsigned int id)
1135{
1136 switch (id) {
1137 case USB_ID(0x154e, 0x1003): /* Denon DA-300USB */
1138 case USB_ID(0x154e, 0x3005): /* Marantz HD-DAC1 */
1139 case USB_ID(0x154e, 0x3006): /* Marantz SA-14S1 */
1140 return true;
1141 }
1142 return false;
1143}
1144
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001145int snd_usb_select_mode_quirk(struct snd_usb_substream *subs,
1146 struct audioformat *fmt)
1147{
1148 struct usb_device *dev = subs->dev;
1149 int err;
1150
Takashi Iwai8b28c932015-03-04 15:37:01 +01001151 if (is_marantz_denon_dac(subs->stream->chip->usb_id)) {
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001152 /* First switch to alt set 0, otherwise the mode switch cmd
1153 * will not be accepted by the DAC
1154 */
1155 err = usb_set_interface(dev, fmt->iface, 0);
1156 if (err < 0)
1157 return err;
1158
1159 mdelay(20); /* Delay needed after setting the interface */
1160
1161 switch (fmt->altsetting) {
1162 case 2: /* DSD mode requested */
1163 case 1: /* PCM mode requested */
1164 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1165 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1166 fmt->altsetting - 1, 1, NULL, 0);
1167 if (err < 0)
1168 return err;
1169 break;
1170 }
1171 mdelay(20);
1172 }
1173 return 0;
1174}
1175
Daniel Mack2b58fd52012-09-04 10:23:07 +02001176void snd_usb_endpoint_start_quirk(struct snd_usb_endpoint *ep)
1177{
1178 /*
1179 * "Playback Design" products send bogus feedback data at the start
1180 * of the stream. Ignore them.
1181 */
1182 if ((le16_to_cpu(ep->chip->dev->descriptor.idVendor) == 0x23ba) &&
1183 ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
1184 ep->skip_packets = 4;
Eldad Zack83e3acd2013-01-13 23:02:03 +01001185
1186 /*
Matt Gruskine9a25e02013-02-09 12:56:35 -05001187 * M-Audio Fast Track C400/C600 - when packets are not skipped, real
1188 * world latency varies by approx. +/- 50 frames (at 96KHz) each time
1189 * the stream is (re)started. When skipping packets 16 at endpoint
1190 * start up, the real world latency is stable within +/- 1 frame (also
Eldad Zack83e3acd2013-01-13 23:02:03 +01001191 * across power cycles).
1192 */
Matt Gruskine9a25e02013-02-09 12:56:35 -05001193 if ((ep->chip->usb_id == USB_ID(0x0763, 0x2030) ||
1194 ep->chip->usb_id == USB_ID(0x0763, 0x2031)) &&
Eldad Zack83e3acd2013-01-13 23:02:03 +01001195 ep->type == SND_USB_ENDPOINT_TYPE_DATA)
1196 ep->skip_packets = 16;
Daniel Mack2b58fd52012-09-04 10:23:07 +02001197}
1198
Daniel Mack21bb5aa2013-04-10 00:56:03 +08001199void snd_usb_set_interface_quirk(struct usb_device *dev)
1200{
1201 /*
1202 * "Playback Design" products need a 50ms delay after setting the
1203 * USB interface.
1204 */
Guillaume Fougnies465cc6d2016-01-26 00:28:27 +01001205 switch (le16_to_cpu(dev->descriptor.idVendor)) {
1206 case 0x23ba: /* Playback Design */
1207 case 0x0644: /* TEAC Corp. */
Daniel Mack21bb5aa2013-04-10 00:56:03 +08001208 mdelay(50);
Guillaume Fougnies465cc6d2016-01-26 00:28:27 +01001209 break;
1210 }
Daniel Mack21bb5aa2013-04-10 00:56:03 +08001211}
1212
Daniel Mack2b58fd52012-09-04 10:23:07 +02001213void snd_usb_ctl_msg_quirk(struct usb_device *dev, unsigned int pipe,
1214 __u8 request, __u8 requesttype, __u16 value,
1215 __u16 index, void *data, __u16 size)
1216{
1217 /*
1218 * "Playback Design" products need a 20ms delay after each
1219 * class compliant request
1220 */
1221 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x23ba) &&
1222 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1223 mdelay(20);
Jurgen Kramer6e84a8d2014-11-15 14:01:21 +01001224
Guillaume Fougnies465cc6d2016-01-26 00:28:27 +01001225 /*
1226 * "TEAC Corp." products need a 20ms delay after each
1227 * class compliant request
1228 */
1229 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x0644) &&
1230 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1231 mdelay(20);
1232
Jurgen Kramer6e84a8d2014-11-15 14:01:21 +01001233 /* Marantz/Denon devices with USB DAC functionality need a delay
1234 * after each class compliant request
1235 */
Takashi Iwai8b28c932015-03-04 15:37:01 +01001236 if (is_marantz_denon_dac(USB_ID(le16_to_cpu(dev->descriptor.idVendor),
1237 le16_to_cpu(dev->descriptor.idProduct)))
1238 && (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1239 mdelay(20);
Panu Matilainendacacb02014-11-30 18:45:40 +02001240
1241 /* Zoom R16/24 needs a tiny delay here, otherwise requests like
1242 * get/set frequency return as failed despite actually succeeding.
1243 */
1244 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x1686) &&
1245 (le16_to_cpu(dev->descriptor.idProduct) == 0x00dd) &&
1246 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1247 mdelay(1);
Daniel Mack2b58fd52012-09-04 10:23:07 +02001248}
1249
Daniel Mack126825e2013-04-17 00:01:40 +08001250/*
1251 * snd_usb_interface_dsd_format_quirks() is called from format.c to
1252 * augment the PCM format bit-field for DSD types. The UAC standards
1253 * don't have a designated bit field to denote DSD-capable interfaces,
1254 * hence all hardware that is known to support this format has to be
1255 * listed here.
1256 */
1257u64 snd_usb_interface_dsd_format_quirks(struct snd_usb_audio *chip,
1258 struct audioformat *fp,
1259 unsigned int sample_bytes)
1260{
1261 /* Playback Designs */
1262 if (le16_to_cpu(chip->dev->descriptor.idVendor) == 0x23ba) {
1263 switch (fp->altsetting) {
1264 case 1:
1265 fp->dsd_dop = true;
1266 return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1267 case 2:
1268 fp->dsd_bitrev = true;
1269 return SNDRV_PCM_FMTBIT_DSD_U8;
1270 case 3:
1271 fp->dsd_bitrev = true;
1272 return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1273 }
1274 }
1275
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001276 /* XMOS based USB DACs */
1277 switch (chip->usb_id) {
Jurgen Kramer38f74d52014-12-17 17:45:20 +01001278 case USB_ID(0x20b1, 0x3008): /* iFi Audio micro/nano iDSD */
1279 case USB_ID(0x20b1, 0x2008): /* Matrix Audio X-Sabre */
1280 case USB_ID(0x20b1, 0x300a): /* Matrix Audio Mini-i Pro */
Jurgen Krameraa135852016-01-11 08:16:58 +01001281 case USB_ID(0x22d8, 0x0416): /* OPPO HA-1*/
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001282 if (fp->altsetting == 2)
Jussi Laakod42472e2014-11-21 16:04:46 +02001283 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001284 break;
Jurgen Kramer3b7e5c72015-06-05 09:42:49 +02001285
Jurgen Kramere6212cc2015-08-21 09:48:35 +02001286 case USB_ID(0x20b1, 0x000a): /* Gustard DAC-X20U */
Jurgen Kramer3b7e5c72015-06-05 09:42:49 +02001287 case USB_ID(0x20b1, 0x2009): /* DIYINHK DSD DXD 384kHz USB to I2S/DSD */
1288 case USB_ID(0x20b1, 0x2023): /* JLsounds I2SoverUSB */
Jurgen Kramer3577eb62015-11-09 12:13:55 +01001289 case USB_ID(0x20b1, 0x3023): /* Aune X1S 32BIT/384 DSD DAC */
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001290 if (fp->altsetting == 3)
Jussi Laakod42472e2014-11-21 16:04:46 +02001291 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001292 break;
1293 default:
1294 break;
1295 }
1296
Jurgen Kramer7a2e9dd2014-11-28 17:32:53 +01001297 /* Denon/Marantz devices with USB DAC functionality */
Takashi Iwai8b28c932015-03-04 15:37:01 +01001298 if (is_marantz_denon_dac(chip->usb_id)) {
Jurgen Kramer7a2e9dd2014-11-28 17:32:53 +01001299 if (fp->altsetting == 2)
1300 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
Jurgen Kramer7a2e9dd2014-11-28 17:32:53 +01001301 }
1302
Daniel Mack126825e2013-04-17 00:01:40 +08001303 return 0;
1304}