File Index
Files and directories
Sign in
FILE

card.c

File preview with syntax highlighting.

linux-6.19.14/sound/usb/card.c C
   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 "midi2.h"
  48#include "mixer.h"
  49#include "proc.h"
  50#include "quirks.h"
  51#include "endpoint.h"
  52#include "helper.h"
  53#include "pcm.h"
  54#include "format.h"
  55#include "power.h"
  56#include "stream.h"
  57#include "media.h"
  58
  59MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  60MODULE_DESCRIPTION("USB Audio");
  61MODULE_LICENSE("GPL");
  62
  63static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
  64static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
  65static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
  66/* Vendor/product IDs for this card */
  67static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
  68static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
  69static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
  70static bool ignore_ctl_error;
  71static bool autoclock = true;
  72static bool lowlatency = true;
  73static char *quirk_alias[SNDRV_CARDS];
  74static char *delayed_register[SNDRV_CARDS];
  75static bool implicit_fb[SNDRV_CARDS];
  76static char *quirk_flags[SNDRV_CARDS];
  77
  78bool snd_usb_use_vmalloc = true;
  79bool snd_usb_skip_validation;
  80
  81module_param_array(index, int, NULL, 0444);
  82MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
  83module_param_array(id, charp, NULL, 0444);
  84MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
  85module_param_array(enable, bool, NULL, 0444);
  86MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
  87module_param_array(vid, int, NULL, 0444);
  88MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
  89module_param_array(pid, int, NULL, 0444);
  90MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
  91module_param_array(device_setup, int, NULL, 0444);
  92MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
  93module_param(ignore_ctl_error, bool, 0444);
  94MODULE_PARM_DESC(ignore_ctl_error,
  95		 "Ignore errors from USB controller for mixer interfaces.");
  96module_param(autoclock, bool, 0444);
  97MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes).");
  98module_param(lowlatency, bool, 0444);
  99MODULE_PARM_DESC(lowlatency, "Enable low latency playback (default: yes).");
 100module_param_array(quirk_alias, charp, NULL, 0444);
 101MODULE_PARM_DESC(quirk_alias, "Quirk aliases, e.g. 0123abcd:5678beef.");
 102module_param_array(delayed_register, charp, NULL, 0444);
 103MODULE_PARM_DESC(delayed_register, "Quirk for delayed registration, given by id:iface, e.g. 0123abcd:4.");
 104module_param_array(implicit_fb, bool, NULL, 0444);
 105MODULE_PARM_DESC(implicit_fb, "Apply generic implicit feedback sync mode.");
 106module_param_named(use_vmalloc, snd_usb_use_vmalloc, bool, 0444);
 107MODULE_PARM_DESC(use_vmalloc, "Use vmalloc for PCM intermediate buffers (default: yes).");
 108module_param_named(skip_validation, snd_usb_skip_validation, bool, 0444);
 109MODULE_PARM_DESC(skip_validation, "Skip unit descriptor validation (default: no).");
 110
 111/* protects quirk_flags */
 112static DEFINE_MUTEX(quirk_flags_mutex);
 113
 114static int param_set_quirkp(const char *val,
 115			    const struct kernel_param *kp)
 116{
 117	guard(mutex)(&quirk_flags_mutex);
 118	return param_set_charp(val, kp);
 119}
 120
 121static const struct kernel_param_ops param_ops_quirkp = {
 122	.set = param_set_quirkp,
 123	.get = param_get_charp,
 124	.free = param_free_charp,
 125};
 126
 127#define param_check_quirkp param_check_charp
 128
 129module_param_array(quirk_flags, quirkp, NULL, 0644);
 130MODULE_PARM_DESC(quirk_flags, "Add/modify USB audio quirks");
 131
 132/*
 133 * we keep the snd_usb_audio_t instances by ourselves for merging
 134 * the all interfaces on the same card as one sound device.
 135 */
 136
 137static DEFINE_MUTEX(register_mutex);
 138static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
 139static struct usb_driver usb_audio_driver;
 140static struct snd_usb_platform_ops *platform_ops;
 141
 142/*
 143 * Register platform specific operations that will be notified on events
 144 * which occur in USB SND.  The platform driver can utilize this path to
 145 * enable features, such as USB audio offloading, which allows for audio data
 146 * to be queued by an audio DSP.
 147 *
 148 * Only one set of platform operations can be registered to USB SND.  The
 149 * platform register operation is protected by the register_mutex.
 150 */
 151int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops)
 152{
 153	guard(mutex)(&register_mutex);
 154	if (platform_ops)
 155		return -EEXIST;
 156
 157	platform_ops = ops;
 158	return 0;
 159}
 160EXPORT_SYMBOL_GPL(snd_usb_register_platform_ops);
 161
 162/*
 163 * Unregisters the current set of platform operations.  This allows for
 164 * a new set to be registered if required.
 165 *
 166 * The platform unregister operation is protected by the register_mutex.
 167 */
 168int snd_usb_unregister_platform_ops(void)
 169{
 170	guard(mutex)(&register_mutex);
 171	platform_ops = NULL;
 172
 173	return 0;
 174}
 175EXPORT_SYMBOL_GPL(snd_usb_unregister_platform_ops);
 176
 177/*
 178 * in case the platform driver was not ready at the time of USB SND
 179 * device connect, expose an API to discover all connected USB devices
 180 * so it can populate any dependent resources/structures.
 181 */
 182void snd_usb_rediscover_devices(void)
 183{
 184	int i;
 185
 186	guard(mutex)(&register_mutex);
 187
 188	if (!platform_ops || !platform_ops->connect_cb)
 189		return;
 190
 191	for (i = 0; i < SNDRV_CARDS; i++) {
 192		if (usb_chip[i])
 193			platform_ops->connect_cb(usb_chip[i]);
 194	}
 195}
 196EXPORT_SYMBOL_GPL(snd_usb_rediscover_devices);
 197
 198/*
 199 * Checks to see if requested audio profile, i.e sample rate, # of
 200 * channels, etc... is supported by the substream associated to the
 201 * USB audio device.
 202 */
 203struct snd_usb_stream *
 204snd_usb_find_suppported_substream(int card_idx, struct snd_pcm_hw_params *params,
 205				  int direction)
 206{
 207	struct snd_usb_audio *chip;
 208	struct snd_usb_substream *subs;
 209	struct snd_usb_stream *as;
 210
 211	/*
 212	 * Register mutex is held when populating and clearing usb_chip
 213	 * array.
 214	 */
 215	guard(mutex)(&register_mutex);
 216	chip = usb_chip[card_idx];
 217
 218	if (chip && enable[card_idx]) {
 219		list_for_each_entry(as, &chip->pcm_list, list) {
 220			subs = &as->substream[direction];
 221			if (snd_usb_find_substream_format(subs, params))
 222				return as;
 223		}
 224	}
 225
 226	return NULL;
 227}
 228EXPORT_SYMBOL_GPL(snd_usb_find_suppported_substream);
 229
 230/*
 231 * disconnect streams
 232 * called from usb_audio_disconnect()
 233 */
 234static void snd_usb_stream_disconnect(struct snd_usb_stream *as)
 235{
 236	int idx;
 237	struct snd_usb_substream *subs;
 238
 239	for (idx = 0; idx < 2; idx++) {
 240		subs = &as->substream[idx];
 241		if (!subs->num_formats)
 242			continue;
 243		subs->data_endpoint = NULL;
 244		subs->sync_endpoint = NULL;
 245	}
 246}
 247
 248static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
 249{
 250	struct usb_device *dev = chip->dev;
 251	struct usb_host_interface *alts;
 252	struct usb_interface_descriptor *altsd;
 253	struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
 254
 255	if (!iface) {
 256		dev_err(&dev->dev, "%u:%d : does not exist\n",
 257			ctrlif, interface);
 258		return -EINVAL;
 259	}
 260
 261	alts = &iface->altsetting[0];
 262	altsd = get_iface_desc(alts);
 263
 264	/*
 265	 * Android with both accessory and audio interfaces enabled gets the
 266	 * interface numbers wrong.
 267	 */
 268	if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) ||
 269	     chip->usb_id == USB_ID(0x18d1, 0x2d05)) &&
 270	    interface == 0 &&
 271	    altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
 272	    altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) {
 273		interface = 2;
 274		iface = usb_ifnum_to_if(dev, interface);
 275		if (!iface)
 276			return -EINVAL;
 277		alts = &iface->altsetting[0];
 278		altsd = get_iface_desc(alts);
 279	}
 280
 281	if (usb_interface_claimed(iface)) {
 282		dev_dbg(&dev->dev, "%d:%d: skipping, already claimed\n",
 283			ctrlif, interface);
 284		return -EINVAL;
 285	}
 286
 287	if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
 288	     altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
 289	    altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
 290		int err = snd_usb_midi_v2_create(chip, iface, NULL,
 291						 chip->usb_id);
 292		if (err < 0) {
 293			dev_err(&dev->dev,
 294				"%u:%d: cannot create sequencer device\n",
 295				ctrlif, interface);
 296			return -EINVAL;
 297		}
 298		return usb_driver_claim_interface(&usb_audio_driver, iface,
 299						  USB_AUDIO_IFACE_UNUSED);
 300	}
 301
 302	if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
 303	     altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
 304	    altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
 305		dev_dbg(&dev->dev,
 306			"%u:%d: skipping non-supported interface %d\n",
 307			ctrlif, interface, altsd->bInterfaceClass);
 308		/* skip non-supported classes */
 309		return -EINVAL;
 310	}
 311
 312	if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
 313		dev_err(&dev->dev, "low speed audio streaming not supported\n");
 314		return -EINVAL;
 315	}
 316
 317	snd_usb_add_ctrl_interface_link(chip, interface, ctrlif);
 318
 319	if (! snd_usb_parse_audio_interface(chip, interface)) {
 320		usb_set_interface(dev, interface, 0); /* reset the current interface */
 321		return usb_driver_claim_interface(&usb_audio_driver, iface,
 322						  USB_AUDIO_IFACE_UNUSED);
 323	}
 324
 325	return 0;
 326}
 327
 328/*
 329 * parse audio control descriptor and create pcm/midi streams
 330 */
 331static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
 332{
 333	struct usb_device *dev = chip->dev;
 334	struct usb_host_interface *host_iface;
 335	struct usb_interface_descriptor *altsd;
 336	int i, protocol;
 337
 338	/* find audiocontrol interface */
 339	host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
 340	altsd = get_iface_desc(host_iface);
 341	protocol = altsd->bInterfaceProtocol;
 342
 343	switch (protocol) {
 344	default:
 345		dev_warn(&dev->dev,
 346			 "unknown interface protocol %#02x, assuming v1\n",
 347			 protocol);
 348		fallthrough;
 349
 350	case UAC_VERSION_1: {
 351		struct uac1_ac_header_descriptor *h1;
 352		int rest_bytes;
 353
 354		h1 = snd_usb_find_csint_desc(host_iface->extra,
 355							 host_iface->extralen,
 356							 NULL, UAC_HEADER);
 357		if (!h1 || h1->bLength < sizeof(*h1)) {
 358			dev_err(&dev->dev, "cannot find UAC_HEADER\n");
 359			return -EINVAL;
 360		}
 361
 362		rest_bytes = (void *)(host_iface->extra +
 363				host_iface->extralen) - (void *)h1;
 364
 365		/* just to be sure -- this shouldn't hit at all */
 366		if (rest_bytes <= 0) {
 367			dev_err(&dev->dev, "invalid control header\n");
 368			return -EINVAL;
 369		}
 370
 371		if (rest_bytes < sizeof(*h1)) {
 372			dev_err(&dev->dev, "too short v1 buffer descriptor\n");
 373			return -EINVAL;
 374		}
 375
 376		if (!h1->bInCollection) {
 377			dev_info(&dev->dev, "skipping empty audio interface (v1)\n");
 378			return -EINVAL;
 379		}
 380
 381		if (rest_bytes < h1->bLength) {
 382			dev_err(&dev->dev, "invalid buffer length (v1)\n");
 383			return -EINVAL;
 384		}
 385
 386		if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
 387			dev_err(&dev->dev, "invalid UAC_HEADER (v1)\n");
 388			return -EINVAL;
 389		}
 390
 391		for (i = 0; i < h1->bInCollection; i++)
 392			snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
 393
 394		break;
 395	}
 396
 397	case UAC_VERSION_2:
 398	case UAC_VERSION_3: {
 399		struct usb_interface_assoc_descriptor *assoc =
 400			usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
 401
 402		if (!assoc) {
 403			/*
 404			 * Firmware writers cannot count to three.  So to find
 405			 * the IAD on the NuForce UDH-100, also check the next
 406			 * interface.
 407			 */
 408			struct usb_interface *iface =
 409				usb_ifnum_to_if(dev, ctrlif + 1);
 410			if (iface &&
 411			    iface->intf_assoc &&
 412			    iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO &&
 413			    iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2)
 414				assoc = iface->intf_assoc;
 415		}
 416
 417		if (!assoc) {
 418			dev_err(&dev->dev, "Audio class v2/v3 interfaces need an interface association\n");
 419			return -EINVAL;
 420		}
 421
 422		if (protocol == UAC_VERSION_3) {
 423			int badd = assoc->bFunctionSubClass;
 424
 425			if (badd != UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0 &&
 426			    (badd < UAC3_FUNCTION_SUBCLASS_GENERIC_IO ||
 427			     badd > UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE)) {
 428				dev_err(&dev->dev,
 429					"Unsupported UAC3 BADD profile\n");
 430				return -EINVAL;
 431			}
 432
 433			chip->badd_profile = badd;
 434		}
 435
 436		for (i = 0; i < assoc->bInterfaceCount; i++) {
 437			int intf = assoc->bFirstInterface + i;
 438
 439			if (intf != ctrlif)
 440				snd_usb_create_stream(chip, ctrlif, intf);
 441		}
 442
 443		break;
 444	}
 445	}
 446
 447	return 0;
 448}
 449
 450/*
 451 * Profile name preset table
 452 */
 453struct usb_audio_device_name {
 454	u32 id;
 455	const char *vendor_name;
 456	const char *product_name;
 457	const char *profile_name;	/* override card->longname */
 458};
 459
 460#define PROFILE_NAME(vid, pid, vendor, product, profile)	 \
 461	{ .id = USB_ID(vid, pid), .vendor_name = (vendor),	 \
 462	  .product_name = (product), .profile_name = (profile) }
 463#define DEVICE_NAME(vid, pid, vendor, product) \
 464	PROFILE_NAME(vid, pid, vendor, product, NULL)
 465
 466/* vendor/product and profile name presets, sorted in device id order */
 467static const struct usb_audio_device_name usb_audio_names[] = {
 468	/* HP Thunderbolt Dock Audio Headset */
 469	PROFILE_NAME(0x03f0, 0x0269, "HP", "Thunderbolt Dock Audio Headset",
 470		     "HP-Thunderbolt-Dock-Audio-Headset"),
 471	/* HP Thunderbolt Dock Audio Module */
 472	PROFILE_NAME(0x03f0, 0x0567, "HP", "Thunderbolt Dock Audio Module",
 473		     "HP-Thunderbolt-Dock-Audio-Module"),
 474
 475	/* Two entries for Gigabyte TRX40 Aorus Master:
 476	 * TRX40 Aorus Master has two USB-audio devices, one for the front
 477	 * headphone with ESS SABRE9218 DAC chip, while another for the rest
 478	 * I/O (the rear panel and the front mic) with Realtek ALC1220-VB.
 479	 * Here we provide two distinct names for making UCM profiles easier.
 480	 */
 481	PROFILE_NAME(0x0414, 0xa000, "Gigabyte", "Aorus Master Front Headphone",
 482		     "Gigabyte-Aorus-Master-Front-Headphone"),
 483	PROFILE_NAME(0x0414, 0xa001, "Gigabyte", "Aorus Master Main Audio",
 484		     "Gigabyte-Aorus-Master-Main-Audio"),
 485
 486	/* Gigabyte TRX40 Aorus Pro WiFi */
 487	PROFILE_NAME(0x0414, 0xa002,
 488		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
 489
 490	/* Creative/E-Mu devices */
 491	DEVICE_NAME(0x041e, 0x3010, "Creative Labs", "Sound Blaster MP3+"),
 492	/* Creative/Toshiba Multimedia Center SB-0500 */
 493	DEVICE_NAME(0x041e, 0x3048, "Toshiba", "SB-0500"),
 494
 495	/* Logitech Audio Devices */
 496	DEVICE_NAME(0x046d, 0x0867, "Logitech, Inc.", "Logi-MeetUp"),
 497	DEVICE_NAME(0x046d, 0x0874, "Logitech, Inc.", "Logi-Tap-Audio"),
 498	DEVICE_NAME(0x046d, 0x087c, "Logitech, Inc.", "Logi-Huddle"),
 499	DEVICE_NAME(0x046d, 0x0898, "Logitech, Inc.", "Logi-RB-Audio"),
 500	DEVICE_NAME(0x046d, 0x08d2, "Logitech, Inc.", "Logi-RBM-Audio"),
 501	DEVICE_NAME(0x046d, 0x0990, "Logitech, Inc.", "QuickCam Pro 9000"),
 502
 503	DEVICE_NAME(0x05e1, 0x0408, "Syntek", "STK1160"),
 504	DEVICE_NAME(0x05e1, 0x0480, "Hauppauge", "Woodbury"),
 505
 506	/* ASUS ROG Zenith II: this machine has also two devices, one for
 507	 * the front headphone and another for the rest
 508	 */
 509	PROFILE_NAME(0x0b05, 0x1915, "ASUS", "Zenith II Front Headphone",
 510		     "Zenith-II-Front-Headphone"),
 511	PROFILE_NAME(0x0b05, 0x1916, "ASUS", "Zenith II Main Audio",
 512		     "Zenith-II-Main-Audio"),
 513
 514	/* ASUS ROG Strix */
 515	PROFILE_NAME(0x0b05, 0x1917,
 516		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
 517	/* ASUS PRIME TRX40 PRO-S */
 518	PROFILE_NAME(0x0b05, 0x1918,
 519		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
 520
 521	/* Dell WD15 Dock */
 522	PROFILE_NAME(0x0bda, 0x4014, "Dell", "WD15 Dock", "Dell-WD15-Dock"),
 523	/* Dell WD19 Dock */
 524	PROFILE_NAME(0x0bda, 0x402e, "Dell", "WD19 Dock", "Dell-WD15-Dock"),
 525
 526	DEVICE_NAME(0x0ccd, 0x0028, "TerraTec", "Aureon5.1MkII"),
 527
 528	/*
 529	 * The original product_name is "USB Sound Device", however this name
 530	 * is also used by the CM106 based cards, so make it unique.
 531	 */
 532	DEVICE_NAME(0x0d8c, 0x0102, NULL, "ICUSBAUDIO7D"),
 533	DEVICE_NAME(0x0d8c, 0x0103, NULL, "Audio Advantage MicroII"),
 534
 535	/* MSI TRX40 Creator */
 536	PROFILE_NAME(0x0db0, 0x0d64,
 537		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
 538	/* MSI TRX40 */
 539	PROFILE_NAME(0x0db0, 0x543d,
 540		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
 541
 542	DEVICE_NAME(0x0fd9, 0x0008, "Hauppauge", "HVR-950Q"),
 543
 544	/* Dock/Stand for HP Engage Go */
 545	PROFILE_NAME(0x103c, 0x830a, "HP", "HP Engage Go Dock",
 546		     "HP-Engage-Go-Dock"),
 547
 548	/* Stanton/N2IT Final Scratch v1 device ('Scratchamp') */
 549	DEVICE_NAME(0x103d, 0x0100, "Stanton", "ScratchAmp"),
 550	DEVICE_NAME(0x103d, 0x0101, "Stanton", "ScratchAmp"),
 551
 552	/* aka. Serato Scratch Live DJ Box */
 553	DEVICE_NAME(0x13e5, 0x0001, "Rane", "SL-1"),
 554
 555	/* Lenovo ThinkStation P620 Rear Line-in, Line-out and Microphone */
 556	PROFILE_NAME(0x17aa, 0x1046, "Lenovo", "ThinkStation P620 Rear",
 557		     "Lenovo-ThinkStation-P620-Rear"),
 558	/* Lenovo ThinkStation P620 Internal Speaker + Front Headset */
 559	PROFILE_NAME(0x17aa, 0x104d, "Lenovo", "ThinkStation P620 Main",
 560		     "Lenovo-ThinkStation-P620-Main"),
 561
 562	/* Asrock TRX40 Creator */
 563	PROFILE_NAME(0x26ce, 0x0a01,
 564		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
 565
 566	DEVICE_NAME(0x2040, 0x7200, "Hauppauge", "HVR-950Q"),
 567	DEVICE_NAME(0x2040, 0x7201, "Hauppauge", "HVR-950Q-MXL"),
 568	DEVICE_NAME(0x2040, 0x7210, "Hauppauge", "HVR-950Q"),
 569	DEVICE_NAME(0x2040, 0x7211, "Hauppauge", "HVR-950Q-MXL"),
 570	DEVICE_NAME(0x2040, 0x7213, "Hauppauge", "HVR-950Q"),
 571	DEVICE_NAME(0x2040, 0x7217, "Hauppauge", "HVR-950Q"),
 572	DEVICE_NAME(0x2040, 0x721b, "Hauppauge", "HVR-950Q"),
 573	DEVICE_NAME(0x2040, 0x721e, "Hauppauge", "HVR-950Q"),
 574	DEVICE_NAME(0x2040, 0x721f, "Hauppauge", "HVR-950Q"),
 575	DEVICE_NAME(0x2040, 0x7240, "Hauppauge", "HVR-850"),
 576	DEVICE_NAME(0x2040, 0x7260, "Hauppauge", "HVR-950Q"),
 577	DEVICE_NAME(0x2040, 0x7270, "Hauppauge", "HVR-950Q"),
 578	DEVICE_NAME(0x2040, 0x7280, "Hauppauge", "HVR-950Q"),
 579	DEVICE_NAME(0x2040, 0x7281, "Hauppauge", "HVR-950Q-MXL"),
 580	DEVICE_NAME(0x2040, 0x8200, "Hauppauge", "Woodbury"),
 581
 582	{ } /* terminator */
 583};
 584
 585static const struct usb_audio_device_name *
 586lookup_device_name(u32 id)
 587{
 588	static const struct usb_audio_device_name *p;
 589
 590	for (p = usb_audio_names; p->id; p++)
 591		if (p->id == id)
 592			return p;
 593	return NULL;
 594}
 595
 596/*
 597 * free the chip instance
 598 *
 599 * here we have to do not much, since pcm and controls are already freed
 600 *
 601 */
 602
 603static void snd_usb_audio_free(struct snd_card *card)
 604{
 605	struct snd_usb_audio *chip = card->private_data;
 606
 607	snd_usb_endpoint_free_all(chip);
 608	snd_usb_midi_v2_free_all(chip);
 609
 610	mutex_destroy(&chip->mutex);
 611	if (!atomic_read(&chip->shutdown))
 612		dev_set_drvdata(&chip->dev->dev, NULL);
 613}
 614
 615static void usb_audio_make_shortname(struct usb_device *dev,
 616				     struct snd_usb_audio *chip,
 617				     const struct snd_usb_audio_quirk *quirk)
 618{
 619	struct snd_card *card = chip->card;
 620	const struct usb_audio_device_name *preset;
 621	const char *s = NULL;
 622
 623	preset = lookup_device_name(chip->usb_id);
 624	if (preset && preset->product_name)
 625		s = preset->product_name;
 626	else if (quirk && quirk->product_name)
 627		s = quirk->product_name;
 628	if (s && *s) {
 629		strscpy(card->shortname, s, sizeof(card->shortname));
 630		return;
 631	}
 632
 633	/* retrieve the device string as shortname */
 634	if (!dev->descriptor.iProduct ||
 635	    usb_string(dev, dev->descriptor.iProduct,
 636		       card->shortname, sizeof(card->shortname)) <= 0) {
 637		/* no name available from anywhere, so use ID */
 638		scnprintf(card->shortname, sizeof(card->shortname),
 639			  "USB Device %#04x:%#04x",
 640			  USB_ID_VENDOR(chip->usb_id),
 641			  USB_ID_PRODUCT(chip->usb_id));
 642	}
 643
 644	strim(card->shortname);
 645}
 646
 647static void usb_audio_make_longname(struct usb_device *dev,
 648				    struct snd_usb_audio *chip,
 649				    const struct snd_usb_audio_quirk *quirk)
 650{
 651	struct snd_card *card = chip->card;
 652	const struct usb_audio_device_name *preset;
 653	const char *s = NULL;
 654	int len;
 655
 656	preset = lookup_device_name(chip->usb_id);
 657
 658	/* shortcut - if any pre-defined string is given, use it */
 659	if (preset && preset->profile_name)
 660		s = preset->profile_name;
 661	if (s && *s) {
 662		strscpy(card->longname, s, sizeof(card->longname));
 663		return;
 664	}
 665
 666	if (preset && preset->vendor_name)
 667		s = preset->vendor_name;
 668	else if (quirk && quirk->vendor_name)
 669		s = quirk->vendor_name;
 670	*card->longname = 0;
 671	if (s && *s) {
 672		strscpy(card->longname, s, sizeof(card->longname));
 673	} else {
 674		/* retrieve the vendor and device strings as longname */
 675		if (dev->descriptor.iManufacturer)
 676			usb_string(dev, dev->descriptor.iManufacturer,
 677				   card->longname, sizeof(card->longname));
 678		/* we don't really care if there isn't any vendor string */
 679	}
 680	if (*card->longname) {
 681		strim(card->longname);
 682		if (*card->longname)
 683			strlcat(card->longname, " ", sizeof(card->longname));
 684	}
 685
 686	strlcat(card->longname, card->shortname, sizeof(card->longname));
 687
 688	len = strlcat(card->longname, " at ", sizeof(card->longname));
 689
 690	if (len < sizeof(card->longname))
 691		usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
 692
 693	switch (snd_usb_get_speed(dev)) {
 694	case USB_SPEED_LOW:
 695		strlcat(card->longname, ", low speed", sizeof(card->longname));
 696		break;
 697	case USB_SPEED_FULL:
 698		strlcat(card->longname, ", full speed", sizeof(card->longname));
 699		break;
 700	case USB_SPEED_HIGH:
 701		strlcat(card->longname, ", high speed", sizeof(card->longname));
 702		break;
 703	case USB_SPEED_SUPER:
 704		strlcat(card->longname, ", super speed", sizeof(card->longname));
 705		break;
 706	case USB_SPEED_SUPER_PLUS:
 707		strlcat(card->longname, ", super speed plus", sizeof(card->longname));
 708		break;
 709	default:
 710		break;
 711	}
 712}
 713
 714static void snd_usb_init_quirk_flags(int idx, struct snd_usb_audio *chip)
 715{
 716	size_t i;
 717
 718	guard(mutex)(&quirk_flags_mutex);
 719
 720	/* old style option found: the position-based integer value */
 721	if (quirk_flags[idx] &&
 722	    !kstrtou32(quirk_flags[idx], 0, &chip->quirk_flags)) {
 723		snd_usb_apply_flag_dbg("module param", chip, chip->quirk_flags);
 724		return;
 725	}
 726
 727	/* take the default quirk from the quirk table */
 728	snd_usb_init_quirk_flags_table(chip);
 729
 730	/* add or correct quirk bits from options */
 731	for (i = 0; i < ARRAY_SIZE(quirk_flags); i++) {
 732		if (!quirk_flags[i] || !*quirk_flags[i])
 733			break;
 734
 735		snd_usb_init_quirk_flags_parse_string(chip, quirk_flags[i]);
 736	}
 737}
 738
 739/*
 740 * create a chip instance and set its names.
 741 */
 742static int snd_usb_audio_create(struct usb_interface *intf,
 743				struct usb_device *dev, int idx,
 744				const struct snd_usb_audio_quirk *quirk,
 745				unsigned int usb_id,
 746				struct snd_usb_audio **rchip)
 747{
 748	struct snd_card *card;
 749	struct snd_usb_audio *chip;
 750	int err;
 751	char component[14];
 752
 753	*rchip = NULL;
 754
 755	switch (snd_usb_get_speed(dev)) {
 756	case USB_SPEED_LOW:
 757	case USB_SPEED_FULL:
 758	case USB_SPEED_HIGH:
 759	case USB_SPEED_SUPER:
 760	case USB_SPEED_SUPER_PLUS:
 761		break;
 762	default:
 763		dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev));
 764		return -ENXIO;
 765	}
 766
 767	err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE,
 768			   sizeof(*chip), &card);
 769	if (err < 0) {
 770		dev_err(&dev->dev, "cannot create card instance %d\n", idx);
 771		return err;
 772	}
 773
 774	chip = card->private_data;
 775	mutex_init(&chip->mutex);
 776	init_waitqueue_head(&chip->shutdown_wait);
 777	chip->index = idx;
 778	chip->dev = dev;
 779	chip->card = card;
 780	chip->setup = device_setup[idx];
 781	chip->generic_implicit_fb = implicit_fb[idx];
 782	chip->autoclock = autoclock;
 783	chip->lowlatency = lowlatency;
 784	atomic_set(&chip->active, 1); /* avoid autopm during probing */
 785	atomic_set(&chip->usage_count, 0);
 786	atomic_set(&chip->shutdown, 0);
 787
 788	chip->usb_id = usb_id;
 789	INIT_LIST_HEAD(&chip->pcm_list);
 790	INIT_LIST_HEAD(&chip->ep_list);
 791	INIT_LIST_HEAD(&chip->iface_ref_list);
 792	INIT_LIST_HEAD(&chip->clock_ref_list);
 793	INIT_LIST_HEAD(&chip->midi_list);
 794	INIT_LIST_HEAD(&chip->midi_v2_list);
 795	INIT_LIST_HEAD(&chip->mixer_list);
 796
 797	snd_usb_init_quirk_flags(idx, chip);
 798
 799	card->private_free = snd_usb_audio_free;
 800
 801	strscpy(card->driver, "USB-Audio");
 802	scnprintf(component, sizeof(component), "USB%04x:%04x",
 803		  USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
 804	snd_component_add(card, component);
 805
 806	usb_audio_make_shortname(dev, chip, quirk);
 807	usb_audio_make_longname(dev, chip, quirk);
 808
 809	snd_usb_audio_create_proc(chip);
 810
 811	*rchip = chip;
 812	return 0;
 813}
 814
 815/* look for a matching quirk alias id */
 816static bool get_alias_id(struct usb_device *dev, unsigned int *id)
 817{
 818	int i;
 819	unsigned int src, dst;
 820
 821	for (i = 0; i < ARRAY_SIZE(quirk_alias); i++) {
 822		if (!quirk_alias[i] ||
 823		    sscanf(quirk_alias[i], "%x:%x", &src, &dst) != 2 ||
 824		    src != *id)
 825			continue;
 826		dev_info(&dev->dev,
 827			 "device (%04x:%04x): applying quirk alias %04x:%04x\n",
 828			 USB_ID_VENDOR(*id), USB_ID_PRODUCT(*id),
 829			 USB_ID_VENDOR(dst), USB_ID_PRODUCT(dst));
 830		*id = dst;
 831		return true;
 832	}
 833
 834	return false;
 835}
 836
 837static int check_delayed_register_option(struct snd_usb_audio *chip)
 838{
 839	int i;
 840	unsigned int id, inum;
 841
 842	for (i = 0; i < ARRAY_SIZE(delayed_register); i++) {
 843		if (delayed_register[i] &&
 844		    sscanf(delayed_register[i], "%x:%x", &id, &inum) == 2 &&
 845		    id == chip->usb_id)
 846			return inum;
 847	}
 848
 849	return -1;
 850}
 851
 852static const struct usb_device_id usb_audio_ids[]; /* defined below */
 853
 854/* look for the last interface that matches with our ids and remember it */
 855static void find_last_interface(struct snd_usb_audio *chip)
 856{
 857	struct usb_host_config *config = chip->dev->actconfig;
 858	struct usb_interface *intf;
 859	int i;
 860
 861	if (!config)
 862		return;
 863	for (i = 0; i < config->desc.bNumInterfaces; i++) {
 864		intf = config->interface[i];
 865		if (usb_match_id(intf, usb_audio_ids))
 866			chip->last_iface = intf->altsetting[0].desc.bInterfaceNumber;
 867	}
 868	usb_audio_dbg(chip, "Found last interface = %d\n", chip->last_iface);
 869}
 870
 871/* look for the corresponding quirk */
 872static const struct snd_usb_audio_quirk *
 873get_alias_quirk(struct usb_device *dev, unsigned int id)
 874{
 875	const struct usb_device_id *p;
 876
 877	for (p = usb_audio_ids; p->match_flags; p++) {
 878		/* FIXME: this checks only vendor:product pair in the list */
 879		if ((p->match_flags & USB_DEVICE_ID_MATCH_DEVICE) ==
 880		    USB_DEVICE_ID_MATCH_DEVICE &&
 881		    p->idVendor == USB_ID_VENDOR(id) &&
 882		    p->idProduct == USB_ID_PRODUCT(id))
 883			return (const struct snd_usb_audio_quirk *)p->driver_info;
 884	}
 885
 886	return NULL;
 887}
 888
 889/* register card if we reach to the last interface or to the specified
 890 * one given via option
 891 */
 892static int try_to_register_card(struct snd_usb_audio *chip, int ifnum)
 893{
 894	struct usb_interface *iface;
 895
 896	if (check_delayed_register_option(chip) == ifnum ||
 897	    chip->last_iface == ifnum)
 898		return snd_card_register(chip->card);
 899
 900	iface = usb_ifnum_to_if(chip->dev, chip->last_iface);
 901	if (iface && usb_interface_claimed(iface))
 902		return snd_card_register(chip->card);
 903
 904	return 0;
 905}
 906
 907/*
 908 * probe the active usb device
 909 *
 910 * note that this can be called multiple times per a device, when it
 911 * includes multiple audio control interfaces.
 912 *
 913 * thus we check the usb device pointer and creates the card instance
 914 * only at the first time.  the successive calls of this function will
 915 * append the pcm interface to the corresponding card.
 916 */
 917static int usb_audio_probe(struct usb_interface *intf,
 918			   const struct usb_device_id *usb_id)
 919{
 920	struct usb_device *dev = interface_to_usbdev(intf);
 921	const struct snd_usb_audio_quirk *quirk =
 922		(const struct snd_usb_audio_quirk *)usb_id->driver_info;
 923	struct snd_usb_audio *chip;
 924	int i, err;
 925	struct usb_host_interface *alts;
 926	int ifnum;
 927	u32 id;
 928
 929	alts = &intf->altsetting[0];
 930	ifnum = get_iface_desc(alts)->bInterfaceNumber;
 931	id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
 932		    le16_to_cpu(dev->descriptor.idProduct));
 933	if (get_alias_id(dev, &id))
 934		quirk = get_alias_quirk(dev, id);
 935	if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
 936		return -ENXIO;
 937	if (quirk && quirk->ifnum == QUIRK_NODEV_INTERFACE)
 938		return -ENODEV;
 939
 940	err = snd_usb_apply_boot_quirk(dev, intf, quirk, id);
 941	if (err < 0)
 942		return err;
 943
 944	/*
 945	 * found a config.  now register to ALSA
 946	 */
 947
 948	/* check whether it's already registered */
 949	chip = NULL;
 950	guard(mutex)(&register_mutex);
 951	for (i = 0; i < SNDRV_CARDS; i++) {
 952		if (usb_chip[i] && usb_chip[i]->dev == dev) {
 953			if (atomic_read(&usb_chip[i]->shutdown)) {
 954				dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n");
 955				err = -EIO;
 956				goto __error;
 957			}
 958			chip = usb_chip[i];
 959			atomic_inc(&chip->active); /* avoid autopm */
 960			break;
 961		}
 962	}
 963	if (! chip) {
 964		err = snd_usb_apply_boot_quirk_once(dev, intf, quirk, id);
 965		if (err < 0)
 966			goto __error;
 967
 968		/* it's a fresh one.
 969		 * now look for an empty slot and create a new card instance
 970		 */
 971		for (i = 0; i < SNDRV_CARDS; i++)
 972			if (!usb_chip[i] &&
 973			    (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
 974			    (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
 975				if (enable[i]) {
 976					err = snd_usb_audio_create(intf, dev, i, quirk,
 977								   id, &chip);
 978					if (err < 0)
 979						goto __error;
 980					break;
 981				} else if (vid[i] != -1 || pid[i] != -1) {
 982					dev_info(&dev->dev,
 983						 "device (%04x:%04x) is disabled\n",
 984						 USB_ID_VENDOR(id),
 985						 USB_ID_PRODUCT(id));
 986					err = -ENOENT;
 987					goto __error;
 988				}
 989			}
 990		if (!chip) {
 991			dev_err(&dev->dev, "no available usb audio device\n");
 992			err = -ENODEV;
 993			goto __error;
 994		}
 995		find_last_interface(chip);
 996	}
 997
 998	if (chip->num_interfaces >= MAX_CARD_INTERFACES) {
 999		dev_info(&dev->dev, "Too many interfaces assigned to the single USB-audio card\n");
1000		err = -EINVAL;
1001		goto __error;
1002	}
1003
1004	dev_set_drvdata(&dev->dev, chip);
1005
1006	if (ignore_ctl_error)
1007		chip->quirk_flags |= QUIRK_FLAG_IGNORE_CTL_ERROR;
1008
1009	if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND)
1010		usb_disable_autosuspend(interface_to_usbdev(intf));
1011
1012	/*
1013	 * For devices with more than one control interface, we assume the
1014	 * first contains the audio controls. We might need a more specific
1015	 * check here in the future.
1016	 */
1017	if (!chip->ctrl_intf)
1018		chip->ctrl_intf = alts;
1019
1020	err = 1; /* continue */
1021	if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
1022		/* need some special handlings */
1023		err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk);
1024		if (err < 0)
1025			goto __error;
1026	}
1027
1028	if (err > 0) {
1029		/* create normal USB audio interfaces */
1030		err = snd_usb_create_streams(chip, ifnum);
1031		if (err < 0)
1032			goto __error;
1033		err = snd_usb_create_mixer(chip, ifnum);
1034		if (err < 0)
1035			goto __error;
1036	}
1037
1038	if (chip->need_delayed_register) {
1039		dev_info(&dev->dev,
1040			 "Found post-registration device assignment: %08x:%02x\n",
1041			 chip->usb_id, ifnum);
1042		chip->need_delayed_register = false; /* clear again */
1043	}
1044
1045	err = try_to_register_card(chip, ifnum);
1046	if (err < 0)
1047		goto __error_no_register;
1048
1049	if (chip->quirk_flags & QUIRK_FLAG_SHARE_MEDIA_DEVICE) {
1050		/* don't want to fail when snd_media_device_create() fails */
1051		snd_media_device_create(chip, intf);
1052	}
1053
1054	if (quirk)
1055		chip->quirk_type = quirk->type;
1056
1057	usb_chip[chip->index] = chip;
1058	chip->intf[chip->num_interfaces] = intf;
1059	chip->num_interfaces++;
1060	usb_set_intfdata(intf, chip);
1061	atomic_dec(&chip->active);
1062
1063	if (platform_ops && platform_ops->connect_cb)
1064		platform_ops->connect_cb(chip);
1065
1066	return 0;
1067
1068 __error:
1069	/* in the case of error in secondary interface, still try to register */
1070	if (chip)
1071		try_to_register_card(chip, ifnum);
1072
1073 __error_no_register:
1074	if (chip) {
1075		/* chip->active is inside the chip->card object,
1076		 * decrement before memory is possibly returned.
1077		 */
1078		atomic_dec(&chip->active);
1079		if (!chip->num_interfaces)
1080			snd_card_free(chip->card);
1081	}
1082	return err;
1083}
1084
1085/*
1086 * we need to take care of counter, since disconnection can be called also
1087 * many times as well as usb_audio_probe().
1088 */
1089static bool __usb_audio_disconnect(struct usb_interface *intf,
1090				   struct snd_usb_audio *chip,
1091				   struct snd_card *card)
1092{
1093	struct list_head *p;
1094
1095	guard(mutex)(&register_mutex);
1096
1097	if (platform_ops && platform_ops->disconnect_cb)
1098		platform_ops->disconnect_cb(chip);
1099
1100	if (atomic_inc_return(&chip->shutdown) == 1) {
1101		struct snd_usb_stream *as;
1102		struct snd_usb_endpoint *ep;
1103		struct usb_mixer_interface *mixer;
1104
1105		/* wait until all pending tasks done;
1106		 * they are protected by snd_usb_lock_shutdown()
1107		 */
1108		wait_event(chip->shutdown_wait,
1109			   !atomic_read(&chip->usage_count));
1110		snd_card_disconnect(card);
1111		/* release the pcm resources */
1112		list_for_each_entry(as, &chip->pcm_list, list) {
1113			snd_usb_stream_disconnect(as);
1114		}
1115		/* release the endpoint resources */
1116		list_for_each_entry(ep, &chip->ep_list, list) {
1117			snd_usb_endpoint_release(ep);
1118		}
1119		/* release the midi resources */
1120		list_for_each(p, &chip->midi_list) {
1121			snd_usbmidi_disconnect(p);
1122		}
1123		snd_usb_midi_v2_disconnect_all(chip);
1124		/*
1125		 * Nice to check quirk && quirk->shares_media_device and
1126		 * then call the snd_media_device_delete(). Don't have
1127		 * access to the quirk here. snd_media_device_delete()
1128		 * accesses mixer_list
1129		 */
1130		snd_media_device_delete(chip);
1131
1132		/* release mixer resources */
1133		list_for_each_entry(mixer, &chip->mixer_list, list) {
1134			snd_usb_mixer_disconnect(mixer);
1135		}
1136	}
1137
1138	if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND)
1139		usb_enable_autosuspend(interface_to_usbdev(intf));
1140
1141	chip->num_interfaces--;
1142	if (chip->num_interfaces > 0)
1143		return false;
1144
1145	usb_chip[chip->index] = NULL;
1146	return true;
1147}
1148
1149static void usb_audio_disconnect(struct usb_interface *intf)
1150{
1151	struct snd_usb_audio *chip = usb_get_intfdata(intf);
1152	struct snd_card *card;
1153
1154	if (chip == USB_AUDIO_IFACE_UNUSED)
1155		return;
1156
1157	card = chip->card;
1158	if (__usb_audio_disconnect(intf, chip, card))
1159		snd_card_free_when_closed(card);
1160}
1161
1162/* lock the shutdown (disconnect) task and autoresume */
1163int snd_usb_lock_shutdown(struct snd_usb_audio *chip)
1164{
1165	int err;
1166
1167	atomic_inc(&chip->usage_count);
1168	if (atomic_read(&chip->shutdown)) {
1169		err = -EIO;
1170		goto error;
1171	}
1172	err = snd_usb_autoresume(chip);
1173	if (err < 0)
1174		goto error;
1175	return 0;
1176
1177 error:
1178	if (atomic_dec_and_test(&chip->usage_count))
1179		wake_up(&chip->shutdown_wait);
1180	return err;
1181}
1182EXPORT_SYMBOL_GPL(snd_usb_lock_shutdown);
1183
1184/* autosuspend and unlock the shutdown */
1185void snd_usb_unlock_shutdown(struct snd_usb_audio *chip)
1186{
1187	snd_usb_autosuspend(chip);
1188	if (atomic_dec_and_test(&chip->usage_count))
1189		wake_up(&chip->shutdown_wait);
1190}
1191EXPORT_SYMBOL_GPL(snd_usb_unlock_shutdown);
1192
1193int snd_usb_autoresume(struct snd_usb_audio *chip)
1194{
1195	int i, err;
1196
1197	if (atomic_read(&chip->shutdown))
1198		return -EIO;
1199	if (atomic_inc_return(&chip->active) != 1)
1200		return 0;
1201
1202	for (i = 0; i < chip->num_interfaces; i++) {
1203		err = usb_autopm_get_interface(chip->intf[i]);
1204		if (err < 0) {
1205			/* rollback */
1206			while (--i >= 0)
1207				usb_autopm_put_interface(chip->intf[i]);
1208			atomic_dec(&chip->active);
1209			return err;
1210		}
1211	}
1212	return 0;
1213}
1214EXPORT_SYMBOL_GPL(snd_usb_autoresume);
1215
1216void snd_usb_autosuspend(struct snd_usb_audio *chip)
1217{
1218	int i;
1219
1220	if (atomic_read(&chip->shutdown))
1221		return;
1222	if (!atomic_dec_and_test(&chip->active))
1223		return;
1224
1225	for (i = 0; i < chip->num_interfaces; i++)
1226		usb_autopm_put_interface(chip->intf[i]);
1227}
1228EXPORT_SYMBOL_GPL(snd_usb_autosuspend);
1229
1230static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
1231{
1232	struct snd_usb_audio *chip = usb_get_intfdata(intf);
1233	struct snd_usb_stream *as;
1234	struct snd_usb_endpoint *ep;
1235	struct usb_mixer_interface *mixer;
1236	struct list_head *p;
1237
1238	if (chip == USB_AUDIO_IFACE_UNUSED)
1239		return 0;
1240
1241	if (!chip->num_suspended_intf++) {
1242		list_for_each_entry(as, &chip->pcm_list, list)
1243			snd_usb_pcm_suspend(as);
1244		list_for_each_entry(ep, &chip->ep_list, list)
1245			snd_usb_endpoint_suspend(ep);
1246		list_for_each(p, &chip->midi_list)
1247			snd_usbmidi_suspend(p);
1248		list_for_each_entry(mixer, &chip->mixer_list, list)
1249			snd_usb_mixer_suspend(mixer);
1250		snd_usb_midi_v2_suspend_all(chip);
1251	}
1252
1253	if (!PMSG_IS_AUTO(message) && !chip->system_suspend) {
1254		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1255		chip->system_suspend = chip->num_suspended_intf;
1256	}
1257
1258	if (platform_ops && platform_ops->suspend_cb)
1259		platform_ops->suspend_cb(intf, message);
1260
1261	return 0;
1262}
1263
1264static int usb_audio_resume(struct usb_interface *intf)
1265{
1266	struct snd_usb_audio *chip = usb_get_intfdata(intf);
1267	struct snd_usb_stream *as;
1268	struct usb_mixer_interface *mixer;
1269	struct list_head *p;
1270	int err = 0;
1271
1272	if (chip == USB_AUDIO_IFACE_UNUSED)
1273		return 0;
1274
1275	atomic_inc(&chip->active); /* avoid autopm */
1276	if (chip->num_suspended_intf > 1)
1277		goto out;
1278
1279	list_for_each_entry(as, &chip->pcm_list, list) {
1280		err = snd_usb_pcm_resume(as);
1281		if (err < 0)
1282			goto err_out;
1283	}
1284
1285	/*
1286	 * ALSA leaves material resumption to user space
1287	 * we just notify and restart the mixers
1288	 */
1289	list_for_each_entry(mixer, &chip->mixer_list, list) {
1290		err = snd_usb_mixer_resume(mixer);
1291		if (err < 0)
1292			goto err_out;
1293	}
1294
1295	list_for_each(p, &chip->midi_list) {
1296		snd_usbmidi_resume(p);
1297	}
1298
1299	snd_usb_midi_v2_resume_all(chip);
1300
1301	if (platform_ops && platform_ops->resume_cb)
1302		platform_ops->resume_cb(intf);
1303
1304 out:
1305	if (chip->num_suspended_intf == chip->system_suspend) {
1306		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1307		chip->system_suspend = 0;
1308	}
1309	chip->num_suspended_intf--;
1310
1311err_out:
1312	atomic_dec(&chip->active); /* allow autopm after this point */
1313	return err;
1314}
1315
1316static const struct usb_device_id usb_audio_ids [] = {
1317#include "quirks-table.h"
1318    { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
1319      .bInterfaceClass = USB_CLASS_AUDIO,
1320      .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
1321    { }						/* Terminating entry */
1322};
1323MODULE_DEVICE_TABLE(usb, usb_audio_ids);
1324
1325/*
1326 * entry point for linux usb interface
1327 */
1328
1329static struct usb_driver usb_audio_driver = {
1330	.name =		"snd-usb-audio",
1331	.probe =	usb_audio_probe,
1332	.disconnect =	usb_audio_disconnect,
1333	.suspend =	usb_audio_suspend,
1334	.resume =	usb_audio_resume,
1335	.reset_resume =	usb_audio_resume,
1336	.id_table =	usb_audio_ids,
1337	.supports_autosuspend = 1,
1338};
1339
1340module_usb_driver(usb_audio_driver);
~/ Read-only file preview.