tcp: enforce tcp_min_snd_mss in tcp_mtu_probing()
[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         INIT_LIST_HEAD(&fp->list);
140         if (fp->nr_rates > MAX_NR_RATES) {
141                 kfree(fp);
142                 return -EINVAL;
143         }
144         if (fp->nr_rates > 0) {
145                 rate_table = kmemdup(fp->rate_table,
146                                      sizeof(int) * fp->nr_rates, GFP_KERNEL);
147                 if (!rate_table) {
148                         kfree(fp);
149                         return -ENOMEM;
150                 }
151                 fp->rate_table = rate_table;
152         }
153
154         stream = (fp->endpoint & USB_DIR_IN)
155                 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
156         err = snd_usb_add_audio_stream(chip, stream, fp);
157         if (err < 0)
158                 goto error;
159         if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
160             fp->altset_idx >= iface->num_altsetting) {
161                 err = -EINVAL;
162                 goto error;
163         }
164         alts = &iface->altsetting[fp->altset_idx];
165         if (get_iface_desc(alts)->bNumEndpoints < 1) {
166                 err = -EINVAL;
167                 goto error;
168         }
169
170         fp->datainterval = snd_usb_parse_datainterval(chip, alts);
171         fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
172         usb_set_interface(chip->dev, fp->iface, 0);
173         snd_usb_init_pitch(chip, fp->iface, alts, fp);
174         snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
175         return 0;
176
177  error:
178         list_del(&fp->list); /* unlink for avoiding double-free */
179         kfree(fp);
180         kfree(rate_table);
181         return err;
182 }
183
184 /*
185  * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.  
186  * The only way to detect the sample rate is by looking at wMaxPacketSize.
187  */
188 static int create_uaxx_quirk(struct snd_usb_audio *chip,
189                              struct usb_interface *iface,
190                              struct usb_driver *driver,
191                              const struct snd_usb_audio_quirk *quirk)
192 {
193         static const struct audioformat ua_format = {
194                 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
195                 .channels = 2,
196                 .fmt_type = UAC_FORMAT_TYPE_I,
197                 .altsetting = 1,
198                 .altset_idx = 1,
199                 .rates = SNDRV_PCM_RATE_CONTINUOUS,
200         };
201         struct usb_host_interface *alts;
202         struct usb_interface_descriptor *altsd;
203         struct audioformat *fp;
204         int stream, err;
205
206         /* both PCM and MIDI interfaces have 2 or more altsettings */
207         if (iface->num_altsetting < 2)
208                 return -ENXIO;
209         alts = &iface->altsetting[1];
210         altsd = get_iface_desc(alts);
211
212         if (altsd->bNumEndpoints == 2) {
213                 static const struct snd_usb_midi_endpoint_info ua700_ep = {
214                         .out_cables = 0x0003,
215                         .in_cables  = 0x0003
216                 };
217                 static const struct snd_usb_audio_quirk ua700_quirk = {
218                         .type = QUIRK_MIDI_FIXED_ENDPOINT,
219                         .data = &ua700_ep
220                 };
221                 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
222                         .out_cables = 0x0001,
223                         .in_cables  = 0x0001
224                 };
225                 static const struct snd_usb_audio_quirk uaxx_quirk = {
226                         .type = QUIRK_MIDI_FIXED_ENDPOINT,
227                         .data = &uaxx_ep
228                 };
229                 const struct snd_usb_audio_quirk *quirk =
230                         chip->usb_id == USB_ID(0x0582, 0x002b)
231                         ? &ua700_quirk : &uaxx_quirk;
232                 return snd_usbmidi_create(chip->card, iface,
233                                           &chip->midi_list, quirk);
234         }
235
236         if (altsd->bNumEndpoints != 1)
237                 return -ENXIO;
238
239         fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
240         if (!fp)
241                 return -ENOMEM;
242
243         fp->iface = altsd->bInterfaceNumber;
244         fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
245         fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
246         fp->datainterval = 0;
247         fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
248         INIT_LIST_HEAD(&fp->list);
249
250         switch (fp->maxpacksize) {
251         case 0x120:
252                 fp->rate_max = fp->rate_min = 44100;
253                 break;
254         case 0x138:
255         case 0x140:
256                 fp->rate_max = fp->rate_min = 48000;
257                 break;
258         case 0x258:
259         case 0x260:
260                 fp->rate_max = fp->rate_min = 96000;
261                 break;
262         default:
263                 snd_printk(KERN_ERR "unknown sample rate\n");
264                 kfree(fp);
265                 return -ENXIO;
266         }
267
268         stream = (fp->endpoint & USB_DIR_IN)
269                 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
270         err = snd_usb_add_audio_stream(chip, stream, fp);
271         if (err < 0) {
272                 list_del(&fp->list); /* unlink for avoiding double-free */
273                 kfree(fp);
274                 return err;
275         }
276         usb_set_interface(chip->dev, fp->iface, 0);
277         return 0;
278 }
279
280 /*
281  * Create a standard mixer for the specified interface.
282  */
283 static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
284                                        struct usb_interface *iface,
285                                        struct usb_driver *driver,
286                                        const struct snd_usb_audio_quirk *quirk)
287 {
288         if (quirk->ifnum < 0)
289                 return 0;
290
291         return snd_usb_create_mixer(chip, quirk->ifnum, 0);
292 }
293
294 /*
295  * audio-interface quirks
296  *
297  * returns zero if no standard audio/MIDI parsing is needed.
298  * returns a positive value if standard audio/midi interfaces are parsed
299  * after this.
300  * returns a negative value at error.
301  */
302 int snd_usb_create_quirk(struct snd_usb_audio *chip,
303                          struct usb_interface *iface,
304                          struct usb_driver *driver,
305                          const struct snd_usb_audio_quirk *quirk)
306 {
307         typedef int (*quirk_func_t)(struct snd_usb_audio *,
308                                     struct usb_interface *,
309                                     struct usb_driver *,
310                                     const struct snd_usb_audio_quirk *);
311         static const quirk_func_t quirk_funcs[] = {
312                 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
313                 [QUIRK_COMPOSITE] = create_composite_quirk,
314                 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
315                 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
316                 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
317                 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
318                 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
319                 [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
320                 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
321                 [QUIRK_MIDI_CME] = create_any_midi_quirk,
322                 [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
323                 [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
324                 [QUIRK_MIDI_CH345] = create_any_midi_quirk,
325                 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
326                 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
327                 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
328                 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
329                 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
330         };
331
332         if (quirk->type < QUIRK_TYPE_COUNT) {
333                 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
334         } else {
335                 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
336                 return -ENXIO;
337         }
338 }
339
340 /*
341  * boot quirks
342  */
343
344 #define EXTIGY_FIRMWARE_SIZE_OLD 794
345 #define EXTIGY_FIRMWARE_SIZE_NEW 483
346
347 static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
348 {
349         struct usb_host_config *config = dev->actconfig;
350         int err;
351
352         if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
353             le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
354                 snd_printdd("sending Extigy boot sequence...\n");
355                 /* Send message to force it to reconnect with full interface. */
356                 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
357                                       0x10, 0x43, 0x0001, 0x000a, NULL, 0);
358                 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
359                 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
360                                 &dev->descriptor, sizeof(dev->descriptor));
361                 config = dev->actconfig;
362                 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
363                 err = usb_reset_configuration(dev);
364                 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
365                 snd_printdd("extigy_boot: new boot length = %d\n",
366                             le16_to_cpu(get_cfg_desc(config)->wTotalLength));
367                 return -ENODEV; /* quit this anyway */
368         }
369         return 0;
370 }
371
372 static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
373 {
374         u8 buf = 1;
375
376         snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
377                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
378                         0, 0, &buf, 1);
379         if (buf == 0) {
380                 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
381                                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
382                                 1, 2000, NULL, 0);
383                 return -ENODEV;
384         }
385         return 0;
386 }
387
388 static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
389 {
390         int err;
391
392         if (dev->actconfig->desc.bConfigurationValue == 1) {
393                 snd_printk(KERN_INFO "usb-audio: "
394                            "Fast Track Pro switching to config #2\n");
395                 /* This function has to be available by the usb core module.
396                  * if it is not avialable the boot quirk has to be left out
397                  * and the configuration has to be set by udev or hotplug
398                  * rules
399                  */
400                 err = usb_driver_set_configuration(dev, 2);
401                 if (err < 0)
402                         snd_printdd("error usb_driver_set_configuration: %d\n",
403                                     err);
404                 /* Always return an error, so that we stop creating a device
405                    that will just be destroyed and recreated with a new
406                    configuration */
407                 return -ENODEV;
408         } else
409                 snd_printk(KERN_INFO "usb-audio: Fast Track Pro config OK\n");
410
411         return 0;
412 }
413
414 /*
415  * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
416  * documented in the device's data sheet.
417  */
418 static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
419 {
420         u8 buf[4];
421         buf[0] = 0x20;
422         buf[1] = value & 0xff;
423         buf[2] = (value >> 8) & 0xff;
424         buf[3] = reg;
425         return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
426                                USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
427                                0, 0, &buf, 4);
428 }
429
430 static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
431 {
432         /*
433          * Enable line-out driver mode, set headphone source to front
434          * channels, enable stereo mic.
435          */
436         return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
437 }
438
439 /*
440  * C-Media CM6206 is based on CM106 with two additional
441  * registers that are not documented in the data sheet.
442  * Values here are chosen based on sniffing USB traffic
443  * under Windows.
444  */
445 static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
446 {
447         int err  = 0, reg;
448         int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
449
450         for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
451                 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
452                 if (err < 0)
453                         return err;
454         }
455
456         return err;
457 }
458
459 /*
460  * This call will put the synth in "USB send" mode, i.e it will send MIDI
461  * messages through USB (this is disabled at startup). The synth will
462  * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
463  * sign on its LCD. Values here are chosen based on sniffing USB traffic
464  * under Windows.
465  */
466 static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
467 {
468         int err, actual_length;
469
470         /* "midi send" enable */
471         static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
472
473         void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
474         if (!buf)
475                 return -ENOMEM;
476         err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
477                         ARRAY_SIZE(seq), &actual_length, 1000);
478         kfree(buf);
479         if (err < 0)
480                 return err;
481
482         return 0;
483 }
484
485 /*
486  * Some sound cards from Native Instruments are in fact compliant to the USB
487  * audio standard of version 2 and other approved USB standards, even though
488  * they come up as vendor-specific device when first connected.
489  *
490  * However, they can be told to come up with a new set of descriptors
491  * upon their next enumeration, and the interfaces announced by the new
492  * descriptors will then be handled by the kernel's class drivers. As the
493  * product ID will also change, no further checks are required.
494  */
495
496 static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
497 {
498         int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
499                                   0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
500                                   1, 0, NULL, 0, 1000);
501
502         if (ret < 0)
503                 return ret;
504
505         usb_reset_device(dev);
506
507         /* return -EAGAIN, so the creation of an audio interface for this
508          * temporary device is aborted. The device will reconnect with a
509          * new product ID */
510         return -EAGAIN;
511 }
512
513 /*
514  * Setup quirks
515  */
516 #define MAUDIO_SET              0x01 /* parse device_setup */
517 #define MAUDIO_SET_COMPATIBLE   0x80 /* use only "win-compatible" interfaces */
518 #define MAUDIO_SET_DTS          0x02 /* enable DTS Digital Output */
519 #define MAUDIO_SET_96K          0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
520 #define MAUDIO_SET_24B          0x08 /* 24bits sample if set, 16bits otherwise */
521 #define MAUDIO_SET_DI           0x10 /* enable Digital Input */
522 #define MAUDIO_SET_MASK         0x1f /* bit mask for setup value */
523 #define MAUDIO_SET_24B_48K_DI    0x19 /* 24bits+48KHz+Digital Input */
524 #define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
525 #define MAUDIO_SET_16B_48K_DI    0x11 /* 16bits+48KHz+Digital Input */
526 #define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
527
528 static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
529                                       int iface, int altno)
530 {
531         /* Reset ALL ifaces to 0 altsetting.
532          * Call it for every possible altsetting of every interface.
533          */
534         usb_set_interface(chip->dev, iface, 0);
535         if (chip->setup & MAUDIO_SET) {
536                 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
537                         if (iface != 1 && iface != 2)
538                                 return 1; /* skip all interfaces but 1 and 2 */
539                 } else {
540                         unsigned int mask;
541                         if (iface == 1 || iface == 2)
542                                 return 1; /* skip interfaces 1 and 2 */
543                         if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
544                                 return 1; /* skip this altsetting */
545                         mask = chip->setup & MAUDIO_SET_MASK;
546                         if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
547                                 return 1; /* skip this altsetting */
548                         if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
549                                 return 1; /* skip this altsetting */
550                         if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
551                                 return 1; /* skip this altsetting */
552                 }
553         }
554         snd_printdd(KERN_INFO
555                     "using altsetting %d for interface %d config %d\n",
556                     altno, iface, chip->setup);
557         return 0; /* keep this altsetting */
558 }
559
560 static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
561                                          int iface,
562                                          int altno)
563 {
564         /* Reset ALL ifaces to 0 altsetting.
565          * Call it for every possible altsetting of every interface.
566          */
567         usb_set_interface(chip->dev, iface, 0);
568
569         if (chip->setup & MAUDIO_SET) {
570                 unsigned int mask;
571                 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
572                         return 1; /* skip this altsetting */
573                 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
574                         return 1; /* skip this altsetting */
575                 mask = chip->setup & MAUDIO_SET_MASK;
576                 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
577                         return 1; /* skip this altsetting */
578                 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
579                         return 1; /* skip this altsetting */
580                 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
581                         return 1; /* skip this altsetting */
582                 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
583                         return 1; /* skip this altsetting */
584         }
585
586         return 0; /* keep this altsetting */
587 }
588
589
590 static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
591                                            int iface, int altno)
592 {
593         /* Reset ALL ifaces to 0 altsetting.
594          * Call it for every possible altsetting of every interface.
595          */
596         usb_set_interface(chip->dev, iface, 0);
597
598         /* possible configuration where both inputs and only one output is
599          *used is not supported by the current setup
600          */
601         if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
602                 if (chip->setup & MAUDIO_SET_96K) {
603                         if (altno != 3 && altno != 6)
604                                 return 1;
605                 } else if (chip->setup & MAUDIO_SET_DI) {
606                         if (iface == 4)
607                                 return 1; /* no analog input */
608                         if (altno != 2 && altno != 5)
609                                 return 1; /* enable only altsets 2 and 5 */
610                 } else {
611                         if (iface == 5)
612                                 return 1; /* disable digialt input */
613                         if (altno != 2 && altno != 5)
614                                 return 1; /* enalbe only altsets 2 and 5 */
615                 }
616         } else {
617                 /* keep only 16-Bit mode */
618                 if (altno != 1)
619                         return 1;
620         }
621
622         snd_printdd(KERN_INFO
623                     "using altsetting %d for interface %d config %d\n",
624                     altno, iface, chip->setup);
625         return 0; /* keep this altsetting */
626 }
627
628 int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
629                                   int iface,
630                                   int altno)
631 {
632         /* audiophile usb: skip altsets incompatible with device_setup */
633         if (chip->usb_id == USB_ID(0x0763, 0x2003))
634                 return audiophile_skip_setting_quirk(chip, iface, altno);
635         /* quattro usb: skip altsets incompatible with device_setup */
636         if (chip->usb_id == USB_ID(0x0763, 0x2001))
637                 return quattro_skip_setting_quirk(chip, iface, altno);
638         /* fasttrackpro usb: skip altsets incompatible with device_setup */
639         if (chip->usb_id == USB_ID(0x0763, 0x2012))
640                 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
641
642         return 0;
643 }
644
645 int snd_usb_apply_boot_quirk(struct usb_device *dev,
646                              struct usb_interface *intf,
647                              const struct snd_usb_audio_quirk *quirk)
648 {
649         u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
650                         le16_to_cpu(dev->descriptor.idProduct));
651
652         switch (id) {
653         case USB_ID(0x041e, 0x3000):
654                 /* SB Extigy needs special boot-up sequence */
655                 /* if more models come, this will go to the quirk list. */
656                 return snd_usb_extigy_boot_quirk(dev, intf);
657
658         case USB_ID(0x041e, 0x3020):
659                 /* SB Audigy 2 NX needs its own boot-up magic, too */
660                 return snd_usb_audigy2nx_boot_quirk(dev);
661
662         case USB_ID(0x10f5, 0x0200):
663                 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
664                 return snd_usb_cm106_boot_quirk(dev);
665
666         case USB_ID(0x0d8c, 0x0102):
667                 /* C-Media CM6206 / CM106-Like Sound Device */
668         case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
669                 return snd_usb_cm6206_boot_quirk(dev);
670
671         case USB_ID(0x133e, 0x0815):
672                 /* Access Music VirusTI Desktop */
673                 return snd_usb_accessmusic_boot_quirk(dev);
674
675         case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
676         case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
677         case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
678                 return snd_usb_nativeinstruments_boot_quirk(dev);
679         case USB_ID(0x0763, 0x2012):  /* M-Audio Fast Track Pro USB */
680                 return snd_usb_fasttrackpro_boot_quirk(dev);
681         }
682
683         return 0;
684 }
685
686 /*
687  * check if the device uses big-endian samples
688  */
689 int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
690 {
691         /* it depends on altsetting wether the device is big-endian or not */
692         switch (chip->usb_id) {
693         case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
694                 if (fp->altsetting == 2 || fp->altsetting == 3 ||
695                         fp->altsetting == 5 || fp->altsetting == 6)
696                         return 1;
697                 break;
698         case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
699                 if (chip->setup == 0x00 ||
700                         fp->altsetting == 1 || fp->altsetting == 2 ||
701                         fp->altsetting == 3)
702                         return 1;
703                 break;
704         case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
705                 if (fp->altsetting == 2 || fp->altsetting == 3 ||
706                         fp->altsetting == 5 || fp->altsetting == 6)
707                         return 1;
708                 break;
709         }
710         return 0;
711 }
712
713 /*
714  * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
715  * not for interface.
716  */
717
718 enum {
719         EMU_QUIRK_SR_44100HZ = 0,
720         EMU_QUIRK_SR_48000HZ,
721         EMU_QUIRK_SR_88200HZ,
722         EMU_QUIRK_SR_96000HZ,
723         EMU_QUIRK_SR_176400HZ,
724         EMU_QUIRK_SR_192000HZ
725 };
726
727 static void set_format_emu_quirk(struct snd_usb_substream *subs,
728                                  struct audioformat *fmt)
729 {
730         unsigned char emu_samplerate_id = 0;
731
732         /* When capture is active
733          * sample rate shouldn't be changed
734          * by playback substream
735          */
736         if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
737                 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
738                         return;
739         }
740
741         switch (fmt->rate_min) {
742         case 48000:
743                 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
744                 break;
745         case 88200:
746                 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
747                 break;
748         case 96000:
749                 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
750                 break;
751         case 176400:
752                 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
753                 break;
754         case 192000:
755                 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
756                 break;
757         default:
758                 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
759                 break;
760         }
761         snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
762         subs->pkt_offset_adj = (emu_samplerate_id >= EMU_QUIRK_SR_176400HZ) ? 4 : 0;
763 }
764
765 void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
766                               struct audioformat *fmt)
767 {
768         switch (subs->stream->chip->usb_id) {
769         case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
770         case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
771         case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
772         case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
773                 set_format_emu_quirk(subs, fmt);
774                 break;
775         }
776 }
777