1 /* 2 * (Tentative) USB Audio Driver for ALSA 3 * 4 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de> 5 * 6 * Many codes borrowed from audio.c by 7 * Alan Cox (alan@lxorguk.ukuu.org.uk) 8 * Thomas Sailer (sailer@ife.ee.ethz.ch) 9 * 10 * Audio Class 3.0 support by Ruslan Bilovol <ruslan.bilovol@gmail.com> 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, write to the Free Software 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 25 * 26 * 27 * NOTES: 28 * 29 * - the linked URBs would be preferred but not used so far because of 30 * the instability of unlinking. 31 * - type II is not supported properly. there is no device which supports 32 * this type *correctly*. SB extigy looks as if it supports, but it's 33 * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream). 34 */ 35 36 37 #include <linux/bitops.h> 38 #include <linux/init.h> 39 #include <linux/list.h> 40 #include <linux/slab.h> 41 #include <linux/string.h> 42 #include <linux/ctype.h> 43 #include <linux/usb.h> 44 #include <linux/moduleparam.h> 45 #include <linux/mutex.h> 46 #include <linux/usb/audio.h> 47 #include <linux/usb/audio-v2.h> 48 #include <linux/usb/audio-v3.h> 49 #include <linux/module.h> 50 51 #include <sound/control.h> 52 #include <sound/core.h> 53 #include <sound/info.h> 54 #include <sound/pcm.h> 55 #include <sound/pcm_params.h> 56 #include <sound/initval.h> 57 58 #include "usbaudio.h" 59 #include "card.h" 60 #include "midi.h" 61 #include "mixer.h" 62 #include "proc.h" 63 #include "quirks.h" 64 #include "endpoint.h" 65 #include "helper.h" 66 #include "debug.h" 67 #include "pcm.h" 68 #include "format.h" 69 #include "power.h" 70 #include "stream.h" 71 #include "media.h" 72 73 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>"); 74 MODULE_DESCRIPTION("USB Audio"); 75 MODULE_LICENSE("GPL"); 76 MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}"); 77 78 79 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ 80 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ 81 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */ 82 /* Vendor/product IDs for this card */ 83 static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; 84 static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; 85 static int device_setup[SNDRV_CARDS]; /* device parameter for this card */ 86 static bool ignore_ctl_error; 87 static bool autoclock = true; 88 static char *quirk_alias[SNDRV_CARDS]; 89 90 bool snd_usb_use_vmalloc = true; 91 92 module_param_array(index, int, NULL, 0444); 93 MODULE_PARM_DESC(index, "Index value for the USB audio adapter."); 94 module_param_array(id, charp, NULL, 0444); 95 MODULE_PARM_DESC(id, "ID string for the USB audio adapter."); 96 module_param_array(enable, bool, NULL, 0444); 97 MODULE_PARM_DESC(enable, "Enable USB audio adapter."); 98 module_param_array(vid, int, NULL, 0444); 99 MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device."); 100 module_param_array(pid, int, NULL, 0444); 101 MODULE_PARM_DESC(pid, "Product ID for the USB audio device."); 102 module_param_array(device_setup, int, NULL, 0444); 103 MODULE_PARM_DESC(device_setup, "Specific device setup (if needed)."); 104 module_param(ignore_ctl_error, bool, 0444); 105 MODULE_PARM_DESC(ignore_ctl_error, 106 "Ignore errors from USB controller for mixer interfaces."); 107 module_param(autoclock, bool, 0444); 108 MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes)."); 109 module_param_array(quirk_alias, charp, NULL, 0444); 110 MODULE_PARM_DESC(quirk_alias, "Quirk aliases, e.g. 0123abcd:5678beef."); 111 module_param_named(use_vmalloc, snd_usb_use_vmalloc, bool, 0444); 112 MODULE_PARM_DESC(use_vmalloc, "Use vmalloc for PCM intermediate buffers (default: yes)."); 113 114 /* 115 * we keep the snd_usb_audio_t instances by ourselves for merging 116 * the all interfaces on the same card as one sound device. 117 */ 118 119 static DEFINE_MUTEX(register_mutex); 120 static struct snd_usb_audio *usb_chip[SNDRV_CARDS]; 121 static struct usb_driver usb_audio_driver; 122 123 /* 124 * disconnect streams 125 * called from usb_audio_disconnect() 126 */ 127 static void snd_usb_stream_disconnect(struct snd_usb_stream *as) 128 { 129 int idx; 130 struct snd_usb_substream *subs; 131 132 for (idx = 0; idx < 2; idx++) { 133 subs = &as->substream[idx]; 134 if (!subs->num_formats) 135 continue; 136 subs->interface = -1; 137 subs->data_endpoint = NULL; 138 subs->sync_endpoint = NULL; 139 } 140 } 141 142 static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface) 143 { 144 struct usb_device *dev = chip->dev; 145 struct usb_host_interface *alts; 146 struct usb_interface_descriptor *altsd; 147 struct usb_interface *iface = usb_ifnum_to_if(dev, interface); 148 149 if (!iface) { 150 dev_err(&dev->dev, "%u:%d : does not exist\n", 151 ctrlif, interface); 152 return -EINVAL; 153 } 154 155 alts = &iface->altsetting[0]; 156 altsd = get_iface_desc(alts); 157 158 /* 159 * Android with both accessory and audio interfaces enabled gets the 160 * interface numbers wrong. 161 */ 162 if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) || 163 chip->usb_id == USB_ID(0x18d1, 0x2d05)) && 164 interface == 0 && 165 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC && 166 altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) { 167 interface = 2; 168 iface = usb_ifnum_to_if(dev, interface); 169 if (!iface) 170 return -EINVAL; 171 alts = &iface->altsetting[0]; 172 altsd = get_iface_desc(alts); 173 } 174 175 if (usb_interface_claimed(iface)) { 176 dev_dbg(&dev->dev, "%d:%d: skipping, already claimed\n", 177 ctrlif, interface); 178 return -EINVAL; 179 } 180 181 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO || 182 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) && 183 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) { 184 int err = __snd_usbmidi_create(chip->card, iface, 185 &chip->midi_list, NULL, 186 chip->usb_id); 187 if (err < 0) { 188 dev_err(&dev->dev, 189 "%u:%d: cannot create sequencer device\n", 190 ctrlif, interface); 191 return -EINVAL; 192 } 193 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); 194 195 return 0; 196 } 197 198 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO && 199 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || 200 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) { 201 dev_dbg(&dev->dev, 202 "%u:%d: skipping non-supported interface %d\n", 203 ctrlif, interface, altsd->bInterfaceClass); 204 /* skip non-supported classes */ 205 return -EINVAL; 206 } 207 208 if (snd_usb_get_speed(dev) == USB_SPEED_LOW) { 209 dev_err(&dev->dev, "low speed audio streaming not supported\n"); 210 return -EINVAL; 211 } 212 213 if (! snd_usb_parse_audio_interface(chip, interface)) { 214 usb_set_interface(dev, interface, 0); /* reset the current interface */ 215 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); 216 } 217 218 return 0; 219 } 220 221 /* 222 * parse audio control descriptor and create pcm/midi streams 223 */ 224 static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif) 225 { 226 struct usb_device *dev = chip->dev; 227 struct usb_host_interface *host_iface; 228 struct usb_interface_descriptor *altsd; 229 int i, protocol; 230 231 /* find audiocontrol interface */ 232 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0]; 233 altsd = get_iface_desc(host_iface); 234 protocol = altsd->bInterfaceProtocol; 235 236 switch (protocol) { 237 default: 238 dev_warn(&dev->dev, 239 "unknown interface protocol %#02x, assuming v1\n", 240 protocol); 241 /* fall through */ 242 243 case UAC_VERSION_1: { 244 struct uac1_ac_header_descriptor *h1; 245 int rest_bytes; 246 247 h1 = snd_usb_find_csint_desc(host_iface->extra, 248 host_iface->extralen, 249 NULL, UAC_HEADER); 250 if (!h1 || h1->bLength < sizeof(*h1)) { 251 dev_err(&dev->dev, "cannot find UAC_HEADER\n"); 252 return -EINVAL; 253 } 254 255 rest_bytes = (void *)(host_iface->extra + 256 host_iface->extralen) - (void *)h1; 257 258 /* just to be sure -- this shouldn't hit at all */ 259 if (rest_bytes <= 0) { 260 dev_err(&dev->dev, "invalid control header\n"); 261 return -EINVAL; 262 } 263 264 if (rest_bytes < sizeof(*h1)) { 265 dev_err(&dev->dev, "too short v1 buffer descriptor\n"); 266 return -EINVAL; 267 } 268 269 if (!h1->bInCollection) { 270 dev_info(&dev->dev, "skipping empty audio interface (v1)\n"); 271 return -EINVAL; 272 } 273 274 if (rest_bytes < h1->bLength) { 275 dev_err(&dev->dev, "invalid buffer length (v1)\n"); 276 return -EINVAL; 277 } 278 279 if (h1->bLength < sizeof(*h1) + h1->bInCollection) { 280 dev_err(&dev->dev, "invalid UAC_HEADER (v1)\n"); 281 return -EINVAL; 282 } 283 284 for (i = 0; i < h1->bInCollection; i++) 285 snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]); 286 287 break; 288 } 289 290 case UAC_VERSION_2: 291 case UAC_VERSION_3: { 292 struct usb_interface_assoc_descriptor *assoc = 293 usb_ifnum_to_if(dev, ctrlif)->intf_assoc; 294 295 if (!assoc) { 296 /* 297 * Firmware writers cannot count to three. So to find 298 * the IAD on the NuForce UDH-100, also check the next 299 * interface. 300 */ 301 struct usb_interface *iface = 302 usb_ifnum_to_if(dev, ctrlif + 1); 303 if (iface && 304 iface->intf_assoc && 305 iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO && 306 iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2) 307 assoc = iface->intf_assoc; 308 } 309 310 if (!assoc) { 311 dev_err(&dev->dev, "Audio class v2/v3 interfaces need an interface association\n"); 312 return -EINVAL; 313 } 314 315 if (protocol == UAC_VERSION_3) { 316 int badd = assoc->bFunctionSubClass; 317 318 if (badd != UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0 && 319 (badd < UAC3_FUNCTION_SUBCLASS_GENERIC_IO || 320 badd > UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE)) { 321 dev_err(&dev->dev, 322 "Unsupported UAC3 BADD profile\n"); 323 return -EINVAL; 324 } 325 326 chip->badd_profile = badd; 327 } 328 329 for (i = 0; i < assoc->bInterfaceCount; i++) { 330 int intf = assoc->bFirstInterface + i; 331 332 if (intf != ctrlif) 333 snd_usb_create_stream(chip, ctrlif, intf); 334 } 335 336 break; 337 } 338 } 339 340 return 0; 341 } 342 343 /* 344 * free the chip instance 345 * 346 * here we have to do not much, since pcm and controls are already freed 347 * 348 */ 349 350 static void snd_usb_audio_free(struct snd_card *card) 351 { 352 struct snd_usb_audio *chip = card->private_data; 353 struct snd_usb_endpoint *ep, *n; 354 355 list_for_each_entry_safe(ep, n, &chip->ep_list, list) 356 snd_usb_endpoint_free(ep); 357 358 mutex_destroy(&chip->mutex); 359 if (!atomic_read(&chip->shutdown)) 360 dev_set_drvdata(&chip->dev->dev, NULL); 361 } 362 363 static void usb_audio_make_shortname(struct usb_device *dev, 364 struct snd_usb_audio *chip, 365 const struct snd_usb_audio_quirk *quirk) 366 { 367 struct snd_card *card = chip->card; 368 369 if (quirk && quirk->product_name && *quirk->product_name) { 370 strlcpy(card->shortname, quirk->product_name, 371 sizeof(card->shortname)); 372 return; 373 } 374 375 /* retrieve the device string as shortname */ 376 if (!dev->descriptor.iProduct || 377 usb_string(dev, dev->descriptor.iProduct, 378 card->shortname, sizeof(card->shortname)) <= 0) { 379 /* no name available from anywhere, so use ID */ 380 sprintf(card->shortname, "USB Device %#04x:%#04x", 381 USB_ID_VENDOR(chip->usb_id), 382 USB_ID_PRODUCT(chip->usb_id)); 383 } 384 385 strim(card->shortname); 386 } 387 388 static void usb_audio_make_longname(struct usb_device *dev, 389 struct snd_usb_audio *chip, 390 const struct snd_usb_audio_quirk *quirk) 391 { 392 struct snd_card *card = chip->card; 393 int len; 394 395 /* shortcut - if any pre-defined string is given, use it */ 396 if (quirk && quirk->profile_name && *quirk->profile_name) { 397 strlcpy(card->longname, quirk->profile_name, 398 sizeof(card->longname)); 399 return; 400 } 401 402 if (quirk && quirk->vendor_name && *quirk->vendor_name) { 403 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname)); 404 } else { 405 /* retrieve the vendor and device strings as longname */ 406 if (dev->descriptor.iManufacturer) 407 len = usb_string(dev, dev->descriptor.iManufacturer, 408 card->longname, sizeof(card->longname)); 409 else 410 len = 0; 411 /* we don't really care if there isn't any vendor string */ 412 } 413 if (len > 0) { 414 strim(card->longname); 415 if (*card->longname) 416 strlcat(card->longname, " ", sizeof(card->longname)); 417 } 418 419 strlcat(card->longname, card->shortname, sizeof(card->longname)); 420 421 len = strlcat(card->longname, " at ", sizeof(card->longname)); 422 423 if (len < sizeof(card->longname)) 424 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len); 425 426 switch (snd_usb_get_speed(dev)) { 427 case USB_SPEED_LOW: 428 strlcat(card->longname, ", low speed", sizeof(card->longname)); 429 break; 430 case USB_SPEED_FULL: 431 strlcat(card->longname, ", full speed", sizeof(card->longname)); 432 break; 433 case USB_SPEED_HIGH: 434 strlcat(card->longname, ", high speed", sizeof(card->longname)); 435 break; 436 case USB_SPEED_SUPER: 437 strlcat(card->longname, ", super speed", sizeof(card->longname)); 438 break; 439 case USB_SPEED_SUPER_PLUS: 440 strlcat(card->longname, ", super speed plus", sizeof(card->longname)); 441 break; 442 default: 443 break; 444 } 445 } 446 447 /* 448 * create a chip instance and set its names. 449 */ 450 static int snd_usb_audio_create(struct usb_interface *intf, 451 struct usb_device *dev, int idx, 452 const struct snd_usb_audio_quirk *quirk, 453 unsigned int usb_id, 454 struct snd_usb_audio **rchip) 455 { 456 struct snd_card *card; 457 struct snd_usb_audio *chip; 458 int err; 459 char component[14]; 460 461 *rchip = NULL; 462 463 switch (snd_usb_get_speed(dev)) { 464 case USB_SPEED_LOW: 465 case USB_SPEED_FULL: 466 case USB_SPEED_HIGH: 467 case USB_SPEED_WIRELESS: 468 case USB_SPEED_SUPER: 469 case USB_SPEED_SUPER_PLUS: 470 break; 471 default: 472 dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev)); 473 return -ENXIO; 474 } 475 476 err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE, 477 sizeof(*chip), &card); 478 if (err < 0) { 479 dev_err(&dev->dev, "cannot create card instance %d\n", idx); 480 return err; 481 } 482 483 chip = card->private_data; 484 mutex_init(&chip->mutex); 485 init_waitqueue_head(&chip->shutdown_wait); 486 chip->index = idx; 487 chip->dev = dev; 488 chip->card = card; 489 chip->setup = device_setup[idx]; 490 chip->autoclock = autoclock; 491 atomic_set(&chip->active, 1); /* avoid autopm during probing */ 492 atomic_set(&chip->usage_count, 0); 493 atomic_set(&chip->shutdown, 0); 494 495 chip->usb_id = usb_id; 496 INIT_LIST_HEAD(&chip->pcm_list); 497 INIT_LIST_HEAD(&chip->ep_list); 498 INIT_LIST_HEAD(&chip->midi_list); 499 INIT_LIST_HEAD(&chip->mixer_list); 500 501 card->private_free = snd_usb_audio_free; 502 503 strcpy(card->driver, "USB-Audio"); 504 sprintf(component, "USB%04x:%04x", 505 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id)); 506 snd_component_add(card, component); 507 508 usb_audio_make_shortname(dev, chip, quirk); 509 usb_audio_make_longname(dev, chip, quirk); 510 511 snd_usb_audio_create_proc(chip); 512 513 *rchip = chip; 514 return 0; 515 } 516 517 /* look for a matching quirk alias id */ 518 static bool get_alias_id(struct usb_device *dev, unsigned int *id) 519 { 520 int i; 521 unsigned int src, dst; 522 523 for (i = 0; i < ARRAY_SIZE(quirk_alias); i++) { 524 if (!quirk_alias[i] || 525 sscanf(quirk_alias[i], "%x:%x", &src, &dst) != 2 || 526 src != *id) 527 continue; 528 dev_info(&dev->dev, 529 "device (%04x:%04x): applying quirk alias %04x:%04x\n", 530 USB_ID_VENDOR(*id), USB_ID_PRODUCT(*id), 531 USB_ID_VENDOR(dst), USB_ID_PRODUCT(dst)); 532 *id = dst; 533 return true; 534 } 535 536 return false; 537 } 538 539 static const struct usb_device_id usb_audio_ids[]; /* defined below */ 540 541 /* look for the corresponding quirk */ 542 static const struct snd_usb_audio_quirk * 543 get_alias_quirk(struct usb_device *dev, unsigned int id) 544 { 545 const struct usb_device_id *p; 546 547 for (p = usb_audio_ids; p->match_flags; p++) { 548 /* FIXME: this checks only vendor:product pair in the list */ 549 if ((p->match_flags & USB_DEVICE_ID_MATCH_DEVICE) == 550 USB_DEVICE_ID_MATCH_DEVICE && 551 p->idVendor == USB_ID_VENDOR(id) && 552 p->idProduct == USB_ID_PRODUCT(id)) 553 return (const struct snd_usb_audio_quirk *)p->driver_info; 554 } 555 556 return NULL; 557 } 558 559 /* 560 * probe the active usb device 561 * 562 * note that this can be called multiple times per a device, when it 563 * includes multiple audio control interfaces. 564 * 565 * thus we check the usb device pointer and creates the card instance 566 * only at the first time. the successive calls of this function will 567 * append the pcm interface to the corresponding card. 568 */ 569 static int usb_audio_probe(struct usb_interface *intf, 570 const struct usb_device_id *usb_id) 571 { 572 struct usb_device *dev = interface_to_usbdev(intf); 573 const struct snd_usb_audio_quirk *quirk = 574 (const struct snd_usb_audio_quirk *)usb_id->driver_info; 575 struct snd_usb_audio *chip; 576 int i, err; 577 struct usb_host_interface *alts; 578 int ifnum; 579 u32 id; 580 581 alts = &intf->altsetting[0]; 582 ifnum = get_iface_desc(alts)->bInterfaceNumber; 583 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor), 584 le16_to_cpu(dev->descriptor.idProduct)); 585 if (get_alias_id(dev, &id)) 586 quirk = get_alias_quirk(dev, id); 587 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum) 588 return -ENXIO; 589 590 err = snd_usb_apply_boot_quirk(dev, intf, quirk, id); 591 if (err < 0) 592 return err; 593 594 /* 595 * found a config. now register to ALSA 596 */ 597 598 /* check whether it's already registered */ 599 chip = NULL; 600 mutex_lock(®ister_mutex); 601 for (i = 0; i < SNDRV_CARDS; i++) { 602 if (usb_chip[i] && usb_chip[i]->dev == dev) { 603 if (atomic_read(&usb_chip[i]->shutdown)) { 604 dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n"); 605 err = -EIO; 606 goto __error; 607 } 608 chip = usb_chip[i]; 609 atomic_inc(&chip->active); /* avoid autopm */ 610 break; 611 } 612 } 613 if (! chip) { 614 /* it's a fresh one. 615 * now look for an empty slot and create a new card instance 616 */ 617 for (i = 0; i < SNDRV_CARDS; i++) 618 if (!usb_chip[i] && 619 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) && 620 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) { 621 if (enable[i]) { 622 err = snd_usb_audio_create(intf, dev, i, quirk, 623 id, &chip); 624 if (err < 0) 625 goto __error; 626 chip->pm_intf = intf; 627 break; 628 } else if (vid[i] != -1 || pid[i] != -1) { 629 dev_info(&dev->dev, 630 "device (%04x:%04x) is disabled\n", 631 USB_ID_VENDOR(id), 632 USB_ID_PRODUCT(id)); 633 err = -ENOENT; 634 goto __error; 635 } 636 } 637 if (!chip) { 638 dev_err(&dev->dev, "no available usb audio device\n"); 639 err = -ENODEV; 640 goto __error; 641 } 642 } 643 dev_set_drvdata(&dev->dev, chip); 644 645 /* 646 * For devices with more than one control interface, we assume the 647 * first contains the audio controls. We might need a more specific 648 * check here in the future. 649 */ 650 if (!chip->ctrl_intf) 651 chip->ctrl_intf = alts; 652 653 chip->txfr_quirk = 0; 654 err = 1; /* continue */ 655 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) { 656 /* need some special handlings */ 657 err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk); 658 if (err < 0) 659 goto __error; 660 } 661 662 if (err > 0) { 663 /* create normal USB audio interfaces */ 664 err = snd_usb_create_streams(chip, ifnum); 665 if (err < 0) 666 goto __error; 667 err = snd_usb_create_mixer(chip, ifnum, ignore_ctl_error); 668 if (err < 0) 669 goto __error; 670 } 671 672 /* we are allowed to call snd_card_register() many times */ 673 err = snd_card_register(chip->card); 674 if (err < 0) 675 goto __error; 676 677 if (quirk && quirk->shares_media_device) { 678 /* don't want to fail when snd_media_device_create() fails */ 679 snd_media_device_create(chip, intf); 680 } 681 682 usb_chip[chip->index] = chip; 683 chip->num_interfaces++; 684 usb_set_intfdata(intf, chip); 685 atomic_dec(&chip->active); 686 mutex_unlock(®ister_mutex); 687 return 0; 688 689 __error: 690 if (chip) { 691 /* chip->active is inside the chip->card object, 692 * decrement before memory is possibly returned. 693 */ 694 atomic_dec(&chip->active); 695 if (!chip->num_interfaces) 696 snd_card_free(chip->card); 697 } 698 mutex_unlock(®ister_mutex); 699 return err; 700 } 701 702 /* 703 * we need to take care of counter, since disconnection can be called also 704 * many times as well as usb_audio_probe(). 705 */ 706 static void usb_audio_disconnect(struct usb_interface *intf) 707 { 708 struct snd_usb_audio *chip = usb_get_intfdata(intf); 709 struct snd_card *card; 710 struct list_head *p; 711 712 if (chip == (void *)-1L) 713 return; 714 715 card = chip->card; 716 717 mutex_lock(®ister_mutex); 718 if (atomic_inc_return(&chip->shutdown) == 1) { 719 struct snd_usb_stream *as; 720 struct snd_usb_endpoint *ep; 721 struct usb_mixer_interface *mixer; 722 723 /* wait until all pending tasks done; 724 * they are protected by snd_usb_lock_shutdown() 725 */ 726 wait_event(chip->shutdown_wait, 727 !atomic_read(&chip->usage_count)); 728 snd_card_disconnect(card); 729 /* release the pcm resources */ 730 list_for_each_entry(as, &chip->pcm_list, list) { 731 snd_usb_stream_disconnect(as); 732 } 733 /* release the endpoint resources */ 734 list_for_each_entry(ep, &chip->ep_list, list) { 735 snd_usb_endpoint_release(ep); 736 } 737 /* release the midi resources */ 738 list_for_each(p, &chip->midi_list) { 739 snd_usbmidi_disconnect(p); 740 } 741 /* 742 * Nice to check quirk && quirk->shares_media_device and 743 * then call the snd_media_device_delete(). Don't have 744 * access to the quirk here. snd_media_device_delete() 745 * accesses mixer_list 746 */ 747 snd_media_device_delete(chip); 748 749 /* release mixer resources */ 750 list_for_each_entry(mixer, &chip->mixer_list, list) { 751 snd_usb_mixer_disconnect(mixer); 752 } 753 } 754 755 chip->num_interfaces--; 756 if (chip->num_interfaces <= 0) { 757 usb_chip[chip->index] = NULL; 758 mutex_unlock(®ister_mutex); 759 snd_card_free_when_closed(card); 760 } else { 761 mutex_unlock(®ister_mutex); 762 } 763 } 764 765 /* lock the shutdown (disconnect) task and autoresume */ 766 int snd_usb_lock_shutdown(struct snd_usb_audio *chip) 767 { 768 int err; 769 770 atomic_inc(&chip->usage_count); 771 if (atomic_read(&chip->shutdown)) { 772 err = -EIO; 773 goto error; 774 } 775 err = snd_usb_autoresume(chip); 776 if (err < 0) 777 goto error; 778 return 0; 779 780 error: 781 if (atomic_dec_and_test(&chip->usage_count)) 782 wake_up(&chip->shutdown_wait); 783 return err; 784 } 785 786 /* autosuspend and unlock the shutdown */ 787 void snd_usb_unlock_shutdown(struct snd_usb_audio *chip) 788 { 789 snd_usb_autosuspend(chip); 790 if (atomic_dec_and_test(&chip->usage_count)) 791 wake_up(&chip->shutdown_wait); 792 } 793 794 #ifdef CONFIG_PM 795 796 int snd_usb_autoresume(struct snd_usb_audio *chip) 797 { 798 if (atomic_read(&chip->shutdown)) 799 return -EIO; 800 if (atomic_inc_return(&chip->active) == 1) 801 return usb_autopm_get_interface(chip->pm_intf); 802 return 0; 803 } 804 805 void snd_usb_autosuspend(struct snd_usb_audio *chip) 806 { 807 if (atomic_read(&chip->shutdown)) 808 return; 809 if (atomic_dec_and_test(&chip->active)) 810 usb_autopm_put_interface(chip->pm_intf); 811 } 812 813 static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message) 814 { 815 struct snd_usb_audio *chip = usb_get_intfdata(intf); 816 struct snd_usb_stream *as; 817 struct usb_mixer_interface *mixer; 818 struct list_head *p; 819 820 if (chip == (void *)-1L) 821 return 0; 822 823 chip->autosuspended = !!PMSG_IS_AUTO(message); 824 if (!chip->autosuspended) 825 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot); 826 if (!chip->num_suspended_intf++) { 827 list_for_each_entry(as, &chip->pcm_list, list) { 828 snd_usb_pcm_suspend(as); 829 as->substream[0].need_setup_ep = 830 as->substream[1].need_setup_ep = true; 831 } 832 list_for_each(p, &chip->midi_list) 833 snd_usbmidi_suspend(p); 834 list_for_each_entry(mixer, &chip->mixer_list, list) 835 snd_usb_mixer_suspend(mixer); 836 } 837 838 return 0; 839 } 840 841 static int __usb_audio_resume(struct usb_interface *intf, bool reset_resume) 842 { 843 struct snd_usb_audio *chip = usb_get_intfdata(intf); 844 struct snd_usb_stream *as; 845 struct usb_mixer_interface *mixer; 846 struct list_head *p; 847 int err = 0; 848 849 if (chip == (void *)-1L) 850 return 0; 851 if (--chip->num_suspended_intf) 852 return 0; 853 854 atomic_inc(&chip->active); /* avoid autopm */ 855 856 list_for_each_entry(as, &chip->pcm_list, list) { 857 err = snd_usb_pcm_resume(as); 858 if (err < 0) 859 goto err_out; 860 } 861 862 /* 863 * ALSA leaves material resumption to user space 864 * we just notify and restart the mixers 865 */ 866 list_for_each_entry(mixer, &chip->mixer_list, list) { 867 err = snd_usb_mixer_resume(mixer, reset_resume); 868 if (err < 0) 869 goto err_out; 870 } 871 872 list_for_each(p, &chip->midi_list) { 873 snd_usbmidi_resume(p); 874 } 875 876 if (!chip->autosuspended) 877 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0); 878 chip->autosuspended = 0; 879 880 err_out: 881 atomic_dec(&chip->active); /* allow autopm after this point */ 882 return err; 883 } 884 885 static int usb_audio_resume(struct usb_interface *intf) 886 { 887 return __usb_audio_resume(intf, false); 888 } 889 890 static int usb_audio_reset_resume(struct usb_interface *intf) 891 { 892 return __usb_audio_resume(intf, true); 893 } 894 #else 895 #define usb_audio_suspend NULL 896 #define usb_audio_resume NULL 897 #define usb_audio_reset_resume NULL 898 #endif /* CONFIG_PM */ 899 900 static const struct usb_device_id usb_audio_ids [] = { 901 #include "quirks-table.h" 902 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS), 903 .bInterfaceClass = USB_CLASS_AUDIO, 904 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL }, 905 { } /* Terminating entry */ 906 }; 907 MODULE_DEVICE_TABLE(usb, usb_audio_ids); 908 909 /* 910 * entry point for linux usb interface 911 */ 912 913 static struct usb_driver usb_audio_driver = { 914 .name = "snd-usb-audio", 915 .probe = usb_audio_probe, 916 .disconnect = usb_audio_disconnect, 917 .suspend = usb_audio_suspend, 918 .resume = usb_audio_resume, 919 .reset_resume = usb_audio_reset_resume, 920 .id_table = usb_audio_ids, 921 .supports_autosuspend = 1, 922 }; 923 924 module_usb_driver(usb_audio_driver); 925