card.c 24.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
D
Daniel Mack 已提交
2 3 4 5 6 7 8 9 10
/*
 *   (Tentative) USB Audio Driver for ALSA
 *
 *   Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
 *
 *   Many codes borrowed from audio.c by
 *	    Alan Cox (alan@lxorguk.ukuu.org.uk)
 *	    Thomas Sailer (sailer@ife.ee.ethz.ch)
 *
11
 *   Audio Class 3.0 support by Ruslan Bilovol <ruslan.bilovol@gmail.com>
D
Daniel Mack 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 *  NOTES:
 *
 *   - the linked URBs would be preferred but not used so far because of
 *     the instability of unlinking.
 *   - type II is not supported properly.  there is no device which supports
 *     this type *correctly*.  SB extigy looks as if it supports, but it's
 *     indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
 */


#include <linux/bitops.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/string.h>
28
#include <linux/ctype.h>
D
Daniel Mack 已提交
29 30 31 32
#include <linux/usb.h>
#include <linux/moduleparam.h>
#include <linux/mutex.h>
#include <linux/usb/audio.h>
D
Daniel Mack 已提交
33
#include <linux/usb/audio-v2.h>
34
#include <linux/usb/audio-v3.h>
35
#include <linux/module.h>
D
Daniel Mack 已提交
36

37
#include <sound/control.h>
D
Daniel Mack 已提交
38 39 40 41 42 43 44 45 46
#include <sound/core.h>
#include <sound/info.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/initval.h>

#include "usbaudio.h"
#include "card.h"
#include "midi.h"
47
#include "mixer.h"
D
Daniel Mack 已提交
48 49 50 51 52 53 54
#include "proc.h"
#include "quirks.h"
#include "endpoint.h"
#include "helper.h"
#include "debug.h"
#include "pcm.h"
#include "format.h"
55
#include "power.h"
D
Daniel Mack 已提交
56
#include "stream.h"
57
#include "media.h"
D
Daniel Mack 已提交
58 59 60 61 62 63 64 65 66

MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
MODULE_DESCRIPTION("USB Audio");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");


static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
67
static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
D
Daniel Mack 已提交
68 69 70 71
/* Vendor/product IDs for this card */
static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
72
static bool ignore_ctl_error;
73
static bool autoclock = true;
74
static char *quirk_alias[SNDRV_CARDS];
D
Daniel Mack 已提交
75

76 77
bool snd_usb_use_vmalloc = true;

D
Daniel Mack 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
module_param_array(vid, int, NULL, 0444);
MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
module_param_array(pid, int, NULL, 0444);
MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
module_param_array(device_setup, int, NULL, 0444);
MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
module_param(ignore_ctl_error, bool, 0444);
MODULE_PARM_DESC(ignore_ctl_error,
		 "Ignore errors from USB controller for mixer interfaces.");
93 94
module_param(autoclock, bool, 0444);
MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes).");
95 96
module_param_array(quirk_alias, charp, NULL, 0444);
MODULE_PARM_DESC(quirk_alias, "Quirk aliases, e.g. 0123abcd:5678beef.");
97 98
module_param_named(use_vmalloc, snd_usb_use_vmalloc, bool, 0444);
MODULE_PARM_DESC(use_vmalloc, "Use vmalloc for PCM intermediate buffers (default: yes).");
D
Daniel Mack 已提交
99 100 101 102 103 104 105 106 107 108 109 110

/*
 * we keep the snd_usb_audio_t instances by ourselves for merging
 * the all interfaces on the same card as one sound device.
 */

static DEFINE_MUTEX(register_mutex);
static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
static struct usb_driver usb_audio_driver;

/*
 * disconnect streams
111
 * called from usb_audio_disconnect()
D
Daniel Mack 已提交
112
 */
113
static void snd_usb_stream_disconnect(struct snd_usb_stream *as)
D
Daniel Mack 已提交
114 115 116 117 118 119 120
{
	int idx;
	struct snd_usb_substream *subs;

	for (idx = 0; idx < 2; idx++) {
		subs = &as->substream[idx];
		if (!subs->num_formats)
121
			continue;
D
Daniel Mack 已提交
122
		subs->interface = -1;
123 124
		subs->data_endpoint = NULL;
		subs->sync_endpoint = NULL;
D
Daniel Mack 已提交
125 126 127 128 129 130 131 132 133 134 135
	}
}

static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
{
	struct usb_device *dev = chip->dev;
	struct usb_host_interface *alts;
	struct usb_interface_descriptor *altsd;
	struct usb_interface *iface = usb_ifnum_to_if(dev, interface);

	if (!iface) {
136 137
		dev_err(&dev->dev, "%u:%d : does not exist\n",
			ctrlif, interface);
D
Daniel Mack 已提交
138 139 140
		return -EINVAL;
	}

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	alts = &iface->altsetting[0];
	altsd = get_iface_desc(alts);

	/*
	 * Android with both accessory and audio interfaces enabled gets the
	 * interface numbers wrong.
	 */
	if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) ||
	     chip->usb_id == USB_ID(0x18d1, 0x2d05)) &&
	    interface == 0 &&
	    altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
	    altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) {
		interface = 2;
		iface = usb_ifnum_to_if(dev, interface);
		if (!iface)
			return -EINVAL;
		alts = &iface->altsetting[0];
		altsd = get_iface_desc(alts);
	}

D
Daniel Mack 已提交
161
	if (usb_interface_claimed(iface)) {
162 163
		dev_dbg(&dev->dev, "%d:%d: skipping, already claimed\n",
			ctrlif, interface);
D
Daniel Mack 已提交
164 165 166 167 168 169
		return -EINVAL;
	}

	if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
	     altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
	    altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
170 171 172
		int err = __snd_usbmidi_create(chip->card, iface,
					     &chip->midi_list, NULL,
					     chip->usb_id);
D
Daniel Mack 已提交
173
		if (err < 0) {
174 175 176
			dev_err(&dev->dev,
				"%u:%d: cannot create sequencer device\n",
				ctrlif, interface);
D
Daniel Mack 已提交
177 178 179 180 181 182 183 184 185 186
			return -EINVAL;
		}
		usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);

		return 0;
	}

	if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
	     altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
	    altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
187 188 189
		dev_dbg(&dev->dev,
			"%u:%d: skipping non-supported interface %d\n",
			ctrlif, interface, altsd->bInterfaceClass);
D
Daniel Mack 已提交
190 191 192 193 194
		/* skip non-supported classes */
		return -EINVAL;
	}

	if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
195
		dev_err(&dev->dev, "low speed audio streaming not supported\n");
D
Daniel Mack 已提交
196 197 198
		return -EINVAL;
	}

D
Daniel Mack 已提交
199
	if (! snd_usb_parse_audio_interface(chip, interface)) {
D
Daniel Mack 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
		usb_set_interface(dev, interface, 0); /* reset the current interface */
		usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
	}

	return 0;
}

/*
 * parse audio control descriptor and create pcm/midi streams
 */
static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
{
	struct usb_device *dev = chip->dev;
	struct usb_host_interface *host_iface;
	struct usb_interface_descriptor *altsd;
	int i, protocol;

	/* find audiocontrol interface */
	host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
	altsd = get_iface_desc(host_iface);
	protocol = altsd->bInterfaceProtocol;

	switch (protocol) {
223
	default:
224 225 226
		dev_warn(&dev->dev,
			 "unknown interface protocol %#02x, assuming v1\n",
			 protocol);
227 228
		/* fall through */

D
Daniel Mack 已提交
229
	case UAC_VERSION_1: {
230 231 232 233 234 235
		struct uac1_ac_header_descriptor *h1;
		int rest_bytes;

		h1 = snd_usb_find_csint_desc(host_iface->extra,
							 host_iface->extralen,
							 NULL, UAC_HEADER);
236
		if (!h1 || h1->bLength < sizeof(*h1)) {
237 238 239 240 241 242 243 244 245 246 247 248
			dev_err(&dev->dev, "cannot find UAC_HEADER\n");
			return -EINVAL;
		}

		rest_bytes = (void *)(host_iface->extra +
				host_iface->extralen) - (void *)h1;

		/* just to be sure -- this shouldn't hit at all */
		if (rest_bytes <= 0) {
			dev_err(&dev->dev, "invalid control header\n");
			return -EINVAL;
		}
D
Daniel Mack 已提交
249

250 251 252 253 254
		if (rest_bytes < sizeof(*h1)) {
			dev_err(&dev->dev, "too short v1 buffer descriptor\n");
			return -EINVAL;
		}

D
Daniel Mack 已提交
255
		if (!h1->bInCollection) {
256
			dev_info(&dev->dev, "skipping empty audio interface (v1)\n");
D
Daniel Mack 已提交
257 258 259
			return -EINVAL;
		}

260 261 262 263 264
		if (rest_bytes < h1->bLength) {
			dev_err(&dev->dev, "invalid buffer length (v1)\n");
			return -EINVAL;
		}

D
Daniel Mack 已提交
265
		if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
266
			dev_err(&dev->dev, "invalid UAC_HEADER (v1)\n");
D
Daniel Mack 已提交
267 268 269 270 271 272 273 274 275
			return -EINVAL;
		}

		for (i = 0; i < h1->bInCollection; i++)
			snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);

		break;
	}

276 277
	case UAC_VERSION_2:
	case UAC_VERSION_3: {
D
Daniel Mack 已提交
278 279 280
		struct usb_interface_assoc_descriptor *assoc =
			usb_ifnum_to_if(dev, ctrlif)->intf_assoc;

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
		if (!assoc) {
			/*
			 * Firmware writers cannot count to three.  So to find
			 * the IAD on the NuForce UDH-100, also check the next
			 * interface.
			 */
			struct usb_interface *iface =
				usb_ifnum_to_if(dev, ctrlif + 1);
			if (iface &&
			    iface->intf_assoc &&
			    iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO &&
			    iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2)
				assoc = iface->intf_assoc;
		}

D
Daniel Mack 已提交
296
		if (!assoc) {
297
			dev_err(&dev->dev, "Audio class v2/v3 interfaces need an interface association\n");
D
Daniel Mack 已提交
298 299 300
			return -EINVAL;
		}

301 302 303 304 305 306 307 308 309 310 311 312 313 314
		if (protocol == UAC_VERSION_3) {
			int badd = assoc->bFunctionSubClass;

			if (badd != UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0 &&
			    (badd < UAC3_FUNCTION_SUBCLASS_GENERIC_IO ||
			     badd > UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE)) {
				dev_err(&dev->dev,
					"Unsupported UAC3 BADD profile\n");
				return -EINVAL;
			}

			chip->badd_profile = badd;
		}

D
Daniel Mack 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
		for (i = 0; i < assoc->bInterfaceCount; i++) {
			int intf = assoc->bFirstInterface + i;

			if (intf != ctrlif)
				snd_usb_create_stream(chip, ctrlif, intf);
		}

		break;
	}
	}

	return 0;
}

/*
 * free the chip instance
 *
 * here we have to do not much, since pcm and controls are already freed
 *
 */

336
static void snd_usb_audio_free(struct snd_card *card)
D
Daniel Mack 已提交
337
{
338
	struct snd_usb_audio *chip = card->private_data;
339
	struct snd_usb_endpoint *ep, *n;
340

341 342
	list_for_each_entry_safe(ep, n, &chip->ep_list, list)
		snd_usb_endpoint_free(ep);
343

344
	mutex_destroy(&chip->mutex);
345 346
	if (!atomic_read(&chip->shutdown))
		dev_set_drvdata(&chip->dev->dev, NULL);
D
Daniel Mack 已提交
347 348
}

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
static void usb_audio_make_shortname(struct usb_device *dev,
				     struct snd_usb_audio *chip,
				     const struct snd_usb_audio_quirk *quirk)
{
	struct snd_card *card = chip->card;

	if (quirk && quirk->product_name && *quirk->product_name) {
		strlcpy(card->shortname, quirk->product_name,
			sizeof(card->shortname));
		return;
	}

	/* retrieve the device string as shortname */
	if (!dev->descriptor.iProduct ||
	    usb_string(dev, dev->descriptor.iProduct,
		       card->shortname, sizeof(card->shortname)) <= 0) {
		/* no name available from anywhere, so use ID */
		sprintf(card->shortname, "USB Device %#04x:%#04x",
			USB_ID_VENDOR(chip->usb_id),
			USB_ID_PRODUCT(chip->usb_id));
	}

	strim(card->shortname);
}

static void usb_audio_make_longname(struct usb_device *dev,
				    struct snd_usb_audio *chip,
				    const struct snd_usb_audio_quirk *quirk)
{
	struct snd_card *card = chip->card;
	int len;

	/* shortcut - if any pre-defined string is given, use it */
	if (quirk && quirk->profile_name && *quirk->profile_name) {
		strlcpy(card->longname, quirk->profile_name,
			sizeof(card->longname));
		return;
	}

	if (quirk && quirk->vendor_name && *quirk->vendor_name) {
		len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
	} else {
		/* retrieve the vendor and device strings as longname */
		if (dev->descriptor.iManufacturer)
			len = usb_string(dev, dev->descriptor.iManufacturer,
					 card->longname, sizeof(card->longname));
		else
			len = 0;
		/* we don't really care if there isn't any vendor string */
	}
	if (len > 0) {
		strim(card->longname);
		if (*card->longname)
			strlcat(card->longname, " ", sizeof(card->longname));
	}

	strlcat(card->longname, card->shortname, sizeof(card->longname));

	len = strlcat(card->longname, " at ", sizeof(card->longname));

	if (len < sizeof(card->longname))
		usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);

	switch (snd_usb_get_speed(dev)) {
	case USB_SPEED_LOW:
		strlcat(card->longname, ", low speed", sizeof(card->longname));
		break;
	case USB_SPEED_FULL:
		strlcat(card->longname, ", full speed", sizeof(card->longname));
		break;
	case USB_SPEED_HIGH:
		strlcat(card->longname, ", high speed", sizeof(card->longname));
		break;
	case USB_SPEED_SUPER:
		strlcat(card->longname, ", super speed", sizeof(card->longname));
		break;
	case USB_SPEED_SUPER_PLUS:
		strlcat(card->longname, ", super speed plus", sizeof(card->longname));
		break;
	default:
		break;
	}
}

D
Daniel Mack 已提交
433 434 435
/*
 * create a chip instance and set its names.
 */
436 437
static int snd_usb_audio_create(struct usb_interface *intf,
				struct usb_device *dev, int idx,
D
Daniel Mack 已提交
438
				const struct snd_usb_audio_quirk *quirk,
439
				unsigned int usb_id,
D
Daniel Mack 已提交
440 441 442 443
				struct snd_usb_audio **rchip)
{
	struct snd_card *card;
	struct snd_usb_audio *chip;
444
	int err;
D
Daniel Mack 已提交
445 446 447 448
	char component[14];

	*rchip = NULL;

449 450 451 452
	switch (snd_usb_get_speed(dev)) {
	case USB_SPEED_LOW:
	case USB_SPEED_FULL:
	case USB_SPEED_HIGH:
453
	case USB_SPEED_WIRELESS:
454
	case USB_SPEED_SUPER:
455
	case USB_SPEED_SUPER_PLUS:
456 457
		break;
	default:
458
		dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev));
D
Daniel Mack 已提交
459 460 461
		return -ENXIO;
	}

462
	err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE,
463
			   sizeof(*chip), &card);
D
Daniel Mack 已提交
464
	if (err < 0) {
465
		dev_err(&dev->dev, "cannot create card instance %d\n", idx);
D
Daniel Mack 已提交
466 467 468
		return err;
	}

469
	chip = card->private_data;
470
	mutex_init(&chip->mutex);
471
	init_waitqueue_head(&chip->shutdown_wait);
D
Daniel Mack 已提交
472 473 474 475
	chip->index = idx;
	chip->dev = dev;
	chip->card = card;
	chip->setup = device_setup[idx];
476
	chip->autoclock = autoclock;
477
	atomic_set(&chip->active, 1); /* avoid autopm during probing */
478 479
	atomic_set(&chip->usage_count, 0);
	atomic_set(&chip->shutdown, 0);
D
Daniel Mack 已提交
480

481
	chip->usb_id = usb_id;
D
Daniel Mack 已提交
482
	INIT_LIST_HEAD(&chip->pcm_list);
483
	INIT_LIST_HEAD(&chip->ep_list);
D
Daniel Mack 已提交
484 485 486
	INIT_LIST_HEAD(&chip->midi_list);
	INIT_LIST_HEAD(&chip->mixer_list);

487
	card->private_free = snd_usb_audio_free;
D
Daniel Mack 已提交
488 489 490 491 492 493

	strcpy(card->driver, "USB-Audio");
	sprintf(component, "USB%04x:%04x",
		USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
	snd_component_add(card, component);

494 495
	usb_audio_make_shortname(dev, chip, quirk);
	usb_audio_make_longname(dev, chip, quirk);
D
Daniel Mack 已提交
496 497 498 499 500 501 502

	snd_usb_audio_create_proc(chip);

	*rchip = chip;
	return 0;
}

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
/* look for a matching quirk alias id */
static bool get_alias_id(struct usb_device *dev, unsigned int *id)
{
	int i;
	unsigned int src, dst;

	for (i = 0; i < ARRAY_SIZE(quirk_alias); i++) {
		if (!quirk_alias[i] ||
		    sscanf(quirk_alias[i], "%x:%x", &src, &dst) != 2 ||
		    src != *id)
			continue;
		dev_info(&dev->dev,
			 "device (%04x:%04x): applying quirk alias %04x:%04x\n",
			 USB_ID_VENDOR(*id), USB_ID_PRODUCT(*id),
			 USB_ID_VENDOR(dst), USB_ID_PRODUCT(dst));
		*id = dst;
		return true;
	}

	return false;
}

525
static const struct usb_device_id usb_audio_ids[]; /* defined below */
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

/* look for the corresponding quirk */
static const struct snd_usb_audio_quirk *
get_alias_quirk(struct usb_device *dev, unsigned int id)
{
	const struct usb_device_id *p;

	for (p = usb_audio_ids; p->match_flags; p++) {
		/* FIXME: this checks only vendor:product pair in the list */
		if ((p->match_flags & USB_DEVICE_ID_MATCH_DEVICE) ==
		    USB_DEVICE_ID_MATCH_DEVICE &&
		    p->idVendor == USB_ID_VENDOR(id) &&
		    p->idProduct == USB_ID_PRODUCT(id))
			return (const struct snd_usb_audio_quirk *)p->driver_info;
	}

	return NULL;
}

D
Daniel Mack 已提交
545 546 547 548 549 550 551 552 553 554
/*
 * probe the active usb device
 *
 * note that this can be called multiple times per a device, when it
 * includes multiple audio control interfaces.
 *
 * thus we check the usb device pointer and creates the card instance
 * only at the first time.  the successive calls of this function will
 * append the pcm interface to the corresponding card.
 */
555 556
static int usb_audio_probe(struct usb_interface *intf,
			   const struct usb_device_id *usb_id)
D
Daniel Mack 已提交
557
{
558 559 560
	struct usb_device *dev = interface_to_usbdev(intf);
	const struct snd_usb_audio_quirk *quirk =
		(const struct snd_usb_audio_quirk *)usb_id->driver_info;
D
Daniel Mack 已提交
561
	struct snd_usb_audio *chip;
562
	int i, err;
D
Daniel Mack 已提交
563 564 565 566 567 568 569 570
	struct usb_host_interface *alts;
	int ifnum;
	u32 id;

	alts = &intf->altsetting[0];
	ifnum = get_iface_desc(alts)->bInterfaceNumber;
	id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
		    le16_to_cpu(dev->descriptor.idProduct));
571 572
	if (get_alias_id(dev, &id))
		quirk = get_alias_quirk(dev, id);
D
Daniel Mack 已提交
573
	if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
574
		return -ENXIO;
D
Daniel Mack 已提交
575

576
	err = snd_usb_apply_boot_quirk(dev, intf, quirk, id);
577 578
	if (err < 0)
		return err;
D
Daniel Mack 已提交
579 580 581 582 583 584 585 586 587 588

	/*
	 * found a config.  now register to ALSA
	 */

	/* check whether it's already registered */
	chip = NULL;
	mutex_lock(&register_mutex);
	for (i = 0; i < SNDRV_CARDS; i++) {
		if (usb_chip[i] && usb_chip[i]->dev == dev) {
589
			if (atomic_read(&usb_chip[i]->shutdown)) {
590
				dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n");
591
				err = -EIO;
D
Daniel Mack 已提交
592 593 594
				goto __error;
			}
			chip = usb_chip[i];
595
			atomic_inc(&chip->active); /* avoid autopm */
D
Daniel Mack 已提交
596 597 598 599 600 601 602 603
			break;
		}
	}
	if (! chip) {
		/* it's a fresh one.
		 * now look for an empty slot and create a new card instance
		 */
		for (i = 0; i < SNDRV_CARDS; i++)
604
			if (!usb_chip[i] &&
D
Daniel Mack 已提交
605 606
			    (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
			    (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
607 608 609 610 611 612 613 614 615 616 617 618 619
				if (enable[i]) {
					err = snd_usb_audio_create(intf, dev, i, quirk,
								   id, &chip);
					if (err < 0)
						goto __error;
					chip->pm_intf = intf;
					break;
				} else if (vid[i] != -1 || pid[i] != -1) {
					dev_info(&dev->dev,
						 "device (%04x:%04x) is disabled\n",
						 USB_ID_VENDOR(id),
						 USB_ID_PRODUCT(id));
					err = -ENOENT;
D
Daniel Mack 已提交
620
					goto __error;
621
				}
D
Daniel Mack 已提交
622 623
			}
		if (!chip) {
624
			dev_err(&dev->dev, "no available usb audio device\n");
625
			err = -ENODEV;
D
Daniel Mack 已提交
626 627 628
			goto __error;
		}
	}
629
	dev_set_drvdata(&dev->dev, chip);
D
Daniel Mack 已提交
630

631 632 633 634 635 636 637
	/*
	 * For devices with more than one control interface, we assume the
	 * first contains the audio controls. We might need a more specific
	 * check here in the future.
	 */
	if (!chip->ctrl_intf)
		chip->ctrl_intf = alts;
638

639 640 641 642
	chip->txfr_quirk = 0;
	err = 1; /* continue */
	if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
		/* need some special handlings */
643 644
		err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk);
		if (err < 0)
645 646 647
			goto __error;
	}

D
Daniel Mack 已提交
648 649
	if (err > 0) {
		/* create normal USB audio interfaces */
650 651 652 653 654
		err = snd_usb_create_streams(chip, ifnum);
		if (err < 0)
			goto __error;
		err = snd_usb_create_mixer(chip, ifnum, ignore_ctl_error);
		if (err < 0)
D
Daniel Mack 已提交
655 656 657 658
			goto __error;
	}

	/* we are allowed to call snd_card_register() many times */
659 660
	err = snd_card_register(chip->card);
	if (err < 0)
D
Daniel Mack 已提交
661 662
		goto __error;

663 664 665 666 667
	if (quirk && quirk->shares_media_device) {
		/* don't want to fail when snd_media_device_create() fails */
		snd_media_device_create(chip, intf);
	}

D
Daniel Mack 已提交
668 669
	usb_chip[chip->index] = chip;
	chip->num_interfaces++;
670
	usb_set_intfdata(intf, chip);
671
	atomic_dec(&chip->active);
D
Daniel Mack 已提交
672
	mutex_unlock(&register_mutex);
673
	return 0;
D
Daniel Mack 已提交
674 675

 __error:
676
	if (chip) {
677 678 679 680
		/* chip->active is inside the chip->card object,
		 * decrement before memory is possibly returned.
		 */
		atomic_dec(&chip->active);
681 682 683
		if (!chip->num_interfaces)
			snd_card_free(chip->card);
	}
D
Daniel Mack 已提交
684
	mutex_unlock(&register_mutex);
685
	return err;
D
Daniel Mack 已提交
686 687 688 689 690 691
}

/*
 * we need to take care of counter, since disconnection can be called also
 * many times as well as usb_audio_probe().
 */
692
static void usb_audio_disconnect(struct usb_interface *intf)
D
Daniel Mack 已提交
693
{
694
	struct snd_usb_audio *chip = usb_get_intfdata(intf);
D
Daniel Mack 已提交
695
	struct snd_card *card;
696
	struct list_head *p;
D
Daniel Mack 已提交
697

698
	if (chip == (void *)-1L)
D
Daniel Mack 已提交
699 700 701
		return;

	card = chip->card;
702 703

	mutex_lock(&register_mutex);
704
	if (atomic_inc_return(&chip->shutdown) == 1) {
705
		struct snd_usb_stream *as;
706
		struct snd_usb_endpoint *ep;
707
		struct usb_mixer_interface *mixer;
708

709 710 711 712 713
		/* wait until all pending tasks done;
		 * they are protected by snd_usb_lock_shutdown()
		 */
		wait_event(chip->shutdown_wait,
			   !atomic_read(&chip->usage_count));
D
Daniel Mack 已提交
714 715
		snd_card_disconnect(card);
		/* release the pcm resources */
716 717
		list_for_each_entry(as, &chip->pcm_list, list) {
			snd_usb_stream_disconnect(as);
D
Daniel Mack 已提交
718
		}
719
		/* release the endpoint resources */
720 721
		list_for_each_entry(ep, &chip->ep_list, list) {
			snd_usb_endpoint_release(ep);
722
		}
D
Daniel Mack 已提交
723 724 725 726
		/* release the midi resources */
		list_for_each(p, &chip->midi_list) {
			snd_usbmidi_disconnect(p);
		}
727 728 729 730 731 732 733 734
		/*
		 * Nice to check quirk && quirk->shares_media_device and
		 * then call the snd_media_device_delete(). Don't have
		 * access to the quirk here. snd_media_device_delete()
		 * accesses mixer_list
		 */
		snd_media_device_delete(chip);

D
Daniel Mack 已提交
735
		/* release mixer resources */
736 737
		list_for_each_entry(mixer, &chip->mixer_list, list) {
			snd_usb_mixer_disconnect(mixer);
D
Daniel Mack 已提交
738
		}
739 740 741 742
	}

	chip->num_interfaces--;
	if (chip->num_interfaces <= 0) {
D
Daniel Mack 已提交
743 744 745 746 747 748 749 750
		usb_chip[chip->index] = NULL;
		mutex_unlock(&register_mutex);
		snd_card_free_when_closed(card);
	} else {
		mutex_unlock(&register_mutex);
	}
}

751 752
/* lock the shutdown (disconnect) task and autoresume */
int snd_usb_lock_shutdown(struct snd_usb_audio *chip)
753
{
754
	int err;
755

756 757 758 759 760 761 762 763 764
	atomic_inc(&chip->usage_count);
	if (atomic_read(&chip->shutdown)) {
		err = -EIO;
		goto error;
	}
	err = snd_usb_autoresume(chip);
	if (err < 0)
		goto error;
	return 0;
765

766 767 768
 error:
	if (atomic_dec_and_test(&chip->usage_count))
		wake_up(&chip->shutdown_wait);
769 770 771
	return err;
}

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
/* autosuspend and unlock the shutdown */
void snd_usb_unlock_shutdown(struct snd_usb_audio *chip)
{
	snd_usb_autosuspend(chip);
	if (atomic_dec_and_test(&chip->usage_count))
		wake_up(&chip->shutdown_wait);
}

#ifdef CONFIG_PM

int snd_usb_autoresume(struct snd_usb_audio *chip)
{
	if (atomic_read(&chip->shutdown))
		return -EIO;
	if (atomic_inc_return(&chip->active) == 1)
		return usb_autopm_get_interface(chip->pm_intf);
	return 0;
}

791 792
void snd_usb_autosuspend(struct snd_usb_audio *chip)
{
793 794
	if (atomic_read(&chip->shutdown))
		return;
795
	if (atomic_dec_and_test(&chip->active))
796 797 798
		usb_autopm_put_interface(chip->pm_intf);
}

D
Daniel Mack 已提交
799 800 801 802
static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
{
	struct snd_usb_audio *chip = usb_get_intfdata(intf);
	struct snd_usb_stream *as;
803
	struct usb_mixer_interface *mixer;
804
	struct list_head *p;
D
Daniel Mack 已提交
805 806 807 808

	if (chip == (void *)-1L)
		return 0;

809 810
	chip->autosuspended = !!PMSG_IS_AUTO(message);
	if (!chip->autosuspended)
811
		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
812 813
	if (!chip->num_suspended_intf++) {
		list_for_each_entry(as, &chip->pcm_list, list) {
814
			snd_usb_pcm_suspend(as);
815 816
			as->substream[0].need_setup_ep =
				as->substream[1].need_setup_ep = true;
817
		}
818 819
		list_for_each(p, &chip->midi_list)
			snd_usbmidi_suspend(p);
820 821
		list_for_each_entry(mixer, &chip->mixer_list, list)
			snd_usb_mixer_suspend(mixer);
822
	}
823

D
Daniel Mack 已提交
824 825 826
	return 0;
}

827
static int __usb_audio_resume(struct usb_interface *intf, bool reset_resume)
D
Daniel Mack 已提交
828 829
{
	struct snd_usb_audio *chip = usb_get_intfdata(intf);
830
	struct snd_usb_stream *as;
831
	struct usb_mixer_interface *mixer;
832
	struct list_head *p;
833
	int err = 0;
D
Daniel Mack 已提交
834 835 836 837 838

	if (chip == (void *)-1L)
		return 0;
	if (--chip->num_suspended_intf)
		return 0;
839

840
	atomic_inc(&chip->active); /* avoid autopm */
841 842 843 844 845 846 847

	list_for_each_entry(as, &chip->pcm_list, list) {
		err = snd_usb_pcm_resume(as);
		if (err < 0)
			goto err_out;
	}

D
Daniel Mack 已提交
848 849
	/*
	 * ALSA leaves material resumption to user space
850
	 * we just notify and restart the mixers
D
Daniel Mack 已提交
851
	 */
852
	list_for_each_entry(mixer, &chip->mixer_list, list) {
853
		err = snd_usb_mixer_resume(mixer, reset_resume);
854 855 856
		if (err < 0)
			goto err_out;
	}
D
Daniel Mack 已提交
857

858 859 860 861
	list_for_each(p, &chip->midi_list) {
		snd_usbmidi_resume(p);
	}

862 863 864
	if (!chip->autosuspended)
		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
	chip->autosuspended = 0;
D
Daniel Mack 已提交
865

866
err_out:
867
	atomic_dec(&chip->active); /* allow autopm after this point */
868
	return err;
D
Daniel Mack 已提交
869
}
870 871 872 873 874 875 876 877 878 879

static int usb_audio_resume(struct usb_interface *intf)
{
	return __usb_audio_resume(intf, false);
}

static int usb_audio_reset_resume(struct usb_interface *intf)
{
	return __usb_audio_resume(intf, true);
}
880 881 882
#else
#define usb_audio_suspend	NULL
#define usb_audio_resume	NULL
883
#define usb_audio_reset_resume	NULL
D
Daniel Mack 已提交
884 885
#endif		/* CONFIG_PM */

886
static const struct usb_device_id usb_audio_ids [] = {
D
Daniel Mack 已提交
887 888 889 890 891 892
#include "quirks-table.h"
    { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
      .bInterfaceClass = USB_CLASS_AUDIO,
      .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
    { }						/* Terminating entry */
};
893
MODULE_DEVICE_TABLE(usb, usb_audio_ids);
D
Daniel Mack 已提交
894 895 896 897 898 899 900 901 902 903 904

/*
 * entry point for linux usb interface
 */

static struct usb_driver usb_audio_driver = {
	.name =		"snd-usb-audio",
	.probe =	usb_audio_probe,
	.disconnect =	usb_audio_disconnect,
	.suspend =	usb_audio_suspend,
	.resume =	usb_audio_resume,
905
	.reset_resume =	usb_audio_reset_resume,
D
Daniel Mack 已提交
906
	.id_table =	usb_audio_ids,
907
	.supports_autosuspend = 1,
D
Daniel Mack 已提交
908 909
};

910
module_usb_driver(usb_audio_driver);