ALSA: usb-mixer: factor out quirks
[linux-2.6-block.git] / sound / usb / quirks.c
CommitLineData
e5779998
DM
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/usb.h>
19#include <linux/usb/audio.h>
20
21#include <sound/core.h>
22#include <sound/info.h>
23#include <sound/pcm.h>
24
25#include "usbaudio.h"
26#include "card.h"
27#include "usbmixer.h"
7b1eda22 28#include "mixer_quirks.h"
e5779998
DM
29#include "midi.h"
30#include "quirks.h"
31#include "helper.h"
32#include "endpoint.h"
33#include "pcm.h"
34
35/*
36 * handle the quirks for the contained interfaces
37 */
38static int create_composite_quirk(struct snd_usb_audio *chip,
39 struct usb_interface *iface,
40 struct usb_driver *driver,
41 const struct snd_usb_audio_quirk *quirk)
42{
43 int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
44 int err;
45
46 for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
47 iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
48 if (!iface)
49 continue;
50 if (quirk->ifnum != probed_ifnum &&
51 usb_interface_claimed(iface))
52 continue;
53 err = snd_usb_create_quirk(chip, iface, driver, quirk);
54 if (err < 0)
55 return err;
56 if (quirk->ifnum != probed_ifnum)
57 usb_driver_claim_interface(driver, iface, (void *)-1L);
58 }
59 return 0;
60}
61
62static int ignore_interface_quirk(struct snd_usb_audio *chip,
63 struct usb_interface *iface,
64 struct usb_driver *driver,
65 const struct snd_usb_audio_quirk *quirk)
66{
67 return 0;
68}
69
70
71/*
72 * Allow alignment on audio sub-slot (channel samples) rather than
73 * on audio slots (audio frames)
74 */
75static int create_align_transfer_quirk(struct snd_usb_audio *chip,
76 struct usb_interface *iface,
77 struct usb_driver *driver,
78 const struct snd_usb_audio_quirk *quirk)
79{
80 chip->txfr_quirk = 1;
81 return 1; /* Continue with creating streams and mixer */
82}
83
84static int create_any_midi_quirk(struct snd_usb_audio *chip,
85 struct usb_interface *intf,
86 struct usb_driver *driver,
87 const struct snd_usb_audio_quirk *quirk)
88{
89 return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
90}
91
92/*
93 * create a stream for an interface with proper descriptors
94 */
95static int create_standard_audio_quirk(struct snd_usb_audio *chip,
96 struct usb_interface *iface,
97 struct usb_driver *driver,
98 const struct snd_usb_audio_quirk *quirk)
99{
100 struct usb_host_interface *alts;
101 struct usb_interface_descriptor *altsd;
102 int err;
103
104 alts = &iface->altsetting[0];
105 altsd = get_iface_desc(alts);
106 err = snd_usb_parse_audio_endpoints(chip, altsd->bInterfaceNumber);
107 if (err < 0) {
108 snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
109 altsd->bInterfaceNumber, err);
110 return err;
111 }
112 /* reset the current interface */
113 usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
114 return 0;
115}
116
117/*
118 * create a stream for an endpoint/altsetting without proper descriptors
119 */
120static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
121 struct usb_interface *iface,
122 struct usb_driver *driver,
123 const struct snd_usb_audio_quirk *quirk)
124{
125 struct audioformat *fp;
126 struct usb_host_interface *alts;
127 int stream, err;
128 unsigned *rate_table = NULL;
129
130 fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
131 if (! fp) {
132 snd_printk(KERN_ERR "cannot memdup\n");
133 return -ENOMEM;
134 }
135 if (fp->nr_rates > 0) {
136 rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
137 if (!rate_table) {
138 kfree(fp);
139 return -ENOMEM;
140 }
141 memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
142 fp->rate_table = rate_table;
143 }
144
145 stream = (fp->endpoint & USB_DIR_IN)
146 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
147 err = snd_usb_add_audio_endpoint(chip, stream, fp);
148 if (err < 0) {
149 kfree(fp);
150 kfree(rate_table);
151 return err;
152 }
153 if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
154 fp->altset_idx >= iface->num_altsetting) {
155 kfree(fp);
156 kfree(rate_table);
157 return -EINVAL;
158 }
159 alts = &iface->altsetting[fp->altset_idx];
160 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
161 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
162 usb_set_interface(chip->dev, fp->iface, 0);
767d75ad
DM
163 snd_usb_init_pitch(chip, fp->iface, alts, fp);
164 snd_usb_init_sample_rate(chip, fp->iface, alts, fp, fp->rate_max);
e5779998
DM
165 return 0;
166}
167
168/*
169 * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
170 * The only way to detect the sample rate is by looking at wMaxPacketSize.
171 */
172static int create_uaxx_quirk(struct snd_usb_audio *chip,
173 struct usb_interface *iface,
174 struct usb_driver *driver,
175 const struct snd_usb_audio_quirk *quirk)
176{
177 static const struct audioformat ua_format = {
015eb0b0 178 .formats = SNDRV_PCM_FMTBIT_S24_3LE,
e5779998
DM
179 .channels = 2,
180 .fmt_type = UAC_FORMAT_TYPE_I,
181 .altsetting = 1,
182 .altset_idx = 1,
183 .rates = SNDRV_PCM_RATE_CONTINUOUS,
184 };
185 struct usb_host_interface *alts;
186 struct usb_interface_descriptor *altsd;
187 struct audioformat *fp;
188 int stream, err;
189
190 /* both PCM and MIDI interfaces have 2 or more altsettings */
191 if (iface->num_altsetting < 2)
192 return -ENXIO;
193 alts = &iface->altsetting[1];
194 altsd = get_iface_desc(alts);
195
196 if (altsd->bNumEndpoints == 2) {
197 static const struct snd_usb_midi_endpoint_info ua700_ep = {
198 .out_cables = 0x0003,
199 .in_cables = 0x0003
200 };
201 static const struct snd_usb_audio_quirk ua700_quirk = {
202 .type = QUIRK_MIDI_FIXED_ENDPOINT,
203 .data = &ua700_ep
204 };
205 static const struct snd_usb_midi_endpoint_info uaxx_ep = {
206 .out_cables = 0x0001,
207 .in_cables = 0x0001
208 };
209 static const struct snd_usb_audio_quirk uaxx_quirk = {
210 .type = QUIRK_MIDI_FIXED_ENDPOINT,
211 .data = &uaxx_ep
212 };
213 const struct snd_usb_audio_quirk *quirk =
214 chip->usb_id == USB_ID(0x0582, 0x002b)
215 ? &ua700_quirk : &uaxx_quirk;
216 return snd_usbmidi_create(chip->card, iface,
217 &chip->midi_list, quirk);
218 }
219
220 if (altsd->bNumEndpoints != 1)
221 return -ENXIO;
222
223 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
224 if (!fp)
225 return -ENOMEM;
226 memcpy(fp, &ua_format, sizeof(*fp));
227
228 fp->iface = altsd->bInterfaceNumber;
229 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
230 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
231 fp->datainterval = 0;
232 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
233
234 switch (fp->maxpacksize) {
235 case 0x120:
236 fp->rate_max = fp->rate_min = 44100;
237 break;
238 case 0x138:
239 case 0x140:
240 fp->rate_max = fp->rate_min = 48000;
241 break;
242 case 0x258:
243 case 0x260:
244 fp->rate_max = fp->rate_min = 96000;
245 break;
246 default:
247 snd_printk(KERN_ERR "unknown sample rate\n");
248 kfree(fp);
249 return -ENXIO;
250 }
251
252 stream = (fp->endpoint & USB_DIR_IN)
253 ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
254 err = snd_usb_add_audio_endpoint(chip, stream, fp);
255 if (err < 0) {
256 kfree(fp);
257 return err;
258 }
259 usb_set_interface(chip->dev, fp->iface, 0);
260 return 0;
261}
262
263/*
264 * audio-interface quirks
265 *
266 * returns zero if no standard audio/MIDI parsing is needed.
267 * returns a postive value if standard audio/midi interfaces are parsed
268 * after this.
269 * returns a negative value at error.
270 */
271int snd_usb_create_quirk(struct snd_usb_audio *chip,
272 struct usb_interface *iface,
273 struct usb_driver *driver,
274 const struct snd_usb_audio_quirk *quirk)
275{
276 typedef int (*quirk_func_t)(struct snd_usb_audio *,
277 struct usb_interface *,
278 struct usb_driver *,
279 const struct snd_usb_audio_quirk *);
280 static const quirk_func_t quirk_funcs[] = {
281 [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
282 [QUIRK_COMPOSITE] = create_composite_quirk,
283 [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
284 [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
285 [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
286 [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
287 [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
288 [QUIRK_MIDI_FASTLANE] = create_any_midi_quirk,
289 [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
290 [QUIRK_MIDI_CME] = create_any_midi_quirk,
291 [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
292 [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
293 [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
294 [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk
295 };
296
297 if (quirk->type < QUIRK_TYPE_COUNT) {
298 return quirk_funcs[quirk->type](chip, iface, driver, quirk);
299 } else {
300 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
301 return -ENXIO;
302 }
303}
304
305/*
306 * boot quirks
307 */
308
309#define EXTIGY_FIRMWARE_SIZE_OLD 794
310#define EXTIGY_FIRMWARE_SIZE_NEW 483
311
312static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
313{
314 struct usb_host_config *config = dev->actconfig;
315 int err;
316
317 if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
318 le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
319 snd_printdd("sending Extigy boot sequence...\n");
320 /* Send message to force it to reconnect with full interface. */
321 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
322 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
323 if (err < 0) snd_printdd("error sending boot message: %d\n", err);
324 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
325 &dev->descriptor, sizeof(dev->descriptor));
326 config = dev->actconfig;
327 if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
328 err = usb_reset_configuration(dev);
329 if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
330 snd_printdd("extigy_boot: new boot length = %d\n",
331 le16_to_cpu(get_cfg_desc(config)->wTotalLength));
332 return -ENODEV; /* quit this anyway */
333 }
334 return 0;
335}
336
337static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
338{
339 u8 buf = 1;
340
341 snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
342 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
343 0, 0, &buf, 1, 1000);
344 if (buf == 0) {
345 snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
346 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
347 1, 2000, NULL, 0, 1000);
348 return -ENODEV;
349 }
350 return 0;
351}
352
353/*
354 * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
355 * documented in the device's data sheet.
356 */
357static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
358{
359 u8 buf[4];
360 buf[0] = 0x20;
361 buf[1] = value & 0xff;
362 buf[2] = (value >> 8) & 0xff;
363 buf[3] = reg;
364 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
365 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
366 0, 0, &buf, 4, 1000);
367}
368
369static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
370{
371 /*
372 * Enable line-out driver mode, set headphone source to front
373 * channels, enable stereo mic.
374 */
375 return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
376}
377
378/*
379 * C-Media CM6206 is based on CM106 with two additional
380 * registers that are not documented in the data sheet.
381 * Values here are chosen based on sniffing USB traffic
382 * under Windows.
383 */
384static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
385{
386 int err, reg;
387 int val[] = {0x200c, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
388
389 for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
390 err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
391 if (err < 0)
392 return err;
393 }
394
395 return err;
396}
397
398/*
399 * This call will put the synth in "USB send" mode, i.e it will send MIDI
400 * messages through USB (this is disabled at startup). The synth will
401 * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
402 * sign on its LCD. Values here are chosen based on sniffing USB traffic
403 * under Windows.
404 */
405static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
406{
407 int err, actual_length;
408
409 /* "midi send" enable */
410 static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
411
412 void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
413 if (!buf)
414 return -ENOMEM;
415 err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
416 ARRAY_SIZE(seq), &actual_length, 1000);
417 kfree(buf);
418 if (err < 0)
419 return err;
420
421 return 0;
422}
423
424/*
425 * Setup quirks
426 */
427#define AUDIOPHILE_SET 0x01 /* if set, parse device_setup */
428#define AUDIOPHILE_SET_DTS 0x02 /* if set, enable DTS Digital Output */
429#define AUDIOPHILE_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
430#define AUDIOPHILE_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
431#define AUDIOPHILE_SET_DI 0x10 /* if set, enable Digital Input */
432#define AUDIOPHILE_SET_MASK 0x1F /* bit mask for setup value */
433#define AUDIOPHILE_SET_24B_48K_DI 0x19 /* value for 24bits+48KHz+Digital Input */
434#define AUDIOPHILE_SET_24B_48K_NOTDI 0x09 /* value for 24bits+48KHz+No Digital Input */
435#define AUDIOPHILE_SET_16B_48K_DI 0x11 /* value for 16bits+48KHz+Digital Input */
436#define AUDIOPHILE_SET_16B_48K_NOTDI 0x01 /* value for 16bits+48KHz+No Digital Input */
437
438static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
439 int iface,
440 int altno)
441{
442 /* Reset ALL ifaces to 0 altsetting.
443 * Call it for every possible altsetting of every interface.
444 */
445 usb_set_interface(chip->dev, iface, 0);
446
447 if (chip->setup & AUDIOPHILE_SET) {
448 if ((chip->setup & AUDIOPHILE_SET_DTS)
449 && altno != 6)
450 return 1; /* skip this altsetting */
451 if ((chip->setup & AUDIOPHILE_SET_96K)
452 && altno != 1)
453 return 1; /* skip this altsetting */
454 if ((chip->setup & AUDIOPHILE_SET_MASK) ==
455 AUDIOPHILE_SET_24B_48K_DI && altno != 2)
456 return 1; /* skip this altsetting */
457 if ((chip->setup & AUDIOPHILE_SET_MASK) ==
458 AUDIOPHILE_SET_24B_48K_NOTDI && altno != 3)
459 return 1; /* skip this altsetting */
460 if ((chip->setup & AUDIOPHILE_SET_MASK) ==
461 AUDIOPHILE_SET_16B_48K_DI && altno != 4)
462 return 1; /* skip this altsetting */
463 if ((chip->setup & AUDIOPHILE_SET_MASK) ==
464 AUDIOPHILE_SET_16B_48K_NOTDI && altno != 5)
465 return 1; /* skip this altsetting */
466 }
467
468 return 0; /* keep this altsetting */
469}
470
471int snd_usb_apply_interface_quirk(struct snd_usb_audio *chip,
472 int iface,
473 int altno)
474{
475 /* audiophile usb: skip altsets incompatible with device_setup */
476 if (chip->usb_id == USB_ID(0x0763, 0x2003))
477 return audiophile_skip_setting_quirk(chip, iface, altno);
478
479 return 0;
480}
481
482int snd_usb_apply_boot_quirk(struct usb_device *dev,
483 struct usb_interface *intf,
484 const struct snd_usb_audio_quirk *quirk)
485{
486 u32 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
487 le16_to_cpu(dev->descriptor.idProduct));
488
489 /* SB Extigy needs special boot-up sequence */
490 /* if more models come, this will go to the quirk list. */
491 if (id == USB_ID(0x041e, 0x3000))
492 return snd_usb_extigy_boot_quirk(dev, intf);
493
494 /* SB Audigy 2 NX needs its own boot-up magic, too */
495 if (id == USB_ID(0x041e, 0x3020))
496 return snd_usb_audigy2nx_boot_quirk(dev);
497
498 /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
499 if (id == USB_ID(0x10f5, 0x0200))
500 return snd_usb_cm106_boot_quirk(dev);
501
502 /* C-Media CM6206 / CM106-Like Sound Device */
503 if (id == USB_ID(0x0d8c, 0x0102))
504 return snd_usb_cm6206_boot_quirk(dev);
505
506 /* Access Music VirusTI Desktop */
507 if (id == USB_ID(0x133e, 0x0815))
508 return snd_usb_accessmusic_boot_quirk(dev);
509
510 return 0;
511}
512
513/*
514 * check if the device uses big-endian samples
515 */
516int snd_usb_is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
517{
518 switch (chip->usb_id) {
519 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
520 if (fp->endpoint & USB_DIR_IN)
521 return 1;
522 break;
523 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
524 if (chip->setup == 0x00 ||
525 fp->altsetting==1 || fp->altsetting==2 || fp->altsetting==3)
526 return 1;
527 }
528 return 0;
529}
530
531/*
532 * For E-Mu 0404USB/0202USB/TrackerPre sample rate should be set for device,
533 * not for interface.
534 */
535
536enum {
537 EMU_QUIRK_SR_44100HZ = 0,
538 EMU_QUIRK_SR_48000HZ,
539 EMU_QUIRK_SR_88200HZ,
540 EMU_QUIRK_SR_96000HZ,
541 EMU_QUIRK_SR_176400HZ,
542 EMU_QUIRK_SR_192000HZ
543};
544
545static void set_format_emu_quirk(struct snd_usb_substream *subs,
546 struct audioformat *fmt)
547{
548 unsigned char emu_samplerate_id = 0;
549
550 /* When capture is active
551 * sample rate shouldn't be changed
552 * by playback substream
553 */
554 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
555 if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
556 return;
557 }
558
559 switch (fmt->rate_min) {
560 case 48000:
561 emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
562 break;
563 case 88200:
564 emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
565 break;
566 case 96000:
567 emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
568 break;
569 case 176400:
570 emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
571 break;
572 case 192000:
573 emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
574 break;
575 default:
576 emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
577 break;
578 }
579 snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
580}
581
582void snd_usb_set_format_quirk(struct snd_usb_substream *subs,
583 struct audioformat *fmt)
584{
585 switch (subs->stream->chip->usb_id) {
586 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
587 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
588 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
589 set_format_emu_quirk(subs, fmt);
590 break;
591 }
592}
593