xref: /linux/sound/usb/card.c (revision a8e7ef3cec99ba2487110e01d77a8a278593b3e9)
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 
59 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
60 MODULE_DESCRIPTION("USB Audio");
61 MODULE_LICENSE("GPL");
62 
63 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
64 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
65 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
66 /* Vendor/product IDs for this card */
67 static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
68 static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
69 static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
70 static bool ignore_ctl_error;
71 static bool autoclock = true;
72 static bool lowlatency = true;
73 static char *quirk_alias[SNDRV_CARDS];
74 static char *delayed_register[SNDRV_CARDS];
75 static bool implicit_fb[SNDRV_CARDS];
76 static char *quirk_flags[SNDRV_CARDS];
77 
78 bool snd_usb_use_vmalloc = true;
79 bool snd_usb_skip_validation;
80 
81 module_param_array(index, int, NULL, 0444);
82 MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
83 module_param_array(id, charp, NULL, 0444);
84 MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
85 module_param_array(enable, bool, NULL, 0444);
86 MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
87 module_param_array(vid, int, NULL, 0444);
88 MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
89 module_param_array(pid, int, NULL, 0444);
90 MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
91 module_param_array(device_setup, int, NULL, 0444);
92 MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
93 module_param(ignore_ctl_error, bool, 0444);
94 MODULE_PARM_DESC(ignore_ctl_error,
95 		 "Ignore errors from USB controller for mixer interfaces.");
96 module_param(autoclock, bool, 0444);
97 MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes).");
98 module_param(lowlatency, bool, 0444);
99 MODULE_PARM_DESC(lowlatency, "Enable low latency playback (default: yes).");
100 module_param_array(quirk_alias, charp, NULL, 0444);
101 MODULE_PARM_DESC(quirk_alias, "Quirk aliases, e.g. 0123abcd:5678beef.");
102 module_param_array(delayed_register, charp, NULL, 0444);
103 MODULE_PARM_DESC(delayed_register, "Quirk for delayed registration, given by id:iface, e.g. 0123abcd:4.");
104 module_param_array(implicit_fb, bool, NULL, 0444);
105 MODULE_PARM_DESC(implicit_fb, "Apply generic implicit feedback sync mode.");
106 module_param_named(use_vmalloc, snd_usb_use_vmalloc, bool, 0444);
107 MODULE_PARM_DESC(use_vmalloc, "Use vmalloc for PCM intermediate buffers (default: yes).");
108 module_param_named(skip_validation, snd_usb_skip_validation, bool, 0444);
109 MODULE_PARM_DESC(skip_validation, "Skip unit descriptor validation (default: no).");
110 
111 /* protects quirk_flags */
112 static DEFINE_MUTEX(quirk_flags_mutex);
113 
114 static 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 
121 static 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 
129 module_param_array(quirk_flags, quirkp, NULL, 0644);
130 MODULE_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 
137 static DEFINE_MUTEX(register_mutex);
138 static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
139 static struct usb_driver usb_audio_driver;
140 static 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  */
151 int 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 }
160 EXPORT_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  */
168 int snd_usb_unregister_platform_ops(void)
169 {
170 	guard(mutex)(&register_mutex);
171 	platform_ops = NULL;
172 
173 	return 0;
174 }
175 EXPORT_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  */
182 void 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 }
196 EXPORT_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  */
203 struct snd_usb_stream *
204 snd_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 }
228 EXPORT_SYMBOL_GPL(snd_usb_find_suppported_substream);
229 
230 /*
231  * disconnect streams
232  * called from usb_audio_disconnect()
233  */
234 static 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 
248 static 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  */
331 static 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  */
453 struct 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 */
467 static 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 
585 static const struct usb_audio_device_name *
586 lookup_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 
603 static 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 
615 static 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->product && *dev->product) {
635 		strscpy(card->shortname, dev->product);
636 	} else {
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 
647 static 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);
673 	else if (dev->manufacturer && *dev->manufacturer)
674 		strscpy(card->longname, dev->manufacturer);
675 
676 	if (*card->longname) {
677 		strim(card->longname);
678 		if (*card->longname)
679 			strlcat(card->longname, " ", sizeof(card->longname));
680 	}
681 
682 	strlcat(card->longname, card->shortname, sizeof(card->longname));
683 
684 	len = strlcat(card->longname, " at ", sizeof(card->longname));
685 
686 	if (len < sizeof(card->longname))
687 		usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
688 
689 	switch (snd_usb_get_speed(dev)) {
690 	case USB_SPEED_LOW:
691 		strlcat(card->longname, ", low speed", sizeof(card->longname));
692 		break;
693 	case USB_SPEED_FULL:
694 		strlcat(card->longname, ", full speed", sizeof(card->longname));
695 		break;
696 	case USB_SPEED_HIGH:
697 		strlcat(card->longname, ", high speed", sizeof(card->longname));
698 		break;
699 	case USB_SPEED_SUPER:
700 		strlcat(card->longname, ", super speed", sizeof(card->longname));
701 		break;
702 	case USB_SPEED_SUPER_PLUS:
703 		strlcat(card->longname, ", super speed plus", sizeof(card->longname));
704 		break;
705 	default:
706 		break;
707 	}
708 }
709 
710 static void snd_usb_init_quirk_flags(int idx, struct snd_usb_audio *chip)
711 {
712 	size_t i;
713 
714 	guard(mutex)(&quirk_flags_mutex);
715 
716 	/* old style option found: the position-based integer value */
717 	if (quirk_flags[idx] &&
718 	    !kstrtou32(quirk_flags[idx], 0, &chip->quirk_flags)) {
719 		snd_usb_apply_flag_dbg("module param", chip, chip->quirk_flags);
720 		return;
721 	}
722 
723 	/* take the default quirk from the quirk table */
724 	snd_usb_init_quirk_flags_table(chip);
725 
726 	/* add or correct quirk bits from options */
727 	for (i = 0; i < ARRAY_SIZE(quirk_flags); i++) {
728 		if (!quirk_flags[i] || !*quirk_flags[i])
729 			break;
730 
731 		snd_usb_init_quirk_flags_parse_string(chip, quirk_flags[i]);
732 	}
733 }
734 
735 /*
736  * create a chip instance and set its names.
737  */
738 static int snd_usb_audio_create(struct usb_interface *intf,
739 				struct usb_device *dev, int idx,
740 				const struct snd_usb_audio_quirk *quirk,
741 				unsigned int usb_id,
742 				struct snd_usb_audio **rchip)
743 {
744 	struct snd_card *card;
745 	struct snd_usb_audio *chip;
746 	int err;
747 	char component[14];
748 
749 	*rchip = NULL;
750 
751 	switch (snd_usb_get_speed(dev)) {
752 	case USB_SPEED_LOW:
753 	case USB_SPEED_FULL:
754 	case USB_SPEED_HIGH:
755 	case USB_SPEED_SUPER:
756 	case USB_SPEED_SUPER_PLUS:
757 		break;
758 	default:
759 		dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev));
760 		return -ENXIO;
761 	}
762 
763 	err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE,
764 			   sizeof(*chip), &card);
765 	if (err < 0) {
766 		dev_err(&dev->dev, "cannot create card instance %d\n", idx);
767 		return err;
768 	}
769 
770 	chip = card->private_data;
771 	mutex_init(&chip->mutex);
772 	init_waitqueue_head(&chip->shutdown_wait);
773 	chip->index = idx;
774 	chip->dev = dev;
775 	chip->card = card;
776 	chip->setup = device_setup[idx];
777 	chip->generic_implicit_fb = implicit_fb[idx];
778 	chip->autoclock = autoclock;
779 	chip->lowlatency = lowlatency;
780 	atomic_set(&chip->active, 1); /* avoid autopm during probing */
781 	atomic_set(&chip->usage_count, 0);
782 	atomic_set(&chip->shutdown, 0);
783 
784 	chip->usb_id = usb_id;
785 	INIT_LIST_HEAD(&chip->pcm_list);
786 	INIT_LIST_HEAD(&chip->ep_list);
787 	INIT_LIST_HEAD(&chip->iface_ref_list);
788 	INIT_LIST_HEAD(&chip->clock_ref_list);
789 	INIT_LIST_HEAD(&chip->midi_list);
790 	INIT_LIST_HEAD(&chip->midi_v2_list);
791 	INIT_LIST_HEAD(&chip->mixer_list);
792 
793 	snd_usb_init_quirk_flags(idx, chip);
794 
795 	card->private_free = snd_usb_audio_free;
796 
797 	strscpy(card->driver, "USB-Audio");
798 	scnprintf(component, sizeof(component), "USB%04x:%04x",
799 		  USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
800 	snd_component_add(card, component);
801 
802 	usb_audio_make_shortname(dev, chip, quirk);
803 	usb_audio_make_longname(dev, chip, quirk);
804 
805 	snd_usb_audio_create_proc(chip);
806 
807 	*rchip = chip;
808 	return 0;
809 }
810 
811 /* look for a matching quirk alias id */
812 static bool get_alias_id(struct usb_device *dev, unsigned int *id)
813 {
814 	int i;
815 	unsigned int src, dst;
816 
817 	for (i = 0; i < ARRAY_SIZE(quirk_alias); i++) {
818 		if (!quirk_alias[i] ||
819 		    sscanf(quirk_alias[i], "%x:%x", &src, &dst) != 2 ||
820 		    src != *id)
821 			continue;
822 		dev_info(&dev->dev,
823 			 "device (%04x:%04x): applying quirk alias %04x:%04x\n",
824 			 USB_ID_VENDOR(*id), USB_ID_PRODUCT(*id),
825 			 USB_ID_VENDOR(dst), USB_ID_PRODUCT(dst));
826 		*id = dst;
827 		return true;
828 	}
829 
830 	return false;
831 }
832 
833 static int check_delayed_register_option(struct snd_usb_audio *chip)
834 {
835 	int i;
836 	unsigned int id, inum;
837 
838 	for (i = 0; i < ARRAY_SIZE(delayed_register); i++) {
839 		if (delayed_register[i] &&
840 		    sscanf(delayed_register[i], "%x:%x", &id, &inum) == 2 &&
841 		    id == chip->usb_id)
842 			return inum;
843 	}
844 
845 	return -1;
846 }
847 
848 static const struct usb_device_id usb_audio_ids[]; /* defined below */
849 
850 /* look for the last interface that matches with our ids and remember it */
851 static void find_last_interface(struct snd_usb_audio *chip)
852 {
853 	struct usb_host_config *config = chip->dev->actconfig;
854 	struct usb_interface *intf;
855 	int i;
856 
857 	if (!config)
858 		return;
859 	for (i = 0; i < config->desc.bNumInterfaces; i++) {
860 		intf = config->interface[i];
861 		if (usb_match_id(intf, usb_audio_ids))
862 			chip->last_iface = intf->altsetting[0].desc.bInterfaceNumber;
863 	}
864 	usb_audio_dbg(chip, "Found last interface = %d\n", chip->last_iface);
865 }
866 
867 /* look for the corresponding quirk */
868 static const struct snd_usb_audio_quirk *
869 get_alias_quirk(struct usb_interface *intf, unsigned int id)
870 {
871 	const struct usb_device_id *p;
872 	struct usb_device_id match_id;
873 
874 	for (p = usb_audio_ids; p->match_flags; p++) {
875 		if ((p->match_flags & USB_DEVICE_ID_MATCH_DEVICE) !=
876 		     USB_DEVICE_ID_MATCH_DEVICE)
877 			continue;
878 		if (p->idVendor != USB_ID_VENDOR(id) ||
879 		    p->idProduct != USB_ID_PRODUCT(id))
880 			continue;
881 
882 		match_id = *p;
883 		match_id.match_flags &= ~USB_DEVICE_ID_MATCH_DEVICE;
884 		if (!match_id.match_flags || usb_match_one_id(intf, &match_id))
885 			return (const struct snd_usb_audio_quirk *)
886 				p->driver_info;
887 	}
888 	return NULL;
889 }
890 
891 /* register card if we reach to the last interface or to the specified
892  * one given via option
893  */
894 static int try_to_register_card(struct snd_usb_audio *chip, int ifnum)
895 {
896 	struct usb_interface *iface;
897 
898 	if (check_delayed_register_option(chip) == ifnum ||
899 	    chip->last_iface == ifnum)
900 		return snd_card_register(chip->card);
901 
902 	iface = usb_ifnum_to_if(chip->dev, chip->last_iface);
903 	if (iface && usb_interface_claimed(iface))
904 		return snd_card_register(chip->card);
905 
906 	return 0;
907 }
908 
909 /*
910  * probe the active usb device
911  *
912  * note that this can be called multiple times per a device, when it
913  * includes multiple audio control interfaces.
914  *
915  * thus we check the usb device pointer and creates the card instance
916  * only at the first time.  the successive calls of this function will
917  * append the pcm interface to the corresponding card.
918  */
919 static int usb_audio_probe(struct usb_interface *intf,
920 			   const struct usb_device_id *usb_id)
921 {
922 	struct usb_device *dev = interface_to_usbdev(intf);
923 	const struct snd_usb_audio_quirk *quirk =
924 		(const struct snd_usb_audio_quirk *)usb_id->driver_info;
925 	struct snd_usb_audio *chip;
926 	int i, err;
927 	struct usb_host_interface *alts;
928 	int ifnum;
929 	u32 id;
930 
931 	alts = &intf->altsetting[0];
932 	ifnum = get_iface_desc(alts)->bInterfaceNumber;
933 	id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
934 		    le16_to_cpu(dev->descriptor.idProduct));
935 	if (get_alias_id(dev, &id))
936 		quirk = get_alias_quirk(intf, id);
937 	if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
938 		return -ENXIO;
939 	if (quirk && quirk->ifnum == QUIRK_NODEV_INTERFACE)
940 		return -ENODEV;
941 
942 	err = snd_usb_apply_boot_quirk(dev, intf, quirk, id);
943 	if (err < 0)
944 		return err;
945 
946 	/*
947 	 * found a config.  now register to ALSA
948 	 */
949 
950 	/* check whether it's already registered */
951 	chip = NULL;
952 	guard(mutex)(&register_mutex);
953 	for (i = 0; i < SNDRV_CARDS; i++) {
954 		if (usb_chip[i] && usb_chip[i]->dev == dev) {
955 			if (atomic_read(&usb_chip[i]->shutdown)) {
956 				dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n");
957 				err = -EIO;
958 				goto __error;
959 			}
960 			chip = usb_chip[i];
961 			atomic_inc(&chip->active); /* avoid autopm */
962 			break;
963 		}
964 	}
965 	if (! chip) {
966 		err = snd_usb_apply_boot_quirk_once(dev, intf, quirk, id);
967 		if (err < 0)
968 			goto __error;
969 
970 		/* it's a fresh one.
971 		 * now look for an empty slot and create a new card instance
972 		 */
973 		for (i = 0; i < SNDRV_CARDS; i++)
974 			if (!usb_chip[i] &&
975 			    (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
976 			    (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
977 				if (enable[i]) {
978 					err = snd_usb_audio_create(intf, dev, i, quirk,
979 								   id, &chip);
980 					if (err < 0)
981 						goto __error;
982 					break;
983 				} else if (vid[i] != -1 || pid[i] != -1) {
984 					dev_info(&dev->dev,
985 						 "device (%04x:%04x) is disabled\n",
986 						 USB_ID_VENDOR(id),
987 						 USB_ID_PRODUCT(id));
988 					err = -ENOENT;
989 					goto __error;
990 				}
991 			}
992 		if (!chip) {
993 			dev_err(&dev->dev, "no available usb audio device\n");
994 			err = -ENODEV;
995 			goto __error;
996 		}
997 		find_last_interface(chip);
998 	}
999 
1000 	if (chip->num_interfaces >= MAX_CARD_INTERFACES) {
1001 		dev_info(&dev->dev, "Too many interfaces assigned to the single USB-audio card\n");
1002 		err = -EINVAL;
1003 		goto __error;
1004 	}
1005 
1006 	dev_set_drvdata(&dev->dev, chip);
1007 
1008 	if (ignore_ctl_error)
1009 		chip->quirk_flags |= QUIRK_FLAG_IGNORE_CTL_ERROR;
1010 
1011 	if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND)
1012 		usb_disable_autosuspend(interface_to_usbdev(intf));
1013 
1014 	/*
1015 	 * For devices with more than one control interface, we assume the
1016 	 * first contains the audio controls. We might need a more specific
1017 	 * check here in the future.
1018 	 */
1019 	if (!chip->ctrl_intf)
1020 		chip->ctrl_intf = alts;
1021 
1022 	err = 1; /* continue */
1023 	if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
1024 		/* need some special handlings */
1025 		err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk);
1026 		if (err < 0)
1027 			goto __error;
1028 	}
1029 
1030 	if (err > 0) {
1031 		/* create normal USB audio interfaces */
1032 		err = snd_usb_create_streams(chip, ifnum);
1033 		if (err < 0)
1034 			goto __error;
1035 		err = snd_usb_create_mixer(chip, ifnum);
1036 		if (err < 0)
1037 			goto __error;
1038 	}
1039 
1040 	if (chip->need_delayed_register) {
1041 		dev_info(&dev->dev,
1042 			 "Found post-registration device assignment: %08x:%02x\n",
1043 			 chip->usb_id, ifnum);
1044 		chip->need_delayed_register = false; /* clear again */
1045 	}
1046 
1047 	err = try_to_register_card(chip, ifnum);
1048 	if (err < 0)
1049 		goto __error_no_register;
1050 
1051 	if (chip->quirk_flags & QUIRK_FLAG_SHARE_MEDIA_DEVICE) {
1052 		/* don't want to fail when snd_media_device_create() fails */
1053 		snd_media_device_create(chip, intf);
1054 	}
1055 
1056 	if (quirk)
1057 		chip->quirk_type = quirk->type;
1058 
1059 	usb_chip[chip->index] = chip;
1060 	chip->intf[chip->num_interfaces] = intf;
1061 	chip->num_interfaces++;
1062 	usb_set_intfdata(intf, chip);
1063 	atomic_dec(&chip->active);
1064 
1065 	if (platform_ops && platform_ops->connect_cb)
1066 		platform_ops->connect_cb(chip);
1067 
1068 	return 0;
1069 
1070  __error:
1071 	/* in the case of error in secondary interface, still try to register */
1072 	if (chip)
1073 		try_to_register_card(chip, ifnum);
1074 
1075  __error_no_register:
1076 	if (chip) {
1077 		/* chip->active is inside the chip->card object,
1078 		 * decrement before memory is possibly returned.
1079 		 */
1080 		atomic_dec(&chip->active);
1081 		if (!chip->num_interfaces)
1082 			snd_card_free(chip->card);
1083 	}
1084 	return err;
1085 }
1086 
1087 /*
1088  * we need to take care of counter, since disconnection can be called also
1089  * many times as well as usb_audio_probe().
1090  */
1091 static bool __usb_audio_disconnect(struct usb_interface *intf,
1092 				   struct snd_usb_audio *chip,
1093 				   struct snd_card *card)
1094 {
1095 	struct list_head *p;
1096 
1097 	guard(mutex)(&register_mutex);
1098 
1099 	if (platform_ops && platform_ops->disconnect_cb)
1100 		platform_ops->disconnect_cb(chip);
1101 
1102 	if (atomic_inc_return(&chip->shutdown) == 1) {
1103 		struct snd_usb_stream *as;
1104 		struct snd_usb_endpoint *ep;
1105 		struct usb_mixer_interface *mixer;
1106 
1107 		/* wait until all pending tasks done;
1108 		 * they are protected by snd_usb_lock_shutdown()
1109 		 */
1110 		wait_event(chip->shutdown_wait,
1111 			   !atomic_read(&chip->usage_count));
1112 		snd_card_disconnect(card);
1113 		/* release the pcm resources */
1114 		list_for_each_entry(as, &chip->pcm_list, list) {
1115 			snd_usb_stream_disconnect(as);
1116 		}
1117 		/* release the endpoint resources */
1118 		list_for_each_entry(ep, &chip->ep_list, list) {
1119 			snd_usb_endpoint_release(ep);
1120 		}
1121 		/* release the midi resources */
1122 		list_for_each(p, &chip->midi_list) {
1123 			snd_usbmidi_disconnect(p);
1124 		}
1125 		snd_usb_midi_v2_disconnect_all(chip);
1126 		/*
1127 		 * Nice to check quirk && quirk->shares_media_device and
1128 		 * then call the snd_media_device_delete(). Don't have
1129 		 * access to the quirk here. snd_media_device_delete()
1130 		 * accesses mixer_list
1131 		 */
1132 		snd_media_device_delete(chip);
1133 
1134 		/* release mixer resources */
1135 		list_for_each_entry(mixer, &chip->mixer_list, list) {
1136 			snd_usb_mixer_disconnect(mixer);
1137 		}
1138 	}
1139 
1140 	if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND)
1141 		usb_enable_autosuspend(interface_to_usbdev(intf));
1142 
1143 	chip->num_interfaces--;
1144 	if (chip->num_interfaces > 0)
1145 		return false;
1146 
1147 	usb_chip[chip->index] = NULL;
1148 	return true;
1149 }
1150 
1151 static void usb_audio_disconnect(struct usb_interface *intf)
1152 {
1153 	struct snd_usb_audio *chip = usb_get_intfdata(intf);
1154 	struct snd_card *card;
1155 
1156 	if (chip == USB_AUDIO_IFACE_UNUSED)
1157 		return;
1158 
1159 	card = chip->card;
1160 	if (__usb_audio_disconnect(intf, chip, card))
1161 		snd_card_free_when_closed(card);
1162 }
1163 
1164 /* lock the shutdown (disconnect) task and autoresume */
1165 int snd_usb_lock_shutdown(struct snd_usb_audio *chip)
1166 {
1167 	int err;
1168 
1169 	atomic_inc(&chip->usage_count);
1170 	if (atomic_read(&chip->shutdown)) {
1171 		err = -EIO;
1172 		goto error;
1173 	}
1174 	err = snd_usb_autoresume(chip);
1175 	if (err < 0)
1176 		goto error;
1177 	return 0;
1178 
1179  error:
1180 	if (atomic_dec_and_test(&chip->usage_count))
1181 		wake_up(&chip->shutdown_wait);
1182 	return err;
1183 }
1184 EXPORT_SYMBOL_GPL(snd_usb_lock_shutdown);
1185 
1186 /* autosuspend and unlock the shutdown */
1187 void snd_usb_unlock_shutdown(struct snd_usb_audio *chip)
1188 {
1189 	snd_usb_autosuspend(chip);
1190 	if (atomic_dec_and_test(&chip->usage_count))
1191 		wake_up(&chip->shutdown_wait);
1192 }
1193 EXPORT_SYMBOL_GPL(snd_usb_unlock_shutdown);
1194 
1195 int snd_usb_autoresume(struct snd_usb_audio *chip)
1196 {
1197 	int i, err;
1198 
1199 	if (atomic_read(&chip->shutdown))
1200 		return -EIO;
1201 	if (atomic_inc_return(&chip->active) != 1)
1202 		return 0;
1203 
1204 	for (i = 0; i < chip->num_interfaces; i++) {
1205 		err = usb_autopm_get_interface(chip->intf[i]);
1206 		if (err < 0) {
1207 			/* rollback */
1208 			while (--i >= 0)
1209 				usb_autopm_put_interface(chip->intf[i]);
1210 			atomic_dec(&chip->active);
1211 			return err;
1212 		}
1213 	}
1214 	return 0;
1215 }
1216 EXPORT_SYMBOL_GPL(snd_usb_autoresume);
1217 
1218 void snd_usb_autosuspend(struct snd_usb_audio *chip)
1219 {
1220 	int i;
1221 
1222 	if (atomic_read(&chip->shutdown))
1223 		return;
1224 	if (!atomic_dec_and_test(&chip->active))
1225 		return;
1226 
1227 	for (i = 0; i < chip->num_interfaces; i++)
1228 		usb_autopm_put_interface(chip->intf[i]);
1229 }
1230 EXPORT_SYMBOL_GPL(snd_usb_autosuspend);
1231 
1232 static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
1233 {
1234 	struct snd_usb_audio *chip = usb_get_intfdata(intf);
1235 	struct snd_usb_stream *as;
1236 	struct snd_usb_endpoint *ep;
1237 	struct usb_mixer_interface *mixer;
1238 	struct list_head *p;
1239 
1240 	if (chip == USB_AUDIO_IFACE_UNUSED)
1241 		return 0;
1242 
1243 	if (!chip->num_suspended_intf++) {
1244 		list_for_each_entry(as, &chip->pcm_list, list)
1245 			snd_usb_pcm_suspend(as);
1246 		list_for_each_entry(ep, &chip->ep_list, list)
1247 			snd_usb_endpoint_suspend(ep);
1248 		list_for_each(p, &chip->midi_list)
1249 			snd_usbmidi_suspend(p);
1250 		list_for_each_entry(mixer, &chip->mixer_list, list)
1251 			snd_usb_mixer_suspend(mixer);
1252 		snd_usb_midi_v2_suspend_all(chip);
1253 	}
1254 
1255 	if (!PMSG_IS_AUTO(message) && !chip->system_suspend) {
1256 		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1257 		chip->system_suspend = chip->num_suspended_intf;
1258 	}
1259 
1260 	if (platform_ops && platform_ops->suspend_cb)
1261 		platform_ops->suspend_cb(intf, message);
1262 
1263 	return 0;
1264 }
1265 
1266 static int usb_audio_resume(struct usb_interface *intf)
1267 {
1268 	struct snd_usb_audio *chip = usb_get_intfdata(intf);
1269 	struct snd_usb_stream *as;
1270 	struct usb_mixer_interface *mixer;
1271 	struct list_head *p;
1272 	int err = 0;
1273 
1274 	if (chip == USB_AUDIO_IFACE_UNUSED)
1275 		return 0;
1276 
1277 	atomic_inc(&chip->active); /* avoid autopm */
1278 	if (chip->num_suspended_intf > 1)
1279 		goto out;
1280 
1281 	list_for_each_entry(as, &chip->pcm_list, list) {
1282 		err = snd_usb_pcm_resume(as);
1283 		if (err < 0)
1284 			goto err_out;
1285 	}
1286 
1287 	/*
1288 	 * ALSA leaves material resumption to user space
1289 	 * we just notify and restart the mixers
1290 	 */
1291 	list_for_each_entry(mixer, &chip->mixer_list, list) {
1292 		err = snd_usb_mixer_resume(mixer);
1293 		if (err < 0)
1294 			goto err_out;
1295 	}
1296 
1297 	list_for_each(p, &chip->midi_list) {
1298 		snd_usbmidi_resume(p);
1299 	}
1300 
1301 	snd_usb_midi_v2_resume_all(chip);
1302 
1303 	if (platform_ops && platform_ops->resume_cb)
1304 		platform_ops->resume_cb(intf);
1305 
1306  out:
1307 	if (chip->num_suspended_intf == chip->system_suspend) {
1308 		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1309 		chip->system_suspend = 0;
1310 	}
1311 	chip->num_suspended_intf--;
1312 
1313 err_out:
1314 	atomic_dec(&chip->active); /* allow autopm after this point */
1315 	return err;
1316 }
1317 
1318 static const struct usb_device_id usb_audio_ids [] = {
1319 #include "quirks-table.h"
1320     { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
1321       .bInterfaceClass = USB_CLASS_AUDIO,
1322       .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
1323     { }						/* Terminating entry */
1324 };
1325 MODULE_DEVICE_TABLE(usb, usb_audio_ids);
1326 
1327 /*
1328  * entry point for linux usb interface
1329  */
1330 
1331 static struct usb_driver usb_audio_driver = {
1332 	.name =		"snd-usb-audio",
1333 	.probe =	usb_audio_probe,
1334 	.disconnect =	usb_audio_disconnect,
1335 	.suspend =	usb_audio_suspend,
1336 	.resume =	usb_audio_resume,
1337 	.reset_resume =	usb_audio_resume,
1338 	.id_table =	usb_audio_ids,
1339 	.supports_autosuspend = 1,
1340 };
1341 
1342 module_usb_driver(usb_audio_driver);
1343