ALSA: usb-audio: Playback and MIDI support for Novation Twitch DJ controller
[linux-2.6-block.git] / sound / usb / quirks.c
CommitLineData
e5779998
DM
1/*
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>
36db0456 18#include <linux/slab.h>
e5779998
DM
19#include <linux/usb.h>
20#include <linux/usb/audio.h>
21
9e38658f 22#include <sound/control.h>
e5779998
DM
23#include <sound/core.h>
24#include <sound/info.h>
25#include <sound/pcm.h>
26
27#include "usbaudio.h"
28#include "card.h"
f0b5e634 29#include "mixer.h"
7b1eda22 30#include "mixer_quirks.h"
e5779998
DM
31#include "midi.h"
32#include "quirks.h"
33#include "helper.h"
34#include "endpoint.h"
35#include "pcm.h"
3d8d4dcf 36#include "clock.h"
e8e8babf 37#include "stream.h"
e5779998
DM
38
39/*
40 * handle the quirks for the contained interfaces
41 */
42static int create_composite_quirk(struct snd_usb_audio *chip,
43 struct usb_interface *iface,
44 struct usb_driver *driver,
45 const struct snd_usb_audio_quirk *quirk)
46{
47 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
48 int err;
49
50 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
51 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
52 if (!iface)
53 continue;
54 if (quirk->ifnum != probed_ifnum &&
55 usb_interface_claimed(iface))
56 continue;
57 err = snd_usb_create_quirk(chip, iface, driver, quirk);
58 if (err < 0)
59 return err;
60 if (quirk->ifnum != probed_ifnum)
61 usb_driver_claim_interface(driver, iface, (void *)-1L);
62 }
63 return 0;
64}
65
66static int ignore_interface_quirk(struct snd_usb_audio *chip,
67 struct usb_interface *iface,
68 struct usb_driver *driver,
69 const struct snd_usb_audio_quirk *quirk)
70{
71 return 0;
72}
73
74
75/*
76 * Allow alignment on audio sub-slot (channel samples) rather than
77 * on audio slots (audio frames)
78 */
79static int create_align_transfer_quirk(struct snd_usb_audio *chip,
80 struct usb_interface *iface,
81 struct usb_driver *driver,
82 const struct snd_usb_audio_quirk *quirk)
83{
84 chip->txfr_quirk = 1;
85 return 1; /* Continue with creating streams and mixer */
86}
87
88static int create_any_midi_quirk(struct snd_usb_audio *chip,
89 struct usb_interface *intf,
90 struct usb_driver *driver,
91 const struct snd_usb_audio_quirk *quirk)
92{
93 return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
94}
95
96/*
97 * create a stream for an interface with proper descriptors
98 */
99static int create_standard_audio_quirk(struct snd_usb_audio *chip,
100 struct usb_interface *iface,
101 struct usb_driver *driver,
102 const struct snd_usb_audio_quirk *quirk)
103{
104 struct usb_host_interface *alts;
105 struct usb_interface_descriptor *altsd;
106 int err;
107
108 alts = &iface->altsetting[0];
109 altsd = get_iface_desc(alts);
e8e8babf 110 err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
e5779998
DM
111 if (err < 0) {
112 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
113 altsd->bInterfaceNumber, err);
114 return err;
115 }
116 /* reset the current interface */
117 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
118 return 0;
119}
120
121/*
122 * create a stream for an endpoint/altsetting without proper descriptors
123 */
124static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
125 struct usb_interface *iface,
126 struct usb_driver *driver,
127 const struct snd_usb_audio_quirk *quirk)
128{
129 struct audioformat *fp;
130 struct usb_host_interface *alts;
131 int stream, err;
132 unsigned *rate_table = NULL;
133
134 fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
8866f405 135 if (!fp) {
e5779998
DM
136 snd_printk(KERN_ERR "cannot memdup\n");
137 return -ENOMEM;
138 }
8866f405
XW
139 if (fp->nr_rates > MAX_NR_RATES) {
140 kfree(fp);
141 return -EINVAL;
142 }
e5779998 143 if (fp->nr_rates > 0) {
43df2a57
TM
144 rate_table = kmemdup(fp->rate_table,
145 sizeof(int) * fp->nr_rates, GFP_KERNEL);
e5779998
DM
146 if (!rate_table) {
147 kfree(fp);
148 return -ENOMEM;
149 }
e5779998
DM
150 fp->rate_table = rate_table;
151 }
152
153 stream = (fp->endpoint & USB_DIR_IN)
154 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
e8e8babf 155 err = snd_usb_add_audio_stream(chip, stream, fp);
e5779998
DM
156 if (err < 0) {
157 kfree(fp);
158 kfree(rate_table);
159 return err;
160 }
161 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
162 fp->altset_idx >= iface->num_altsetting) {
163 kfree(fp);
164 kfree(rate_table);
165 return -EINVAL;
166 }
167 alts = &iface->altsetting[fp->altset_idx];
168 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
169 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
170 usb_set_interface(chip->dev, fp->iface, 0);
767d75ad
DM
171 snd_usb_init_pitch(chip, fp->iface, alts, fp);
172 snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
e5779998
DM
173 return 0;
174}
175
176/*
177 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
178 * The only way to detect the sample rate is by looking at wMaxPacketSize.
179 */
180static int create_uaxx_quirk(struct snd_usb_audio *chip,
181 struct usb_interface *iface,
182 struct usb_driver *driver,
183 const struct snd_usb_audio_quirk *quirk)
184{
185 static const struct audioformat ua_format = {
015eb0b0 186 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
e5779998
DM
187 .channels = 2,
188 .fmt_type = UAC_FORMAT_TYPE_I,
189 .altsetting = 1,
190 .altset_idx = 1,
191 .rates = SNDRV_PCM_RATE_CONTINUOUS,
192 };
193 struct usb_host_interface *alts;
194 struct usb_interface_descriptor *altsd;
195 struct audioformat *fp;
196 int stream, err;
197
198 /* both PCM and MIDI interfaces have 2 or more altsettings */
199 if (iface->num_altsetting < 2)
200 return -ENXIO;
201 alts = &iface->altsetting[1];
202 altsd = get_iface_desc(alts);
203
204 if (altsd->bNumEndpoints == 2) {
205 static const struct snd_usb_midi_endpoint_info ua700_ep = {
206 .out_cables = 0x0003,
207 .in_cables = 0x0003
208 };
209 static const struct snd_usb_audio_quirk ua700_quirk = {
210 .type = QUIRK_MIDI_FIXED_ENDPOINT,
211 .data = &ua700_ep
212 };
213 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
214 .out_cables = 0x0001,
215 .in_cables = 0x0001
216 };
217 static const struct snd_usb_audio_quirk uaxx_quirk = {
218 .type = QUIRK_MIDI_FIXED_ENDPOINT,
219 .data = &uaxx_ep
220 };
221 const struct snd_usb_audio_quirk *quirk =
222 chip->usb_id == USB_ID(0x0582, 0x002b)
223 ? &ua700_quirk : &uaxx_quirk;
224 return snd_usbmidi_create(chip->card, iface,
225 &chip->midi_list, quirk);
226 }
227
228 if (altsd->bNumEndpoints != 1)
229 return -ENXIO;
230
43df2a57 231 fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
e5779998
DM
232 if (!fp)
233 return -ENOMEM;
e5779998
DM
234
235 fp->iface = altsd->bInterfaceNumber;
236 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
237 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
238 fp->datainterval = 0;
239 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
240
241 switch (fp->maxpacksize) {
242 case 0x120:
243 fp->rate_max = fp->rate_min = 44100;
244 break;
245 case 0x138:
246 case 0x140:
247 fp->rate_max = fp->rate_min = 48000;
248 break;
249 case 0x258:
250 case 0x260:
251 fp->rate_max = fp->rate_min = 96000;
252 break;
253 default:
254 snd_printk(KERN_ERR "unknown sample rate\n");
255 kfree(fp);
256 return -ENXIO;
257 }
258
259 stream = (fp->endpoint & USB_DIR_IN)
260 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
e8e8babf 261 err = snd_usb_add_audio_stream(chip, stream, fp);
e5779998
DM
262 if (err < 0) {
263 kfree(fp);
264 return err;
265 }
266 usb_set_interface(chip->dev, fp->iface, 0);
267 return 0;
268}
269
014950b0
DM
270/*
271 * Create a standard mixer for the specified interface.
272 */
273static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
274 struct usb_interface *iface,
275 struct usb_driver *driver,
276 const struct snd_usb_audio_quirk *quirk)
277{
278 if (quirk->ifnum < 0)
279 return 0;
280
281 return snd_usb_create_mixer(chip, quirk->ifnum, 0);
282}
283
e5779998
DM
284/*
285 * audio-interface quirks
286 *
287 * returns zero if no standard audio/MIDI parsing is needed.
25985edc 288 * returns a positive value if standard audio/midi interfaces are parsed
e5779998
DM
289 * after this.
290 * returns a negative value at error.
291 */
292int snd_usb_create_quirk(struct snd_usb_audio *chip,
293 struct usb_interface *iface,
294 struct usb_driver *driver,
295 const struct snd_usb_audio_quirk *quirk)
296{
297 typedef int (*quirk_func_t)(struct snd_usb_audio *,
298 struct usb_interface *,
299 struct usb_driver *,
300 const struct snd_usb_audio_quirk *);
301 static const quirk_func_t quirk_funcs[] = {
302 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
303 [QUIRK_COMPOSITE] = create_composite_quirk,
304 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
305 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
306 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
307 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
308 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
c7f57216 309 [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
e5779998
DM
310 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
311 [QUIRK_MIDI_CME] = create_any_midi_quirk,
4434ade8 312 [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
1ef0e0a0 313 [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
e5779998
DM
314 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
315 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
316 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
014950b0
DM
317 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
318 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
e5779998
DM
319 };
320
321 if (quirk->type < QUIRK_TYPE_COUNT) {
322 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
323 } else {
324 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
325 return -ENXIO;
326 }
327}
328
329/*
330 * boot quirks
331 */
332
333#define EXTIGY_FIRMWARE_SIZE_OLD 794
334#define EXTIGY_FIRMWARE_SIZE_NEW 483
335
336static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
337{
338 struct usb_host_config *config = dev->actconfig;
339 int err;
340
341 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
342 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
343 snd_printdd("sending Extigy boot sequence...\n");
344 /* Send message to force it to reconnect with full interface. */
345 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
17d900c4 346 0x10, 0x43, 0x0001, 0x000a, NULL, 0);
e5779998
DM
347 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
348 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
349 &dev->descriptor, sizeof(dev->descriptor));
350 config = dev->actconfig;
351 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
352 err = usb_reset_configuration(dev);
353 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
354 snd_printdd("extigy_boot: new boot length = %d\n",
355 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
356 return -ENODEV; /* quit this anyway */
357 }
358 return 0;
359}
360
361static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
362{
363 u8 buf = 1;
364
365 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
366 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
17d900c4 367 0, 0, &buf, 1);
e5779998
DM
368 if (buf == 0) {
369 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
370 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
17d900c4 371 1, 2000, NULL, 0);
e5779998
DM
372 return -ENODEV;
373 }
374 return 0;
375}
376
0f5733b0
GP
377static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
378{
379 int err;
380
381 if (dev->actconfig->desc.bConfigurationValue == 1) {
382 snd_printk(KERN_INFO "usb-audio: "
383 "Fast Track Pro switching to config #2\n");
384 /* This function has to be available by the usb core module.
385 * if it is not avialable the boot quirk has to be left out
386 * and the configuration has to be set by udev or hotplug
387 * rules
388 */
389 err = usb_driver_set_configuration(dev, 2);
b98ae272 390 if (err < 0)
0f5733b0
GP
391 snd_printdd("error usb_driver_set_configuration: %d\n",
392 err);
b98ae272
DH
393 /* Always return an error, so that we stop creating a device
394 that will just be destroyed and recreated with a new
395 configuration */
396 return -ENODEV;
0f5733b0
GP
397 } else
398 snd_printk(KERN_INFO "usb-audio: Fast Track Pro config OK\n");
399
400 return 0;
401}
402
e5779998
DM
403/*
404 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
405 * documented in the device's data sheet.
406 */
407static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
408{
409 u8 buf[4];
410 buf[0] = 0x20;
411 buf[1] = value & 0xff;
412 buf[2] = (value >> 8) & 0xff;
413 buf[3] = reg;
414 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
415 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
17d900c4 416 0, 0, &buf, 4);
e5779998
DM
417}
418
419static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
420{
421 /*
422 * Enable line-out driver mode, set headphone source to front
423 * channels, enable stereo mic.
424 */
425 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
426}
427
428/*
429 * C-Media CM6206 is based on CM106 with two additional
430 * registers that are not documented in the data sheet.
431 * Values here are chosen based on sniffing USB traffic
432 * under Windows.
433 */
434static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
435{
dac8f847 436 int err = 0, reg;
157186bc 437 int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
e5779998
DM
438
439 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
440 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
441 if (err < 0)
442 return err;
443 }
444
445 return err;
446}
447
5e212332
MH
448/*
449 * Novation Twitch DJ controller
450 */
451static int snd_usb_twitch_boot_quirk(struct usb_device *dev)
452{
453 /* preemptively set up the device because otherwise the
454 * raw MIDI endpoints are not active */
455 usb_set_interface(dev, 0, 1);
456 return 0;
457}
458
e5779998
DM
459/*
460 * This call will put the synth in "USB send" mode, i.e it will send MIDI
461 * messages through USB (this is disabled at startup). The synth will
462 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
463 * sign on its LCD. Values here are chosen based on sniffing USB traffic
464 * under Windows.
465 */
466static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
467{
468 int err, actual_length;
469
470 /* "midi send" enable */
471 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
472
473 void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
474 if (!buf)
475 return -ENOMEM;
476 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
477 ARRAY_SIZE(seq), &actual_length, 1000);
478 kfree(buf);
479 if (err < 0)
480 return err;
481
482 return 0;
483}
484
54a8c500
DM
485/*
486 * Some sound cards from Native Instruments are in fact compliant to the USB
487 * audio standard of version 2 and other approved USB standards, even though
488 * they come up as vendor-specific device when first connected.
489 *
490 * However, they can be told to come up with a new set of descriptors
491 * upon their next enumeration, and the interfaces announced by the new
492 * descriptors will then be handled by the kernel's class drivers. As the
493 * product ID will also change, no further checks are required.
494 */
495
496static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
497{
498 int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
499 0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
500 cpu_to_le16(1), 0, NULL, 0, 1000);
501
502 if (ret < 0)
503 return ret;
504
505 usb_reset_device(dev);
506
507 /* return -EAGAIN, so the creation of an audio interface for this
508 * temporary device is aborted. The device will reconnect with a
509 * new product ID */
510 return -EAGAIN;
511}
512
cb99864d
DZ
513static void mbox2_setup_48_24_magic(struct usb_device *dev)
514{
515 u8 srate[3];
516 u8 temp[12];
517
518 /* Choose 48000Hz permanently */
519 srate[0] = 0x80;
520 srate[1] = 0xbb;
521 srate[2] = 0x00;
522
523 /* Send the magic! */
524 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
525 0x01, 0x22, 0x0100, 0x0085, &temp, 0x0003);
526 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
527 0x81, 0xa2, 0x0100, 0x0085, &srate, 0x0003);
528 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
529 0x81, 0xa2, 0x0100, 0x0086, &srate, 0x0003);
530 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
531 0x81, 0xa2, 0x0100, 0x0003, &srate, 0x0003);
532 return;
533}
534
535/* Digidesign Mbox 2 needs to load firmware onboard
536 * and driver must wait a few seconds for initialisation.
537 */
538
539#define MBOX2_FIRMWARE_SIZE 646
540#define MBOX2_BOOT_LOADING 0x01 /* Hard coded into the device */
541#define MBOX2_BOOT_READY 0x02 /* Hard coded into the device */
542
b7b435e8 543static int snd_usb_mbox2_boot_quirk(struct usb_device *dev)
cb99864d
DZ
544{
545 struct usb_host_config *config = dev->actconfig;
546 int err;
4909a0ca 547 u8 bootresponse[0x12];
cb99864d
DZ
548 int fwsize;
549 int count;
550
551 fwsize = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
552
553 if (fwsize != MBOX2_FIRMWARE_SIZE) {
554 snd_printk(KERN_ERR "usb-audio: Invalid firmware size=%d.\n", fwsize);
555 return -ENODEV;
556 }
557
558 snd_printd("usb-audio: Sending Digidesign Mbox 2 boot sequence...\n");
559
560 count = 0;
b7b435e8
DZ
561 bootresponse[0] = MBOX2_BOOT_LOADING;
562 while ((bootresponse[0] == MBOX2_BOOT_LOADING) && (count < 10)) {
cb99864d
DZ
563 msleep(500); /* 0.5 second delay */
564 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
565 /* Control magic - load onboard firmware */
566 0x85, 0xc0, 0x0001, 0x0000, &bootresponse, 0x0012);
b7b435e8 567 if (bootresponse[0] == MBOX2_BOOT_READY)
cb99864d
DZ
568 break;
569 snd_printd("usb-audio: device not ready, resending boot sequence...\n");
570 count++;
571 }
572
b7b435e8
DZ
573 if (bootresponse[0] != MBOX2_BOOT_READY) {
574 snd_printk(KERN_ERR "usb-audio: Unknown bootresponse=%d, or timed out, ignoring device.\n", bootresponse[0]);
cb99864d
DZ
575 return -ENODEV;
576 }
577
578 snd_printdd("usb-audio: device initialised!\n");
579
580 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
581 &dev->descriptor, sizeof(dev->descriptor));
582 config = dev->actconfig;
583 if (err < 0)
584 snd_printd("error usb_get_descriptor: %d\n", err);
585
586 err = usb_reset_configuration(dev);
587 if (err < 0)
588 snd_printd("error usb_reset_configuration: %d\n", err);
589 snd_printdd("mbox2_boot: new boot length = %d\n",
590 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
591
592 mbox2_setup_48_24_magic(dev);
593
594 snd_printk(KERN_INFO "usb-audio: Digidesign Mbox 2: 24bit 48kHz");
595
596 return 0; /* Successful boot */
597}
598
e5779998
DM
599/*
600 * Setup quirks
601 */
0f5733b0
GP
602#define MAUDIO_SET 0x01 /* parse device_setup */
603#define MAUDIO_SET_COMPATIBLE 0x80 /* use only "win-compatible" interfaces */
604#define MAUDIO_SET_DTS 0x02 /* enable DTS Digital Output */
605#define MAUDIO_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
606#define MAUDIO_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
607#define MAUDIO_SET_DI 0x10 /* enable Digital Input */
608#define MAUDIO_SET_MASK 0x1f /* bit mask for setup value */
609#define MAUDIO_SET_24B_48K_DI 0x19 /* 24bits+48KHz+Digital Input */
610#define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
611#define MAUDIO_SET_16B_48K_DI 0x11 /* 16bits+48KHz+Digital Input */
612#define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
613
614static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
615 int iface, int altno)
616{
617 /* Reset ALL ifaces to 0 altsetting.
618 * Call it for every possible altsetting of every interface.
619 */
620 usb_set_interface(chip->dev, iface, 0);
621 if (chip->setup & MAUDIO_SET) {
622 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
623 if (iface != 1 && iface != 2)
624 return 1; /* skip all interfaces but 1 and 2 */
625 } else {
626 unsigned int mask;
627 if (iface == 1 || iface == 2)
628 return 1; /* skip interfaces 1 and 2 */
629 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
630 return 1; /* skip this altsetting */
631 mask = chip->setup & MAUDIO_SET_MASK;
632 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
633 return 1; /* skip this altsetting */
634 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
635 return 1; /* skip this altsetting */
636 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
637 return 1; /* skip this altsetting */
638 }
639 }
640 snd_printdd(KERN_INFO
641 "using altsetting %d for interface %d config %d\n",
642 altno, iface, chip->setup);
643 return 0; /* keep this altsetting */
644}
e5779998
DM
645
646static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
647 int iface,
648 int altno)
649{
650 /* Reset ALL ifaces to 0 altsetting.
651 * Call it for every possible altsetting of every interface.
652 */
653 usb_set_interface(chip->dev, iface, 0);
654
0f5733b0
GP
655 if (chip->setup & MAUDIO_SET) {
656 unsigned int mask;
657 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
e5779998 658 return 1; /* skip this altsetting */
0f5733b0 659 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
e5779998 660 return 1; /* skip this altsetting */
0f5733b0
GP
661 mask = chip->setup & MAUDIO_SET_MASK;
662 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
e5779998 663 return 1; /* skip this altsetting */
0f5733b0 664 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
e5779998 665 return 1; /* skip this altsetting */
0f5733b0 666 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
e5779998 667 return 1; /* skip this altsetting */
0f5733b0 668 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
e5779998
DM
669 return 1; /* skip this altsetting */
670 }
671
672 return 0; /* keep this altsetting */
673}
674
0f5733b0
GP
675static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
676 int iface, int altno)
677{
678 /* Reset ALL ifaces to 0 altsetting.
679 * Call it for every possible altsetting of every interface.
680 */
681 usb_set_interface(chip->dev, iface, 0);
682
683 /* possible configuration where both inputs and only one output is
684 *used is not supported by the current setup
685 */
686 if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
687 if (chip->setup & MAUDIO_SET_96K) {
688 if (altno != 3 && altno != 6)
689 return 1;
690 } else if (chip->setup & MAUDIO_SET_DI) {
691 if (iface == 4)
692 return 1; /* no analog input */
693 if (altno != 2 && altno != 5)
694 return 1; /* enable only altsets 2 and 5 */
695 } else {
696 if (iface == 5)
697 return 1; /* disable digialt input */
698 if (altno != 2 && altno != 5)
699 return 1; /* enalbe only altsets 2 and 5 */
700 }
701 } else {
702 /* keep only 16-Bit mode */
703 if (altno != 1)
704 return 1;
705 }
706
707 snd_printdd(KERN_INFO
708 "using altsetting %d for interface %d config %d\n",
709 altno, iface, chip->setup);
710 return 0; /* keep this altsetting */
711}
712
e5779998
DM
713int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
714 int iface,
715 int altno)
716{
717 /* audiophile usb: skip altsets incompatible with device_setup */
718 if (chip->usb_id == USB_ID(0x0763, 0x2003))
719 return audiophile_skip_setting_quirk(chip, iface, altno);
0f5733b0
GP
720 /* quattro usb: skip altsets incompatible with device_setup */
721 if (chip->usb_id == USB_ID(0x0763, 0x2001))
722 return quattro_skip_setting_quirk(chip, iface, altno);
723 /* fasttrackpro usb: skip altsets incompatible with device_setup */
724 if (chip->usb_id == USB_ID(0x0763, 0x2012))
725 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
e5779998
DM
726
727 return 0;
728}
729
730int snd_usb_apply_boot_quirk(struct usb_device *dev,
731 struct usb_interface *intf,
732 const struct snd_usb_audio_quirk *quirk)
733{
734 u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
735 le16_to_cpu(dev->descriptor.idProduct));
736
3347b26c
DM
737 switch (id) {
738 case USB_ID(0x041e, 0x3000):
739 /* SB Extigy needs special boot-up sequence */
740 /* if more models come, this will go to the quirk list. */
e5779998
DM
741 return snd_usb_extigy_boot_quirk(dev, intf);
742
3347b26c
DM
743 case USB_ID(0x041e, 0x3020):
744 /* SB Audigy 2 NX needs its own boot-up magic, too */
e5779998
DM
745 return snd_usb_audigy2nx_boot_quirk(dev);
746
3347b26c
DM
747 case USB_ID(0x10f5, 0x0200):
748 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
e5779998
DM
749 return snd_usb_cm106_boot_quirk(dev);
750
3347b26c
DM
751 case USB_ID(0x0d8c, 0x0102):
752 /* C-Media CM6206 / CM106-Like Sound Device */
8129e79e 753 case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
e5779998
DM
754 return snd_usb_cm6206_boot_quirk(dev);
755
cb99864d
DZ
756 case USB_ID(0x0dba, 0x3000):
757 /* Digidesign Mbox 2 */
758 return snd_usb_mbox2_boot_quirk(dev);
759
5e212332
MH
760 case USB_ID(0x1235, 0x0018):
761 /* Focusrite Novation Twitch */
762 return snd_usb_twitch_boot_quirk(dev);
763
3347b26c
DM
764 case USB_ID(0x133e, 0x0815):
765 /* Access Music VirusTI Desktop */
e5779998
DM
766 return snd_usb_accessmusic_boot_quirk(dev);
767
759e890f 768 case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
3347b26c
DM
769 case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
770 case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
54a8c500 771 return snd_usb_nativeinstruments_boot_quirk(dev);
0f5733b0
GP
772 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
773 return snd_usb_fasttrackpro_boot_quirk(dev);
3347b26c 774 }
54a8c500 775
e5779998
DM
776 return 0;
777}
778
779/*
780 * check if the device uses big-endian samples
781 */
782int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
783{
48fc7f7e 784 /* it depends on altsetting whether the device is big-endian or not */
e5779998
DM
785 switch (chip->usb_id) {
786 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
0f5733b0
GP
787 if (fp->altsetting == 2 || fp->altsetting == 3 ||
788 fp->altsetting == 5 || fp->altsetting == 6)
e5779998
DM
789 return 1;
790 break;
791 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
792 if (chip->setup == 0x00 ||
0f5733b0
GP
793 fp->altsetting == 1 || fp->altsetting == 2 ||
794 fp->altsetting == 3)
795 return 1;
796 break;
797 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
798 if (fp->altsetting == 2 || fp->altsetting == 3 ||
799 fp->altsetting == 5 || fp->altsetting == 6)
e5779998 800 return 1;
0f5733b0 801 break;
e5779998
DM
802 }
803 return 0;
804}
805
806/*
1cdfa9f3 807 * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
e5779998
DM
808 * not for interface.
809 */
810
811enum {
812 EMU_QUIRK_SR_44100HZ = 0,
813 EMU_QUIRK_SR_48000HZ,
814 EMU_QUIRK_SR_88200HZ,
815 EMU_QUIRK_SR_96000HZ,
816 EMU_QUIRK_SR_176400HZ,
817 EMU_QUIRK_SR_192000HZ
818};
819
820static void set_format_emu_quirk(struct snd_usb_substream *subs,
821 struct audioformat *fmt)
822{
823 unsigned char emu_samplerate_id = 0;
824
825 /* When capture is active
826 * sample rate shouldn't be changed
827 * by playback substream
828 */
829 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
830 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
831 return;
832 }
833
834 switch (fmt->rate_min) {
835 case 48000:
836 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
837 break;
838 case 88200:
839 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
840 break;
841 case 96000:
842 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
843 break;
844 case 176400:
845 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
846 break;
847 case 192000:
848 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
849 break;
850 default:
851 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
852 break;
853 }
854 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
855}
856
857void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
858 struct audioformat *fmt)
859{
860 switch (subs->stream->chip->usb_id) {
861 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
862 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
863 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
1cdfa9f3 864 case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
e5779998
DM
865 set_format_emu_quirk(subs, fmt);
866 break;
867 }
868}
869
2b58fd5b
DM
870void snd_usb_endpoint_start_quirk(struct snd_usb_endpoint *ep)
871{
872 /*
873 * "Playback Design" products send bogus feedback data at the start
874 * of the stream. Ignore them.
875 */
876 if ((le16_to_cpu(ep->chip->dev->descriptor.idVendor) == 0x23ba) &&
877 ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
878 ep->skip_packets = 4;
83e3acd4
EZ
879
880 /*
e9a25e04
MG
881 * M-Audio Fast Track C400/C600 - when packets are not skipped, real
882 * world latency varies by approx. +/- 50 frames (at 96KHz) each time
883 * the stream is (re)started. When skipping packets 16 at endpoint
884 * start up, the real world latency is stable within +/- 1 frame (also
83e3acd4
EZ
885 * across power cycles).
886 */
e9a25e04
MG
887 if ((ep->chip->usb_id == USB_ID(0x0763, 0x2030) ||
888 ep->chip->usb_id == USB_ID(0x0763, 0x2031)) &&
83e3acd4
EZ
889 ep->type == SND_USB_ENDPOINT_TYPE_DATA)
890 ep->skip_packets = 16;
2b58fd5b
DM
891}
892
893void snd_usb_ctl_msg_quirk(struct usb_device *dev, unsigned int pipe,
894 __u8 request, __u8 requesttype, __u16 value,
895 __u16 index, void *data, __u16 size)
896{
897 /*
898 * "Playback Design" products need a 20ms delay after each
899 * class compliant request
900 */
901 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x23ba) &&
902 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
903 mdelay(20);
904}
905