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