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