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