blob: 754e689596a21b43f3b3a45b8f3062ec29b74099 [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,
Daniel Macke5779992010-03-04 19:46:13 +0100538 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
539 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
540 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
Daniel Mack014950b2011-05-25 09:09:02 +0200541 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
542 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100543 };
544
545 if (quirk->type < QUIRK_TYPE_COUNT) {
546 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
547 } else {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100548 usb_audio_err(chip, "invalid quirk type %d\n", quirk->type);
Daniel Macke5779992010-03-04 19:46:13 +0100549 return -ENXIO;
550 }
551}
552
553/*
554 * boot quirks
555 */
556
557#define EXTIGY_FIRMWARE_SIZE_OLD 794
558#define EXTIGY_FIRMWARE_SIZE_NEW 483
559
560static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
561{
562 struct usb_host_config *config = dev->actconfig;
563 int err;
564
565 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
566 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100567 dev_dbg(&dev->dev, "sending Extigy boot sequence...\n");
Daniel Macke5779992010-03-04 19:46:13 +0100568 /* Send message to force it to reconnect with full interface. */
569 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200570 0x10, 0x43, 0x0001, 0x000a, NULL, 0);
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100571 if (err < 0)
572 dev_dbg(&dev->dev, "error sending boot message: %d\n", err);
Daniel Macke5779992010-03-04 19:46:13 +0100573 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
574 &dev->descriptor, sizeof(dev->descriptor));
575 config = dev->actconfig;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100576 if (err < 0)
577 dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
Daniel Macke5779992010-03-04 19:46:13 +0100578 err = usb_reset_configuration(dev);
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100579 if (err < 0)
580 dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
581 dev_dbg(&dev->dev, "extigy_boot: new boot length = %d\n",
Daniel Macke5779992010-03-04 19:46:13 +0100582 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
583 return -ENODEV; /* quit this anyway */
584 }
585 return 0;
586}
587
588static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
589{
590 u8 buf = 1;
591
592 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
593 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200594 0, 0, &buf, 1);
Daniel Macke5779992010-03-04 19:46:13 +0100595 if (buf == 0) {
596 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
597 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200598 1, 2000, NULL, 0);
Daniel Macke5779992010-03-04 19:46:13 +0100599 return -ENODEV;
600 }
601 return 0;
602}
603
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200604static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
605{
606 int err;
607
608 if (dev->actconfig->desc.bConfigurationValue == 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100609 dev_info(&dev->dev,
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200610 "Fast Track Pro switching to config #2\n");
611 /* This function has to be available by the usb core module.
612 * if it is not avialable the boot quirk has to be left out
613 * and the configuration has to be set by udev or hotplug
614 * rules
615 */
616 err = usb_driver_set_configuration(dev, 2);
David Henningssonb98ae272013-01-04 17:02:18 +0100617 if (err < 0)
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100618 dev_dbg(&dev->dev,
619 "error usb_driver_set_configuration: %d\n",
620 err);
David Henningssonb98ae272013-01-04 17:02:18 +0100621 /* Always return an error, so that we stop creating a device
622 that will just be destroyed and recreated with a new
623 configuration */
624 return -ENODEV;
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200625 } else
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100626 dev_info(&dev->dev, "Fast Track Pro config OK\n");
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200627
628 return 0;
629}
630
Daniel Macke5779992010-03-04 19:46:13 +0100631/*
632 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
633 * documented in the device's data sheet.
634 */
635static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
636{
637 u8 buf[4];
638 buf[0] = 0x20;
639 buf[1] = value & 0xff;
640 buf[2] = (value >> 8) & 0xff;
641 buf[3] = reg;
642 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
643 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200644 0, 0, &buf, 4);
Daniel Macke5779992010-03-04 19:46:13 +0100645}
646
647static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
648{
649 /*
650 * Enable line-out driver mode, set headphone source to front
651 * channels, enable stereo mic.
652 */
653 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
654}
655
656/*
657 * C-Media CM6206 is based on CM106 with two additional
658 * registers that are not documented in the data sheet.
659 * Values here are chosen based on sniffing USB traffic
660 * under Windows.
661 */
662static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
663{
Daniel Mackdac8f842011-08-06 00:23:18 +0200664 int err = 0, reg;
Eric Lammerts157186b2011-05-27 18:16:52 -0400665 int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
Daniel Macke5779992010-03-04 19:46:13 +0100666
667 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
668 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
669 if (err < 0)
670 return err;
671 }
672
673 return err;
674}
675
Takashi Iwai19570d72013-12-19 14:32:53 +0100676/* quirk for Plantronics GameCom 780 with CM6302 chip */
677static int snd_usb_gamecon780_boot_quirk(struct usb_device *dev)
678{
679 /* set the initial volume and don't change; other values are either
680 * too loud or silent due to firmware bug (bko#65251)
681 */
Paul S McSpadden542baf92014-08-03 17:47:36 -0500682 u8 buf[2] = { 0x74, 0xe3 };
Takashi Iwai19570d72013-12-19 14:32:53 +0100683 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
684 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
685 UAC_FU_VOLUME << 8, 9 << 8, buf, 2);
686}
687
Daniel Macke5779992010-03-04 19:46:13 +0100688/*
Mark Hills5e212332013-03-17 11:07:53 +0000689 * Novation Twitch DJ controller
Eduard Gilmutdinov11e424e2013-12-20 14:06:58 +0600690 * Focusrite Novation Saffire 6 USB audio card
Mark Hills5e212332013-03-17 11:07:53 +0000691 */
Eduard Gilmutdinov11e424e2013-12-20 14:06:58 +0600692static int snd_usb_novation_boot_quirk(struct usb_device *dev)
Mark Hills5e212332013-03-17 11:07:53 +0000693{
694 /* preemptively set up the device because otherwise the
695 * raw MIDI endpoints are not active */
696 usb_set_interface(dev, 0, 1);
697 return 0;
698}
699
700/*
Daniel Macke5779992010-03-04 19:46:13 +0100701 * This call will put the synth in "USB send" mode, i.e it will send MIDI
702 * messages through USB (this is disabled at startup). The synth will
703 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
704 * sign on its LCD. Values here are chosen based on sniffing USB traffic
705 * under Windows.
706 */
707static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
708{
709 int err, actual_length;
710
711 /* "midi send" enable */
712 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
713
714 void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
715 if (!buf)
716 return -ENOMEM;
717 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
718 ARRAY_SIZE(seq), &actual_length, 1000);
719 kfree(buf);
720 if (err < 0)
721 return err;
722
723 return 0;
724}
725
726/*
Daniel Mack54a8c502011-02-11 11:08:06 +0000727 * Some sound cards from Native Instruments are in fact compliant to the USB
728 * audio standard of version 2 and other approved USB standards, even though
729 * they come up as vendor-specific device when first connected.
730 *
731 * However, they can be told to come up with a new set of descriptors
732 * upon their next enumeration, and the interfaces announced by the new
733 * descriptors will then be handled by the kernel's class drivers. As the
734 * product ID will also change, no further checks are required.
735 */
736
737static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
738{
739 int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
740 0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Eldad Zack889d6682013-04-05 20:49:46 +0200741 1, 0, NULL, 0, 1000);
Daniel Mack54a8c502011-02-11 11:08:06 +0000742
743 if (ret < 0)
744 return ret;
745
746 usb_reset_device(dev);
747
748 /* return -EAGAIN, so the creation of an audio interface for this
749 * temporary device is aborted. The device will reconnect with a
750 * new product ID */
751 return -EAGAIN;
752}
753
Damien Zammitcb998642012-12-19 11:27:22 +0100754static void mbox2_setup_48_24_magic(struct usb_device *dev)
755{
756 u8 srate[3];
757 u8 temp[12];
758
759 /* Choose 48000Hz permanently */
760 srate[0] = 0x80;
761 srate[1] = 0xbb;
762 srate[2] = 0x00;
763
764 /* Send the magic! */
765 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
766 0x01, 0x22, 0x0100, 0x0085, &temp, 0x0003);
767 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
768 0x81, 0xa2, 0x0100, 0x0085, &srate, 0x0003);
769 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
770 0x81, 0xa2, 0x0100, 0x0086, &srate, 0x0003);
771 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
772 0x81, 0xa2, 0x0100, 0x0003, &srate, 0x0003);
773 return;
774}
775
776/* Digidesign Mbox 2 needs to load firmware onboard
777 * and driver must wait a few seconds for initialisation.
778 */
779
780#define MBOX2_FIRMWARE_SIZE 646
781#define MBOX2_BOOT_LOADING 0x01 /* Hard coded into the device */
782#define MBOX2_BOOT_READY 0x02 /* Hard coded into the device */
783
Damien Zammitb7b435e2013-01-04 09:51:44 +0100784static int snd_usb_mbox2_boot_quirk(struct usb_device *dev)
Damien Zammitcb998642012-12-19 11:27:22 +0100785{
786 struct usb_host_config *config = dev->actconfig;
787 int err;
Jiri Slaby4909a0c2013-02-17 14:33:04 +0100788 u8 bootresponse[0x12];
Damien Zammitcb998642012-12-19 11:27:22 +0100789 int fwsize;
790 int count;
791
792 fwsize = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
793
794 if (fwsize != MBOX2_FIRMWARE_SIZE) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100795 dev_err(&dev->dev, "Invalid firmware size=%d.\n", fwsize);
Damien Zammitcb998642012-12-19 11:27:22 +0100796 return -ENODEV;
797 }
798
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100799 dev_dbg(&dev->dev, "Sending Digidesign Mbox 2 boot sequence...\n");
Damien Zammitcb998642012-12-19 11:27:22 +0100800
801 count = 0;
Damien Zammitb7b435e2013-01-04 09:51:44 +0100802 bootresponse[0] = MBOX2_BOOT_LOADING;
803 while ((bootresponse[0] == MBOX2_BOOT_LOADING) && (count < 10)) {
Damien Zammitcb998642012-12-19 11:27:22 +0100804 msleep(500); /* 0.5 second delay */
805 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
806 /* Control magic - load onboard firmware */
807 0x85, 0xc0, 0x0001, 0x0000, &bootresponse, 0x0012);
Damien Zammitb7b435e2013-01-04 09:51:44 +0100808 if (bootresponse[0] == MBOX2_BOOT_READY)
Damien Zammitcb998642012-12-19 11:27:22 +0100809 break;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100810 dev_dbg(&dev->dev, "device not ready, resending boot sequence...\n");
Damien Zammitcb998642012-12-19 11:27:22 +0100811 count++;
812 }
813
Damien Zammitb7b435e2013-01-04 09:51:44 +0100814 if (bootresponse[0] != MBOX2_BOOT_READY) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100815 dev_err(&dev->dev, "Unknown bootresponse=%d, or timed out, ignoring device.\n", bootresponse[0]);
Damien Zammitcb998642012-12-19 11:27:22 +0100816 return -ENODEV;
817 }
818
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100819 dev_dbg(&dev->dev, "device initialised!\n");
Damien Zammitcb998642012-12-19 11:27:22 +0100820
821 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
822 &dev->descriptor, sizeof(dev->descriptor));
823 config = dev->actconfig;
824 if (err < 0)
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100825 dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
Damien Zammitcb998642012-12-19 11:27:22 +0100826
827 err = usb_reset_configuration(dev);
828 if (err < 0)
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100829 dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
830 dev_dbg(&dev->dev, "mbox2_boot: new boot length = %d\n",
Damien Zammitcb998642012-12-19 11:27:22 +0100831 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
832
833 mbox2_setup_48_24_magic(dev);
834
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100835 dev_info(&dev->dev, "Digidesign Mbox 2: 24bit 48kHz");
Damien Zammitcb998642012-12-19 11:27:22 +0100836
837 return 0; /* Successful boot */
838}
839
Daniel Mack54a8c502011-02-11 11:08:06 +0000840/*
Daniel Macke5779992010-03-04 19:46:13 +0100841 * Setup quirks
842 */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200843#define MAUDIO_SET 0x01 /* parse device_setup */
844#define MAUDIO_SET_COMPATIBLE 0x80 /* use only "win-compatible" interfaces */
845#define MAUDIO_SET_DTS 0x02 /* enable DTS Digital Output */
846#define MAUDIO_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
847#define MAUDIO_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
848#define MAUDIO_SET_DI 0x10 /* enable Digital Input */
849#define MAUDIO_SET_MASK 0x1f /* bit mask for setup value */
850#define MAUDIO_SET_24B_48K_DI 0x19 /* 24bits+48KHz+Digital Input */
851#define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
852#define MAUDIO_SET_16B_48K_DI 0x11 /* 16bits+48KHz+Digital Input */
853#define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
854
855static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
856 int iface, int altno)
857{
858 /* Reset ALL ifaces to 0 altsetting.
859 * Call it for every possible altsetting of every interface.
860 */
861 usb_set_interface(chip->dev, iface, 0);
862 if (chip->setup & MAUDIO_SET) {
863 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
864 if (iface != 1 && iface != 2)
865 return 1; /* skip all interfaces but 1 and 2 */
866 } else {
867 unsigned int mask;
868 if (iface == 1 || iface == 2)
869 return 1; /* skip interfaces 1 and 2 */
870 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
871 return 1; /* skip this altsetting */
872 mask = chip->setup & MAUDIO_SET_MASK;
873 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
874 return 1; /* skip this altsetting */
875 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
876 return 1; /* skip this altsetting */
877 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
878 return 1; /* skip this altsetting */
879 }
880 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100881 usb_audio_dbg(chip,
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200882 "using altsetting %d for interface %d config %d\n",
883 altno, iface, chip->setup);
884 return 0; /* keep this altsetting */
885}
Daniel Macke5779992010-03-04 19:46:13 +0100886
887static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
888 int iface,
889 int altno)
890{
891 /* Reset ALL ifaces to 0 altsetting.
892 * Call it for every possible altsetting of every interface.
893 */
894 usb_set_interface(chip->dev, iface, 0);
895
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200896 if (chip->setup & MAUDIO_SET) {
897 unsigned int mask;
898 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
Daniel Macke5779992010-03-04 19:46:13 +0100899 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200900 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
Daniel Macke5779992010-03-04 19:46:13 +0100901 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200902 mask = chip->setup & MAUDIO_SET_MASK;
903 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
Daniel Macke5779992010-03-04 19:46:13 +0100904 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200905 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
Daniel Macke5779992010-03-04 19:46:13 +0100906 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200907 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
Daniel Macke5779992010-03-04 19:46:13 +0100908 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200909 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
Daniel Macke5779992010-03-04 19:46:13 +0100910 return 1; /* skip this altsetting */
911 }
912
913 return 0; /* keep this altsetting */
914}
915
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200916static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
917 int iface, int altno)
918{
919 /* Reset ALL ifaces to 0 altsetting.
920 * Call it for every possible altsetting of every interface.
921 */
922 usb_set_interface(chip->dev, iface, 0);
923
924 /* possible configuration where both inputs and only one output is
925 *used is not supported by the current setup
926 */
927 if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
928 if (chip->setup & MAUDIO_SET_96K) {
929 if (altno != 3 && altno != 6)
930 return 1;
931 } else if (chip->setup & MAUDIO_SET_DI) {
932 if (iface == 4)
933 return 1; /* no analog input */
934 if (altno != 2 && altno != 5)
935 return 1; /* enable only altsets 2 and 5 */
936 } else {
937 if (iface == 5)
938 return 1; /* disable digialt input */
939 if (altno != 2 && altno != 5)
940 return 1; /* enalbe only altsets 2 and 5 */
941 }
942 } else {
943 /* keep only 16-Bit mode */
944 if (altno != 1)
945 return 1;
946 }
947
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100948 usb_audio_dbg(chip,
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200949 "using altsetting %d for interface %d config %d\n",
950 altno, iface, chip->setup);
951 return 0; /* keep this altsetting */
952}
953
Daniel Macke5779992010-03-04 19:46:13 +0100954int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
955 int iface,
956 int altno)
957{
958 /* audiophile usb: skip altsets incompatible with device_setup */
959 if (chip->usb_id == USB_ID(0x0763, 0x2003))
960 return audiophile_skip_setting_quirk(chip, iface, altno);
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200961 /* quattro usb: skip altsets incompatible with device_setup */
962 if (chip->usb_id == USB_ID(0x0763, 0x2001))
963 return quattro_skip_setting_quirk(chip, iface, altno);
964 /* fasttrackpro usb: skip altsets incompatible with device_setup */
965 if (chip->usb_id == USB_ID(0x0763, 0x2012))
966 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
Daniel Macke5779992010-03-04 19:46:13 +0100967
968 return 0;
969}
970
971int snd_usb_apply_boot_quirk(struct usb_device *dev,
972 struct usb_interface *intf,
973 const struct snd_usb_audio_quirk *quirk)
974{
975 u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
976 le16_to_cpu(dev->descriptor.idProduct));
977
Daniel Mack3347b262011-02-11 11:34:12 +0000978 switch (id) {
979 case USB_ID(0x041e, 0x3000):
980 /* SB Extigy needs special boot-up sequence */
981 /* if more models come, this will go to the quirk list. */
Daniel Macke5779992010-03-04 19:46:13 +0100982 return snd_usb_extigy_boot_quirk(dev, intf);
983
Daniel Mack3347b262011-02-11 11:34:12 +0000984 case USB_ID(0x041e, 0x3020):
985 /* SB Audigy 2 NX needs its own boot-up magic, too */
Daniel Macke5779992010-03-04 19:46:13 +0100986 return snd_usb_audigy2nx_boot_quirk(dev);
987
Daniel Mack3347b262011-02-11 11:34:12 +0000988 case USB_ID(0x10f5, 0x0200):
989 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
Daniel Macke5779992010-03-04 19:46:13 +0100990 return snd_usb_cm106_boot_quirk(dev);
991
Daniel Mack3347b262011-02-11 11:34:12 +0000992 case USB_ID(0x0d8c, 0x0102):
993 /* C-Media CM6206 / CM106-Like Sound Device */
Wolfgang Breyha8129e792011-04-28 16:18:40 +0200994 case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
Daniel Macke5779992010-03-04 19:46:13 +0100995 return snd_usb_cm6206_boot_quirk(dev);
996
Damien Zammitcb998642012-12-19 11:27:22 +0100997 case USB_ID(0x0dba, 0x3000):
998 /* Digidesign Mbox 2 */
999 return snd_usb_mbox2_boot_quirk(dev);
1000
Eduard Gilmutdinov11e424e2013-12-20 14:06:58 +06001001 case USB_ID(0x1235, 0x0010): /* Focusrite Novation Saffire 6 USB */
1002 case USB_ID(0x1235, 0x0018): /* Focusrite Novation Twitch */
1003 return snd_usb_novation_boot_quirk(dev);
Mark Hills5e212332013-03-17 11:07:53 +00001004
Daniel Mack3347b262011-02-11 11:34:12 +00001005 case USB_ID(0x133e, 0x0815):
1006 /* Access Music VirusTI Desktop */
Daniel Macke5779992010-03-04 19:46:13 +01001007 return snd_usb_accessmusic_boot_quirk(dev);
1008
Daniel Mack759e890f2011-05-18 11:28:41 +02001009 case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
Daniel Mack3347b262011-02-11 11:34:12 +00001010 case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
1011 case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
Daniel Mack54a8c502011-02-11 11:08:06 +00001012 return snd_usb_nativeinstruments_boot_quirk(dev);
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001013 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
1014 return snd_usb_fasttrackpro_boot_quirk(dev);
Takashi Iwai19570d72013-12-19 14:32:53 +01001015 case USB_ID(0x047f, 0xc010): /* Plantronics Gamecom 780 */
1016 return snd_usb_gamecon780_boot_quirk(dev);
Daniel Mack3347b262011-02-11 11:34:12 +00001017 }
Daniel Mack54a8c502011-02-11 11:08:06 +00001018
Daniel Macke5779992010-03-04 19:46:13 +01001019 return 0;
1020}
1021
1022/*
1023 * check if the device uses big-endian samples
1024 */
1025int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
1026{
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04001027 /* it depends on altsetting whether the device is big-endian or not */
Daniel Macke5779992010-03-04 19:46:13 +01001028 switch (chip->usb_id) {
1029 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001030 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1031 fp->altsetting == 5 || fp->altsetting == 6)
Daniel Macke5779992010-03-04 19:46:13 +01001032 return 1;
1033 break;
1034 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
1035 if (chip->setup == 0x00 ||
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001036 fp->altsetting == 1 || fp->altsetting == 2 ||
1037 fp->altsetting == 3)
Daniel Macke5779992010-03-04 19:46:13 +01001038 return 1;
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001039 break;
1040 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
1041 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1042 fp->altsetting == 5 || fp->altsetting == 6)
1043 return 1;
1044 break;
Daniel Macke5779992010-03-04 19:46:13 +01001045 }
1046 return 0;
1047}
1048
1049/*
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -05001050 * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
Daniel Macke5779992010-03-04 19:46:13 +01001051 * not for interface.
1052 */
1053
1054enum {
1055 EMU_QUIRK_SR_44100HZ = 0,
1056 EMU_QUIRK_SR_48000HZ,
1057 EMU_QUIRK_SR_88200HZ,
1058 EMU_QUIRK_SR_96000HZ,
1059 EMU_QUIRK_SR_176400HZ,
1060 EMU_QUIRK_SR_192000HZ
1061};
1062
1063static void set_format_emu_quirk(struct snd_usb_substream *subs,
1064 struct audioformat *fmt)
1065{
1066 unsigned char emu_samplerate_id = 0;
1067
1068 /* When capture is active
1069 * sample rate shouldn't be changed
1070 * by playback substream
1071 */
1072 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1073 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
1074 return;
1075 }
1076
1077 switch (fmt->rate_min) {
1078 case 48000:
1079 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
1080 break;
1081 case 88200:
1082 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
1083 break;
1084 case 96000:
1085 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
1086 break;
1087 case 176400:
1088 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
1089 break;
1090 case 192000:
1091 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
1092 break;
1093 default:
1094 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
1095 break;
1096 }
1097 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
Calvin Owens1539d4f2013-04-12 22:33:59 -05001098 subs->pkt_offset_adj = (emu_samplerate_id >= EMU_QUIRK_SR_176400HZ) ? 4 : 0;
Daniel Macke5779992010-03-04 19:46:13 +01001099}
1100
1101void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
1102 struct audioformat *fmt)
1103{
1104 switch (subs->stream->chip->usb_id) {
1105 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
1106 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
1107 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -05001108 case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
Daniel Macke5779992010-03-04 19:46:13 +01001109 set_format_emu_quirk(subs, fmt);
1110 break;
1111 }
1112}
1113
Joe Turnerb62b9982015-02-16 20:44:33 +00001114bool snd_usb_get_sample_rate_quirk(struct snd_usb_audio *chip)
1115{
Eric Wong9fc88ad2015-03-31 07:34:05 +00001116 /* devices which do not support reading the sample rate. */
1117 switch (chip->usb_id) {
Adam Honseeef03422015-04-12 01:03:07 -05001118 case USB_ID(0x045E, 0x075D): /* MS Lifecam Cinema */
Eric Wong9fc88ad2015-03-31 07:34:05 +00001119 case USB_ID(0x045E, 0x076D): /* MS Lifecam HD-5000 */
Takashi Iwaifa94b0d2015-05-19 10:46:49 +02001120 case USB_ID(0x045E, 0x0772): /* MS Lifecam Studio */
Vittorio G (VittGam)ae425bb2015-05-22 21:15:19 +02001121 case USB_ID(0x045E, 0x0779): /* MS Lifecam HD-3000 */
Eric Wong9fc88ad2015-03-31 07:34:05 +00001122 case USB_ID(0x04D8, 0xFEEA): /* Benchmark DAC1 Pre */
Eric Wong2f80b292015-05-30 09:15:39 +00001123 case USB_ID(0x074D, 0x3553): /* Outlaw RR2150 (Micronas UAC3553B) */
Eric Wong9fc88ad2015-03-31 07:34:05 +00001124 return true;
1125 }
1126 return false;
Joe Turnerb62b9982015-02-16 20:44:33 +00001127}
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001128
1129/* Marantz/Denon USB DACs need a vendor cmd to switch
1130 * between PCM and native DSD mode
1131 */
Takashi Iwai8b28c932015-03-04 15:37:01 +01001132static bool is_marantz_denon_dac(unsigned int id)
1133{
1134 switch (id) {
1135 case USB_ID(0x154e, 0x1003): /* Denon DA-300USB */
1136 case USB_ID(0x154e, 0x3005): /* Marantz HD-DAC1 */
1137 case USB_ID(0x154e, 0x3006): /* Marantz SA-14S1 */
1138 return true;
1139 }
1140 return false;
1141}
1142
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001143int snd_usb_select_mode_quirk(struct snd_usb_substream *subs,
1144 struct audioformat *fmt)
1145{
1146 struct usb_device *dev = subs->dev;
1147 int err;
1148
Takashi Iwai8b28c932015-03-04 15:37:01 +01001149 if (is_marantz_denon_dac(subs->stream->chip->usb_id)) {
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001150 /* First switch to alt set 0, otherwise the mode switch cmd
1151 * will not be accepted by the DAC
1152 */
1153 err = usb_set_interface(dev, fmt->iface, 0);
1154 if (err < 0)
1155 return err;
1156
1157 mdelay(20); /* Delay needed after setting the interface */
1158
1159 switch (fmt->altsetting) {
1160 case 2: /* DSD mode requested */
1161 case 1: /* PCM mode requested */
1162 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1163 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1164 fmt->altsetting - 1, 1, NULL, 0);
1165 if (err < 0)
1166 return err;
1167 break;
1168 }
1169 mdelay(20);
1170 }
1171 return 0;
1172}
1173
Daniel Mack2b58fd52012-09-04 10:23:07 +02001174void snd_usb_endpoint_start_quirk(struct snd_usb_endpoint *ep)
1175{
1176 /*
1177 * "Playback Design" products send bogus feedback data at the start
1178 * of the stream. Ignore them.
1179 */
1180 if ((le16_to_cpu(ep->chip->dev->descriptor.idVendor) == 0x23ba) &&
1181 ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
1182 ep->skip_packets = 4;
Eldad Zack83e3acd2013-01-13 23:02:03 +01001183
1184 /*
Matt Gruskine9a25e02013-02-09 12:56:35 -05001185 * M-Audio Fast Track C400/C600 - when packets are not skipped, real
1186 * world latency varies by approx. +/- 50 frames (at 96KHz) each time
1187 * the stream is (re)started. When skipping packets 16 at endpoint
1188 * start up, the real world latency is stable within +/- 1 frame (also
Eldad Zack83e3acd2013-01-13 23:02:03 +01001189 * across power cycles).
1190 */
Matt Gruskine9a25e02013-02-09 12:56:35 -05001191 if ((ep->chip->usb_id == USB_ID(0x0763, 0x2030) ||
1192 ep->chip->usb_id == USB_ID(0x0763, 0x2031)) &&
Eldad Zack83e3acd2013-01-13 23:02:03 +01001193 ep->type == SND_USB_ENDPOINT_TYPE_DATA)
1194 ep->skip_packets = 16;
Daniel Mack2b58fd52012-09-04 10:23:07 +02001195}
1196
Daniel Mack21bb5aa2013-04-10 00:56:03 +08001197void snd_usb_set_interface_quirk(struct usb_device *dev)
1198{
1199 /*
1200 * "Playback Design" products need a 50ms delay after setting the
1201 * USB interface.
1202 */
1203 if (le16_to_cpu(dev->descriptor.idVendor) == 0x23ba)
1204 mdelay(50);
1205}
1206
Daniel Mack2b58fd52012-09-04 10:23:07 +02001207void snd_usb_ctl_msg_quirk(struct usb_device *dev, unsigned int pipe,
1208 __u8 request, __u8 requesttype, __u16 value,
1209 __u16 index, void *data, __u16 size)
1210{
1211 /*
1212 * "Playback Design" products need a 20ms delay after each
1213 * class compliant request
1214 */
1215 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x23ba) &&
1216 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1217 mdelay(20);
Jurgen Kramer6e84a8d2014-11-15 14:01:21 +01001218
1219 /* Marantz/Denon devices with USB DAC functionality need a delay
1220 * after each class compliant request
1221 */
Takashi Iwai8b28c932015-03-04 15:37:01 +01001222 if (is_marantz_denon_dac(USB_ID(le16_to_cpu(dev->descriptor.idVendor),
1223 le16_to_cpu(dev->descriptor.idProduct)))
1224 && (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1225 mdelay(20);
Panu Matilainendacacb02014-11-30 18:45:40 +02001226
1227 /* Zoom R16/24 needs a tiny delay here, otherwise requests like
1228 * get/set frequency return as failed despite actually succeeding.
1229 */
1230 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x1686) &&
1231 (le16_to_cpu(dev->descriptor.idProduct) == 0x00dd) &&
1232 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1233 mdelay(1);
Daniel Mack2b58fd52012-09-04 10:23:07 +02001234}
1235
Daniel Mack126825e2013-04-17 00:01:40 +08001236/*
1237 * snd_usb_interface_dsd_format_quirks() is called from format.c to
1238 * augment the PCM format bit-field for DSD types. The UAC standards
1239 * don't have a designated bit field to denote DSD-capable interfaces,
1240 * hence all hardware that is known to support this format has to be
1241 * listed here.
1242 */
1243u64 snd_usb_interface_dsd_format_quirks(struct snd_usb_audio *chip,
1244 struct audioformat *fp,
1245 unsigned int sample_bytes)
1246{
1247 /* Playback Designs */
1248 if (le16_to_cpu(chip->dev->descriptor.idVendor) == 0x23ba) {
1249 switch (fp->altsetting) {
1250 case 1:
1251 fp->dsd_dop = true;
1252 return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1253 case 2:
1254 fp->dsd_bitrev = true;
1255 return SNDRV_PCM_FMTBIT_DSD_U8;
1256 case 3:
1257 fp->dsd_bitrev = true;
1258 return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1259 }
1260 }
1261
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001262 /* XMOS based USB DACs */
1263 switch (chip->usb_id) {
Jurgen Kramer38f74d52014-12-17 17:45:20 +01001264 case USB_ID(0x20b1, 0x3008): /* iFi Audio micro/nano iDSD */
1265 case USB_ID(0x20b1, 0x2008): /* Matrix Audio X-Sabre */
1266 case USB_ID(0x20b1, 0x300a): /* Matrix Audio Mini-i Pro */
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001267 if (fp->altsetting == 2)
Jussi Laakod42472e2014-11-21 16:04:46 +02001268 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001269 break;
Jurgen Kramer3b7e5c72015-06-05 09:42:49 +02001270
1271 case USB_ID(0x20b1, 0x2009): /* DIYINHK DSD DXD 384kHz USB to I2S/DSD */
1272 case USB_ID(0x20b1, 0x2023): /* JLsounds I2SoverUSB */
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001273 if (fp->altsetting == 3)
Jussi Laakod42472e2014-11-21 16:04:46 +02001274 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001275 break;
1276 default:
1277 break;
1278 }
1279
Jurgen Kramer7a2e9dd2014-11-28 17:32:53 +01001280 /* Denon/Marantz devices with USB DAC functionality */
Takashi Iwai8b28c932015-03-04 15:37:01 +01001281 if (is_marantz_denon_dac(chip->usb_id)) {
Jurgen Kramer7a2e9dd2014-11-28 17:32:53 +01001282 if (fp->altsetting == 2)
1283 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
Jurgen Kramer7a2e9dd2014-11-28 17:32:53 +01001284 }
1285
Daniel Mack126825e2013-04-17 00:01:40 +08001286 return 0;
1287}