Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / sound / usb / quirks.c
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>
18 #include <linux/slab.h>
19 #include <linux/usb.h>
20 #include <linux/usb/audio.h>
21
22 #include <sound/control.h>
23 #include <sound/core.h>
24 #include <sound/info.h>
25 #include <sound/pcm.h>
26
27 #include "usbaudio.h"
28 #include "card.h"
29 #include "mixer.h"
30 #include "mixer_quirks.h"
31 #include "midi.h"
32 #include "quirks.h"
33 #include "helper.h"
34 #include "endpoint.h"
35 #include "pcm.h"
36 #include "clock.h"
37 #include "stream.h"
38
39 /*
40  * handle the quirks for the contained interfaces
41  */
42 static 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
66 static 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  */
79 static 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
88 static 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  */
99 static 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);
110         err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
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  */
124 static 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);
135         if (!fp) {
136                 snd_printk(KERN_ERR "cannot memdup\n");
137                 return -ENOMEM;
138         }
139         if (fp->nr_rates > MAX_NR_RATES) {
140                 kfree(fp);
141                 return -EINVAL;
142         }
143         if (fp->nr_rates > 0) {
144                 rate_table = kmemdup(fp->rate_table,
145                                      sizeof(int) * fp->nr_rates, GFP_KERNEL);
146                 if (!rate_table) {
147                         kfree(fp);
148                         return -ENOMEM;
149                 }
150                 fp->rate_table = rate_table;
151         }
152
153         stream = (fp->endpoint & USB_DIR_IN)
154                 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
155         err = snd_usb_add_audio_stream(chip, stream, fp);
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);
171         snd_usb_init_pitch(chip, fp->iface, alts, fp);
172         snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
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  */
180 static 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 = {
186                 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
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
231         fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
232         if (!fp)
233                 return -ENOMEM;
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;
261         err = snd_usb_add_audio_stream(chip, stream, fp);
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
270 /*
271  * Create a standard mixer for the specified interface.
272  */
273 static 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
284 /*
285  * audio-interface quirks
286  *
287  * returns zero if no standard audio/MIDI parsing is needed.
288  * returns a positive value if standard audio/midi interfaces are parsed
289  * after this.
290  * returns a negative value at error.
291  */
292 int 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,
309                 [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
310                 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
311                 [QUIRK_MIDI_CME] = create_any_midi_quirk,
312                 [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
313                 [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
314                 [QUIRK_MIDI_CH345] = create_any_midi_quirk,
315                 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
316                 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
317                 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
318                 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
319                 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
320         };
321
322         if (quirk->type < QUIRK_TYPE_COUNT) {
323                 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
324         } else {
325                 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
326                 return -ENXIO;
327         }
328 }
329
330 /*
331  * boot quirks
332  */
333
334 #define EXTIGY_FIRMWARE_SIZE_OLD 794
335 #define EXTIGY_FIRMWARE_SIZE_NEW 483
336
337 static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
338 {
339         struct usb_host_config *config = dev->actconfig;
340         int err;
341
342         if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
343             le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
344                 snd_printdd("sending Extigy boot sequence...\n");
345                 /* Send message to force it to reconnect with full interface. */
346                 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
347                                       0x10, 0x43, 0x0001, 0x000a, NULL, 0);
348                 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
349                 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
350                                 &dev->descriptor, sizeof(dev->descriptor));
351                 config = dev->actconfig;
352                 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
353                 err = usb_reset_configuration(dev);
354                 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
355                 snd_printdd("extigy_boot: new boot length = %d\n",
356                             le16_to_cpu(get_cfg_desc(config)->wTotalLength));
357                 return -ENODEV; /* quit this anyway */
358         }
359         return 0;
360 }
361
362 static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
363 {
364         u8 buf = 1;
365
366         snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
367                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
368                         0, 0, &buf, 1);
369         if (buf == 0) {
370                 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
371                                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
372                                 1, 2000, NULL, 0);
373                 return -ENODEV;
374         }
375         return 0;
376 }
377
378 static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
379 {
380         int err;
381
382         if (dev->actconfig->desc.bConfigurationValue == 1) {
383                 snd_printk(KERN_INFO "usb-audio: "
384                            "Fast Track Pro switching to config #2\n");
385                 /* This function has to be available by the usb core module.
386                  * if it is not avialable the boot quirk has to be left out
387                  * and the configuration has to be set by udev or hotplug
388                  * rules
389                  */
390                 err = usb_driver_set_configuration(dev, 2);
391                 if (err < 0)
392                         snd_printdd("error usb_driver_set_configuration: %d\n",
393                                     err);
394                 /* Always return an error, so that we stop creating a device
395                    that will just be destroyed and recreated with a new
396                    configuration */
397                 return -ENODEV;
398         } else
399                 snd_printk(KERN_INFO "usb-audio: Fast Track Pro config OK\n");
400
401         return 0;
402 }
403
404 /*
405  * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
406  * documented in the device's data sheet.
407  */
408 static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
409 {
410         u8 buf[4];
411         buf[0] = 0x20;
412         buf[1] = value & 0xff;
413         buf[2] = (value >> 8) & 0xff;
414         buf[3] = reg;
415         return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
416                                USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
417                                0, 0, &buf, 4);
418 }
419
420 static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
421 {
422         /*
423          * Enable line-out driver mode, set headphone source to front
424          * channels, enable stereo mic.
425          */
426         return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
427 }
428
429 /*
430  * C-Media CM6206 is based on CM106 with two additional
431  * registers that are not documented in the data sheet.
432  * Values here are chosen based on sniffing USB traffic
433  * under Windows.
434  */
435 static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
436 {
437         int err  = 0, reg;
438         int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
439
440         for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
441                 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
442                 if (err < 0)
443                         return err;
444         }
445
446         return err;
447 }
448
449 /*
450  * This call will put the synth in "USB send" mode, i.e it will send MIDI
451  * messages through USB (this is disabled at startup). The synth will
452  * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
453  * sign on its LCD. Values here are chosen based on sniffing USB traffic
454  * under Windows.
455  */
456 static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
457 {
458         int err, actual_length;
459
460         /* "midi send" enable */
461         static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
462
463         void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
464         if (!buf)
465                 return -ENOMEM;
466         err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
467                         ARRAY_SIZE(seq), &actual_length, 1000);
468         kfree(buf);
469         if (err < 0)
470                 return err;
471
472         return 0;
473 }
474
475 /*
476  * Some sound cards from Native Instruments are in fact compliant to the USB
477  * audio standard of version 2 and other approved USB standards, even though
478  * they come up as vendor-specific device when first connected.
479  *
480  * However, they can be told to come up with a new set of descriptors
481  * upon their next enumeration, and the interfaces announced by the new
482  * descriptors will then be handled by the kernel's class drivers. As the
483  * product ID will also change, no further checks are required.
484  */
485
486 static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
487 {
488         int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
489                                   0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
490                                   1, 0, NULL, 0, 1000);
491
492         if (ret < 0)
493                 return ret;
494
495         usb_reset_device(dev);
496
497         /* return -EAGAIN, so the creation of an audio interface for this
498          * temporary device is aborted. The device will reconnect with a
499          * new product ID */
500         return -EAGAIN;
501 }
502
503 /*
504  * Setup quirks
505  */
506 #define MAUDIO_SET              0x01 /* parse device_setup */
507 #define MAUDIO_SET_COMPATIBLE   0x80 /* use only "win-compatible" interfaces */
508 #define MAUDIO_SET_DTS          0x02 /* enable DTS Digital Output */
509 #define MAUDIO_SET_96K          0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
510 #define MAUDIO_SET_24B          0x08 /* 24bits sample if set, 16bits otherwise */
511 #define MAUDIO_SET_DI           0x10 /* enable Digital Input */
512 #define MAUDIO_SET_MASK         0x1f /* bit mask for setup value */
513 #define MAUDIO_SET_24B_48K_DI    0x19 /* 24bits+48KHz+Digital Input */
514 #define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
515 #define MAUDIO_SET_16B_48K_DI    0x11 /* 16bits+48KHz+Digital Input */
516 #define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
517
518 static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
519                                       int iface, int altno)
520 {
521         /* Reset ALL ifaces to 0 altsetting.
522          * Call it for every possible altsetting of every interface.
523          */
524         usb_set_interface(chip->dev, iface, 0);
525         if (chip->setup & MAUDIO_SET) {
526                 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
527                         if (iface != 1 && iface != 2)
528                                 return 1; /* skip all interfaces but 1 and 2 */
529                 } else {
530                         unsigned int mask;
531                         if (iface == 1 || iface == 2)
532                                 return 1; /* skip interfaces 1 and 2 */
533                         if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
534                                 return 1; /* skip this altsetting */
535                         mask = chip->setup & MAUDIO_SET_MASK;
536                         if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
537                                 return 1; /* skip this altsetting */
538                         if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
539                                 return 1; /* skip this altsetting */
540                         if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
541                                 return 1; /* skip this altsetting */
542                 }
543         }
544         snd_printdd(KERN_INFO
545                     "using altsetting %d for interface %d config %d\n",
546                     altno, iface, chip->setup);
547         return 0; /* keep this altsetting */
548 }
549
550 static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
551                                          int iface,
552                                          int altno)
553 {
554         /* Reset ALL ifaces to 0 altsetting.
555          * Call it for every possible altsetting of every interface.
556          */
557         usb_set_interface(chip->dev, iface, 0);
558
559         if (chip->setup & MAUDIO_SET) {
560                 unsigned int mask;
561                 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
562                         return 1; /* skip this altsetting */
563                 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
564                         return 1; /* skip this altsetting */
565                 mask = chip->setup & MAUDIO_SET_MASK;
566                 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
567                         return 1; /* skip this altsetting */
568                 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
569                         return 1; /* skip this altsetting */
570                 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
571                         return 1; /* skip this altsetting */
572                 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
573                         return 1; /* skip this altsetting */
574         }
575
576         return 0; /* keep this altsetting */
577 }
578
579
580 static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
581                                            int iface, int altno)
582 {
583         /* Reset ALL ifaces to 0 altsetting.
584          * Call it for every possible altsetting of every interface.
585          */
586         usb_set_interface(chip->dev, iface, 0);
587
588         /* possible configuration where both inputs and only one output is
589          *used is not supported by the current setup
590          */
591         if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
592                 if (chip->setup & MAUDIO_SET_96K) {
593                         if (altno != 3 && altno != 6)
594                                 return 1;
595                 } else if (chip->setup & MAUDIO_SET_DI) {
596                         if (iface == 4)
597                                 return 1; /* no analog input */
598                         if (altno != 2 && altno != 5)
599                                 return 1; /* enable only altsets 2 and 5 */
600                 } else {
601                         if (iface == 5)
602                                 return 1; /* disable digialt input */
603                         if (altno != 2 && altno != 5)
604                                 return 1; /* enalbe only altsets 2 and 5 */
605                 }
606         } else {
607                 /* keep only 16-Bit mode */
608                 if (altno != 1)
609                         return 1;
610         }
611
612         snd_printdd(KERN_INFO
613                     "using altsetting %d for interface %d config %d\n",
614                     altno, iface, chip->setup);
615         return 0; /* keep this altsetting */
616 }
617
618 int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
619                                   int iface,
620                                   int altno)
621 {
622         /* audiophile usb: skip altsets incompatible with device_setup */
623         if (chip->usb_id == USB_ID(0x0763, 0x2003))
624                 return audiophile_skip_setting_quirk(chip, iface, altno);
625         /* quattro usb: skip altsets incompatible with device_setup */
626         if (chip->usb_id == USB_ID(0x0763, 0x2001))
627                 return quattro_skip_setting_quirk(chip, iface, altno);
628         /* fasttrackpro usb: skip altsets incompatible with device_setup */
629         if (chip->usb_id == USB_ID(0x0763, 0x2012))
630                 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
631
632         return 0;
633 }
634
635 int snd_usb_apply_boot_quirk(struct usb_device *dev,
636                              struct usb_interface *intf,
637                              const struct snd_usb_audio_quirk *quirk)
638 {
639         u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
640                         le16_to_cpu(dev->descriptor.idProduct));
641
642         switch (id) {
643         case USB_ID(0x041e, 0x3000):
644                 /* SB Extigy needs special boot-up sequence */
645                 /* if more models come, this will go to the quirk list. */
646                 return snd_usb_extigy_boot_quirk(dev, intf);
647
648         case USB_ID(0x041e, 0x3020):
649                 /* SB Audigy 2 NX needs its own boot-up magic, too */
650                 return snd_usb_audigy2nx_boot_quirk(dev);
651
652         case USB_ID(0x10f5, 0x0200):
653                 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
654                 return snd_usb_cm106_boot_quirk(dev);
655
656         case USB_ID(0x0d8c, 0x0102):
657                 /* C-Media CM6206 / CM106-Like Sound Device */
658         case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
659                 return snd_usb_cm6206_boot_quirk(dev);
660
661         case USB_ID(0x133e, 0x0815):
662                 /* Access Music VirusTI Desktop */
663                 return snd_usb_accessmusic_boot_quirk(dev);
664
665         case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
666         case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
667         case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
668                 return snd_usb_nativeinstruments_boot_quirk(dev);
669         case USB_ID(0x0763, 0x2012):  /* M-Audio Fast Track Pro USB */
670                 return snd_usb_fasttrackpro_boot_quirk(dev);
671         }
672
673         return 0;
674 }
675
676 /*
677  * check if the device uses big-endian samples
678  */
679 int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
680 {
681         /* it depends on altsetting wether the device is big-endian or not */
682         switch (chip->usb_id) {
683         case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
684                 if (fp->altsetting == 2 || fp->altsetting == 3 ||
685                         fp->altsetting == 5 || fp->altsetting == 6)
686                         return 1;
687                 break;
688         case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
689                 if (chip->setup == 0x00 ||
690                         fp->altsetting == 1 || fp->altsetting == 2 ||
691                         fp->altsetting == 3)
692                         return 1;
693                 break;
694         case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
695                 if (fp->altsetting == 2 || fp->altsetting == 3 ||
696                         fp->altsetting == 5 || fp->altsetting == 6)
697                         return 1;
698                 break;
699         }
700         return 0;
701 }
702
703 /*
704  * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
705  * not for interface.
706  */
707
708 enum {
709         EMU_QUIRK_SR_44100HZ = 0,
710         EMU_QUIRK_SR_48000HZ,
711         EMU_QUIRK_SR_88200HZ,
712         EMU_QUIRK_SR_96000HZ,
713         EMU_QUIRK_SR_176400HZ,
714         EMU_QUIRK_SR_192000HZ
715 };
716
717 static void set_format_emu_quirk(struct snd_usb_substream *subs,
718                                  struct audioformat *fmt)
719 {
720         unsigned char emu_samplerate_id = 0;
721
722         /* When capture is active
723          * sample rate shouldn't be changed
724          * by playback substream
725          */
726         if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
727                 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
728                         return;
729         }
730
731         switch (fmt->rate_min) {
732         case 48000:
733                 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
734                 break;
735         case 88200:
736                 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
737                 break;
738         case 96000:
739                 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
740                 break;
741         case 176400:
742                 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
743                 break;
744         case 192000:
745                 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
746                 break;
747         default:
748                 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
749                 break;
750         }
751         snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
752         subs->pkt_offset_adj = (emu_samplerate_id >= EMU_QUIRK_SR_176400HZ) ? 4 : 0;
753 }
754
755 void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
756                               struct audioformat *fmt)
757 {
758         switch (subs->stream->chip->usb_id) {
759         case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
760         case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
761         case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
762         case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
763                 set_format_emu_quirk(subs, fmt);
764                 break;
765         }
766 }
767