card.c 29.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
Daniel Mack's avatar
Daniel Mack committed
2
3
4
5
6
7
8
9
10
/*
 *   (Tentative) USB Audio Driver for ALSA
 *
 *   Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
 *
 *   Many codes borrowed from audio.c by
 *	    Alan Cox (alan@lxorguk.ukuu.org.uk)
 *	    Thomas Sailer (sailer@ife.ee.ethz.ch)
 *
11
 *   Audio Class 3.0 support by Ruslan Bilovol <ruslan.bilovol@gmail.com>
Daniel Mack's avatar
Daniel Mack committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 *
 *  NOTES:
 *
 *   - the linked URBs would be preferred but not used so far because of
 *     the instability of unlinking.
 *   - type II is not supported properly.  there is no device which supports
 *     this type *correctly*.  SB extigy looks as if it supports, but it's
 *     indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
 */


#include <linux/bitops.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/string.h>
28
#include <linux/ctype.h>
Daniel Mack's avatar
Daniel Mack committed
29
30
31
32
#include <linux/usb.h>
#include <linux/moduleparam.h>
#include <linux/mutex.h>
#include <linux/usb/audio.h>
Daniel Mack's avatar
Daniel Mack committed
33
#include <linux/usb/audio-v2.h>
34
#include <linux/usb/audio-v3.h>
35
#include <linux/module.h>
Daniel Mack's avatar
Daniel Mack committed
36

37
#include <sound/control.h>
Daniel Mack's avatar
Daniel Mack committed
38
39
40
41
42
43
44
45
46
#include <sound/core.h>
#include <sound/info.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/initval.h>

#include "usbaudio.h"
#include "card.h"
#include "midi.h"
47
#include "mixer.h"
Daniel Mack's avatar
Daniel Mack committed
48
49
50
51
52
53
#include "proc.h"
#include "quirks.h"
#include "endpoint.h"
#include "helper.h"
#include "pcm.h"
#include "format.h"
54
#include "power.h"
Daniel Mack's avatar
Daniel Mack committed
55
#include "stream.h"
56
#include "media.h"
Daniel Mack's avatar
Daniel Mack committed
57
58
59
60
61
62
63
64
65

MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
MODULE_DESCRIPTION("USB Audio");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");


static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
66
static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
Daniel Mack's avatar
Daniel Mack committed
67
68
69
70
/* Vendor/product IDs for this card */
static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
71
static bool ignore_ctl_error;
72
static bool autoclock = true;
73
static char *quirk_alias[SNDRV_CARDS];
74
static char *delayed_register[SNDRV_CARDS];
Daniel Mack's avatar
Daniel Mack committed
75

76
bool snd_usb_use_vmalloc = true;
77
bool snd_usb_skip_validation;
78

Daniel Mack's avatar
Daniel Mack committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
module_param_array(vid, int, NULL, 0444);
MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
module_param_array(pid, int, NULL, 0444);
MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
module_param_array(device_setup, int, NULL, 0444);
MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
module_param(ignore_ctl_error, bool, 0444);
MODULE_PARM_DESC(ignore_ctl_error,
		 "Ignore errors from USB controller for mixer interfaces.");
94
95
module_param(autoclock, bool, 0444);
MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes).");
96
97
module_param_array(quirk_alias, charp, NULL, 0444);
MODULE_PARM_DESC(quirk_alias, "Quirk aliases, e.g. 0123abcd:5678beef.");
98
99
module_param_array(delayed_register, charp, NULL, 0444);
MODULE_PARM_DESC(delayed_register, "Quirk for delayed registration, given by id:iface, e.g. 0123abcd:4.");
100
101
module_param_named(use_vmalloc, snd_usb_use_vmalloc, bool, 0444);
MODULE_PARM_DESC(use_vmalloc, "Use vmalloc for PCM intermediate buffers (default: yes).");
102
103
module_param_named(skip_validation, snd_usb_skip_validation, bool, 0444);
MODULE_PARM_DESC(skip_validation, "Skip unit descriptor validation (default: no).");
Daniel Mack's avatar
Daniel Mack committed
104
105
106
107
108
109
110
111
112
113
114
115

/*
 * we keep the snd_usb_audio_t instances by ourselves for merging
 * the all interfaces on the same card as one sound device.
 */

static DEFINE_MUTEX(register_mutex);
static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
static struct usb_driver usb_audio_driver;

/*
 * disconnect streams
116
 * called from usb_audio_disconnect()
Daniel Mack's avatar
Daniel Mack committed
117
 */
118
static void snd_usb_stream_disconnect(struct snd_usb_stream *as)
Daniel Mack's avatar
Daniel Mack committed
119
120
121
122
123
124
125
{
	int idx;
	struct snd_usb_substream *subs;

	for (idx = 0; idx < 2; idx++) {
		subs = &as->substream[idx];
		if (!subs->num_formats)
126
			continue;
Daniel Mack's avatar
Daniel Mack committed
127
		subs->interface = -1;
128
129
		subs->data_endpoint = NULL;
		subs->sync_endpoint = NULL;
Daniel Mack's avatar
Daniel Mack committed
130
131
132
133
134
135
136
137
138
139
140
	}
}

static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
{
	struct usb_device *dev = chip->dev;
	struct usb_host_interface *alts;
	struct usb_interface_descriptor *altsd;
	struct usb_interface *iface = usb_ifnum_to_if(dev, interface);

	if (!iface) {
141
142
		dev_err(&dev->dev, "%u:%d : does not exist\n",
			ctrlif, interface);
Daniel Mack's avatar
Daniel Mack committed
143
144
145
		return -EINVAL;
	}

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
	alts = &iface->altsetting[0];
	altsd = get_iface_desc(alts);

	/*
	 * Android with both accessory and audio interfaces enabled gets the
	 * interface numbers wrong.
	 */
	if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) ||
	     chip->usb_id == USB_ID(0x18d1, 0x2d05)) &&
	    interface == 0 &&
	    altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
	    altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) {
		interface = 2;
		iface = usb_ifnum_to_if(dev, interface);
		if (!iface)
			return -EINVAL;
		alts = &iface->altsetting[0];
		altsd = get_iface_desc(alts);
	}

Daniel Mack's avatar
Daniel Mack committed
166
	if (usb_interface_claimed(iface)) {
167
168
		dev_dbg(&dev->dev, "%d:%d: skipping, already claimed\n",
			ctrlif, interface);
Daniel Mack's avatar
Daniel Mack committed
169
170
171
172
173
174
		return -EINVAL;
	}

	if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
	     altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
	    altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
175
176
177
		int err = __snd_usbmidi_create(chip->card, iface,
					     &chip->midi_list, NULL,
					     chip->usb_id);
Daniel Mack's avatar
Daniel Mack committed
178
		if (err < 0) {
179
180
181
			dev_err(&dev->dev,
				"%u:%d: cannot create sequencer device\n",
				ctrlif, interface);
Daniel Mack's avatar
Daniel Mack committed
182
183
184
185
186
187
188
189
190
191
			return -EINVAL;
		}
		usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);

		return 0;
	}

	if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
	     altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
	    altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
192
193
194
		dev_dbg(&dev->dev,
			"%u:%d: skipping non-supported interface %d\n",
			ctrlif, interface, altsd->bInterfaceClass);
Daniel Mack's avatar
Daniel Mack committed
195
196
197
198
199
		/* skip non-supported classes */
		return -EINVAL;
	}

	if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
200
		dev_err(&dev->dev, "low speed audio streaming not supported\n");
Daniel Mack's avatar
Daniel Mack committed
201
202
203
		return -EINVAL;
	}

Daniel Mack's avatar
Daniel Mack committed
204
	if (! snd_usb_parse_audio_interface(chip, interface)) {
Daniel Mack's avatar
Daniel Mack committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
		usb_set_interface(dev, interface, 0); /* reset the current interface */
		usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
	}

	return 0;
}

/*
 * parse audio control descriptor and create pcm/midi streams
 */
static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
{
	struct usb_device *dev = chip->dev;
	struct usb_host_interface *host_iface;
	struct usb_interface_descriptor *altsd;
	int i, protocol;

	/* find audiocontrol interface */
	host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
	altsd = get_iface_desc(host_iface);
	protocol = altsd->bInterfaceProtocol;

	switch (protocol) {
228
	default:
229
230
231
		dev_warn(&dev->dev,
			 "unknown interface protocol %#02x, assuming v1\n",
			 protocol);
232
		fallthrough;
233

Daniel Mack's avatar
Daniel Mack committed
234
	case UAC_VERSION_1: {
235
236
237
238
239
240
		struct uac1_ac_header_descriptor *h1;
		int rest_bytes;

		h1 = snd_usb_find_csint_desc(host_iface->extra,
							 host_iface->extralen,
							 NULL, UAC_HEADER);
241
		if (!h1 || h1->bLength < sizeof(*h1)) {
242
243
244
245
246
247
248
249
250
251
252
253
			dev_err(&dev->dev, "cannot find UAC_HEADER\n");
			return -EINVAL;
		}

		rest_bytes = (void *)(host_iface->extra +
				host_iface->extralen) - (void *)h1;

		/* just to be sure -- this shouldn't hit at all */
		if (rest_bytes <= 0) {
			dev_err(&dev->dev, "invalid control header\n");
			return -EINVAL;
		}
Daniel Mack's avatar
Daniel Mack committed
254

255
256
257
258
259
		if (rest_bytes < sizeof(*h1)) {
			dev_err(&dev->dev, "too short v1 buffer descriptor\n");
			return -EINVAL;
		}

Daniel Mack's avatar
Daniel Mack committed
260
		if (!h1->bInCollection) {
261
			dev_info(&dev->dev, "skipping empty audio interface (v1)\n");
Daniel Mack's avatar
Daniel Mack committed
262
263
264
			return -EINVAL;
		}

265
266
267
268
269
		if (rest_bytes < h1->bLength) {
			dev_err(&dev->dev, "invalid buffer length (v1)\n");
			return -EINVAL;
		}

Daniel Mack's avatar
Daniel Mack committed
270
		if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
271
			dev_err(&dev->dev, "invalid UAC_HEADER (v1)\n");
Daniel Mack's avatar
Daniel Mack committed
272
273
274
275
276
277
278
279
280
			return -EINVAL;
		}

		for (i = 0; i < h1->bInCollection; i++)
			snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);

		break;
	}

281
282
	case UAC_VERSION_2:
	case UAC_VERSION_3: {
Daniel Mack's avatar
Daniel Mack committed
283
284
285
		struct usb_interface_assoc_descriptor *assoc =
			usb_ifnum_to_if(dev, ctrlif)->intf_assoc;

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
		if (!assoc) {
			/*
			 * Firmware writers cannot count to three.  So to find
			 * the IAD on the NuForce UDH-100, also check the next
			 * interface.
			 */
			struct usb_interface *iface =
				usb_ifnum_to_if(dev, ctrlif + 1);
			if (iface &&
			    iface->intf_assoc &&
			    iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO &&
			    iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2)
				assoc = iface->intf_assoc;
		}

Daniel Mack's avatar
Daniel Mack committed
301
		if (!assoc) {
302
			dev_err(&dev->dev, "Audio class v2/v3 interfaces need an interface association\n");
Daniel Mack's avatar
Daniel Mack committed
303
304
305
			return -EINVAL;
		}

306
307
308
309
310
311
312
313
314
315
316
317
318
319
		if (protocol == UAC_VERSION_3) {
			int badd = assoc->bFunctionSubClass;

			if (badd != UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0 &&
			    (badd < UAC3_FUNCTION_SUBCLASS_GENERIC_IO ||
			     badd > UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE)) {
				dev_err(&dev->dev,
					"Unsupported UAC3 BADD profile\n");
				return -EINVAL;
			}

			chip->badd_profile = badd;
		}

Daniel Mack's avatar
Daniel Mack committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
		for (i = 0; i < assoc->bInterfaceCount; i++) {
			int intf = assoc->bFirstInterface + i;

			if (intf != ctrlif)
				snd_usb_create_stream(chip, ctrlif, intf);
		}

		break;
	}
	}

	return 0;
}

334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Profile name preset table
 */
struct usb_audio_device_name {
	u32 id;
	const char *vendor_name;
	const char *product_name;
	const char *profile_name;	/* override card->longname */
};

#define PROFILE_NAME(vid, pid, vendor, product, profile)	 \
	{ .id = USB_ID(vid, pid), .vendor_name = (vendor),	 \
	  .product_name = (product), .profile_name = (profile) }
#define DEVICE_NAME(vid, pid, vendor, product) \
	PROFILE_NAME(vid, pid, vendor, product, NULL)

/* vendor/product and profile name presets, sorted in device id order */
static const struct usb_audio_device_name usb_audio_names[] = {
	/* HP Thunderbolt Dock Audio Headset */
	PROFILE_NAME(0x03f0, 0x0269, "HP", "Thunderbolt Dock Audio Headset",
		     "HP-Thunderbolt-Dock-Audio-Headset"),
	/* HP Thunderbolt Dock Audio Module */
	PROFILE_NAME(0x03f0, 0x0567, "HP", "Thunderbolt Dock Audio Module",
		     "HP-Thunderbolt-Dock-Audio-Module"),

	/* Two entries for Gigabyte TRX40 Aorus Master:
	 * TRX40 Aorus Master has two USB-audio devices, one for the front
	 * headphone with ESS SABRE9218 DAC chip, while another for the rest
	 * I/O (the rear panel and the front mic) with Realtek ALC1220-VB.
	 * Here we provide two distinct names for making UCM profiles easier.
	 */
	PROFILE_NAME(0x0414, 0xa000, "Gigabyte", "Aorus Master Front Headphone",
		     "Gigabyte-Aorus-Master-Front-Headphone"),
	PROFILE_NAME(0x0414, 0xa001, "Gigabyte", "Aorus Master Main Audio",
		     "Gigabyte-Aorus-Master-Main-Audio"),

	/* Gigabyte TRX40 Aorus Pro WiFi */
	PROFILE_NAME(0x0414, 0xa002,
		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),

	/* Creative/E-Mu devices */
	DEVICE_NAME(0x041e, 0x3010, "Creative Labs", "Sound Blaster MP3+"),
	/* Creative/Toshiba Multimedia Center SB-0500 */
	DEVICE_NAME(0x041e, 0x3048, "Toshiba", "SB-0500"),

	DEVICE_NAME(0x046d, 0x0990, "Logitech, Inc.", "QuickCam Pro 9000"),

381
382
383
384
	/* ASUS ROG Strix */
	PROFILE_NAME(0x0b05, 0x1917,
		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
	/* Dell WD15 Dock */
	PROFILE_NAME(0x0bda, 0x4014, "Dell", "WD15 Dock", "Dell-WD15-Dock"),
	/* Dell WD19 Dock */
	PROFILE_NAME(0x0bda, 0x402e, "Dell", "WD19 Dock", "Dell-WD15-Dock"),

	DEVICE_NAME(0x0ccd, 0x0028, "TerraTec", "Aureon5.1MkII"),

	/*
	 * The original product_name is "USB Sound Device", however this name
	 * is also used by the CM106 based cards, so make it unique.
	 */
	DEVICE_NAME(0x0d8c, 0x0102, NULL, "ICUSBAUDIO7D"),
	DEVICE_NAME(0x0d8c, 0x0103, NULL, "Audio Advantage MicroII"),

	/* MSI TRX40 Creator */
	PROFILE_NAME(0x0db0, 0x0d64,
		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
	/* MSI TRX40 */
	PROFILE_NAME(0x0db0, 0x543d,
		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),

	/* Stanton/N2IT Final Scratch v1 device ('Scratchamp') */
	DEVICE_NAME(0x103d, 0x0100, "Stanton", "ScratchAmp"),
	DEVICE_NAME(0x103d, 0x0101, "Stanton", "ScratchAmp"),

	/* aka. Serato Scratch Live DJ Box */
	DEVICE_NAME(0x13e5, 0x0001, "Rane", "SL-1"),

	/* Lenovo ThinkStation P620 Rear Line-in, Line-out and Microphone */
	PROFILE_NAME(0x17aa, 0x1046, "Lenovo", "ThinkStation P620 Rear",
		     "Lenovo-ThinkStation-P620-Rear"),
	/* Lenovo ThinkStation P620 Internal Speaker + Front Headset */
	PROFILE_NAME(0x17aa, 0x104d, "Lenovo", "ThinkStation P620 Main",
		     "Lenovo-ThinkStation-P620-Main"),

	/* Asrock TRX40 Creator */
	PROFILE_NAME(0x26ce, 0x0a01,
		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),

	{ } /* terminator */
};

static const struct usb_audio_device_name *
lookup_device_name(u32 id)
{
	static const struct usb_audio_device_name *p;

	for (p = usb_audio_names; p->id; p++)
		if (p->id == id)
			return p;
	return NULL;
}

Daniel Mack's avatar
Daniel Mack committed
438
439
440
441
442
443
444
/*
 * free the chip instance
 *
 * here we have to do not much, since pcm and controls are already freed
 *
 */

445
static void snd_usb_audio_free(struct snd_card *card)
Daniel Mack's avatar
Daniel Mack committed
446
{
447
	struct snd_usb_audio *chip = card->private_data;
448
	struct snd_usb_endpoint *ep, *n;
449

450
451
	list_for_each_entry_safe(ep, n, &chip->ep_list, list)
		snd_usb_endpoint_free(ep);
452

453
	mutex_destroy(&chip->mutex);
454
455
	if (!atomic_read(&chip->shutdown))
		dev_set_drvdata(&chip->dev->dev, NULL);
Daniel Mack's avatar
Daniel Mack committed
456
457
}

458
459
460
461
462
static void usb_audio_make_shortname(struct usb_device *dev,
				     struct snd_usb_audio *chip,
				     const struct snd_usb_audio_quirk *quirk)
{
	struct snd_card *card = chip->card;
463
464
465
466
467
468
469
470
471
472
	const struct usb_audio_device_name *preset;
	const char *s = NULL;

	preset = lookup_device_name(chip->usb_id);
	if (preset && preset->product_name)
		s = preset->product_name;
	else if (quirk && quirk->product_name)
		s = quirk->product_name;
	if (s && *s) {
		strlcpy(card->shortname, s, sizeof(card->shortname));
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
		return;
	}

	/* retrieve the device string as shortname */
	if (!dev->descriptor.iProduct ||
	    usb_string(dev, dev->descriptor.iProduct,
		       card->shortname, sizeof(card->shortname)) <= 0) {
		/* no name available from anywhere, so use ID */
		sprintf(card->shortname, "USB Device %#04x:%#04x",
			USB_ID_VENDOR(chip->usb_id),
			USB_ID_PRODUCT(chip->usb_id));
	}

	strim(card->shortname);
}

static void usb_audio_make_longname(struct usb_device *dev,
				    struct snd_usb_audio *chip,
				    const struct snd_usb_audio_quirk *quirk)
{
	struct snd_card *card = chip->card;
494
495
	const struct usb_audio_device_name *preset;
	const char *s = NULL;
496
497
	int len;

498
499
	preset = lookup_device_name(chip->usb_id);

500
	/* shortcut - if any pre-defined string is given, use it */
501
502
503
504
	if (preset && preset->profile_name)
		s = preset->profile_name;
	if (s && *s) {
		strlcpy(card->longname, s, sizeof(card->longname));
505
506
507
		return;
	}

508
509
510
511
512
513
	if (preset && preset->vendor_name)
		s = preset->vendor_name;
	else if (quirk && quirk->vendor_name)
		s = quirk->vendor_name;
	if (s && *s) {
		len = strlcpy(card->longname, s, sizeof(card->longname));
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
	} else {
		/* retrieve the vendor and device strings as longname */
		if (dev->descriptor.iManufacturer)
			len = usb_string(dev, dev->descriptor.iManufacturer,
					 card->longname, sizeof(card->longname));
		else
			len = 0;
		/* we don't really care if there isn't any vendor string */
	}
	if (len > 0) {
		strim(card->longname);
		if (*card->longname)
			strlcat(card->longname, " ", sizeof(card->longname));
	}

	strlcat(card->longname, card->shortname, sizeof(card->longname));

	len = strlcat(card->longname, " at ", sizeof(card->longname));

	if (len < sizeof(card->longname))
		usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);

	switch (snd_usb_get_speed(dev)) {
	case USB_SPEED_LOW:
		strlcat(card->longname, ", low speed", sizeof(card->longname));
		break;
	case USB_SPEED_FULL:
		strlcat(card->longname, ", full speed", sizeof(card->longname));
		break;
	case USB_SPEED_HIGH:
		strlcat(card->longname, ", high speed", sizeof(card->longname));
		break;
	case USB_SPEED_SUPER:
		strlcat(card->longname, ", super speed", sizeof(card->longname));
		break;
	case USB_SPEED_SUPER_PLUS:
		strlcat(card->longname, ", super speed plus", sizeof(card->longname));
		break;
	default:
		break;
	}
}

Daniel Mack's avatar
Daniel Mack committed
557
558
559
/*
 * create a chip instance and set its names.
 */
560
561
static int snd_usb_audio_create(struct usb_interface *intf,
				struct usb_device *dev, int idx,
Daniel Mack's avatar
Daniel Mack committed
562
				const struct snd_usb_audio_quirk *quirk,
563
				unsigned int usb_id,
Daniel Mack's avatar
Daniel Mack committed
564
565
566
567
				struct snd_usb_audio **rchip)
{
	struct snd_card *card;
	struct snd_usb_audio *chip;
568
	int err;
Daniel Mack's avatar
Daniel Mack committed
569
570
571
572
	char component[14];

	*rchip = NULL;

573
574
575
576
	switch (snd_usb_get_speed(dev)) {
	case USB_SPEED_LOW:
	case USB_SPEED_FULL:
	case USB_SPEED_HIGH:
577
	case USB_SPEED_WIRELESS:
578
	case USB_SPEED_SUPER:
579
	case USB_SPEED_SUPER_PLUS:
580
581
		break;
	default:
582
		dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev));
Daniel Mack's avatar
Daniel Mack committed
583
584
585
		return -ENXIO;
	}

586
	err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE,
587
			   sizeof(*chip), &card);
Daniel Mack's avatar
Daniel Mack committed
588
	if (err < 0) {
589
		dev_err(&dev->dev, "cannot create card instance %d\n", idx);
Daniel Mack's avatar
Daniel Mack committed
590
591
592
		return err;
	}

593
	chip = card->private_data;
594
	mutex_init(&chip->mutex);
595
	init_waitqueue_head(&chip->shutdown_wait);
Daniel Mack's avatar
Daniel Mack committed
596
597
598
599
	chip->index = idx;
	chip->dev = dev;
	chip->card = card;
	chip->setup = device_setup[idx];
600
	chip->autoclock = autoclock;
601
	atomic_set(&chip->active, 1); /* avoid autopm during probing */
602
603
	atomic_set(&chip->usage_count, 0);
	atomic_set(&chip->shutdown, 0);
Daniel Mack's avatar
Daniel Mack committed
604

605
	chip->usb_id = usb_id;
Daniel Mack's avatar
Daniel Mack committed
606
	INIT_LIST_HEAD(&chip->pcm_list);
607
	INIT_LIST_HEAD(&chip->ep_list);
Daniel Mack's avatar
Daniel Mack committed
608
609
610
	INIT_LIST_HEAD(&chip->midi_list);
	INIT_LIST_HEAD(&chip->mixer_list);

611
	card->private_free = snd_usb_audio_free;
Daniel Mack's avatar
Daniel Mack committed
612
613
614
615
616
617

	strcpy(card->driver, "USB-Audio");
	sprintf(component, "USB%04x:%04x",
		USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
	snd_component_add(card, component);

618
619
	usb_audio_make_shortname(dev, chip, quirk);
	usb_audio_make_longname(dev, chip, quirk);
Daniel Mack's avatar
Daniel Mack committed
620
621
622
623
624
625
626

	snd_usb_audio_create_proc(chip);

	*rchip = chip;
	return 0;
}

627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/* look for a matching quirk alias id */
static bool get_alias_id(struct usb_device *dev, unsigned int *id)
{
	int i;
	unsigned int src, dst;

	for (i = 0; i < ARRAY_SIZE(quirk_alias); i++) {
		if (!quirk_alias[i] ||
		    sscanf(quirk_alias[i], "%x:%x", &src, &dst) != 2 ||
		    src != *id)
			continue;
		dev_info(&dev->dev,
			 "device (%04x:%04x): applying quirk alias %04x:%04x\n",
			 USB_ID_VENDOR(*id), USB_ID_PRODUCT(*id),
			 USB_ID_VENDOR(dst), USB_ID_PRODUCT(dst));
		*id = dst;
		return true;
	}

	return false;
}

649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
static bool check_delayed_register_option(struct snd_usb_audio *chip, int iface)
{
	int i;
	unsigned int id, inum;

	for (i = 0; i < ARRAY_SIZE(delayed_register); i++) {
		if (delayed_register[i] &&
		    sscanf(delayed_register[i], "%x:%x", &id, &inum) == 2 &&
		    id == chip->usb_id)
			return inum != iface;
	}

	return false;
}

664
static const struct usb_device_id usb_audio_ids[]; /* defined below */
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683

/* look for the corresponding quirk */
static const struct snd_usb_audio_quirk *
get_alias_quirk(struct usb_device *dev, unsigned int id)
{
	const struct usb_device_id *p;

	for (p = usb_audio_ids; p->match_flags; p++) {
		/* FIXME: this checks only vendor:product pair in the list */
		if ((p->match_flags & USB_DEVICE_ID_MATCH_DEVICE) ==
		    USB_DEVICE_ID_MATCH_DEVICE &&
		    p->idVendor == USB_ID_VENDOR(id) &&
		    p->idProduct == USB_ID_PRODUCT(id))
			return (const struct snd_usb_audio_quirk *)p->driver_info;
	}

	return NULL;
}

Daniel Mack's avatar
Daniel Mack committed
684
685
686
687
688
689
690
691
692
693
/*
 * probe the active usb device
 *
 * note that this can be called multiple times per a device, when it
 * includes multiple audio control interfaces.
 *
 * thus we check the usb device pointer and creates the card instance
 * only at the first time.  the successive calls of this function will
 * append the pcm interface to the corresponding card.
 */
694
695
static int usb_audio_probe(struct usb_interface *intf,
			   const struct usb_device_id *usb_id)
Daniel Mack's avatar
Daniel Mack committed
696
{
697
698
699
	struct usb_device *dev = interface_to_usbdev(intf);
	const struct snd_usb_audio_quirk *quirk =
		(const struct snd_usb_audio_quirk *)usb_id->driver_info;
Daniel Mack's avatar
Daniel Mack committed
700
	struct snd_usb_audio *chip;
701
	int i, err;
Daniel Mack's avatar
Daniel Mack committed
702
703
704
705
706
707
708
709
	struct usb_host_interface *alts;
	int ifnum;
	u32 id;

	alts = &intf->altsetting[0];
	ifnum = get_iface_desc(alts)->bInterfaceNumber;
	id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
		    le16_to_cpu(dev->descriptor.idProduct));
710
711
	if (get_alias_id(dev, &id))
		quirk = get_alias_quirk(dev, id);
Daniel Mack's avatar
Daniel Mack committed
712
	if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
713
		return -ENXIO;
Daniel Mack's avatar
Daniel Mack committed
714

715
	err = snd_usb_apply_boot_quirk(dev, intf, quirk, id);
716
717
	if (err < 0)
		return err;
Daniel Mack's avatar
Daniel Mack committed
718
719
720
721
722
723
724
725
726
727

	/*
	 * found a config.  now register to ALSA
	 */

	/* check whether it's already registered */
	chip = NULL;
	mutex_lock(&register_mutex);
	for (i = 0; i < SNDRV_CARDS; i++) {
		if (usb_chip[i] && usb_chip[i]->dev == dev) {
728
			if (atomic_read(&usb_chip[i]->shutdown)) {
729
				dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n");
730
				err = -EIO;
Daniel Mack's avatar
Daniel Mack committed
731
732
733
				goto __error;
			}
			chip = usb_chip[i];
734
			atomic_inc(&chip->active); /* avoid autopm */
Daniel Mack's avatar
Daniel Mack committed
735
736
737
738
			break;
		}
	}
	if (! chip) {
739
740
		err = snd_usb_apply_boot_quirk_once(dev, intf, quirk, id);
		if (err < 0)
741
			goto __error;
742

Daniel Mack's avatar
Daniel Mack committed
743
744
745
746
		/* it's a fresh one.
		 * now look for an empty slot and create a new card instance
		 */
		for (i = 0; i < SNDRV_CARDS; i++)
747
			if (!usb_chip[i] &&
Daniel Mack's avatar
Daniel Mack committed
748
749
			    (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
			    (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
750
751
752
753
754
755
756
757
758
759
760
761
				if (enable[i]) {
					err = snd_usb_audio_create(intf, dev, i, quirk,
								   id, &chip);
					if (err < 0)
						goto __error;
					break;
				} else if (vid[i] != -1 || pid[i] != -1) {
					dev_info(&dev->dev,
						 "device (%04x:%04x) is disabled\n",
						 USB_ID_VENDOR(id),
						 USB_ID_PRODUCT(id));
					err = -ENOENT;
Daniel Mack's avatar
Daniel Mack committed
762
					goto __error;
763
				}
Daniel Mack's avatar
Daniel Mack committed
764
765
			}
		if (!chip) {
766
			dev_err(&dev->dev, "no available usb audio device\n");
767
			err = -ENODEV;
Daniel Mack's avatar
Daniel Mack committed
768
769
770
			goto __error;
		}
	}
771
772
773
774
775
776
777

	if (chip->num_interfaces >= MAX_CARD_INTERFACES) {
		dev_info(&dev->dev, "Too many interfaces assigned to the single USB-audio card\n");
		err = -EINVAL;
		goto __error;
	}

778
	dev_set_drvdata(&dev->dev, chip);
Daniel Mack's avatar
Daniel Mack committed
779

780
781
782
783
784
785
786
	/*
	 * For devices with more than one control interface, we assume the
	 * first contains the audio controls. We might need a more specific
	 * check here in the future.
	 */
	if (!chip->ctrl_intf)
		chip->ctrl_intf = alts;
787

788
789
790
791
	chip->txfr_quirk = 0;
	err = 1; /* continue */
	if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
		/* need some special handlings */
792
793
		err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk);
		if (err < 0)
794
795
796
			goto __error;
	}

Daniel Mack's avatar
Daniel Mack committed
797
798
	if (err > 0) {
		/* create normal USB audio interfaces */
799
800
801
802
803
		err = snd_usb_create_streams(chip, ifnum);
		if (err < 0)
			goto __error;
		err = snd_usb_create_mixer(chip, ifnum, ignore_ctl_error);
		if (err < 0)
Daniel Mack's avatar
Daniel Mack committed
804
805
806
			goto __error;
	}

807
808
809
810
811
812
813
	if (chip->need_delayed_register) {
		dev_info(&dev->dev,
			 "Found post-registration device assignment: %08x:%02x\n",
			 chip->usb_id, ifnum);
		chip->need_delayed_register = false; /* clear again */
	}

814
815
816
	/* we are allowed to call snd_card_register() many times, but first
	 * check to see if a device needs to skip it or do anything special
	 */
817
818
	if (!snd_usb_registration_quirk(chip, ifnum) &&
	    !check_delayed_register_option(chip, ifnum)) {
819
820
821
822
		err = snd_card_register(chip->card);
		if (err < 0)
			goto __error;
	}
Daniel Mack's avatar
Daniel Mack committed
823

824
825
826
827
828
	if (quirk && quirk->shares_media_device) {
		/* don't want to fail when snd_media_device_create() fails */
		snd_media_device_create(chip, intf);
	}

Daniel Mack's avatar
Daniel Mack committed
829
	usb_chip[chip->index] = chip;
830
	chip->intf[chip->num_interfaces] = intf;
Daniel Mack's avatar
Daniel Mack committed
831
	chip->num_interfaces++;
832
	usb_set_intfdata(intf, chip);
833
	atomic_dec(&chip->active);
Daniel Mack's avatar
Daniel Mack committed
834
	mutex_unlock(&register_mutex);
835
	return 0;
Daniel Mack's avatar
Daniel Mack committed
836
837

 __error:
838
	if (chip) {
839
840
841
842
		/* chip->active is inside the chip->card object,
		 * decrement before memory is possibly returned.
		 */
		atomic_dec(&chip->active);
843
844
845
		if (!chip->num_interfaces)
			snd_card_free(chip->card);
	}
Daniel Mack's avatar
Daniel Mack committed
846
	mutex_unlock(&register_mutex);
847
	return err;
Daniel Mack's avatar
Daniel Mack committed
848
849
850
851
852
853
}

/*
 * we need to take care of counter, since disconnection can be called also
 * many times as well as usb_audio_probe().
 */
854
static void usb_audio_disconnect(struct usb_interface *intf)
Daniel Mack's avatar
Daniel Mack committed
855
{
856
	struct snd_usb_audio *chip = usb_get_intfdata(intf);
Daniel Mack's avatar
Daniel Mack committed
857
	struct snd_card *card;
858
	struct list_head *p;
Daniel Mack's avatar
Daniel Mack committed
859

860
	if (chip == (void *)-1L)
Daniel Mack's avatar
Daniel Mack committed
861
862
863
		return;

	card = chip->card;
864
865

	mutex_lock(&register_mutex);
866
	if (atomic_inc_return(&chip->shutdown) == 1) {
867
		struct snd_usb_stream *as;
868
		struct snd_usb_endpoint *ep;
869
		struct usb_mixer_interface *mixer;
870

871
872
873
874
875
		/* wait until all pending tasks done;
		 * they are protected by snd_usb_lock_shutdown()
		 */
		wait_event(chip->shutdown_wait,
			   !atomic_read(&chip->usage_count));
Daniel Mack's avatar
Daniel Mack committed
876
877
		snd_card_disconnect(card);
		/* release the pcm resources */
878
879
		list_for_each_entry(as, &chip->pcm_list, list) {
			snd_usb_stream_disconnect(as);
Daniel Mack's avatar
Daniel Mack committed
880
		}
881
		/* release the endpoint resources */
882
883
		list_for_each_entry(ep, &chip->ep_list, list) {
			snd_usb_endpoint_release(ep);
884
		}
Daniel Mack's avatar
Daniel Mack committed
885
886
887
888
		/* release the midi resources */
		list_for_each(p, &chip->midi_list) {
			snd_usbmidi_disconnect(p);
		}
889
890
891
892
893
894
895
896
		/*
		 * Nice to check quirk && quirk->shares_media_device and
		 * then call the snd_media_device_delete(). Don't have
		 * access to the quirk here. snd_media_device_delete()
		 * accesses mixer_list
		 */
		snd_media_device_delete(chip);

Daniel Mack's avatar
Daniel Mack committed
897
		/* release mixer resources */
898
899
		list_for_each_entry(mixer, &chip->mixer_list, list) {
			snd_usb_mixer_disconnect(mixer);
Daniel Mack's avatar
Daniel Mack committed
900
		}
901
902
903
904
	}

	chip->num_interfaces--;
	if (chip->num_interfaces <= 0) {
Daniel Mack's avatar
Daniel Mack committed
905
906
907
908
909
910
911
912
		usb_chip[chip->index] = NULL;
		mutex_unlock(&register_mutex);
		snd_card_free_when_closed(card);
	} else {
		mutex_unlock(&register_mutex);
	}
}

913
914
/* lock the shutdown (disconnect) task and autoresume */
int snd_usb_lock_shutdown(struct snd_usb_audio *chip)
915
{
916
	int err;
917

918
919
920
921
922
923
924
925
926
	atomic_inc(&chip->usage_count);
	if (atomic_read(&chip->shutdown)) {
		err = -EIO;
		goto error;
	}
	err = snd_usb_autoresume(chip);
	if (err < 0)
		goto error;
	return 0;
927

928
929
930
 error:
	if (atomic_dec_and_test(&chip->usage_count))
		wake_up(&chip->shutdown_wait);
931
932
933
	return err;
}

934
935
936
937
938
939
940
941
942
943
944
945
/* autosuspend and unlock the shutdown */
void snd_usb_unlock_shutdown(struct snd_usb_audio *chip)
{
	snd_usb_autosuspend(chip);
	if (atomic_dec_and_test(&chip->usage_count))
		wake_up(&chip->shutdown_wait);
}

#ifdef CONFIG_PM

int snd_usb_autoresume(struct snd_usb_audio *chip)
{
946
947
	int i, err;

948
949
	if (atomic_read(&chip->shutdown))
		return -EIO;
950
951
952
953
954
955
956
957
958
959
960
961
962
	if (atomic_inc_return(&chip->active) != 1)
		return 0;

	for (i = 0; i < chip->num_interfaces; i++) {
		err = usb_autopm_get_interface(chip->intf[i]);
		if (err < 0) {
			/* rollback */
			while (--i >= 0)
				usb_autopm_put_interface(chip->intf[i]);
			atomic_dec(&chip->active);
			return err;
		}
	}
963
964
965
	return 0;
}

966
967
void snd_usb_autosuspend(struct snd_usb_audio *chip)
{
968
969
	int i;

970
971
	if (atomic_read(&chip->shutdown))
		return;
972
973
974
975
976
	if (!atomic_dec_and_test(&chip->active))
		return;

	for (i = 0; i < chip->num_interfaces; i++)
		usb_autopm_put_interface(chip->intf[i]);
977
978
}

Daniel Mack's avatar
Daniel Mack committed
979
980
981
982
static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
{
	struct snd_usb_audio *chip = usb_get_intfdata(intf);
	struct snd_usb_stream *as;
983
	struct snd_usb_endpoint *ep;
984
	struct usb_mixer_interface *mixer;
985
	struct list_head *p;
Daniel Mack's avatar
Daniel Mack committed
986
987
988
989

	if (chip == (void *)-1L)
		return 0;

990
	if (!chip->num_suspended_intf++) {
991
		list_for_each_entry(as, &chip->pcm_list, list)
992
			snd_usb_pcm_suspend(as);
993
994
		list_for_each_entry(ep, &chip->ep_list, list)
			snd_usb_endpoint_suspend(ep);
995
996
		list_for_each(p, &chip->midi_list)
			snd_usbmidi_suspend(p);
997
998
		list_for_each_entry(mixer, &chip->mixer_list, list)
			snd_usb_mixer_suspend(mixer);
999
	}
1000

1001
1002
1003
1004
1005
	if (!PMSG_IS_AUTO(message) && !chip->system_suspend) {
		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
		chip->system_suspend = chip->num_suspended_intf;
	}

Daniel Mack's avatar
Daniel Mack committed
1006
1007
1008
	return 0;
}

1009
static int __usb_audio_resume(struct usb_interface *intf, bool reset_resume)
Daniel Mack's avatar
Daniel Mack committed
1010
1011
{
	struct snd_usb_audio *chip = usb_get_intfdata(intf);
1012
	struct snd_usb_stream *as;
1013
	struct usb_mixer_interface *mixer;
1014
	struct list_head *p;
1015
	int err = 0;
Daniel Mack's avatar
Daniel Mack committed
1016
1017
1018

	if (chip == (void *)-1L)
		return 0;
1019

1020
	atomic_inc(&chip->active); /* avoid autopm */
1021
1022
	if (chip->num_suspended_intf > 1)
		goto out;
1023
1024
1025
1026
1027
1028
1029

	list_for_each_entry(as, &chip->pcm_list, list) {
		err = snd_usb_pcm_resume(as);
		if (err < 0)
			goto err_out;
	}

Daniel Mack's avatar
Daniel Mack committed
1030
1031
	/*
	 * ALSA leaves material resumption to user space
1032
	 * we just notify and restart the mixers
Daniel Mack's avatar
Daniel Mack committed
1033
	 */
1034
	list_for_each_entry(mixer, &chip->mixer_list, list) {
1035
		err = snd_usb_mixer_resume(mixer, reset_resume);
1036
1037
1038
		if (err < 0)
			goto err_out;
	}
Daniel Mack's avatar
Daniel Mack committed
1039

1040
1041
1042
1043
	list_for_each(p, &chip->midi_list) {
		snd_usbmidi_resume(p);
	}

1044
1045
 out:
	if (chip->num_suspended_intf == chip->system_suspend) {
1046
		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1047
1048
1049
		chip->system_suspend = 0;
	}
	chip->num_suspended_intf--;
Daniel Mack's avatar
Daniel Mack committed
1050

1051
err_out:
1052
	atomic_dec(&chip->active); /* allow autopm after this point */
1053
	return err;
Daniel Mack's avatar
Daniel Mack committed
1054
}
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064

static int usb_audio_resume(struct usb_interface *intf)
{
	return __usb_audio_resume(intf, false);
}

static int usb_audio_reset_resume(struct usb_interface *intf)
{
	return __usb_audio_resume(intf, true);
}
1065
1066
1067
#else
#define usb_audio_suspend	NULL
#define usb_audio_resume	NULL
1068
#define usb_audio_reset_resume	NULL
Daniel Mack's avatar
Daniel Mack committed
1069
1070
#endif		/* CONFIG_PM */

1071
static const struct usb_device_id usb_audio_ids [] = {
Daniel Mack's avatar
Daniel Mack committed
1072
1073
1074
1075
1076
1077
#include "quirks-table.h"
    { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
      .bInterfaceClass = USB_CLASS_AUDIO,
      .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
    { }						/* Terminating entry */
};
1078
MODULE_DEVICE_TABLE(usb, usb_audio_ids);
Daniel Mack's avatar
Daniel Mack committed
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089

/*
 * entry point for linux usb interface
 */

static struct usb_driver usb_audio_driver = {
	.name =		"snd-usb-audio",
	.probe =	usb_audio_probe,
	.disconnect =	usb_audio_disconnect,
	.suspend =	usb_audio_suspend,
	.resume =	usb_audio_resume,
1090
	.reset_resume =	usb_audio_reset_resume,
Daniel Mack's avatar
Daniel Mack committed
1091
	.id_table =	usb_audio_ids,
1092
	.supports_autosuspend = 1,
Daniel Mack's avatar
Daniel Mack committed
1093
1094
};

1095
module_usb_driver(usb_audio_driver);