blob: f54221d0dddfdf34354c8e9831ba600e1d8d347c [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 }
Vladis Dronov810eca42016-03-31 12:05:43 -0400139 INIT_LIST_HEAD(&fp->list);
Xi Wang8866f402012-02-14 05:18:48 -0500140 if (fp->nr_rates > MAX_NR_RATES) {
141 kfree(fp);
142 return -EINVAL;
143 }
Daniel Macke5779992010-03-04 19:46:13 +0100144 if (fp->nr_rates > 0) {
Thomas Meyer43df2a52011-11-10 19:38:43 +0100145 rate_table = kmemdup(fp->rate_table,
146 sizeof(int) * fp->nr_rates, GFP_KERNEL);
Daniel Macke5779992010-03-04 19:46:13 +0100147 if (!rate_table) {
148 kfree(fp);
149 return -ENOMEM;
150 }
Daniel Macke5779992010-03-04 19:46:13 +0100151 fp->rate_table = rate_table;
152 }
153
154 stream = (fp->endpoint & USB_DIR_IN)
155 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200156 err = snd_usb_add_audio_stream(chip, stream, fp);
Takashi Iwai26982a22016-03-15 12:14:49 +0100157 if (err < 0)
158 goto error;
Daniel Macke5779992010-03-04 19:46:13 +0100159 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
160 fp->altset_idx >= iface->num_altsetting) {
Takashi Iwai26982a22016-03-15 12:14:49 +0100161 err = -EINVAL;
162 goto error;
Daniel Macke5779992010-03-04 19:46:13 +0100163 }
164 alts = &iface->altsetting[fp->altset_idx];
165 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
166 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
167 usb_set_interface(chip->dev, fp->iface, 0);
Daniel Mack767d75a2010-03-04 19:46:17 +0100168 snd_usb_init_pitch(chip, fp->iface, alts, fp);
169 snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
Daniel Macke5779992010-03-04 19:46:13 +0100170 return 0;
Takashi Iwai26982a22016-03-15 12:14:49 +0100171
172 error:
Vladis Dronov810eca42016-03-31 12:05:43 -0400173 list_del(&fp->list); /* unlink for avoiding double-free */
Takashi Iwai26982a22016-03-15 12:14:49 +0100174 kfree(fp);
175 kfree(rate_table);
176 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100177}
178
179/*
180 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
181 * The only way to detect the sample rate is by looking at wMaxPacketSize.
182 */
183static int create_uaxx_quirk(struct snd_usb_audio *chip,
184 struct usb_interface *iface,
185 struct usb_driver *driver,
186 const struct snd_usb_audio_quirk *quirk)
187{
188 static const struct audioformat ua_format = {
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100189 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
Daniel Macke5779992010-03-04 19:46:13 +0100190 .channels = 2,
191 .fmt_type = UAC_FORMAT_TYPE_I,
192 .altsetting = 1,
193 .altset_idx = 1,
194 .rates = SNDRV_PCM_RATE_CONTINUOUS,
195 };
196 struct usb_host_interface *alts;
197 struct usb_interface_descriptor *altsd;
198 struct audioformat *fp;
199 int stream, err;
200
201 /* both PCM and MIDI interfaces have 2 or more altsettings */
202 if (iface->num_altsetting < 2)
203 return -ENXIO;
204 alts = &iface->altsetting[1];
205 altsd = get_iface_desc(alts);
206
207 if (altsd->bNumEndpoints == 2) {
208 static const struct snd_usb_midi_endpoint_info ua700_ep = {
209 .out_cables = 0x0003,
210 .in_cables = 0x0003
211 };
212 static const struct snd_usb_audio_quirk ua700_quirk = {
213 .type = QUIRK_MIDI_FIXED_ENDPOINT,
214 .data = &ua700_ep
215 };
216 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
217 .out_cables = 0x0001,
218 .in_cables = 0x0001
219 };
220 static const struct snd_usb_audio_quirk uaxx_quirk = {
221 .type = QUIRK_MIDI_FIXED_ENDPOINT,
222 .data = &uaxx_ep
223 };
224 const struct snd_usb_audio_quirk *quirk =
225 chip->usb_id == USB_ID(0x0582, 0x002b)
226 ? &ua700_quirk : &uaxx_quirk;
227 return snd_usbmidi_create(chip->card, iface,
228 &chip->midi_list, quirk);
229 }
230
231 if (altsd->bNumEndpoints != 1)
232 return -ENXIO;
233
Thomas Meyer43df2a52011-11-10 19:38:43 +0100234 fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
Daniel Macke5779992010-03-04 19:46:13 +0100235 if (!fp)
236 return -ENOMEM;
Daniel Macke5779992010-03-04 19:46:13 +0100237
238 fp->iface = altsd->bInterfaceNumber;
239 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
240 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
241 fp->datainterval = 0;
242 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Vladis Dronov810eca42016-03-31 12:05:43 -0400243 INIT_LIST_HEAD(&fp->list);
Daniel Macke5779992010-03-04 19:46:13 +0100244
245 switch (fp->maxpacksize) {
246 case 0x120:
247 fp->rate_max = fp->rate_min = 44100;
248 break;
249 case 0x138:
250 case 0x140:
251 fp->rate_max = fp->rate_min = 48000;
252 break;
253 case 0x258:
254 case 0x260:
255 fp->rate_max = fp->rate_min = 96000;
256 break;
257 default:
258 snd_printk(KERN_ERR "unknown sample rate\n");
259 kfree(fp);
260 return -ENXIO;
261 }
262
263 stream = (fp->endpoint & USB_DIR_IN)
264 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200265 err = snd_usb_add_audio_stream(chip, stream, fp);
Daniel Macke5779992010-03-04 19:46:13 +0100266 if (err < 0) {
Vladis Dronov810eca42016-03-31 12:05:43 -0400267 list_del(&fp->list); /* unlink for avoiding double-free */
Daniel Macke5779992010-03-04 19:46:13 +0100268 kfree(fp);
269 return err;
270 }
271 usb_set_interface(chip->dev, fp->iface, 0);
272 return 0;
273}
274
275/*
Daniel Mack014950b2011-05-25 09:09:02 +0200276 * Create a standard mixer for the specified interface.
277 */
278static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
279 struct usb_interface *iface,
280 struct usb_driver *driver,
281 const struct snd_usb_audio_quirk *quirk)
282{
283 if (quirk->ifnum < 0)
284 return 0;
285
286 return snd_usb_create_mixer(chip, quirk->ifnum, 0);
287}
288
289/*
Daniel Macke5779992010-03-04 19:46:13 +0100290 * audio-interface quirks
291 *
292 * returns zero if no standard audio/MIDI parsing is needed.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300293 * returns a positive value if standard audio/midi interfaces are parsed
Daniel Macke5779992010-03-04 19:46:13 +0100294 * after this.
295 * returns a negative value at error.
296 */
297int snd_usb_create_quirk(struct snd_usb_audio *chip,
298 struct usb_interface *iface,
299 struct usb_driver *driver,
300 const struct snd_usb_audio_quirk *quirk)
301{
302 typedef int (*quirk_func_t)(struct snd_usb_audio *,
303 struct usb_interface *,
304 struct usb_driver *,
305 const struct snd_usb_audio_quirk *);
306 static const quirk_func_t quirk_funcs[] = {
307 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
308 [QUIRK_COMPOSITE] = create_composite_quirk,
309 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
310 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
311 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
312 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
313 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
Clemens Ladischc7f57212010-10-22 18:20:48 +0200314 [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100315 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
316 [QUIRK_MIDI_CME] = create_any_midi_quirk,
Krzysztof Foltman4434ade2010-05-20 20:31:10 +0100317 [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
Kristian Amlie1ef0e0a2011-08-26 13:19:49 +0200318 [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100319 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
320 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
321 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
Daniel Mack014950b2011-05-25 09:09:02 +0200322 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
323 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
Daniel Macke5779992010-03-04 19:46:13 +0100324 };
325
326 if (quirk->type < QUIRK_TYPE_COUNT) {
327 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
328 } else {
329 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
330 return -ENXIO;
331 }
332}
333
334/*
335 * boot quirks
336 */
337
338#define EXTIGY_FIRMWARE_SIZE_OLD 794
339#define EXTIGY_FIRMWARE_SIZE_NEW 483
340
341static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
342{
343 struct usb_host_config *config = dev->actconfig;
344 int err;
345
346 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
347 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
348 snd_printdd("sending Extigy boot sequence...\n");
349 /* Send message to force it to reconnect with full interface. */
350 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200351 0x10, 0x43, 0x0001, 0x000a, NULL, 0);
Daniel Macke5779992010-03-04 19:46:13 +0100352 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
353 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
354 &dev->descriptor, sizeof(dev->descriptor));
355 config = dev->actconfig;
356 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
357 err = usb_reset_configuration(dev);
358 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
359 snd_printdd("extigy_boot: new boot length = %d\n",
360 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
361 return -ENODEV; /* quit this anyway */
362 }
363 return 0;
364}
365
366static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
367{
368 u8 buf = 1;
369
370 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
371 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200372 0, 0, &buf, 1);
Daniel Macke5779992010-03-04 19:46:13 +0100373 if (buf == 0) {
374 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
375 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200376 1, 2000, NULL, 0);
Daniel Macke5779992010-03-04 19:46:13 +0100377 return -ENODEV;
378 }
379 return 0;
380}
381
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200382static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
383{
384 int err;
385
386 if (dev->actconfig->desc.bConfigurationValue == 1) {
387 snd_printk(KERN_INFO "usb-audio: "
388 "Fast Track Pro switching to config #2\n");
389 /* This function has to be available by the usb core module.
390 * if it is not avialable the boot quirk has to be left out
391 * and the configuration has to be set by udev or hotplug
392 * rules
393 */
394 err = usb_driver_set_configuration(dev, 2);
395 if (err < 0) {
396 snd_printdd("error usb_driver_set_configuration: %d\n",
397 err);
398 return -ENODEV;
399 }
400 } else
401 snd_printk(KERN_INFO "usb-audio: Fast Track Pro config OK\n");
402
403 return 0;
404}
405
Daniel Macke5779992010-03-04 19:46:13 +0100406/*
407 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
408 * documented in the device's data sheet.
409 */
410static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
411{
412 u8 buf[4];
413 buf[0] = 0x20;
414 buf[1] = value & 0xff;
415 buf[2] = (value >> 8) & 0xff;
416 buf[3] = reg;
417 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
418 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200419 0, 0, &buf, 4);
Daniel Macke5779992010-03-04 19:46:13 +0100420}
421
422static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
423{
424 /*
425 * Enable line-out driver mode, set headphone source to front
426 * channels, enable stereo mic.
427 */
428 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
429}
430
431/*
432 * C-Media CM6206 is based on CM106 with two additional
433 * registers that are not documented in the data sheet.
434 * Values here are chosen based on sniffing USB traffic
435 * under Windows.
436 */
437static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
438{
Daniel Mackdac8f842011-08-06 00:23:18 +0200439 int err = 0, reg;
Eric Lammerts157186b2011-05-27 18:16:52 -0400440 int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
Daniel Macke5779992010-03-04 19:46:13 +0100441
442 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
443 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
444 if (err < 0)
445 return err;
446 }
447
448 return err;
449}
450
451/*
452 * This call will put the synth in "USB send" mode, i.e it will send MIDI
453 * messages through USB (this is disabled at startup). The synth will
454 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
455 * sign on its LCD. Values here are chosen based on sniffing USB traffic
456 * under Windows.
457 */
458static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
459{
460 int err, actual_length;
461
462 /* "midi send" enable */
463 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
464
465 void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
466 if (!buf)
467 return -ENOMEM;
468 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
469 ARRAY_SIZE(seq), &actual_length, 1000);
470 kfree(buf);
471 if (err < 0)
472 return err;
473
474 return 0;
475}
476
477/*
Daniel Mack54a8c502011-02-11 11:08:06 +0000478 * Some sound cards from Native Instruments are in fact compliant to the USB
479 * audio standard of version 2 and other approved USB standards, even though
480 * they come up as vendor-specific device when first connected.
481 *
482 * However, they can be told to come up with a new set of descriptors
483 * upon their next enumeration, and the interfaces announced by the new
484 * descriptors will then be handled by the kernel's class drivers. As the
485 * product ID will also change, no further checks are required.
486 */
487
488static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
489{
490 int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
491 0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
492 cpu_to_le16(1), 0, NULL, 0, 1000);
493
494 if (ret < 0)
495 return ret;
496
497 usb_reset_device(dev);
498
499 /* return -EAGAIN, so the creation of an audio interface for this
500 * temporary device is aborted. The device will reconnect with a
501 * new product ID */
502 return -EAGAIN;
503}
504
505/*
Daniel Macke5779992010-03-04 19:46:13 +0100506 * Setup quirks
507 */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200508#define MAUDIO_SET 0x01 /* parse device_setup */
509#define MAUDIO_SET_COMPATIBLE 0x80 /* use only "win-compatible" interfaces */
510#define MAUDIO_SET_DTS 0x02 /* enable DTS Digital Output */
511#define MAUDIO_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
512#define MAUDIO_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
513#define MAUDIO_SET_DI 0x10 /* enable Digital Input */
514#define MAUDIO_SET_MASK 0x1f /* bit mask for setup value */
515#define MAUDIO_SET_24B_48K_DI 0x19 /* 24bits+48KHz+Digital Input */
516#define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
517#define MAUDIO_SET_16B_48K_DI 0x11 /* 16bits+48KHz+Digital Input */
518#define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
519
520static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
521 int iface, int altno)
522{
523 /* Reset ALL ifaces to 0 altsetting.
524 * Call it for every possible altsetting of every interface.
525 */
526 usb_set_interface(chip->dev, iface, 0);
527 if (chip->setup & MAUDIO_SET) {
528 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
529 if (iface != 1 && iface != 2)
530 return 1; /* skip all interfaces but 1 and 2 */
531 } else {
532 unsigned int mask;
533 if (iface == 1 || iface == 2)
534 return 1; /* skip interfaces 1 and 2 */
535 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
536 return 1; /* skip this altsetting */
537 mask = chip->setup & MAUDIO_SET_MASK;
538 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
539 return 1; /* skip this altsetting */
540 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
541 return 1; /* skip this altsetting */
542 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
543 return 1; /* skip this altsetting */
544 }
545 }
546 snd_printdd(KERN_INFO
547 "using altsetting %d for interface %d config %d\n",
548 altno, iface, chip->setup);
549 return 0; /* keep this altsetting */
550}
Daniel Macke5779992010-03-04 19:46:13 +0100551
552static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
553 int iface,
554 int altno)
555{
556 /* Reset ALL ifaces to 0 altsetting.
557 * Call it for every possible altsetting of every interface.
558 */
559 usb_set_interface(chip->dev, iface, 0);
560
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200561 if (chip->setup & MAUDIO_SET) {
562 unsigned int mask;
563 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
Daniel Macke5779992010-03-04 19:46:13 +0100564 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200565 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
Daniel Macke5779992010-03-04 19:46:13 +0100566 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200567 mask = chip->setup & MAUDIO_SET_MASK;
568 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
Daniel Macke5779992010-03-04 19:46:13 +0100569 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200570 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
Daniel Macke5779992010-03-04 19:46:13 +0100571 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200572 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
Daniel Macke5779992010-03-04 19:46:13 +0100573 return 1; /* skip this altsetting */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200574 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
Daniel Macke5779992010-03-04 19:46:13 +0100575 return 1; /* skip this altsetting */
576 }
577
578 return 0; /* keep this altsetting */
579}
580
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200581
582static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
583 int iface, int altno)
584{
585 /* Reset ALL ifaces to 0 altsetting.
586 * Call it for every possible altsetting of every interface.
587 */
588 usb_set_interface(chip->dev, iface, 0);
589
590 /* possible configuration where both inputs and only one output is
591 *used is not supported by the current setup
592 */
593 if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
594 if (chip->setup & MAUDIO_SET_96K) {
595 if (altno != 3 && altno != 6)
596 return 1;
597 } else if (chip->setup & MAUDIO_SET_DI) {
598 if (iface == 4)
599 return 1; /* no analog input */
600 if (altno != 2 && altno != 5)
601 return 1; /* enable only altsets 2 and 5 */
602 } else {
603 if (iface == 5)
604 return 1; /* disable digialt input */
605 if (altno != 2 && altno != 5)
606 return 1; /* enalbe only altsets 2 and 5 */
607 }
608 } else {
609 /* keep only 16-Bit mode */
610 if (altno != 1)
611 return 1;
612 }
613
614 snd_printdd(KERN_INFO
615 "using altsetting %d for interface %d config %d\n",
616 altno, iface, chip->setup);
617 return 0; /* keep this altsetting */
618}
619
Daniel Macke5779992010-03-04 19:46:13 +0100620int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
621 int iface,
622 int altno)
623{
624 /* audiophile usb: skip altsets incompatible with device_setup */
625 if (chip->usb_id == USB_ID(0x0763, 0x2003))
626 return audiophile_skip_setting_quirk(chip, iface, altno);
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200627 /* quattro usb: skip altsets incompatible with device_setup */
628 if (chip->usb_id == USB_ID(0x0763, 0x2001))
629 return quattro_skip_setting_quirk(chip, iface, altno);
630 /* fasttrackpro usb: skip altsets incompatible with device_setup */
631 if (chip->usb_id == USB_ID(0x0763, 0x2012))
632 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
Daniel Macke5779992010-03-04 19:46:13 +0100633
634 return 0;
635}
636
637int snd_usb_apply_boot_quirk(struct usb_device *dev,
638 struct usb_interface *intf,
639 const struct snd_usb_audio_quirk *quirk)
640{
641 u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
642 le16_to_cpu(dev->descriptor.idProduct));
643
Daniel Mack3347b262011-02-11 11:34:12 +0000644 switch (id) {
645 case USB_ID(0x041e, 0x3000):
646 /* SB Extigy needs special boot-up sequence */
647 /* if more models come, this will go to the quirk list. */
Daniel Macke5779992010-03-04 19:46:13 +0100648 return snd_usb_extigy_boot_quirk(dev, intf);
649
Daniel Mack3347b262011-02-11 11:34:12 +0000650 case USB_ID(0x041e, 0x3020):
651 /* SB Audigy 2 NX needs its own boot-up magic, too */
Daniel Macke5779992010-03-04 19:46:13 +0100652 return snd_usb_audigy2nx_boot_quirk(dev);
653
Daniel Mack3347b262011-02-11 11:34:12 +0000654 case USB_ID(0x10f5, 0x0200):
655 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
Daniel Macke5779992010-03-04 19:46:13 +0100656 return snd_usb_cm106_boot_quirk(dev);
657
Daniel Mack3347b262011-02-11 11:34:12 +0000658 case USB_ID(0x0d8c, 0x0102):
659 /* C-Media CM6206 / CM106-Like Sound Device */
Wolfgang Breyha8129e792011-04-28 16:18:40 +0200660 case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
Daniel Macke5779992010-03-04 19:46:13 +0100661 return snd_usb_cm6206_boot_quirk(dev);
662
Daniel Mack3347b262011-02-11 11:34:12 +0000663 case USB_ID(0x133e, 0x0815):
664 /* Access Music VirusTI Desktop */
Daniel Macke5779992010-03-04 19:46:13 +0100665 return snd_usb_accessmusic_boot_quirk(dev);
666
Daniel Mack759e8902011-05-18 11:28:41 +0200667 case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
Daniel Mack3347b262011-02-11 11:34:12 +0000668 case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
669 case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
Daniel Mack54a8c502011-02-11 11:08:06 +0000670 return snd_usb_nativeinstruments_boot_quirk(dev);
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200671 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
672 return snd_usb_fasttrackpro_boot_quirk(dev);
Daniel Mack3347b262011-02-11 11:34:12 +0000673 }
Daniel Mack54a8c502011-02-11 11:08:06 +0000674
Daniel Macke5779992010-03-04 19:46:13 +0100675 return 0;
676}
677
678/*
679 * check if the device uses big-endian samples
680 */
681int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
682{
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200683 /* it depends on altsetting wether the device is big-endian or not */
Daniel Macke5779992010-03-04 19:46:13 +0100684 switch (chip->usb_id) {
685 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200686 if (fp->altsetting == 2 || fp->altsetting == 3 ||
687 fp->altsetting == 5 || fp->altsetting == 6)
Daniel Macke5779992010-03-04 19:46:13 +0100688 return 1;
689 break;
690 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
691 if (chip->setup == 0x00 ||
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200692 fp->altsetting == 1 || fp->altsetting == 2 ||
693 fp->altsetting == 3)
Daniel Macke5779992010-03-04 19:46:13 +0100694 return 1;
Guillaume Pellerin0f5733b2011-07-12 18:13:46 +0200695 break;
696 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
697 if (fp->altsetting == 2 || fp->altsetting == 3 ||
698 fp->altsetting == 5 || fp->altsetting == 6)
699 return 1;
700 break;
Daniel Macke5779992010-03-04 19:46:13 +0100701 }
702 return 0;
703}
704
705/*
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -0500706 * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
Daniel Macke5779992010-03-04 19:46:13 +0100707 * not for interface.
708 */
709
710enum {
711 EMU_QUIRK_SR_44100HZ = 0,
712 EMU_QUIRK_SR_48000HZ,
713 EMU_QUIRK_SR_88200HZ,
714 EMU_QUIRK_SR_96000HZ,
715 EMU_QUIRK_SR_176400HZ,
716 EMU_QUIRK_SR_192000HZ
717};
718
719static void set_format_emu_quirk(struct snd_usb_substream *subs,
720 struct audioformat *fmt)
721{
722 unsigned char emu_samplerate_id = 0;
723
724 /* When capture is active
725 * sample rate shouldn't be changed
726 * by playback substream
727 */
728 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
729 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
730 return;
731 }
732
733 switch (fmt->rate_min) {
734 case 48000:
735 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
736 break;
737 case 88200:
738 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
739 break;
740 case 96000:
741 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
742 break;
743 case 176400:
744 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
745 break;
746 case 192000:
747 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
748 break;
749 default:
750 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
751 break;
752 }
753 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
754}
755
756void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
757 struct audioformat *fmt)
758{
759 switch (subs->stream->chip->usb_id) {
760 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
761 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
762 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
Joseph Teichman1cdfa9f2011-02-08 01:22:36 -0500763 case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
Daniel Macke5779992010-03-04 19:46:13 +0100764 set_format_emu_quirk(subs, fmt);
765 break;
766 }
767}
768