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