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