blob: 5fb308d39e2a1aa0f2d05bbbd0b314a6852af9a5 [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);
Takashi Iwai6ed72ce2016-03-15 12:09:10 +0100180 if (altsd->bNumEndpoints < 1) {
181 kfree(fp);
182 kfree(rate_table);
183 return -EINVAL;
184 }
185
Eldad Zack42d4ab82013-07-09 20:36:15 +0200186 fp->protocol = altsd->bInterfaceProtocol;
187
Mark Hills59ea5862013-03-17 11:07:54 +0000188 if (fp->datainterval == 0)
189 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
190 if (fp->maxpacksize == 0)
191 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Daniel Macke5779992010-03-04 19:46:13 +0100192 usb_set_interface(chip->dev, fp->iface, 0);
Daniel Mack767d75a2010-03-04 19:46:17 +0100193 snd_usb_init_pitch(chip, fp->iface, alts, fp);
194 snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
Daniel Macke5779992010-03-04 19:46:13 +0100195 return 0;
196}
197
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200198static int create_auto_pcm_quirk(struct snd_usb_audio *chip,
199 struct usb_interface *iface,
200 struct usb_driver *driver)
201{
202 struct usb_host_interface *alts;
203 struct usb_interface_descriptor *altsd;
204 struct usb_endpoint_descriptor *epd;
205 struct uac1_as_header_descriptor *ashd;
206 struct uac_format_type_i_discrete_descriptor *fmtd;
207
208 /*
209 * Most Roland/Yamaha audio streaming interfaces have more or less
210 * standard descriptors, but older devices might lack descriptors, and
211 * future ones might change, so ensure that we fail silently if the
212 * interface doesn't look exactly right.
213 */
214
215 /* must have a non-zero altsetting for streaming */
216 if (iface->num_altsetting < 2)
217 return -ENODEV;
218 alts = &iface->altsetting[1];
219 altsd = get_iface_desc(alts);
220
221 /* must have an isochronous endpoint for streaming */
222 if (altsd->bNumEndpoints < 1)
223 return -ENODEV;
224 epd = get_endpoint(alts, 0);
225 if (!usb_endpoint_xfer_isoc(epd))
226 return -ENODEV;
227
228 /* must have format descriptors */
229 ashd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
230 UAC_AS_GENERAL);
231 fmtd = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
232 UAC_FORMAT_TYPE);
233 if (!ashd || ashd->bLength < 7 ||
234 !fmtd || fmtd->bLength < 8)
235 return -ENODEV;
236
237 return create_standard_audio_quirk(chip, iface, driver, NULL);
238}
239
240static int create_yamaha_midi_quirk(struct snd_usb_audio *chip,
241 struct usb_interface *iface,
242 struct usb_driver *driver,
243 struct usb_host_interface *alts)
244{
245 static const struct snd_usb_audio_quirk yamaha_midi_quirk = {
246 .type = QUIRK_MIDI_YAMAHA
247 };
248 struct usb_midi_in_jack_descriptor *injd;
249 struct usb_midi_out_jack_descriptor *outjd;
250
251 /* must have some valid jack descriptors */
252 injd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
253 NULL, USB_MS_MIDI_IN_JACK);
254 outjd = snd_usb_find_csint_desc(alts->extra, alts->extralen,
255 NULL, USB_MS_MIDI_OUT_JACK);
256 if (!injd && !outjd)
257 return -ENODEV;
258 if (injd && (injd->bLength < 5 ||
259 (injd->bJackType != USB_MS_EMBEDDED &&
260 injd->bJackType != USB_MS_EXTERNAL)))
261 return -ENODEV;
262 if (outjd && (outjd->bLength < 6 ||
263 (outjd->bJackType != USB_MS_EMBEDDED &&
264 outjd->bJackType != USB_MS_EXTERNAL)))
265 return -ENODEV;
266 return create_any_midi_quirk(chip, iface, driver, &yamaha_midi_quirk);
267}
268
269static int create_roland_midi_quirk(struct snd_usb_audio *chip,
270 struct usb_interface *iface,
271 struct usb_driver *driver,
272 struct usb_host_interface *alts)
273{
274 static const struct snd_usb_audio_quirk roland_midi_quirk = {
275 .type = QUIRK_MIDI_ROLAND
276 };
277 u8 *roland_desc = NULL;
278
279 /* might have a vendor-specific descriptor <06 24 F1 02 ...> */
280 for (;;) {
281 roland_desc = snd_usb_find_csint_desc(alts->extra,
282 alts->extralen,
283 roland_desc, 0xf1);
284 if (!roland_desc)
285 return -ENODEV;
286 if (roland_desc[0] < 6 || roland_desc[3] != 2)
287 continue;
288 return create_any_midi_quirk(chip, iface, driver,
289 &roland_midi_quirk);
290 }
291}
292
293static int create_std_midi_quirk(struct snd_usb_audio *chip,
294 struct usb_interface *iface,
295 struct usb_driver *driver,
296 struct usb_host_interface *alts)
297{
298 struct usb_ms_header_descriptor *mshd;
299 struct usb_ms_endpoint_descriptor *msepd;
300
301 /* must have the MIDIStreaming interface header descriptor*/
302 mshd = (struct usb_ms_header_descriptor *)alts->extra;
303 if (alts->extralen < 7 ||
304 mshd->bLength < 7 ||
305 mshd->bDescriptorType != USB_DT_CS_INTERFACE ||
306 mshd->bDescriptorSubtype != USB_MS_HEADER)
307 return -ENODEV;
308 /* must have the MIDIStreaming endpoint descriptor*/
309 msepd = (struct usb_ms_endpoint_descriptor *)alts->endpoint[0].extra;
310 if (alts->endpoint[0].extralen < 4 ||
311 msepd->bLength < 4 ||
312 msepd->bDescriptorType != USB_DT_CS_ENDPOINT ||
313 msepd->bDescriptorSubtype != UAC_MS_GENERAL ||
314 msepd->bNumEmbMIDIJack < 1 ||
315 msepd->bNumEmbMIDIJack > 16)
316 return -ENODEV;
317
318 return create_any_midi_quirk(chip, iface, driver, NULL);
319}
320
321static int create_auto_midi_quirk(struct snd_usb_audio *chip,
322 struct usb_interface *iface,
323 struct usb_driver *driver)
324{
325 struct usb_host_interface *alts;
326 struct usb_interface_descriptor *altsd;
327 struct usb_endpoint_descriptor *epd;
328 int err;
329
330 alts = &iface->altsetting[0];
331 altsd = get_iface_desc(alts);
332
333 /* must have at least one bulk/interrupt endpoint for streaming */
334 if (altsd->bNumEndpoints < 1)
335 return -ENODEV;
336 epd = get_endpoint(alts, 0);
Clemens Ladischaa773bf2013-08-11 14:13:13 +0200337 if (!usb_endpoint_xfer_bulk(epd) &&
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200338 !usb_endpoint_xfer_int(epd))
339 return -ENODEV;
340
341 switch (USB_ID_VENDOR(chip->usb_id)) {
342 case 0x0499: /* Yamaha */
343 err = create_yamaha_midi_quirk(chip, iface, driver, alts);
Clemens Ladischaa773bf2013-08-11 14:13:13 +0200344 if (err != -ENODEV)
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200345 return err;
346 break;
347 case 0x0582: /* Roland */
348 err = create_roland_midi_quirk(chip, iface, driver, alts);
Clemens Ladischaa773bf2013-08-11 14:13:13 +0200349 if (err != -ENODEV)
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200350 return err;
351 break;
352 }
353
354 return create_std_midi_quirk(chip, iface, driver, alts);
355}
356
357static int create_autodetect_quirk(struct snd_usb_audio *chip,
358 struct usb_interface *iface,
Clemens Ladischb1ce7ba2013-04-04 21:43:57 +0200359 struct usb_driver *driver)
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200360{
361 int err;
362
363 err = create_auto_pcm_quirk(chip, iface, driver);
364 if (err == -ENODEV)
365 err = create_auto_midi_quirk(chip, iface, driver);
366 return err;
367}
368
Clemens Ladischb1ce7ba2013-04-04 21:43:57 +0200369static int create_autodetect_quirks(struct snd_usb_audio *chip,
370 struct usb_interface *iface,
371 struct usb_driver *driver,
372 const struct snd_usb_audio_quirk *quirk)
373{
374 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
375 int ifcount, ifnum, err;
376
377 err = create_autodetect_quirk(chip, iface, driver);
378 if (err < 0)
379 return err;
380
381 /*
382 * ALSA PCM playback/capture devices cannot be registered in two steps,
383 * so we have to claim the other corresponding interface here.
384 */
385 ifcount = chip->dev->actconfig->desc.bNumInterfaces;
386 for (ifnum = 0; ifnum < ifcount; ifnum++) {
387 if (ifnum == probed_ifnum || quirk->ifnum >= 0)
388 continue;
389 iface = usb_ifnum_to_if(chip->dev, ifnum);
390 if (!iface ||
391 usb_interface_claimed(iface) ||
392 get_iface_desc(iface->altsetting)->bInterfaceClass !=
393 USB_CLASS_VENDOR_SPEC)
394 continue;
395
396 err = create_autodetect_quirk(chip, iface, driver);
397 if (err >= 0)
398 usb_driver_claim_interface(driver, iface, (void *)-1L);
399 }
400
401 return 0;
402}
403
Daniel Macke5779992010-03-04 19:46:13 +0100404/*
405 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
406 * The only way to detect the sample rate is by looking at wMaxPacketSize.
407 */
408static int create_uaxx_quirk(struct snd_usb_audio *chip,
409 struct usb_interface *iface,
410 struct usb_driver *driver,
411 const struct snd_usb_audio_quirk *quirk)
412{
413 static const struct audioformat ua_format = {
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100414 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
Daniel Macke5779992010-03-04 19:46:13 +0100415 .channels = 2,
416 .fmt_type = UAC_FORMAT_TYPE_I,
417 .altsetting = 1,
418 .altset_idx = 1,
419 .rates = SNDRV_PCM_RATE_CONTINUOUS,
420 };
421 struct usb_host_interface *alts;
422 struct usb_interface_descriptor *altsd;
423 struct audioformat *fp;
424 int stream, err;
425
426 /* both PCM and MIDI interfaces have 2 or more altsettings */
427 if (iface->num_altsetting < 2)
428 return -ENXIO;
429 alts = &iface->altsetting[1];
430 altsd = get_iface_desc(alts);
431
432 if (altsd->bNumEndpoints == 2) {
433 static const struct snd_usb_midi_endpoint_info ua700_ep = {
434 .out_cables = 0x0003,
435 .in_cables = 0x0003
436 };
437 static const struct snd_usb_audio_quirk ua700_quirk = {
438 .type = QUIRK_MIDI_FIXED_ENDPOINT,
439 .data = &ua700_ep
440 };
441 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
442 .out_cables = 0x0001,
443 .in_cables = 0x0001
444 };
445 static const struct snd_usb_audio_quirk uaxx_quirk = {
446 .type = QUIRK_MIDI_FIXED_ENDPOINT,
447 .data = &uaxx_ep
448 };
449 const struct snd_usb_audio_quirk *quirk =
450 chip->usb_id == USB_ID(0x0582, 0x002b)
451 ? &ua700_quirk : &uaxx_quirk;
452 return snd_usbmidi_create(chip->card, iface,
453 &chip->midi_list, quirk);
454 }
455
456 if (altsd->bNumEndpoints != 1)
457 return -ENXIO;
458
Thomas Meyer43df2a52011-11-10 19:38:43 +0100459 fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
Daniel Macke5779992010-03-04 19:46:13 +0100460 if (!fp)
461 return -ENOMEM;
Daniel Macke5779992010-03-04 19:46:13 +0100462
463 fp->iface = altsd->bInterfaceNumber;
464 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
465 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
466 fp->datainterval = 0;
467 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
468
469 switch (fp->maxpacksize) {
470 case 0x120:
471 fp->rate_max = fp->rate_min = 44100;
472 break;
473 case 0x138:
474 case 0x140:
475 fp->rate_max = fp->rate_min = 48000;
476 break;
477 case 0x258:
478 case 0x260:
479 fp->rate_max = fp->rate_min = 96000;
480 break;
481 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100482 usb_audio_err(chip, "unknown sample rate\n");
Daniel Macke5779992010-03-04 19:46:13 +0100483 kfree(fp);
484 return -ENXIO;
485 }
486
487 stream = (fp->endpoint & USB_DIR_IN)
488 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200489 err = snd_usb_add_audio_stream(chip, stream, fp);
Daniel Macke5779992010-03-04 19:46:13 +0100490 if (err < 0) {
491 kfree(fp);
492 return err;
493 }
494 usb_set_interface(chip->dev, fp->iface, 0);
495 return 0;
496}
497
498/*
Daniel Mack014950b2011-05-25 09:09:02 +0200499 * Create a standard mixer for the specified interface.
500 */
501static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
502 struct usb_interface *iface,
503 struct usb_driver *driver,
504 const struct snd_usb_audio_quirk *quirk)
505{
506 if (quirk->ifnum < 0)
507 return 0;
508
509 return snd_usb_create_mixer(chip, quirk->ifnum, 0);
510}
511
512/*
Daniel Macke5779992010-03-04 19:46:13 +0100513 * audio-interface quirks
514 *
515 * returns zero if no standard audio/MIDI parsing is needed.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300516 * returns a positive value if standard audio/midi interfaces are parsed
Daniel Macke5779992010-03-04 19:46:13 +0100517 * after this.
518 * returns a negative value at error.
519 */
520int snd_usb_create_quirk(struct snd_usb_audio *chip,
521 struct usb_interface *iface,
522 struct usb_driver *driver,
523 const struct snd_usb_audio_quirk *quirk)
524{
525 typedef int (*quirk_func_t)(struct snd_usb_audio *,
526 struct usb_interface *,
527 struct usb_driver *,
528 const struct snd_usb_audio_quirk *);
529 static const quirk_func_t quirk_funcs[] = {
530 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
531 [QUIRK_COMPOSITE] = create_composite_quirk,
Clemens Ladischb1ce7ba2013-04-04 21:43:57 +0200532 [QUIRK_AUTODETECT] = create_autodetect_quirks,
Daniel Macke5779992010-03-04 19:46:13 +0100533 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
534 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
535 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200536 [QUIRK_MIDI_ROLAND] = create_any_midi_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100537 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
538 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
Clemens Ladischc7f57212010-10-22 18:20:48 +0200539 [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100540 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
541 [QUIRK_MIDI_CME] = create_any_midi_quirk,
Krzysztof Foltman4434ade2010-05-20 20:31:10 +0100542 [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
Kristian Amlie1ef0e0a2011-08-26 13:19:49 +0200543 [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
Clemens Ladisch96c93462015-11-15 22:38:29 +0100544 [QUIRK_MIDI_CH345] = create_any_midi_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100545 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
546 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
547 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
Daniel Mack014950b2011-05-25 09:09:02 +0200548 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
549 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100550 };
551
552 if (quirk->type < QUIRK_TYPE_COUNT) {
553 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
554 } else {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100555 usb_audio_err(chip, "invalid quirk type %d\n", quirk->type);
Daniel Macke5779992010-03-04 19:46:13 +0100556 return -ENXIO;
557 }
558}
559
560/*
561 * boot quirks
562 */
563
564#define EXTIGY_FIRMWARE_SIZE_OLD 794
565#define EXTIGY_FIRMWARE_SIZE_NEW 483
566
567static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
568{
569 struct usb_host_config *config = dev->actconfig;
570 int err;
571
572 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
573 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100574 dev_dbg(&dev->dev, "sending Extigy boot sequence...\n");
Daniel Macke5779992010-03-04 19:46:13 +0100575 /* Send message to force it to reconnect with full interface. */
576 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200577 0x10, 0x43, 0x0001, 0x000a, NULL, 0);
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100578 if (err < 0)
579 dev_dbg(&dev->dev, "error sending boot message: %d\n", err);
Daniel Macke5779992010-03-04 19:46:13 +0100580 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
581 &dev->descriptor, sizeof(dev->descriptor));
582 config = dev->actconfig;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100583 if (err < 0)
584 dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
Daniel Macke5779992010-03-04 19:46:13 +0100585 err = usb_reset_configuration(dev);
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100586 if (err < 0)
587 dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
588 dev_dbg(&dev->dev, "extigy_boot: new boot length = %d\n",
Daniel Macke5779992010-03-04 19:46:13 +0100589 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
590 return -ENODEV; /* quit this anyway */
591 }
592 return 0;
593}
594
595static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
596{
597 u8 buf = 1;
598
599 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
600 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200601 0, 0, &buf, 1);
Daniel Macke5779992010-03-04 19:46:13 +0100602 if (buf == 0) {
603 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
604 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200605 1, 2000, NULL, 0);
Daniel Macke5779992010-03-04 19:46:13 +0100606 return -ENODEV;
607 }
608 return 0;
609}
610
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200611static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
612{
613 int err;
614
615 if (dev->actconfig->desc.bConfigurationValue == 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100616 dev_info(&dev->dev,
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200617 "Fast Track Pro switching to config #2\n");
618 /* This function has to be available by the usb core module.
619 * if it is not avialable the boot quirk has to be left out
620 * and the configuration has to be set by udev or hotplug
621 * rules
622 */
623 err = usb_driver_set_configuration(dev, 2);
David Henningssonb98ae272013-01-04 17:02:18 +0100624 if (err < 0)
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100625 dev_dbg(&dev->dev,
626 "error usb_driver_set_configuration: %d\n",
627 err);
David Henningssonb98ae272013-01-04 17:02:18 +0100628 /* Always return an error, so that we stop creating a device
629 that will just be destroyed and recreated with a new
630 configuration */
631 return -ENODEV;
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200632 } else
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100633 dev_info(&dev->dev, "Fast Track Pro config OK\n");
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200634
635 return 0;
636}
637
Daniel Macke5779992010-03-04 19:46:13 +0100638/*
639 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
640 * documented in the device's data sheet.
641 */
642static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
643{
644 u8 buf[4];
645 buf[0] = 0x20;
646 buf[1] = value & 0xff;
647 buf[2] = (value >> 8) & 0xff;
648 buf[3] = reg;
649 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
650 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200651 0, 0, &buf, 4);
Daniel Macke5779992010-03-04 19:46:13 +0100652}
653
654static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
655{
656 /*
657 * Enable line-out driver mode, set headphone source to front
658 * channels, enable stereo mic.
659 */
660 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
661}
662
663/*
664 * C-Media CM6206 is based on CM106 with two additional
665 * registers that are not documented in the data sheet.
666 * Values here are chosen based on sniffing USB traffic
667 * under Windows.
668 */
669static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
670{
Daniel Mackdac8f842011-08-06 00:23:18 +0200671 int err = 0, reg;
Eric Lammerts157186b2011-05-27 18:16:52 -0400672 int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
Daniel Macke5779992010-03-04 19:46:13 +0100673
674 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
675 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
676 if (err < 0)
677 return err;
678 }
679
680 return err;
681}
682
Takashi Iwai19570d72013-12-19 14:32:53 +0100683/* quirk for Plantronics GameCom 780 with CM6302 chip */
684static int snd_usb_gamecon780_boot_quirk(struct usb_device *dev)
685{
686 /* set the initial volume and don't change; other values are either
687 * too loud or silent due to firmware bug (bko#65251)
688 */
Paul S McSpadden542baf92014-08-03 17:47:36 -0500689 u8 buf[2] = { 0x74, 0xe3 };
Takashi Iwai19570d72013-12-19 14:32:53 +0100690 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
691 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
692 UAC_FU_VOLUME << 8, 9 << 8, buf, 2);
693}
694
Daniel Macke5779992010-03-04 19:46:13 +0100695/*
Mark Hills5e212332013-03-17 11:07:53 +0000696 * Novation Twitch DJ controller
Eduard Gilmutdinov11e424e2013-12-20 14:06:58 +0600697 * Focusrite Novation Saffire 6 USB audio card
Mark Hills5e212332013-03-17 11:07:53 +0000698 */
Eduard Gilmutdinov11e424e2013-12-20 14:06:58 +0600699static int snd_usb_novation_boot_quirk(struct usb_device *dev)
Mark Hills5e212332013-03-17 11:07:53 +0000700{
701 /* preemptively set up the device because otherwise the
702 * raw MIDI endpoints are not active */
703 usb_set_interface(dev, 0, 1);
704 return 0;
705}
706
707/*
Daniel Macke5779992010-03-04 19:46:13 +0100708 * This call will put the synth in "USB send" mode, i.e it will send MIDI
709 * messages through USB (this is disabled at startup). The synth will
710 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
711 * sign on its LCD. Values here are chosen based on sniffing USB traffic
712 * under Windows.
713 */
714static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
715{
716 int err, actual_length;
717
718 /* "midi send" enable */
719 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
720
721 void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
722 if (!buf)
723 return -ENOMEM;
724 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
725 ARRAY_SIZE(seq), &actual_length, 1000);
726 kfree(buf);
727 if (err < 0)
728 return err;
729
730 return 0;
731}
732
733/*
Daniel Mack54a8c502011-02-11 11:08:06 +0000734 * Some sound cards from Native Instruments are in fact compliant to the USB
735 * audio standard of version 2 and other approved USB standards, even though
736 * they come up as vendor-specific device when first connected.
737 *
738 * However, they can be told to come up with a new set of descriptors
739 * upon their next enumeration, and the interfaces announced by the new
740 * descriptors will then be handled by the kernel's class drivers. As the
741 * product ID will also change, no further checks are required.
742 */
743
744static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
745{
746 int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
747 0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Eldad Zack889d6682013-04-05 20:49:46 +0200748 1, 0, NULL, 0, 1000);
Daniel Mack54a8c502011-02-11 11:08:06 +0000749
750 if (ret < 0)
751 return ret;
752
753 usb_reset_device(dev);
754
755 /* return -EAGAIN, so the creation of an audio interface for this
756 * temporary device is aborted. The device will reconnect with a
757 * new product ID */
758 return -EAGAIN;
759}
760
Damien Zammitcb998642012-12-19 11:27:22 +0100761static void mbox2_setup_48_24_magic(struct usb_device *dev)
762{
763 u8 srate[3];
764 u8 temp[12];
765
766 /* Choose 48000Hz permanently */
767 srate[0] = 0x80;
768 srate[1] = 0xbb;
769 srate[2] = 0x00;
770
771 /* Send the magic! */
772 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
773 0x01, 0x22, 0x0100, 0x0085, &temp, 0x0003);
774 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
775 0x81, 0xa2, 0x0100, 0x0085, &srate, 0x0003);
776 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
777 0x81, 0xa2, 0x0100, 0x0086, &srate, 0x0003);
778 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
779 0x81, 0xa2, 0x0100, 0x0003, &srate, 0x0003);
780 return;
781}
782
783/* Digidesign Mbox 2 needs to load firmware onboard
784 * and driver must wait a few seconds for initialisation.
785 */
786
787#define MBOX2_FIRMWARE_SIZE 646
788#define MBOX2_BOOT_LOADING 0x01 /* Hard coded into the device */
789#define MBOX2_BOOT_READY 0x02 /* Hard coded into the device */
790
Damien Zammitb7b435e2013-01-04 09:51:44 +0100791static int snd_usb_mbox2_boot_quirk(struct usb_device *dev)
Damien Zammitcb998642012-12-19 11:27:22 +0100792{
793 struct usb_host_config *config = dev->actconfig;
794 int err;
Jiri Slaby4909a0c2013-02-17 14:33:04 +0100795 u8 bootresponse[0x12];
Damien Zammitcb998642012-12-19 11:27:22 +0100796 int fwsize;
797 int count;
798
799 fwsize = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
800
801 if (fwsize != MBOX2_FIRMWARE_SIZE) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100802 dev_err(&dev->dev, "Invalid firmware size=%d.\n", fwsize);
Damien Zammitcb998642012-12-19 11:27:22 +0100803 return -ENODEV;
804 }
805
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100806 dev_dbg(&dev->dev, "Sending Digidesign Mbox 2 boot sequence...\n");
Damien Zammitcb998642012-12-19 11:27:22 +0100807
808 count = 0;
Damien Zammitb7b435e2013-01-04 09:51:44 +0100809 bootresponse[0] = MBOX2_BOOT_LOADING;
810 while ((bootresponse[0] == MBOX2_BOOT_LOADING) && (count < 10)) {
Damien Zammitcb998642012-12-19 11:27:22 +0100811 msleep(500); /* 0.5 second delay */
812 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
813 /* Control magic - load onboard firmware */
814 0x85, 0xc0, 0x0001, 0x0000, &bootresponse, 0x0012);
Damien Zammitb7b435e2013-01-04 09:51:44 +0100815 if (bootresponse[0] == MBOX2_BOOT_READY)
Damien Zammitcb998642012-12-19 11:27:22 +0100816 break;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100817 dev_dbg(&dev->dev, "device not ready, resending boot sequence...\n");
Damien Zammitcb998642012-12-19 11:27:22 +0100818 count++;
819 }
820
Damien Zammitb7b435e2013-01-04 09:51:44 +0100821 if (bootresponse[0] != MBOX2_BOOT_READY) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100822 dev_err(&dev->dev, "Unknown bootresponse=%d, or timed out, ignoring device.\n", bootresponse[0]);
Damien Zammitcb998642012-12-19 11:27:22 +0100823 return -ENODEV;
824 }
825
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100826 dev_dbg(&dev->dev, "device initialised!\n");
Damien Zammitcb998642012-12-19 11:27:22 +0100827
828 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
829 &dev->descriptor, sizeof(dev->descriptor));
830 config = dev->actconfig;
831 if (err < 0)
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100832 dev_dbg(&dev->dev, "error usb_get_descriptor: %d\n", err);
Damien Zammitcb998642012-12-19 11:27:22 +0100833
834 err = usb_reset_configuration(dev);
835 if (err < 0)
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100836 dev_dbg(&dev->dev, "error usb_reset_configuration: %d\n", err);
837 dev_dbg(&dev->dev, "mbox2_boot: new boot length = %d\n",
Damien Zammitcb998642012-12-19 11:27:22 +0100838 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
839
840 mbox2_setup_48_24_magic(dev);
841
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100842 dev_info(&dev->dev, "Digidesign Mbox 2: 24bit 48kHz");
Damien Zammitcb998642012-12-19 11:27:22 +0100843
844 return 0; /* Successful boot */
845}
846
Daniel Mack54a8c502011-02-11 11:08:06 +0000847/*
Daniel Macke5779992010-03-04 19:46:13 +0100848 * Setup quirks
849 */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200850#define MAUDIO_SET 0x01 /* parse device_setup */
851#define MAUDIO_SET_COMPATIBLE 0x80 /* use only "win-compatible" interfaces */
852#define MAUDIO_SET_DTS 0x02 /* enable DTS Digital Output */
853#define MAUDIO_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
854#define MAUDIO_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
855#define MAUDIO_SET_DI 0x10 /* enable Digital Input */
856#define MAUDIO_SET_MASK 0x1f /* bit mask for setup value */
857#define MAUDIO_SET_24B_48K_DI 0x19 /* 24bits+48KHz+Digital Input */
858#define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
859#define MAUDIO_SET_16B_48K_DI 0x11 /* 16bits+48KHz+Digital Input */
860#define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
861
862static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
863 int iface, int altno)
864{
865 /* Reset ALL ifaces to 0 altsetting.
866 * Call it for every possible altsetting of every interface.
867 */
868 usb_set_interface(chip->dev, iface, 0);
869 if (chip->setup & MAUDIO_SET) {
870 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
871 if (iface != 1 && iface != 2)
872 return 1; /* skip all interfaces but 1 and 2 */
873 } else {
874 unsigned int mask;
875 if (iface == 1 || iface == 2)
876 return 1; /* skip interfaces 1 and 2 */
877 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
878 return 1; /* skip this altsetting */
879 mask = chip->setup & MAUDIO_SET_MASK;
880 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
881 return 1; /* skip this altsetting */
882 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
883 return 1; /* skip this altsetting */
884 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
885 return 1; /* skip this altsetting */
886 }
887 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100888 usb_audio_dbg(chip,
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200889 "using altsetting %d for interface %d config %d\n",
890 altno, iface, chip->setup);
891 return 0; /* keep this altsetting */
892}
Daniel Macke5779992010-03-04 19:46:13 +0100893
894static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
895 int iface,
896 int altno)
897{
898 /* Reset ALL ifaces to 0 altsetting.
899 * Call it for every possible altsetting of every interface.
900 */
901 usb_set_interface(chip->dev, iface, 0);
902
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200903 if (chip->setup & MAUDIO_SET) {
904 unsigned int mask;
905 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
Daniel Macke5779992010-03-04 19:46:13 +0100906 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200907 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
Daniel Macke5779992010-03-04 19:46:13 +0100908 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200909 mask = chip->setup & MAUDIO_SET_MASK;
910 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
Daniel Macke5779992010-03-04 19:46:13 +0100911 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200912 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
Daniel Macke5779992010-03-04 19:46:13 +0100913 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200914 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
Daniel Macke5779992010-03-04 19:46:13 +0100915 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200916 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
Daniel Macke5779992010-03-04 19:46:13 +0100917 return 1; /* skip this altsetting */
918 }
919
920 return 0; /* keep this altsetting */
921}
922
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200923static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
924 int iface, int altno)
925{
926 /* Reset ALL ifaces to 0 altsetting.
927 * Call it for every possible altsetting of every interface.
928 */
929 usb_set_interface(chip->dev, iface, 0);
930
931 /* possible configuration where both inputs and only one output is
932 *used is not supported by the current setup
933 */
934 if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
935 if (chip->setup & MAUDIO_SET_96K) {
936 if (altno != 3 && altno != 6)
937 return 1;
938 } else if (chip->setup & MAUDIO_SET_DI) {
939 if (iface == 4)
940 return 1; /* no analog input */
941 if (altno != 2 && altno != 5)
942 return 1; /* enable only altsets 2 and 5 */
943 } else {
944 if (iface == 5)
945 return 1; /* disable digialt input */
946 if (altno != 2 && altno != 5)
947 return 1; /* enalbe only altsets 2 and 5 */
948 }
949 } else {
950 /* keep only 16-Bit mode */
951 if (altno != 1)
952 return 1;
953 }
954
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100955 usb_audio_dbg(chip,
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200956 "using altsetting %d for interface %d config %d\n",
957 altno, iface, chip->setup);
958 return 0; /* keep this altsetting */
959}
960
Daniel Macke5779992010-03-04 19:46:13 +0100961int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
962 int iface,
963 int altno)
964{
965 /* audiophile usb: skip altsets incompatible with device_setup */
966 if (chip->usb_id == USB_ID(0x0763, 0x2003))
967 return audiophile_skip_setting_quirk(chip, iface, altno);
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200968 /* quattro usb: skip altsets incompatible with device_setup */
969 if (chip->usb_id == USB_ID(0x0763, 0x2001))
970 return quattro_skip_setting_quirk(chip, iface, altno);
971 /* fasttrackpro usb: skip altsets incompatible with device_setup */
972 if (chip->usb_id == USB_ID(0x0763, 0x2012))
973 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
Daniel Macke5779992010-03-04 19:46:13 +0100974
975 return 0;
976}
977
978int snd_usb_apply_boot_quirk(struct usb_device *dev,
979 struct usb_interface *intf,
980 const struct snd_usb_audio_quirk *quirk)
981{
982 u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
983 le16_to_cpu(dev->descriptor.idProduct));
984
Daniel Mack3347b262011-02-11 11:34:12 +0000985 switch (id) {
986 case USB_ID(0x041e, 0x3000):
987 /* SB Extigy needs special boot-up sequence */
988 /* if more models come, this will go to the quirk list. */
Daniel Macke5779992010-03-04 19:46:13 +0100989 return snd_usb_extigy_boot_quirk(dev, intf);
990
Daniel Mack3347b262011-02-11 11:34:12 +0000991 case USB_ID(0x041e, 0x3020):
992 /* SB Audigy 2 NX needs its own boot-up magic, too */
Daniel Macke5779992010-03-04 19:46:13 +0100993 return snd_usb_audigy2nx_boot_quirk(dev);
994
Daniel Mack3347b262011-02-11 11:34:12 +0000995 case USB_ID(0x10f5, 0x0200):
996 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
Daniel Macke5779992010-03-04 19:46:13 +0100997 return snd_usb_cm106_boot_quirk(dev);
998
Daniel Mack3347b262011-02-11 11:34:12 +0000999 case USB_ID(0x0d8c, 0x0102):
1000 /* C-Media CM6206 / CM106-Like Sound Device */
Wolfgang Breyha8129e792011-04-28 16:18:40 +02001001 case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
Daniel Macke5779992010-03-04 19:46:13 +01001002 return snd_usb_cm6206_boot_quirk(dev);
1003
Damien Zammitcb998642012-12-19 11:27:22 +01001004 case USB_ID(0x0dba, 0x3000):
1005 /* Digidesign Mbox 2 */
1006 return snd_usb_mbox2_boot_quirk(dev);
1007
Eduard Gilmutdinov11e424e2013-12-20 14:06:58 +06001008 case USB_ID(0x1235, 0x0010): /* Focusrite Novation Saffire 6 USB */
1009 case USB_ID(0x1235, 0x0018): /* Focusrite Novation Twitch */
1010 return snd_usb_novation_boot_quirk(dev);
Mark Hills5e212332013-03-17 11:07:53 +00001011
Daniel Mack3347b262011-02-11 11:34:12 +00001012 case USB_ID(0x133e, 0x0815):
1013 /* Access Music VirusTI Desktop */
Daniel Macke5779992010-03-04 19:46:13 +01001014 return snd_usb_accessmusic_boot_quirk(dev);
1015
Daniel Mack759e890f2011-05-18 11:28:41 +02001016 case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
Daniel Mack3347b262011-02-11 11:34:12 +00001017 case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
1018 case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
Daniel Mack54a8c502011-02-11 11:08:06 +00001019 return snd_usb_nativeinstruments_boot_quirk(dev);
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001020 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
1021 return snd_usb_fasttrackpro_boot_quirk(dev);
Takashi Iwai19570d72013-12-19 14:32:53 +01001022 case USB_ID(0x047f, 0xc010): /* Plantronics Gamecom 780 */
1023 return snd_usb_gamecon780_boot_quirk(dev);
Daniel Mack3347b262011-02-11 11:34:12 +00001024 }
Daniel Mack54a8c502011-02-11 11:08:06 +00001025
Daniel Macke5779992010-03-04 19:46:13 +01001026 return 0;
1027}
1028
1029/*
1030 * check if the device uses big-endian samples
1031 */
1032int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
1033{
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04001034 /* it depends on altsetting whether the device is big-endian or not */
Daniel Macke5779992010-03-04 19:46:13 +01001035 switch (chip->usb_id) {
1036 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001037 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1038 fp->altsetting == 5 || fp->altsetting == 6)
Daniel Macke5779992010-03-04 19:46:13 +01001039 return 1;
1040 break;
1041 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
1042 if (chip->setup == 0x00 ||
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001043 fp->altsetting == 1 || fp->altsetting == 2 ||
1044 fp->altsetting == 3)
Daniel Macke5779992010-03-04 19:46:13 +01001045 return 1;
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +02001046 break;
1047 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
1048 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1049 fp->altsetting == 5 || fp->altsetting == 6)
1050 return 1;
1051 break;
Daniel Macke5779992010-03-04 19:46:13 +01001052 }
1053 return 0;
1054}
1055
1056/*
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -05001057 * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
Daniel Macke5779992010-03-04 19:46:13 +01001058 * not for interface.
1059 */
1060
1061enum {
1062 EMU_QUIRK_SR_44100HZ = 0,
1063 EMU_QUIRK_SR_48000HZ,
1064 EMU_QUIRK_SR_88200HZ,
1065 EMU_QUIRK_SR_96000HZ,
1066 EMU_QUIRK_SR_176400HZ,
1067 EMU_QUIRK_SR_192000HZ
1068};
1069
1070static void set_format_emu_quirk(struct snd_usb_substream *subs,
1071 struct audioformat *fmt)
1072{
1073 unsigned char emu_samplerate_id = 0;
1074
1075 /* When capture is active
1076 * sample rate shouldn't be changed
1077 * by playback substream
1078 */
1079 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1080 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
1081 return;
1082 }
1083
1084 switch (fmt->rate_min) {
1085 case 48000:
1086 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
1087 break;
1088 case 88200:
1089 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
1090 break;
1091 case 96000:
1092 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
1093 break;
1094 case 176400:
1095 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
1096 break;
1097 case 192000:
1098 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
1099 break;
1100 default:
1101 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
1102 break;
1103 }
1104 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
Calvin Owens1539d4f2013-04-12 22:33:59 -05001105 subs->pkt_offset_adj = (emu_samplerate_id >= EMU_QUIRK_SR_176400HZ) ? 4 : 0;
Daniel Macke5779992010-03-04 19:46:13 +01001106}
1107
1108void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
1109 struct audioformat *fmt)
1110{
1111 switch (subs->stream->chip->usb_id) {
1112 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
1113 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
1114 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -05001115 case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
Daniel Macke5779992010-03-04 19:46:13 +01001116 set_format_emu_quirk(subs, fmt);
1117 break;
1118 }
1119}
1120
Joe Turnerb62b9982015-02-16 20:44:33 +00001121bool snd_usb_get_sample_rate_quirk(struct snd_usb_audio *chip)
1122{
Eric Wong9fc88ad2015-03-31 07:34:05 +00001123 /* devices which do not support reading the sample rate. */
1124 switch (chip->usb_id) {
Adam Honseeef03422015-04-12 01:03:07 -05001125 case USB_ID(0x045E, 0x075D): /* MS Lifecam Cinema */
Eric Wong9fc88ad2015-03-31 07:34:05 +00001126 case USB_ID(0x045E, 0x076D): /* MS Lifecam HD-5000 */
Victor Clément35fe4a12016-03-19 13:17:42 +01001127 case USB_ID(0x045E, 0x076E): /* MS Lifecam HD-5001 */
Lev Lybincaec5c92016-01-29 22:55:11 +07001128 case USB_ID(0x045E, 0x076F): /* MS Lifecam HD-6000 */
Takashi Iwaifa94b0d2015-05-19 10:46:49 +02001129 case USB_ID(0x045E, 0x0772): /* MS Lifecam Studio */
Vittorio G (VittGam)ae425bb2015-05-22 21:15:19 +02001130 case USB_ID(0x045E, 0x0779): /* MS Lifecam HD-3000 */
Dennis Kadioglu74a8e4d2016-03-01 14:23:29 +01001131 case USB_ID(0x047F, 0xAA05): /* Plantronics DA45 */
Eric Wong9fc88ad2015-03-31 07:34:05 +00001132 case USB_ID(0x04D8, 0xFEEA): /* Benchmark DAC1 Pre */
Eric Wong2f80b292015-05-30 09:15:39 +00001133 case USB_ID(0x074D, 0x3553): /* Outlaw RR2150 (Micronas UAC3553B) */
Anssi Hannula73a5fda2015-12-13 20:49:59 +02001134 case USB_ID(0x21B4, 0x0081): /* AudioQuest DragonFly */
Eric Wong9fc88ad2015-03-31 07:34:05 +00001135 return true;
1136 }
1137 return false;
Joe Turnerb62b9982015-02-16 20:44:33 +00001138}
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001139
1140/* Marantz/Denon USB DACs need a vendor cmd to switch
1141 * between PCM and native DSD mode
1142 */
Takashi Iwai8b28c932015-03-04 15:37:01 +01001143static bool is_marantz_denon_dac(unsigned int id)
1144{
1145 switch (id) {
1146 case USB_ID(0x154e, 0x1003): /* Denon DA-300USB */
1147 case USB_ID(0x154e, 0x3005): /* Marantz HD-DAC1 */
1148 case USB_ID(0x154e, 0x3006): /* Marantz SA-14S1 */
1149 return true;
1150 }
1151 return false;
1152}
1153
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001154int snd_usb_select_mode_quirk(struct snd_usb_substream *subs,
1155 struct audioformat *fmt)
1156{
1157 struct usb_device *dev = subs->dev;
1158 int err;
1159
Takashi Iwai8b28c932015-03-04 15:37:01 +01001160 if (is_marantz_denon_dac(subs->stream->chip->usb_id)) {
Jurgen Kramer6874daa2014-11-28 17:32:54 +01001161 /* First switch to alt set 0, otherwise the mode switch cmd
1162 * will not be accepted by the DAC
1163 */
1164 err = usb_set_interface(dev, fmt->iface, 0);
1165 if (err < 0)
1166 return err;
1167
1168 mdelay(20); /* Delay needed after setting the interface */
1169
1170 switch (fmt->altsetting) {
1171 case 2: /* DSD mode requested */
1172 case 1: /* PCM mode requested */
1173 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0,
1174 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
1175 fmt->altsetting - 1, 1, NULL, 0);
1176 if (err < 0)
1177 return err;
1178 break;
1179 }
1180 mdelay(20);
1181 }
1182 return 0;
1183}
1184
Daniel Mack2b58fd52012-09-04 10:23:07 +02001185void snd_usb_endpoint_start_quirk(struct snd_usb_endpoint *ep)
1186{
1187 /*
1188 * "Playback Design" products send bogus feedback data at the start
1189 * of the stream. Ignore them.
1190 */
1191 if ((le16_to_cpu(ep->chip->dev->descriptor.idVendor) == 0x23ba) &&
1192 ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
1193 ep->skip_packets = 4;
Eldad Zack83e3acd2013-01-13 23:02:03 +01001194
1195 /*
Matt Gruskine9a25e02013-02-09 12:56:35 -05001196 * M-Audio Fast Track C400/C600 - when packets are not skipped, real
1197 * world latency varies by approx. +/- 50 frames (at 96KHz) each time
1198 * the stream is (re)started. When skipping packets 16 at endpoint
1199 * start up, the real world latency is stable within +/- 1 frame (also
Eldad Zack83e3acd2013-01-13 23:02:03 +01001200 * across power cycles).
1201 */
Matt Gruskine9a25e02013-02-09 12:56:35 -05001202 if ((ep->chip->usb_id == USB_ID(0x0763, 0x2030) ||
1203 ep->chip->usb_id == USB_ID(0x0763, 0x2031)) &&
Eldad Zack83e3acd2013-01-13 23:02:03 +01001204 ep->type == SND_USB_ENDPOINT_TYPE_DATA)
1205 ep->skip_packets = 16;
Daniel Mack2b58fd52012-09-04 10:23:07 +02001206}
1207
Daniel Mack21bb5aa2013-04-10 00:56:03 +08001208void snd_usb_set_interface_quirk(struct usb_device *dev)
1209{
1210 /*
1211 * "Playback Design" products need a 50ms delay after setting the
1212 * USB interface.
1213 */
Guillaume Fougnies465cc6d2016-01-26 00:28:27 +01001214 switch (le16_to_cpu(dev->descriptor.idVendor)) {
1215 case 0x23ba: /* Playback Design */
1216 case 0x0644: /* TEAC Corp. */
Daniel Mack21bb5aa2013-04-10 00:56:03 +08001217 mdelay(50);
Guillaume Fougnies465cc6d2016-01-26 00:28:27 +01001218 break;
1219 }
Daniel Mack21bb5aa2013-04-10 00:56:03 +08001220}
1221
Daniel Mack2b58fd52012-09-04 10:23:07 +02001222void snd_usb_ctl_msg_quirk(struct usb_device *dev, unsigned int pipe,
1223 __u8 request, __u8 requesttype, __u16 value,
1224 __u16 index, void *data, __u16 size)
1225{
1226 /*
1227 * "Playback Design" products need a 20ms delay after each
1228 * class compliant request
1229 */
1230 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x23ba) &&
1231 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1232 mdelay(20);
Jurgen Kramer6e84a8d2014-11-15 14:01:21 +01001233
Guillaume Fougnies465cc6d2016-01-26 00:28:27 +01001234 /*
1235 * "TEAC Corp." products need a 20ms delay after each
1236 * class compliant request
1237 */
1238 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x0644) &&
1239 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1240 mdelay(20);
1241
Jurgen Kramer6e84a8d2014-11-15 14:01:21 +01001242 /* Marantz/Denon devices with USB DAC functionality need a delay
1243 * after each class compliant request
1244 */
Takashi Iwai8b28c932015-03-04 15:37:01 +01001245 if (is_marantz_denon_dac(USB_ID(le16_to_cpu(dev->descriptor.idVendor),
1246 le16_to_cpu(dev->descriptor.idProduct)))
1247 && (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1248 mdelay(20);
Panu Matilainendacacb02014-11-30 18:45:40 +02001249
1250 /* Zoom R16/24 needs a tiny delay here, otherwise requests like
1251 * get/set frequency return as failed despite actually succeeding.
1252 */
1253 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x1686) &&
1254 (le16_to_cpu(dev->descriptor.idProduct) == 0x00dd) &&
1255 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1256 mdelay(1);
Daniel Mack2b58fd52012-09-04 10:23:07 +02001257}
1258
Daniel Mack126825e2013-04-17 00:01:40 +08001259/*
1260 * snd_usb_interface_dsd_format_quirks() is called from format.c to
1261 * augment the PCM format bit-field for DSD types. The UAC standards
1262 * don't have a designated bit field to denote DSD-capable interfaces,
1263 * hence all hardware that is known to support this format has to be
1264 * listed here.
1265 */
1266u64 snd_usb_interface_dsd_format_quirks(struct snd_usb_audio *chip,
1267 struct audioformat *fp,
1268 unsigned int sample_bytes)
1269{
1270 /* Playback Designs */
1271 if (le16_to_cpu(chip->dev->descriptor.idVendor) == 0x23ba) {
1272 switch (fp->altsetting) {
1273 case 1:
1274 fp->dsd_dop = true;
1275 return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1276 case 2:
1277 fp->dsd_bitrev = true;
1278 return SNDRV_PCM_FMTBIT_DSD_U8;
1279 case 3:
1280 fp->dsd_bitrev = true;
1281 return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1282 }
1283 }
1284
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001285 /* XMOS based USB DACs */
1286 switch (chip->usb_id) {
Jurgen Kramer38f74d52014-12-17 17:45:20 +01001287 case USB_ID(0x20b1, 0x3008): /* iFi Audio micro/nano iDSD */
1288 case USB_ID(0x20b1, 0x2008): /* Matrix Audio X-Sabre */
1289 case USB_ID(0x20b1, 0x300a): /* Matrix Audio Mini-i Pro */
Jurgen Kramera16d95b2016-01-29 14:49:55 +01001290 case USB_ID(0x22d9, 0x0416): /* OPPO HA-1 */
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001291 if (fp->altsetting == 2)
Jussi Laakod42472e2014-11-21 16:04:46 +02001292 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001293 break;
Jurgen Kramer3b7e5c72015-06-05 09:42:49 +02001294
Jurgen Kramere6212cc2015-08-21 09:48:35 +02001295 case USB_ID(0x20b1, 0x000a): /* Gustard DAC-X20U */
Jurgen Kramer3b7e5c72015-06-05 09:42:49 +02001296 case USB_ID(0x20b1, 0x2009): /* DIYINHK DSD DXD 384kHz USB to I2S/DSD */
1297 case USB_ID(0x20b1, 0x2023): /* JLsounds I2SoverUSB */
Jurgen Kramer3577eb62015-11-09 12:13:55 +01001298 case USB_ID(0x20b1, 0x3023): /* Aune X1S 32BIT/384 DSD DAC */
Jurgen Kramer29eccda2016-01-29 14:59:25 +01001299 case USB_ID(0x2616, 0x0106): /* PS Audio NuWave DAC */
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001300 if (fp->altsetting == 3)
Jussi Laakod42472e2014-11-21 16:04:46 +02001301 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
Jurgen Kramer848f3a82014-09-05 18:14:46 +02001302 break;
1303 default:
1304 break;
1305 }
1306
Jurgen Kramer7a2e9dd2014-11-28 17:32:53 +01001307 /* Denon/Marantz devices with USB DAC functionality */
Takashi Iwai8b28c932015-03-04 15:37:01 +01001308 if (is_marantz_denon_dac(chip->usb_id)) {
Jurgen Kramer7a2e9dd2014-11-28 17:32:53 +01001309 if (fp->altsetting == 2)
1310 return SNDRV_PCM_FMTBIT_DSD_U32_BE;
Jurgen Kramer7a2e9dd2014-11-28 17:32:53 +01001311 }
1312
Daniel Mack126825e2013-04-17 00:01:40 +08001313 return 0;
1314}