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