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