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