Merge tag 'for-linus-3.11-merge-window-part-2' of git://git.kernel.org/pub/scm/linux...
[cascardo/linux.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 {
342         int err;
343
344         err = create_auto_pcm_quirk(chip, iface, driver);
345         if (err == -ENODEV)
346                 err = create_auto_midi_quirk(chip, iface, driver);
347         return err;
348 }
349
350 static int create_autodetect_quirks(struct snd_usb_audio *chip,
351                                     struct usb_interface *iface,
352                                     struct usb_driver *driver,
353                                     const struct snd_usb_audio_quirk *quirk)
354 {
355         int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
356         int ifcount, ifnum, err;
357
358         err = create_autodetect_quirk(chip, iface, driver);
359         if (err < 0)
360                 return err;
361
362         /*
363          * ALSA PCM playback/capture devices cannot be registered in two steps,
364          * so we have to claim the other corresponding interface here.
365          */
366         ifcount = chip->dev->actconfig->desc.bNumInterfaces;
367         for (ifnum = 0; ifnum < ifcount; ifnum++) {
368                 if (ifnum == probed_ifnum || quirk->ifnum >= 0)
369                         continue;
370                 iface = usb_ifnum_to_if(chip->dev, ifnum);
371                 if (!iface ||
372                     usb_interface_claimed(iface) ||
373                     get_iface_desc(iface->altsetting)->bInterfaceClass !=
374                                                         USB_CLASS_VENDOR_SPEC)
375                         continue;
376
377                 err = create_autodetect_quirk(chip, iface, driver);
378                 if (err >= 0)
379                         usb_driver_claim_interface(driver, iface, (void *)-1L);
380         }
381
382         return 0;
383 }
384
385 /*
386  * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.  
387  * The only way to detect the sample rate is by looking at wMaxPacketSize.
388  */
389 static int create_uaxx_quirk(struct snd_usb_audio *chip,
390                              struct usb_interface *iface,
391                              struct usb_driver *driver,
392                              const struct snd_usb_audio_quirk *quirk)
393 {
394         static const struct audioformat ua_format = {
395                 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
396                 .channels = 2,
397                 .fmt_type = UAC_FORMAT_TYPE_I,
398                 .altsetting = 1,
399                 .altset_idx = 1,
400                 .rates = SNDRV_PCM_RATE_CONTINUOUS,
401         };
402         struct usb_host_interface *alts;
403         struct usb_interface_descriptor *altsd;
404         struct audioformat *fp;
405         int stream, err;
406
407         /* both PCM and MIDI interfaces have 2 or more altsettings */
408         if (iface->num_altsetting < 2)
409                 return -ENXIO;
410         alts = &iface->altsetting[1];
411         altsd = get_iface_desc(alts);
412
413         if (altsd->bNumEndpoints == 2) {
414                 static const struct snd_usb_midi_endpoint_info ua700_ep = {
415                         .out_cables = 0x0003,
416                         .in_cables  = 0x0003
417                 };
418                 static const struct snd_usb_audio_quirk ua700_quirk = {
419                         .type = QUIRK_MIDI_FIXED_ENDPOINT,
420                         .data = &ua700_ep
421                 };
422                 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
423                         .out_cables = 0x0001,
424                         .in_cables  = 0x0001
425                 };
426                 static const struct snd_usb_audio_quirk uaxx_quirk = {
427                         .type = QUIRK_MIDI_FIXED_ENDPOINT,
428                         .data = &uaxx_ep
429                 };
430                 const struct snd_usb_audio_quirk *quirk =
431                         chip->usb_id == USB_ID(0x0582, 0x002b)
432                         ? &ua700_quirk : &uaxx_quirk;
433                 return snd_usbmidi_create(chip->card, iface,
434                                           &chip->midi_list, quirk);
435         }
436
437         if (altsd->bNumEndpoints != 1)
438                 return -ENXIO;
439
440         fp = kmemdup(&ua_format, sizeof(*fp), GFP_KERNEL);
441         if (!fp)
442                 return -ENOMEM;
443
444         fp->iface = altsd->bInterfaceNumber;
445         fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
446         fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
447         fp->datainterval = 0;
448         fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
449
450         switch (fp->maxpacksize) {
451         case 0x120:
452                 fp->rate_max = fp->rate_min = 44100;
453                 break;
454         case 0x138:
455         case 0x140:
456                 fp->rate_max = fp->rate_min = 48000;
457                 break;
458         case 0x258:
459         case 0x260:
460                 fp->rate_max = fp->rate_min = 96000;
461                 break;
462         default:
463                 snd_printk(KERN_ERR "unknown sample rate\n");
464                 kfree(fp);
465                 return -ENXIO;
466         }
467
468         stream = (fp->endpoint & USB_DIR_IN)
469                 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
470         err = snd_usb_add_audio_stream(chip, stream, fp);
471         if (err < 0) {
472                 kfree(fp);
473                 return err;
474         }
475         usb_set_interface(chip->dev, fp->iface, 0);
476         return 0;
477 }
478
479 /*
480  * Create a standard mixer for the specified interface.
481  */
482 static int create_standard_mixer_quirk(struct snd_usb_audio *chip,
483                                        struct usb_interface *iface,
484                                        struct usb_driver *driver,
485                                        const struct snd_usb_audio_quirk *quirk)
486 {
487         if (quirk->ifnum < 0)
488                 return 0;
489
490         return snd_usb_create_mixer(chip, quirk->ifnum, 0);
491 }
492
493 /*
494  * audio-interface quirks
495  *
496  * returns zero if no standard audio/MIDI parsing is needed.
497  * returns a positive value if standard audio/midi interfaces are parsed
498  * after this.
499  * returns a negative value at error.
500  */
501 int snd_usb_create_quirk(struct snd_usb_audio *chip,
502                          struct usb_interface *iface,
503                          struct usb_driver *driver,
504                          const struct snd_usb_audio_quirk *quirk)
505 {
506         typedef int (*quirk_func_t)(struct snd_usb_audio *,
507                                     struct usb_interface *,
508                                     struct usb_driver *,
509                                     const struct snd_usb_audio_quirk *);
510         static const quirk_func_t quirk_funcs[] = {
511                 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
512                 [QUIRK_COMPOSITE] = create_composite_quirk,
513                 [QUIRK_AUTODETECT] = create_autodetect_quirks,
514                 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
515                 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
516                 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
517                 [QUIRK_MIDI_ROLAND] = create_any_midi_quirk,
518                 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
519                 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
520                 [QUIRK_MIDI_RAW_BYTES] = create_any_midi_quirk,
521                 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
522                 [QUIRK_MIDI_CME] = create_any_midi_quirk,
523                 [QUIRK_MIDI_AKAI] = create_any_midi_quirk,
524                 [QUIRK_MIDI_FTDI] = create_any_midi_quirk,
525                 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
526                 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
527                 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
528                 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk,
529                 [QUIRK_AUDIO_STANDARD_MIXER] = create_standard_mixer_quirk,
530         };
531
532         if (quirk->type < QUIRK_TYPE_COUNT) {
533                 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
534         } else {
535                 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
536                 return -ENXIO;
537         }
538 }
539
540 /*
541  * boot quirks
542  */
543
544 #define EXTIGY_FIRMWARE_SIZE_OLD 794
545 #define EXTIGY_FIRMWARE_SIZE_NEW 483
546
547 static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
548 {
549         struct usb_host_config *config = dev->actconfig;
550         int err;
551
552         if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
553             le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
554                 snd_printdd("sending Extigy boot sequence...\n");
555                 /* Send message to force it to reconnect with full interface. */
556                 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
557                                       0x10, 0x43, 0x0001, 0x000a, NULL, 0);
558                 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
559                 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
560                                 &dev->descriptor, sizeof(dev->descriptor));
561                 config = dev->actconfig;
562                 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
563                 err = usb_reset_configuration(dev);
564                 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
565                 snd_printdd("extigy_boot: new boot length = %d\n",
566                             le16_to_cpu(get_cfg_desc(config)->wTotalLength));
567                 return -ENODEV; /* quit this anyway */
568         }
569         return 0;
570 }
571
572 static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
573 {
574         u8 buf = 1;
575
576         snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
577                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
578                         0, 0, &buf, 1);
579         if (buf == 0) {
580                 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
581                                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
582                                 1, 2000, NULL, 0);
583                 return -ENODEV;
584         }
585         return 0;
586 }
587
588 static int snd_usb_fasttrackpro_boot_quirk(struct usb_device *dev)
589 {
590         int err;
591
592         if (dev->actconfig->desc.bConfigurationValue == 1) {
593                 snd_printk(KERN_INFO "usb-audio: "
594                            "Fast Track Pro switching to config #2\n");
595                 /* This function has to be available by the usb core module.
596                  * if it is not avialable the boot quirk has to be left out
597                  * and the configuration has to be set by udev or hotplug
598                  * rules
599                  */
600                 err = usb_driver_set_configuration(dev, 2);
601                 if (err < 0)
602                         snd_printdd("error usb_driver_set_configuration: %d\n",
603                                     err);
604                 /* Always return an error, so that we stop creating a device
605                    that will just be destroyed and recreated with a new
606                    configuration */
607                 return -ENODEV;
608         } else
609                 snd_printk(KERN_INFO "usb-audio: Fast Track Pro config OK\n");
610
611         return 0;
612 }
613
614 /*
615  * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
616  * documented in the device's data sheet.
617  */
618 static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
619 {
620         u8 buf[4];
621         buf[0] = 0x20;
622         buf[1] = value & 0xff;
623         buf[2] = (value >> 8) & 0xff;
624         buf[3] = reg;
625         return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
626                                USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
627                                0, 0, &buf, 4);
628 }
629
630 static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
631 {
632         /*
633          * Enable line-out driver mode, set headphone source to front
634          * channels, enable stereo mic.
635          */
636         return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
637 }
638
639 /*
640  * C-Media CM6206 is based on CM106 with two additional
641  * registers that are not documented in the data sheet.
642  * Values here are chosen based on sniffing USB traffic
643  * under Windows.
644  */
645 static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
646 {
647         int err  = 0, reg;
648         int val[] = {0x2004, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
649
650         for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
651                 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
652                 if (err < 0)
653                         return err;
654         }
655
656         return err;
657 }
658
659 /*
660  * Novation Twitch DJ controller
661  */
662 static int snd_usb_twitch_boot_quirk(struct usb_device *dev)
663 {
664         /* preemptively set up the device because otherwise the
665          * raw MIDI endpoints are not active */
666         usb_set_interface(dev, 0, 1);
667         return 0;
668 }
669
670 /*
671  * This call will put the synth in "USB send" mode, i.e it will send MIDI
672  * messages through USB (this is disabled at startup). The synth will
673  * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
674  * sign on its LCD. Values here are chosen based on sniffing USB traffic
675  * under Windows.
676  */
677 static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
678 {
679         int err, actual_length;
680
681         /* "midi send" enable */
682         static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
683
684         void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
685         if (!buf)
686                 return -ENOMEM;
687         err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
688                         ARRAY_SIZE(seq), &actual_length, 1000);
689         kfree(buf);
690         if (err < 0)
691                 return err;
692
693         return 0;
694 }
695
696 /*
697  * Some sound cards from Native Instruments are in fact compliant to the USB
698  * audio standard of version 2 and other approved USB standards, even though
699  * they come up as vendor-specific device when first connected.
700  *
701  * However, they can be told to come up with a new set of descriptors
702  * upon their next enumeration, and the interfaces announced by the new
703  * descriptors will then be handled by the kernel's class drivers. As the
704  * product ID will also change, no further checks are required.
705  */
706
707 static int snd_usb_nativeinstruments_boot_quirk(struct usb_device *dev)
708 {
709         int ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
710                                   0xaf, USB_TYPE_VENDOR | USB_RECIP_DEVICE,
711                                   1, 0, NULL, 0, 1000);
712
713         if (ret < 0)
714                 return ret;
715
716         usb_reset_device(dev);
717
718         /* return -EAGAIN, so the creation of an audio interface for this
719          * temporary device is aborted. The device will reconnect with a
720          * new product ID */
721         return -EAGAIN;
722 }
723
724 static void mbox2_setup_48_24_magic(struct usb_device *dev)
725 {
726         u8 srate[3];
727         u8 temp[12];
728
729         /* Choose 48000Hz permanently */
730         srate[0] = 0x80;
731         srate[1] = 0xbb;
732         srate[2] = 0x00;
733
734         /* Send the magic! */
735         snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
736                 0x01, 0x22, 0x0100, 0x0085, &temp, 0x0003);
737         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
738                 0x81, 0xa2, 0x0100, 0x0085, &srate, 0x0003);
739         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
740                 0x81, 0xa2, 0x0100, 0x0086, &srate, 0x0003);
741         snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
742                 0x81, 0xa2, 0x0100, 0x0003, &srate, 0x0003);
743         return;
744 }
745
746 /* Digidesign Mbox 2 needs to load firmware onboard
747  * and driver must wait a few seconds for initialisation.
748  */
749
750 #define MBOX2_FIRMWARE_SIZE    646
751 #define MBOX2_BOOT_LOADING     0x01 /* Hard coded into the device */
752 #define MBOX2_BOOT_READY       0x02 /* Hard coded into the device */
753
754 static int snd_usb_mbox2_boot_quirk(struct usb_device *dev)
755 {
756         struct usb_host_config *config = dev->actconfig;
757         int err;
758         u8 bootresponse[0x12];
759         int fwsize;
760         int count;
761
762         fwsize = le16_to_cpu(get_cfg_desc(config)->wTotalLength);
763
764         if (fwsize != MBOX2_FIRMWARE_SIZE) {
765                 snd_printk(KERN_ERR "usb-audio: Invalid firmware size=%d.\n", fwsize);
766                 return -ENODEV;
767         }
768
769         snd_printd("usb-audio: Sending Digidesign Mbox 2 boot sequence...\n");
770
771         count = 0;
772         bootresponse[0] = MBOX2_BOOT_LOADING;
773         while ((bootresponse[0] == MBOX2_BOOT_LOADING) && (count < 10)) {
774                 msleep(500); /* 0.5 second delay */
775                 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
776                         /* Control magic - load onboard firmware */
777                         0x85, 0xc0, 0x0001, 0x0000, &bootresponse, 0x0012);
778                 if (bootresponse[0] == MBOX2_BOOT_READY)
779                         break;
780                 snd_printd("usb-audio: device not ready, resending boot sequence...\n");
781                 count++;
782         }
783
784         if (bootresponse[0] != MBOX2_BOOT_READY) {
785                 snd_printk(KERN_ERR "usb-audio: Unknown bootresponse=%d, or timed out, ignoring device.\n", bootresponse[0]);
786                 return -ENODEV;
787         }
788
789         snd_printdd("usb-audio: device initialised!\n");
790
791         err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
792                 &dev->descriptor, sizeof(dev->descriptor));
793         config = dev->actconfig;
794         if (err < 0)
795                 snd_printd("error usb_get_descriptor: %d\n", err);
796
797         err = usb_reset_configuration(dev);
798         if (err < 0)
799                 snd_printd("error usb_reset_configuration: %d\n", err);
800         snd_printdd("mbox2_boot: new boot length = %d\n",
801                 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
802
803         mbox2_setup_48_24_magic(dev);
804
805         snd_printk(KERN_INFO "usb-audio: Digidesign Mbox 2: 24bit 48kHz");
806
807         return 0; /* Successful boot */
808 }
809
810 /*
811  * Setup quirks
812  */
813 #define MAUDIO_SET              0x01 /* parse device_setup */
814 #define MAUDIO_SET_COMPATIBLE   0x80 /* use only "win-compatible" interfaces */
815 #define MAUDIO_SET_DTS          0x02 /* enable DTS Digital Output */
816 #define MAUDIO_SET_96K          0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
817 #define MAUDIO_SET_24B          0x08 /* 24bits sample if set, 16bits otherwise */
818 #define MAUDIO_SET_DI           0x10 /* enable Digital Input */
819 #define MAUDIO_SET_MASK         0x1f /* bit mask for setup value */
820 #define MAUDIO_SET_24B_48K_DI    0x19 /* 24bits+48KHz+Digital Input */
821 #define MAUDIO_SET_24B_48K_NOTDI 0x09 /* 24bits+48KHz+No Digital Input */
822 #define MAUDIO_SET_16B_48K_DI    0x11 /* 16bits+48KHz+Digital Input */
823 #define MAUDIO_SET_16B_48K_NOTDI 0x01 /* 16bits+48KHz+No Digital Input */
824
825 static int quattro_skip_setting_quirk(struct snd_usb_audio *chip,
826                                       int iface, int altno)
827 {
828         /* Reset ALL ifaces to 0 altsetting.
829          * Call it for every possible altsetting of every interface.
830          */
831         usb_set_interface(chip->dev, iface, 0);
832         if (chip->setup & MAUDIO_SET) {
833                 if (chip->setup & MAUDIO_SET_COMPATIBLE) {
834                         if (iface != 1 && iface != 2)
835                                 return 1; /* skip all interfaces but 1 and 2 */
836                 } else {
837                         unsigned int mask;
838                         if (iface == 1 || iface == 2)
839                                 return 1; /* skip interfaces 1 and 2 */
840                         if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
841                                 return 1; /* skip this altsetting */
842                         mask = chip->setup & MAUDIO_SET_MASK;
843                         if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
844                                 return 1; /* skip this altsetting */
845                         if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
846                                 return 1; /* skip this altsetting */
847                         if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 4)
848                                 return 1; /* skip this altsetting */
849                 }
850         }
851         snd_printdd(KERN_INFO
852                     "using altsetting %d for interface %d config %d\n",
853                     altno, iface, chip->setup);
854         return 0; /* keep this altsetting */
855 }
856
857 static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
858                                          int iface,
859                                          int altno)
860 {
861         /* Reset ALL ifaces to 0 altsetting.
862          * Call it for every possible altsetting of every interface.
863          */
864         usb_set_interface(chip->dev, iface, 0);
865
866         if (chip->setup & MAUDIO_SET) {
867                 unsigned int mask;
868                 if ((chip->setup & MAUDIO_SET_DTS) && altno != 6)
869                         return 1; /* skip this altsetting */
870                 if ((chip->setup & MAUDIO_SET_96K) && altno != 1)
871                         return 1; /* skip this altsetting */
872                 mask = chip->setup & MAUDIO_SET_MASK;
873                 if (mask == MAUDIO_SET_24B_48K_DI && altno != 2)
874                         return 1; /* skip this altsetting */
875                 if (mask == MAUDIO_SET_24B_48K_NOTDI && altno != 3)
876                         return 1; /* skip this altsetting */
877                 if (mask == MAUDIO_SET_16B_48K_DI && altno != 4)
878                         return 1; /* skip this altsetting */
879                 if (mask == MAUDIO_SET_16B_48K_NOTDI && altno != 5)
880                         return 1; /* skip this altsetting */
881         }
882
883         return 0; /* keep this altsetting */
884 }
885
886 static int fasttrackpro_skip_setting_quirk(struct snd_usb_audio *chip,
887                                            int iface, int altno)
888 {
889         /* Reset ALL ifaces to 0 altsetting.
890          * Call it for every possible altsetting of every interface.
891          */
892         usb_set_interface(chip->dev, iface, 0);
893
894         /* possible configuration where both inputs and only one output is
895          *used is not supported by the current setup
896          */
897         if (chip->setup & (MAUDIO_SET | MAUDIO_SET_24B)) {
898                 if (chip->setup & MAUDIO_SET_96K) {
899                         if (altno != 3 && altno != 6)
900                                 return 1;
901                 } else if (chip->setup & MAUDIO_SET_DI) {
902                         if (iface == 4)
903                                 return 1; /* no analog input */
904                         if (altno != 2 && altno != 5)
905                                 return 1; /* enable only altsets 2 and 5 */
906                 } else {
907                         if (iface == 5)
908                                 return 1; /* disable digialt input */
909                         if (altno != 2 && altno != 5)
910                                 return 1; /* enalbe only altsets 2 and 5 */
911                 }
912         } else {
913                 /* keep only 16-Bit mode */
914                 if (altno != 1)
915                         return 1;
916         }
917
918         snd_printdd(KERN_INFO
919                     "using altsetting %d for interface %d config %d\n",
920                     altno, iface, chip->setup);
921         return 0; /* keep this altsetting */
922 }
923
924 int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
925                                   int iface,
926                                   int altno)
927 {
928         /* audiophile usb: skip altsets incompatible with device_setup */
929         if (chip->usb_id == USB_ID(0x0763, 0x2003))
930                 return audiophile_skip_setting_quirk(chip, iface, altno);
931         /* quattro usb: skip altsets incompatible with device_setup */
932         if (chip->usb_id == USB_ID(0x0763, 0x2001))
933                 return quattro_skip_setting_quirk(chip, iface, altno);
934         /* fasttrackpro usb: skip altsets incompatible with device_setup */
935         if (chip->usb_id == USB_ID(0x0763, 0x2012))
936                 return fasttrackpro_skip_setting_quirk(chip, iface, altno);
937
938         return 0;
939 }
940
941 int snd_usb_apply_boot_quirk(struct usb_device *dev,
942                              struct usb_interface *intf,
943                              const struct snd_usb_audio_quirk *quirk)
944 {
945         u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
946                         le16_to_cpu(dev->descriptor.idProduct));
947
948         switch (id) {
949         case USB_ID(0x041e, 0x3000):
950                 /* SB Extigy needs special boot-up sequence */
951                 /* if more models come, this will go to the quirk list. */
952                 return snd_usb_extigy_boot_quirk(dev, intf);
953
954         case USB_ID(0x041e, 0x3020):
955                 /* SB Audigy 2 NX needs its own boot-up magic, too */
956                 return snd_usb_audigy2nx_boot_quirk(dev);
957
958         case USB_ID(0x10f5, 0x0200):
959                 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
960                 return snd_usb_cm106_boot_quirk(dev);
961
962         case USB_ID(0x0d8c, 0x0102):
963                 /* C-Media CM6206 / CM106-Like Sound Device */
964         case USB_ID(0x0ccd, 0x00b1): /* Terratec Aureon 7.1 USB */
965                 return snd_usb_cm6206_boot_quirk(dev);
966
967         case USB_ID(0x0dba, 0x3000):
968                 /* Digidesign Mbox 2 */
969                 return snd_usb_mbox2_boot_quirk(dev);
970
971         case USB_ID(0x1235, 0x0018):
972                 /* Focusrite Novation Twitch */
973                 return snd_usb_twitch_boot_quirk(dev);
974
975         case USB_ID(0x133e, 0x0815):
976                 /* Access Music VirusTI Desktop */
977                 return snd_usb_accessmusic_boot_quirk(dev);
978
979         case USB_ID(0x17cc, 0x1000): /* Komplete Audio 6 */
980         case USB_ID(0x17cc, 0x1010): /* Traktor Audio 6 */
981         case USB_ID(0x17cc, 0x1020): /* Traktor Audio 10 */
982                 return snd_usb_nativeinstruments_boot_quirk(dev);
983         case USB_ID(0x0763, 0x2012):  /* M-Audio Fast Track Pro USB */
984                 return snd_usb_fasttrackpro_boot_quirk(dev);
985         }
986
987         return 0;
988 }
989
990 /*
991  * check if the device uses big-endian samples
992  */
993 int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
994 {
995         /* it depends on altsetting whether the device is big-endian or not */
996         switch (chip->usb_id) {
997         case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
998                 if (fp->altsetting == 2 || fp->altsetting == 3 ||
999                         fp->altsetting == 5 || fp->altsetting == 6)
1000                         return 1;
1001                 break;
1002         case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
1003                 if (chip->setup == 0x00 ||
1004                         fp->altsetting == 1 || fp->altsetting == 2 ||
1005                         fp->altsetting == 3)
1006                         return 1;
1007                 break;
1008         case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro */
1009                 if (fp->altsetting == 2 || fp->altsetting == 3 ||
1010                         fp->altsetting == 5 || fp->altsetting == 6)
1011                         return 1;
1012                 break;
1013         }
1014         return 0;
1015 }
1016
1017 /*
1018  * For E-Mu 0404USB/0202USB/TrackerPre/0204 sample rate should be set for device,
1019  * not for interface.
1020  */
1021
1022 enum {
1023         EMU_QUIRK_SR_44100HZ = 0,
1024         EMU_QUIRK_SR_48000HZ,
1025         EMU_QUIRK_SR_88200HZ,
1026         EMU_QUIRK_SR_96000HZ,
1027         EMU_QUIRK_SR_176400HZ,
1028         EMU_QUIRK_SR_192000HZ
1029 };
1030
1031 static void set_format_emu_quirk(struct snd_usb_substream *subs,
1032                                  struct audioformat *fmt)
1033 {
1034         unsigned char emu_samplerate_id = 0;
1035
1036         /* When capture is active
1037          * sample rate shouldn't be changed
1038          * by playback substream
1039          */
1040         if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
1041                 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
1042                         return;
1043         }
1044
1045         switch (fmt->rate_min) {
1046         case 48000:
1047                 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
1048                 break;
1049         case 88200:
1050                 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
1051                 break;
1052         case 96000:
1053                 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
1054                 break;
1055         case 176400:
1056                 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
1057                 break;
1058         case 192000:
1059                 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
1060                 break;
1061         default:
1062                 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
1063                 break;
1064         }
1065         snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
1066         subs->pkt_offset_adj = (emu_samplerate_id >= EMU_QUIRK_SR_176400HZ) ? 4 : 0;
1067 }
1068
1069 void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
1070                               struct audioformat *fmt)
1071 {
1072         switch (subs->stream->chip->usb_id) {
1073         case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
1074         case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
1075         case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
1076         case USB_ID(0x041e, 0x3f19): /* E-Mu 0204 USB */
1077                 set_format_emu_quirk(subs, fmt);
1078                 break;
1079         }
1080 }
1081
1082 void snd_usb_endpoint_start_quirk(struct snd_usb_endpoint *ep)
1083 {
1084         /*
1085          * "Playback Design" products send bogus feedback data at the start
1086          * of the stream. Ignore them.
1087          */
1088         if ((le16_to_cpu(ep->chip->dev->descriptor.idVendor) == 0x23ba) &&
1089             ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
1090                 ep->skip_packets = 4;
1091
1092         /*
1093          * M-Audio Fast Track C400/C600 - when packets are not skipped, real
1094          * world latency varies by approx. +/- 50 frames (at 96KHz) each time
1095          * the stream is (re)started. When skipping packets 16 at endpoint
1096          * start up, the real world latency is stable within +/- 1 frame (also
1097          * across power cycles).
1098          */
1099         if ((ep->chip->usb_id == USB_ID(0x0763, 0x2030) ||
1100              ep->chip->usb_id == USB_ID(0x0763, 0x2031)) &&
1101             ep->type == SND_USB_ENDPOINT_TYPE_DATA)
1102                 ep->skip_packets = 16;
1103 }
1104
1105 void snd_usb_set_interface_quirk(struct usb_device *dev)
1106 {
1107         /*
1108          * "Playback Design" products need a 50ms delay after setting the
1109          * USB interface.
1110          */
1111         if (le16_to_cpu(dev->descriptor.idVendor) == 0x23ba)
1112                 mdelay(50);
1113 }
1114
1115 void snd_usb_ctl_msg_quirk(struct usb_device *dev, unsigned int pipe,
1116                            __u8 request, __u8 requesttype, __u16 value,
1117                            __u16 index, void *data, __u16 size)
1118 {
1119         /*
1120          * "Playback Design" products need a 20ms delay after each
1121          * class compliant request
1122          */
1123         if ((le16_to_cpu(dev->descriptor.idVendor) == 0x23ba) &&
1124             (requesttype & USB_TYPE_MASK) == USB_TYPE_CLASS)
1125                 mdelay(20);
1126 }
1127
1128 /*
1129  * snd_usb_interface_dsd_format_quirks() is called from format.c to
1130  * augment the PCM format bit-field for DSD types. The UAC standards
1131  * don't have a designated bit field to denote DSD-capable interfaces,
1132  * hence all hardware that is known to support this format has to be
1133  * listed here.
1134  */
1135 u64 snd_usb_interface_dsd_format_quirks(struct snd_usb_audio *chip,
1136                                         struct audioformat *fp,
1137                                         unsigned int sample_bytes)
1138 {
1139         /* Playback Designs */
1140         if (le16_to_cpu(chip->dev->descriptor.idVendor) == 0x23ba) {
1141                 switch (fp->altsetting) {
1142                 case 1:
1143                         fp->dsd_dop = true;
1144                         return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1145                 case 2:
1146                         fp->dsd_bitrev = true;
1147                         return SNDRV_PCM_FMTBIT_DSD_U8;
1148                 case 3:
1149                         fp->dsd_bitrev = true;
1150                         return SNDRV_PCM_FMTBIT_DSD_U16_LE;
1151                 }
1152         }
1153
1154         return 0;
1155 }