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