xref: /linux/sound/usb/card.c (revision c0c9379f235df33a12ceae94370ad80c5278324d)
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 		sprintf(card->shortname, "USB Device %#04x:%#04x",
620 			USB_ID_VENDOR(chip->usb_id),
621 			USB_ID_PRODUCT(chip->usb_id));
622 	}
623 
624 	strim(card->shortname);
625 }
626 
usb_audio_make_longname(struct usb_device * dev,struct snd_usb_audio * chip,const struct snd_usb_audio_quirk * quirk)627 static void usb_audio_make_longname(struct usb_device *dev,
628 				    struct snd_usb_audio *chip,
629 				    const struct snd_usb_audio_quirk *quirk)
630 {
631 	struct snd_card *card = chip->card;
632 	const struct usb_audio_device_name *preset;
633 	const char *s = NULL;
634 	int len;
635 
636 	preset = lookup_device_name(chip->usb_id);
637 
638 	/* shortcut - if any pre-defined string is given, use it */
639 	if (preset && preset->profile_name)
640 		s = preset->profile_name;
641 	if (s && *s) {
642 		strscpy(card->longname, s, sizeof(card->longname));
643 		return;
644 	}
645 
646 	if (preset && preset->vendor_name)
647 		s = preset->vendor_name;
648 	else if (quirk && quirk->vendor_name)
649 		s = quirk->vendor_name;
650 	*card->longname = 0;
651 	if (s && *s) {
652 		strscpy(card->longname, s, sizeof(card->longname));
653 	} else {
654 		/* retrieve the vendor and device strings as longname */
655 		if (dev->descriptor.iManufacturer)
656 			usb_string(dev, dev->descriptor.iManufacturer,
657 				   card->longname, sizeof(card->longname));
658 		/* we don't really care if there isn't any vendor string */
659 	}
660 	if (*card->longname) {
661 		strim(card->longname);
662 		if (*card->longname)
663 			strlcat(card->longname, " ", sizeof(card->longname));
664 	}
665 
666 	strlcat(card->longname, card->shortname, sizeof(card->longname));
667 
668 	len = strlcat(card->longname, " at ", sizeof(card->longname));
669 
670 	if (len < sizeof(card->longname))
671 		usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
672 
673 	switch (snd_usb_get_speed(dev)) {
674 	case USB_SPEED_LOW:
675 		strlcat(card->longname, ", low speed", sizeof(card->longname));
676 		break;
677 	case USB_SPEED_FULL:
678 		strlcat(card->longname, ", full speed", sizeof(card->longname));
679 		break;
680 	case USB_SPEED_HIGH:
681 		strlcat(card->longname, ", high speed", sizeof(card->longname));
682 		break;
683 	case USB_SPEED_SUPER:
684 		strlcat(card->longname, ", super speed", sizeof(card->longname));
685 		break;
686 	case USB_SPEED_SUPER_PLUS:
687 		strlcat(card->longname, ", super speed plus", sizeof(card->longname));
688 		break;
689 	default:
690 		break;
691 	}
692 }
693 
694 /*
695  * create a chip instance and set its names.
696  */
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)697 static int snd_usb_audio_create(struct usb_interface *intf,
698 				struct usb_device *dev, int idx,
699 				const struct snd_usb_audio_quirk *quirk,
700 				unsigned int usb_id,
701 				struct snd_usb_audio **rchip)
702 {
703 	struct snd_card *card;
704 	struct snd_usb_audio *chip;
705 	int err;
706 	char component[14];
707 
708 	*rchip = NULL;
709 
710 	switch (snd_usb_get_speed(dev)) {
711 	case USB_SPEED_LOW:
712 	case USB_SPEED_FULL:
713 	case USB_SPEED_HIGH:
714 	case USB_SPEED_SUPER:
715 	case USB_SPEED_SUPER_PLUS:
716 		break;
717 	default:
718 		dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev));
719 		return -ENXIO;
720 	}
721 
722 	err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE,
723 			   sizeof(*chip), &card);
724 	if (err < 0) {
725 		dev_err(&dev->dev, "cannot create card instance %d\n", idx);
726 		return err;
727 	}
728 
729 	chip = card->private_data;
730 	mutex_init(&chip->mutex);
731 	init_waitqueue_head(&chip->shutdown_wait);
732 	chip->index = idx;
733 	chip->dev = dev;
734 	chip->card = card;
735 	chip->setup = device_setup[idx];
736 	chip->generic_implicit_fb = implicit_fb[idx];
737 	chip->autoclock = autoclock;
738 	chip->lowlatency = lowlatency;
739 	atomic_set(&chip->active, 1); /* avoid autopm during probing */
740 	atomic_set(&chip->usage_count, 0);
741 	atomic_set(&chip->shutdown, 0);
742 
743 	chip->usb_id = usb_id;
744 	INIT_LIST_HEAD(&chip->pcm_list);
745 	INIT_LIST_HEAD(&chip->ep_list);
746 	INIT_LIST_HEAD(&chip->iface_ref_list);
747 	INIT_LIST_HEAD(&chip->clock_ref_list);
748 	INIT_LIST_HEAD(&chip->midi_list);
749 	INIT_LIST_HEAD(&chip->midi_v2_list);
750 	INIT_LIST_HEAD(&chip->mixer_list);
751 
752 	if (quirk_flags[idx])
753 		chip->quirk_flags = quirk_flags[idx];
754 	else
755 		snd_usb_init_quirk_flags(chip);
756 
757 	card->private_free = snd_usb_audio_free;
758 
759 	strcpy(card->driver, "USB-Audio");
760 	sprintf(component, "USB%04x:%04x",
761 		USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
762 	snd_component_add(card, component);
763 
764 	usb_audio_make_shortname(dev, chip, quirk);
765 	usb_audio_make_longname(dev, chip, quirk);
766 
767 	snd_usb_audio_create_proc(chip);
768 
769 	*rchip = chip;
770 	return 0;
771 }
772 
773 /* look for a matching quirk alias id */
get_alias_id(struct usb_device * dev,unsigned int * id)774 static bool get_alias_id(struct usb_device *dev, unsigned int *id)
775 {
776 	int i;
777 	unsigned int src, dst;
778 
779 	for (i = 0; i < ARRAY_SIZE(quirk_alias); i++) {
780 		if (!quirk_alias[i] ||
781 		    sscanf(quirk_alias[i], "%x:%x", &src, &dst) != 2 ||
782 		    src != *id)
783 			continue;
784 		dev_info(&dev->dev,
785 			 "device (%04x:%04x): applying quirk alias %04x:%04x\n",
786 			 USB_ID_VENDOR(*id), USB_ID_PRODUCT(*id),
787 			 USB_ID_VENDOR(dst), USB_ID_PRODUCT(dst));
788 		*id = dst;
789 		return true;
790 	}
791 
792 	return false;
793 }
794 
check_delayed_register_option(struct snd_usb_audio * chip)795 static int check_delayed_register_option(struct snd_usb_audio *chip)
796 {
797 	int i;
798 	unsigned int id, inum;
799 
800 	for (i = 0; i < ARRAY_SIZE(delayed_register); i++) {
801 		if (delayed_register[i] &&
802 		    sscanf(delayed_register[i], "%x:%x", &id, &inum) == 2 &&
803 		    id == chip->usb_id)
804 			return inum;
805 	}
806 
807 	return -1;
808 }
809 
810 static const struct usb_device_id usb_audio_ids[]; /* defined below */
811 
812 /* look for the last interface that matches with our ids and remember it */
find_last_interface(struct snd_usb_audio * chip)813 static void find_last_interface(struct snd_usb_audio *chip)
814 {
815 	struct usb_host_config *config = chip->dev->actconfig;
816 	struct usb_interface *intf;
817 	int i;
818 
819 	if (!config)
820 		return;
821 	for (i = 0; i < config->desc.bNumInterfaces; i++) {
822 		intf = config->interface[i];
823 		if (usb_match_id(intf, usb_audio_ids))
824 			chip->last_iface = intf->altsetting[0].desc.bInterfaceNumber;
825 	}
826 	usb_audio_dbg(chip, "Found last interface = %d\n", chip->last_iface);
827 }
828 
829 /* look for the corresponding quirk */
830 static const struct snd_usb_audio_quirk *
get_alias_quirk(struct usb_device * dev,unsigned int id)831 get_alias_quirk(struct usb_device *dev, unsigned int id)
832 {
833 	const struct usb_device_id *p;
834 
835 	for (p = usb_audio_ids; p->match_flags; p++) {
836 		/* FIXME: this checks only vendor:product pair in the list */
837 		if ((p->match_flags & USB_DEVICE_ID_MATCH_DEVICE) ==
838 		    USB_DEVICE_ID_MATCH_DEVICE &&
839 		    p->idVendor == USB_ID_VENDOR(id) &&
840 		    p->idProduct == USB_ID_PRODUCT(id))
841 			return (const struct snd_usb_audio_quirk *)p->driver_info;
842 	}
843 
844 	return NULL;
845 }
846 
847 /* register card if we reach to the last interface or to the specified
848  * one given via option
849  */
try_to_register_card(struct snd_usb_audio * chip,int ifnum)850 static int try_to_register_card(struct snd_usb_audio *chip, int ifnum)
851 {
852 	if (check_delayed_register_option(chip) == ifnum ||
853 	    chip->last_iface == ifnum ||
854 	    usb_interface_claimed(usb_ifnum_to_if(chip->dev, chip->last_iface)))
855 		return snd_card_register(chip->card);
856 	return 0;
857 }
858 
859 /*
860  * probe the active usb device
861  *
862  * note that this can be called multiple times per a device, when it
863  * includes multiple audio control interfaces.
864  *
865  * thus we check the usb device pointer and creates the card instance
866  * only at the first time.  the successive calls of this function will
867  * append the pcm interface to the corresponding card.
868  */
usb_audio_probe(struct usb_interface * intf,const struct usb_device_id * usb_id)869 static int usb_audio_probe(struct usb_interface *intf,
870 			   const struct usb_device_id *usb_id)
871 {
872 	struct usb_device *dev = interface_to_usbdev(intf);
873 	const struct snd_usb_audio_quirk *quirk =
874 		(const struct snd_usb_audio_quirk *)usb_id->driver_info;
875 	struct snd_usb_audio *chip;
876 	int i, err;
877 	struct usb_host_interface *alts;
878 	int ifnum;
879 	u32 id;
880 
881 	alts = &intf->altsetting[0];
882 	ifnum = get_iface_desc(alts)->bInterfaceNumber;
883 	id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
884 		    le16_to_cpu(dev->descriptor.idProduct));
885 	if (get_alias_id(dev, &id))
886 		quirk = get_alias_quirk(dev, id);
887 	if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
888 		return -ENXIO;
889 	if (quirk && quirk->ifnum == QUIRK_NODEV_INTERFACE)
890 		return -ENODEV;
891 
892 	err = snd_usb_apply_boot_quirk(dev, intf, quirk, id);
893 	if (err < 0)
894 		return err;
895 
896 	/*
897 	 * found a config.  now register to ALSA
898 	 */
899 
900 	/* check whether it's already registered */
901 	chip = NULL;
902 	mutex_lock(&register_mutex);
903 	for (i = 0; i < SNDRV_CARDS; i++) {
904 		if (usb_chip[i] && usb_chip[i]->dev == dev) {
905 			if (atomic_read(&usb_chip[i]->shutdown)) {
906 				dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n");
907 				err = -EIO;
908 				goto __error;
909 			}
910 			chip = usb_chip[i];
911 			atomic_inc(&chip->active); /* avoid autopm */
912 			break;
913 		}
914 	}
915 	if (! chip) {
916 		err = snd_usb_apply_boot_quirk_once(dev, intf, quirk, id);
917 		if (err < 0)
918 			goto __error;
919 
920 		/* it's a fresh one.
921 		 * now look for an empty slot and create a new card instance
922 		 */
923 		for (i = 0; i < SNDRV_CARDS; i++)
924 			if (!usb_chip[i] &&
925 			    (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
926 			    (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
927 				if (enable[i]) {
928 					err = snd_usb_audio_create(intf, dev, i, quirk,
929 								   id, &chip);
930 					if (err < 0)
931 						goto __error;
932 					break;
933 				} else if (vid[i] != -1 || pid[i] != -1) {
934 					dev_info(&dev->dev,
935 						 "device (%04x:%04x) is disabled\n",
936 						 USB_ID_VENDOR(id),
937 						 USB_ID_PRODUCT(id));
938 					err = -ENOENT;
939 					goto __error;
940 				}
941 			}
942 		if (!chip) {
943 			dev_err(&dev->dev, "no available usb audio device\n");
944 			err = -ENODEV;
945 			goto __error;
946 		}
947 		find_last_interface(chip);
948 	}
949 
950 	if (chip->num_interfaces >= MAX_CARD_INTERFACES) {
951 		dev_info(&dev->dev, "Too many interfaces assigned to the single USB-audio card\n");
952 		err = -EINVAL;
953 		goto __error;
954 	}
955 
956 	dev_set_drvdata(&dev->dev, chip);
957 
958 	if (ignore_ctl_error)
959 		chip->quirk_flags |= QUIRK_FLAG_IGNORE_CTL_ERROR;
960 
961 	if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND)
962 		usb_disable_autosuspend(interface_to_usbdev(intf));
963 
964 	/*
965 	 * For devices with more than one control interface, we assume the
966 	 * first contains the audio controls. We might need a more specific
967 	 * check here in the future.
968 	 */
969 	if (!chip->ctrl_intf)
970 		chip->ctrl_intf = alts;
971 
972 	err = 1; /* continue */
973 	if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
974 		/* need some special handlings */
975 		err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk);
976 		if (err < 0)
977 			goto __error;
978 	}
979 
980 	if (err > 0) {
981 		/* create normal USB audio interfaces */
982 		err = snd_usb_create_streams(chip, ifnum);
983 		if (err < 0)
984 			goto __error;
985 		err = snd_usb_create_mixer(chip, ifnum);
986 		if (err < 0)
987 			goto __error;
988 	}
989 
990 	if (chip->need_delayed_register) {
991 		dev_info(&dev->dev,
992 			 "Found post-registration device assignment: %08x:%02x\n",
993 			 chip->usb_id, ifnum);
994 		chip->need_delayed_register = false; /* clear again */
995 	}
996 
997 	err = try_to_register_card(chip, ifnum);
998 	if (err < 0)
999 		goto __error_no_register;
1000 
1001 	if (chip->quirk_flags & QUIRK_FLAG_SHARE_MEDIA_DEVICE) {
1002 		/* don't want to fail when snd_media_device_create() fails */
1003 		snd_media_device_create(chip, intf);
1004 	}
1005 
1006 	if (quirk)
1007 		chip->quirk_type = quirk->type;
1008 
1009 	usb_chip[chip->index] = chip;
1010 	chip->intf[chip->num_interfaces] = intf;
1011 	chip->num_interfaces++;
1012 	usb_set_intfdata(intf, chip);
1013 	atomic_dec(&chip->active);
1014 
1015 	if (platform_ops && platform_ops->connect_cb)
1016 		platform_ops->connect_cb(chip);
1017 	mutex_unlock(&register_mutex);
1018 
1019 	return 0;
1020 
1021  __error:
1022 	/* in the case of error in secondary interface, still try to register */
1023 	if (chip)
1024 		try_to_register_card(chip, ifnum);
1025 
1026  __error_no_register:
1027 	if (chip) {
1028 		/* chip->active is inside the chip->card object,
1029 		 * decrement before memory is possibly returned.
1030 		 */
1031 		atomic_dec(&chip->active);
1032 		if (!chip->num_interfaces)
1033 			snd_card_free(chip->card);
1034 	}
1035 	mutex_unlock(&register_mutex);
1036 	return err;
1037 }
1038 
1039 /*
1040  * we need to take care of counter, since disconnection can be called also
1041  * many times as well as usb_audio_probe().
1042  */
usb_audio_disconnect(struct usb_interface * intf)1043 static void usb_audio_disconnect(struct usb_interface *intf)
1044 {
1045 	struct snd_usb_audio *chip = usb_get_intfdata(intf);
1046 	struct snd_card *card;
1047 	struct list_head *p;
1048 
1049 	if (chip == USB_AUDIO_IFACE_UNUSED)
1050 		return;
1051 
1052 	card = chip->card;
1053 
1054 	mutex_lock(&register_mutex);
1055 	if (platform_ops && platform_ops->disconnect_cb)
1056 		platform_ops->disconnect_cb(chip);
1057 
1058 	if (atomic_inc_return(&chip->shutdown) == 1) {
1059 		struct snd_usb_stream *as;
1060 		struct snd_usb_endpoint *ep;
1061 		struct usb_mixer_interface *mixer;
1062 
1063 		/* wait until all pending tasks done;
1064 		 * they are protected by snd_usb_lock_shutdown()
1065 		 */
1066 		wait_event(chip->shutdown_wait,
1067 			   !atomic_read(&chip->usage_count));
1068 		snd_card_disconnect(card);
1069 		/* release the pcm resources */
1070 		list_for_each_entry(as, &chip->pcm_list, list) {
1071 			snd_usb_stream_disconnect(as);
1072 		}
1073 		/* release the endpoint resources */
1074 		list_for_each_entry(ep, &chip->ep_list, list) {
1075 			snd_usb_endpoint_release(ep);
1076 		}
1077 		/* release the midi resources */
1078 		list_for_each(p, &chip->midi_list) {
1079 			snd_usbmidi_disconnect(p);
1080 		}
1081 		snd_usb_midi_v2_disconnect_all(chip);
1082 		/*
1083 		 * Nice to check quirk && quirk->shares_media_device and
1084 		 * then call the snd_media_device_delete(). Don't have
1085 		 * access to the quirk here. snd_media_device_delete()
1086 		 * accesses mixer_list
1087 		 */
1088 		snd_media_device_delete(chip);
1089 
1090 		/* release mixer resources */
1091 		list_for_each_entry(mixer, &chip->mixer_list, list) {
1092 			snd_usb_mixer_disconnect(mixer);
1093 		}
1094 	}
1095 
1096 	if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND)
1097 		usb_enable_autosuspend(interface_to_usbdev(intf));
1098 
1099 	chip->num_interfaces--;
1100 	if (chip->num_interfaces <= 0) {
1101 		usb_chip[chip->index] = NULL;
1102 		mutex_unlock(&register_mutex);
1103 		snd_card_free_when_closed(card);
1104 	} else {
1105 		mutex_unlock(&register_mutex);
1106 	}
1107 }
1108 
1109 /* lock the shutdown (disconnect) task and autoresume */
snd_usb_lock_shutdown(struct snd_usb_audio * chip)1110 int snd_usb_lock_shutdown(struct snd_usb_audio *chip)
1111 {
1112 	int err;
1113 
1114 	atomic_inc(&chip->usage_count);
1115 	if (atomic_read(&chip->shutdown)) {
1116 		err = -EIO;
1117 		goto error;
1118 	}
1119 	err = snd_usb_autoresume(chip);
1120 	if (err < 0)
1121 		goto error;
1122 	return 0;
1123 
1124  error:
1125 	if (atomic_dec_and_test(&chip->usage_count))
1126 		wake_up(&chip->shutdown_wait);
1127 	return err;
1128 }
1129 EXPORT_SYMBOL_GPL(snd_usb_lock_shutdown);
1130 
1131 /* autosuspend and unlock the shutdown */
snd_usb_unlock_shutdown(struct snd_usb_audio * chip)1132 void snd_usb_unlock_shutdown(struct snd_usb_audio *chip)
1133 {
1134 	snd_usb_autosuspend(chip);
1135 	if (atomic_dec_and_test(&chip->usage_count))
1136 		wake_up(&chip->shutdown_wait);
1137 }
1138 EXPORT_SYMBOL_GPL(snd_usb_unlock_shutdown);
1139 
snd_usb_autoresume(struct snd_usb_audio * chip)1140 int snd_usb_autoresume(struct snd_usb_audio *chip)
1141 {
1142 	int i, err;
1143 
1144 	if (atomic_read(&chip->shutdown))
1145 		return -EIO;
1146 	if (atomic_inc_return(&chip->active) != 1)
1147 		return 0;
1148 
1149 	for (i = 0; i < chip->num_interfaces; i++) {
1150 		err = usb_autopm_get_interface(chip->intf[i]);
1151 		if (err < 0) {
1152 			/* rollback */
1153 			while (--i >= 0)
1154 				usb_autopm_put_interface(chip->intf[i]);
1155 			atomic_dec(&chip->active);
1156 			return err;
1157 		}
1158 	}
1159 	return 0;
1160 }
1161 EXPORT_SYMBOL_GPL(snd_usb_autoresume);
1162 
snd_usb_autosuspend(struct snd_usb_audio * chip)1163 void snd_usb_autosuspend(struct snd_usb_audio *chip)
1164 {
1165 	int i;
1166 
1167 	if (atomic_read(&chip->shutdown))
1168 		return;
1169 	if (!atomic_dec_and_test(&chip->active))
1170 		return;
1171 
1172 	for (i = 0; i < chip->num_interfaces; i++)
1173 		usb_autopm_put_interface(chip->intf[i]);
1174 }
1175 EXPORT_SYMBOL_GPL(snd_usb_autosuspend);
1176 
usb_audio_suspend(struct usb_interface * intf,pm_message_t message)1177 static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
1178 {
1179 	struct snd_usb_audio *chip = usb_get_intfdata(intf);
1180 	struct snd_usb_stream *as;
1181 	struct snd_usb_endpoint *ep;
1182 	struct usb_mixer_interface *mixer;
1183 	struct list_head *p;
1184 
1185 	if (chip == USB_AUDIO_IFACE_UNUSED)
1186 		return 0;
1187 
1188 	if (!chip->num_suspended_intf++) {
1189 		list_for_each_entry(as, &chip->pcm_list, list)
1190 			snd_usb_pcm_suspend(as);
1191 		list_for_each_entry(ep, &chip->ep_list, list)
1192 			snd_usb_endpoint_suspend(ep);
1193 		list_for_each(p, &chip->midi_list)
1194 			snd_usbmidi_suspend(p);
1195 		list_for_each_entry(mixer, &chip->mixer_list, list)
1196 			snd_usb_mixer_suspend(mixer);
1197 		snd_usb_midi_v2_suspend_all(chip);
1198 	}
1199 
1200 	if (!PMSG_IS_AUTO(message) && !chip->system_suspend) {
1201 		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1202 		chip->system_suspend = chip->num_suspended_intf;
1203 	}
1204 
1205 	if (platform_ops && platform_ops->suspend_cb)
1206 		platform_ops->suspend_cb(intf, message);
1207 
1208 	return 0;
1209 }
1210 
usb_audio_resume(struct usb_interface * intf)1211 static int usb_audio_resume(struct usb_interface *intf)
1212 {
1213 	struct snd_usb_audio *chip = usb_get_intfdata(intf);
1214 	struct snd_usb_stream *as;
1215 	struct usb_mixer_interface *mixer;
1216 	struct list_head *p;
1217 	int err = 0;
1218 
1219 	if (chip == USB_AUDIO_IFACE_UNUSED)
1220 		return 0;
1221 
1222 	atomic_inc(&chip->active); /* avoid autopm */
1223 	if (chip->num_suspended_intf > 1)
1224 		goto out;
1225 
1226 	list_for_each_entry(as, &chip->pcm_list, list) {
1227 		err = snd_usb_pcm_resume(as);
1228 		if (err < 0)
1229 			goto err_out;
1230 	}
1231 
1232 	/*
1233 	 * ALSA leaves material resumption to user space
1234 	 * we just notify and restart the mixers
1235 	 */
1236 	list_for_each_entry(mixer, &chip->mixer_list, list) {
1237 		err = snd_usb_mixer_resume(mixer);
1238 		if (err < 0)
1239 			goto err_out;
1240 	}
1241 
1242 	list_for_each(p, &chip->midi_list) {
1243 		snd_usbmidi_resume(p);
1244 	}
1245 
1246 	snd_usb_midi_v2_resume_all(chip);
1247 
1248 	if (platform_ops && platform_ops->resume_cb)
1249 		platform_ops->resume_cb(intf);
1250 
1251  out:
1252 	if (chip->num_suspended_intf == chip->system_suspend) {
1253 		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1254 		chip->system_suspend = 0;
1255 	}
1256 	chip->num_suspended_intf--;
1257 
1258 err_out:
1259 	atomic_dec(&chip->active); /* allow autopm after this point */
1260 	return err;
1261 }
1262 
1263 static const struct usb_device_id usb_audio_ids [] = {
1264 #include "quirks-table.h"
1265     { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
1266       .bInterfaceClass = USB_CLASS_AUDIO,
1267       .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
1268     { }						/* Terminating entry */
1269 };
1270 MODULE_DEVICE_TABLE(usb, usb_audio_ids);
1271 
1272 /*
1273  * entry point for linux usb interface
1274  */
1275 
1276 static struct usb_driver usb_audio_driver = {
1277 	.name =		"snd-usb-audio",
1278 	.probe =	usb_audio_probe,
1279 	.disconnect =	usb_audio_disconnect,
1280 	.suspend =	usb_audio_suspend,
1281 	.resume =	usb_audio_resume,
1282 	.reset_resume =	usb_audio_resume,
1283 	.id_table =	usb_audio_ids,
1284 	.supports_autosuspend = 1,
1285 };
1286 
1287 module_usb_driver(usb_audio_driver);
1288