xref: /linux/sound/usb/card.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
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->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 
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, 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 
714 static 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  */
742 static 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 */
816 static 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 
837 static 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 
852 static 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 */
855 static 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 */
872 static const struct snd_usb_audio_quirk *
873 get_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  */
892 static int try_to_register_card(struct snd_usb_audio *chip, int ifnum)
893 {
894 	if (check_delayed_register_option(chip) == ifnum ||
895 	    chip->last_iface == ifnum ||
896 	    usb_interface_claimed(usb_ifnum_to_if(chip->dev, chip->last_iface)))
897 		return snd_card_register(chip->card);
898 	return 0;
899 }
900 
901 /*
902  * probe the active usb device
903  *
904  * note that this can be called multiple times per a device, when it
905  * includes multiple audio control interfaces.
906  *
907  * thus we check the usb device pointer and creates the card instance
908  * only at the first time.  the successive calls of this function will
909  * append the pcm interface to the corresponding card.
910  */
911 static int usb_audio_probe(struct usb_interface *intf,
912 			   const struct usb_device_id *usb_id)
913 {
914 	struct usb_device *dev = interface_to_usbdev(intf);
915 	const struct snd_usb_audio_quirk *quirk =
916 		(const struct snd_usb_audio_quirk *)usb_id->driver_info;
917 	struct snd_usb_audio *chip;
918 	int i, err;
919 	struct usb_host_interface *alts;
920 	int ifnum;
921 	u32 id;
922 
923 	alts = &intf->altsetting[0];
924 	ifnum = get_iface_desc(alts)->bInterfaceNumber;
925 	id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
926 		    le16_to_cpu(dev->descriptor.idProduct));
927 	if (get_alias_id(dev, &id))
928 		quirk = get_alias_quirk(dev, id);
929 	if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
930 		return -ENXIO;
931 	if (quirk && quirk->ifnum == QUIRK_NODEV_INTERFACE)
932 		return -ENODEV;
933 
934 	err = snd_usb_apply_boot_quirk(dev, intf, quirk, id);
935 	if (err < 0)
936 		return err;
937 
938 	/*
939 	 * found a config.  now register to ALSA
940 	 */
941 
942 	/* check whether it's already registered */
943 	chip = NULL;
944 	guard(mutex)(&register_mutex);
945 	for (i = 0; i < SNDRV_CARDS; i++) {
946 		if (usb_chip[i] && usb_chip[i]->dev == dev) {
947 			if (atomic_read(&usb_chip[i]->shutdown)) {
948 				dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n");
949 				err = -EIO;
950 				goto __error;
951 			}
952 			chip = usb_chip[i];
953 			atomic_inc(&chip->active); /* avoid autopm */
954 			break;
955 		}
956 	}
957 	if (! chip) {
958 		err = snd_usb_apply_boot_quirk_once(dev, intf, quirk, id);
959 		if (err < 0)
960 			goto __error;
961 
962 		/* it's a fresh one.
963 		 * now look for an empty slot and create a new card instance
964 		 */
965 		for (i = 0; i < SNDRV_CARDS; i++)
966 			if (!usb_chip[i] &&
967 			    (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
968 			    (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
969 				if (enable[i]) {
970 					err = snd_usb_audio_create(intf, dev, i, quirk,
971 								   id, &chip);
972 					if (err < 0)
973 						goto __error;
974 					break;
975 				} else if (vid[i] != -1 || pid[i] != -1) {
976 					dev_info(&dev->dev,
977 						 "device (%04x:%04x) is disabled\n",
978 						 USB_ID_VENDOR(id),
979 						 USB_ID_PRODUCT(id));
980 					err = -ENOENT;
981 					goto __error;
982 				}
983 			}
984 		if (!chip) {
985 			dev_err(&dev->dev, "no available usb audio device\n");
986 			err = -ENODEV;
987 			goto __error;
988 		}
989 		find_last_interface(chip);
990 	}
991 
992 	if (chip->num_interfaces >= MAX_CARD_INTERFACES) {
993 		dev_info(&dev->dev, "Too many interfaces assigned to the single USB-audio card\n");
994 		err = -EINVAL;
995 		goto __error;
996 	}
997 
998 	dev_set_drvdata(&dev->dev, chip);
999 
1000 	if (ignore_ctl_error)
1001 		chip->quirk_flags |= QUIRK_FLAG_IGNORE_CTL_ERROR;
1002 
1003 	if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND)
1004 		usb_disable_autosuspend(interface_to_usbdev(intf));
1005 
1006 	/*
1007 	 * For devices with more than one control interface, we assume the
1008 	 * first contains the audio controls. We might need a more specific
1009 	 * check here in the future.
1010 	 */
1011 	if (!chip->ctrl_intf)
1012 		chip->ctrl_intf = alts;
1013 
1014 	err = 1; /* continue */
1015 	if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
1016 		/* need some special handlings */
1017 		err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk);
1018 		if (err < 0)
1019 			goto __error;
1020 	}
1021 
1022 	if (err > 0) {
1023 		/* create normal USB audio interfaces */
1024 		err = snd_usb_create_streams(chip, ifnum);
1025 		if (err < 0)
1026 			goto __error;
1027 		err = snd_usb_create_mixer(chip, ifnum);
1028 		if (err < 0)
1029 			goto __error;
1030 	}
1031 
1032 	if (chip->need_delayed_register) {
1033 		dev_info(&dev->dev,
1034 			 "Found post-registration device assignment: %08x:%02x\n",
1035 			 chip->usb_id, ifnum);
1036 		chip->need_delayed_register = false; /* clear again */
1037 	}
1038 
1039 	err = try_to_register_card(chip, ifnum);
1040 	if (err < 0)
1041 		goto __error_no_register;
1042 
1043 	if (chip->quirk_flags & QUIRK_FLAG_SHARE_MEDIA_DEVICE) {
1044 		/* don't want to fail when snd_media_device_create() fails */
1045 		snd_media_device_create(chip, intf);
1046 	}
1047 
1048 	if (quirk)
1049 		chip->quirk_type = quirk->type;
1050 
1051 	usb_chip[chip->index] = chip;
1052 	chip->intf[chip->num_interfaces] = intf;
1053 	chip->num_interfaces++;
1054 	usb_set_intfdata(intf, chip);
1055 	atomic_dec(&chip->active);
1056 
1057 	if (platform_ops && platform_ops->connect_cb)
1058 		platform_ops->connect_cb(chip);
1059 
1060 	return 0;
1061 
1062  __error:
1063 	/* in the case of error in secondary interface, still try to register */
1064 	if (chip)
1065 		try_to_register_card(chip, ifnum);
1066 
1067  __error_no_register:
1068 	if (chip) {
1069 		/* chip->active is inside the chip->card object,
1070 		 * decrement before memory is possibly returned.
1071 		 */
1072 		atomic_dec(&chip->active);
1073 		if (!chip->num_interfaces)
1074 			snd_card_free(chip->card);
1075 	}
1076 	return err;
1077 }
1078 
1079 /*
1080  * we need to take care of counter, since disconnection can be called also
1081  * many times as well as usb_audio_probe().
1082  */
1083 static bool __usb_audio_disconnect(struct usb_interface *intf,
1084 				   struct snd_usb_audio *chip,
1085 				   struct snd_card *card)
1086 {
1087 	struct list_head *p;
1088 
1089 	guard(mutex)(&register_mutex);
1090 
1091 	if (platform_ops && platform_ops->disconnect_cb)
1092 		platform_ops->disconnect_cb(chip);
1093 
1094 	if (atomic_inc_return(&chip->shutdown) == 1) {
1095 		struct snd_usb_stream *as;
1096 		struct snd_usb_endpoint *ep;
1097 		struct usb_mixer_interface *mixer;
1098 
1099 		/* wait until all pending tasks done;
1100 		 * they are protected by snd_usb_lock_shutdown()
1101 		 */
1102 		wait_event(chip->shutdown_wait,
1103 			   !atomic_read(&chip->usage_count));
1104 		snd_card_disconnect(card);
1105 		/* release the pcm resources */
1106 		list_for_each_entry(as, &chip->pcm_list, list) {
1107 			snd_usb_stream_disconnect(as);
1108 		}
1109 		/* release the endpoint resources */
1110 		list_for_each_entry(ep, &chip->ep_list, list) {
1111 			snd_usb_endpoint_release(ep);
1112 		}
1113 		/* release the midi resources */
1114 		list_for_each(p, &chip->midi_list) {
1115 			snd_usbmidi_disconnect(p);
1116 		}
1117 		snd_usb_midi_v2_disconnect_all(chip);
1118 		/*
1119 		 * Nice to check quirk && quirk->shares_media_device and
1120 		 * then call the snd_media_device_delete(). Don't have
1121 		 * access to the quirk here. snd_media_device_delete()
1122 		 * accesses mixer_list
1123 		 */
1124 		snd_media_device_delete(chip);
1125 
1126 		/* release mixer resources */
1127 		list_for_each_entry(mixer, &chip->mixer_list, list) {
1128 			snd_usb_mixer_disconnect(mixer);
1129 		}
1130 	}
1131 
1132 	if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND)
1133 		usb_enable_autosuspend(interface_to_usbdev(intf));
1134 
1135 	chip->num_interfaces--;
1136 	if (chip->num_interfaces > 0)
1137 		return false;
1138 
1139 	usb_chip[chip->index] = NULL;
1140 	return true;
1141 }
1142 
1143 static void usb_audio_disconnect(struct usb_interface *intf)
1144 {
1145 	struct snd_usb_audio *chip = usb_get_intfdata(intf);
1146 	struct snd_card *card;
1147 
1148 	if (chip == USB_AUDIO_IFACE_UNUSED)
1149 		return;
1150 
1151 	card = chip->card;
1152 	if (__usb_audio_disconnect(intf, chip, card))
1153 		snd_card_free_when_closed(card);
1154 }
1155 
1156 /* lock the shutdown (disconnect) task and autoresume */
1157 int snd_usb_lock_shutdown(struct snd_usb_audio *chip)
1158 {
1159 	int err;
1160 
1161 	atomic_inc(&chip->usage_count);
1162 	if (atomic_read(&chip->shutdown)) {
1163 		err = -EIO;
1164 		goto error;
1165 	}
1166 	err = snd_usb_autoresume(chip);
1167 	if (err < 0)
1168 		goto error;
1169 	return 0;
1170 
1171  error:
1172 	if (atomic_dec_and_test(&chip->usage_count))
1173 		wake_up(&chip->shutdown_wait);
1174 	return err;
1175 }
1176 EXPORT_SYMBOL_GPL(snd_usb_lock_shutdown);
1177 
1178 /* autosuspend and unlock the shutdown */
1179 void snd_usb_unlock_shutdown(struct snd_usb_audio *chip)
1180 {
1181 	snd_usb_autosuspend(chip);
1182 	if (atomic_dec_and_test(&chip->usage_count))
1183 		wake_up(&chip->shutdown_wait);
1184 }
1185 EXPORT_SYMBOL_GPL(snd_usb_unlock_shutdown);
1186 
1187 int snd_usb_autoresume(struct snd_usb_audio *chip)
1188 {
1189 	int i, err;
1190 
1191 	if (atomic_read(&chip->shutdown))
1192 		return -EIO;
1193 	if (atomic_inc_return(&chip->active) != 1)
1194 		return 0;
1195 
1196 	for (i = 0; i < chip->num_interfaces; i++) {
1197 		err = usb_autopm_get_interface(chip->intf[i]);
1198 		if (err < 0) {
1199 			/* rollback */
1200 			while (--i >= 0)
1201 				usb_autopm_put_interface(chip->intf[i]);
1202 			atomic_dec(&chip->active);
1203 			return err;
1204 		}
1205 	}
1206 	return 0;
1207 }
1208 EXPORT_SYMBOL_GPL(snd_usb_autoresume);
1209 
1210 void snd_usb_autosuspend(struct snd_usb_audio *chip)
1211 {
1212 	int i;
1213 
1214 	if (atomic_read(&chip->shutdown))
1215 		return;
1216 	if (!atomic_dec_and_test(&chip->active))
1217 		return;
1218 
1219 	for (i = 0; i < chip->num_interfaces; i++)
1220 		usb_autopm_put_interface(chip->intf[i]);
1221 }
1222 EXPORT_SYMBOL_GPL(snd_usb_autosuspend);
1223 
1224 static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
1225 {
1226 	struct snd_usb_audio *chip = usb_get_intfdata(intf);
1227 	struct snd_usb_stream *as;
1228 	struct snd_usb_endpoint *ep;
1229 	struct usb_mixer_interface *mixer;
1230 	struct list_head *p;
1231 
1232 	if (chip == USB_AUDIO_IFACE_UNUSED)
1233 		return 0;
1234 
1235 	if (!chip->num_suspended_intf++) {
1236 		list_for_each_entry(as, &chip->pcm_list, list)
1237 			snd_usb_pcm_suspend(as);
1238 		list_for_each_entry(ep, &chip->ep_list, list)
1239 			snd_usb_endpoint_suspend(ep);
1240 		list_for_each(p, &chip->midi_list)
1241 			snd_usbmidi_suspend(p);
1242 		list_for_each_entry(mixer, &chip->mixer_list, list)
1243 			snd_usb_mixer_suspend(mixer);
1244 		snd_usb_midi_v2_suspend_all(chip);
1245 	}
1246 
1247 	if (!PMSG_IS_AUTO(message) && !chip->system_suspend) {
1248 		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1249 		chip->system_suspend = chip->num_suspended_intf;
1250 	}
1251 
1252 	if (platform_ops && platform_ops->suspend_cb)
1253 		platform_ops->suspend_cb(intf, message);
1254 
1255 	return 0;
1256 }
1257 
1258 static int usb_audio_resume(struct usb_interface *intf)
1259 {
1260 	struct snd_usb_audio *chip = usb_get_intfdata(intf);
1261 	struct snd_usb_stream *as;
1262 	struct usb_mixer_interface *mixer;
1263 	struct list_head *p;
1264 	int err = 0;
1265 
1266 	if (chip == USB_AUDIO_IFACE_UNUSED)
1267 		return 0;
1268 
1269 	atomic_inc(&chip->active); /* avoid autopm */
1270 	if (chip->num_suspended_intf > 1)
1271 		goto out;
1272 
1273 	list_for_each_entry(as, &chip->pcm_list, list) {
1274 		err = snd_usb_pcm_resume(as);
1275 		if (err < 0)
1276 			goto err_out;
1277 	}
1278 
1279 	/*
1280 	 * ALSA leaves material resumption to user space
1281 	 * we just notify and restart the mixers
1282 	 */
1283 	list_for_each_entry(mixer, &chip->mixer_list, list) {
1284 		err = snd_usb_mixer_resume(mixer);
1285 		if (err < 0)
1286 			goto err_out;
1287 	}
1288 
1289 	list_for_each(p, &chip->midi_list) {
1290 		snd_usbmidi_resume(p);
1291 	}
1292 
1293 	snd_usb_midi_v2_resume_all(chip);
1294 
1295 	if (platform_ops && platform_ops->resume_cb)
1296 		platform_ops->resume_cb(intf);
1297 
1298  out:
1299 	if (chip->num_suspended_intf == chip->system_suspend) {
1300 		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1301 		chip->system_suspend = 0;
1302 	}
1303 	chip->num_suspended_intf--;
1304 
1305 err_out:
1306 	atomic_dec(&chip->active); /* allow autopm after this point */
1307 	return err;
1308 }
1309 
1310 static const struct usb_device_id usb_audio_ids [] = {
1311 #include "quirks-table.h"
1312     { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
1313       .bInterfaceClass = USB_CLASS_AUDIO,
1314       .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
1315     { }						/* Terminating entry */
1316 };
1317 MODULE_DEVICE_TABLE(usb, usb_audio_ids);
1318 
1319 /*
1320  * entry point for linux usb interface
1321  */
1322 
1323 static struct usb_driver usb_audio_driver = {
1324 	.name =		"snd-usb-audio",
1325 	.probe =	usb_audio_probe,
1326 	.disconnect =	usb_audio_disconnect,
1327 	.suspend =	usb_audio_suspend,
1328 	.resume =	usb_audio_resume,
1329 	.reset_resume =	usb_audio_resume,
1330 	.id_table =	usb_audio_ids,
1331 	.supports_autosuspend = 1,
1332 };
1333 
1334 module_usb_driver(usb_audio_driver);
1335