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