blob: a2ac004bbd9a3bd689e047d61cc8937407b0e1b7 [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>
21
Daniel Mack9e386582011-05-25 09:09:01 +020022#include <sound/control.h>
Daniel Macke5779992010-03-04 19:46:13 +010023#include <sound/core.h>
24#include <sound/info.h>
25#include <sound/pcm.h>
26
27#include "usbaudio.h"
28#include "card.h"
Daniel Mackf0b5e632010-03-11 21:13:23 +010029#include "mixer.h"
Daniel Mack7b1eda22010-03-11 21:13:22 +010030#include "mixer_quirks.h"
Daniel Macke5779992010-03-04 19:46:13 +010031#include "midi.h"
32#include "quirks.h"
33#include "helper.h"
34#include "endpoint.h"
35#include "pcm.h"
Daniel Mack3d8d4dc2010-06-16 17:57:31 +020036#include "clock.h"
Daniel Macke8e8bab2011-09-12 18:54:12 +020037#include "stream.h"
Daniel Macke5779992010-03-04 19:46:13 +010038
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);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200110 err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
Daniel Macke5779992010-03-04 19:46:13 +0100111 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);
Xi Wang8866f402012-02-14 05:18:48 -0500135 if (!fp) {
Daniel Macke5779992010-03-04 19:46:13 +0100136 snd_printk(KERN_ERR "cannot memdup\n");
137 return -ENOMEM;
138 }
Xi Wang8866f402012-02-14 05:18:48 -0500139 if (fp->nr_rates > MAX_NR_RATES) {
140 kfree(fp);
141 return -EINVAL;
142 }
Daniel Macke5779992010-03-04 19:46:13 +0100143 if (fp->nr_rates > 0) {
Thomas Meyer43df2a52011-11-10 19:38:43 +0100144 rate_table = kmemdup(fp->rate_table,
145 sizeof(int) * fp->nr_rates, GFP_KERNEL);
Daniel Macke5779992010-03-04 19:46:13 +0100146 if (!rate_table) {
147 kfree(fp);
148 return -ENOMEM;
149 }
Daniel Macke5779992010-03-04 19:46:13 +0100150 fp->rate_table = rate_table;
151 }
152
153 stream = (fp->endpoint & USB_DIR_IN)
154 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200155 err = snd_usb_add_audio_stream(chip, stream, fp);
Daniel Macke5779992010-03-04 19:46:13 +0100156 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];
Mark Hills59ea5862013-03-17 11:07:54 +0000168 if (fp->datainterval == 0)
169 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
170 if (fp->maxpacksize == 0)
171 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Daniel Macke5779992010-03-04 19:46:13 +0100172 usb_set_interface(chip->dev, fp->iface, 0);
Daniel Mack767d75a2010-03-04 19:46:17 +0100173 snd_usb_init_pitch(chip, fp->iface, alts, fp);
174 snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
Daniel Macke5779992010-03-04 19:46:13 +0100175 return 0;
176}
177
178/*
179 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
180 * The only way to detect the sample rate is by looking at wMaxPacketSize.
181 */
182static int create_uaxx_quirk(struct snd_usb_audio *chip,
183 struct usb_interface *iface,
184 struct usb_driver *driver,
185 const struct snd_usb_audio_quirk *quirk)
186{
187 static const struct audioformat ua_format = {
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100188 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
Daniel Macke5779992010-03-04 19:46:13 +0100189 .channels = 2,
190 .fmt_type = UAC_FORMAT_TYPE_I,
191 .altsetting = 1,
192 .altset_idx = 1,
193 .rates = SNDRV_PCM_RATE_CONTINUOUS,
194 };
195 struct usb_host_interface *alts;
196 struct usb_interface_descriptor *altsd;
197 struct audioformat *fp;
198 int stream, err;
199
200 /* both PCM and MIDI interfaces have 2 or more altsettings */
201 if (iface->num_altsetting < 2)
202 return -ENXIO;
203 alts = &iface->altsetting[1];
204 altsd = get_iface_desc(alts);
205
206 if (altsd->bNumEndpoints == 2) {
207 static const struct snd_usb_midi_endpoint_info ua700_ep = {
208 .out_cables = 0x0003,
209 .in_cables = 0x0003
210 };
211 static const struct snd_usb_audio_quirk ua700_quirk = {
212 .type = QUIRK_MIDI_FIXED_ENDPOINT,
213 .data = &ua700_ep
214 };
215 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
216 .out_cables = 0x0001,
217 .in_cables = 0x0001
218 };
219 static const struct snd_usb_audio_quirk uaxx_quirk = {
220 .type = QUIRK_MIDI_FIXED_ENDPOINT,
221 .data = &uaxx_ep
222 };
223 const struct snd_usb_audio_quirk *quirk =
224 chip->usb_id == USB_ID(0x0582, 0x002b)
225 ? &ua700_quirk : &uaxx_quirk;
226 return snd_usbmidi_create(chip->card, iface,
227 &chip->midi_list, quirk);
228 }
229
230 if (altsd->bNumEndpoints != 1)
231 return -ENXIO;
232
Thomas Meyer43df2a52011-11-10 19:38:43 +0100233 fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
Daniel Macke5779992010-03-04 19:46:13 +0100234 if (!fp)
235 return -ENOMEM;
Daniel Macke5779992010-03-04 19:46:13 +0100236
237 fp->iface = altsd->bInterfaceNumber;
238 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
239 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
240 fp->datainterval = 0;
241 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
242
243 switch (fp->maxpacksize) {
244 case 0x120:
245 fp->rate_max = fp->rate_min = 44100;
246 break;
247 case 0x138:
248 case 0x140:
249 fp->rate_max = fp->rate_min = 48000;
250 break;
251 case 0x258:
252 case 0x260:
253 fp->rate_max = fp->rate_min = 96000;
254 break;
255 default:
256 snd_printk(KERN_ERR "unknown sample rate\n");
257 kfree(fp);
258 return -ENXIO;
259 }
260
261 stream = (fp->endpoint & USB_DIR_IN)
262 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200263 err = snd_usb_add_audio_stream(chip, stream, fp);
Daniel Macke5779992010-03-04 19:46:13 +0100264 if (err < 0) {
265 kfree(fp);
266 return err;
267 }
268 usb_set_interface(chip->dev, fp->iface, 0);
269 return 0;
270}
271
272/*
Daniel Mack014950b2011-05-25 09:09:02 +0200273 * Create a standard mixer for the specified interface.
274 */
275static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
276 struct usb_interface *iface,
277 struct usb_driver *driver,
278 const struct snd_usb_audio_quirk *quirk)
279{
280 if (quirk->ifnum < 0)
281 return 0;
282
283 return snd_usb_create_mixer(chip, quirk->ifnum, 0);
284}
285
286/*
Daniel Macke5779992010-03-04 19:46:13 +0100287 * audio-interface quirks
288 *
289 * returns zero if no standard audio/MIDI parsing is needed.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300290 * returns a positive value if standard audio/midi interfaces are parsed
Daniel Macke5779992010-03-04 19:46:13 +0100291 * after this.
292 * returns a negative value at error.
293 */
294int snd_usb_create_quirk(struct snd_usb_audio *chip,
295 struct usb_interface *iface,
296 struct usb_driver *driver,
297 const struct snd_usb_audio_quirk *quirk)
298{
299 typedef int (*quirk_func_t)(struct snd_usb_audio *,
300 struct usb_interface *,
301 struct usb_driver *,
302 const struct snd_usb_audio_quirk *);
303 static const quirk_func_t quirk_funcs[] = {
304 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
305 [QUIRK_COMPOSITE] = create_composite_quirk,
306 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
307 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
308 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
309 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
310 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
Clemens Ladischc7f57212010-10-22 18:20:48 +0200311 [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100312 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
313 [QUIRK_MIDI_CME] = create_any_midi_quirk,
Krzysztof Foltman4434ade2010-05-20 20:31:10 +0100314 [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
Kristian Amlie1ef0e0a2011-08-26 13:19:49 +0200315 [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100316 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
317 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
318 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
Daniel Mack014950b2011-05-25 09:09:02 +0200319 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
320 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100321 };
322
323 if (quirk->type < QUIRK_TYPE_COUNT) {
324 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
325 } else {
326 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
327 return -ENXIO;
328 }
329}
330
331/*
332 * boot quirks
333 */
334
335#define EXTIGY_FIRMWARE_SIZE_OLD 794
336#define EXTIGY_FIRMWARE_SIZE_NEW 483
337
338static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
339{
340 struct usb_host_config *config = dev->actconfig;
341 int err;
342
343 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
344 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
345 snd_printdd("sending Extigy boot sequence...\n");
346 /* Send message to force it to reconnect with full interface. */
347 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200348 0x10, 0x43, 0x0001, 0x000a, NULL, 0);
Daniel Macke5779992010-03-04 19:46:13 +0100349 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
350 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
351 &dev->descriptor, sizeof(dev->descriptor));
352 config = dev->actconfig;
353 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
354 err = usb_reset_configuration(dev);
355 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
356 snd_printdd("extigy_boot: new boot length = %d\n",
357 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
358 return -ENODEV; /* quit this anyway */
359 }
360 return 0;
361}
362
363static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
364{
365 u8 buf = 1;
366
367 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
368 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200369 0, 0, &buf, 1);
Daniel Macke5779992010-03-04 19:46:13 +0100370 if (buf == 0) {
371 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
372 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200373 1, 2000, NULL, 0);
Daniel Macke5779992010-03-04 19:46:13 +0100374 return -ENODEV;
375 }
376 return 0;
377}
378
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200379static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
380{
381 int err;
382
383 if (dev->actconfig->desc.bConfigurationValue == 1) {
384 snd_printk(KERN_INFO "usb-audio: "
385 "Fast Track Pro switching to config #2\n");
386 /* This function has to be available by the usb core module.
387 * if it is not avialable the boot quirk has to be left out
388 * and the configuration has to be set by udev or hotplug
389 * rules
390 */
391 err = usb_driver_set_configuration(dev, 2);
David Henningssonb98ae272013-01-04 17:02:18 +0100392 if (err < 0)
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200393 snd_printdd("error usb_driver_set_configuration: %d\n",
394 err);
David Henningssonb98ae272013-01-04 17:02:18 +0100395 /* Always return an error, so that we stop creating a device
396 that will just be destroyed and recreated with a new
397 configuration */
398 return -ENODEV;
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200399 } else
400 snd_printk(KERN_INFO "usb-audio: Fast Track Pro config OK\n");
401
402 return 0;
403}
404
Daniel Macke5779992010-03-04 19:46:13 +0100405/*
406 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
407 * documented in the device's data sheet.
408 */
409static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
410{
411 u8 buf[4];
412 buf[0] = 0x20;
413 buf[1] = value & 0xff;
414 buf[2] = (value >> 8) & 0xff;
415 buf[3] = reg;
416 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
417 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200418 0, 0, &buf, 4);
Daniel Macke5779992010-03-04 19:46:13 +0100419}
420
421static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
422{
423 /*
424 * Enable line-out driver mode, set headphone source to front
425 * channels, enable stereo mic.
426 */
427 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
428}
429
430/*
431 * C-Media CM6206 is based on CM106 with two additional
432 * registers that are not documented in the data sheet.
433 * Values here are chosen based on sniffing USB traffic
434 * under Windows.
435 */
436static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
437{
Daniel Mackdac8f842011-08-06 00:23:18 +0200438 int err = 0, reg;
Eric Lammerts157186b2011-05-27 18:16:52 -0400439 int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
Daniel Macke5779992010-03-04 19:46:13 +0100440
441 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
442 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
443 if (err < 0)
444 return err;
445 }
446
447 return err;
448}
449
450/*
Mark Hills5e212332013-03-17 11:07:53 +0000451 * Novation Twitch DJ controller
452 */
453static int snd_usb_twitch_boot_quirk(struct usb_device *dev)
454{
455 /* preemptively set up the device because otherwise the
456 * raw MIDI endpoints are not active */
457 usb_set_interface(dev, 0, 1);
458 return 0;
459}
460
461/*
Daniel Macke5779992010-03-04 19:46:13 +0100462 * This call will put the synth in "USB send" mode, i.e it will send MIDI
463 * messages through USB (this is disabled at startup). The synth will
464 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
465 * sign on its LCD. Values here are chosen based on sniffing USB traffic
466 * under Windows.
467 */
468static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
469{
470 int err, actual_length;
471
472 /* "midi send" enable */
473 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
474
475 void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
476 if (!buf)
477 return -ENOMEM;
478 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
479 ARRAY_SIZE(seq), &actual_length, 1000);
480 kfree(buf);
481 if (err < 0)
482 return err;
483
484 return 0;
485}
486
487/*
Daniel Mack54a8c502011-02-11 11:08:06 +0000488 * Some sound cards from Native Instruments are in fact compliant to the USB
489 * audio standard of version 2 and other approved USB standards, even though
490 * they come up as vendor-specific device when first connected.
491 *
492 * However, they can be told to come up with a new set of descriptors
493 * upon their next enumeration, and the interfaces announced by the new
494 * descriptors will then be handled by the kernel's class drivers. As the
495 * product ID will also change, no further checks are required.
496 */
497
498static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
499{
500 int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
501 0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
502 cpu_to_le16(1), 0, NULL, 0, 1000);
503
504 if (ret < 0)
505 return ret;
506
507 usb_reset_device(dev);
508
509 /* return -EAGAIN, so the creation of an audio interface for this
510 * temporary device is aborted. The device will reconnect with a
511 * new product ID */
512 return -EAGAIN;
513}
514
Damien Zammitcb998642012-12-19 11:27:22 +0100515static void mbox2_setup_48_24_magic(struct usb_device *dev)
516{
517 u8 srate[3];
518 u8 temp[12];
519
520 /* Choose 48000Hz permanently */
521 srate[0] = 0x80;
522 srate[1] = 0xbb;
523 srate[2] = 0x00;
524
525 /* Send the magic! */
526 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
527 0x01, 0x22, 0x0100, 0x0085, &temp, 0x0003);
528 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
529 0x81, 0xa2, 0x0100, 0x0085, &srate, 0x0003);
530 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
531 0x81, 0xa2, 0x0100, 0x0086, &srate, 0x0003);
532 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
533 0x81, 0xa2, 0x0100, 0x0003, &srate, 0x0003);
534 return;
535}
536
537/* Digidesign Mbox 2 needs to load firmware onboard
538 * and driver must wait a few seconds for initialisation.
539 */
540
541#define MBOX2_FIRMWARE_SIZE 646
542#define MBOX2_BOOT_LOADING 0x01 /* Hard coded into the device */
543#define MBOX2_BOOT_READY 0x02 /* Hard coded into the device */
544
Damien Zammitb7b435e2013-01-04 09:51:44 +0100545static int snd_usb_mbox2_boot_quirk(struct usb_device *dev)
Damien Zammitcb998642012-12-19 11:27:22 +0100546{
547 struct usb_host_config *config = dev->actconfig;
548 int err;
Jiri Slaby4909a0c2013-02-17 14:33:04 +0100549 u8 bootresponse[0x12];
Damien Zammitcb998642012-12-19 11:27:22 +0100550 int fwsize;
551 int count;
552
553 fwsize = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
554
555 if (fwsize != MBOX2_FIRMWARE_SIZE) {
556 snd_printk(KERN_ERR "usb-audio: Invalid firmware size=%d.\n", fwsize);
557 return -ENODEV;
558 }
559
560 snd_printd("usb-audio: Sending Digidesign Mbox 2 boot sequence...\n");
561
562 count = 0;
Damien Zammitb7b435e2013-01-04 09:51:44 +0100563 bootresponse[0] = MBOX2_BOOT_LOADING;
564 while ((bootresponse[0] == MBOX2_BOOT_LOADING) && (count < 10)) {
Damien Zammitcb998642012-12-19 11:27:22 +0100565 msleep(500); /* 0.5 second delay */
566 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
567 /* Control magic - load onboard firmware */
568 0x85, 0xc0, 0x0001, 0x0000, &bootresponse, 0x0012);
Damien Zammitb7b435e2013-01-04 09:51:44 +0100569 if (bootresponse[0] == MBOX2_BOOT_READY)
Damien Zammitcb998642012-12-19 11:27:22 +0100570 break;
571 snd_printd("usb-audio: device not ready, resending boot sequence...\n");
572 count++;
573 }
574
Damien Zammitb7b435e2013-01-04 09:51:44 +0100575 if (bootresponse[0] != MBOX2_BOOT_READY) {
576 snd_printk(KERN_ERR "usb-audio: Unknown bootresponse=%d, or timed out, ignoring device.\n", bootresponse[0]);
Damien Zammitcb998642012-12-19 11:27:22 +0100577 return -ENODEV;
578 }
579
580 snd_printdd("usb-audio: device initialised!\n");
581
582 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
583 &dev->descriptor, sizeof(dev->descriptor));
584 config = dev->actconfig;
585 if (err < 0)
586 snd_printd("error usb_get_descriptor: %d\n", err);
587
588 err = usb_reset_configuration(dev);
589 if (err < 0)
590 snd_printd("error usb_reset_configuration: %d\n", err);
591 snd_printdd("mbox2_boot: new boot length = %d\n",
592 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
593
594 mbox2_setup_48_24_magic(dev);
595
596 snd_printk(KERN_INFO "usb-audio: Digidesign Mbox 2: 24bit 48kHz");
597
598 return 0; /* Successful boot */
599}
600
Daniel Mack54a8c502011-02-11 11:08:06 +0000601/*
Daniel Macke5779992010-03-04 19:46:13 +0100602 * Setup quirks
603 */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200604#define MAUDIO_SET 0x01 /* parse device_setup */
605#define MAUDIO_SET_COMPATIBLE 0x80 /* use only "win-compatible" interfaces */
606#define MAUDIO_SET_DTS 0x02 /* enable DTS Digital Output */
607#define MAUDIO_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
608#define MAUDIO_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
609#define MAUDIO_SET_DI 0x10 /* enable Digital Input */
610#define MAUDIO_SET_MASK 0x1f /* bit mask for setup value */
611#define MAUDIO_SET_24B_48K_DI 0x19 /* 24bits+48KHz+Digital Input */
612#define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
613#define MAUDIO_SET_16B_48K_DI 0x11 /* 16bits+48KHz+Digital Input */
614#define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
615
616static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
617 int iface, int altno)
618{
619 /* Reset ALL ifaces to 0 altsetting.
620 * Call it for every possible altsetting of every interface.
621 */
622 usb_set_interface(chip->dev, iface, 0);
623 if (chip->setup & MAUDIO_SET) {
624 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
625 if (iface != 1 && iface != 2)
626 return 1; /* skip all interfaces but 1 and 2 */
627 } else {
628 unsigned int mask;
629 if (iface == 1 || iface == 2)
630 return 1; /* skip interfaces 1 and 2 */
631 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
632 return 1; /* skip this altsetting */
633 mask = chip->setup & MAUDIO_SET_MASK;
634 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
635 return 1; /* skip this altsetting */
636 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
637 return 1; /* skip this altsetting */
638 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
639 return 1; /* skip this altsetting */
640 }
641 }
642 snd_printdd(KERN_INFO
643 "using altsetting %d for interface %d config %d\n",
644 altno, iface, chip->setup);
645 return 0; /* keep this altsetting */
646}
Daniel Macke5779992010-03-04 19:46:13 +0100647
648static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
649 int iface,
650 int altno)
651{
652 /* Reset ALL ifaces to 0 altsetting.
653 * Call it for every possible altsetting of every interface.
654 */
655 usb_set_interface(chip->dev, iface, 0);
656
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200657 if (chip->setup & MAUDIO_SET) {
658 unsigned int mask;
659 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
Daniel Macke5779992010-03-04 19:46:13 +0100660 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200661 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
Daniel Macke5779992010-03-04 19:46:13 +0100662 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200663 mask = chip->setup & MAUDIO_SET_MASK;
664 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
Daniel Macke5779992010-03-04 19:46:13 +0100665 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200666 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
Daniel Macke5779992010-03-04 19:46:13 +0100667 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200668 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
Daniel Macke5779992010-03-04 19:46:13 +0100669 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200670 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
Daniel Macke5779992010-03-04 19:46:13 +0100671 return 1; /* skip this altsetting */
672 }
673
674 return 0; /* keep this altsetting */
675}
676
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200677static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
678 int iface, int altno)
679{
680 /* Reset ALL ifaces to 0 altsetting.
681 * Call it for every possible altsetting of every interface.
682 */
683 usb_set_interface(chip->dev, iface, 0);
684
685 /* possible configuration where both inputs and only one output is
686 *used is not supported by the current setup
687 */
688 if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
689 if (chip->setup & MAUDIO_SET_96K) {
690 if (altno != 3 && altno != 6)
691 return 1;
692 } else if (chip->setup & MAUDIO_SET_DI) {
693 if (iface == 4)
694 return 1; /* no analog input */
695 if (altno != 2 && altno != 5)
696 return 1; /* enable only altsets 2 and 5 */
697 } else {
698 if (iface == 5)
699 return 1; /* disable digialt input */
700 if (altno != 2 && altno != 5)
701 return 1; /* enalbe only altsets 2 and 5 */
702 }
703 } else {
704 /* keep only 16-Bit mode */
705 if (altno != 1)
706 return 1;
707 }
708
709 snd_printdd(KERN_INFO
710 "using altsetting %d for interface %d config %d\n",
711 altno, iface, chip->setup);
712 return 0; /* keep this altsetting */
713}
714
Daniel Macke5779992010-03-04 19:46:13 +0100715int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
716 int iface,
717 int altno)
718{
719 /* audiophile usb: skip altsets incompatible with device_setup */
720 if (chip->usb_id == USB_ID(0x0763, 0x2003))
721 return audiophile_skip_setting_quirk(chip, iface, altno);
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200722 /* quattro usb: skip altsets incompatible with device_setup */
723 if (chip->usb_id == USB_ID(0x0763, 0x2001))
724 return quattro_skip_setting_quirk(chip, iface, altno);
725 /* fasttrackpro usb: skip altsets incompatible with device_setup */
726 if (chip->usb_id == USB_ID(0x0763, 0x2012))
727 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
Daniel Macke5779992010-03-04 19:46:13 +0100728
729 return 0;
730}
731
732int snd_usb_apply_boot_quirk(struct usb_device *dev,
733 struct usb_interface *intf,
734 const struct snd_usb_audio_quirk *quirk)
735{
736 u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
737 le16_to_cpu(dev->descriptor.idProduct));
738
Daniel Mack3347b262011-02-11 11:34:12 +0000739 switch (id) {
740 case USB_ID(0x041e, 0x3000):
741 /* SB Extigy needs special boot-up sequence */
742 /* if more models come, this will go to the quirk list. */
Daniel Macke5779992010-03-04 19:46:13 +0100743 return snd_usb_extigy_boot_quirk(dev, intf);
744
Daniel Mack3347b262011-02-11 11:34:12 +0000745 case USB_ID(0x041e, 0x3020):
746 /* SB Audigy 2 NX needs its own boot-up magic, too */
Daniel Macke5779992010-03-04 19:46:13 +0100747 return snd_usb_audigy2nx_boot_quirk(dev);
748
Daniel Mack3347b262011-02-11 11:34:12 +0000749 case USB_ID(0x10f5, 0x0200):
750 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
Daniel Macke5779992010-03-04 19:46:13 +0100751 return snd_usb_cm106_boot_quirk(dev);
752
Daniel Mack3347b262011-02-11 11:34:12 +0000753 case USB_ID(0x0d8c, 0x0102):
754 /* C-Media CM6206 / CM106-Like Sound Device */
Wolfgang Breyha8129e792011-04-28 16:18:40 +0200755 case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
Daniel Macke5779992010-03-04 19:46:13 +0100756 return snd_usb_cm6206_boot_quirk(dev);
757
Damien Zammitcb998642012-12-19 11:27:22 +0100758 case USB_ID(0x0dba, 0x3000):
759 /* Digidesign Mbox 2 */
760 return snd_usb_mbox2_boot_quirk(dev);
761
Mark Hills5e212332013-03-17 11:07:53 +0000762 case USB_ID(0x1235, 0x0018):
763 /* Focusrite Novation Twitch */
764 return snd_usb_twitch_boot_quirk(dev);
765
Daniel Mack3347b262011-02-11 11:34:12 +0000766 case USB_ID(0x133e, 0x0815):
767 /* Access Music VirusTI Desktop */
Daniel Macke5779992010-03-04 19:46:13 +0100768 return snd_usb_accessmusic_boot_quirk(dev);
769
Daniel Mack759e890f2011-05-18 11:28:41 +0200770 case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
Daniel Mack3347b262011-02-11 11:34:12 +0000771 case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
772 case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
Daniel Mack54a8c502011-02-11 11:08:06 +0000773 return snd_usb_nativeinstruments_boot_quirk(dev);
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200774 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
775 return snd_usb_fasttrackpro_boot_quirk(dev);
Daniel Mack3347b262011-02-11 11:34:12 +0000776 }
Daniel Mack54a8c502011-02-11 11:08:06 +0000777
Daniel Macke5779992010-03-04 19:46:13 +0100778 return 0;
779}
780
781/*
782 * check if the device uses big-endian samples
783 */
784int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
785{
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400786 /* it depends on altsetting whether the device is big-endian or not */
Daniel Macke5779992010-03-04 19:46:13 +0100787 switch (chip->usb_id) {
788 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200789 if (fp->altsetting == 2 || fp->altsetting == 3 ||
790 fp->altsetting == 5 || fp->altsetting == 6)
Daniel Macke5779992010-03-04 19:46:13 +0100791 return 1;
792 break;
793 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
794 if (chip->setup == 0x00 ||
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200795 fp->altsetting == 1 || fp->altsetting == 2 ||
796 fp->altsetting == 3)
Daniel Macke5779992010-03-04 19:46:13 +0100797 return 1;
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200798 break;
799 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
800 if (fp->altsetting == 2 || fp->altsetting == 3 ||
801 fp->altsetting == 5 || fp->altsetting == 6)
802 return 1;
803 break;
Daniel Macke5779992010-03-04 19:46:13 +0100804 }
805 return 0;
806}
807
808/*
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -0500809 * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
Daniel Macke5779992010-03-04 19:46:13 +0100810 * not for interface.
811 */
812
813enum {
814 EMU_QUIRK_SR_44100HZ = 0,
815 EMU_QUIRK_SR_48000HZ,
816 EMU_QUIRK_SR_88200HZ,
817 EMU_QUIRK_SR_96000HZ,
818 EMU_QUIRK_SR_176400HZ,
819 EMU_QUIRK_SR_192000HZ
820};
821
822static void set_format_emu_quirk(struct snd_usb_substream *subs,
823 struct audioformat *fmt)
824{
825 unsigned char emu_samplerate_id = 0;
826
827 /* When capture is active
828 * sample rate shouldn't be changed
829 * by playback substream
830 */
831 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
832 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
833 return;
834 }
835
836 switch (fmt->rate_min) {
837 case 48000:
838 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
839 break;
840 case 88200:
841 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
842 break;
843 case 96000:
844 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
845 break;
846 case 176400:
847 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
848 break;
849 case 192000:
850 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
851 break;
852 default:
853 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
854 break;
855 }
856 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
857}
858
859void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
860 struct audioformat *fmt)
861{
862 switch (subs->stream->chip->usb_id) {
863 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
864 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
865 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -0500866 case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
Daniel Macke5779992010-03-04 19:46:13 +0100867 set_format_emu_quirk(subs, fmt);
868 break;
869 }
870}
871
Daniel Mack2b58fd52012-09-04 10:23:07 +0200872void snd_usb_endpoint_start_quirk(struct snd_usb_endpoint *ep)
873{
874 /*
875 * "Playback Design" products send bogus feedback data at the start
876 * of the stream. Ignore them.
877 */
878 if ((le16_to_cpu(ep->chip->dev->descriptor.idVendor) == 0x23ba) &&
879 ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
880 ep->skip_packets = 4;
Eldad Zack83e3acd2013-01-13 23:02:03 +0100881
882 /*
Matt Gruskine9a25e02013-02-09 12:56:35 -0500883 * M-Audio Fast Track C400/C600 - when packets are not skipped, real
884 * world latency varies by approx. +/- 50 frames (at 96KHz) each time
885 * the stream is (re)started. When skipping packets 16 at endpoint
886 * start up, the real world latency is stable within +/- 1 frame (also
Eldad Zack83e3acd2013-01-13 23:02:03 +0100887 * across power cycles).
888 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500889 if ((ep->chip->usb_id == USB_ID(0x0763, 0x2030) ||
890 ep->chip->usb_id == USB_ID(0x0763, 0x2031)) &&
Eldad Zack83e3acd2013-01-13 23:02:03 +0100891 ep->type == SND_USB_ENDPOINT_TYPE_DATA)
892 ep->skip_packets = 16;
Daniel Mack2b58fd52012-09-04 10:23:07 +0200893}
894
895void snd_usb_ctl_msg_quirk(struct usb_device *dev, unsigned int pipe,
896 __u8 request, __u8 requesttype, __u16 value,
897 __u16 index, void *data, __u16 size)
898{
899 /*
900 * "Playback Design" products need a 20ms delay after each
901 * class compliant request
902 */
903 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x23ba) &&
904 (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
905 mdelay(20);
906}
907