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